Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why gcc 4.x default reserve 8 bytes for stack on linux when calling a method?

as a beginner of asm, I am checking gcc -S generated asm code to learn.

why gcc 4.x default reserve 8 bytes for stack when calling a method?

func18 is the empty function with no return no param no local var defined. I can't figure out why 8 bytes is reserved here (neither any forum/site mention for the reason, ppl seems take it for granted) is it for the %ebp just push? or return type?! many thx!

      .globl _func18
  _func18:
     pushl   %ebp 
     movl    %esp, %ebp 
     subl    $8, %esp 
     .text 
like image 220
nikcname Avatar asked Mar 08 '10 03:03

nikcname


1 Answers

Some instructions require certain data types to be aligned to as much as a 16-byte boundary (in particular, the SSE data type __m128). To meet this requirement, gcc ensures that the stack is initially 16-byte aligned, and allocates stack space in multiples of 16 bytes. If only a 4-byte return address and 4-byte frame pointer need to be pushed, 8 additional bytes are needed to keep the stack aligned to a 16-byte boundary. However, if gcc determines that the additional alignment is unnecessary (i.e. the fancy data types are not used and no external functions are called), then it may omit any additional instructions used to align the stack. The analysis necessary to determine this may require certain optimization passes to be performed.

See also the gcc documentation for the option -mpreferred-stack-boundary=num.

like image 183
mark4o Avatar answered Oct 11 '22 00:10

mark4o