开发者

gcc x64 stack manipulation

开发者 https://www.devze.com 2023-03-13 13:54 出处:网络
I try to understand the way gcc x64 organize the stack, a small program generate this asm (gdb) disassemble *main

I try to understand the way gcc x64 organize the stack, a small program generate this asm

(gdb) disassemble *main
Dump of assembler code for function main:
0x0000000000400534 <main+0>:    push   rbp
0x0000000000400535 <main+1>:    mov    rbp,rsp
0x0000000000400538 <main+4>:    sub    rsp,0x30
0x000000000040053c <main+8>:    mov    DWORD PTR [rbp-0x14],edi
0x000000000040053f <main+11>:   mov    QWORD PTR [rbp-0x20],rsi
0x0000000000400543 <main+15>:   mov    DWORD PTR [rsp],0x7
0x000000000040054a开发者_运维百科 <main+22>:   mov    r9d,0x6
0x0000000000400550 <main+28>:   mov    r8d,0x5
0x0000000000400556 <main+34>:   mov    ecx,0x4
0x000000000040055b <main+39>:   mov    edx,0x3
0x0000000000400560 <main+44>:   mov    esi,0x2
0x0000000000400565 <main+49>:   mov    edi,0x1
0x000000000040056a <main+54>:   call   0x4004c7 <addAll>
0x000000000040056f <main+59>:   mov    DWORD PTR [rbp-0x4],eax
0x0000000000400572 <main+62>:   mov    esi,DWORD PTR [rbp-0x4]
0x0000000000400575 <main+65>:   mov    edi,0x400688
0x000000000040057a <main+70>:   mov    eax,0x0
0x000000000040057f <main+75>:   call   0x400398 <printf@plt>
0x0000000000400584 <main+80>:   mov    eax,0x0
0x0000000000400589 <main+85>:   leave
0x000000000040058a <main+86>:   ret
  1. Why it reserve up to 0x30 bytes just to save edi and rsi
  2. I don't see any where restore values of edi and rsi as required by ABI
  3. edi and rsi save at position that has delta 0x20 - 0x14 = 0xC, not a continuous region, does it make sense?

follow is source code

int mix(int a,int b,int c,int d,int e,int f, int g){
    return a | b | c | d | e | f |g;
}
int addAll(int a,int b,int c,int d,int e,int f, int g){
    return a+b+c+d+e+f+g+mix(a,b,c,d,e,f,g);
}
int main(int argc,char **argv){
    int total;
    total = addAll(1,2,3,4,5,6,7);
    printf("result is %d\n",total);
    return 0;
}

Edit It's seem that stack has stored esi,rdi, 7th parameter call to addAll and total , it should take 4x8 = 32 (0x20) bytes, it round up to 0x30 for some reasons.


  1. I dont know your original code, but locals are also stored on the stack, and when you have some local variables that space is also "allocated". Also for alignment reason it can be, that he "rounded" to the next multiple of 16. I would guess you have a local for passing the result from your addAll to the printf, and that is stored at rbp-04.

  2. I just had look in your linked ABI - where does it say that the callee has to restore rdi and rsi? It says already on page 15, footnote:

    Note that in contrast to the Intel386 ABI, %rdi, and %rsi belong to the called function, not the caller.

    Afaik they are used for passing the first arguments to the callee.

  3. 0xC are 12. This comes also from alignment, as you can see, he just needs to store edi not rdi, for alignment purpose I assume that he aligns it on a 4 byte border, while si is rsi, which is 64 bit and aligned on 8 byte border.


2: The ABI explicitly says that rdi/rsi need NOT be saved by the called function; see page 15 and footnote 5.

1 and 3: unsure; probably stack alignment issues.

0

精彩评论

暂无评论...
验证码 换一张
取 消