我没有成功地找到这个问题的答案。
使用GDB,我可以使用命令“call”来获取函数的原型。
例子:
(gdb) call fn
$1 = {void (int, int)} 0x8048414 <fn>
因此,GDB 只能从 elf 文件中找出 fn() 返回 void 并接受两个整数作为参数。
但是,我需要使用其他工具从 elf 文件中提取函数原型。最好,我想使用 objdump / readelf。
有谁知道这是否可能?
如果不可能的话GDB是怎么做到的呢?
函数原型存储在 elf 文件的哪个部分?
GDB 通过以下方式知道函数的签名DWARF http://dwarfstd.org/调试信息。readelf -w ELF
会抛弃那个。你可能想读简介
DWARF 调试格式 http://dwarfstd.org/doc/Debugging%20using%20DWARF-2012.pdf作者:迈克尔·J·伊格。使用派尔夫工具 https://github.com/eliben/pyelftools/blob/master/examples/dwarf_die_tree.py您可以通过交互式 Python 会话探索和试验 DWARF。
要提取函数原型,您需要subprogram
调试信息条目。 DWARF 格式教程中的一个示例是:
strndup.c
1: #include "ansidecl.h"
2: #include <stddef.h>
3:
4: extern size_t strlen (const char*);
5: extern PTR malloc (size_t);
6: extern PTR memcpy (PTR, const PTR, size_t);
7:
8: char *
9: strndup (const char *s, size_t n)
10: {
11: char *result;
12: size_t len = strlen (s);
13:
14: if (n < len)
15: len = n;
16:
17: result = (char *) malloc (len + 1);
18: if (!result)
19: return 0;
20:
21: result[len] = '\0';
22: return (char *) memcpy (result, s, len);
23: }
strndup.c 的 DWARF 说明
<1>: DW_TAG_base_type
DW_AT_name = int
DW_AT_byte_size = 4
DW_AT_encoding = signed
<2>: DW_TAG_typedef
DW_AT_name = size_t
DW_AT_type = <3>
<3>: DW_TAG_base_type
DW_AT_name = unsigned int
DW_AT_byte_size = 4
DW_AT_encoding = unsigned
<4>: DW_TAG_base_type
DW_AT_name = long int
DW_AT_byte_size = 4
DW_AT_encoding = signed
<5>: DW_TAG_subprogram
DW_AT_sibling = <10>
DW_AT_external = 1
DW_AT_name = strndup
DW_AT_prototyped = 1
DW_AT_type = <10>
DW_AT_low_pc = 0
DW_AT_high_pc = 0x7b
<6>: DW_TAG_formal_parameter
DW_AT_name = s
DW_AT_type = <12>
DW_AT_location =
(DW_OP_fbreg: 0)
<7>: DW_TAG_formal_parameter
DW_AT_name = n
DW_AT_type = <2>
DW_AT_location =
(DW_OP_fbreg: 4)
<8>: DW_TAG_variable
DW_AT_name = result
DW_AT_type = <10>
DW_AT_location =
(DW_OP_fbreg: -28)
<9>: DW_TAG_variable
DW_AT_name = len
DW_AT_type = <2>
DW_AT_location =
(DW_OP_fbreg: -24)
<10>: DW_TAG_pointer_type
DW_AT_byte_size = 4
DW_AT_type = <11>
<11>: DW_TAG_base_type
DW_AT_name = char
DW_AT_byte_size = 1
DW_AT_encoding =
signed char
<12>: DW_TAG_pointer_type
DW_AT_byte_size = 4
DW_AT_type = <13>
<13>: DW_TAG_const_type
DW_AT_type = <11>
有关更完整的示例实现,请查看这个C反射库 https://github.com/pmachata/reflection作者:彼得·马查塔。它的代码可以执行您想要的操作,但需要注意以下事项:
- 反射在进程内运行,而不是像 GDB 那样在进程外运行
- 这取决于
libdw
and libdwfl
from elfutils https://fedorahosted.org/elfutils/。不确定您对增加这些外部库依赖项有何感受。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)