首页UC › Linux下查看.so和可执行文件是否debug编译

Linux下查看.so和可执行文件是否debug编译

今天同事问我, 如何判断一个.so是否是debug编译的.

我记得以前自己是用file来查看一个.so, 根据是否包含”not stripped”来判断该.so是否是debug编译的, 于是就没做实验就回答.

然而, stripped/not stripped并不是debug/release编译的判断标准. 我对debug和release的.so运行file后, 得出几乎相同的输出, 都是not stripped. 所以我算是误导同事了.

根据<Computers Systems – A Programmer’s Perspective>所言, debug/release的区别更多的表现在.so和可执行程序所拥有的段上. debug编译出来的.so和可执行程序拥有很多附加的段来包含哪些debug的信息. 所以查看段信息是一种判断的方式.

我在Ubuntu 11.04上分别对同一个开源软件进行debug和release编译, 并且查看某个.so的段(readelf -S), 发现release编译的只有29个段:

  1. There are 29 section headers, starting at offset 0x1b7d78:
  2. Section Headers:
  3. [Nr] Name Type Address Offset
  4. Size EntSize Flags Link Info Align
  5. [ 0] NULL 0000000000000000 00000000
  6. 0000000000000000 0000000000000000 0 0 0
  7. [ 1] .note.gnu.build-i NOTE 00000000000001c8 000001c8
  8. 0000000000000024 0000000000000000 A 0 0 4
  9. [ 2] .gnu.hash GNU_HASH 00000000000001f0 000001f0
  10. 0000000000003430 0000000000000000 A 3 0 8
  11. [ 3] .dynsym DYNSYM 0000000000003620 00003620
  12. 000000000000b400 0000000000000018 A 4 2 8
  13. [ 4] .dynstr STRTAB 000000000000ea20 0000ea20
  14. 00000000000083e2 0000000000000000 A 0 0 1
  15. [ 5] .gnu.version VERSYM 0000000000016e02 00016e02
  16. 0000000000000f00 0000000000000002 A 3 0 2
  17. [ 6] .gnu.version_r VERNEED 0000000000017d08 00017d08
  18. 0000000000000080 0000000000000000 A 4 2 8
  19. [ 7] .rela.dyn RELA 0000000000017d88 00017d88
  20. 0000000000009570 0000000000000018 A 3 0 8
  21. [ 8] .rela.plt RELA 00000000000212f8 000212f8
  22. 00000000000080d0 0000000000000018 A 3 10 8
  23. [ 9] .init PROGBITS 00000000000293c8 000293c8
  24. 0000000000000018 0000000000000000 AX 0 0 4
  25. [10] .plt PROGBITS 00000000000293e0 000293e0
  26. 00000000000055f0 0000000000000010 AX 0 0 4
  27. [11] .text PROGBITS 000000000002e9d0 0002e9d0
  28. 0000000000144658 0000000000000000 AX 0 0 16
  29. [12] .fini PROGBITS 0000000000173028 00173028
  30. 000000000000000e 0000000000000000 AX 0 0 4
  31. [13] .rodata PROGBITS 0000000000173040 00173040
  32. 000000000001e9a0 0000000000000000 A 0 0 32
  33. [14] .eh_frame_hdr PROGBITS 00000000001919e0 001919e0
  34. 000000000000427c 0000000000000000 A 0 0 4
  35. [15] .eh_frame PROGBITS 0000000000195c60 00195c60
  36. 0000000000017d7c 0000000000000000 A 0 0 8
  37. [16] .ctors PROGBITS 00000000003ae1e8 001ae1e8
  38. 0000000000000010 0000000000000000 WA 0 0 8
  39. [17] .dtors PROGBITS 00000000003ae1f8 001ae1f8
  40. 0000000000000010 0000000000000000 WA 0 0 8
  41. [18] .jcr PROGBITS 00000000003ae208 001ae208
  42. 0000000000000008 0000000000000000 WA 0 0 8
  43. [19] .data.rel.ro PROGBITS 00000000003ae220 001ae220
  44. 0000000000005b38 0000000000000000 WA 0 0 32
  45. [20] .dynamic DYNAMIC 00000000003b3d58 001b3d58
  46. 00000000000001a0 0000000000000010 WA 4 0 8
  47. [21] .got PROGBITS 00000000003b3ef8 001b3ef8
  48. 00000000000010e8 0000000000000008 WA 0 0 8
  49. [22] .got.plt PROGBITS 00000000003b4fe8 001b4fe8
  50. 0000000000002b08 0000000000000008 WA 0 0 8
  51. [23] .data PROGBITS 00000000003b7b00 001b7b00
  52. 0000000000000158 0000000000000000 WA 0 0 32
  53. [24] .bss NOBITS 00000000003b7c58 001b7c58
  54. 0000000000000010 0000000000000000 WA 0 0 8
  55. [25] .comment PROGBITS 0000000000000000 001b7c58
  56. 000000000000002a 0000000000000001 MS 0 0 1
  57. [26] .shstrtab STRTAB 0000000000000000 001b7c82
  58. 00000000000000f5 0000000000000000 0 0 1
  59. [27] .symtab SYMTAB 0000000000000000 001b84b8
  60. 000000000000fdb0 0000000000000018 28 788 8
  61. [28] .strtab STRTAB 0000000000000000 001c8268
  62. 000000000000b8b0 0000000000000000 0 0 1
  63. Key to Flags:
  64. W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
  65. I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  66. O (extra OS processing required) o (OS specific), p (processor specific)

