elfbase.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Contains ELF definitions
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit elfbase;
  18. {$i fpcdefs.inc}
  19. interface
  20. const
  21. EI_MAG0 = 0;
  22. ELFMAG0 = $7F;
  23. EI_MAG1 = 1;
  24. ELFMAG1 = ord('E');
  25. EI_MAG2 = 2;
  26. ELFMAG2 = ord('L');
  27. EI_MAG3 = 3;
  28. ELFMAG3 = ord('F');
  29. EI_CLASS = 4;
  30. ELFCLASSNONE = 0;
  31. ELFCLASS32 = 1;
  32. ELFCLASS64 = 2;
  33. EI_DATA = 5;
  34. ELFDATANONE = 0;
  35. ELFDATA2LSB = 1;
  36. ELFDATA2MSB = 2;
  37. EI_VERSION = 6;
  38. EI_OSABI = 7;
  39. ELFOSABI_NONE = 0; { UNIX System V ABI }
  40. ELFOSABI_HPUX = 1; { HP-UX operating system }
  41. ELFOSABI_NETBSD = 2; { NetBSD }
  42. ELFOSABI_LINUX = 3; { GNU/Linux }
  43. ELFOSABI_HURD = 4; { GNU/Hurd }
  44. ELFOSABI_SOLARIS = 6; { Solaris }
  45. ELFOSABI_AIX = 7; { AIX }
  46. ELFOSABI_IRIX = 8; { IRIX }
  47. ELFOSABI_FREEBSD = 9; { FreeBSD }
  48. ELFOSABI_TRU64 = 10; { TRU64 UNIX }
  49. ELFOSABI_MODESTO = 11; { Novell Modesto }
  50. ELFOSABI_OPENBSD = 12; { OpenBSD }
  51. ELFOSABI_OPENVMS = 13; { OpenVMS }
  52. ELFOSABI_NSK = 14; { Hewlett-Packard Non-Stop Kernel }
  53. ELFOSABI_AROS = 15; { AROS }
  54. ELFOSABI_FENIXOS = 16; { FenixOS }
  55. ELFOSABI_C6000_ELFABI = 64; { Bare-metal TMS320C6000 }
  56. ELFOSABI_C6000_LINUX = 65; { Linux TMS320C6000 }
  57. ELFOSABI_ARM = 97; { ARM }
  58. ELFOSABI_STANDALONE = 255; { Standalone (embedded) application }
  59. EI_ABIVERSION = 8;
  60. EI_PAD = 9;
  61. { ELFHeader.e_type }
  62. ET_NONE = 0;
  63. ET_REL = 1;
  64. ET_EXEC = 2;
  65. ET_DYN = 3;
  66. ET_CORE = 4;
  67. { ELFHeader.e_machine }
  68. EM_SPARC = 2;
  69. EM_386 = 3;
  70. EM_M68K = 4;
  71. EM_MIPS = 8;
  72. EM_PPC = 20;
  73. EM_ARM = 40;
  74. EM_X86_64 = 62;
  75. { ElfSechdr.sh_num }
  76. SHN_UNDEF = 0;
  77. SHN_LORESERVE = $FF00;
  78. SHN_ABS = $fff1;
  79. SHN_COMMON = $fff2;
  80. { ElfSechdr.sh_type }
  81. SHT_NULL = 0;
  82. SHT_PROGBITS = 1;
  83. SHT_SYMTAB = 2;
  84. SHT_STRTAB = 3;
  85. SHT_RELA = 4;
  86. SHT_HASH = 5;
  87. SHT_DYNAMIC = 6;
  88. SHT_NOTE = 7;
  89. SHT_NOBITS = 8;
  90. SHT_REL = 9;
  91. SHT_SHLIB = 10;
  92. SHT_DYNSYM = 11;
  93. SHT_INIT_ARRAY = 14;
  94. SHT_FINI_ARRAY = 15;
  95. SHT_PREINIT_ARRAY = 16;
  96. SHT_GROUP = 17;
  97. SHT_SYMTAB_SHNDX = 18;
  98. SHT_GNU_verdef = $6ffffffd;
  99. SHT_GNU_verneed = $6ffffffe;
  100. SHT_GNU_versym = $6fffffff;
  101. { ElfSechdr.sh_flags }
  102. SHF_WRITE = 1;
  103. SHF_ALLOC = 2;
  104. SHF_EXECINSTR = 4;
  105. SHF_MERGE = 16;
  106. SHF_STRINGS = 32;
  107. SHF_INFO_LINK = 64;
  108. SHF_LINK_ORDER = 128;
  109. SHF_OS_NONCONFORMING = 256;
  110. SHF_GROUP = 512;
  111. SHF_TLS = 1024;
  112. STB_LOCAL = 0;
  113. STB_GLOBAL = 1;
  114. STB_WEAK = 2;
  115. STT_NOTYPE = 0;
  116. STT_OBJECT = 1;
  117. STT_FUNC = 2;
  118. STT_SECTION = 3;
  119. STT_FILE = 4;
  120. STT_COMMON = 5;
  121. STT_TLS = 6;
  122. STT_GNU_IFUNC = 10;
  123. { program header types }
  124. PT_NULL = 0;
  125. PT_LOAD = 1;
  126. PT_DYNAMIC = 2;
  127. PT_INTERP = 3;
  128. PT_NOTE = 4;
  129. PT_SHLIB = 5;
  130. PT_PHDR = 6;
  131. PT_TLS = 7;
  132. PT_LOOS = $60000000;
  133. PT_HIOS = $6FFFFFFF;
  134. PT_LOPROC = $70000000;
  135. PT_HIPROC = $7FFFFFFF;
  136. PT_GNU_EH_FRAME = PT_LOOS + $474e550; { Frame unwind information }
  137. PT_GNU_STACK = PT_LOOS + $474e551; { Stack flags }
  138. PT_GNU_RELRO = PT_LOOS + $474e552; { Read-only after relocation }
  139. { program header flags }
  140. PF_X = 1;
  141. PF_W = 2;
  142. PF_R = 4;
  143. PF_MASKOS = $0FF00000; { OS-specific reserved bits }
  144. PF_MASKPROC = $F0000000; { Processor-specific reserved bits }
  145. { .dynamic tags }
  146. DT_NULL = 0;
  147. DT_NEEDED = 1;
  148. DT_PLTRELSZ = 2;
  149. DT_PLTGOT = 3;
  150. DT_HASH = 4;
  151. DT_STRTAB = 5;
  152. DT_SYMTAB = 6;
  153. DT_RELA = 7;
  154. DT_RELASZ = 8;
  155. DT_RELAENT = 9;
  156. DT_STRSZ = 10;
  157. DT_SYMENT = 11;
  158. DT_INIT = 12;
  159. DT_FINI = 13;
  160. DT_SONAME = 14;
  161. DT_RPATH = 15;
  162. DT_SYMBOLIC = 16;
  163. DT_REL = 17;
  164. DT_RELSZ = 18;
  165. DT_RELENT = 19;
  166. DT_PLTREL = 20;
  167. DT_DEBUG = 21;
  168. DT_TEXTREL = 22;
  169. DT_JMPREL = 23;
  170. DT_BIND_NOW = 24;
  171. DT_INIT_ARRAY = 25;
  172. DT_FINI_ARRAY = 26;
  173. DT_INIT_ARRAYSZ = 27;
  174. DT_FINI_ARRAYSZ = 28;
  175. DT_RUNPATH = 29;
  176. DT_FLAGS = 30;
  177. DT_ENCODING = 32;
  178. DT_PREINIT_ARRAY = 32;
  179. DT_PREINIT_ARRAYSZ = 33;
  180. DT_NUM = 34;
  181. DT_LOOS = $6000000D;
  182. DT_HIOS = $6ffff000;
  183. DT_LOPROC = $70000000;
  184. DT_HIPROC = $7fffffff;
  185. DT_RELACOUNT = $6ffffff9;
  186. DT_RELCOUNT = $6ffffffa;
  187. DT_FLAGS_1 = $6ffffffb;
  188. DT_VERDEF = $6ffffffc;
  189. DT_VERDEFNUM = $6ffffffd;
  190. DT_VERNEED = $6ffffffe;
  191. DT_VERNEEDNUM = $6fffffff;
  192. { GNU extension to Solaris versioning scheme }
  193. DT_VERSYM = $6ffffff0;
  194. GRP_COMDAT = 1;
  195. type
  196. { Structures which are written directly to the output file }
  197. TElf32header=record
  198. e_ident : array[0..15] of byte;
  199. e_type : word;
  200. e_machine : word;
  201. e_version : longword;
  202. e_entry : longword; { entrypoint }
  203. e_phoff : longword; { program header offset }
  204. e_shoff : longword; { sections header offset }
  205. e_flags : longword;
  206. e_ehsize : word; { elf header size in bytes }
  207. e_phentsize : word; { size of an entry in the program header array }
  208. e_phnum : word; { 0..e_phnum-1 of entrys }
  209. e_shentsize : word; { size of an entry in sections header array }
  210. e_shnum : word; { 0..e_shnum-1 of entrys }
  211. e_shstrndx : word; { index of string section header }
  212. end;
  213. TElf32sechdr=record
  214. sh_name : longword;
  215. sh_type : longword;
  216. sh_flags : longword;
  217. sh_addr : longword;
  218. sh_offset : longword;
  219. sh_size : longword;
  220. sh_link : longword;
  221. sh_info : longword;
  222. sh_addralign : longword;
  223. sh_entsize : longword;
  224. end;
  225. TElf32proghdr=record
  226. p_type : longword;
  227. p_offset : longword;
  228. p_vaddr : longword;
  229. p_paddr : longword;
  230. p_filesz : longword;
  231. p_memsz : longword;
  232. p_flags : longword;
  233. p_align : longword;
  234. end;
  235. TElf32reloc=record
  236. address : longword;
  237. info : longword; { bit 0-7: type, 8-31: symbol }
  238. addend : longint;
  239. end;
  240. TElf32symbol=record
  241. st_name : longword;
  242. st_value : longword;
  243. st_size : longword;
  244. st_info : byte; { bit 0-3: type, 4-7: bind }
  245. st_other : byte;
  246. st_shndx : word;
  247. end;
  248. TElf32Dyn=record
  249. d_tag: longword;
  250. case integer of
  251. 0: (d_val: longword);
  252. 1: (d_ptr: longword);
  253. end;
  254. telf64header=record
  255. e_ident : array[0..15] of byte;
  256. e_type : word;
  257. e_machine : word;
  258. e_version : longword;
  259. e_entry : qword; { entrypoint }
  260. e_phoff : qword; { program header offset }
  261. e_shoff : qword; { sections header offset }
  262. e_flags : longword;
  263. e_ehsize : word; { elf header size in bytes }
  264. e_phentsize : word; { size of an entry in the program header array }
  265. e_phnum : word; { 0..e_phnum-1 of entrys }
  266. e_shentsize : word; { size of an entry in sections header array }
  267. e_shnum : word; { 0..e_shnum-1 of entrys }
  268. e_shstrndx : word; { index of string section header }
  269. end;
  270. TElf64sechdr=record
  271. sh_name : longword;
  272. sh_type : longword;
  273. sh_flags : qword;
  274. sh_addr : qword;
  275. sh_offset : qword;
  276. sh_size : qword;
  277. sh_link : longword;
  278. sh_info : longword;
  279. sh_addralign : qword;
  280. sh_entsize : qword;
  281. end;
  282. telf64proghdr=record
  283. p_type : longword;
  284. p_flags : longword;
  285. p_offset : qword;
  286. p_vaddr : qword;
  287. p_paddr : qword;
  288. p_filesz : qword;
  289. p_memsz : qword;
  290. p_align : qword;
  291. end;
  292. telf64reloc=record
  293. address : qword;
  294. info : qword; { bit 0-31: type, 32-63: symbol }
  295. addend : int64; { signed! }
  296. end;
  297. telf64symbol=record
  298. st_name : longword;
  299. st_info : byte; { bit 0-3: type, 4-7: bind }
  300. st_other : byte;
  301. st_shndx : word;
  302. st_value : qword;
  303. st_size : qword;
  304. end;
  305. TElf64Dyn=record
  306. d_tag: qword;
  307. case integer of
  308. 0: (d_val: qword);
  309. 1: (d_ptr: qword);
  310. end;
  311. { The following records are same for 32 and 64 bit ELF files }
  312. TElfVerdef=record
  313. vd_version: word; { =VER_DEF_CURRENT }
  314. vd_flags: word;
  315. vd_ndx: word;
  316. vd_cnt: word; { number of verdaux records }
  317. vd_hash: longword; { ELF hash of version name }
  318. vd_aux: longword; { offset to verdaux records }
  319. vd_next: longword; { offset to next verdef record }
  320. end;
  321. TElfVerdaux=record
  322. vda_name: longword;
  323. vda_next: longword;
  324. end;
  325. TElfVerneed=record
  326. vn_version: word; { =VER_NEED_CURRENT }
  327. vn_cnt: word;
  328. vn_file: longword;
  329. vn_aux: longword;
  330. vn_next: longword;
  331. end;
  332. TElfVernaux=record
  333. vna_hash: longword;
  334. vna_flags: word;
  335. vna_other: word;
  336. vna_name: longword;
  337. vna_next: longword;
  338. end;
  339. const
  340. VERSYM_VERSION = $7FFF;
  341. VERSYM_HIDDEN = $8000;
  342. VER_NDX_LOCAL = 0;
  343. VER_NDX_GLOBAL = 1;
  344. { TElfverdef.vd_version }
  345. VER_DEF_CURRENT = 1;
  346. { TElfverneed.vn_version }
  347. VER_NEED_CURRENT = 1;
  348. { TElfverdef.vn_flags }
  349. VER_FLG_BASE = 1;
  350. VER_FLG_WEAK = 2;
  351. VER_FLG_INFO = 4;
  352. implementation
  353. end.