readelf

用於顯示elf格式文件的信息

補充說明

readelf命令 用來顯示一個或者多個elf格式的目標文件的信息,可以通過它的選項來控制顯示哪些信息。這裏的elf-file(s)就表示那些被檢查的文件。可以支持32位,64位的elf格式文件,也支持包含elf文件的文檔(這裏一般指的是使用ar命令將一些elf文件打包之後生成的例如lib*.a之類的“靜態庫”文件)。 

這個程序和objdump提供的功能類似,但是它顯示的信息更爲具體,並且它不依賴BFD庫(BFD庫是一個GNU項目,它的目標就是希望通過一種統一的接口來處理不同的目標文件),所以即使BFD庫有什麼bug存在的話也不會影響到readelf程序。 

運行readelf的時候,除了-v和-H之外,其它的選項必須有一個被指定。 

ELF文件類型

種類型的ELF文件:

  1. 可重定位文件:用戶和其他目標文件一起創建可執行文件或者共享目標文件,例如lib*.a文件。 
  2. 可執行文件:用於生成進程映像,載入內存執行,例如編譯好的可執行文件a.out。 
  3. 共享目標文件:用於和其他共享目標文件或者可重定位文件一起生成elf目標文件或者和執行文件一起創建進程映像,例如lib*.so文件。 

ELF文件作用:

ELF文件參與程序的連接(建立一個程序)和程序的執行(運行一個程序),所以可以從不同的角度來看待elf格式的文件: 

  1. 如果用於編譯和鏈接(可重定位文件),則編譯器和鏈接器將把elf文件看作是節頭表描述的節的集合,程序頭表可選。 
  2. 如果用於加載執行(可執行文件),則加載器則將把elf文件看作是程序頭表描述的段的集合,一個段可能包含多個節,節頭表可選。 
  3. 如果是共享文件,則兩者都含有。 

ELF文件總體組成:  

elf文件頭描述elf文件的總體信息。包括:系統相關,類型相關,加載相關,鏈接相關。 

  • 系統相關表示:elf文件標識的魔術數,以及硬件和平臺等相關信息,增加了elf文件的移植性,使交叉編譯成爲可能。 
  • 類型相關就是前面說的那個類型。 
  • 加載相關:包括程序頭表相關信息。 
  • 鏈接相關:節頭表相關信息。 

選項

 1-a 
 2--all 顯示全部信息,等價於 -h -l -S -s -r -d -V -A -I. 
 3
 4-h 
 5--file-header 顯示elf文件開始的文件頭信息. 
 6
 7-l 
 8--program-headers  
 9--segments 顯示程序頭(段頭)信息(如果有的話)10
11-S 
12--section-headers  
13--sections 顯示節頭信息(如果有的話)14
15-g 
16--section-groups 顯示節組信息(如果有的話)17
18-t 
19--section-details 顯示節的詳細信息(-S的)20
21-s 
22--syms        
23--symbols 顯示符號表段中的項(如果有的話)。 
24
25-e 
26--headers 顯示全部頭信息,等價於: -h -l -S 
27
28-n 
29--notes 顯示note段(內核註釋)的信息。 
30
31-r 
32--relocs 顯示可重定位段的信息。 
33
34-u 
35--unwind 顯示unwind段信息。當前只支持IA64 ELF的unwind段信息。 
36
37-d 
38--dynamic 顯示動態段的信息。 
39
40-V 
41--version-info 顯示版本段的信息。 
42
43-A 
44--arch-specific 顯示CPU構架信息。 
45
46-D 
47--use-dynamic 使用動態段中的符號表顯示符號,而不是使用符號段。 
48
49-x <number or name> 
50--hex-dump=<number or name> 以16進制方式顯示指定段內內容。number指定段表中段的索引,或字符串指定文件中的段名。 
51
52-w[liaprmfFsoR] or 
53--debug-dump[=line,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=frames-interp,=str,=loc,=Ranges] 顯示調試段中指定的內容。 
54
55-I 
56--histogram 顯示符號的時候,顯示bucket list長度的柱狀圖。 
57
58-v 
59--version 顯示readelf的版本信息。 
60
61-H 
62--help 顯示readelf所支持的命令行選項。 
63
64-W 
65--wide 寬行輸出。 
66
67@file 可以將選項集中到一個文件中,然後使用這個@file選項載入。 

