文章目录

  • 1 addr2line
  • 2 nm
  • 3 readelf
    • 3.1 -h
    • 3.2 -l
    • 3.3 -S
    • 3.4 -t
    • 3.5 -s
    • 3.6 -n
    • 3.7 -d
  • 4 size
  • 5 objdump
  • 6 strings
  • 7 strip

在嵌入式开发中,如果我们使用GNU的GCC编译器,那么就会有一个比较重要的工具集binutils,该工具集中的一些工具,比较方便我们开发和调试。

指令 描述
addr2line 根据指令地址获取对应的函数、源文件名、行号
nm 列出程序文件中的符号以及在内存中的地址
readelf 显示有关ELF二进制文件的信息
size 查看目标文件中的段大小
objcopy 将一种对象文件翻译成另外一种
objdump 查看程序段信息以及反汇编
strings 查看目标文件中的字符串
strip 剔除可执行程序中的调试信息
ar 将目标文件打包成静态库
ld 链接器
ranlib 生成文件的索引,加快查找速度

1 addr2line

根据地址来获取对应的函数、以及函数所在的源文件和行号。可以在程序异常或崩溃时候,来判断是那个函数引起的。

测试:

#include <stdio.h>int test_func()
{printf("func addr is:%p\n", test_func);}int main()
{test_func();return 0;
}

将上面程序编译,并运行,在编译时候一定要加上-g选项。

# gcc test_binutils.c -o test
# ./test
func addr is:0x400526

打印的地址为0x400526,通过addr2line来获取此地址的信息:

# addr2line 0x400526 -f -e test
test_func
/home/test_demo/test_binutils.c:4

2 nm

列出程序文件中的符号以及符号在内存中的地址。符号包含程序中的函数名以及变量名

以上面生成的test为例:

 #nm -n testw __gmon_start__w _ITM_deregisterTMCloneTablew _ITM_registerTMCloneTablew _Jv_RegisterClassesU __libc_start_main@@GLIBC_2.2.5U printf@@GLIBC_2.2.5
00000000004003c8 T _init
0000000000400430 T _start
0000000000400460 t deregister_tm_clones
00000000004004a0 t register_tm_clones
00000000004004e0 t __do_global_dtors_aux
0000000000400500 t frame_dummy
0000000000400526 T test_func
0000000000400541 T main
0000000000400560 T __libc_csu_init
00000000004005d0 T __libc_csu_fini
00000000004005d4 T _fini
00000000004005e0 R _IO_stdin_used
00000000004005f8 r __GNU_EH_FRAME_HDR
0000000000400748 r __FRAME_END__
0000000000600e10 t __frame_dummy_init_array_entry
0000000000600e10 t __init_array_start
0000000000600e18 t __do_global_dtors_aux_fini_array_entry
0000000000600e18 t __init_array_end
0000000000600e20 d __JCR_END__
0000000000600e20 d __JCR_LIST__
0000000000600e28 d _DYNAMIC
0000000000601000 d _GLOBAL_OFFSET_TABLE_
0000000000601028 D __data_start
0000000000601028 W data_start
0000000000601030 D __dso_handle
0000000000601038 B __bss_start
0000000000601038 b completed.7594
0000000000601038 D _edata
0000000000601038 D __TMC_END__
0000000000601040 B _end

上面一共有三列,第一列是符号在内存中的地址,比如上面的test_func的地址为0x400526;第二列用于表示符号是放在哪一个段中的;第三列则是符号的名称。

第二部分的信息定义如下:

3 readelf

此指令用于显示读取文件中的ELF信息。
其用法如下:

readelf [option] [file]

file为我们要读取的文件,option为选项,具体如下:


注:上面只展示了部分option信息

下面简单记录下展示的信息,以上面生成的test文件为例。

3.1 -h

3.2 -l

# readelf -l testElf file type is EXEC (Executable file)
Entry point 0x400430
There are 9 program headers, starting at offset 64Program Headers:Type           Offset             VirtAddr           PhysAddrFileSiz            MemSiz              Flags  AlignPHDR           0x0000000000000040 0x0000000000400040 0x00000000004000400x00000000000001f8 0x00000000000001f8  R E    8INTERP         0x0000000000000238 0x0000000000400238 0x00000000004002380x000000000000001c 0x000000000000001c  R      1[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]LOAD           0x0000000000000000 0x0000000000400000 0x00000000004000000x000000000000074c 0x000000000000074c  R E    200000LOAD           0x0000000000000e10 0x0000000000600e10 0x0000000000600e100x0000000000000228 0x0000000000000230  RW     200000DYNAMIC        0x0000000000000e28 0x0000000000600e28 0x0000000000600e280x00000000000001d0 0x00000000000001d0  RW     8NOTE           0x0000000000000254 0x0000000000400254 0x00000000004002540x0000000000000044 0x0000000000000044  R      4GNU_EH_FRAME   0x00000000000005f8 0x00000000004005f8 0x00000000004005f80x000000000000003c 0x000000000000003c  R      4GNU_STACK      0x0000000000000000 0x0000000000000000 0x00000000000000000x0000000000000000 0x0000000000000000  RW     10GNU_RELRO      0x0000000000000e10 0x0000000000600e10 0x0000000000600e100x00000000000001f0 0x00000000000001f0  R      1Section to Segment mapping:Segment Sections...00     01     .interp 02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .plt.got .text .fini .rodata .eh_frame_hdr .eh_frame 03     .init_array .fini_array .jcr .dynamic .got .got.plt .data .bss 04     .dynamic 05     .note.ABI-tag .note.gnu.build-id 06     .eh_frame_hdr 07     08     .init_array .fini_array .jcr .dynamic .got

3.3 -S

