x86 64 Bit Stack Boundaries
While inspecting disassembled code on my Macbook Pro, I couldn’t figure out why the stack allocated so much space when I only requested a little. After some investigation into GCC I figured it out.
According to Apple’s developer documentation on GCC, “If -mpreferred-stack-boundary is not specified, the default is 4 (16 bytes or 128 bits).” That means that the stack will always allocate chunks in factors of 16 (2^4) unless you specify otherwise.
That means that the following code
int main(int argc, char **argv) { char buf[1]; return 0; }
Translates to allocating 16 bytes on the stack even though it’s only 1 byte.
... 0x0000000100000ef2 : sub rsp,0x10 # 16 bytes ...
Update
This is not specific to Mac, all 64 bit platforms use this stack alignment. Here is the Windows documentation and here is the Linux documentation. Thanks Pascal!
Comments are closed.