實例

先給出如下例子:

1.對於可執行文件形式的elf格式文件:

1)查看可執行程序的源代碼如下: 

 1root@localhost [test]$ cat main.cpp 
 2#include <iostream> 
 3using std::cout; 
 4using std::endl; 
 5void my_print(); 
 6
 7int main(int argc, char *argv[]) 
 8{ 
 9        my_print(); 
10        cout<<"hello!"<<endl; 
11        return 0; 
12} 
13
14void  my_print() 
15{ 
16        cout<<"print!"<<endl; 
17} 

2)編譯如下: 

1[root@localhost test]$ g++ main.cpp -o main 
2[root@localhost test]$ g++ -g main.cpp -o main.debug 

3)編譯之後,查看生成的文件: 

1[root@localhost test]$ ls -l 
2總計 64 
3-rwxr-xr-x 1 quietheart quietheart  6700 07-07 18:04 main 
4-rw-r--r-- 1 quietheart quietheart   201 07-07 18:02 main.cpp 
5-rwxr-xr-x 1 quietheart quietheart 38932 07-07 18:04 main.debug 

這裏,main.debug是帶有調試信息的可執行文件,main是一般的可執行文件。 

2.對於庫文件形式的elf格式文件:

1)查看庫的源代碼如下: 

 1//myfile.h 
 2#ifndef __MYFILE_H 
 3#define __MYFILE_H 
 4void printInfo(); 
 5#endif 
 6
 7//myfile.cpp 
 8#include "myfile.h" 
 9#include <iostream> 
10using std::cout; 
11using std::endl; 
12void printInfo() 
13{ 
14    cout<<"hello"<<endl; 
15} 

2)編譯如下: 

1[root@localhost test]$ g++ -c myfile.cpp 
2[root@localhost test]$ g++ -shared -fPCI -o libmy.so myfile.o 
3[root@localhost test]$ ar -r libmy.a myfile.o 
4ar: creating libmy.a 

3)編譯之後,查看生成的文件: 

[root@localhost test]$ ls -l 

總計 44 

1-rw-r--r-- 1 quietheart quietheart 2154 07-08 16:14 libmy.a 
2-rwxr-xr-x 1 quietheart quietheart 5707 07-08 16:08 libmy.so 
3-rwxr-xr-x 1 quietheart quietheart  117 07-08 16:06 myfile.cpp 
4-rwxr-xr-x 1 quietheart quietheart   63 07-08 16:08 myfile.h 
5-rw-r--r-- 1 quietheart quietheart 2004 07-08 16:08 myfile.o 
6libmy.a  libmy.so  myfile.cpp  myfile.h  myfile.o 

這裏,分別生成目標文件myfile.o,共享庫文件libmy.so,和靜態庫文件libmy.a。 

基於以上可執行文件和庫,這裏給出一些常用的命令。 

讀取可執行文件形式的elf文件頭信息:

 1[root@localhost test]$ readelf -h main 
 2ELF Header: 
 3  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
 4  Class:                             ELF32 
 5  Data:                              2's complement, little endian 
 6  Version:                           1 (current) 
 7  OS/ABI:                            UNIX - System V 
 8  ABI Version:                       0 
 9  type:                              exec (Executable file) 
10  Machine:                           Intel 80386 
11  Version:                           0x1 
12  Entry point address:               0x8048580 
13  Start of program headers:          52 (bytes into file) 
14  Start of section headers:          3232 (bytes into file) 
15  Flags:                             0x0 
16  Size of this header:               52 (bytes) 
17  Size of program headers:           32 (bytes) 
18  Number of program headers:         8 
19  Size of section headers:           40 (bytes) 
20  Number of section headers:         29 
21  Section header string table index: 26 

這裏,可見可執行文件的elf文件,其類型爲EXEC(可執行文件)。另外,含調試信息的"main.debug"和不含調試信息的"main"除了一些大小信息之外,其內容是一樣的。並且由此可見文件的體系結構爲Intel 80386。 

