我正在尝试在 C++ 中实现一个矮人解析器,而不使用任何外部依赖项。正如dwarf5标准中提到的,调试信息前4个字节或12个字节表示单元长度 基本上是这样的:
unit_length (initial length)
A 4-byte or 12-byte unsigned integer representing the length of the3
.debug_info contribution for that compilation unit, not including the length field itself. In the 32-bit DWARF format, this is a 4-byte unsigned integer (which must be less than 0xfffffff0); in the 64-bit DWARF format, this consists of the 4-byte value 0xffffffff followed by an 8-byte unsigned integer that gives the actual length (see Section 7.4 on page 196).
当我使用 objdump 以十六进制转储 .debug_info 部分时,我得到了这个(请参阅下面的 readelf 输出)。
objdump -s -j .debug_info hello.o
hello.o:文件格式 elf64-x86-64
.debug_info 部分的内容:
0000 01000000 00000000 9a000000 00000000 ................
0010 01000000 00000000 789c9bc6 c0c0c0ca ........x.......
0020 c0c801a4 18984084 2c031a10 42623372 ......@.,...Bb3r
0030 b0832916 0805d1c6 c804e5b1 4178ac20 ..).........Ax.
0040 8a998535 33af04a8 8115498e 05aa2002 ...53.....I... .
0050 8bf18c73 58131918 99394172 4c137318 ...sX....9ArL.s.
0060 180011e5 0560
所以根据这个长度应该是0x01000000但实际长度是0x96。(参见下面的readelf输出)
readelf -wi 你好.o
.debug_info 部分的内容:
编译单元@偏移0:
长度:0x96(32位)
版本:5
单元类型:DW_UT_compile (1)
缩写偏移量:0
指针大小:8
我知道,我错过了一些基本的东西,但即使在多次阅读标准之后。我找不到我的错误。另一件事是,我搜索了一些基本的 dwaf 解析器,以便我可以理解他们在做什么,但找不到任何东西。所有解析器都是大型库,很难理解。如果你们中的任何人至少可以提供一些可读且易于理解的解析器代码,那也会很有帮助。
.debug_info
部分通常是压缩。 objdump -s
显示压缩数据,包括压缩头。您应该自己处理压缩问题。
要验证,请执行以下操作:
objcopy --decompress-debug-sections hello.o hello.d.o
objdump -s -j .debug_info hello.d.o
您应该看到类似的数据
0000 96000000 05000108 ....