c - 即使使用 -gdwarf-2,GCC 4.8 也会在编译单元 header 中插入版本 4

标签 c gcc elf dwarf

我用 GCC 4.8 编译了一个应用程序,我试图在没有 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 的编译单元 header :

/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

我是不是漏掉了什么?如果它不会生成可以由仅支持 DWARF-2 的旧 GDB 版本调试的二进制文件,那么使用 -gdwarf-2 选项有什么意义?

编辑: 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 不再对此提示。

最佳答案

This happens even if you compile a minimal C program as follows:

即使是这个最小的程序也会静态链接部分 libc(即 crt1.ocrtbegin.o 等)。

您应该验证具有版本 4 的编译单元确实来自您的程序,而不是来自库(只需查看它们的 DW_AT_nameDW_AT_comp_dir)。

我的 gcc-4.8:gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4 在我要求时生成版本 2:

gcc -g -c t.c
readelf -wi t.o | grep -A2 'Compilation Unit'
  Compilation Unit @ offset 0x0:
   Length:        0x4e (32-bit)
   Version:       4

gcc -gdwarf-2 -c t.c
readelf -wi t.o | grep -A2 'Compilation Unit'
  Compilation Unit @ offset 0x0:
   Length:        0x52 (32-bit)
   Version:       2

如果版本 4 对象真的只是 crt1.o 或类似的对象,请注意您可以安全地对这些对象运行 strip -g -- 您不会损失太多(除非您必须调试 libc 启动问题,这是不太可能的)。

关于c - 即使使用 -gdwarf-2,GCC 4.8 也会在编译单元 header 中插入版本 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31975376/

相关文章:

c - 非静态变量初始化

c - 为什么 C 程序(.exe)停止工作?

c - gcc 编译的 main() 中无用序言的动机,禁用它?

linux - 是否可以修改 Sprite 的 .rodata 部分中 char* 的值?

c - ELF 的代理共享库(sharedlib、shlib 等)?

c++ - Windows CreateFile 可能的错误代码

c - "expr1 ? expr 2 : expr 3"的解释?

python - 无法安装 Orange : "error: command ' clang' failed with exit status 1"

c++ - 我可以扩展一个参数包并用它定义一个参数列表吗?

android - 如何在没有可写和可执行段的情况下为Android O编译.so?如何设置特定的ELF权限?