嵌入式软件开发杂谈:Binutils工具集

Posted Stoneshen1211

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了嵌入式软件开发杂谈:Binutils工具集相关的知识,希望对你有一定的参考价值。

文章目录


在嵌入式开发中,如果我们使用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 test
                 w __gmon_start__
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 w _Jv_RegisterClasses
                 U __libc_start_main@@GLIBC_2.2.5
                 U 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 test

Elf file type is EXEC (Executable file)
Entry point 0x400430
There are 9 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x00000000000001f8 0x00000000000001f8  R E    8
  INTERP         0x0000000000000238 0x0000000000400238 0x0000000000400238
                 0x000000000000001c 0x000000000000001c  R      1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x000000000000074c 0x000000000000074c  R E    200000
  LOAD           0x0000000000000e10 0x0000000000600e10 0x0000000000600e10
                 0x0000000000000228 0x0000000000000230  RW     200000
  DYNAMIC        0x0000000000000e28 0x0000000000600e28 0x0000000000600e28
                 0x00000000000001d0 0x00000000000001d0  RW     8
  NOTE           0x0000000000000254 0x0000000000400254 0x0000000000400254
                 0x0000000000000044 0x0000000000000044  R      4
  GNU_EH_FRAME   0x00000000000005f8 0x00000000004005f8 0x00000000004005f8
                 0x000000000000003c 0x000000000000003c  R      4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     10
  GNU_RELRO      0x0000000000000e10 0x0000000000600e10 0x0000000000600e10
                 0x00000000000001f0 0x00000000000001f0  R      1

 Section 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           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] .note.gnu.build-i NOTE             0000000000400274  00000274
       0000000000000024  0000000000000000   A       0     0     4
  [ 4] .gnu.hash         GNU_HASH         0000000000400298  00000298
       000000000000001c  0000000000000000   A       5     0     8
  [ 5] .dynsym           DYNSYM           00000000004002b8  000002b8
       0000000000000060  0000000000000018   A       6     1     8
  [ 6] .dynstr           STRTAB           0000000000400318  00000318
       000000000000003f  0000000000000000   A       0     0     1
  [ 7] .gnu.version      VERSYM           0000000000400358  00000358
       0000000000000008  0000000000000002   A       5     0     2
  [ 8] .gnu.version_r    VERNEED          0000000000400360  00000360
       0000000000000020  0000000000000000   A       6     1     8
  [ 9] .rela.dyn         RELA             0000000000400380  00000380
       0000000000000018  0000000000000018   A       5     0     8
  [10] .rela.plt         RELA             0000000000400398  00000398
       0000000000000030  0000000000000018  AI       5    24     8
  [11] .init             PROGBITS         00000000004003c8  000003c8
       000000000000001a  0000000000000000  AX       0     0     4
  [12] .plt              PROGBITS         00000000004003f0  000003f0
       0000000000000030  0000000000000010  AX       0     0     16
  [13] .plt.got          PROGBITS         0000000000400420  00000420
       0000000000000008  0000000000000000  AX       0     0     8
  [14] .text             PROGBITS         0000000000400430  00000430
       00000000000001a2  0000000000000000  AX       0     0     16
  [15] .fini             PROGBITS         00000000004005d4  000005d4
       0000000000000009  0000000000000000  AX       0     0     4
  [16] .rodata           PROGBITS         00000000004005e0  000005e0
       0000000000000015  0000000000000000   A       0     0     4
  [17] .eh_frame_hdr     PROGBITS         00000000004005f8  000005f8
       000000000000003c  0000000000000000   A       0     0     4
  [18] .eh_frame         PROGBITS         0000000000400638  00000638
       0000000000000114  0000000000000000   A       0     0     8
  [19] .init_array       INIT_ARRAY       0000000000600e10  00000e10
       0000000000000008  0000000000000000  WA       0     0     8
  [20] .fini_array       FINI_ARRAY       0000000000600e18  00000e18
       0000000000000008  0000000000000000  WA       0     0     8
  [21] .jcr              PROGBITS         0000000000600e20  00000e20
       0000000000000008  0000000000000000  WA       0     0     8
  [22] .dynamic          DYNAMIC          0000000000600e28  00000e28
       00000000000001d0  0000000000000010  WA       6     0     8
  [23] .got              PROGBITS         0000000000600ff8  00000ff8
       0000000000000008  0000000000000008  WA       0     0     8
  [24] .got.plt          PROGBITS         0000000000601000  00001000
       0000000000000028  0000000000000008  WA       0     0     8
  [25] .data             PROGBITS         0000000000601028  00001028
       0000000000000010  0000000000000000  WA       0     0     8
  [26] .bss              NOBITS           0000000000601038  00001038
       0000000000000008  0000000000000000  WA       0     0     1
  [27] .comment          PROGBITS         0000000000000000  00001038
       0000000000000035  0000000000000001  MS       0     0     1
  [28] .debug_aranges    PROGBITS         0000000000000000  0000106d
       0000000000000030  0000000000000000           0     0     1
  [29] .debug_info       PROGBITS         0000000000000000  0000109d
       00000000000000ae  0000000000000000           0     0     1
  [30] .debug_abbrev     PROGBITS         0000000000000000  0000114b
       0000000000000042  0000000000000000           0     0     1
  [31] .debug_line       PROGBITS         0000000000000000  0000118d
       0000000000000048  0000000000000000           0     0     1
  [32] .debug_str        PROGBITS         0000000000000000  000011d5
       00000000000000ce  0000000000000001  MS       0     0     1
  [33] .shstrtab         STRTAB           0000000000000000  00001ba8
       000000000000014c  0000000000000000           0     0     1
  [34] .symtab           SYMTAB           0000000000000000  000012a8
       00000000000006d8  0000000000000018          35    52     8
  [35] .strtab           STRTAB           0000000000000000  00001980
       0000000000000228  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] Name
       Type              Address          Offset            Link
       Size              EntSize          Info              Align
       Flags
  [ 0] 
       NULL                   NULL             0000000000000000  0000000000000000  0
       0000000000000000 0000000000000000  0                 0
       [0000000000000000]: 
  [ 1] .interp
       PROGBITS               PROGBITS         0000000000400238  0000000000000238  0
       000000000000001c 0000000000000000  0                 1
       [0000000000000002]: ALLOC
  [ 2] .note.ABI-tag
       NOTE                   NOTE             0000000000400254  0000000000000254  0
       0000000000000020 0000000000000000  0                 4
       [0000000000000002]: ALLOC
  [ 3] .note.gnu.build-id
       NOTE                   NOTE             0000000000400274  0000000000000274  0
       0000000000000024 0000000000000000  0                 4
       [0000000000000002]: ALLOC
  [ 4] .gnu.hash
       GNU_HASH               GNU_HASH         0000000000400298  0000000000000298  5
       000000000000001c 0000000000000000  0                 8
       [0000000000000002]: ALLOC
  [ 5] .dynsym
       DYNSYM                 DYNSYM           00000000004002b8  00000000000002b8  6
       0000000000000060 0000000000000018  1                 8
       [0000000000000002]: ALLOC
  [ 6] .dynstr
       STRTAB                 STRTAB           0000000000400318  0000000000000318  0
       000000000000003f 0000000000000000  0                 1
       [0000000000000002]: ALLOC
  [ 7] .gnu.version
       VERSYM                 VERSYM           0000000000400358  0000000000000358  5
       0000000000000008 0000000000000002  0                 2
       [0000000000000002]: ALLOC
  [ 8] .gnu.version_r
       VERNEED                VERNEED          0000000000400360  0000000000000360  6
       0000000000000020 0000000000000000  1                 8
       [0000000000000002]: ALLOC
  [ 9] .rela.dyn
       RELA                   RELA             0000000000400380  0000000000000380  5
       0000000000000018 0000000000000018  0                 8
       [0000000000000002]: ALLOC
  [10] .rela.plt
       RELA                   RELA             0000000000400398  0000000000000398  5
       0000000000000030 0000000000000018  24                8
       [0000000000000042]: ALLOC, INFO LINK
  [11] .init
       PROGBITS               PROGBITS         00000000004003c8  00000000000003c8  0
       000000000000001a 0000000000000000  0                 4
       [0000000000000006]: ALLOC, EXEC
  [12] .plt
       PROGBITS               PROGBITS         00000000004003f0  00000000000003f0  0
       0000000000000030 0000000000000010  0                 16
       [0000000000000006]: ALLOC, EXEC
  [13] .plt.got
       PROGBITS               PROGBITS         0000000000400420  0000000000000420  0
       0000000000000008 0000000000000000  0                 8
       [0000000000000006]: ALLOC, EXEC
  [14] .text
       PROGBITS               PROGBITS         0000000000400430  0000000000000430  0
       00000000000001a2 0000000000000000  0                 16
       [0000000000000006]: ALLOC, EXEC
  [15] .fini
       PROGBITS               PROGBITS         00000000004005d4  00000000000005d4  0
       0000000000000009 0000000000000000  0                 4
       [0000000000000006]: ALLOC, EXEC
  [16] .rodata
       PROGBITS               PROGBITS         00000000004005e0  00000000000005e0  0
       0000000000000015 0000000000000000  0                 4
       [0000000000000002]: ALLOC
  [17] .eh_frame_hdr
       PROGBITS               PROGBITS         00000000004005f8  00000000000005f8  0
       000000000000003c 0000000000000000  0                 4
       [0000000000000002]: ALLOC
  [18] .eh_frame
       PROGBITS               PROGBITS         0000000000400638  0000000000000638  0
       0000000000000114 0000000000000000  0                 8
       [0000000000000002]: ALLOC
  [19] .init_array
       INIT_ARRAY             INIT_ARRAY       0000000000600e10  0000000000000e10  0
       0000000000000008 0000000000000000  0                 8
       [0000000000000003]: WRITE, ALLOC
  [20] .fini_array
       FINI_ARRAY             FINI_ARRAY       0000000000600e18  0000000000000e18  0
       0000000000000008 0000000000000000  0                 8
       [0000000000000003]: WRITE, ALLOC
  [21] .jcr
       PROGBITS               PROGBITS         0000000000600e20  0000000000000e20  0
       0000000000000008 0000000000000000  0                 8
       [0000000000000003]: WRITE, ALLOC
  [22] .dynamic
       DYNAMIC                DYNAMIC          0000000000600e28  0000000000000e28  6
       00000000000001d0 0000000000000010  0                 8
       [0000000000000003]: WRITE, ALLOC
  [23] .got
       PROGBITS               PROGBITS         0000000000600ff8  0000000000000ff8  0
       0000000000000008 0000000000000008  0                 8
       [0000000000000003]: WRITE, ALLOC
  [24] .got.plt
       PROGBITS               PROGBITS         0000000000601000  0000000000001000  0
       0000000000000028 0000000000000008  0                 8
       [0000000000000003]: WRITE, ALLOC
  [25] .data
       PROGBITS               PROGBITS         0000000000601028  0000000000001028  0
       0000000000000010 0000000000000000  0                 8
       [0000000000000003]: WRITE, ALLOC
  [26] .bss
       NOBITS                 NOBITS           0000000000601038  0000000000001038  0
       0000000000000008 0000000000000000  0                 1
       [0000000000000003]: WRITE, ALLOC
  [27] .comment
       PROGBITS               PROGBITS         0000000000000000  0000000000001038  0
       0000000000000035 0000000000000001  0                 1
       [0000000000000030]: MERGE, STRINGS
  [28] .debug_aranges
       PROGBITS               PROGBITS         0000000000000000  000000000000106d  0
       0000000000000030 0000000000000000  0                 1
       [0000000000000000]: 
  [29] .debug_info
       PROGBITS               PROGBITS         0000000000000000  000000000000109d  0
       00000000000000ae 0000000000000000  0                 1
       [0000000000000000]: 
  [30] .debug_abbrev
       PROGBITS               PROGBITS         0000000000000000  000000000000114b  0
       0000000000000042 0000000000000000  0                 1
       [0000000000000000]: 
  [31] .debug_line
       PROGBITS               PROGBITS         0000000000000000  000000000000118d  0
       0000000000000048 0000000000000000  0                 1
       [0000000000000000]: 
  [32] .debug_str
       PROGBITS               PROGBITS         0000000000000000  00000000000011d5  0
       00000000000000ce 0000000000000001  0                 1
       [0000000000000030]: MERGE, STRINGS
  [33] .shstrtab
       STRTAB                 STRTAB           0000000000000000  0000000000001ba8  0
       000000000000014c 0000000000000000  0                 1
       [0000000000000000]: 
  [34] .symtab
       SYMTAB                 SYMTAB           0000000000000000  00000000000012a8  35
       00000000000006d8 0000000000000018  52                8
       [0000000000000000]: 
  [35] .strtab
       STRTAB                 STRTAB           0000000000000000  0000000000001980  0
       0000000000000228 0000000000000000  0                 1
       [0000000000000000]: 

