Here's some information about my system:
- Ubuntu 22.04.3 running on Intel x86_64;
- ggc version 11.4.0;
I've noticed that the rsp register is not decremented in leaf functions as it is in non leaf functions.
For example consider this C program in a file test.c :
int fx(){
int x = 30;
int y = 34;
int z = 45;
return 30;
}
int main(){
int a = 10;
int b = 20;
int c = fx();
}
After compiling it with "gcc test.c -fno-stack-protector -o test" I run the "objdump -dw -M suffix test" and I get :
0000000000001129 <fx>:
1129: f3 0f 1e fa endbr64
112d: 55 pushq %rbp
112e: 48 89 e5 movq %rsp,%rbp
1131: c7 45 fc 1e 00 00 00 movl $0x1e,-0x4(%rbp)
1138: c7 45 f8 22 00 00 00 movl $0x22,-0x8(%rbp)
113f: c7 45 f4 2d 00 00 00 movl $0x2d,-0xc(%rbp)
1146: b8 1e 00 00 00 movl $0x1e,%eax
114b: 5d popq %rbp
114c: c3 retq
000000000000114d <main>:
114d: f3 0f 1e fa endbr64
1151: 55 pushq %rbp
1152: 48 89 e5 movq %rsp,%rbp
1155: 48 83 ec 10 subq $0x10,%rsp
1159: c7 45 fc 0a 00 00 00 movl $0xa,-0x4(%rbp)
1160: c7 45 f8 14 00 00 00 movl $0x14,-0x8(%rbp)
1167: b8 00 00 00 00 movl $0x0,%eax
116c: e8 b8 ff ff ff callq 1129 <fx>
1171: 89 45 f4 movl %eax,-0xc(%rbp)
1174: b8 00 00 00 00 movl $0x0,%eax
1179: c9 leaveq
117a: c3 retq
As you can see, in main we have subq $0x10,%rsp but in fx this doesn't happen.
My questions:
Does it have something to do with the System V ABI that gcc follows or it is just an optimization performed by the compiler?
What is the reason that warrants this behavior?
Is there a way to tell the compiler I do not want this behavior in leaf functions?