而debug编译的则有38个段, 且所有比release编译多出来的段,都是以.debug_作为开始字符串来命名的:

  1. There are 38 section headers, starting at offset 0xdacc00:
  2. Section Headers:
  3. [Nr] Name Type Address Offset
  4. Size EntSize Flags Link Info Align
  5. [ 0] NULL 0000000000000000 00000000
  6. 0000000000000000 0000000000000000 0 0 0
  7. [ 1] .note.gnu.build-i NOTE 00000000000001c8 000001c8
  8. 0000000000000024 0000000000000000 A 0 0 4
  9. [ 2] .gnu.hash GNU_HASH 00000000000001f0 000001f0
  10. 0000000000003620 0000000000000000 A 3 0 8
  11. [ 3] .dynsym DYNSYM 0000000000003810 00003810
  12. 000000000000c000 0000000000000018 A 4 2 8
  13. [ 4] .dynstr STRTAB 000000000000f810 0000f810
  14. 0000000000008ccb 0000000000000000 A 0 0 1
  15. [ 5] .gnu.version VERSYM 00000000000184dc 000184dc
  16. 0000000000001000 0000000000000002 A 3 0 2
  17. [ 6] .gnu.version_r VERNEED 00000000000194e0 000194e0
  18. 0000000000000070 0000000000000000 A 4 2 8
  19. [ 7] .rela.dyn RELA 0000000000019550 00019550
  20. 0000000000009b40 0000000000000018 A 3 0 8
  21. [ 8] .rela.plt RELA 0000000000023090 00023090
  22. 0000000000008388 0000000000000018 A 3 10 8
  23. [ 9] .init PROGBITS 000000000002b418 0002b418
  24. 0000000000000018 0000000000000000 AX 0 0 4
  25. [10] .plt PROGBITS 000000000002b430 0002b430
  26. 00000000000057c0 0000000000000010 AX 0 0 4
  27. [11] .text PROGBITS 0000000000030bf0 00030bf0
  28. 00000000001d2428 0000000000000000 AX 0 0 16
  29. [12] .fini PROGBITS 0000000000203018 00203018
  30. 000000000000000e 0000000000000000 AX 0 0 4
  31. [13] .rodata PROGBITS 0000000000203040 00203040
  32. 00000000000276b8 0000000000000000 A 0 0 32
  33. [14] .eh_frame_hdr PROGBITS 000000000022a6f8 0022a6f8
  34. 00000000000059dc 0000000000000000 A 0 0 4
  35. [15] .eh_frame PROGBITS 00000000002300d8 002300d8
  36. 000000000001715c 0000000000000000 A 0 0 8
  37. [16] .ctors PROGBITS 0000000000448018 00248018
  38. 0000000000000010 0000000000000000 WA 0 0 8
  39. [17] .dtors PROGBITS 0000000000448028 00248028
  40. 0000000000000010 0000000000000000 WA 0 0 8
  41. [18] .jcr PROGBITS 0000000000448038 00248038
  42. 0000000000000008 0000000000000000 WA 0 0 8
  43. [19] .data.rel.ro PROGBITS 0000000000448040 00248040
  44. 0000000000005b38 0000000000000000 WA 0 0 32
  45. [20] .dynamic DYNAMIC 000000000044db78 0024db78
  46. 00000000000001a0 0000000000000010 WA 4 0 8
  47. [21] .got PROGBITS 000000000044dd18 0024dd18
  48. 00000000000012d0 0000000000000008 WA 0 0 8
  49. [22] .got.plt PROGBITS 000000000044efe8 0024efe8
  50. 0000000000002bf0 0000000000000008 WA 0 0 8
  51. [23] .data PROGBITS 0000000000451be0 00251be0
  52. 0000000000000160 0000000000000000 WA 0 0 32
  53. [24] .bss NOBITS 0000000000451d40 00251d40
  54. 0000000000000040 0000000000000000 WA 0 0 16
  55. [25] .comment PROGBITS 0000000000000000 00251d40
  56. 0000000000000054 0000000000000001 MS 0 0 1
  57. [26] .debug_aranges PROGBITS 0000000000000000 00251d94
  58. 00000000000030c0 0000000000000000 0 0 1
  59. [27] .debug_pubnames PROGBITS 0000000000000000 00254e54
  60. 000000000000b8cb 0000000000000000 0 0 1
  61. [28] .debug_info PROGBITS 0000000000000000 0026071f
  62. 000000000097f69b 0000000000000000 0 0 1
  63. [29] .debug_abbrev PROGBITS 0000000000000000 00bdfdba
  64. 0000000000028211 0000000000000000 0 0 1
  65. [30] .debug_line PROGBITS 0000000000000000 00c07fcb
  66. 0000000000075fbf 0000000000000000 0 0 1
  67. [31] .debug_str PROGBITS 0000000000000000 00c7df8a
  68. 0000000000019789 0000000000000001 MS 0 0 1
  69. [32] .debug_loc PROGBITS 0000000000000000 00c97713
  70. 0000000000043528 0000000000000000 0 0 1
  71. [33] .debug_pubtypes PROGBITS 0000000000000000 00cdac3b
  72. 00000000000d1c97 0000000000000000 0 0 1
  73. [34] .debug_ranges PROGBITS 0000000000000000 00dac8d2
  74. 00000000000001c0 0000000000000000 0 0 1
  75. [35] .shstrtab STRTAB 0000000000000000 00daca92
  76. 000000000000016e 0000000000000000 0 0 1
  77. [36] .symtab SYMTAB 0000000000000000 00dad580
  78. 0000000000014688 0000000000000018 37 1437 8
  79. [37] .strtab STRTAB 0000000000000000 00dc1c08
  80. 000000000000e5a1 0000000000000000 0 0 1
  81. Key to Flags:
  82. W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
  83. I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  84. O (extra OS processing required) o (OS specific), p (processor specific)

对于可执行文件也有类似的区别.

所以这应当是判断debug/release的一个比较正确的方法了.

发表评论