How rsp decreases in prolog on X86-64 architecture

I am trying to understand how functions are called in C. When I parse this code (gcc - gdb; I'm on Linux with i5-3320M) to get the prologue of the toto function:

 void nop(){return ;} void toto(int i, int j) { return; } int main(int argc, char **argv) { int i = 1; int* pt; i = 0; } 

I get the prologue:

  0x0000000000400523 <+0>: push %rbp 0x0000000000400524 <+1>: mov %rsp,%rbp 0x0000000000400527 <+4>: sub $0x8,%rsp 

Here I do not understand why rsp reduced by 8, since I do not use a local variable in toto . Moreover, if I use a local variable:

 void toto(int i, int j) { int i=1 return; } 

I get the following prologue:

  0x0000000000400523 <+0>: push %rbp 0x0000000000400524 <+1>: mov %rsp,%rbp 0x0000000000400527 <+4>: sub $0x18,%rsp 

And here I do not understand why rsp reduced by 0x18 (24 bytes). I would expect something like 16 bytes, because I already have a mysterious offset of 8, plus I need 4 bytes for int. But my architecture is 64 bits, the word on the stack cannot be less than 8 bytes, so 8 + 8 = 16.

+5
source share
1 answer

X86_64 ABI requires that when entering the function %rsp always be a multiple of 16. Thus, after push %rbp , %rsp a value such as 0x8, 0x18, 0x28, etc. must be subtracted.

UPDATE Sorry to everyone who supported this; I tricked you. It is easy to see that each push %rbp% always paired with call or callq , which gives 0x10 bytes, so the extra value subtracted from %rsp must be a multiple of 0x10.

As for your first question, you should compile without optimization. During optimization, all your functions are minimized to a simple repz retq .

+3
source

Source: https://habr.com/ru/post/1200603/


All Articles