ELF文件格式

ELF是linux下的可执行与链接文件格式。

写个简单的程序验证下:

#include <stdio.h>

int main(int argc, char *argv[])
{
    return 0;
}
ELF文件会在起始处包含一个ELF文件头,该结构位于elf.h文件中的ELFN_Ehdr。
 typedef struct {
               unsigned char e_ident[EI_NIDENT];
               uint16_t      e_type;
               uint16_t      e_machine;
               uint32_t      e_version;
               ElfN_Addr     e_entry;
               ElfN_Off      e_phoff;
               ElfN_Off      e_shoff;
               uint32_t      e_flags;
               uint16_t      e_ehsize;
               uint16_t      e_phentsize;
               uint16_t      e_phnum;
               uint16_t      e_shentsize;
               uint16_t      e_shnum;
               uint16_t      e_shstrndx;
           } ElfN_Ehdr;
e_ident为大小EI_NIDENT的char数组,它定义了当前文件格式类型(32位或64位),存储格式(大端或小端),系统调用方式,目标操作系统类型等信息。

使用readelf工具查看刚才编译的文件

<div> [test@localhost]~/Study/linuxc/18.5% readelf test.o -h
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:               0x400390
  Start of program headers:          64 (bytes into file)
  Start of section headers:          2368 (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:         30
  Section header string table index: 27
</div>
这里看到文件为64位ELF文件,使用小端和补码表示数,Version是啥我也没搞清楚,目标操作系统及系统调用方式为UNIX - System V。
e_type制定了ELF文件类型,这里是EXEC,代表可执行文件,其他类型还有重定位文件,内核文件和共享库文件。

e_machine目标硬件平台

e_version

e_entry可执行文件的入口点虚拟地址

e_phoff程序头在文件中的偏移地址

e_shoff段头在文件中的偏移地址

e_flags与文件关联的特定于处理器的标志

e_ehsize当前文件头大小

e_phentsize程序头大小

e_phnum程序头数量

e_shentsize段头大小

e_shnum段头表中的段头数量

e_shstrndx字符串表在段头表中的索引

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值