gcc -ggdb utmp.c -o utmp
readelf -S utmp
# 创建一个包含 debuginfo 文件
objcopy --only-keep-debug utmp utmp.debug
# 添加一个包含路径文件的 .gnu_debuglink section, 文件必须存在.
objcopy --add-gnu-debuglink=utmp.debug utmp
# 查看 .gnu_debuglink section
objdump -s -j .gnu_debuglink utmp
utmp: file format elf64-x86-64
Contents of section .gnu_debuglink:
0000 75746d70 2e646562 75670000 0068651a utmp.debug...he.
#
objcopy --strip-debug utmp
指定gdb 加载 debuginfo 即可
gdb utmp -s utmp.debug
or
(gdb) file utmp
(gdb) symbol utmp.debug
Build ID
linux 下二进制执行文件在built阶段都会根据时间戳生成一个唯一build-id, 并加入到一个"gnu.build-id" section中. 而gdb默认搜索debuginfo会搜索指定目录(show debug-file-directory) 下build-id关联的.debug. 默认搜索的文件名为 nn/nnnn...nnnn.debug, 前两个"nn"就是它的build-id前两位,后面的nnnn...nnnn则是build-id的剩余部分.
以下命令都可查看 "gnu.build-id" section 信息.
readelf -n utmp
Notes at offset 0x0000021c with length 0x00000020:
Owner Data size Description
GNU 0x00000010 NT_GNU_ABI_TAG (ABI version tag)
OS: Linux, ABI: 2.6.18
Notes at offset 0x0000023c with length 0x00000024:
Owner Data size Description
GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring)
Build ID: 3b756a4a68a963bcc368ff7174da8a0fae61c37f
readelf -t utmp |grep build-id
[ 3] .note.gnu.build-id
readelf --wide --sections utmp |grep build
[ 3] .note.gnu.build-id NOTE 000000000040023c 00023c 000024 00 A 0 0 4
objdump -s -j .note.gnu.build-id utmp
utmp: file format elf64-x86-64
Contents of section .note.gnu.build-id:
40023c 04000000 14000000 03000000 474e5500 ............GNU.
40024c 3b756a4a 68a963bc c368ff71 74da8a0f ;ujJh.c..h.qt...
40025c ae61c37f .a..
示例:
# 显示当前debuginfo默认搜索目录,也可以通过 "set debug-file-directory path" 重新指定.
(gdb) show debug-file-directory
The directory where separate debug symbols are searched for is "/usr/lib/debug".
# 修改debuginfo为关联build-id debuginfo
readelf -n utmp |grep Build
Build ID: 3b756a4a68a963bcc368ff7174da8a0fae61c37f
mkdir -p /usr/lib/debug/.build-id/3b
mv utmp.debug /usr/lib/debug/.build-id/3b/756a4a68a963bcc368ff7174da8a0fae61c37f.debug
(gdb) file utmp
Reading symbols from /mnt/hgfs/DPDK/dsw/utmp...Reading symbols from /usr/lib/debug/.build-id/3b/756a4a68a963bcc368ff7174da8a0fae61c37f.debug...done.
done.
参考: