0

I have a little C program:

#include<stdio.h>

void getInput();
void what();

int main()
{
        getInput();
}

void getInput()
{
        char buffer[6];
        gets(buffer);
        puts(buffer);
}

void what()
{
        printf("What!! No one called me!!\n");
}

And when debugging in GDB, I get(I have not included the code for function what() ):

0x5555555546f0 <main>           push   %rbp                                                                                                                         │
   │0x5555555546f1 <main+1>         mov    %rsp,%rbp                                                                                                                    │
   │0x5555555546f4 <main+4>         mov    $0x0,%eax                                                                                                                    │
   │0x5555555546f9 <main+9>         callq  0x555555554705 <getInput>                                                                                                    │
   │0x5555555546fe <main+14>        mov    $0x0,%eax                                                                                                                    │
   │0x555555554703 <main+19>        pop    %rbp                                                                                                                         │
   │0x555555554704 <main+20>        retq
   |0x555555554705 <getInput>       push   %rbp                                                                                                                         │
   │0x555555554706 <getInput+1>     mov    %rsp,%rbp                                                                                                                    │
   │0x555555554709 <getInput+4>     sub    $0x10,%rsp                                                                                                                  │
  >│0x55555555470d <getInput+8>     lea    -0x6(%rbp),%rax                                                                                                              │
   │0x555555554711 <getInput+12>    mov    %rax,%rdi                                                                                                                    │
   │0x555555554714 <getInput+15>    mov    $0x0,%eax                                                                                                                    │
   │0x555555554719 <getInput+20>    callq  0x5555555545a0 <gets@plt>                                                                                                    │
   │0x55555555471e <getInput+25>    lea    -0x6(%rbp),%rax                                                                                                              │
   │0x555555554722 <getInput+29>    mov    %rax,%rdi                                                                                                                    │
   │0x555555554725 <getInput+32>    callq  0x555555554590 <puts@plt>                                                                                                    │
   │0x55555555472a <getInput+37>    nop                                                                                                                                 │
   │0x55555555472b <getInput+38>    leaveq                                                                                                                              │
   │0x55555555472c <getInput+39>    retq

I believe that sub $0x10,%rsp in getInput() at is used to allocate the memory onto the stack by making the stack pointer to point 16 bytes away from its current position. I wonder why did the compiler allocated 16 bytes of memory on the stack while the C code only asks for 6 bytes? I understand that the very next instruction, lea -0x6(%rbp),%rax, lets the user use only 6 bytes of memory but why did we allocate 16 instead?

7_R3X
  • 3,904
  • 4
  • 25
  • 43
  • 1
    @MattGibson, the alleged duplicate question does not actually answer this question. It does not explain *why* stack alignment is required. – Florian Weimer Jul 26 '17 at 07:26
  • 1
    @FlorianWeimer *"which should produce 16-byte alignment, which needed for SSE instructions"* is one of rational reasons (copied from an answer there!), and the other reason is "because why not, I like my compiler keeping stack aligned". (it may have some performance implications when using stack for local variables, and the amounts of memory today is not a (that big) problem) – Ped7g Jul 26 '17 at 09:02
  • 2
    Most ABI have stack alignment requirements. This isn't limited to certain machine level instructions. This is why the compiler will allocate "more than is needed..." though, since it is required for alignment, it really isn't "unneeded" – David Hoelzer Jul 26 '17 at 11:45

0 Answers0