3.5 -s

# readelf -s test

Symbol table '.dynsym' contains 4 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 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 Name
     0: 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.c
    34: 0000000000600e20     0 OBJECT  LOCAL  DEFAULT   21 __JCR_LIST__
    35: 0000000000400460     0 FUNC    LOCAL  DEFAULT   14 deregister_tm_clones
    36: 00000000004004a0     0 FUNC    LOCAL  DEFAULT   14 register_tm_clones
    37: 00000000004004e0     0 FUNC    LOCAL  DEFAULT   14 __do_global_dtors_aux
    38: 0000000000601038     1 OBJECT  LOCAL  DEFAULT   26 completed.7594
    39: 0000000000600e18     0 OBJECT  LOCAL  DEFAULT   20 __do_global_dtors_aux_fin
    40: 0000000000400500     0 FUNC    LOCAL  DEFAULT   14 frame_dummy
    41: 0000000000600e10     0 OBJECT  LOCAL  DEFAULT   19 __frame_dummy_init_array_
    42: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS test_binutils.c
    43: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    44: 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_end
    48: 0000000000600e28     0 OBJECT  LOCAL  DEFAULT   22 _DYNAMIC
    49: 0000000000600e10     0 NOTYPE  LOCAL  DEFAULT   19 __init_array_start
    50: 00000000004005f8     0 NOTYPE  LOCAL  DEFAULT   17 __GNU_EH_FRAME_HDR
    51: 0000000000601000     0 OBJECT  LOCAL  DEFAULT   24 _GLOBAL_OFFSET_TABLE_
    52: 00000000004005d0     2 FUNC    GLOBAL DEFAULT   14 __libc_csu_fini
    53: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab
    54: 0000000000601028     0 NOTYPE  WEAK   DEFAULT   25 data_start
    55: 0000000000601038     0 NOTYPE  GLOBAL DEFAULT   25 _edata
    56: 00000000004005d4     0 FUNC    GLOBAL DEFAULT   15 _fini
    57: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@@GLIBC_2.2.5
    58: 0000000000400526    27 FUNC    GLOBAL DEFAULT   14 test_func
    59: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_
    60: 0000000000601028     0 NOTYPE  GLOBAL DEFAULT   25 __data_start
    61: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
    62: 0000000000601030     0 OBJECT  GLOBAL HIDDEN    25 __dso_handle
    63: 00000000004005e0     4 OBJECT  GLOBAL DEFAULT   16 _IO_stdin_used
    64: 0000000000400560   101 FUNC    GLOBAL DEFAULT   14 __libc_csu_init
    65: 0000000000601040     0 NOTYPE  GLOBAL DEFAULT   26 _end
    66: 0000000000400430    42 FUNC    GLOBAL DEFAULT   14 _start
    67: 0000000000601038     0 NOTYPE  GLOBAL DEFAULT   26 __bss_start
    68: 0000000000400541    21 FUNC    GLOBAL DEFAULT   14 main
    69: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
    70: 0000000000601038     0 OBJECT  GLOBAL HIDDEN    25 __TMC_END__
    71: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
    72: 00000000004003c8     0 FUNC    GLOBAL DEFAULT   11 _init

