欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Linux下查看.so和可执行文件是否debug编译的方法(必看)

程序员文章站 2022-07-04 18:29:59
今天同事问我,如何判断一个.so是否是debug编译的。 我记得以前自己是用file来查看一个.so,根据是否包含"not stripped"来判断该.so是否是debu...

今天同事问我,如何判断一个.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个段:

there are 29 section headers, starting at offset 0x1b7d78:
section headers:
[nr] name type address offset
size entsize flags link info align
[ 0] null 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .note.gnu.build-i note 00000000000001c8 000001c8
0000000000000024 0000000000000000 a 0 0 4
[ 2] .gnu.hash gnu_hash 00000000000001f0 000001f0
0000000000003430 0000000000000000 a 3 0 8
[ 3] .dynsym dynsym 0000000000003620 00003620
000000000000b400 0000000000000018 a 4 2 8
[ 4] .dynstr strtab 000000000000ea20 0000ea20
00000000000083e2 0000000000000000 a 0 0 1
[ 5] .gnu.version versym 0000000000016e02 00016e02
0000000000000f00 0000000000000002 a 3 0 2
[ 6] .gnu.version_r verneed 0000000000017d08 00017d08
0000000000000080 0000000000000000 a 4 2 8
[ 7] .rela.dyn rela 0000000000017d88 00017d88
0000000000009570 0000000000000018 a 3 0 8
[ 8] .rela.plt rela 00000000000212f8 000212f8
00000000000080d0 0000000000000018 a 3 10 8
[ 9] .init progbits 00000000000293c8 000293c8
0000000000000018 0000000000000000 ax 0 0 4
[10] .plt progbits 00000000000293e0 000293e0
00000000000055f0 0000000000000010 ax 0 0 4
[11] .text progbits 000000000002e9d0 0002e9d0
0000000000144658 0000000000000000 ax 0 0 16
[12] .fini progbits 0000000000173028 00173028
000000000000000e 0000000000000000 ax 0 0 4
[13] .rodata progbits 0000000000173040 00173040
000000000001e9a0 0000000000000000 a 0 0 32
[14] .eh_frame_hdr progbits 00000000001919e0 001919e0
000000000000427c 0000000000000000 a 0 0 4
[15] .eh_frame progbits 0000000000195c60 00195c60
0000000000017d7c 0000000000000000 a 0 0 8
[16] .ctors progbits 00000000003ae1e8 001ae1e8
0000000000000010 0000000000000000 wa 0 0 8
[17] .dtors progbits 00000000003ae1f8 001ae1f8
0000000000000010 0000000000000000 wa 0 0 8
[18] .jcr progbits 00000000003ae208 001ae208
0000000000000008 0000000000000000 wa 0 0 8
[19] .data.rel.ro progbits 00000000003ae220 001ae220
0000000000005b38 0000000000000000 wa 0 0 32
[20] .dynamic dynamic 00000000003b3d58 001b3d58
00000000000001a0 0000000000000010 wa 4 0 8
[21] .got progbits 00000000003b3ef8 001b3ef8
00000000000010e8 0000000000000008 wa 0 0 8
[22] .got.plt progbits 00000000003b4fe8 001b4fe8
0000000000002b08 0000000000000008 wa 0 0 8
[23] .data progbits 00000000003b7b00 001b7b00
0000000000000158 0000000000000000 wa 0 0 32
[24] .bss nobits 00000000003b7c58 001b7c58
0000000000000010 0000000000000000 wa 0 0 8
[25] .comment progbits 0000000000000000 001b7c58
000000000000002a 0000000000000001 ms 0 0 1
[26] .shstrtab strtab 0000000000000000 001b7c82
00000000000000f5 0000000000000000 0 0 1
[27] .symtab symtab 0000000000000000 001b84b8
000000000000fdb0 0000000000000018 28 788 8
[28] .strtab strtab 0000000000000000 001c8268
000000000000b8b0 0000000000000000 0 0 1
key to flags:
w (write), a (alloc), x (execute), m (merge), s (strings), l (large)
i (info), l (link order), g (group), t (tls), e (exclude), x (unknown)
o (extra os processing required) o (os specific), p (processor specific)

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