讀取目標文件形式的elf文件頭信息:

 1[root@localhost test]$ readelf -h myfile.o 
 2ELF Header: 
 3  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
 4  Class:                             ELF32 
 5  Data:                              2's complement, little endian 
 6  Version:                           1 (current) 
 7  OS/ABI:                            UNIX - System V 
 8  ABI Version:                       0 
 9  Type:                              REL (Relocatable file) 
10  Machine:                           Intel 80386 
11  Version:                           0x1 
12  Entry point address:               0x0 
13  Start of program headers:          0 (bytes into file) 
14  Start of section headers:          516 (bytes into file) 
15  Flags:                             0x0 
16  Size of this header:               52 (bytes) 
17  Size of program headers:           0 (bytes) 
18  Number of program headers:         0 
19  Size of section headers:           40 (bytes) 
20  Number of section headers:         15 
21  Section header string table index: 12 

這裏,可見目標文件的elf文件,其類型爲REL(可重定位文件)。 

讀取靜態庫文件形式的elf文件頭信息:

 1[root@localhost test]$ readelf -h libmy.a 
 2File: libmy.a(myfile.o) 
 3ELF Header: 
 4  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
 5  Class:                             ELF32 
 6  Data:                              2's complement, little endian 
 7  Version:                           1 (current) 
 8  OS/ABI:                            UNIX - System V 
 9  ABI Version:                       0 
10  Type:                              REL (Relocatable file) 
11  Machine:                           Intel 80386 
12  Version:                           0x1 
13  Entry point address:               0x0 
14  Start of program headers:          0 (bytes into file) 
15  Start of section headers:          516 (bytes into file) 
16  Flags:                             0x0 
17  Size of this header:               52 (bytes) 
18  Size of program headers:           0 (bytes) 
19  Number of program headers:         0 
20  Size of section headers:           40 (bytes) 
21  Number of section headers:         15 
22  Section header string table index: 12 

這裏,可見靜態庫文件的elf文件,其類型爲REL(可重定位文件)。 

讀取動態庫文件形式的elf文件頭信息:

 1[root@localhost test]$ readelf -h libmy.so 
 2ELF Header: 
 3  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
 4  Class:                             ELF32 
 5  Data:                              2's complement, little endian 
 6  Version:                           1 (current) 
 7  OS/ABI:                            UNIX - System V 
 8  ABI Version:                       0 
 9  Type:                              DYN (Shared object file) 
10  Machine:                           Intel 80386 
11  Version:                           0x1 
12  Entry point address:               0x550 
13  Start of program headers:          52 (bytes into file) 
14  Start of section headers:          2768 (bytes into file) 
15  Flags:                             0x0 
16  Size of this header:               52 (bytes) 
17  Size of program headers:           32 (bytes) 
18  Number of program headers:         5 
19  Size of section headers:           40 (bytes) 
20  Number of section headers:         27 
21  Section header string table index: 24 

這裏,可見動態庫文件的elf文件,其類型爲DYN(共享目標文件)。 

查看可執行的elf文件程序頭表信息:

 1[root@localhost test]$ readelf -l main 
 2Elf file type is EXEC (Executable file) 
 3Entry point 0x8048580 
 4There are 8 program headers, starting at offset 52 
 5
 6Program Headers: 
 7  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align 
 8  PHDR           0x000034 0x08048034 0x08048034 0x00100 0x00100 R E 0x4 
 9  INTERP         0x000134 0x08048134 0x08048134 0x00013 0x00013 R   0x1 
10      Requesting program interpreter: /lib/[ld-linux.so.2] 
11  LOAD           0x000000 0x08048000 0x08048000 0x00970 0x00970 R E 0x1000 
12  LOAD           0x000970 0x08049970 0x08049970 0x00130 0x001c8 RW  0x1000 
13  DYNAMIC        0x000988 0x08049988 0x08049988 0x000e0 0x000e0 RW  0x4 
14  NOTE           0x000148 0x08048148 0x08048148 0x00020 0x00020 R   0x4 
15  GNU_EH_FRAME   0x000820 0x08048820 0x08048820 0x00044 0x00044 R   0x4 
16  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x4 
17
18Section to Segment mapping: 
19  Segment Sections... 
20   00     
21   01     .interp 
22   02     .interp .note.ABI-tag .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 
23   03     .ctors .dtors .jcr .dynamic .got .got.plt .data .bss 
24   04     .dynamic 
25   05     .note.ABI-tag 
26   06     .eh_frame_hdr 
27   07     