3.6 -n

# readelf -n test

Displaying notes found at file offset 0x00000254 with length 0x00000020:
  Owner                 Data size	Description
  GNU                  0x00000010	NT_GNU_ABI_TAG (ABI version tag)
    OS: Linux, ABI: 2.6.32

Displaying notes found at file offset 0x00000274 with length 0x00000024:
  Owner                 Data size	Description
  GNU                  0x00000014	NT_GNU_BUILD_ID (unique build ID bitstring)
    Build ID: 86febe6b7e9155bf559d0667e18b029d66e5749a

3.7 -d

# readelf -d test

Dynamic section at offset 0xe28 contains 24 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000c (INIT)               0x4003c8
 0x000000000000000d (FINI)               0x4005d4
 0x0000000000000019 (INIT_ARRAY)         0x600e10
 0x000000000000001b (INIT_ARRAYSZ)       8 (bytes)
 0x000000000000001a (FINI_ARRAY)         0x600e18
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x400298
 0x0000000000000005 (STRTAB)             0x400318
 0x0000000000000006 (SYMTAB)             0x4002b8
 0x000000000000000a (STRSZ)              63 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x601000
 0x0000000000000002 (PLTRELSZ)           48 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x400398
 0x0000000000000007 (RELA)               0x400380
 0x0000000000000008 (RELASZ)             24 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffffe (VERNEED)            0x400360
 0x000000006fffffff (VERNEEDNUM)         1
 0x000000006ffffff0 (VERSYM)             0x400358
 0x0000000000000000 (NULL)               0x0

