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]

Re: Question about -mpreferred-stack-boundary


Hi Andreas,

You wrote:
> 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?


To keep the stack aligned there are two capabilities. One way is an 
absolute aligning, here you delete least significant bits of the stack 
pointer (3 bits => double word aligned):

(1)
   andl $0xFFFFFFF8,%esp	// after this you have a aligned stack
				// but if you call a function you need
				// a partameter handling too

The other way is a relative aligning. Before each calling of function
you keep stack aligned with an extra subtration of the stack:

(2)
   subl $4,%esp			// extra subtration 4 bytes
   push %eax			// two paraneter (eax,ebx) 8 bytes 
   push %ebx			
   call _your_function		// don't forget 4 bytes return address

				// => total 32 bytes subtracted 
				//    -> stack is aligned


If you take a look of the compiler's asembler output (gcc xyz.c -S -O2)
you will find no constuctions like I explained in (1). The compiler
use only the relative aligning (2). Therefore in the startup code
(crt1.o) is a absolute aligning implemented.

The relative aligning of the gcc compiler works well. But the from the
startup code is seted a wrong stack bias on my system configuration:

a) x86
b) gcc version 2.95.2.1 19991024 (release)
c) libc.so.6

A wrong stack bias caused a misaligned stack. PLEASE NOTE, on older or 
other systems the stack might be perfectly aligned. You can check your
stack bias which the following short program:

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

You can change the stack bias in:
glibc-2.1/sysdeps/i386/elf/start.S

PLEASE post your system's konfiguration if you have a misaligned
stack!

-- Jens

------------- mailto:wallner@ims.uni-hannover.de  -------------
 
| | | | /   Dipl.-Ing. Jens Wallner
| | | | \   Institute of Microelectronic Systems
| | | | /   Appelstr. 4, D - 30 167 Hannover, Germany

---------------------------------------------------------------

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