Why does the Mac ABI require 16-byte stack alignment for x86-32?

前端 未结 10 2084
野趣味
野趣味 2020-11-27 16:01

I can understand this requirement for the old PPC RISC systems and even for x86-64, but for the old tried-and-true x86? In this case, the stack needs to be aligned on 4 byte

相关标签:
10条回答
  • 2020-11-27 16:52

    My guess is that Apple believes everyone just uses XCode (gcc) which aligns the stack for you. So requiring the stack to be aligned so the kernel doesn't have to is just a micro-optimization.

    0 讨论(0)
  • 2020-11-27 16:54

    I believe it's to keep it inline with the x86-64 ABI.

    0 讨论(0)
  • 2020-11-27 16:55

    I am not sure as I don't have first hand proof, but I believe the reason is SSE. SSE is much faster if your buffers are already aligned on a 16 bytes boundary (movps vs movups), and any x86 has at least sse2 for mac os x. It can be taken care of by the application user, but the cost is pretty significant. If the overall cost for making it mandatory in the ABI is not too significant, it may worth it. SSE is used quite pervasively in mac os X: accelerate framework, etc...

    0 讨论(0)
  • 2020-11-27 16:57

    Not sure why no one has considered the possibility of easy portability from legacy PowerPC-based platform?

    Read this:

    http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/LowLevelABI/100-32-bit_PowerPC_Function_Calling_Conventions/32bitPowerPC.html#//apple_ref/doc/uid/TP40002438-SW20

    And then zoomed into "32-bit PowerPC Function Calling Conventions" and finally this:

    "These are the embedding alignment modes available in the 32-bit PowerPC environment:

    Power alignment mode is derived from the alignment rules used by the IBM XLC compiler for the AIX operating system. It is the default alignment mode for the PowerPC-architecture version of GCC used on AIX and Mac OS X. Because this mode is most likely to be compatible between PowerPC-architecture compilers from different vendors, it’s typically used with data structures that are shared between different programs."

    In view of the legacy PowerPC-based background of OSX, portability is a major consideration - it dictates following the convention all the way back to AIX's XLC compiler. When you think in terms of the need to make sure all the tools and applications will work together with minimal rework, I think it is important to stick to the same legacy ABI as far as possible.

    That gives the philosophy, and reading further is the rule explicitly mentioned ("Prolog and Epilog"):

    The called function is responsible for allocating its own stack frame, making sure to preserve 16-byte alignment in the stack. This operation is accomplished by a section of code called the prolog, which the compiler places before the body of the subroutine. After the body of the subroutine, the compiler places an epilog to restore the processor to the state it was prior to the subroutine call.

    0 讨论(0)
提交回复
热议问题