這裏,含調試信息的"main.debug"和不含調試信息的"main"其內容是一樣的。 

**查看目標文件的elf文件程序頭表信息: **

1[root@localhost test]$ readelf -l myfile.o 
2There are no program headers in this file. 

這裏可知,可重定位的目標文件,它沒程序頭表。 

查看靜態庫文件的elf文件程序頭表信息:

1[root@localhost test]$ readelf -l libmy.a 
2File: libmy.a(myfile.o) 
3There are no program headers in this file. 

這裏可知,可重定位的靜態庫文件,它沒程序頭表。 

查看動態庫文件的elf文件程序頭表信息:

 1[root@localhost test]$ readelf -l libmy.so 
 2Elf file type is DYN (Shared object file) 
 3Entry point 0x550 
 4There are 5 program headers, starting at offset 52 
 5
 6Program Headers: 
 7  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align 
 8  LOAD           0x000000 0x00000000 0x00000000 0x007f4 0x007f4 R E 0x1000 
 9  LOAD           0x0007f4 0x000017f4 0x000017f4 0x0011c 0x00128 RW  0x1000 
10  DYNAMIC        0x000810 0x00001810 0x00001810 0x000e0 0x000e0 RW  0x4 
11  GNU_EH_FRAME   0x000738 0x00000738 0x00000738 0x0002c 0x0002c R   0x4 
12  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x4 
13
14Section to Segment mapping: 
15  Segment Sections... 
16   00     .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 
17   01     .ctors .dtors .jcr .data.rel.ro .dynamic .got .got.plt .bss 
18   02     .dynamic 
19   03     .eh_frame_hdr 
20   04     

這裏可知,做爲共享目標文件的動態庫,它程序頭表。 

查看一個可執行的elf文件的節信息:

 1[root@localhost test]$ readelf -S main 
 2There are 29 section headers, starting at offset 0xca0: 
 3Section Headers: 
 4  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al 
 5  [ 0]                   NULL            00000000 000000 000000 00      0   0  0 
 6  [ 1] .interp           PROGBITS        08048134 000134 000013 00   A  0   0  1 
 7  [ 2] .note.ABI-tag     NOTE            08048148 000148 000020 00   A  0   0  4 
 8  [ 3] .gnu.hash         GNU_HASH        08048168 000168 000030 04   A  4   0  4 
 9  [ 4] .dynsym           DYNSYM          08048198 000198 0000d0 10   A  5   1  4 
10  [ 5] .dynstr           STRTAB          08048268 000268 000183 00   A  0   0  1 
11  [ 6] .gnu.version      VERSYM          080483ec 0003ec 00001a 02   A  4   0  2 
12  [ 7] .gnu.version_r    VERNEED         08048408 000408 000060 00   A  5   2  4 
13  [ 8] .rel.dyn          REL             08048468 000468 000010 08   A  4   0  4 
14  [ 9] .rel.plt          REL             08048478 000478 000048 08   A  4  11  4 
15  [10] .init             PROGBITS        080484c0 0004c0 000017 00  AX  0   0  4 
16  [11] .plt              PROGBITS        080484d8 0004d8 0000a0 04  AX  0   0  4 
17  [12] .text             PROGBITS        08048580 000580 000268 00  AX  0   0 16 
18  [13] .fini             PROGBITS        080487e8 0007e8 00001c 00  AX  0   0  4 
19  [14] .rodata           PROGBITS        08048804 000804 00001a 00   A  0   0  4 
20  [15] .eh_frame_hdr     PROGBITS        08048820 000820 000044 00   A  0   0  4 
21  [16] .eh_frame         PROGBITS        08048864 000864 00010c 00   A  0   0  4 
22  [17] .ctors            PROGBITS        08049970 000970 00000c 00  WA  0   0  4 
23  [18] .dtors            PROGBITS        0804997c 00097c 000008 00  WA  0   0  4 
24  [19] .jcr              PROGBITS        08049984 000984 000004 00  WA  0   0  4 
25  [20] .dynamic          DYNAMIC         08049988 000988 0000e0 08  WA  5   0  4 
26  [21] .got              PROGBITS        08049a68 000a68 000004 04  WA  0   0  4 
27  [22] .got.plt          PROGBITS        08049a6c 000a6c 000030 04  WA  0   0  4 
28  [23] .data             PROGBITS        08049a9c 000a9c 000004 00  WA  0   0  4 
29  [24] .bss              NOBITS          08049aa0 000aa0 000098 00  WA  0   0  8 
30  [25] .comment          PROGBITS        00000000 000aa0 000114 00      0   0  1 
31  [26] .shstrtab         STRTAB          00000000 000bb4 0000e9 00      0   0  1 
32  [27] .symtab           SYMTAB          00000000 001128 000510 10     28  53  4 
33  [28] .strtab           STRTAB          00000000 001638 0003f4 00      0   0  1 
34Key to Flags: 
35  W (write), A (alloc), X (execute), M (merge), S (strings) 
36  I (info), L (link order), G (group), x (unknown) 
37  O (extra OS processing required) o (OS specific), p (processor specific) 

