1.ELF之Elf Header

1.ELF的三个文件头

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

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

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

(3).SH = Section Header 组成此文件的所有section的索引。

 

2. elf.h

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

/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.示例程序

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。

转载于:https://www.cnblogs.com/altc/p/8989493.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值