there are 38 section headers, starting at offset 0xdacc00:
section headers:
[nr] name type address offset
size entsize flags link info align
[ 0] null 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .note.gnu.build-i note 00000000000001c8 000001c8
0000000000000024 0000000000000000 a 0 0 4
[ 2] .gnu.hash gnu_hash 00000000000001f0 000001f0
0000000000003620 0000000000000000 a 3 0 8
[ 3] .dynsym dynsym 0000000000003810 00003810
000000000000c000 0000000000000018 a 4 2 8
[ 4] .dynstr strtab 000000000000f810 0000f810
0000000000008ccb 0000000000000000 a 0 0 1
[ 5] .gnu.version versym 00000000000184dc 000184dc
0000000000001000 0000000000000002 a 3 0 2
[ 6] .gnu.version_r verneed 00000000000194e0 000194e0
0000000000000070 0000000000000000 a 4 2 8
[ 7] .rela.dyn rela 0000000000019550 00019550
0000000000009b40 0000000000000018 a 3 0 8
[ 8] .rela.plt rela 0000000000023090 00023090
0000000000008388 0000000000000018 a 3 10 8
[ 9] .init progbits 000000000002b418 0002b418
0000000000000018 0000000000000000 ax 0 0 4
[10] .plt progbits 000000000002b430 0002b430
00000000000057c0 0000000000000010 ax 0 0 4
[11] .text progbits 0000000000030bf0 00030bf0
00000000001d2428 0000000000000000 ax 0 0 16
[12] .fini progbits 0000000000203018 00203018
000000000000000e 0000000000000000 ax 0 0 4
[13] .rodata progbits 0000000000203040 00203040
00000000000276b8 0000000000000000 a 0 0 32
[14] .eh_frame_hdr progbits 000000000022a6f8 0022a6f8
00000000000059dc 0000000000000000 a 0 0 4
[15] .eh_frame progbits 00000000002300d8 002300d8
000000000001715c 0000000000000000 a 0 0 8
[16] .ctors progbits 0000000000448018 00248018
0000000000000010 0000000000000000 wa 0 0 8
[17] .dtors progbits 0000000000448028 00248028
0000000000000010 0000000000000000 wa 0 0 8
[18] .jcr progbits 0000000000448038 00248038
0000000000000008 0000000000000000 wa 0 0 8
[19] .data.rel.ro progbits 0000000000448040 00248040
0000000000005b38 0000000000000000 wa 0 0 32
[20] .dynamic dynamic 000000000044db78 0024db78
00000000000001a0 0000000000000010 wa 4 0 8
[21] .got progbits 000000000044dd18 0024dd18
00000000000012d0 0000000000000008 wa 0 0 8
[22] .got.plt progbits 000000000044efe8 0024efe8
0000000000002bf0 0000000000000008 wa 0 0 8
[23] .data progbits 0000000000451be0 00251be0
0000000000000160 0000000000000000 wa 0 0 32
[24] .bss nobits 0000000000451d40 00251d40
0000000000000040 0000000000000000 wa 0 0 16
[25] .comment progbits 0000000000000000 00251d40
0000000000000054 0000000000000001 ms 0 0 1
[26] .debug_aranges progbits 0000000000000000 00251d94
00000000000030c0 0000000000000000 0 0 1
[27] .debug_pubnames progbits 0000000000000000 00254e54
000000000000b8cb 0000000000000000 0 0 1
[28] .debug_info progbits 0000000000000000 0026071f
000000000097f69b 0000000000000000 0 0 1
[29] .debug_abbrev progbits 0000000000000000 00bdfdba
0000000000028211 0000000000000000 0 0 1
[30] .debug_line progbits 0000000000000000 00c07fcb
0000000000075fbf 0000000000000000 0 0 1
[31] .debug_str progbits 0000000000000000 00c7df8a
0000000000019789 0000000000000001 ms 0 0 1
[32] .debug_loc progbits 0000000000000000 00c97713
0000000000043528 0000000000000000 0 0 1
[33] .debug_pubtypes progbits 0000000000000000 00cdac3b
00000000000d1c97 0000000000000000 0 0 1
[34] .debug_ranges progbits 0000000000000000 00dac8d2
00000000000001c0 0000000000000000 0 0 1
[35] .shstrtab strtab 0000000000000000 00daca92
000000000000016e 0000000000000000 0 0 1
[36] .symtab symtab 0000000000000000 00dad580
0000000000014688 0000000000000018 37 1437 8
[37] .strtab strtab 0000000000000000 00dc1c08
000000000000e5a1 0000000000000000 0 0 1
key to flags:
w (write), a (alloc), x (execute), m (merge), s (strings), l (large)
i (info), l (link order), g (group), t (tls), e (exclude), x (unknown)
o (extra os processing required) o (os specific), p (processor specific)

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

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

===================================

命令

readelf -s libxxx.so |grep debug

以上这篇linux下查看.so和可执行文件是否debug编译的方法(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。