0

I write a small C program just for fun:

struct A {
    int data[64];
};

struct A good(int x, int y) {
    struct A a;
    a.data[x] = y;
    return a;
}

int main() {
    struct A a;
    a = good(10,11);
}

Compile and disassemble it by commands:

$ gcc good.c -g --no-stack-protector
$ gdb -batch -ex 'disassemble good' ./a.out
Dump of assembler code for function good:
   0x0000000000001129 <+0>: endbr64 
   0x000000000000112d <+4>: push   rbp
   0x000000000000112e <+5>: mov    rbp,rsp
   0x0000000000001131 <+8>: push   rbx
   0x0000000000001132 <+9>: sub    rsp,0xa0         <- this line confuse me, why 0xa0?
   0x0000000000001139 <+16>:    mov    QWORD PTR [rbp-0x118],rdi
   0x0000000000001140 <+23>:    mov    DWORD PTR [rbp-0x11c],esi
   0x0000000000001146 <+29>:    mov    DWORD PTR [rbp-0x120],edx
   0x000000000000114c <+35>:    mov    eax,DWORD PTR [rbp-0x11c]
   0x0000000000001152 <+41>:    cdqe   
   ...

Why there is such instruction sub rsp,0xa0? and why the value is 0xa0? (int data[64] should take 0x100 memory space)

Gcc version is:

$ gcc --version
gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0

Architecture is:

$ arch
x86_64

Updated: After adding compile flag -mno-red-zone, 0xa0 is changed to 0x118, almost understandable.

L. Ouyang
  • 505
  • 6
  • 14
  • 1
    Which `gcc` version, which architecture? – Mathieu Jul 19 '22 at 08:18
  • 1
    Think about where local variables are stored, how many you have, and the size needed. Did you calculate 0xa0 into decimal? – the busybee Jul 19 '22 at 08:21
  • 1
    Indeed it is because of red zone, when I add flag ```-mno-red-zone```, 0xa0 is changed to 0x118, and considering the alignment, it is almost understandable. – L. Ouyang Jul 19 '22 at 10:03

0 Answers0