# readelf -S test
There are 36 section headers, starting at offset 0x1cf8:Section Headers:[Nr] Name              Type             Address           OffsetSize              EntSize          Flags  Link  Info  Align[ 0]                   NULL             0000000000000000  000000000000000000000000  0000000000000000           0     0     0[ 1] .interp           PROGBITS         0000000000400238  00000238000000000000001c  0000000000000000   A       0     0     1[ 2] .note.ABI-tag     NOTE             0000000000400254  000002540000000000000020  0000000000000000   A       0     0     4[ 3] .note.gnu.build-i NOTE             0000000000400274  000002740000000000000024  0000000000000000   A       0     0     4[ 4] .gnu.hash         GNU_HASH         0000000000400298  00000298000000000000001c  0000000000000000   A       5     0     8[ 5] .dynsym           DYNSYM           00000000004002b8  000002b80000000000000060  0000000000000018   A       6     1     8[ 6] .dynstr           STRTAB           0000000000400318  00000318000000000000003f  0000000000000000   A       0     0     1[ 7] .gnu.version      VERSYM           0000000000400358  000003580000000000000008  0000000000000002   A       5     0     2[ 8] .gnu.version_r    VERNEED          0000000000400360  000003600000000000000020  0000000000000000   A       6     1     8[ 9] .rela.dyn         RELA             0000000000400380  000003800000000000000018  0000000000000018   A       5     0     8[10] .rela.plt         RELA             0000000000400398  000003980000000000000030  0000000000000018  AI       5    24     8[11] .init             PROGBITS         00000000004003c8  000003c8000000000000001a  0000000000000000  AX       0     0     4[12] .plt              PROGBITS         00000000004003f0  000003f00000000000000030  0000000000000010  AX       0     0     16[13] .plt.got          PROGBITS         0000000000400420  000004200000000000000008  0000000000000000  AX       0     0     8[14] .text             PROGBITS         0000000000400430  0000043000000000000001a2  0000000000000000  AX       0     0     16[15] .fini             PROGBITS         00000000004005d4  000005d40000000000000009  0000000000000000  AX       0     0     4[16] .rodata           PROGBITS         00000000004005e0  000005e00000000000000015  0000000000000000   A       0     0     4[17] .eh_frame_hdr     PROGBITS         00000000004005f8  000005f8000000000000003c  0000000000000000   A       0     0     4[18] .eh_frame         PROGBITS         0000000000400638  000006380000000000000114  0000000000000000   A       0     0     8[19] .init_array       INIT_ARRAY       0000000000600e10  00000e100000000000000008  0000000000000000  WA       0     0     8[20] .fini_array       FINI_ARRAY       0000000000600e18  00000e180000000000000008  0000000000000000  WA       0     0     8[21] .jcr              PROGBITS         0000000000600e20  00000e200000000000000008  0000000000000000  WA       0     0     8[22] .dynamic          DYNAMIC          0000000000600e28  00000e2800000000000001d0  0000000000000010  WA       6     0     8[23] .got              PROGBITS         0000000000600ff8  00000ff80000000000000008  0000000000000008  WA       0     0     8[24] .got.plt          PROGBITS         0000000000601000  000010000000000000000028  0000000000000008  WA       0     0     8[25] .data             PROGBITS         0000000000601028  000010280000000000000010  0000000000000000  WA       0     0     8[26] .bss              NOBITS           0000000000601038  000010380000000000000008  0000000000000000  WA       0     0     1[27] .comment          PROGBITS         0000000000000000  000010380000000000000035  0000000000000001  MS       0     0     1[28] .debug_aranges    PROGBITS         0000000000000000  0000106d0000000000000030  0000000000000000           0     0     1[29] .debug_info       PROGBITS         0000000000000000  0000109d00000000000000ae  0000000000000000           0     0     1[30] .debug_abbrev     PROGBITS         0000000000000000  0000114b0000000000000042  0000000000000000           0     0     1[31] .debug_line       PROGBITS         0000000000000000  0000118d0000000000000048  0000000000000000           0     0     1[32] .debug_str        PROGBITS         0000000000000000  000011d500000000000000ce  0000000000000001  MS       0     0     1[33] .shstrtab         STRTAB           0000000000000000  00001ba8000000000000014c  0000000000000000           0     0     1[34] .symtab           SYMTAB           0000000000000000  000012a800000000000006d8  0000000000000018          35    52     8[35] .strtab           STRTAB           0000000000000000  000019800000000000000228  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)

3.4 -t