這裏,main是可執行文件,不含調試信息。 

查看一個包含調試信息的可執行的elf文件的節信息:

 1[root@localhost test]$ readelf -S main.debug 
 2There are 37 section headers, starting at offset 0x88c8: 
 3
 4Section Headers: 
 5  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al 
 6  [ 0]                   NULL            00000000 000000 000000 00      0   0  0 
 7  [ 1] .interp           PROGBITS        08048134 000134 000013 00   A  0   0  1 
 8  [ 2] .note.ABI-tag     NOTE            08048148 000148 000020 00   A  0   0  4 
 9  [ 3] .gnu.hash         GNU_HASH        08048168 000168 000030 04   A  4   0  4 
10  [ 4] .dynsym           DYNSYM          08048198 000198 0000d0 10   A  5   1  4 
11  [ 5] .dynstr           STRTAB          08048268 000268 000183 00   A  0   0  1 
12  [ 6] .gnu.version      VERSYM          080483ec 0003ec 00001a 02   A  4   0  2 
13  [ 7] .gnu.version_r    VERNEED         08048408 000408 000060 00   A  5   2  4 
14  [ 8] .rel.dyn          REL             08048468 000468 000010 08   A  4   0  4 
15  [ 9] .rel.plt          REL             08048478 000478 000048 08   A  4  11  4 
16  [10] .init             PROGBITS        080484c0 0004c0 000017 00  AX  0   0  4 
17  [11] .plt              PROGBITS        080484d8 0004d8 0000a0 04  AX  0   0  4 
18  [12] .text             PROGBITS        08048580 000580 000268 00  AX  0   0 16 
19  [13] .fini             PROGBITS        080487e8 0007e8 00001c 00  AX  0   0  4 
20  [14] .rodata           PROGBITS        08048804 000804 00001a 00   A  0   0  4 
21  [15] .eh_frame_hdr     PROGBITS        08048820 000820 000044 00   A  0   0  4 
22  [16] .eh_frame         PROGBITS        08048864 000864 00010c 00   A  0   0  4 
23  [17] .ctors            PROGBITS        08049970 000970 00000c 00  WA  0   0  4 
24  [18] .dtors            PROGBITS        0804997c 00097c 000008 00  WA  0   0  4 
25  [19] .jcr              PROGBITS        08049984 000984 000004 00  WA  0   0  4 
26  [20] .dynamic          DYNAMIC         08049988 000988 0000e0 08  WA  5   0  4 
27  [21] .got              PROGBITS        08049a68 000a68 000004 04  WA  0   0  4 
28  [22] .got.plt          PROGBITS        08049a6c 000a6c 000030 04  WA  0   0  4 
29  [23] .data             PROGBITS        08049a9c 000a9c 000004 00  WA  0   0  4 
30  [24] .bss              NOBITS          08049aa0 000aa0 000098 00  WA  0   0  8 
31  [25] .comment          PROGBITS        00000000 000aa0 000114 00      0   0  1 
32  [26] .debug_aranges    PROGBITS        00000000 000bb4 000020 00      0   0  1 
33  [27] .debug_pubnames   PROGBITS        00000000 000bd4 000028 00      0   0  1 
34  [28] .debug_info       PROGBITS        00000000 000bfc 0067aa 00      0   0  1 
35  [29] .debug_abbrev     PROGBITS        00000000 0073a6 000726 00      0   0  1 
36  [30] .debug_line       PROGBITS        00000000 007acc 0003e1 00      0   0  1 
37  [31] .debug_frame      PROGBITS        00000000 007eb0 00009c 00      0   0  4 
38  [32] .debug_str        PROGBITS        00000000 007f4c 000735 00      0   0  1 
39  [33] .debug_loc        PROGBITS        00000000 008681 0000f3 00      0   0  1 
40  [34] .shstrtab         STRTAB          00000000 008774 000151 00      0   0  1 
41  [35] .symtab           SYMTAB          00000000 008e90 000590 10     36  61  4 
42  [36] .strtab           STRTAB          00000000 009420 0003f4 00      0   0  1 
43Key to Flags: 
44  W (write), A (alloc), X (execute), M (merge), S (strings) 
45  I (info), L (link order), G (group), x (unknown) 
46  O (extra OS processing required) o (OS specific), p (processor specific) 

