我的堆栈帧与传统堆栈帧不同

时间:2016-07-10 20:01:45

标签: c gcc stack gdb

以下是堆栈框架的外观:

(high memory addresses)
    -function arguments
    -return address
    -saved frame pointer
    -local variables
(low memory addresses)

问题是为什么我的堆栈帧看起来像这样:

(high memory addresses)
    -return address
    -saved frame pointer
    -local variables
    -function arguments
(low memory addresses)

我在gdb中调试时注意到了它。 我在C中编码并在Kali Linux x86_64(intel core i7)上使用gcc 5.4.0进行编译。

C代码:

void test_function(int a, int b, int c, int d) {
    int flag;
    char buffer[10];
    flag = 31337;
    buffer[0] = 'A';
}

int main() {
    test_function(1, 2, 3, 4);
    return 0;
}

主要的rbp值:

0x7fffffffe260

test_functtion调用地址后的汇编指令:

0x00000000004004e1

位于test_function框架内的rsp上x命令的结果:

0x7fffffffe240: 0x00000004  0x00000003  0x00000002  0x00000001
0x7fffffffe250: 0x00400441  0x00000000  0x004003b0  0x00007a69
0x7fffffffe260: 0xffffe270  0x00007fff  0x004004e1  0x00000000
0x7fffffffe270: 0x004004f0  0x00000000  0xf7a575f0  0x00007fff

1 个答案:

答案 0 :(得分:0)

看起来堆栈框架的规格在x86和x86_64之间发生了显着变化。您对x86堆栈帧(Intel386 Processor Supplement)是正确的。然而,x86_64规范(AMD64 Architecture Support Supplement)传递寄存器中的整数参数(第3.2.3段)。下面的项目#2:

  
      
  1. 如果类是MEMORY,则在堆栈上传递参数。

  2.   
  3. 如果类是INTEGER,则使用序列rdi,%rsi,%rdx,%rcx,%r8和%r9的下一个可用寄存器13。

  4.   
  5. 如果类是SSE,则使用下一个可用的向量寄存器,寄存器按从%xmm0到%xmm7的顺序获取。

  6.   
  7. 如果该类是SSEUP,则在最后使用的向量寄存器的下一个可用的8字节块中传递8字节。

  8.   
  9. 如果该类是X87,X87UP或COMPLEX_X87,则会在内存中传递。

  10.   

现在,堆栈框架如下所示:

x86_64 stack frame

如果查看堆栈帧,main中的返回地址是8 [%rbp]或0x004005be,参数位于正确的寄存器中:

(gdb) x/32 $rbp
0x7fffffffe040: 0xffffe050  0x00007fff  0x004005be  0x00000000
0x7fffffffe050: 0x00000000  0x00000000  0xf7a36f45  0x00007fff
0x7fffffffe060: 0x00000000  0x00000000  0xffffe138  0x00007fff
0x7fffffffe070: 0x00000000  0x00000001  0x004005a1  0x00000000
0x7fffffffe080: 0x00000000  0x00000000  0xdf5e7534  0x8acdbc8c
0x7fffffffe090: 0x00400470  0x00000000  0xffffe130  0x00007fff
0x7fffffffe0a0: 0x00000000  0x00000000  0x00000000  0x00000000
0x7fffffffe0b0: 0x1f9e7534  0x75324373  0x02a47534  0x753253ca
(gdb) info registers 
rax            0x4005a1 4195745
rbx            0x0  0
rcx            0x4  4
rdx            0x3  3
rsi            0x2  2
rdi            0x1  1
....
相关问题