# readelf -t test
There are 36 section headers, starting at offset 0x1cf8:Section Headers:[Nr] NameType              Address          Offset            LinkSize              EntSize          Info              AlignFlags[ 0] NULL                   NULL             0000000000000000  0000000000000000  00000000000000000 0000000000000000  0                 0[0000000000000000]: [ 1] .interpPROGBITS               PROGBITS         0000000000400238  0000000000000238  0000000000000001c 0000000000000000  0                 1[0000000000000002]: ALLOC[ 2] .note.ABI-tagNOTE                   NOTE             0000000000400254  0000000000000254  00000000000000020 0000000000000000  0                 4[0000000000000002]: ALLOC[ 3] .note.gnu.build-idNOTE                   NOTE             0000000000400274  0000000000000274  00000000000000024 0000000000000000  0                 4[0000000000000002]: ALLOC[ 4] .gnu.hashGNU_HASH               GNU_HASH         0000000000400298  0000000000000298  5000000000000001c 0000000000000000  0                 8[0000000000000002]: ALLOC[ 5] .dynsymDYNSYM                 DYNSYM           00000000004002b8  00000000000002b8  60000000000000060 0000000000000018  1                 8[0000000000000002]: ALLOC[ 6] .dynstrSTRTAB                 STRTAB           0000000000400318  0000000000000318  0000000000000003f 0000000000000000  0                 1[0000000000000002]: ALLOC[ 7] .gnu.versionVERSYM                 VERSYM           0000000000400358  0000000000000358  50000000000000008 0000000000000002  0                 2[0000000000000002]: ALLOC[ 8] .gnu.version_rVERNEED                VERNEED          0000000000400360  0000000000000360  60000000000000020 0000000000000000  1                 8[0000000000000002]: ALLOC[ 9] .rela.dynRELA                   RELA             0000000000400380  0000000000000380  50000000000000018 0000000000000018  0                 8[0000000000000002]: ALLOC[10] .rela.pltRELA                   RELA             0000000000400398  0000000000000398  50000000000000030 0000000000000018  24                8[0000000000000042]: ALLOC, INFO LINK[11] .initPROGBITS               PROGBITS         00000000004003c8  00000000000003c8  0000000000000001a 0000000000000000  0                 4[0000000000000006]: ALLOC, EXEC[12] .pltPROGBITS               PROGBITS         00000000004003f0  00000000000003f0  00000000000000030 0000000000000010  0                 16[0000000000000006]: ALLOC, EXEC[13] .plt.gotPROGBITS               PROGBITS         0000000000400420  0000000000000420  00000000000000008 0000000000000000  0                 8[0000000000000006]: ALLOC, EXEC[14] .textPROGBITS               PROGBITS         0000000000400430  0000000000000430  000000000000001a2 0000000000000000  0                 16[0000000000000006]: ALLOC, EXEC[15] .finiPROGBITS               PROGBITS         00000000004005d4  00000000000005d4  00000000000000009 0000000000000000  0                 4[0000000000000006]: ALLOC, EXEC[16] .rodataPROGBITS               PROGBITS         00000000004005e0  00000000000005e0  00000000000000015 0000000000000000  0                 4[0000000000000002]: ALLOC[17] .eh_frame_hdrPROGBITS               PROGBITS         00000000004005f8  00000000000005f8  0000000000000003c 0000000000000000  0                 4[0000000000000002]: ALLOC[18] .eh_framePROGBITS               PROGBITS         0000000000400638  0000000000000638  00000000000000114 0000000000000000  0                 8[0000000000000002]: ALLOC[19] .init_arrayINIT_ARRAY             INIT_ARRAY       0000000000600e10  0000000000000e10  00000000000000008 0000000000000000  0                 8[0000000000000003]: WRITE, ALLOC[20] .fini_arrayFINI_ARRAY             FINI_ARRAY       0000000000600e18  0000000000000e18  00000000000000008 0000000000000000  0                 8[0000000000000003]: WRITE, ALLOC[21] .jcrPROGBITS               PROGBITS         0000000000600e20  0000000000000e20  00000000000000008 0000000000000000  0                 8[0000000000000003]: WRITE, ALLOC[22] .dynamicDYNAMIC                DYNAMIC          0000000000600e28  0000000000000e28  600000000000001d0 0000000000000010  0                 8[0000000000000003]: WRITE, ALLOC[23] .gotPROGBITS               PROGBITS         0000000000600ff8  0000000000000ff8  00000000000000008 0000000000000008  0                 8[0000000000000003]: WRITE, ALLOC[24] .got.pltPROGBITS               PROGBITS         0000000000601000  0000000000001000  00000000000000028 0000000000000008  0                 8[0000000000000003]: WRITE, ALLOC[25] .dataPROGBITS               PROGBITS         0000000000601028  0000000000001028  00000000000000010 0000000000000000  0                 8[0000000000000003]: WRITE, ALLOC[26] .bssNOBITS                 NOBITS           0000000000601038  0000000000001038  00000000000000008 0000000000000000  0                 1[0000000000000003]: WRITE, ALLOC[27] .commentPROGBITS               PROGBITS         0000000000000000  0000000000001038  00000000000000035 0000000000000001  0                 1[0000000000000030]: MERGE, STRINGS[28] .debug_arangesPROGBITS               PROGBITS         0000000000000000  000000000000106d  00000000000000030 0000000000000000  0                 1[0000000000000000]: [29] .debug_infoPROGBITS               PROGBITS         0000000000000000  000000000000109d  000000000000000ae 0000000000000000  0                 1[0000000000000000]: [30] .debug_abbrevPROGBITS               PROGBITS         0000000000000000  000000000000114b  00000000000000042 0000000000000000  0                 1[0000000000000000]: [31] .debug_linePROGBITS               PROGBITS         0000000000000000  000000000000118d  00000000000000048 0000000000000000  0                 1[0000000000000000]: [32] .debug_strPROGBITS               PROGBITS         0000000000000000  00000000000011d5  000000000000000ce 0000000000000001  0                 1[0000000000000030]: MERGE, STRINGS[33] .shstrtabSTRTAB                 STRTAB           0000000000000000  0000000000001ba8  0000000000000014c 0000000000000000  0                 1[0000000000000000]: [34] .symtabSYMTAB                 SYMTAB           0000000000000000  00000000000012a8  3500000000000006d8 0000000000000018  52                8[0000000000000000]: [35] .strtabSTRTAB                 STRTAB           0000000000000000  0000000000001980  00000000000000228 0000000000000000  0                 1[0000000000000000]:

3.5 -s