可見,相對非調試版本的可執行文件,多了".debug*"段的信息。 

查看一個目標文件的elf文件的節信息:

 1[root@localhost test]$ readelf -S myfile.o 
 2There are 15 section headers, starting at offset 0x204: 
 3
 4Section Headers: 
 5  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al 
 6  [ 0]                   NULL            00000000 000000 000000 00      0   0  0 
 7  [ 1] .text             PROGBITS        00000000 000034 00009e 00  AX  0   0  4 
 8  [ 2] .rel.text         REL             00000000 000744 000060 08     13   1  4 
 9  [ 3] .data             PROGBITS        00000000 0000d4 000000 00  WA  0   0  4 
10  [ 4] .bss              NOBITS          00000000 0000d4 000001 00  WA  0   0  4 
11  [ 5] .ctors            PROGBITS        00000000 0000d4 000004 00  WA  0   0  4 
12  [ 6] .rel.ctors        REL             00000000 0007a4 000008 08     13   5  4 
13  [ 7] .rodata           PROGBITS        00000000 0000d8 000006 00   A  0   0  1 
14  [ 8] .eh_frame         PROGBITS        00000000 0000e0 00008c 00   A  0   0  4 
15  [ 9] .rel.eh_frame     REL             00000000 0007ac 000028 08     13   8  4 
16  [10] .comment          PROGBITS        00000000 00016c 00002e 00      0   0  1 
17  [11] .note.GNU-stack   PROGBITS        00000000 00019a 000000 00      0   0  1 
18  [12] .shstrtab         STRTAB          00000000 00019a 00006a 00      0   0  1 
19  [13] .symtab           SYMTAB          00000000 00045c 000180 10     14  14  4 
20  [14] .strtab           STRTAB          00000000 0005dc 000166 00      0   0  1 
21Key to Flags: 
22  W (write), A (alloc), X (execute), M (merge), S (strings) 
23  I (info), L (link order), G (group), x (unknown) 
24  O (extra OS processing required) o (OS specific), p (processor specific) 
25
26
27```shell
28
29 **查看一個靜態庫文件的elf文件的節信息:** 
30
31```shell
32[root@localhost test]$ readelf -S libmy.a 
33File: libmy.a(myfile.o) 
34There are 15 section headers, starting at offset 0x204: 
35
36Section Headers: 
37  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al 
38  [ 0]                   NULL            00000000 000000 000000 00      0   0  0 
39  [ 1] .text             PROGBITS        00000000 000034 00009e 00  AX  0   0  4 
40  [ 2] .rel.text         REL             00000000 000744 000060 08     13   1  4 
41  [ 3] .data             PROGBITS        00000000 0000d4 000000 00  WA  0   0  4 
42  [ 4] .bss              NOBITS          00000000 0000d4 000001 00  WA  0   0  4 
43  [ 5] .ctors            PROGBITS        00000000 0000d4 000004 00  WA  0   0  4 
44  [ 6] .rel.ctors        REL             00000000 0007a4 000008 08     13   5  4 
45  [ 7] .rodata           PROGBITS        00000000 0000d8 000006 00   A  0   0  1 
46  [ 8] .eh_frame         PROGBITS        00000000 0000e0 00008c 00   A  0   0  4 
47  [ 9] .rel.eh_frame     REL             00000000 0007ac 000028 08     13   8  4 
48  [10] .comment          PROGBITS        00000000 00016c 00002e 00      0   0  1 
49  [11] .note.GNU-stack   PROGBITS        00000000 00019a 000000 00      0   0  1 
50  [12] .shstrtab         STRTAB          00000000 00019a 00006a 00      0   0  1 
51  [13] .symtab           SYMTAB          00000000 00045c 000180 10     14  14  4 
52  [14] .strtab           STRTAB          00000000 0005dc 000166 00      0   0  1 
53Key to Flags: 
54  W (write), A (alloc), X (execute), M (merge), S (strings) 
55  I (info), L (link order), G (group), x (unknown) 
56  O (extra OS processing required) o (OS specific), p (processor specific) 

