分享

ELF文件头解析

 t涂鸦 2012-03-06

命令:

readelf  -h main.o

 -h --file-header       Display the ELF file header

结构:

ELF Header:

  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00    //elf 魔数

  Class:                             ELF32 //机器字节长度

  Data:                              2's complement, little endian    //数据存储方式,2的补码,小端模式

  Version:                           1 (current)  //版本

  OS/ABI:                            UNIX - System V  //操作系统

  ABI Version:                       0 //ABI 版本

  Type:                              REL (Relocatable file)  //文件类新,可重定向文件,可以被用来链接成可执行文件或共享目标文件,静态链接库格式

  Machine:                           Intel 80386  //硬件平台

  Version:                           0x1  //硬件平台版本

  Entry point address:               0x0  //入口地址

  Start of program headers:          0 (bytes into file)  //程序头入口

  Start of section headers:          276 (bytes into file)  //段表的位置

  Flags:                             0x0  

  Size of this header:               52 (bytes)  

  Size of program headers:           0 (bytes)

  Number of program headers:         0

  Size of section headers:           40 (bytes)

  Number of section headers:         11  

  Section header string table index: 8


ELF文件头结构和相关常数被定义再/usr/include /elf.h 中,因为ELF文件再各种平台下都通用,ELF文件有32位版本和64位版本。它的头文件结构也有两种版本,分别是“Elf32_Ehdr”和"Elf64_Ehdr".32位版本和64位版本的ELF文件的文件头内容是一样的,只不过成员的大小不一样。为了对每个成员的大小做出明确的规定便于在不同的编译环境下都拥有相同的字段长度。elf.h使用typedef定义了一套自己的变量体系。

看一下定义:(取自/usr/include/elf.h)

/* Types for signed and unsigned 64-bit quantities.  */

typedef uint64_t Elf32_Xword;

typedef int64_t  Elf32_Sxword;

typedef uint64_t Elf64_Xword;

typedef int64_t  Elf64_Sxword;

 

/* Type of addresses.  */

typedef uint32_t Elf32_Addr;

typedef uint64_t Elf64_Addr;

 

/* Type of file offsets.  */

typedef uint32_t Elf32_Off;

typedef uint64_t Elf64_Off;

 

/* Type for section indices, which are 16-bit quantities.  */

typedef uint16_t Elf32_Section;

typedef uint16_t Elf64_Section;

 

/* Type for version symbol information.  */

typedef Elf32_Half Elf32_Versym;

typedef Elf64_Half Elf64_Versym;

 

 

/* The ELF file header.  This appears at the start of every ELF file.  */

//上面定义了一些平台相关的变量

#define EI_NIDENT (16)

typedef struct

{

  unsigned char e_ident[EI_NIDENT];     /* Magic number and other info */

  Elf32_Half    e_type;                 /* Object file type */

  Elf32_Half    e_machine;              /* Architecture */

  Elf32_Word    e_version;              /* Object file version */

  Elf32_Addr    e_entry;                /* Entry point virtual address */

  Elf32_Off     e_phoff;                /* Program header table file offset */

  Elf32_Off     e_shoff;                /* Section header table file offset */

  Elf32_Word    e_flags;                /* Processor-specific flags */

  Elf32_Half    e_ehsize;               /* ELF header size in bytes */

  Elf32_Half    e_phentsize;            /* Program header table entry size */

  Elf32_Half    e_phnum;                /* Program header table entry count */

  Elf32_Half    e_shentsize;            /* Section header table entry size */

  Elf32_Half    e_shnum;                /* Section header table entry count */

  Elf32_Half    e_shstrndx;             /* Section header string table index */

} Elf32_Ehdr;

//注释讲的很清楚了

 

一、ELF魔数

用来标识ELF文件的平台属性,比如这个ELF字长度、字节序、ELF文件版本

  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00

a、开始4个字节7f 45 4c 46是所有ELF文件相同的。第一个字节对应ascii中的DEL控制符,后面三个是ELF这三个字母的ascii码。被称为ELF文件的魔数。每种可执行的文件都有不同的魔数,操作系统在加载可执行文件的时候都会确认魔数是否正确。

b、第五个字节是标识ELF的文件类,01表示32位,02表示64位。

c、第六个字节是字节序,规定该ELF文件的大小端

d、第七个是ELF文件的版本号,ELF标准再1.2以后就没有更新了。

e、后面的9个字节没有定义

 

二、文件类型

3中ELF文件类型,每个类型对应一个常量。系统通过这个常量来判断文件类型,而不是文件的扩展名。

ET_REL    1     可重定位文件

ET_EXEC  2     可执行文件

ET_DYN    3    共享目标文件

 

三、机器类型

ELF文件格式被设计成可以再多个平台下使用,但是并不表示同一个ELF文件可以在不同平台下面使用,而是不同平台的ELF文件都遵循同一套标准。

EM_M32 1  AT&T WE 32100

EM_386   3  INTEL X86

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多