# readelf -s testSymbol table '.dynsym' contains 4 entries:Num:    Value          Size Type    Bind   Vis      Ndx Name0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5 (2)2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.2.5 (2)3: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__Symbol table '.symtab' contains 73 entries:Num:    Value          Size Type    Bind   Vis      Ndx Name0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 1: 0000000000400238     0 SECTION LOCAL  DEFAULT    1 2: 0000000000400254     0 SECTION LOCAL  DEFAULT    2 3: 0000000000400274     0 SECTION LOCAL  DEFAULT    3 4: 0000000000400298     0 SECTION LOCAL  DEFAULT    4 5: 00000000004002b8     0 SECTION LOCAL  DEFAULT    5 6: 0000000000400318     0 SECTION LOCAL  DEFAULT    6 7: 0000000000400358     0 SECTION LOCAL  DEFAULT    7 8: 0000000000400360     0 SECTION LOCAL  DEFAULT    8 9: 0000000000400380     0 SECTION LOCAL  DEFAULT    9 10: 0000000000400398     0 SECTION LOCAL  DEFAULT   10 11: 00000000004003c8     0 SECTION LOCAL  DEFAULT   11 12: 00000000004003f0     0 SECTION LOCAL  DEFAULT   12 13: 0000000000400420     0 SECTION LOCAL  DEFAULT   13 14: 0000000000400430     0 SECTION LOCAL  DEFAULT   14 15: 00000000004005d4     0 SECTION LOCAL  DEFAULT   15 16: 00000000004005e0     0 SECTION LOCAL  DEFAULT   16 17: 00000000004005f8     0 SECTION LOCAL  DEFAULT   17 18: 0000000000400638     0 SECTION LOCAL  DEFAULT   18 19: 0000000000600e10     0 SECTION LOCAL  DEFAULT   19 20: 0000000000600e18     0 SECTION LOCAL  DEFAULT   20 21: 0000000000600e20     0 SECTION LOCAL  DEFAULT   21 22: 0000000000600e28     0 SECTION LOCAL  DEFAULT   22 23: 0000000000600ff8     0 SECTION LOCAL  DEFAULT   23 24: 0000000000601000     0 SECTION LOCAL  DEFAULT   24 25: 0000000000601028     0 SECTION LOCAL  DEFAULT   25 26: 0000000000601038     0 SECTION LOCAL  DEFAULT   26 27: 0000000000000000     0 SECTION LOCAL  DEFAULT   27 28: 0000000000000000     0 SECTION LOCAL  DEFAULT   28 29: 0000000000000000     0 SECTION LOCAL  DEFAULT   29 30: 0000000000000000     0 SECTION LOCAL  DEFAULT   30 31: 0000000000000000     0 SECTION LOCAL  DEFAULT   31 32: 0000000000000000     0 SECTION LOCAL  DEFAULT   32 33: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c34: 0000000000600e20     0 OBJECT  LOCAL  DEFAULT   21 __JCR_LIST__35: 0000000000400460     0 FUNC    LOCAL  DEFAULT   14 deregister_tm_clones36: 00000000004004a0     0 FUNC    LOCAL  DEFAULT   14 register_tm_clones37: 00000000004004e0     0 FUNC    LOCAL  DEFAULT   14 __do_global_dtors_aux38: 0000000000601038     1 OBJECT  LOCAL  DEFAULT   26 completed.759439: 0000000000600e18     0 OBJECT  LOCAL  DEFAULT   20 __do_global_dtors_aux_fin40: 0000000000400500     0 FUNC    LOCAL  DEFAULT   14 frame_dummy41: 0000000000600e10     0 OBJECT  LOCAL  DEFAULT   19 __frame_dummy_init_array_42: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS test_binutils.c43: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c44: 0000000000400748     0 OBJECT  LOCAL  DEFAULT   18 __FRAME_END__45: 0000000000600e20     0 OBJECT  LOCAL  DEFAULT   21 __JCR_END__46: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS 47: 0000000000600e18     0 NOTYPE  LOCAL  DEFAULT   19 __init_array_end48: 0000000000600e28     0 OBJECT  LOCAL  DEFAULT   22 _DYNAMIC49: 0000000000600e10     0 NOTYPE  LOCAL  DEFAULT   19 __init_array_start50: 00000000004005f8     0 NOTYPE  LOCAL  DEFAULT   17 __GNU_EH_FRAME_HDR51: 0000000000601000     0 OBJECT  LOCAL  DEFAULT   24 _GLOBAL_OFFSET_TABLE_52: 00000000004005d0     2 FUNC    GLOBAL DEFAULT   14 __libc_csu_fini53: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab54: 0000000000601028     0 NOTYPE  WEAK   DEFAULT   25 data_start55: 0000000000601038     0 NOTYPE  GLOBAL DEFAULT   25 _edata56: 00000000004005d4     0 FUNC    GLOBAL DEFAULT   15 _fini57: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@@GLIBC_2.2.558: 0000000000400526    27 FUNC    GLOBAL DEFAULT   14 test_func59: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_60: 0000000000601028     0 NOTYPE  GLOBAL DEFAULT   25 __data_start61: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__62: 0000000000601030     0 OBJECT  GLOBAL HIDDEN    25 __dso_handle63: 00000000004005e0     4 OBJECT  GLOBAL DEFAULT   16 _IO_stdin_used64: 0000000000400560   101 FUNC    GLOBAL DEFAULT   14 __libc_csu_init65: 0000000000601040     0 NOTYPE  GLOBAL DEFAULT   26 _end66: 0000000000400430    42 FUNC    GLOBAL DEFAULT   14 _start67: 0000000000601038     0 NOTYPE  GLOBAL DEFAULT   26 __bss_start68: 0000000000400541    21 FUNC    GLOBAL DEFAULT   14 main69: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses70: 0000000000601038     0 OBJECT  GLOBAL HIDDEN    25 __TMC_END__71: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable72: 00000000004003c8     0 FUNC    GLOBAL DEFAULT   11 _init

3.6 -n

# readelf -n testDisplaying notes found at file offset 0x00000254 with length 0x00000020:Owner                 Data size DescriptionGNU                  0x00000010  NT_GNU_ABI_TAG (ABI version tag)OS: Linux, ABI: 2.6.32Displaying notes found at file offset 0x00000274 with length 0x00000024:Owner                 Data size   DescriptionGNU                  0x00000014  NT_GNU_BUILD_ID (unique build ID bitstring)Build ID: 86febe6b7e9155bf559d0667e18b029d66e5749a

3.7 -d