查看一個動態庫文件的elf文件的節信息:

 1[root@localhost test]$ readelf -S libmy.so 
 2There are 27 section headers, starting at offset 0xad0: 
 3
 4Section Headers: 
 5  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al 
 6  [ 0]                   NULL            00000000 000000 000000 00      0   0  0 
 7  [ 1] .gnu.hash         GNU_HASH        000000d4 0000d4 00003c 04   A  2   0  4 
 8  [ 2] .dynsym           DYNSYM          00000110 000110 000120 10   A  3   1  4 
 9  [ 3] .dynstr           STRTAB          00000230 000230 000199 00   A  0   0  1 
10  [ 4] .gnu.version      VERSYM          000003ca 0003ca 000024 02   A  2   0  2 
11  [ 5] .gnu.version_r    VERNEED         000003f0 0003f0 000050 00   A  3   2  4 
12  [ 6] .rel.dyn          REL             00000440 000440 0000b0 08   A  2   0  4 
13  [ 7] .rel.plt          REL             000004f0 0004f0 000010 08   A  2   9  4 
14  [ 8] .init             PROGBITS        00000500 000500 000017 00  AX  0   0  4 
15  [ 9] .plt              PROGBITS        00000518 000518 000030 04  AX  0   0  4 
16  [10] .text             PROGBITS        00000550 000550 0001c4 00  AX  0   0 16 
17  [11] .fini             PROGBITS        00000714 000714 00001c 00  AX  0   0  4 
18  [12] .rodata           PROGBITS        00000730 000730 000006 00   A  0   0  1 
19  [13] .eh_frame_hdr     PROGBITS        00000738 000738 00002c 00   A  0   0  4 
20  [14] .eh_frame         PROGBITS        00000764 000764 000090 00   A  0   0  4 
21  [15] .ctors            PROGBITS        000017f4 0007f4 00000c 00  WA  0   0  4 
22  [16] .dtors            PROGBITS        00001800 000800 000008 00  WA  0   0  4 
23  [17] .jcr              PROGBITS        00001808 000808 000004 00  WA  0   0  4 
24  [18] .data.rel.ro      PROGBITS        0000180c 00080c 000004 00  WA  0   0  4 
25  [19] .dynamic          DYNAMIC         00001810 000810 0000e0 08  WA  3   0  4 
26  [20] .got              PROGBITS        000018f0 0008f0 00000c 04  WA  0   0  4 
27  [21] .got.plt          PROGBITS        000018fc 0008fc 000014 04  WA  0   0  4 
28  [22] .bss              NOBITS          00001910 000910 00000c 00  WA  0   0  4 
29  [23] .comment          PROGBITS        00000000 000910 0000e6 00      0   0  1 
30  [24] .shstrtab         STRTAB          00000000 0009f6 0000da 00      0   0  1 
31  [25] .symtab           SYMTAB          00000000 000f08 000410 10     26  48  4 
32  [26] .strtab           STRTAB          00000000 001318 000333 00      0   0  1 
33Key to Flags: 
34  W (write), A (alloc), X (execute), M (merge), S (strings) 
35  I (info), L (link order), G (group), x (unknown) 
36  O (extra OS processing required) o (OS specific), p (processor specific) 

來源:https://github.com/jaywcjlove/linux-command

最後修改於: Wednesday, January 31, 2024

相關文章:

翻譯: