如何在linux上区分调试和发布可执行文件

时间:2016-12-29 04:13:39

标签: linux debugging release

我有一个带有调试和发布版本的程序。这是他们的大小:

[root@build]$ ll -h debug release 
-rwxr-xr-x. 1 root root  12M Dec 29 11:49 debug
-rwxr-xr-x. 1 root root 3.6M Dec 29 11:51 release

我想用一两个命令来区分linux上的调试版本和发行版本,但我不知道该怎么做。当我用选项-g编译我的程序时,创建了调试版本。这里的命令是 readelf -S debug 输出:

There are 41 section headers, starting at offset 0xa0bb88:

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         0000000000400238  00000238
       000000000000001c  0000000000000000   A       0     0     1
  [ 2] .note.ABI-tag     NOTE             0000000000400254  00000254
       0000000000000020  0000000000000000   A       0     0     4
  [ 3] .hash             HASH             0000000000400278  00000278
       00000000000132f0  0000000000000004   A       4     0     8
  [ 4] .dynsym           DYNSYM           0000000000413568  00013568
       0000000000042fd8  0000000000000018   A       5     1     8
  [ 5] .dynstr           STRTAB           0000000000456540  00056540
       00000000000dfa70  0000000000000000   A       0     0     1
  [ 6] .gnu.version      VERSYM           0000000000535fb0  00135fb0
       0000000000005952  0000000000000002   A       4     0     2
  [ 7] .gnu.version_r    VERNEED          000000000053b908  0013b908
       0000000000000250  0000000000000000   A       5     9     8
  [ 8] .rela.dyn         RELA             000000000053bb58  0013bb58
       0000000000000c78  0000000000000018   A       4     0     8
  [ 9] .rela.plt         RELA             000000000053c7d0  0013c7d0
       0000000000002958  0000000000000018   A       4    11     8
  [10] .init             PROGBITS         000000000053f128  0013f128
       0000000000000018  0000000000000000  AX       0     0     4
  [11] .plt              PROGBITS         000000000053f140  0013f140
       0000000000001ba0  0000000000000010  AX       0     0     4
  [12] .text             PROGBITS         0000000000540ce0  00140ce0
       0000000000136368  0000000000000000  AX       0     0     16
  [13] .fini             PROGBITS         0000000000677048  00277048
       000000000000000e  0000000000000000  AX       0     0     4
  [14] .rodata           PROGBITS         0000000000677080  00277080
       0000000000024480  0000000000000000   A       0     0     64
  [15] .eh_frame_hdr     PROGBITS         000000000069b500  0029b500
       00000000000111ec  0000000000000000   A       0     0     4
  [16] .eh_frame         PROGBITS         00000000006ac6f0  002ac6f0
       000000000005058c  0000000000000000   A       0     0     8
  [17] .gcc_except_table PROGBITS         00000000006fcc7c  002fcc7c
       000000000000a850  0000000000000000   A       0     0     4
  [18] .tdata            PROGBITS         00000000009074d0  003074d0
       0000000000000004  0000000000000000 WAT       0     0     8
  [19] .tbss             NOBITS           00000000009074d8  003074d4
       000000000001b4d8  0000000000000000 WAT       0     0     8
  [20] .preinit_array    PREINIT_ARRAY    00000000009074d8  003074d8
       0000000000000008  0000000000000000  WA       0     0     8
  [21] .ctors            PROGBITS         00000000009074e0  003074e0
       0000000000000158  0000000000000000  WA       0     0     8
  [22] .dtors            PROGBITS         0000000000907638  00307638
       0000000000000010  0000000000000000  WA       0     0     8
  [23] .jcr              PROGBITS         0000000000907648  00307648
       0000000000000008  0000000000000000  WA       0     0     8
  [24] .data.rel.ro      PROGBITS         0000000000907680  00307680
       0000000000004990  0000000000000000  WA       0     0     64
  [25] .dynamic          DYNAMIC          000000000090c010  0030c010
       0000000000000240  0000000000000010  WA       5     0     8
  [26] .got              PROGBITS         000000000090c250  0030c250
       0000000000000310  0000000000000008  WA       0     0     8
  [27] .got.plt          PROGBITS         000000000090c560  0030c560
       0000000000000de0  0000000000000008  WA       0     0     8
  [28] .data             PROGBITS         000000000090d340  0030d340
       0000000000000fe0  0000000000000000  WA       0     0     64
  [29] .bss              NOBITS           000000000090e340  0030e320
       0000000000c18a38  0000000000000000  WA       0     0     64
  [30] .comment          PROGBITS         0000000000000000  0030e320
       000000000000003e  0000000000000001  MS       0     0     1
  [31] .debug_aranges    PROGBITS         0000000000000000  0030e35e
       00000000000200b0  0000000000000000           0     0     1
  [32] .debug_info       PROGBITS         0000000000000000  0032e40e
       000000000033aded  0000000000000000           0     0     1
  [33] .debug_abbrev     PROGBITS         0000000000000000  006691fb
       00000000000296ae  0000000000000000           0     0     1
  [34] .debug_line       PROGBITS         0000000000000000  006928a9
       0000000000068038  0000000000000000           0     0     1
  [35] .debug_str        PROGBITS         0000000000000000  006fa8e1
       00000000001afb89  0000000000000001  MS       0     0     1
  [36] .debug_loc        PROGBITS         0000000000000000  008aa46a
       000000000010b830  0000000000000000           0     0     1
  [37] .debug_ranges     PROGBITS         0000000000000000  009b5c9a
       0000000000055d70  0000000000000000           0     0     1
  [38] .shstrtab         STRTAB           0000000000000000  00a0ba0a
       000000000000017b  0000000000000000           0     0     1
  [39] .symtab           SYMTAB           0000000000000000  00a0c5c8
       00000000000562f8  0000000000000018          40   3277     8
  [40] .strtab           STRTAB           0000000000000000  00a628c0
       00000000000fa11b  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)

