如何查看 ELF 文件的 GNU debuglink 值?

How to see the GNU debuglink value of an ELF file?

所以我可以添加一个link到这样的调试符号文件objcopy --add-gnu-debuglink=$name.dbg $name,但我以后如何再次检索该值?

我检查了 readelf -a 并搜索了 \.dbg 但没有任何运气。同样,我用 objdump -sj .gnu_debuglink 检查了(.gnu_debuglink 是部分)并且可以看到那里的值:

$ objdump -sj .gnu_debuglink helloworld|grep \.dbg
0000 68656c6c 6f776f72 6c642e64 62670000  helloworld.dbg..

但是,是否有命令允许我再次提取和检索准确值(即上例中的 helloworld.dbg)?也就是文件名only ...

我知道我可以在这里使用一些 shell foo,但是存在一个选项来设置这个值但 none 来检索它似乎很奇怪。所以我可能只是错过了它。

像这样的东西应该可以工作:

objcopy --output-target=binary --set-section-flags .gnu_debuglink=alloc \
  --only-section=.gnu_debuglink helloworld helloworld.dbg

--output-target=binary 避免添加 ELF headers。 --set-section-flags .gnu_debuglink=alloc 是必需的,因为 objcopy 默认情况下只写入分配的部分(使用 binary 仿真)。 --only-section=.gnu_debuglink 终于确定了答案。 See this earlier answer.

请注意,生成的文件可能有一个尾随 NUL 字节和四个字节的 CRC,因此需要一些 post-processing 来提取第一个 NUL 字节之前的所有内容(可能使用 head -z -n 1 helloworld.dbg | tr -d '[=16=]' 或其他东西相似)。

可以直接使用readelf

$ readelf --string-dump=.gnu_debuglink helloworld

String dump of section '.gnu_debuglink':
  [     0]  helloworld
  [    1b]  9

不知道第二条是什么意思(好像总是不一样)。要摆脱 header 和偏移量,您可以使用 sed:

$ readelf --string-dump=.gnu_debuglink helloworld | sed -n '/]/{s/.* //;p;q}'
helloworld