This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Question about -mpreferred-stack-boundary



We (glibc team) got a bug report that the stack is not aligned
properly - and I'm a bit confused by the documentation of
-mpreferred-stack-boundary which is:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@item -mpreferred-stack-boundary=@var{num}
Attempt to keep the stack boundary aligned to a 2 raised to @var{num}
byte boundary.  If @samp{-mpreferred-stack-boundary} is not specified,
the default is 4 (16 bytes or 128 bits).

The stack is required to be aligned on a 4 byte boundary.  On Pentium
and PentiumPro, @code{double} and @code{long double} values should be
aligned to an 8 byte boundary (see @samp{-malign-double}) or suffer
significant run time performance penalties.  On Pentium III, the
Streaming SIMD Extension (SSE) data type @code{__m128} suffers similar
penalties if it is not 16 byte aligned.

To ensure proper alignment of this values on the stack, the stack boundary
must be as aligned as that required by any value stored on the stack. 
Further, every function must be generated such that it keeps the stack
aligned.  Thus calling a function compiled with a higher preferred
stack boundary from a function compiled with a lower preferred stack
boundary will most likely misalign the stack.  It is recommended that
libraries that use callbacks always use the default setting.

This extra alignment does consume extra stack space.  Code that is sensitive
to stack space usage, such as embedded systems and operating system kernels,
may want to reduce the preferred alignment to
@samp{-mpreferred-stack-boundary=2}.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Who has to align the stack for calls to a function - the caller or the
callee?  In other words:  Does this mean that the stack has to be
aligned before calling a function?  Or does it have to be aligned when
entering a function?

Andreas
-- 
 Andreas Jaeger
  SuSE Labs aj@suse.de
   private aj@arthur.inka.de
    http://www.suse.de/~aj

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]