我使用GCC 4.8编译了一个应用程序,现在想在没有GDB 7.5+的旧系统上进行调试(据说GDB 7.5+增加了对DWARF-4的支持)。但是升级该系统上的GDB并不可行。由于GDB输出以下消息,我无法进行调试:
Dwarf Error: wrong version in compilation unit header (is 4, should be 2) [in module a.out]
我尝试按照其他问题中建议的使用-gdwarf-2和-gstrict-dwarf进行编译,但编译器仍然会插入几个版本为4的编译单元头。
/tmp> readelf --debug-dump=info a.out | grep -A2 'Compilation Unit @'
readelf: Warning: CU at offset 6b contains corrupt or unsupported version number: 4.
readelf: Warning: CU at offset 1eb contains corrupt or unsupported version number: 4.
Compilation Unit @ offset 0x0:
Length: 0x67 (32-bit)
Version: 2
--
Compilation Unit @ offset 0x6b:
Length: 0x84 (32-bit)
Version: 4
--
Compilation Unit @ offset 0xf3:
Length: 0x62 (32-bit)
Version: 2
--
Compilation Unit @ offset 0x159:
Length: 0x8e (32-bit)
Version: 2
--
Compilation Unit @ offset 0x1eb:
Length: 0x136 (32-bit)
Version: 4
--
Compilation Unit @ offset 0x325:
Length: 0x62 (32-bit)
Version: 2
即使您按照以下方式编译最小的C程序,仍会发生此情况:
/home/MuchToLearn/src> cat main.c
int main(void)
{
return 0;
}
/home/MuchToLearn/src> gcc -gdwarf-2 -gstrict-dwarf main.c
我有点不明白,如果使用-gdwarf-2选项无法生成可以由只支持DWARF-2的旧版GDB进行调试的二进制文件,那么这个选项的意义是什么?
编辑:Employed Russian的回答是正确的。版本4编译单元来自/usr/lib/crt1.o和/usr/lib/libc_nonshared.a。为了解决这个问题,我将它们复制到本地目录并使用strip -g删除它们的调试符号。然后,我按以下方式链接可执行文件:
ld -o main -dynamic-linker /lib/ld-linux.so.2 crt1.o /usr/lib/crti.o main.o /lib/libc.so.6 libc_nonshared.a /usr/lib/crtn.o
生成的可执行文件不包含任何版本为4的编译单元,GDB不再抱怨。
printf()
。 - MuchToLearn