# readelf -d testDynamic section at offset 0xe28 contains 24 entries:Tag        Type                         Name/Value0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]0x000000000000000c (INIT)               0x4003c80x000000000000000d (FINI)               0x4005d40x0000000000000019 (INIT_ARRAY)         0x600e100x000000000000001b (INIT_ARRAYSZ)       8 (bytes)0x000000000000001a (FINI_ARRAY)         0x600e180x000000000000001c (FINI_ARRAYSZ)       8 (bytes)0x000000006ffffef5 (GNU_HASH)           0x4002980x0000000000000005 (STRTAB)             0x4003180x0000000000000006 (SYMTAB)             0x4002b80x000000000000000a (STRSZ)              63 (bytes)0x000000000000000b (SYMENT)             24 (bytes)0x0000000000000015 (DEBUG)              0x00x0000000000000003 (PLTGOT)             0x6010000x0000000000000002 (PLTRELSZ)           48 (bytes)0x0000000000000014 (PLTREL)             RELA0x0000000000000017 (JMPREL)             0x4003980x0000000000000007 (RELA)               0x4003800x0000000000000008 (RELASZ)             24 (bytes)0x0000000000000009 (RELAENT)            24 (bytes)0x000000006ffffffe (VERNEED)            0x4003600x000000006fffffff (VERNEEDNUM)         10x000000006ffffff0 (VERSYM)             0x4003580x0000000000000000 (NULL)               0x0

4 size

查看目标文件中的段大小

如下:

# size testtext     data     bss     dec     hex filename1261        552       8    1821     71d test

上面显示了代码段、数据段、BSS段的大小,其中.rdata段被分到代码段中;.idata段被划分到数据段中。

使用-A指令可以看到更详细的信息:

# size -A test
test  :
section              size      addr
.interp                28   4194872
.note.ABI-tag          32   4194900
.note.gnu.build-id     36   4194932
.gnu.hash              28   4194968
.dynsym                96   4195000
.dynstr                63   4195096
.gnu.version            8   4195160
.gnu.version_r         32   4195168
.rela.dyn              24   4195200
.rela.plt              48   4195224
.init                  26   4195272
.plt                   48   4195312
.plt.got                8   4195360
.text                 418   4195376
.fini                   9   4195796
.rodata                21   4195808
.eh_frame_hdr          60   4195832
.eh_frame             276   4195896
.init_array             8   6295056
.fini_array             8   6295064
.jcr                    8   6295072
.dynamic              464   6295080
.got                    8   6295544
.got.plt               40   6295552
.data                  16   6295592
.bss                    8   6295608
.comment               53         0
.debug_aranges         48         0
.debug_info           174         0
.debug_abbrev          66         0
.debug_line            72         0
.debug_str            206         0
Total                2440

5 objdump

查看程序中的调试信息和段信息,也可以对程序进行反汇编。

查看信息:
后跟-h选项用来查看信息

# objdump -h testtest:     file format elf64-x86-64Sections:
Idx Name          Size      VMA               LMA               File off  Algn0 .interp       0000001c  0000000000400238  0000000000400238  00000238  2**0CONTENTS, ALLOC, LOAD, READONLY, DATA1 .note.ABI-tag 00000020  0000000000400254  0000000000400254  00000254  2**2CONTENTS, ALLOC, LOAD, READONLY, DATA2 .note.gnu.build-id 00000024  0000000000400274  0000000000400274  00000274  2**2CONTENTS, ALLOC, LOAD, READONLY, DATA3 .gnu.hash     0000001c  0000000000400298  0000000000400298  00000298  2**3CONTENTS, ALLOC, LOAD, READONLY, DATA4 .dynsym       00000060  00000000004002b8  00000000004002b8  000002b8  2**3CONTENTS, ALLOC, LOAD, READONLY, DATA5 .dynstr       0000003f  0000000000400318  0000000000400318  00000318  2**0CONTENTS, ALLOC, LOAD, READONLY, DATA6 .gnu.version  00000008  0000000000400358  0000000000400358  00000358  2**1CONTENTS, ALLOC, LOAD, READONLY, DATA7 .gnu.version_r 00000020  0000000000400360  0000000000400360  00000360  2**3CONTENTS, ALLOC, LOAD, READONLY, DATA8 .rela.dyn     00000018  0000000000400380  0000000000400380  00000380  2**3CONTENTS, ALLOC, LOAD, READONLY, DATA9 .rela.plt     00000030  0000000000400398  0000000000400398  00000398  2**3CONTENTS, ALLOC, LOAD, READONLY, DATA10 .init         0000001a  00000000004003c8  00000000004003c8  000003c8  2**2CONTENTS, ALLOC, LOAD, READONLY, CODE11 .plt          00000030  00000000004003f0  00000000004003f0  000003f0  2**4CONTENTS, ALLOC, LOAD, READONLY, CODE12 .plt.got      00000008  0000000000400420  0000000000400420  00000420  2**3CONTENTS, ALLOC, LOAD, READONLY, CODE13 .text         000001a2  0000000000400430  0000000000400430  00000430  2**4CONTENTS, ALLOC, LOAD, READONLY, CODE14 .fini         00000009  00000000004005d4  00000000004005d4  000005d4  2**2CONTENTS, ALLOC, LOAD, READONLY, CODE15 .rodata       00000015  00000000004005e0  00000000004005e0  000005e0  2**2CONTENTS, ALLOC, LOAD, READONLY, DATA16 .eh_frame_hdr 0000003c  00000000004005f8  00000000004005f8  000005f8  2**2CONTENTS, ALLOC, LOAD, READONLY, DATA17 .eh_frame     00000114  0000000000400638  0000000000400638  00000638  2**3CONTENTS, ALLOC, LOAD, READONLY, DATA18 .init_array   00000008  0000000000600e10  0000000000600e10  00000e10  2**3CONTENTS, ALLOC, LOAD, DATA19 .fini_array   00000008  0000000000600e18  0000000000600e18  00000e18  2**3CONTENTS, ALLOC, LOAD, DATA20 .jcr          00000008  0000000000600e20  0000000000600e20  00000e20  2**3CONTENTS, ALLOC, LOAD, DATA21 .dynamic      000001d0  0000000000600e28  0000000000600e28  00000e28  2**3CONTENTS, ALLOC, LOAD, DATA22 .got          00000008  0000000000600ff8  0000000000600ff8  00000ff8  2**3CONTENTS, ALLOC, LOAD, DATA23 .got.plt      00000028  0000000000601000  0000000000601000  00001000  2**3CONTENTS, ALLOC, LOAD, DATA24 .data         00000010  0000000000601028  0000000000601028  00001028  2**3CONTENTS, ALLOC, LOAD, DATA25 .bss          00000008  0000000000601038  0000000000601038  00001038  2**0ALLOC26 .comment      00000035  0000000000000000  0000000000000000  00001038  2**0CONTENTS, READONLY27 .debug_aranges 00000030  0000000000000000  0000000000000000  0000106d  2**0CONTENTS, READONLY, DEBUGGING28 .debug_info   000000ae  0000000000000000  0000000000000000  0000109d  2**0CONTENTS, READONLY, DEBUGGING29 .debug_abbrev 00000042  0000000000000000  0000000000000000  0000114b  2**0CONTENTS, READONLY, DEBUGGING30 .debug_line   00000048  0000000000000000  0000000000000000  0000118d  2**0CONTENTS, READONLY, DEBUGGING31 .debug_str    000000ce  0000000000000000  0000000000000000  000011d5  2**0CONTENTS, READONLY, DEBUGGING

