gpt4 book ai didi

calling-convention - 从 DWARF 信息中获取调用约定

转载 作者:行者123 更新时间:2023-12-04 11:56:03 27 4
gpt4 key购买 nike

我正在尝试从 DWARF 信息中获取有关调用约定的信息。更具体地说,我想了解哪些寄存器/堆栈位置用于将参数传递给函数。我的问题是,在某些情况下,我从 DWARF 转储中获得了某种错误的信息。我使用的示例是以下“C 代码”:

int __attribute__ ((fastcall)) __attribute__ ((noinline)) mult (int x, int y) {
return x*y;
}

我使用以下命令编译此示例:
gcc -c -g -m32 test.c -o test.o

现在,当我使用以下命令获取矮人转储时:
dwarfdump test.o

我收到有关此功能的以下信息:
< 2><0x00000042>      DW_TAG_formal_parameter
DW_AT_name "x"
DW_AT_decl_file 0x00000001 /home/khaled/Repo_current/trunk/test.c
DW_AT_decl_line 0x00000001
DW_AT_type <0x0000005b>
DW_AT_location DW_OP_fbreg -12
< 2><0x0000004e> DW_TAG_formal_parameter
DW_AT_name "y"
DW_AT_decl_file 0x00000001 /home/khaled/Repo_current/trunk/test.c
DW_AT_decl_line 0x00000001
DW_AT_type <0x0000005b>
DW_AT_location DW_OP_fbreg -16

查看 DW_AT_location 条目,它与框架基础有一些偏移。这意味着它们是内存参数,但实际的调用约定“fastcall”强制将它们传递到寄存器中。通过查看生成的目标文件的反汇编,我可以看到它们从寄存器复制到函数入口点的堆栈位置。有没有办法从矮人转储中知道——或使用任何其他方式——最初在调用时传递参数的位置?

谢谢,

最佳答案

因为您正在使用 gcc -c -g -m32 test.c -o test.o .虽然是fastcall功能,GCC仍然需要生成代码,以便在函数开始时将寄存器中的值保存到堆栈帧中。没有那个,任何调试器或 gdb无法调试程序,否则他们会说参数正在优化但未显示。它使调试变得不可能。

在 x86_64 中,编译器默认也会使用一些寄存器来传递一些参数,即使没有指定属性 fastcall对于一个函数。您还可以发现这些寄存器也被复制到堆栈中。

// x86_64 assembly code
_mult:
Leh_func_begin1:
pushq %rbp
Ltmp0:
movq %rsp, %rbp
Ltmp1:
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl -4(%rbp), %eax
movl -8(%rbp), %ecx
imull %ecx, %eax

如果您打开优化标志 -O , -O2 , -O3 (不管 -g与否),你可以反汇编,发现没有任何东西被复制到堆栈帧。而当你 gdb 优化的可执行文件,并在函数的开头停止显示局部变量时, gdb会告诉你这些参数正在被优化。
dwarfdump 32 位程序的示例看起来像
0x00000083:      TAG_formal_parameter [4]  
AT_name( "x" )
AT_decl_file( "test.c" )
AT_decl_line( 1 )
AT_type( {0x0000005f} ( int ) )
AT_location( 0x00000000
0x00000000 - 0x00000003: ecx
0x00000003 - 0x00000018: ecx )

0x00000090: TAG_formal_parameter [4]
AT_name( "y" )
AT_decl_file( "test.c" )
AT_decl_line( 1 )
AT_type( {0x0000005f} ( int ) )
AT_location( 0x0000001e
0x00000000 - 0x00000003: edx
0x00000003 - 0x00000018: edx )

你会发现生成的汇编代码非常简单和干净。
_mult:
pushl %ebp
movl %esp, %ebp
movl %ecx, %eax
imull %edx, %eax
popl %ebp
ret $12

关于calling-convention - 从 DWARF 信息中获取调用约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12252176/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com