1.ELF之Elf Header

1.ELF的三个文件头架构

每一个ELF文件有三个文件头,用来索引信息。app

(1).EH = ELF file Header 可在此读到PH,SH在文件中的offset。ide

(2).PH = Program Header 与load program有关的索引,.o的PH为空。spa

(3).SH = Section Header 组成此文件的全部section的索引。操作系统

 

2. elf.hcode

先以32位的.o文件为例。blog

/usr/include/elf.h中能够查到三个文件头的组成结构,以及各个值对应的意义。索引

/* 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;

3.示例程序ci

bar.c编译器

int  bar0 = 0;
int  bar1 = 1;

这个.c只有两个变量定义组成。把它编译成bar.o。

4.对应关系:

 

(1)magic number:  给操做系统和编译器辨别此文件是ELF二进制库。

          0x45 0x4C 0x46为ELF三个字母的ASCII码。

(2)type: 文件类型

  Relocatable file = 1(.o, .a 可重定位文件,静态库)

  Executable file = 2(可执行文件,a.out,exe,运行库)

      Shared object file = 3(共享目标文件,.so,动态库)

(3)machine:架构

  eg:0x0028=40=ARM,  0x0003=3=Intel_80386

(4)version: ELF 版本,目前均为1

(5)entry:程序的入口地址(虚拟地址),.o文件没有入口,故为0。

      可执行文件应该为_start的虚拟地址。

(6)PH相关:    phoff:          H在文件中的offset

        phentsize:    PH的每一个entry有多大,Byte

        phnum:        PH有多少项entry

(7)SH相关:        shoff:           SH在文件中的offset

        shentsize:     SH的每一个entry有多大

        shnum:         SH有多少项entry

        shstrndx:      SH中String Table Section排第几项。

(8)flags:ABI版本

(9)ehsize:ELF Header的大小:0x0034=52(B),EH大小为固定值52B。

相关文章
相关标签/搜索