反汇编:

# objdump -d testtest:     file format elf64-x86-64Disassembly of section .init:00000000004003c8 <_init>:4003c8:   48 83 ec 08             sub    $0x8,%rsp4003cc: 48 8b 05 25 0c 20 00    mov    0x200c25(%rip),%rax        # 600ff8 <_DYNAMIC+0x1d0>4003d3:   48 85 c0                test   %rax,%rax4003d6: 74 05                   je     4003dd <_init+0x15>4003d8:    e8 43 00 00 00          callq  400420 <__libc_start_main@plt+0x10>4003dd:   48 83 c4 08             add    $0x8,%rsp4003e1: c3                      retq   Disassembly of section .plt:00000000004003f0 <printf@plt-0x10>:4003f0:    ff 35 12 0c 20 00       pushq  0x200c12(%rip)        # 601008 <_GLOBAL_OFFSET_TABLE_+0x8>4003f6: ff 25 14 0c 20 00       jmpq   *0x200c14(%rip)        # 601010 <_GLOBAL_OFFSET_TABLE_+0x10>4003fc:   0f 1f 40 00             nopl   0x0(%rax)0000000000400400 <printf@plt>:400400:    ff 25 12 0c 20 00       jmpq   *0x200c12(%rip)        # 601018 <_GLOBAL_OFFSET_TABLE_+0x18>400406:   68 00 00 00 00          pushq  $0x040040b:  e9 e0 ff ff ff          jmpq   4003f0 <_init+0x28>0000000000400410 <__libc_start_main@plt>:400410:    ff 25 0a 0c 20 00       jmpq   *0x200c0a(%rip)        # 601020 <_GLOBAL_OFFSET_TABLE_+0x20>400416:   68 01 00 00 00          pushq  $0x140041b:  e9 d0 ff ff ff          jmpq   4003f0 <_init+0x28>Disassembly of section .plt.got:0000000000400420 <.plt.got>:400420:  ff 25 d2 0b 20 00       jmpq   *0x200bd2(%rip)        # 600ff8 <_DYNAMIC+0x1d0>400426:   66 90                   xchg   %ax,%axDisassembly of section .text:0000000000400430 <_start>:400430:  31 ed                   xor    %ebp,%ebp400432: 49 89 d1                mov    %rdx,%r9400435:  5e                      pop    %rsi400436:  48 89 e2                mov    %rsp,%rdx400439: 48 83 e4 f0             and    $0xfffffffffffffff0,%rsp40043d:  50                      push   %rax40043e:  54                      push   %rsp40043f:  49 c7 c0 d0 05 40 00    mov    $0x4005d0,%r8400446: 48 c7 c1 60 05 40 00    mov    $0x400560,%rcx40044d:    48 c7 c7 41 05 40 00    mov    $0x400541,%rdi400454:    e8 b7 ff ff ff          callq  400410 <__libc_start_main@plt>400459: f4                      hlt    40045a:  66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)0000000000400460 <deregister_tm_clones>:400460:    b8 3f 10 60 00          mov    $0x60103f,%eax400465:    55                      push   %rbp400466:  48 2d 38 10 60 00       sub    $0x601038,%rax40046c:    48 83 f8 0e             cmp    $0xe,%rax400470: 48 89 e5                mov    %rsp,%rbp400473: 76 1b                   jbe    400490 <deregister_tm_clones+0x30>400475: b8 00 00 00 00          mov    $0x0,%eax40047a: 48 85 c0                test   %rax,%rax40047d: 74 11                   je     400490 <deregister_tm_clones+0x30>40047f: 5d                      pop    %rbp400480:  bf 38 10 60 00          mov    $0x601038,%edi400485:    ff e0                   jmpq   *%rax400487: 66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)40048e:  00 00 400490:   5d                      pop    %rbp400491:  c3                      retq   400492:  0f 1f 40 00             nopl   0x0(%rax)400496: 66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)40049d:  00 00 00 00000000004004a0 <register_tm_clones>:4004a0:    be 38 10 60 00          mov    $0x601038,%esi4004a5:    55                      push   %rbp4004a6:  48 81 ee 38 10 60 00    sub    $0x601038,%rsi4004ad:    48 c1 fe 03             sar    $0x3,%rsi4004b1: 48 89 e5                mov    %rsp,%rbp4004b4: 48 89 f0                mov    %rsi,%rax4004b7: 48 c1 e8 3f             shr    $0x3f,%rax4004bb:    48 01 c6                add    %rax,%rsi4004be: 48 d1 fe                sar    %rsi4004c1:  74 15                   je     4004d8 <register_tm_clones+0x38>4004c3:   b8 00 00 00 00          mov    $0x0,%eax4004c8: 48 85 c0                test   %rax,%rax4004cb: 74 0b                   je     4004d8 <register_tm_clones+0x38>4004cd:   5d                      pop    %rbp4004ce:  bf 38 10 60 00          mov    $0x601038,%edi4004d3:    ff e0                   jmpq   *%rax4004d5: 0f 1f 00                nopl   (%rax)4004d8:    5d                      pop    %rbp4004d9:  c3                      retq   4004da:  66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)00000000004004e0 <__do_global_dtors_aux>:4004e0:   80 3d 51 0b 20 00 00    cmpb   $0x0,0x200b51(%rip)        # 601038 <__TMC_END__>4004e7:   75 11                   jne    4004fa <__do_global_dtors_aux+0x1a>4004e9:    55                      push   %rbp4004ea:  48 89 e5                mov    %rsp,%rbp4004ed: e8 6e ff ff ff          callq  400460 <deregister_tm_clones>4004f2:   5d                      pop    %rbp4004f3:  c6 05 3e 0b 20 00 01    movb   $0x1,0x200b3e(%rip)        # 601038 <__TMC_END__>4004fa:   f3 c3                   repz retq 4004fc:   0f 1f 40 00             nopl   0x0(%rax)0000000000400500 <frame_dummy>:400500:    bf 20 0e 60 00          mov    $0x600e20,%edi400505:    48 83 3f 00             cmpq   $0x0,(%rdi)400509:   75 05                   jne    400510 <frame_dummy+0x10>40050b:  eb 93                   jmp    4004a0 <register_tm_clones>40050d: 0f 1f 00                nopl   (%rax)400510:    b8 00 00 00 00          mov    $0x0,%eax400515: 48 85 c0                test   %rax,%rax400518: 74 f1                   je     40050b <frame_dummy+0xb>40051a:   55                      push   %rbp40051b:  48 89 e5                mov    %rsp,%rbp40051e: ff d0                   callq  *%rax400520: 5d                      pop    %rbp400521:  e9 7a ff ff ff          jmpq   4004a0 <register_tm_clones>0000000000400526 <test_func>:400526:  55                      push   %rbp400527:  48 89 e5                mov    %rsp,%rbp40052a: be 26 05 40 00          mov    $0x400526,%esi40052f:    bf e4 05 40 00          mov    $0x4005e4,%edi400534:    b8 00 00 00 00          mov    $0x0,%eax400539: e8 c2 fe ff ff          callq  400400 <printf@plt>40053e:    90                      nop40053f:  5d                      pop    %rbp400540:  c3                      retq   0000000000400541 <main>:400541:    55                      push   %rbp400542:  48 89 e5                mov    %rsp,%rbp400545: b8 00 00 00 00          mov    $0x0,%eax40054a: e8 d7 ff ff ff          callq  400526 <test_func>40054f:  b8 00 00 00 00          mov    $0x0,%eax400554: 5d                      pop    %rbp400555:  c3                      retq   400556:  66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)40055d:  00 00 00 0000000000400560 <__libc_csu_init>:400560:   41 57                   push   %r15400562:  41 56                   push   %r14400564:  41 89 ff                mov    %edi,%r15d400567:    41 55                   push   %r13400569:  41 54                   push   %r1240056b:  4c 8d 25 9e 08 20 00    lea    0x20089e(%rip),%r12        # 600e10 <__frame_dummy_init_array_entry>400572:    55                      push   %rbp400573:  48 8d 2d 9e 08 20 00    lea    0x20089e(%rip),%rbp        # 600e18 <__init_array_end>40057a:  53                      push   %rbx40057b:  49 89 f6                mov    %rsi,%r1440057e: 49 89 d5                mov    %rdx,%r13400581: 4c 29 e5                sub    %r12,%rbp400584: 48 83 ec 08             sub    $0x8,%rsp400588: 48 c1 fd 03             sar    $0x3,%rbp40058c: e8 37 fe ff ff          callq  4003c8 <_init>400591:  48 85 ed                test   %rbp,%rbp400594: 74 20                   je     4005b6 <__libc_csu_init+0x56>400596:  31 db                   xor    %ebx,%ebx400598: 0f 1f 84 00 00 00 00    nopl   0x0(%rax,%rax,1)40059f:  00 4005a0:  4c 89 ea                mov    %r13,%rdx4005a3: 4c 89 f6                mov    %r14,%rsi4005a6: 44 89 ff                mov    %r15d,%edi4005a9:    41 ff 14 dc             callq  *(%r12,%rbx,8)4005ad:    48 83 c3 01             add    $0x1,%rbx4005b1: 48 39 eb                cmp    %rbp,%rbx4005b4: 75 ea                   jne    4005a0 <__libc_csu_init+0x40>4005b6:  48 83 c4 08             add    $0x8,%rsp4005ba: 5b                      pop    %rbx4005bb:  5d                      pop    %rbp4005bc:  41 5c                   pop    %r124005be:  41 5d                   pop    %r134005c0:  41 5e                   pop    %r144005c2:  41 5f                   pop    %r154005c4:  c3                      retq   4005c5:  90                      nop4005c6:  66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)4005cd:  00 00 00 00000000004005d0 <__libc_csu_fini>:4005d0:   f3 c3                   repz retq Disassembly of section .fini:00000000004005d4 <_fini>:4005d4:   48 83 ec 08             sub    $0x8,%rsp4005d8: 48 83 c4 08             add    $0x8,%rsp4005dc: c3                      retq

