gcc x64堆栈操作 [英] gcc x64 stack manipulation

查看:116
本文介绍了gcc x64堆栈操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解gcc x64组织堆栈的方式,一个小程序生成了这个asm

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. 为什么只保存edirsi保留最多0x30字节
  2. 我没有看到 ABI
  3. edirsi保存在增量为0x20-0x14 = 0xC的位置,而不是连续区域,这有意义吗?
  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?

下面是源代码

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;
}

修改 看来堆栈已存储了esi,rdi,addAlltotal的第7个参数调用,它应该占用4x8 = 32(0x20)字节,由于某些原因它会四舍五入到0x30.

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. 我不知道您的原始代码,但是本地变量也存储在堆栈中,并且当您有一些本地变量时,空间也会被分配".同样出于对齐的原因,他可以四舍五入"到下一个16的倍数.我想您有一个本地将结果从addAll传递到printf的地方,并且存储在rbp-04处.

  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.

我刚刚查看了您链接的ABI-在哪里说被叫方必须恢复rdi和rsi?它已经在第15页上说,脚注:

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:

请注意,与Intel386 ABI相反,%rdi和%rsi属于被调用函数,而不是 呼叫者.

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

Afaik它们用于将第一个参数传递给被调用者.

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

0xC是12.这也来自对齐,如您所见,他只需要存储edi而不是rdi,出于对齐的目的,我假设他将其对齐在4个字节的边界上,而si是rsi,这是64位并在8个字节的边界上对齐.

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.

这篇关于gcc x64堆栈操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