ELF 文件有三种类型:可重定位文件:也就是通常称的目标文件,后缀为.o。共享文件:也就是通常称的库文件,后缀为.so。可执行文件:本文主要讨论的文件格式,总的来说,可执行文件的格式与上述两种文件的格式之间的区别主要在于观察的角度不同:一种称为连接视图(Linking View),一种称为执行视图(Execution View)。
一个典型的ELF文件有两种描述视图:program header和section header.
program header:是对程序运行时所使用的段的描述.
section header: 是对所有二进制段的描述.
每一个ELF文件是由一个ELF 文件头(ELF header)和其余的文件数据构成.这些文件数据包括一下一些内容:
·Program header table 描述0个或是多个段(segments)
·Section header table, 描述0个或是多个节(sections)
·要写到上面两个表中的数据.
段(segments)包含的是程序运行是必要的信息
节(sections)包含的是链接和重定向时所需要的重要数据
同一时间整个文件中的每个beyt不会属于一个以上的段,但是也可以存在不属于任何段的字节.
ELF文件分析工具:
readelfis: 是一个unix下的二进制工具,用来显示一个或多个ELF文件的信息.
elfdump: 是一个Solaris命令,用来查看单个ELF文件的信息.
objdump: 可以查看ELF文件或是其它对象格式的更多信息.
ELF文件头的描述:
typedef struct elf32_hdr{
unsigned char e_ident[EI_NIDENT];//魔数和相关信息
Elf32_Half e_type; //文件类型
Elf32_Half e_machine; //针对体系结构
Elf32_Word e_version; //版本信息
Elf32_Addr e_entry; //Entry point 程序入口点
Elf32_Off e_phoff; //程序头偏移量
Elf32_Off e_shoff; //节偏移量
Elf32_Word e_flags; //处理器特定标志
Elf32_Half e_ehsize; //文件头长度
Elf32_Half e_phentsize;//程序头部长度
Elf32_Half e_phnum; //程序头部个数
Elf32_Half e_shentsize;//节头部长度
Elf32_Half e_shnum; //节头部个数
Elf32_Half e_shstrndx; //节头部字符索引
} Elf32_Ehdr;
e_ident [0]-e_ident[3]包含了ELF文件的魔数,依次是0x7f、'E'、'L'、'F'。注意,任何一个ELF 文件必须包含此魔数。e_ident[4]表示硬件系统的位数,1代表32位,2代表64位。 e_ident[5] 表示数据编码方式,1代表小印第安排序(最大有意义的字节占有最低的地址),2代表大印第安排序(最大有意义的字节占有最高的地址)。e_ident [6]指定ELF头部的版本,当前必须为1。e_ident[7]到e_ident[14]是填充符,通常是0。ELF格式规范中定义这几个字节是被忽略的,但实际上是这几个字节完全可以可被利用。如有些病毒设置 e_ident[7]为0x21,表示本文件已被感染;或者存放可执行代码。ELF头部中大多数字段都是对子头部数据的描述,其意义相对比较简单。值得注意的是某些病毒可能修改字段e_entry(程序进入点)的值,以指向病毒代码.下面是用readelf工具读取的一个elf文件文件头.使用命令"readelf -h hello"即可.
helight@helight-desktop:~/linux/do-while$ readelf -h hello
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: 0x80482f0
Start of program headers: 52 (bytes into file)
Start of section headers: 3220 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 7
Size of section headers: 40 (bytes)
Number of section headers: 36
Section header string table index: 33
helight@helight-desktop:~/linux/do-while$
对应上面的数据结构可以很清楚的理解这里读取的文件头.
紧接ELF头部的是程序头表,它是一个结构数组,包含了ELF头表中字段e_phnum定义的条目,结构描述一个段或其他系统准备执行该程序所需要的信息。
ELF文件程序头的描述
typedef struct elf32_phdr{
Elf32_Word p_type; //段类型
Elf32_Off p_offset; //段位置相对于文件开始处的偏移量
Elf32_Addr p_vaddr; //段在内存中地址
Elf32_Addr p_paddr; //段的物理地址
Elf32_Word p_filesz; //段在文件中的长度
Elf32_Word p_memsz; //段在内存中的长度
Elf32_Word p_flags; //段标志
Elf32_Word p_align; //段在内存中的对齐标志
} Elf32_Phdr
下面是用readelf工具读取的一个elf文件程序头.使用命令"readelf -l hello"即可.
helight@helight-desktop:~/linux/do-while$ readelf -l hello
Elf file type is EXEC (Executable file) //文件类型
Entry point 0x80482f0 //程序入口点
There are 7 program headers, starting at offset 52 //有7个程序头,偏移量52
//7个程序头: PHDR,INTERP,LOAD,LOAD,DYNAMIC,NOTE,GUN_STACK
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000034 0x08048034 0x08048034 0x000e0 0x000e0 R E 0x4
INTERP 0x000114 0x08048114 0x08048114 0x00013 0x00013 R 0x1
[Requesting program interpreter: /lib/ld-linux.so.2]
LOAD 0x000000 0x08048000 0x08048000 0x00474 0x00474 R E 0x1000
LOAD 0x000474 0x08049474 0x08049474 0x0010c 0x00110 RW 0x1000
DYNAMIC 0x000488 0x08049488 0x08049488 0x000d0 0x000d0 RW 0x4
NOTE 0x000128 0x08048128 0x08048128 0x00020 0x00020 R 0x4
GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4
Section to Segment mapping://节到段的映射
Segment Sections...
00
01 .interp
02 .interp .note.ABI-tag .hash .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame
03 .ctors .dtors .jcr .dynamic .got .got.plt .data .bss
04 .dynamic
05 .note.ABI-tag
06
helight@helight-desktop:~/linux/do-while$
对一个ELF可执行程序而言,一个基本的段是标记p_type为PT_INTERP的段,它表明了运行此程序所需要的程序解释器(/lib/ld- linux.so.2),实际上也就是动态连接器(dynamic linker)。最重要的段是标记p_type为PT_LOAD的段,它表明了为运行程序而需要加载到内存的数据。查看上面实际输入,可以看见有两个可 LOAD段,第一个为只读可执行(FLg为R E),第二个为可读可写(Flg为RW)。
段1包含了文本节.text,注意到ELF文件头部中程序进入点的值为0x80482f0,正好是指向节. text在内存中的地址。段二包含了数据节.data,此数据节中数据是可读可写的,相对的只读数据节.rodata包含在段1中。
在I386平台LINUX系统下,用命令file查看一个ELF可执行程序的可能输出是:
helight@helight-desktop:~/linux/do-while$ file hello
hello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.8, dynamically linked (uses shared libs), not stripped
一下是用readelf工具查看的一些段信息
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 08048114 000114 000013 00 A 0 0 1
[ 2] .note.ABI-tag NOTE 08048128 000128 000020 00 A 0 0 4
[ 3] .hash HASH 08048148 000148 000028 04 A 5 0 4
[ 4] .gnu.hash GNU_HASH 08048170 000170 000020 04 A 5 0 4
[ 5] .dynsym DYNSYM 08048190 000190 000050 10 A 6 1 4
[ 6] .dynstr STRTAB 080481e0 0001e0 00004a 00 A 0 0 1
[ 7] .gnu.version VERSYM 0804822a 00022a 00000a 02 A 5 0 2
[ 8] .gnu.version_r VERNEED 08048234 000234 000020 00 A 6 1 4
[ 9] .rel.dyn REL 08048254 000254 000008 08 A 5 0 4
[10] .rel.plt REL 0804825c 00025c 000018 08 A 5 12 4
[11] .init PROGBITS 08048274 000274 000030 00 AX 0 0 4
[12] .plt PROGBITS 080482a4 0002a4 000040 04 AX 0 0 4
[13] .text PROGBITS 080482f0 0002f0 00014c 00 AX 0 0 16
[14] .fini PROGBITS 0804843c 00043c 00001c 00 AX 0 0 4
[15] .rodata PROGBITS 08048458 000458 000015 00 A 0 0 4
[16] .eh_frame PROGBITS 08048470 000470 000004 00 A 0 0 4
[17] .ctors PROGBITS 08049474 000474 000008 00 WA 0 0 4
[18] .dtors PROGBITS 0804947c 00047c 000008 00 WA 0 0 4
[19] .jcr PROGBITS 08049484 000484 000004 00 WA 0 0 4
[20] .dynamic DYNAMIC 08049488 000488 0000d0 08 WA 6 0 4
[21] .got PROGBITS 08049558 000558 000004 04 WA 0 0 4
[22] .got.plt PROGBITS 0804955c 00055c 000018 04 WA 0 0 4
[23] .data PROGBITS 08049574 000574 00000c 00 WA 0 0 4
[24] .bss NOBITS 08049580 000580 000004 00 WA 0 0 4
[25] .comment PROGBITS 00000000 000580 000126 00 0 0 1
[26] .debug_aranges PROGBITS 00000000 0006a8 000050 00 0 0 8
[27] .debug_pubnames PROGBITS 00000000 0006f8 000025 00 0 0 1
[28] .debug_info PROGBITS 00000000 00071d 0001a7 00 0 0 1
[29] .debug_abbrev PROGBITS 00000000 0008c4 00006f 00 0 0 1
[30] .debug_line PROGBITS 00000000 000933 000129 00 0 0 1
[31] .debug_str PROGBITS 00000000 000a5c 0000bb 01 MS 0 0 1
[32] .debug_ranges PROGBITS 00000000 000b18 000040 00 0 0 8
[33] .shstrtab STRTAB 00000000 000b58 000139 00 0 0 1
[34] .symtab SYMTAB 00000000 001234 0004a0 10 35 55 4
[35] .strtab STRTAB 00000000 0016d4 000206 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
最后附上程序:
#include <stdio.h>
int main()
{
printf("hello world/r/n");
return 0;
}
5万+

被折叠的 条评论
为什么被折叠?