6 strings

查看目标文件中的字符串。

# strings test
/lib64/ld-linux-x86-64.so.2
libc.so.6
printf
__libc_start_main
__gmon_start__
GLIBC_2.2.5
UH-8
AWAVA
AUATL
[]A\A]A^A_
func addr is:%p
;*3$"
GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
test_binutils.c
long unsigned int
short unsigned int
short int
test_func
test_binutils.c
unsigned char
long int
sizetype
main
GNU C11 5.4.0 20160609 -mtune=generic -march=x86-64 -g -fstack-protector-strong
/home/test_demo
crtstuff.c
__JCR_LIST__
deregister_tm_clones
__do_global_dtors_aux
completed.7594
__do_global_dtors_aux_fini_array_entry
frame_dummy
__frame_dummy_init_array_entry
test_binutils.c
__FRAME_END__
__JCR_END__
__init_array_end
_DYNAMIC
__init_array_start
__GNU_EH_FRAME_HDR
_GLOBAL_OFFSET_TABLE_
__libc_csu_fini
_ITM_deregisterTMCloneTable
_edata
printf@@GLIBC_2.2.5
test_func
__libc_start_main@@GLIBC_2.2.5
__data_start
__gmon_start__
__dso_handle
_IO_stdin_used
__libc_csu_init
__bss_start
main
_Jv_RegisterClasses
__TMC_END__
_ITM_registerTMCloneTable
.symtab
.strtab
.shstrtab
.interp
.note.ABI-tag
.note.gnu.build-id
.gnu.hash
.dynsym
.dynstr
.gnu.version
.gnu.version_r
.rela.dyn
.rela.plt
.init
.plt.got
.text
.fini
.rodata
.eh_frame_hdr
.eh_frame
.init_array
.fini_array
.jcr
.dynamic
.got.plt
.data
.bss
.comment
.debug_aranges
.debug_info
.debug_abbrev
.debug_line
.debug_str

