[root@Betty gdb_-g_test]# cat test.c #include <stdio.h> #include <stdlib.h> int main() { int m = 0, n =9; int k = m+n; printf("k is %d\n", k); m = k + n; printf("m is %d\n", m); n = m-n; printf("n is %d\n", n); return 0; } [root@Betty gdb_-g_test]#
[root@Betty gdb_-g_test]# gcc -g -o test test.c [root@Betty gdb_-g_test]# ll 总用量 12 -rwxr-xr-x 1 root root 7858 8月 12 17:28 test -rw-r--r-- 1 root root 219 8月 12 14:23 test.c确认 MD5 校验值
[root@Betty gdb_-g_test]# md5sum test 23e4cda9e7419d2062cb1afd5f5d1069 test确认 test 文件类型信息
[root@Betty gdb_-g_test]# file test test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped读取二进制文件 段头 信息
[root@Betty gdb_-g_test]# readelf -S test There are 37 section headers, starting at offset 0xcd8: Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [ 1] .interp PROGBITS 0000000000400200 00000200 000000000000001c 0000000000000000 A 0 0 1 [ 2] .note.ABI-tag NOTE 000000000040021c 0000021c 0000000000000020 0000000000000000 A 0 0 4 [ 3] .note.gnu.build-i NOTE 000000000040023c 0000023c 0000000000000024 0000000000000000 A 0 0 4 [ 4] .gnu.hash GNU_HASH 0000000000400260 00000260 000000000000001c 0000000000000000 A 5 0 8 [ 5] .dynsym DYNSYM 0000000000400280 00000280 0000000000000060 0000000000000018 A 6 1 8 [ 6] .dynstr STRTAB 00000000004002e0 000002e0 000000000000003f 0000000000000000 A 0 0 1 [ 7] .gnu.version VERSYM 0000000000400320 00000320 0000000000000008 0000000000000002 A 5 0 2 [ 8] .gnu.version_r VERNEED 0000000000400328 00000328 0000000000000020 0000000000000000 A 6 1 8 [ 9] .rela.dyn RELA 0000000000400348 00000348 0000000000000018 0000000000000018 A 5 0 8 [10] .rela.plt RELA 0000000000400360 00000360 0000000000000030 0000000000000018 A 5 12 8 [11] .init PROGBITS 0000000000400390 00000390 0000000000000018 0000000000000000 AX 0 0 4 [12] .plt PROGBITS 00000000004003a8 000003a8 0000000000000030 0000000000000010 AX 0 0 4 [13] .text PROGBITS 00000000004003e0 000003e0 0000000000000248 0000000000000000 AX 0 0 16 [14] .fini PROGBITS 0000000000400628 00000628 000000000000000e 0000000000000000 AX 0 0 4 [15] .rodata PROGBITS 0000000000400638 00000638 000000000000002b 0000000000000000 A 0 0 8 [16] .eh_frame_hdr PROGBITS 0000000000400664 00000664 0000000000000024 0000000000000000 A 0 0 4 [17] .eh_frame PROGBITS 0000000000400688 00000688 000000000000007c 0000000000000000 A 0 0 8 [18] .ctors PROGBITS 0000000000600708 00000708 0000000000000010 0000000000000000 WA 0 0 8 [19] .dtors PROGBITS 0000000000600718 00000718 0000000000000010 0000000000000000 WA 0 0 8 [20] .jcr PROGBITS 0000000000600728 00000728 0000000000000008 0000000000000000 WA 0 0 8 [21] .dynamic DYNAMIC 0000000000600730 00000730 0000000000000190 0000000000000010 WA 6 0 8 [22] .got PROGBITS 00000000006008c0 000008c0 0000000000000008 0000000000000008 WA 0 0 8 [23] .got.plt PROGBITS 00000000006008c8 000008c8 0000000000000028 0000000000000008 WA 0 0 8 [24] .data PROGBITS 00000000006008f0 000008f0 0000000000000004 0000000000000000 WA 0 0 4 [25] .bss NOBITS 00000000006008f8 000008f4 0000000000000010 0000000000000000 WA 0 0 8 [26] .comment PROGBITS 0000000000000000 000008f4 000000000000002d 0000000000000001 MS 0 0 1 [27] .debug_aranges PROGBITS 0000000000000000 00000921 0000000000000030 0000000000000000 0 0 1 [28] .debug_pubnames PROGBITS 0000000000000000 00000951 000000000000001b 0000000000000000 0 0 1 [29] .debug_info PROGBITS 0000000000000000 0000096c 00000000000000be 0000000000000000 0 0 1 [30] .debug_abbrev PROGBITS 0000000000000000 00000a2a 000000000000004e 0000000000000000 0 0 1 [31] .debug_line PROGBITS 0000000000000000 00000a78 0000000000000044 0000000000000000 0 0 1 [32] .debug_str PROGBITS 0000000000000000 00000abc 00000000000000a9 0000000000000001 MS 0 0 1 [33] .debug_pubtypes PROGBITS 0000000000000000 00000b65 0000000000000012 0000000000000000 0 0 1 [34] .shstrtab STRTAB 0000000000000000 00000b77 000000000000015e 0000000000000000 0 0 1 [35] .symtab SYMTAB 0000000000000000 00001618 00000000000006a8 0000000000000018 36 53 8 [36] .strtab STRTAB 0000000000000000 00001cc0 00000000000001f2 0000000000000000 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific) [root@Betty gdb_-g_test]#
b.将 test 中的调试信息拷贝到 test.debug
[root@Betty gdb_-g_test]# objcopy --only-keep-debug test test.debug [root@Betty gdb_-g_test]# ll 总用量 20 -rwxr-xr-x 1 root root 7858 8月 12 17:28 test -rw-r--r-- 1 root root 219 8月 12 14:23 test.c -rwxr-xr-x 1 root root 6146 8月 12 17:31 test.debug可以看到 test 文件的 MD5 值没有被改变
[root@Betty gdb_-g_test]# md5sum test 23e4cda9e7419d2062cb1afd5f5d1069 test可以看到 test.debug 和 test 的文件类型信息相同
[root@Betty gdb_-g_test]# file test.debug test.debug: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped [root@Betty gdb_-g_test]#同样可以从 test.debug 中获得段头信息
[root@Betty gdb_-g_test]# readelf -S test.debug There are 37 section headers, starting at offset 0x628: Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [ 1] .interp NOBITS 0000000000400200 00000200 000000000000001c 0000000000000000 A 0 0 1 [ 2] .note.ABI-tag NOTE 000000000040021c 00000200 0000000000000020 0000000000000000 A 0 0 4 [ 3] .note.gnu.build-i NOTE 000000000040023c 00000220 0000000000000024 0000000000000000 A 0 0 4 [ 4] .gnu.hash NOBITS 0000000000400260 00000244 000000000000001c 0000000000000000 A 0 0 8 [ 5] .dynsym NOBITS 0000000000400280 00000244 0000000000000060 0000000000000018 A 0 1 8 [ 6] .dynstr NOBITS 00000000004002e0 00000244 000000000000003f 0000000000000000 A 0 0 1 [ 7] .gnu.version NOBITS 0000000000400320 00000244 0000000000000008 0000000000000002 A 0 0 2 [ 8] .gnu.version_r NOBITS 0000000000400328 00000244 0000000000000020 0000000000000000 A 0 1 8 [ 9] .rela.dyn NOBITS 0000000000400348 00000244 0000000000000018 0000000000000018 A 0 0 8 [10] .rela.plt NOBITS 0000000000400360 00000244 0000000000000030 0000000000000018 A 0 0 8 [11] .init NOBITS 0000000000400390 00000244 0000000000000018 0000000000000000 AX 0 0 4 [12] .plt NOBITS 00000000004003a8 00000244 0000000000000030 0000000000000010 AX 0 0 4 [13] .text NOBITS 00000000004003e0 00000244 0000000000000248 0000000000000000 AX 0 0 16 [14] .fini NOBITS 0000000000400628 00000244 000000000000000e 0000000000000000 AX 0 0 4 [15] .rodata NOBITS 0000000000400638 00000244 000000000000002b 0000000000000000 A 0 0 8 [16] .eh_frame_hdr NOBITS 0000000000400664 00000244 0000000000000024 0000000000000000 A 0 0 4 [17] .eh_frame NOBITS 0000000000400688 00000244 000000000000007c 0000000000000000 A 0 0 8 [18] .ctors NOBITS 0000000000600708 00000708 0000000000000010 0000000000000000 WA 0 0 8 [19] .dtors NOBITS 0000000000600718 00000708 0000000000000010 0000000000000000 WA 0 0 8 [20] .jcr NOBITS 0000000000600728 00000708 0000000000000008 0000000000000000 WA 0 0 8 [21] .dynamic NOBITS 0000000000600730 00000708 0000000000000190 0000000000000010 WA 0 0 8 [22] .got NOBITS 00000000006008c0 00000708 0000000000000008 0000000000000008 WA 0 0 8 [23] .got.plt NOBITS 00000000006008c8 00000708 0000000000000028 0000000000000008 WA 0 0 8 [24] .data NOBITS 00000000006008f0 00000708 0000000000000004 0000000000000000 WA 0 0 4 [25] .bss NOBITS 00000000006008f8 00000708 0000000000000010 0000000000000000 WA 0 0 8 [26] .comment PROGBITS 0000000000000000 00000244 000000000000002d 0000000000000001 MS 0 0 1 [27] .debug_aranges PROGBITS 0000000000000000 00000271 0000000000000030 0000000000000000 0 0 1 [28] .debug_pubnames PROGBITS 0000000000000000 000002a1 000000000000001b 0000000000000000 0 0 1 [29] .debug_info PROGBITS 0000000000000000 000002bc 00000000000000be 0000000000000000 0 0 1 [30] .debug_abbrev PROGBITS 0000000000000000 0000037a 000000000000004e 0000000000000000 0 0 1 [31] .debug_line PROGBITS 0000000000000000 000003c8 0000000000000044 0000000000000000 0 0 1 [32] .debug_str PROGBITS 0000000000000000 0000040c 00000000000000a9 0000000000000001 MS 0 0 1 [33] .debug_pubtypes PROGBITS 0000000000000000 000004b5 0000000000000012 0000000000000000 0 0 1 [34] .shstrtab STRTAB 0000000000000000 000004c7 000000000000015e 0000000000000000 0 0 1 [35] .symtab SYMTAB 0000000000000000 00000f68 00000000000006a8 0000000000000018 36 53 8 [36] .strtab STRTAB 0000000000000000 00001610 00000000000001f2 0000000000000000 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific) [root@Betty gdb_-g_test]#通过比较工具,可以看到两次得到的段头信息中,只有部分内容发生了变化。
[root@Betty gdb_-g_test]# md5sum test.debug 0f7c5372716becc6eac033e5a76258f5 test.debug [root@Betty gdb_-g_test]#
[root@Betty gdb_-g_test]# ll 总用量 20 -rwxr-xr-x 1 root root 7858 8月 12 17:28 test -rw-r--r-- 1 root root 219 8月 12 14:23 test.c -rwxr-xr-x 1 root root 6146 8月 12 17:31 test.debug对 test 进行调试信息移除
[root@Betty gdb_-g_test]# strip --strip-debug --strip-unneeded test [root@Betty gdb_-g_test]# ll 总用量 20 -rwxr-xr-x 1 root root 4368 8月 12 17:38 test -rw-r--r-- 1 root root 219 8月 12 14:23 test.c -rwxr-xr-x 1 root root 6146 8月 12 17:31 test.debug [root@Betty gdb_-g_test]#确认 test 文件类型的变化
[root@Betty gdb_-g_test]# file test test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped [root@Betty gdb_-g_test]#重新确认 test 文件的 MD5 值
[root@Betty gdb_-g_test]# md5sum test bf725c6a07a17880ab2193c5e98bdec3 test [root@Betty gdb_-g_test]#查看移除调试信息后 test 的 段头 信息
[root@Betty gdb_-g_test]# readelf -S test There are 28 section headers, starting at offset 0xa10: Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [ 1] .interp PROGBITS 0000000000400200 00000200 000000000000001c 0000000000000000 A 0 0 1 [ 2] .note.ABI-tag NOTE 000000000040021c 0000021c 0000000000000020 0000000000000000 A 0 0 4 [ 3] .note.gnu.build-i NOTE 000000000040023c 0000023c 0000000000000024 0000000000000000 A 0 0 4 [ 4] .gnu.hash GNU_HASH 0000000000400260 00000260 000000000000001c 0000000000000000 A 5 0 8 [ 5] .dynsym DYNSYM 0000000000400280 00000280 0000000000000060 0000000000000018 A 6 1 8 [ 6] .dynstr STRTAB 00000000004002e0 000002e0 000000000000003f 0000000000000000 A 0 0 1 [ 7] .gnu.version VERSYM 0000000000400320 00000320 0000000000000008 0000000000000002 A 5 0 2 [ 8] .gnu.version_r VERNEED 0000000000400328 00000328 0000000000000020 0000000000000000 A 6 1 8 [ 9] .rela.dyn RELA 0000000000400348 00000348 0000000000000018 0000000000000018 A 5 0 8 [10] .rela.plt RELA 0000000000400360 00000360 0000000000000030 0000000000000018 A 5 12 8 [11] .init PROGBITS 0000000000400390 00000390 0000000000000018 0000000000000000 AX 0 0 4 [12] .plt PROGBITS 00000000004003a8 000003a8 0000000000000030 0000000000000010 AX 0 0 4 [13] .text PROGBITS 00000000004003e0 000003e0 0000000000000248 0000000000000000 AX 0 0 16 [14] .fini PROGBITS 0000000000400628 00000628 000000000000000e 0000000000000000 AX 0 0 4 [15] .rodata PROGBITS 0000000000400638 00000638 000000000000002b 0000000000000000 A 0 0 8 [16] .eh_frame_hdr PROGBITS 0000000000400664 00000664 0000000000000024 0000000000000000 A 0 0 4 [17] .eh_frame PROGBITS 0000000000400688 00000688 000000000000007c 0000000000000000 A 0 0 8 [18] .ctors PROGBITS 0000000000600708 00000708 0000000000000010 0000000000000000 WA 0 0 8 [19] .dtors PROGBITS 0000000000600718 00000718 0000000000000010 0000000000000000 WA 0 0 8 [20] .jcr PROGBITS 0000000000600728 00000728 0000000000000008 0000000000000000 WA 0 0 8 [21] .dynamic DYNAMIC 0000000000600730 00000730 0000000000000190 0000000000000010 WA 6 0 8 [22] .got PROGBITS 00000000006008c0 000008c0 0000000000000008 0000000000000008 WA 0 0 8 [23] .got.plt PROGBITS 00000000006008c8 000008c8 0000000000000028 0000000000000008 WA 0 0 8 [24] .data PROGBITS 00000000006008f0 000008f0 0000000000000004 0000000000000000 WA 0 0 4 [25] .bss NOBITS 00000000006008f8 000008f4 0000000000000010 0000000000000000 WA 0 0 8 [26] .comment PROGBITS 0000000000000000 000008f4 000000000000002d 0000000000000001 MS 0 0 1 [27] .shstrtab STRTAB 0000000000000000 00000921 00000000000000ee 0000000000000000 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific) [root@Betty gdb_-g_test]#对边前后两次信息变化
[root@Betty gdb_-g_test]# gdb -s test.debug -e test GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6) Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /root/workspace/GDB_TEST/gdb_-g_test/test.debug...done. (gdb)也可以这样加载
[root@Betty gdb_-g_test]# gdb GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6) Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. (gdb) exec-file test (gdb) symbol-file test.debug Reading symbols from /root/workspace/GDB_TEST/gdb_-g_test/test.debug...done. (gdb)
e.将分离出的调试信息 test.debug 链接回可执行文件 test 中
备份并确认 MD5 值
[root@Betty gdb_-g_test]# cp test test.bak [root@Betty gdb_-g_test]# ll 总用量 28 -rwxr-xr-x 1 root root 4368 8月 12 17:38 test -rwxr-xr-x 1 root root 4368 8月 12 17:52 test.bak -rw-r--r-- 1 root root 219 8月 12 14:23 test.c -rwxr-xr-x 1 root root 6146 8月 12 17:31 test.debug [root@Betty gdb_-g_test]# md5sum test bf725c6a07a17880ab2193c5e98bdec3 test [root@Betty gdb_-g_test]# md5sum test.bak bf725c6a07a17880ab2193c5e98bdec3 test.bak
将调试信息链回 test
[root@Betty gdb_-g_test]# objcopy --add-gnu-debuglink test.debug test [root@Betty gdb_-g_test]# ll 总用量 28 -rwxr-xr-x 1 root root 4464 8月 12 17:53 test -rwxr-xr-x 1 root root 4368 8月 12 17:52 test.bak -rw-r--r-- 1 root root 219 8月 12 14:23 test.c -rwxr-xr-x 1 root root 6146 8月 12 17:31 test.debug
产看文件类型变化,以及 MD5 值变化
[root@Betty gdb_-g_test]# file test test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped [root@Betty gdb_-g_test]# md5sum test a201d3195542a0897f633d187776d65c test确认链回后 test 的 段头 内容变化
[root@Betty gdb_-g_test]# readelf -S test There are 29 section headers, starting at offset 0xa30: Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [ 1] .interp PROGBITS 0000000000400200 00000200 000000000000001c 0000000000000000 A 0 0 1 [ 2] .note.ABI-tag NOTE 000000000040021c 0000021c 0000000000000020 0000000000000000 A 0 0 4 [ 3] .note.gnu.build-i NOTE 000000000040023c 0000023c 0000000000000024 0000000000000000 A 0 0 4 [ 4] .gnu.hash GNU_HASH 0000000000400260 00000260 000000000000001c 0000000000000000 A 5 0 8 [ 5] .dynsym DYNSYM 0000000000400280 00000280 0000000000000060 0000000000000018 A 6 1 8 [ 6] .dynstr STRTAB 00000000004002e0 000002e0 000000000000003f 0000000000000000 A 0 0 1 [ 7] .gnu.version VERSYM 0000000000400320 00000320 0000000000000008 0000000000000002 A 5 0 2 [ 8] .gnu.version_r VERNEED 0000000000400328 00000328 0000000000000020 0000000000000000 A 6 1 8 [ 9] .rela.dyn RELA 0000000000400348 00000348 0000000000000018 0000000000000018 A 5 0 8 [10] .rela.plt RELA 0000000000400360 00000360 0000000000000030 0000000000000018 A 5 12 8 [11] .init PROGBITS 0000000000400390 00000390 0000000000000018 0000000000000000 AX 0 0 4 [12] .plt PROGBITS 00000000004003a8 000003a8 0000000000000030 0000000000000010 AX 0 0 4 [13] .text PROGBITS 00000000004003e0 000003e0 0000000000000248 0000000000000000 AX 0 0 16 [14] .fini PROGBITS 0000000000400628 00000628 000000000000000e 0000000000000000 AX 0 0 4 [15] .rodata PROGBITS 0000000000400638 00000638 000000000000002b 0000000000000000 A 0 0 8 [16] .eh_frame_hdr PROGBITS 0000000000400664 00000664 0000000000000024 0000000000000000 A 0 0 4 [17] .eh_frame PROGBITS 0000000000400688 00000688 000000000000007c 0000000000000000 A 0 0 8 [18] .ctors PROGBITS 0000000000600708 00000708 0000000000000010 0000000000000000 WA 0 0 8 [19] .dtors PROGBITS 0000000000600718 00000718 0000000000000010 0000000000000000 WA 0 0 8 [20] .jcr PROGBITS 0000000000600728 00000728 0000000000000008 0000000000000000 WA 0 0 8 [21] .dynamic DYNAMIC 0000000000600730 00000730 0000000000000190 0000000000000010 WA 6 0 8 [22] .got PROGBITS 00000000006008c0 000008c0 0000000000000008 0000000000000008 WA 0 0 8 [23] .got.plt PROGBITS 00000000006008c8 000008c8 0000000000000028 0000000000000008 WA 0 0 8 [24] .data PROGBITS 00000000006008f0 000008f0 0000000000000004 0000000000000000 WA 0 0 4 [25] .bss NOBITS 00000000006008f8 000008f4 0000000000000010 0000000000000000 WA 0 0 8 [26] .comment PROGBITS 0000000000000000 000008f4 000000000000002d 0000000000000001 MS 0 0 1 [27] .gnu_debuglink PROGBITS 0000000000000000 00000921 0000000000000010 0000000000000000 0 0 1 [28] .shstrtab STRTAB 0000000000000000 00000931 00000000000000fd 0000000000000000 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific) [root@Betty gdb_-g_test]#可以看到执行后,多了 .gnu_debuglink 段信息。