OS X - x64: stack not 16 byte aligned error

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 07:33:29

Like you said, MacOS X has a 16 byte stack alignment, which means that the machine expects each variable on the stack to start on a byte that is a multiple of 16 from the current stack pointer.

When the stack is misaligned, it means we start trying to read variables from the middle of that 16 byte window and usually end up with a segmentation fault.

Before you call a routine in your code, you need to make sure that your stack is aligned correctly; in this case, meaning that the base pointer register is divisible by 16.

subq $8, %rsp               # stack is misaligned by 8 bytes
movq %rdi, 8(%rsp)          #
movq obj_size(%rdi), %rax   #
imul $8, %rax               #
movq %rax, %rdi             #
callq _malloc               # stack is still misaligned when this is called

To fix this, you can subq the %rsp by something like 16 instead of 8.

subq $16, %rsp               # stack is still aligned
movq %rdi, 16(%rsp)          #
...                          #
callq _malloc                # stack is still aligned when this is called, good
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!