7 strip

剔除可执行程序中的调试信息,减小文件的大小。

嵌入式软件开发杂谈(4):Binutils工具集相关推荐

  1. binutils工具集

    如果使用 gcc 作为编译器,那么 binutils 就是必不可少的一个工具集.工具集中的部分工具除了被 gcc 在后台使用为我们创建程序文件(目标文件.库文件或可执行程序)外,其它的则有助于方便开发 ...

  2. binutils工具集用法

    addr2line用于得到程序指令地址所对应的函数,以及函数所在的源文件名和行号. 在不少嵌入式开发环境中,编译器的名称往往不是gcc,而是想arm-rtems-gcc这样的,对于这种命名形式的编译器 ...

  3. Binutils工具集 GCC工具集介绍

    GNU Binutils详解:http://www.crifan.com/files/doc/docbook/binutils_intro/release/html/binutils_intro.ht ...

  4. Linux二进制实用工具Binutils工具集解析()

    From:http://blog.csdn.net/zqixiao_09/article/details/50783007 GNU Binutils:http://www.gnu.org/softwa ...

  5. GNU binutils工具集

    前言 GNU binutils工具提供了一系列二进制工具,用于日常开发过程中对程序调试和维护,经常会用到的工具如下: as:汇编编译器,用于将汇编代码编译成目标文件; addr2line:用于得到程序 ...

  6. Binutils工具集 解析

    对于嵌入式系统开发,掌握相应的工具至关重要,它能使我们解决问题的效率大大提高.目前,可以说嵌入式系统的开发工具是GNU的天下,因为来自GNU的GCC编译器支持大量的目标处理器.除了GCC,还有一个非常 ...

  7. GNU工具链简介(GCC编译器+Binutils工具集+GDB调试器+make工具)

    以下内容源于网络资源的学习与整理,如有侵权请告知删除. 一.什么是工具链 在软件工程中,工具链(toolchain)是一系列用于制作软件的工具.它包括用来编辑源代码的文本编辑器.生成可执行文件的编译器 ...

  8. UG\NX二次开发 Visual Studio平台工具集介绍

    文章作者:里海 来源网站:https://blog.csdn.net/WangPaiFeiXingYuan 一.平台工具集 平台工具集用于控制生成软件的方式. 二.设置方式 三.Visual Stud ...

  9. 嵌入式软件开发杂谈(7):静态分析工具Pclint与source insight的整合

    文章目录 1 安装 1.1 安装 1.2 配置 2 集成到SI中 3 生成头文件 4 修改配置 5 告警级别 6 常见错误消除 6.1 告警号40 6.2 告警号 1057 1 安装 1.1 安装 点 ...

最新文章

  1. 【重磅】助力企业复工,飞书宣布为中小企业提供三年免费服务
  2. 修改 jar 的一些体会
  3. AI 应届生就业意向大搜查,哪家企业才是大家的心仪首选?
  4. k2677场效应管参数引脚_场效应管参数大全2.doc
  5. SSO 认证机制对比
  6. 【Android 逆向】整体加固脱壳 ( DEX 优化流程分析 | DexPrepare.cpp 中 dvmContinueOptimizati() 函数分析 )
  7. MySQL高级最左前缀法则
  8. apiexample.c例子教我们如何利用FFMPEG库中的API函数来编写自己的编解码程序
  9. linux不重启更新内核,编译并使用kpath不重启更新linux-kernel
  10. ios与android指纹识别,iOS开发swift -- 指纹识别
  11. input之question
  12. 使用函数计算打包下载OSS文件
  13. 一、什么是类,一些关键字
  14. 兰资环单招计算机基础主要考什么,高职单招考试范围单招一般都考什么科目
  15. 大数据应用之Windows平台Hbase客户端Eclipse开发环境搭建
  16. POP3协议命令原始码及工作原理
  17. 谈谈我对面向对象的理解
  18. 各个蒙版的作用与区别
  19. Jetpack ViewBinding
  20. 今天第一次解决了程序在未装VS和XP下运行的问题

热门文章

  1. 用java编写博弈树_MathorCup竞赛优秀论文基于MonteCarlo局面评估和UCT博弈树搜索的...
  2. 关于调用高德地图路径规划清除问题解决方案【js】
  3. VMware:速修复这三个严重的 Workspace ONE Assist 软件漏洞
  4. android高德地图中心点,高德地图中心点以及自定义infowindow
  5. Redis:本地客户端连接远程服务器方法
  6. 分享制作系统启动U盘的方法
  7. git fetch学习之旅
  8. 1、OpenCV——图片的读、改、显、存操作函数
  9. python基础-结束循环的方式
  10. 小融资 大趋势 小嗨互联网融资研究分析 20190101-20190104