GDB 符号来自哪里?

Posted

技术标签:

【中文标题】GDB 符号来自哪里?【英文标题】:Where are GDB symbols coming from? 【发布时间】:2019-05-03 11:35:52 【问题描述】:

当我将 Fedora 28 的 /usr/bin/ls 文件加载到 GDB 中时,我可以访问符号 abformat_init,即使它既不作为字符串出现,也不在二进制文件的符号表中。

$ file /usr/bin/ls
/usr/bin/ls: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=d6d0ea6be508665f5586e90a30819d090710842f, stripped, too many notes (256)
$ readelf -S /usr/bin/ls | grep abformat
$ nm /usr/bin/ls
nm: /usr/bin/ls: no symbols
$ strings /usr/bin/ls | grep abformat
$ gdb /usr/bin/ls
[...]
Reading symbols from /usr/bin/ls...Reading symbols from /usr/bin/ls...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Missing separate debuginfos, use: dnf debuginfo-install coreutils-8.29-7.fc28.x86_64
(gdb) info symbol abformat_init 
abformat_init in section .text of /usr/bin/ls

这个符号从何而来?是否有允许在 GDB 之外提取它们的程序?

【问题讨论】:

【参考方案1】:

TL;DR:

    GDB 读取的 Fedora 二进制文件中有一个特殊的 .gnu_debugdata 压缩部分,其中包含 mini-symbols。 使用eu-readelf -Ws --elf-section /usr/bin/ls可以方便地打印该部分的内容

readelf -S /usr/bin/ls | grep abformat

该命令正在转储。你想要 symbols 代替:

readelf -s /usr/bin/ls | grep abformat
readelf --all /usr/bin/ls | grep abformat

strings /usr/bin/ls | grep abformat

Strings 会尝试猜测您想要什么,并且不会输出在二进制文件中找到的所有字符串。请参阅this blog post 并尝试:

strings -a /usr/bin/ls | grep abformat

更新:我确认了您观察到的结果:abformat 没有出现在任何地方,但 GDB 知道。

原来,有一个.gnu_debugdata压缩部分(描述为here),其中有mini-symbols。

要提取这些数据,通常你会这样做:

objcopy -O binary -j .gnu_debugdata /usr/bin/ls ls.mini.xz

但是,我系统上的 is broken(产生空输出),所以我改用 dd

# You may need to adjust the numbers below from "readelf -WS /usr/bin/ls"
dd if=/usr/bin/ls of=ls.mini.xz bs=1 skip=151896 count=3764
xz -d ls.mini.xz
nm ls.mini | grep abformat

这产生了:

00000000000005db0 t abformat_init

QED。

附加信息:

    令人困惑的 GDB no debugging symbols 在 this bug 中得到解决。 objcopy拒绝复制.gnu_debugdata是this bug的主题。

    一个工具可以方便地转储这些信息:

    eu-readelf -Ws --elf-section /usr/bin/ls | grep abformat 37: 0000000000005db0 593 FUNC LOCAL DEFAULT 14 abformat_init

【讨论】:

不幸的是,它似乎也不起作用:$ strings -a /usr/bin/ls | grep abformat $ readelf -a /usr/bin/ls | grep abformat @JonathanHussey 我想我解决了这个难题。答案已更新。 @JonathanHussey 我再次更新了答案:有一种简单的方法可以获取此信息。【参考方案2】:

是否有程序可以将它们提取到 GDB 之外?

是的,您可以使用nm 来提取符号,但您应该在单独的调试信息文件中查找该符号,因为二进制文件本身已被剥离。

您可以使用readelfobjdump 了解单独的调试信息文件名,请参阅How to know the name and/or path of the debug symbol file which is linked to a binary executable?:

$ objdump -s -j .gnu_debuglink /usr/bin/ls

/usr/bin/ls:     file format elf64-x86-64

Contents of section .gnu_debuglink:
 0000 6c732d38 2e33302d 362e6663 32392e78  ls-8.30-6.fc29.x
 0010 38365f36 342e6465 62756700 5cddcc98  86_64.debug.\...

在 Fedora 29 上,/usr/bin/ls 的单独调试信息文件名为 ls-8.30-6.fc29.x86_64.debug

通常,在 Fedora 上,单独的调试信息会安装到 /usr/lib/debug/ 目录,因此调试信息文件的完整路径是 /usr/lib/debug/usr/bin/ls-8.30-6.fc29.x86_64.debug

现在您可以使用nm 查找符号:

$ nm /usr/lib/debug/usr/bin/ls-8.30-6.fc29.x86_64.debug | grep abformat_init
0000000000006d70 t abformat_init

注意,单独的调试信息应该与debuginfo-install一起安装,这是gdb告诉你的。

【讨论】:

在安装 debuginfo 时知道abformat_init 的来源很简单。这里的难题是弄清楚当 debuginfo not 安装时 GDB 如何知道abformat_init 这也是我不明白的,我只是在回答问题。 GDB 不应该知道abformat_init,因为(no debugging symbols found)...done 奇怪的 gdb 输出应该在sourceware.org/bugzilla/show_bug.cgi?id=19551 中修复。

以上是关于GDB 符号来自哪里?的主要内容,如果未能解决你的问题,请参考以下文章

Windows下CDT GDB调试

GDB:直到命令停止在哪里?

特定于 Cell Linux 环境的 GDB 文档在哪里?

当另一个库具有相同的符号时,gdb 不显示来自共享库的符号

开启内核地址随机化KASLR后, qemu 调试 kernel 不能设置断点

无法理解核心文件分析的 GDB x 命令输出