这个工具和objdump命令提供的功能类似,但是它显示的信息更为具体,并且它不依赖bfd库(bfd库是一个gnu项目,它的目标就是希望通过一种统一的接口来处理不同的目标文件);
- elf文件类型
- elf(executable and linking format)是一种对象文件的格式,用于定义不同类型的对象文件(object files)中都放了什么东西、以及都以什么样的格式去放这些东西。它自最早在 system v 系统上出现后,被 xnix 世界所广泛接受,作为缺省的二进制文件格式来使用。可以说,elf是构成众多xnix系统的基础之一。
elf文件有三种类型:
-
- 可重定位的对象文件(relocatable file)
- 由汇编器汇编生成的 .o 文件
-
- 可执行的对象文件(executable file)
- 可执行应用程序
-
- 可被共享的对象文件(shared object file)
- 动态库文件,也即 .so 文件
- .text section 里装载了可执行代码;
- .data section 里面装载了被初始化的数据;
- .bss section 里面装载了未被初始化的数据;
- 以 .rec 打头的 sections 里面装载了重定位条目;
- .symtab 或者 .dynsym section 里面装载了符号信息;
- .strtab 或者 .dynstr section 里面装载了字符串信息;
1. 参数说明
- -a –all 全部 equivalent to: -h -l -s -s -r -d -v -a -i
-
-h –file-header 文件头 display the elf file header
-
-l –program-headers 程序 display the program headers
-
–segments an alias for –program-headers
-
-s –section-headers 段头 display the sections’ header
-
--sections
-
an alias for –section-headers
-
-e –headers 全部头 equivalent to: -h -l -s
-
-s –syms 符号表 display the symbol table
-
--symbols
-
an alias for –syms
-
-n –notes 内核注释 display the core notes (if present)
-
-r –relocs 重定位 display the relocations (if present)
-
-u –unwind display the unwind info (if present)
-
-d –dynamic 动态段 display the dynamic segment (if present)
-
-v –version-info 版本 display the version sections (if present)
-
-a –arch-specific cpu构架 display architecture specific information (if any).
-
-d –use-dynamic 动态段 use the dynamic section info when displaying symbols
-
-x –hex-dump=
显示 段内内容dump the contents of section -
-w[liaprmffso] or
-
-i –histogram display histogram of bucket list lengths
-
-w –wide 宽行输出 allow output width to exceed 80 characters
-
-h –help display this information
-
-v –version display the version number of readelf
2. 示例
想知道一个应用程序的可运行的架构平台:
$readelf -h main| grep machine
-h选项将显示文件头的概要信息,从里面可以看到,有很多有用的信息:
$readelf -h main elf header: magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 class: elf64 data: 2 s complement, little endian version: 1 (current) os/abi: unix - system v abi version: 0 type: exec (executable file) machine: advanced micro devices x86-64 version: 0x1 entry point address: 0x400790 start of program headers: 64 (bytes into file) start of section headers: 5224 (bytes into file) flags: 0x0 size of this header: 64 (bytes) size of program headers: 56 (bytes) number of program headers: 8 size of section headers: 64 (bytes) number of section headers: 29 section header string table index: 26
一个编译好的应用程序,想知道其编译时是否使用了-g选项(加入调试信息):
$readelf -s main| grep debug
用-s选项是显示所有段信息;如果编译时使用了-g选项,则会有debug段;
查看.o文件是否编入了调试信息(编译的时候是否加了-g):
$readelf -s shpos.o | grep debug
3. 完整输出
readelf输出的完整内容:
$readelf -all a.out elf header: magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 class: elf32 data: 2's complement, little endian version: 1 (current) os/abi: unix - system v abi version: 0 type: exec (executable file) machine: intel 80386 version: 0x1 entry point address: 0x8048330 start of program headers: 52 (bytes into file) start of section headers: 4412 (bytes into file) flags: 0x0 size of this header: 52 (bytes) size of program headers: 32 (bytes) number of program headers: 9 size of section headers: 40 (bytes) number of section headers: 30 section header string table index: 27 section headers: [nr] name type addr off size es flg lk inf al [ 0] null 00000000 000000 000000 00 0 0 0 [ 1] .interp progbits 08048154 000154 000013 00 a 0 0 1 [ 2] .note.abi-tag note 08048168 000168 000020 00 a 0 0 4 [ 3] .note.gnu.build-i note 08048188 000188 000024 00 a 0 0 4 [ 4] .gnu.hash gnu_hash 080481ac 0001ac 000020 04 a 5 0 4 [ 5] .dynsym dynsym 080481cc 0001cc 000050 10 a 6 1 4 [ 6] .dynstr strtab 0804821c 00021c 00004c 00 a 0 0 1 [ 7] .gnu.version versym 08048268 000268 00000a 02 a 5 0 2 [ 8] .gnu.version_r verneed 08048274 000274 000020 00 a 6 1 4 [ 9] .rel.dyn rel 08048294 000294 000008 08 a 5 0 4 [10] .rel.plt rel 0804829c 00029c 000018 08 a 5 12 4 [11] .init progbits 080482b4 0002b4 00002e 00 ax 0 0 4 [12] .plt progbits 080482f0 0002f0 000040 04 ax 0 0 16 [13] .text progbits 08048330 000330 00018c 00 ax 0 0 16 [14] .fini progbits 080484bc 0004bc 00001a 00 ax 0 0 4 [15] .rodata progbits 080484d8 0004d8 000011 00 a 0 0 4 [16] .eh_frame_hdr progbits 080484ec 0004ec 000034 00 a 0 0 4 [17] .eh_frame progbits 08048520 000520 0000c4 00 a 0 0 4 [18] .ctors progbits 08049f14 000f14 000008 00 wa 0 0 4 [19] .dtors progbits 08049f1c 000f1c 000008 00 wa 0 0 4 [20] .jcr progbits 08049f24 000f24 000004 00 wa 0 0 4 [21] .dynamic dynamic 08049f28 000f28 0000c8 08 wa 6 0 4 [22] .got progbits 08049ff0 000ff0 000004 04 wa 0 0 4 [23] .got.plt progbits 08049ff4 000ff4 000018 04 wa 0 0 4 [24] .data progbits 0804a00c 00100c 000008 00 wa 0 0 4 [25] .bss nobits 0804a014 001014 000008 00 wa 0 0 4 [26] .comment progbits 00000000 001014 00002a 01 ms 0 0 1 [27] .shstrtab strtab 00000000 00103e 0000fc 00 0 0 1 [28] .symtab symtab 00000000 0015ec 000410 10 29 45 4 [29] .strtab strtab 00000000 0019fc 0001f9 00 0 0 1 key to flags: w (write), a (alloc), x (execute), m (merge), s (strings) i (info), l (link order), g (group), t (tls), e (exclude), x (unknown) o (extra os processing required) o (os specific), p (processor specific) there are no section groups in this file. program headers: type offset virtaddr physaddr filesiz memsiz flg align phdr 0x000034 0x08048034 0x08048034 0x00120 0x00120 r e 0x4 interp 0x000154 0x08048154 0x08048154 0x00013 0x00013 r 0x1 [requesting program interpreter: /lib/ld-linux.so.2] load 0x000000 0x08048000 0x08048000 0x005e4 0x005e4 r e 0x1000 load 0x000f14 0x08049f14 0x08049f14 0x00100 0x00108 rw 0x1000 dynamic 0x000f28 0x08049f28 0x08049f28 0x000c8 0x000c8 rw 0x4 note 0x000168 0x08048168 0x08048168 0x00044 0x00044 r 0x4 gnu_eh_frame 0x0004ec 0x080484ec 0x080484ec 0x00034 0x00034 r 0x4 gnu_stack 0x000000 0x00000000 0x00000000 0x00000 0x00000 rw 0x4 gnu_relro 0x000f14 0x08049f14 0x08049f14 0x000ec 0x000ec r 0x1 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 .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 03 .ctors .dtors .jcr .dynamic .got .got.plt .data .bss 04 .dynamic 05 .note.abi-tag .note.gnu.build-id 06 .eh_frame_hdr 07 08 .ctors .dtors .jcr .dynamic .got dynamic section at offset 0xf28 contains 20 entries: tag type name/value 0x00000001 (needed) shared library: [libc.so.6] 0x0000000c (init) 0x80482b4 0x0000000d (fini) 0x80484bc 0x6ffffef5 (gnu_hash) 0x80481ac 0x00000005 (strtab) 0x804821c 0x00000006 (symtab) 0x80481cc 0x0000000a (strsz) 76 (bytes) 0x0000000b (syment) 16 (bytes) 0x00000015 (debug) 0x0 0x00000003 (pltgot) 0x8049ff4 0x00000002 (pltrelsz) 24 (bytes) 0x00000014 (pltrel) rel 0x00000017 (jmprel) 0x804829c 0x00000011 (rel) 0x8048294 0x00000012 (relsz) 8 (bytes) 0x00000013 (relent) 8 (bytes) 0x6ffffffe (verneed) 0x8048274 0x6fffffff (verneednum) 1 0x6ffffff0 (versym) 0x8048268 0x00000000 (null) 0x0 relocation section '.rel.dyn' at offset 0x294 contains 1 entries: offset info type sym.value sym. name 08049ff0 00000206 r_386_glob_dat 00000000 __gmon_start__ relocation section '.rel.plt' at offset 0x29c contains 3 entries: offset info type sym.value sym. name 0804a000 00000107 r_386_jump_slot 00000000 printf 0804a004 00000207 r_386_jump_slot 00000000 __gmon_start__ 0804a008 00000307 r_386_jump_slot 00000000 __libc_start_main there are no unwind sections in this file. symbol table '.dynsym' contains 5 entries: num: value size type bind vis ndx name 0: 00000000 0 notype local default und 1: 00000000 0 func global default und printf@glibc_2.0 (2) 2: 00000000 0 notype weak default und __gmon_start__ 3: 00000000 0 func global default und __libc_start_main@glibc_2.0 (2) 4: 080484dc 4 object global default 15 _io_stdin_used symbol table '.symtab' contains 65 entries: num: value size type bind vis ndx name 0: 00000000 0 notype local default und 1: 08048154 0 section local default 1 2: 08048168 0 section local default 2 3: 08048188 0 section local default 3 4: 080481ac 0 section local default 4 5: 080481cc 0 section local default 5 6: 0804821c 0 section local default 6 7: 08048268 0 section local default 7 8: 08048274 0 section local default 8 9: 08048294 0 section local default 9 10: 0804829c 0 section local default 10 11: 080482b4 0 section local default 11 12: 080482f0 0 section local default 12 13: 08048330 0 section local default 13 14: 080484bc 0 section local default 14 15: 080484d8 0 section local default 15 16: 080484ec 0 section local default 16 17: 08048520 0 section local default 17 18: 08049f14 0 section local default 18 19: 08049f1c 0 section local default 19 20: 08049f24 0 section local default 20 21: 08049f28 0 section local default 21 22: 08049ff0 0 section local default 22 23: 08049ff4 0 section local default 23 24: 0804a00c 0 section local default 24 25: 0804a014 0 section local default 25 26: 00000000 0 section local default 26 27: 00000000 0 file local default abs crtstuff.c 28: 08049f14 0 object local default 18 __ctor_list__ 29: 08049f1c 0 object local default 19 __dtor_list__ 30: 08049f24 0 object local default 20 __jcr_list__ 31: 08048360 0 func local default 13 __do_global_dtors_aux 32: 0804a014 1 object local default 25 completed.6086 33: 0804a018 4 object local default 25 dtor_idx.6088 34: 080483c0 0 func local default 13 frame_dummy 35: 00000000 0 file local default abs crtstuff.c 36: 08049f18 0 object local default 18 __ctor_end__ 37: 080485e0 0 object local default 17 __frame_end__ 38: 08049f24 0 object local default 20 __jcr_end__ 39: 08048490 0 func local default 13 __do_global_ctors_aux 40: 00000000 0 file local default abs a.c 41: 08049f14 0 notype local default 18 __init_array_end 42: 08049f28 0 object local default 21 _dynamic 43: 08049f14 0 notype local default 18 __init_array_start 44: 08049ff4 0 object local default 23 _global_offset_table_ 45: 08048480 2 func global default 13 __libc_csu_fini 46: 08048482 0 func global hidden 13 __i686.get_pc_thunk.bx 47: 0804a00c 0 notype weak default 24 data_start 48: 00000000 0 func global default und printf@@glibc_2.0 49: 0804a014 0 notype global default abs _edata 50: 080484bc 0 func global default 14 _fini 51: 08049f20 0 object global hidden 19 __dtor_end__ 52: 0804a00c 0 notype global default 24 __data_start 53: 00000000 0 notype weak default und __gmon_start__ 54: 0804a010 0 object global hidden 24 __dso_handle 55: 080484dc 4 object global default 15 _io_stdin_used 56: 00000000 0 func global default und __libc_start_main@@glibc_ 57: 08048410 97 func global default 13 __libc_csu_init 58: 0804a01c 0 notype global default abs _end 59: 08048330 0 func global default 13 _start 60: 080484d8 4 object global default 15 _fp_hw 61: 0804a014 0 notype global default abs __bss_start 62: 080483e4 40 func global default 13 main 63: 00000000 0 notype weak default und _jv_registerclasses 64: 080482b4 0 func global default 11 _init histogram for `.gnu.hash' bucket list length (total of 2 buckets): length number % of total coverage 0 1 ( 50.0%) 1 1 ( 50.0%) 100.0% version symbols section '.gnu.version' contains 5 entries: addr: 0000000008048268 offset: 0x000268 link: 5 (.dynsym) 000: 0 (*local*) 2 (glibc_2.0) 0 (*local*) 2 (glibc_2.0) 004: 1 (*global*) version needs section '.gnu.version_r' contains 1 entries: addr: 0x0000000008048274 offset: 0x000274 link: 6 (.dynstr) 000000: version: 1 file: libc.so.6 cnt: 1 0x0010: name: glibc_2.0 flags: none version: 2 notes at offset 0x00000168 with length 0x00000020: owner data size description gnu 0x00000010 nt_gnu_abi_tag (abi version tag) os: linux, abi: 2.6.15 notes at offset 0x00000188 with length 0x00000024: owner data size description gnu 0x00000014 nt_gnu_build_id (unique build id bitstring) build id: 17fb9651029b6a8543bfafec9eea23bd16454e65
参考 elf文件格式