GDB符号来自哪里?

Where are GDB symbols coming from?

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

$ 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. GDB 读取的 Fedora 二进制文件中有一个特殊的 .gnu_debugdata 压缩部分,其中包含 mini-symbols.
  2. 该部分的内容可以方便地打印 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 compressed 部分(在 这里 描述),其中有 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.

附加信息:

  1. 令人困惑的 GDB no debugging symbolsthis bug 中得到解决。
  2. objcopy拒绝复制.gnu_debugdatathis bug的主题。
  3. 一个可以方便地转储此信息的工具:

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

Is there a program that allows to extract them outside of GDB?

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

您可以使用 readelfobjdump 来了解单独的调试信息文件名,请参阅 :

$ 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 告诉你的。