linux 下分离 debuginfo 到独立文件.

来源:互联网 发布:阿里云 appscan 编辑:程序博客网 时间:2024/06/03 22:39

linux 下分离 debuginfo 到独立文件.


gcc -ggdb utmp.c -o utmpreadelf -S utmp# 创建一个包含 debuginfo 文件objcopy --only-keep-debug utmp utmp.debug# 添加一个包含路径文件的 .gnu_debuglink section, 文件必须存在.objcopy --add-gnu-debuglink=utmp.debug utmp# 查看 .gnu_debuglink sectionobjdump -s -j .gnu_debuglink utmputmp:     file format elf64-x86-64Contents of section .gnu_debuglink: 0000 75746d70 2e646562 75670000 0068651a  utmp.debug...he.# objcopy --strip-debug utmp指定gdb 加载 debuginfo 即可 gdb utmp -s utmp.debugor(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 utmpNotes at offset 0x0000021c with length 0x00000020:  Owner                 Data size       Description  GNU                  0x00000010       NT_GNU_ABI_TAG (ABI version tag)    OS: Linux, ABI: 2.6.18Notes at offset 0x0000023c with length 0x00000024:  Owner                 Data size       Description  GNU                  0x00000014       NT_GNU_BUILD_ID (unique build ID bitstring)    Build ID: 3b756a4a68a963bcc368ff7174da8a0fae61c37freadelf  -t utmp  |grep build-id  [ 3] .note.gnu.build-idreadelf --wide --sections utmp |grep build  [ 3] .note.gnu.build-id NOTE            000000000040023c 00023c 000024 00   A  0   0  4objdump -s -j .note.gnu.build-id utmputmp:     file format elf64-x86-64Contents 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-directoryThe directory where separate debug symbols are searched for is "/usr/lib/debug".# 修改debuginfo为关联build-id debuginforeadelf -n utmp |grep Build    Build ID: 3b756a4a68a963bcc368ff7174da8a0fae61c37fmkdir -p /usr/lib/debug/.build-id/3bmv utmp.debug  /usr/lib/debug/.build-id/3b/756a4a68a963bcc368ff7174da8a0fae61c37f.debug(gdb) file utmpReading symbols from /mnt/hgfs/DPDK/dsw/utmp...Reading symbols from /usr/lib/debug/.build-id/3b/756a4a68a963bcc368ff7174da8a0fae61c37f.debug...done.done.

参考:

https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html

0 0
原创粉丝点击