4 size

查看目标文件中的段大小

如下:

# size test
   text	   data	    bss	    dec	    hex	filename
   1261	    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 test

test:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .interp       0000001c  0000000000400238  0000000000400238  00000238  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  1 .note.ABI-tag 00000020  0000000000400254  0000000000400254  00000254  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  2 .note.gnu.build-id 00000024  0000000000400274  0000000000400274  00000274  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  3 .gnu.hash     0000001c  0000000000400298  0000000000400298  00000298  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .dynsym       00000060  00000000004002b8  00000000004002b8  000002b8  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  5 .dynstr       0000003f  0000000000400318  0000000000400318  00000318  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  6 .gnu.version  00000008  0000000000400358  0000000000400358  00000358  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  7 .gnu.version_r 00000020  0000000000400360  0000000000400360  00000360  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  8 .rela.dyn     00000018  0000000000400380  0000000000400380  00000380  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  9 .rela.plt     00000030  0000000000400398  0000000000400398  00000398  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 10 .init         0000001a  00000000004003c8  00000000004003c8  000003c8  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 11 .plt          00000030  00000000004003f0  00000000004003f0  000003f0  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 12 .plt.got      00000008  0000000000400420  0000000000400420  00000420  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 13 .text         000001a2  0000000000400430  0000000000400430  00000430  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 14 .fini         00000009  00000000004005d4  00000000004005d4  000005d4  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 15 .rodata       00000015  00000000004005e0  00000000004005e0  000005e0  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 16 .eh_frame_hdr 0000003c  00000000004005f8  00000000004005f8  000005f8  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 17 .eh_frame     00000114  0000000000400638  0000000000400638  00000638  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 18 .init_array   00000008  0000000000600e10  0000000000600e10  00000e10  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 19 .fini_array   00000008  0000000000600e18  0000000000600e18  00000e18  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 20 .jcr          00000008  0000000000600e20  0000000000600e20  00000e20  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 21 .dynamic      000001d0  0000000000600e28  0000000000600e28  00000e28  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 22 .got          00000008  0000000000600ff8  0000000000600ff8  00000ff8  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 23 .got.plt      00000028  0000000000601000  0000000000601000  00001000  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 24 .data         00000010  0000000000601028  0000000000601028  00001028  2**3
                  CONTENTS, ALLOC, LOAD, DATA
 25 .bss          00000008  0000000000601038  0000000000601038  00001038  2**0
                  ALLOC
 26 .comment      00000035  0000000000000000  0000000000000000  00001038  2**0
                  CONTENTS, READONLY
 27 .debug_aranges 00000030  0000000000000000  0000000000000000  0000106d  2**0
                  CONTENTS, READONLY, DEBUGGING
 28 .debug_info   000000ae  0000000000000000  0000000000000000  0000109d  2**0
                  CONTENTS, READONLY, DEBUGGING
 29 .debug_abbrev 00000042  0000000000000000  0000000000000000  0000114b  2**0
                  CONTENTS, READONLY, DEBUGGING
 30 .debug_line   00000048  0000000000000000  0000000000000000  0000118d  2**0
                  CONTENTS, READONLY, DEBUGGING
 31 .debug_str    000000ce  0000000000000000  0000000000000000  000011d5  2**0
                  CONTENTS, READONLY, DEBUGGING