如果没有-g选项,则会创建发布版本,此处为 readelf -S release 输出:

    There are 39 section headers, starting at offset 0x3290b8:

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         0000000000400238  00000238
       000000000000001c  0000000000000000   A       0     0     1
  [ 2] .note.ABI-tag     NOTE             0000000000400254  00000254
       0000000000000020  0000000000000000   A       0     0     4
  [ 3] .hash             HASH             0000000000400278  00000278
       0000000000008558  0000000000000004   A       4     0     8
  [ 4] .dynsym           DYNSYM           00000000004087d0  000087d0
       0000000000019f98  0000000000000018   A       5     1     8
  [ 5] .dynstr           STRTAB           0000000000422768  00022768
       000000000003d80b  0000000000000000   A       0     0     1
  [ 6] .gnu.version      VERSYM           000000000045ff74  0005ff74
       00000000000022a2  0000000000000002   A       4     0     2
  [ 7] .gnu.version_r    VERNEED          0000000000462218  00062218
       0000000000000200  0000000000000000   A       5     8     8
  [ 8] .rela.dyn         RELA             0000000000462418  00062418
       0000000000000840  0000000000000018   A       4     0     8
  [ 9] .rela.plt         RELA             0000000000462c58  00062c58
       00000000000020e8  0000000000000018   A       4    11     8
  [10] .init             PROGBITS         0000000000464d40  00064d40
       0000000000000018  0000000000000000  AX       0     0     4
  [11] .plt              PROGBITS         0000000000464d58  00064d58
       0000000000001600  0000000000000010  AX       0     0     4
  [12] .text             PROGBITS         0000000000466360  00066360
       00000000000e6a28  0000000000000000  AX       0     0     16
  [13] .fini             PROGBITS         000000000054cd88  0014cd88
       000000000000000e  0000000000000000  AX       0     0     4
  [14] .rodata           PROGBITS         000000000054cdc0  0014cdc0
       000000000001d3f0  0000000000000000   A       0     0     64
  [15] .eh_frame_hdr     PROGBITS         000000000056a1b0  0016a1b0
       00000000000068e4  0000000000000000   A       0     0     4
  [16] .eh_frame         PROGBITS         0000000000570a98  00170a98
       0000000000025e7c  0000000000000000   A       0     0     8
  [17] .gcc_except_table PROGBITS         0000000000596914  00196914
       0000000000009914  0000000000000000   A       0     0     4
  [18] .tbss             NOBITS           00000000007a0228  001a0228
       0000000000000950  0000000000000000 WAT       0     0     8
  [19] .ctors            PROGBITS         00000000007a0228  001a0228
       0000000000000130  0000000000000000  WA       0     0     8
  [20] .dtors            PROGBITS         00000000007a0358  001a0358
       0000000000000010  0000000000000000  WA       0     0     8
  [21] .jcr              PROGBITS         00000000007a0368  001a0368
       0000000000000008  0000000000000000  WA       0     0     8
  [22] .data.rel.ro      PROGBITS         00000000007a0380  001a0380
       0000000000003bd0  0000000000000000  WA       0     0     64
  [23] .dynamic          DYNAMIC          00000000007a3f50  001a3f50
       0000000000000210  0000000000000010  WA       5     0     8
  [24] .got              PROGBITS         00000000007a4160  001a4160
       0000000000000158  0000000000000008  WA       0     0     8
  [25] .got.plt          PROGBITS         00000000007a42b8  001a42b8
       0000000000000b10  0000000000000008  WA       0     0     8
  [26] .data             PROGBITS         00000000007a4e00  001a4e00
       00000000000007c0  0000000000000000  WA       0     0     64
  [27] .bss              NOBITS           00000000007a55c0  001a55c0
       0000000000002038  0000000000000000  WA       0     0     64
  [28] .comment          PROGBITS         0000000000000000  001a55c0
       000000000000003e  0000000000000001  MS       0     0     1
  [29] .debug_aranges    PROGBITS         0000000000000000  001a55fe
       0000000000000e10  0000000000000000           0     0     1
  [30] .debug_info       PROGBITS         0000000000000000  001a640e
       00000000000a1d76  0000000000000000           0     0     1
  [31] .debug_abbrev     PROGBITS         0000000000000000  00248184
       000000000000b09d  0000000000000000           0     0     1
  [32] .debug_line       PROGBITS         0000000000000000  00253221
       00000000000113c8  0000000000000000           0     0     1
  [33] .debug_str        PROGBITS         0000000000000000  002645e9
       000000000001731a  0000000000000001  MS       0     0     1
  [34] .debug_loc        PROGBITS         0000000000000000  0027b903
       00000000000923ad  0000000000000000           0     0     1
  [35] .debug_ranges     PROGBITS         0000000000000000  0030dcb0
       000000000001b2a0  0000000000000000           0     0     1
  [36] .shstrtab         STRTAB           0000000000000000  00328f50
       0000000000000165  0000000000000000           0     0     1
  [37] .symtab           SYMTAB           0000000000000000  00329a78
       0000000000021048  0000000000000018          38   1203     8
  [38] .strtab           STRTAB           0000000000000000  0034aac0
       000000000004b633  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)

两个条件的节标题数不同。看来我无法用这个命令区分这两个版本。还有其他解决方案吗?

1 个答案:

答案 0 :(得分:1)

如果您可以进行重建,请尝试将LOCALVERSION=build_releaseLOCALVERSION=build_debug分别添加到make命令行以分别发布和调试版本。然后,您应该能够找到这些字符串:

strings <kernel image> | fgrep build_

您可能需要确保.config文件中未定义CONFIG_LOCALVERSION

通过查看生成的图像可能很难知道构建意图是什么,除非故意将某些字符串放在那里。这是我上面试图完成的。