elf.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #pragma once
  2. #include <common/glib.h>
  3. // --> begin ==============EHDR=====================
  4. // Reference: https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-43405.html#scrolltoc
  5. // ====== ELF32 Header中的数据类型定义 ====
  6. typedef uint32_t Elf32_Addr;
  7. typedef uint16_t Elf32_Half;
  8. typedef uint32_t Elf32_Off;
  9. typedef uint32_t Elf32_SWord;
  10. typedef uint32_t Elf32_Word;
  11. // ====== ELF64 Header中的数据类型定义 ====
  12. typedef uint64_t Elf64_Addr;
  13. typedef uint16_t Elf64_Half;
  14. typedef uint64_t Elf64_Off;
  15. typedef uint32_t Elf64_Sword;
  16. typedef uint32_t Elf64_Word;
  17. typedef uint64_t Elf64_Xword;
  18. typedef uint64_t Elf64_Sxword;
  19. // ====== ELF Identification Index ======
  20. // Purpose: File identification
  21. #define EI_MAG0 0
  22. #define EI_MAG1 1
  23. #define EI_MAG2 2
  24. #define EI_MAG3 3
  25. // Purpose: File class
  26. #define EI_CLASS 4
  27. // Purpose: Data encoding
  28. #define EI_DATA 5
  29. // Purpose: File version
  30. #define EI_VERSION 6 // e_ident[EI_VERSION]指定了ELF header的版本号 当前这个值必须是EV_CURRENT
  31. // Purpose: Operating system/ABI identification
  32. #define EI_OSABI 7 // e_ident[EI_OSABI]指定了操作系统以及对象所对应的ABI
  33. // Purpose: ABI version
  34. #define EI_ABIVERSION 8 // e_ident[EI_ABIVERSION] 指定了对象所对应的ABI版本.
  35. // Purpose: Start of padding bytes
  36. #define EI_PAD 9 // 这个值标志了e_ident中未使用字节的的起始下标
  37. // Purpose: Size of e_ident[]
  38. #define EI_NIDENT 16
  39. // EI_MAG0 - EI_MAG3 这是一个4byte的 magic number
  40. #define ELFMAG0 0x7f
  41. #define ELFMAG1 'E'
  42. #define ELFMAG2 'L'
  43. #define ELFMAG3 'F'
  44. // EI_CLASS e_ident[EI_CLASS]指明了文件的类型或capacity
  45. #define ELFCLASSNONE 0 // Invalid class
  46. #define ELFCLASS32 1 // 32–bit objects
  47. #define ELFCLASS64 2 // 64–bit objects
  48. // EI_DATA e_ident[EI_DATA]指明了与处理器相关的数据的编码方式
  49. #define ELFDATANONE 0
  50. #define ELFDATA2LSB 1 // 小端对齐
  51. #define ELFDATA2MSB 2 // 大端对齐
  52. // ELF e_type的类型定义
  53. #define ET_NONE 0 // No file type
  54. #define ET_REL 1 // Relocatable file
  55. #define ET_EXEC 2 // Executable file
  56. #define ET_DYN 3 // Shared object file
  57. #define ET_CORE 4 // Core file
  58. #define ET_LOPROC 0xff00 // Processor-specific
  59. #define ET_HIPROC 0xffff // Processor-specific
  60. // e_machine的类型定义
  61. #define EM_NONE 0 // No machine
  62. #define EM_SPARC 2 // SPARC
  63. #define EM_386 3 // Intel 80386
  64. #define EM_SPARC32PLUS 18 // Sun SPARC 32+
  65. #define EM_SPARCV9 43 // SPARC V9
  66. #define EM_AMD64 62 // AMD 64
  67. // e_version的类型定义
  68. #define EV_NONE 0 // Invalid Version
  69. // EV_CURRENT: Value>=1 means current version
  70. // e_flags 定义
  71. // e_flags for SPARC
  72. #define EF_SPARC_EXT_MASK 0xffff00 // Vendor Extension mask
  73. #define EF_SPARC_32PLUS 0x000100 // Generic V8+ features
  74. #define EF_SPARC_SUN_US1 0x000200 // Sun UltraSPARC 1 Extensions
  75. #define EF_SPARC_HAL_R1 0x000400 // HAL R1 Extensions
  76. #define EF_SPARC_SUN_US3 0x000800 // Sun UltraSPARC 3 Extensions
  77. #define EF_SPARCV9_MM 0x3 // Mask for Memory Model
  78. #define EF_SPARCV9_TSO 0x0 // Total Store Ordering
  79. #define EF_SPARCV9_PSO 0x1 // Partial Store Ordering
  80. #define EF_SPARCV9_RMO 0x2 // Relaxed Memory Ordering
  81. #define PN_XNUM 0xffff
  82. typedef struct
  83. {
  84. unsigned char e_ident[EI_NIDENT];
  85. Elf32_Half e_type;
  86. Elf32_Half e_machine;
  87. Elf32_Word e_version;
  88. Elf32_Addr e_entry;
  89. Elf32_Off e_phoff;
  90. Elf32_Off e_shoff;
  91. Elf32_Word e_flags;
  92. Elf32_Half e_ehsize;
  93. Elf32_Half e_phentsize;
  94. Elf32_Half e_phnum;
  95. Elf32_Half e_shentsize;
  96. Elf32_Half e_shnum;
  97. Elf32_Half e_shstrndx;
  98. } Elf32_Ehdr;
  99. typedef struct
  100. {
  101. unsigned char e_ident[EI_NIDENT]; // 标志字节,这些字节与机器架构类型无关。目的是为了告诉我们如何解析这个文件的内容
  102. Elf64_Half e_type; // 文件类型标志符
  103. Elf64_Half e_machine; // 该文件依赖的处理器架构类型
  104. Elf64_Word e_version; // 对象文件的版本
  105. Elf64_Addr e_entry; // 进程的虚拟地址入点,使用字节偏移量表示。如果没有entry point,则该值为0
  106. Elf64_Off e_phoff; // The program header table's file offset in bytes. 若没有,则为0
  107. Elf64_Off e_shoff; // The section header table's file offset in bytes. 若没有,则为0
  108. Elf64_Word e_flags; // 与处理器相关联的flags。格式为: EF_machine_flag 如果是x86架构,那么该值为0
  109. Elf64_Half e_ehsize; // ELF Header的大小(单位:字节)
  110. Elf64_Half e_phentsize; // 程序的program header table中的一个entry的大小(所有的entry大小相同)
  111. Elf64_Half e_phnum; // program header table的entry数量
  112. // e_phentsize*e_phnum=program header table的大小
  113. // 如果没有program header table,该值为0
  114. // 如果entry num>=PN_XNUM(0xffff), 那么该值为0xffff,且真实的pht的entry数量存储在section header的sh_info中(index=0)
  115. // 其他情况下,第一个section header entry的sh_info的值为0
  116. Elf64_Half e_shentsize; // 每个section header的大小(字节
  117. // 每个section header是section header table的一个entry
  118. Elf64_Half e_shnum; // section header table的entry数量
  119. // e_shentsize*e_shnum=section header table的大小
  120. // 如果没有section header table,那么该值为0
  121. // 如果section的数量>=SHN_LORESERVE(0xff00),那么该值为0,且真实的section数量存储在
  122. // section header at index 0的sh_size变量中,否则第一个sh_size为0
  123. Elf64_Half e_shstrndx; // 与section name string表相关联的section header table的entry的索引下标
  124. // 如果没有name string table,那么该值等于SHN_UNDEF
  125. // 如果对应的index>=SHN_LORESERVE(0xff00), 那么该变量值为SHN_XINDEX(0xffff)
  126. // 且真正的section name string table的index被存放在section header的index=0处的sh_link变量中
  127. // 否则初始section header entry的sh_link变量为0
  128. } Elf64_Ehdr;
  129. // --> end ==============EHDR=====================
  130. // --> begin ==============SHDR=====================
  131. // reference: https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-94076.html#scrolltoc
  132. // ===== ELF Special Section Indexes =====
  133. #define SHN_UNDEF 0 // An undefined, missing, irrelevant, or otherwise meaningless section reference.
  134. #define SHN_LORESERVE 0xff00 // The lower boundary of the range of reserved indexes.
  135. // The system reserves indexes between SHN_LORESERVE and SHN_HIRESERVE, inclusive.
  136. #define SHN_LOPROC 0xff00 // SHN_LOPROC - SHN_HIPROC 这个范围以内的数据为处理器特定的语义所保留
  137. #define SHN_BEFORE 0xff00 // SHN_BEFORE, SHN_AFTER 与SHF_LINK_ORDER及SHF_ORDERED section flags一起,提供初始和终止section的
  138. #define SHN_AFTER 0xff01
  139. #define SHN_AMD64_LCOMMON 0xff02 // x64 specific common block label. This label is similar to SHN_COMMON, but provides for identifying a large common block.
  140. #define SHN_HIPROC 0xff1f
  141. #define SHN_LOOS 0xff20 // SHN_LOOS - SHN_HIOS 这个范围你的数为操作系统特定的语义所保留
  142. #define SHN_LOSUNW 0xff3f // SHN_LOSUNW - SHN_HISUNW Values in this inclusive range are reserved for Sun-specific semantics.
  143. #define SHN_SUNW_IGNORE 0xff3f // This section index provides a temporary symbol definition within relocatable objects. Reserved for internal use by dtrace(1M).
  144. #define SHN_HISUNW 0xff3f
  145. #define SHN_HIOS 0xff3f
  146. #define SHN_ABS 0xfff1 // 对应的引用的绝对值。 举个例子,symbols defined relative to section number SHN_ABS have absolute values and are not affected by relocation.
  147. #define SHN_COMMON 0xfff2 // Symbols defined relative to this section are common symbols
  148. #define SHN_XINDEX 0xffff
  149. #define SHN_HIRESERVE 0xffff // The upper boundary of the range of reserved indexes.
  150. /*
  151. Note -
  152. Although index 0 is reserved as the undefined value,
  153. the section header table contains an entry for index 0.
  154. That is, if the e_shnum member of the ELF header indicates
  155. a file has 6 entries in the section header table, the sections
  156. have the indexes 0 through 5. The contents of the initial entry
  157. are specified later in this section.
  158. */
  159. typedef struct
  160. {
  161. Elf32_Word sh_name;
  162. Elf32_Word sh_type;
  163. Elf32_Word sh_flags;
  164. Elf32_Addr sh_addr;
  165. Elf32_Off sh_offset;
  166. Elf32_Word sh_size;
  167. Elf32_Word sh_link;
  168. Elf32_Word sh_info;
  169. Elf32_Word sh_addralign;
  170. Elf32_Word sh_entsize;
  171. } Elf32_Shdr;
  172. typedef struct
  173. {
  174. Elf64_Word sh_name; // 段名
  175. Elf64_Word sh_type; // 段的类型(按照内容和语义来分类)
  176. Elf64_Xword sh_flags;
  177. Elf64_Addr sh_addr; // 该section在进程的内存空间中的基地址。如果该段不需要出现在内存中,该值为0
  178. Elf64_Off sh_offset; // The byte offset from the beginning of the file to the first byte in the section
  179. // 对于一个 SHT_NOBITS section,这个变量指的是概念上的偏移量。因为这种段并不是真正存在于文件中
  180. Elf64_Xword sh_size; // The section's size in bytes(如果是SHT_NOBITS类型的section,section不会在文件中真正占用sh_size的空间)
  181. Elf64_Word sh_link; // A section header table index link, whose interpretation depends on the section type.
  182. Elf64_Word sh_info; // 依赖于section type来解析的额外的信息。如果sh_flags有SHF_INFO_LINK属性,那么这个变量代表一个section header table index.
  183. Elf64_Xword sh_addralign; // 地址按照多少bytes对齐。只允许使用2的n次幂的值。如果值为0或1,则意味着地址没有对齐要求。
  184. Elf64_Xword sh_entsize; // 如果某个段拥有指定size的entry,则在这里指定,否则为0
  185. } Elf64_Shdr;
  186. // ELF Section Types, sh_type
  187. #define SHT_NULL 0
  188. #define SHT_PROGBITS 1
  189. #define SHT_SYMTAB 2 // Identifies a symbol table
  190. #define SHT_STRTAB 3 // Identifies a string table.
  191. #define SHT_RELA 4
  192. #define SHT_HASH 5
  193. #define SHT_DYNAMIC 6
  194. #define SHT_NOTE 7
  195. #define SHT_NOBITS 8
  196. #define SHT_REL 9
  197. #define SHT_SHLIB 10
  198. #define SHT_DYNSYM 11 // Identifies a symbol table
  199. #define SHT_INIT_ARRAY 14
  200. #define SHT_FINI_ARRAY 15
  201. #define SHT_PREINIT_ARRAY 16
  202. #define SHT_GROUP 17
  203. #define SHT_SYMTAB_SHNDX 18
  204. #define SHT_LOOS 0x60000000
  205. #define SHT_LOSUNW 0x6fffffef
  206. #define SHT_SUNW_capchain 0x6fffffef
  207. #define SHT_SUNW_capinfo 0x6ffffff0
  208. #define SHT_SUNW_symsort 0x6ffffff1
  209. #define SHT_SUNW_tlssort 0x6ffffff2
  210. #define SHT_SUNW_LDYNSYM 0x6ffffff3 // Identifies a symbol table
  211. #define SHT_SUNW_dof 0x6ffffff4
  212. #define SHT_SUNW_cap 0x6ffffff5
  213. #define SHT_SUNW_SIGNATURE 0x6ffffff6
  214. #define SHT_SUNW_ANNOTATE 0x6ffffff7
  215. #define SHT_SUNW_DEBUGSTR 0x6ffffff8
  216. #define SHT_SUNW_DEBUG 0x6ffffff9
  217. #define SHT_SUNW_move 0x6ffffffa
  218. #define SHT_SUNW_COMDAT 0x6ffffffb
  219. #define SHT_SUNW_syminfo 0x6ffffffc
  220. #define SHT_SUNW_verdef 0x6ffffffd
  221. #define SHT_SUNW_verneed 0x6ffffffe
  222. #define SHT_SUNW_versym 0x6fffffff
  223. #define SHT_HISUNW 0x6fffffff
  224. #define SHT_HIOS 0x6fffffff
  225. #define SHT_LOPROC 0x70000000
  226. #define SHT_SPARC_GOTDATA 0x70000000
  227. #define SHT_AMD64_UNWIND 0x70000001
  228. #define SHT_HIPROC 0x7fffffff
  229. #define SHT_LOUSER 0x80000000
  230. #define SHT_HIUSER 0xffffffff
  231. // ELF Section Attribute Flags
  232. #define SHF_WRITE 0x1 // Identifies a section that should be writable during process execution
  233. #define SHF_ALLOC 0x2 // Identifies a section that occupies memory during process execution
  234. #define SHF_EXECINSTR 0x4 // contains executable machine instructions
  235. #define SHF_MERGE 0x10
  236. #define SHF_STRINGS 0x20
  237. #define SHF_INFO_LINK 0x40 // This section headers sh_info field holds a section header table index
  238. #define SHF_LINK_ORDER 0x80 // This section adds special ordering requirements to the link-editor
  239. #define SHF_OS_NONCONFORMING 0x100
  240. #define SHF_GROUP 0x200
  241. #define SHF_TLS 0x400
  242. #define SHF_MASKOS 0x0ff00000
  243. #define SHF_AMD64_LARGE 0x10000000 // identifies a section that can hold more than 2 Gbyte
  244. #define SHF_ORDERED 0x40000000
  245. #define SHF_EXCLUDE 0x80000000
  246. #define SHF_MASKPROC 0xf0000000
  247. // --> end ==============SHDR=====================
  248. // --> begin ========== symbol table section ======
  249. typedef struct
  250. {
  251. Elf32_Word st_name;
  252. Elf32_Addr st_value;
  253. Elf32_Word st_size;
  254. unsigned char st_info;
  255. unsigned char st_other;
  256. Elf32_Half st_shndx;
  257. } Elf32_Sym;
  258. typedef struct
  259. {
  260. Elf64_Word st_name;
  261. unsigned char st_info;
  262. unsigned char st_other;
  263. Elf64_Half st_shndx;
  264. Elf64_Addr st_value;
  265. Elf64_Xword st_size;
  266. } Elf64_Sym;
  267. // --> end ========== symbol table section ======
  268. // --> begin ========== program header =========
  269. // Ref: https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-83432.html#scrolltoc
  270. typedef struct
  271. {
  272. Elf32_Word p_type;
  273. Elf32_Off p_offset;
  274. Elf32_Addr p_vaddr;
  275. Elf32_Addr p_paddr;
  276. Elf32_Word p_filesz;
  277. Elf32_Word p_memsz;
  278. Elf32_Word p_flags;
  279. Elf32_Word p_align;
  280. } Elf32_Phdr;
  281. typedef struct
  282. {
  283. Elf64_Word p_type;
  284. Elf64_Word p_flags;
  285. Elf64_Off p_offset;
  286. Elf64_Addr p_vaddr;
  287. Elf64_Addr p_paddr;
  288. Elf64_Xword p_filesz;
  289. Elf64_Xword p_memsz;
  290. Elf64_Xword p_align;
  291. } Elf64_Phdr;
  292. // ELF segment types
  293. #define PT_NULL 0
  294. #define PT_LOAD 1 // Specifies a loadable segment
  295. #define PT_DYNAMIC 2 // Specifies dynamic linking information.
  296. #define PT_INTERP 3 // Specifies the location and size of a null-terminated path name to invoke as an interpreter
  297. #define PT_NOTE 4 // Specifies the location and size of auxiliary information
  298. #define PT_SHLIB 5
  299. #define PT_PHDR 6 // Specifies the location and size of the program header table
  300. #define PT_TLS 7 // Specifies a thread-local storage template
  301. /*
  302. PT_LOOS - PT_HIOS
  303. Values in this inclusive range are reserved for OS-specific semantics.
  304. */
  305. #define PT_LOOS 0x60000000
  306. #define PT_SUNW_UNWIND 0x6464e550
  307. #define PT_SUNW_EH_FRAME 0x6474e550
  308. #define PT_LOSUNW 0x6ffffffa
  309. #define PT_SUNWBSS 0x6ffffffa
  310. #define PT_SUNWSTACK 0x6ffffffb
  311. #define PT_SUNWDTRACE 0x6ffffffc
  312. #define PT_SUNWCAP 0x6ffffffd
  313. #define PT_HISUNW 0x6fffffff
  314. #define PT_HIOS 0x6fffffff
  315. #define PT_LOPROC 0x70000000
  316. #define PT_HIPROC 0x7fffffff
  317. // ELF Segment Flags
  318. #define PF_X 0x1 // Execute
  319. #define PF_W 0x2 // Write
  320. #define PF_R 0x4 // Read
  321. #define PF_MASKPROC 0xf0000000 // Unspecified
  322. // --> end ========== program header =========
  323. /**
  324. * @brief 校验是否为ELF文件
  325. *
  326. * @param ehdr
  327. */
  328. bool elf_check(void * ehdr);