反汇编:

# objdump -d test

test:     file format elf64-x86-64


Disassembly of section .init:

00000000004003c8 <_init>:
  4003c8:	48 83 ec 08          	sub    $0x8,%rsp
  4003cc:	48 8b 05 25 0c 20 00 	mov    0x200c25(%rip),%rax        # 600ff8 <_DYNAMIC+0x1d0>
  4003d3:	48 85 c0             	test   %rax,%rax
  4003d6:	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,%rsp
  4003e1:	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  $0x0
  40040b:	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  $0x1
  40041b:	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,%ax

Disassembly of section .text:

0000000000400430 <_start>:
  400430:	31 ed                	xor    %ebp,%ebp
  400432:	49 89 d1             	mov    %rdx,%r9
  400435:	5e                   	pop    %rsi
  400436:	48 89 e2             	mov    %rsp,%rdx
  400439:	48 83 e4 f0          	and    $0xfffffffffffffff0,%rsp
  40043d:	50                   	push   %rax
  40043e:	54                   	push   %rsp
  40043f:	49 c7 c0 d0 05 40 00 	mov    $0x4005d0,%r8
  400446:	48 c7 c1 60 05 40 00 	mov    $0x400560,%rcx
  40044d:	48 c7 c7 41 05 40 00 	mov    $0x400541,%rdi
  400454:	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,%eax
  400465:	55                   	push   %rbp
  400466:	48 2d 38 10 60 00    	sub    $0x601038,%rax
  40046c:	48 83 f8 0e          	cmp    $0xe,%rax
  400470:	48 89 e5             	mov    %rsp,%rbp
  400473:	76 1b                	jbe    400490 <deregister_tm_clones+0x30>
  400475:	b8 00 00 00 00       	mov    $0x0,%eax
  40047a:	48 85 c0             	test   %rax,%rax
  40047d:	74 11                	je     400490 <deregister_tm_clones+0x30>
  40047f:	5d                   	pop    %rbp
  400480:	bf 38 10 60 00       	mov    $0x601038,%edi
  400485:	ff e0                	jmpq   *%rax
  400487:	66 0f 1f 84 00 00 00 	nopw   0x0(%rax,%rax,1)
  40048e:	00 00 
  400490:	5d                   	pop    %rbp
  400491:	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,%esi
  4004a5:	55                   	push   %rbp
  4004a6:	48 81 ee 38 10 60 00 	sub    $0x601038,%rsi
  4004ad:	48 c1 fe 03          	sar    $0x3,%rsi
  4004b1:	48 89 e5             	mov    %rsp,%rbp
  4004b4:	48 89 f0             	mov    %rsi,%rax
  4004b7:	48 c1 e8 3f          	shr    $0x3f,%rax
  4004bb:	48 01 c6             	add    %rax,%rsi
  4004be:	48 d1 fe             	sar    %rsi
  4004c1:	74 15                	je     4004d8 <register_tm_clones+0x38>
  4004c3:	b8 00 00 00 00       	mov    $0x0,%eax
  4004c8:	48 85 c0             	test   %rax,%rax
  4004cb:	74 0b                	je     4004d8 <register_tm_clones+0x38>
  4004cd:	5d                   	pop    %rbp
  4004ce:	bf 38 10 60 00       	mov    $0x601038,%edi
  4004d3:	ff e0                	jmpq   *%rax
  4004d5:	0f 1f 00             	nopl   (%rax)
  4004d8:	5d                   	pop    %rbp
  4004d9:	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   %rbp
  4004ea:	48 89 e5             	mov    %rsp,%rbp
  4004ed:	e8 6e ff ff ff       	callq  400460 <deregister_tm_clones>
  4004f2:	5d                   	pop    %rbp
  4004f3:	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,%edi
  400505:	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,%eax
  400515:	48 85 c0             	test   %rax,%rax
  400518:	74 f1                	je     40050b <frame_dummy+0xb>
  40051a:	55                   	push   %rbp
  40051b:	48 89 e5             	mov    %rsp,%rbp
  40051e:	ff d0                	callq  *%rax
  400520:	5d                   	pop    %rbp
  400521:	e9 7a ff ff ff       	jmpq   4004a0 <register_tm_clones>

