bug-glibc
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

The stack is not correctly dobble word aligned


From: Jens Wallner
Subject: The stack is not correctly dobble word aligned
Date: Thu, 1 Feb 2001 13:06:24 +0100 (MET)

----------
X-Sun-Data-Type: text
X-Sun-Data-Description: text
X-Sun-Data-Name: text
X-Sun-Charset: us-ascii
X-Sun-Content-Lines: 43

Hi guys,

on Linux i386 the startup code in sysdeps/i386/elf/start.S caused
a misaligned stack when using the gcc C/C++ compiler. You can easily
check this out with the following program:

#include<stdio.h>
main() {
  double x;
  printf( "This value must be zero: %li\n" , ((long)&x) & 0x7 );
}

In sysdeps/i386/elf/start.S you have implemented a stack alignment
but the stack bias is still wrong. Let me explain why:

>       /* Before pushing the arguments align the stack to a double word
>          boundary to avoid penalties from misaligned accesses.  Thanks
>          to Edward Seidl <seidl@janed.com> for pointing this out.  */
>       andl $0xfffffff8, %esp
>       pushl %eax              /* Push garbage because we allocate
>                                  28 more bytes.  */

7 words (28 bytes) plus one garbage word (dummy) are pushed before
calling the user's main function. Please note that the call stores
the return address on the stack. The gcc compiler expects this return
address on 0x???????c or 0x???????4 with -mpreferred-stack-boundary=3
(double word boundary).
So the extra push is counterproductive in this case.

Just deleting this extra push gives dramatical speed gains
when a program uses double stack variables extensively.
We found one that runs up to 4 times faster (!) with the
correct stack alignment.

To let the gcc option -mpreferred-stack-boundary=4 make sense one
should increase the stack alignment to quad word boundary. But on our
tested systems (Pentium II/III, Athlon) we found no speed gain, so
this may be less important.

Best regards

Jens Wallner
Nikolaus Meine
----------
X-Sun-Data-Type: default
X-Sun-Data-Name: alignfix.patch
X-Sun-Charset: us-ascii
X-Sun-Content-Lines: 26

*** sysdeps/i386/elf/start.S.orig       Sun Jan 21 15:12:35 2001
--- sysdeps/i386/elf/start.S    Thu Feb  1 12:47:37 2001
***************
*** 53,61 ****
           boundary to avoid penalties from misaligned accesses.  Thanks
           to Edward Seidl <seidl@janed.com> for pointing this out.  */
        andl $0xfffffff8, %esp
-       pushl %eax              /* Push garbage because we allocate
-                                  28 more bytes.  */
  
        /* Provide the highest stack address to the user code (for stacks
           which grow downwards).  */
        pushl %esp
--- 53,64 ----
           boundary to avoid penalties from misaligned accesses.  Thanks
           to Edward Seidl <seidl@janed.com> for pointing this out.  */
        andl $0xfffffff8, %esp
  
+       /* Before calling the user's main function the followed stack
+          modifications must keep the double word boundary. 
+          Note that we allocate 28 more byte plus 4 bytes for the return
+          address. Therefore, there is no extra bias necessary. */
+ 
        /* Provide the highest stack address to the user code (for stacks
           which grow downwards).  */
        pushl %esp



reply via email to

[Prev in Thread] Current Thread [Next in Thread]