What
- 是一种文件格式(File Format) ABI,一种被广泛接受的 SPEC,
- 主要由四个部分组成:ELF Header (必须且位置固定)、Program Header Table、Segment (Sections) 和 Section Header Table
- 提供的能力(储存的信息)
- 32 bit / 64 bit
- 大端小端(MSB / LSB)
- 文件类型
- executable
- relocateable library
- shared library
- core dump
- Machine Architecture (eg: x86-64)
- 程序起始地址
- 链接,调试等元数据
Why
提供了一种标准化的方式来存储和组织可执行代码、数据和元信息,让操作系统知道如何处理二进制文件。
Alternatives:
What
Binary
$ hexdump -e '16/1 "%02x " "\n"' -v -n 64 quine
7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
02 00 3e 00 01 00 00 00 78 00 40 00 00 00 00 00
40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 40 00 38 00 01 00 00 00 00 00 00 00
- 头四个 byte 始终相同,是 elf 的 magic number,用于识别。
7f 45 4c 46
// The second, third, and fourth bytes [45 4c 46] are the string “ELF” encoded in ASCII.
- 接下来的三个字节告诉我们如何解释文件的其余部分
- 第 5 个字节告诉我们使用的是 32 位还是 64 位 ELF 格式
ELF Header
由于 ELF 的部份字段是 optional 的,所以只有一个固定位置的头部 ELF Header