0000000000400526 <test_func>:
  400526:	55                   	push   %rbp
  400527:	48 89 e5             	mov    %rsp,%rbp
  40052a:	be 26 05 40 00       	mov    $0x400526,%esi
  40052f:	bf e4 05 40 00       	mov    $0x4005e4,%edi
  400534:	b8 00 00 00 00       	mov    $0x0,%eax
  400539:	e8 c2 fe ff ff       	callq  400400 <printf@plt>
  40053e:	90                   	nop
  40053f:	5d                   	pop    %rbp
  400540:	c3                   	retq   

0000000000400541 <main>:
  400541:	55                   	push   %rbp
  400542:	48 89 e5             	mov    %rsp,%rbp
  400545:	b8 00 00 00 00       	mov    $0x0,%eax
  40054a:	e8 d7 ff ff ff       	callq  400526 <test_func>
  40054f:	b8 00 00 00 00       	mov    $0x0,%eax
  400554:	5d                   	pop    %rbp
  400555:	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   %r15
  400562:	41 56                	push   %r14
  400564:	41 89 ff             	mov    %edi,%r15d
  400567:	41 55                	push   %r13
  400569:	41 54                	push   %r12
  40056b:	4c 8d 25 9e 08 20 00 	lea    0x20089e(%rip),%r12        # 600e10 <__frame_dummy_init_array_entry>
  400572:	55                   	push   %rbp
  400573:	48 8d 2d 9e 08 20 00 	lea    0x20089e(%rip),%rbp        # 600e18 <__init_array_end>
  40057a:	53                   	push   %rbx
  40057b:	49 89 f6             	mov    %rsi,%r14
  40057e:	49 89 d5             	mov    %rdx,%r13
  400581:	4c 29 e5             	sub    %r12,%rbp
  400584:	48 83 ec 08          	sub    $0x8,%rsp
  400588:	48 c1 fd 03          	sar    $0x3,%rbp
  40058c:	e8 37 fe ff ff       	callq  4003c8 <_init>
  400591:	48 85 ed             	test   %rbp,%rbp
  400594:	74 20                	je     4005b6 <__libc_csu_init+0x56>
  400596:	31 db                	xor    %ebx,%ebx
  400598:	0f 1f 84 00 00 00 00 	nopl   0x0(%rax,%rax,1)
  40059f:	00 
  4005a0:	4c 89 ea             	mov    %r13,%rdx
  4005a3:	4c 89 f6             	mov    %r14,%rsi
  4005a6:	44 89 ff             	mov    %r15d,%edi
  4005a9:	41 ff 14 dc          	callq  *(%r12,%rbx,8)
  4005ad:	48 83 c3 01          	add    $0x1,%rbx
  4005b1:	48 39 eb             	cmp    %rbp,%rbx
  4005b4:	75 ea                	jne    4005a0 <__libc_csu_init+0x40>
  4005b6:	48 83 c4 08          	add    $0x8,%rsp
  4005ba:	5b                   	pop    %rbx
  4005bb:	5d                   	pop    %rbp
  4005bc:	41 5c                	pop    %r12
  4005be:	41 5d                	pop    %r13
  4005c0:	41 5e                	pop    %r14
  4005c2:	41 5f                	pop    %r15
  4005c4:	c3                   	retq   
  4005c5:	90                   	nop
  4005c6:	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,%rsp
  4005d8:	48 83 c4 08          	add    $0x8,%rsp
  4005dc:	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

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

以上是关于嵌入式软件开发杂谈:Binutils工具集的主要内容,如果未能解决你的问题,请参考以下文章

嵌入式软件开发杂谈:Binutils工具集

binutils工具集之---objdump

binutils工具集之---addr2line

嵌入式软件开发杂谈:动态分析工具Valgrind的配置使用

嵌入式软件开发杂谈:动态分析工具Valgrind的配置使用

嵌入式软件开发杂谈:动态分析工具Valgrind的配置使用