elfbase.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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_ATTRIBUTES = $6ffffff5;
  99. SHT_GNU_HASH = $6ffffff6;
  100. SHT_GNU_LIBLIST = $6ffffff7;
  101. SHT_GNU_verdef = $6ffffffd;
  102. SHT_GNU_verneed = $6ffffffe;
  103. SHT_GNU_versym = $6fffffff;
  104. { ElfSechdr.sh_flags }
  105. SHF_WRITE = 1;
  106. SHF_ALLOC = 2;
  107. SHF_EXECINSTR = 4;
  108. SHF_MERGE = 16;
  109. SHF_STRINGS = 32;
  110. SHF_INFO_LINK = 64;
  111. SHF_LINK_ORDER = 128;
  112. SHF_OS_NONCONFORMING = 256;
  113. SHF_GROUP = 512;
  114. SHF_TLS = 1024;
  115. STB_LOCAL = 0;
  116. STB_GLOBAL = 1;
  117. STB_WEAK = 2;
  118. STT_NOTYPE = 0;
  119. STT_OBJECT = 1;
  120. STT_FUNC = 2;
  121. STT_SECTION = 3;
  122. STT_FILE = 4;
  123. STT_COMMON = 5;
  124. STT_TLS = 6;
  125. STT_GNU_IFUNC = 10;
  126. { program header types }
  127. PT_NULL = 0;
  128. PT_LOAD = 1;
  129. PT_DYNAMIC = 2;
  130. PT_INTERP = 3;
  131. PT_NOTE = 4;
  132. PT_SHLIB = 5;
  133. PT_PHDR = 6;
  134. PT_TLS = 7;
  135. PT_LOOS = $60000000;
  136. PT_HIOS = $6FFFFFFF;
  137. PT_LOPROC = $70000000;
  138. PT_HIPROC = $7FFFFFFF;
  139. PT_GNU_EH_FRAME = PT_LOOS + $474e550; { Frame unwind information }
  140. PT_GNU_STACK = PT_LOOS + $474e551; { Stack flags }
  141. PT_GNU_RELRO = PT_LOOS + $474e552; { Read-only after relocation }
  142. { program header flags }
  143. PF_X = 1;
  144. PF_W = 2;
  145. PF_R = 4;
  146. PF_MASKOS = $0FF00000; { OS-specific reserved bits }
  147. PF_MASKPROC = $F0000000; { Processor-specific reserved bits }
  148. { .dynamic tags }
  149. DT_NULL = 0;
  150. DT_NEEDED = 1;
  151. DT_PLTRELSZ = 2;
  152. DT_PLTGOT = 3;
  153. DT_HASH = 4;
  154. DT_STRTAB = 5;
  155. DT_SYMTAB = 6;
  156. DT_RELA = 7;
  157. DT_RELASZ = 8;
  158. DT_RELAENT = 9;
  159. DT_STRSZ = 10;
  160. DT_SYMENT = 11;
  161. DT_INIT = 12;
  162. DT_FINI = 13;
  163. DT_SONAME = 14;
  164. DT_RPATH = 15;
  165. DT_SYMBOLIC = 16;
  166. DT_REL = 17;
  167. DT_RELSZ = 18;
  168. DT_RELENT = 19;
  169. DT_PLTREL = 20;
  170. DT_DEBUG = 21;
  171. DT_TEXTREL = 22;
  172. DT_JMPREL = 23;
  173. DT_BIND_NOW = 24;
  174. DT_INIT_ARRAY = 25;
  175. DT_FINI_ARRAY = 26;
  176. DT_INIT_ARRAYSZ = 27;
  177. DT_FINI_ARRAYSZ = 28;
  178. DT_RUNPATH = 29;
  179. DT_FLAGS = 30;
  180. DT_ENCODING = 32;
  181. DT_PREINIT_ARRAY = 32;
  182. DT_PREINIT_ARRAYSZ = 33;
  183. DT_NUM = 34;
  184. DT_LOOS = $6000000D;
  185. DT_HIOS = $6ffff000;
  186. DT_LOPROC = $70000000;
  187. DT_HIPROC = $7fffffff;
  188. DT_RELACOUNT = $6ffffff9;
  189. DT_RELCOUNT = $6ffffffa;
  190. DT_FLAGS_1 = $6ffffffb;
  191. DT_VERDEF = $6ffffffc;
  192. DT_VERDEFNUM = $6ffffffd;
  193. DT_VERNEED = $6ffffffe;
  194. DT_VERNEEDNUM = $6fffffff;
  195. { GNU extension to Solaris versioning scheme }
  196. DT_VERSYM = $6ffffff0;
  197. GRP_COMDAT = 1;
  198. { DT_FLAGS }
  199. DF_ORIGIN = 1;
  200. DF_SYMBOLIC = 2; // supersedes DT_SYMBOLIC
  201. DF_TEXTREL = 4; // supersedes DT_TEXTREL
  202. DF_BIND_NOW = 8; // supersedes DT_BIND_NOW
  203. DF_STATIC_TLS = 16;
  204. { DT_FLAGS_1 }
  205. DF_1_NOW = $01;
  206. DF_1_GLOBAL = $02;
  207. DF_1_GROUP = $04;
  208. DF_1_NODELETE = $08;
  209. DF_1_LOADFLTR = $10;
  210. DF_1_INITFIRST = $20;
  211. DF_1_NOOPEN = $40;
  212. DF_1_ORIGIN = $80;
  213. DF_1_DIRECT = $100;
  214. DF_1_TRANS = $200;
  215. DF_1_INTERPOSE = $400;
  216. DF_1_NODEFLIB = $800;
  217. DF_1_NODUMP = $1000;
  218. DF_1_CONFALT = $2000;
  219. type
  220. TElfIdent = array[0..15] of byte;
  221. { Structures which are written directly to the output file }
  222. TElf32header=record
  223. e_ident : TElfIdent;
  224. e_type : word;
  225. e_machine : word;
  226. e_version : longword;
  227. e_entry : longword; { entrypoint }
  228. e_phoff : longword; { program header offset }
  229. e_shoff : longword; { sections header offset }
  230. e_flags : longword;
  231. e_ehsize : word; { elf header size in bytes }
  232. e_phentsize : word; { size of an entry in the program header array }
  233. e_phnum : word; { 0..e_phnum-1 of entrys }
  234. e_shentsize : word; { size of an entry in sections header array }
  235. e_shnum : word; { 0..e_shnum-1 of entrys }
  236. e_shstrndx : word; { index of string section header }
  237. end;
  238. TElf32sechdr=record
  239. sh_name : longword;
  240. sh_type : longword;
  241. sh_flags : longword;
  242. sh_addr : longword;
  243. sh_offset : longword;
  244. sh_size : longword;
  245. sh_link : longword;
  246. sh_info : longword;
  247. sh_addralign : longword;
  248. sh_entsize : longword;
  249. end;
  250. TElf32proghdr=record
  251. p_type : longword;
  252. p_offset : longword;
  253. p_vaddr : longword;
  254. p_paddr : longword;
  255. p_filesz : longword;
  256. p_memsz : longword;
  257. p_flags : longword;
  258. p_align : longword;
  259. end;
  260. TElf32reloc=record
  261. address : longword;
  262. info : longword; { bit 0-7: type, 8-31: symbol }
  263. addend : longint;
  264. end;
  265. TElf32symbol=record
  266. st_name : longword;
  267. st_value : longword;
  268. st_size : longword;
  269. st_info : byte; { bit 0-3: type, 4-7: bind }
  270. st_other : byte;
  271. st_shndx : word;
  272. end;
  273. TElf32Dyn=record
  274. d_tag: longword;
  275. case integer of
  276. 0: (d_val: longword);
  277. 1: (d_ptr: longword);
  278. end;
  279. telf64header=record
  280. e_ident : TElfIdent;
  281. e_type : word;
  282. e_machine : word;
  283. e_version : longword;
  284. e_entry : qword; { entrypoint }
  285. e_phoff : qword; { program header offset }
  286. e_shoff : qword; { sections header offset }
  287. e_flags : longword;
  288. e_ehsize : word; { elf header size in bytes }
  289. e_phentsize : word; { size of an entry in the program header array }
  290. e_phnum : word; { 0..e_phnum-1 of entrys }
  291. e_shentsize : word; { size of an entry in sections header array }
  292. e_shnum : word; { 0..e_shnum-1 of entrys }
  293. e_shstrndx : word; { index of string section header }
  294. end;
  295. TElf64sechdr=record
  296. sh_name : longword;
  297. sh_type : longword;
  298. sh_flags : qword;
  299. sh_addr : qword;
  300. sh_offset : qword;
  301. sh_size : qword;
  302. sh_link : longword;
  303. sh_info : longword;
  304. sh_addralign : qword;
  305. sh_entsize : qword;
  306. end;
  307. telf64proghdr=record
  308. p_type : longword;
  309. p_flags : longword;
  310. p_offset : qword;
  311. p_vaddr : qword;
  312. p_paddr : qword;
  313. p_filesz : qword;
  314. p_memsz : qword;
  315. p_align : qword;
  316. end;
  317. telf64reloc=record
  318. address : qword;
  319. info : qword; { bit 0-31: type, 32-63: symbol }
  320. addend : int64; { signed! }
  321. end;
  322. telf64symbol=record
  323. st_name : longword;
  324. st_info : byte; { bit 0-3: type, 4-7: bind }
  325. st_other : byte;
  326. st_shndx : word;
  327. st_value : qword;
  328. st_size : qword;
  329. end;
  330. TElf64Dyn=record
  331. d_tag: qword;
  332. case integer of
  333. 0: (d_val: qword);
  334. 1: (d_ptr: qword);
  335. end;
  336. { The following records are same for 32 and 64 bit ELF files }
  337. TElfVerdef=record
  338. vd_version: word; { =VER_DEF_CURRENT }
  339. vd_flags: word;
  340. vd_ndx: word;
  341. vd_cnt: word; { number of verdaux records }
  342. vd_hash: longword; { ELF hash of version name }
  343. vd_aux: longword; { offset to verdaux records }
  344. vd_next: longword; { offset to next verdef record }
  345. end;
  346. TElfVerdaux=record
  347. vda_name: longword;
  348. vda_next: longword;
  349. end;
  350. TElfVerneed=record
  351. vn_version: word; { =VER_NEED_CURRENT }
  352. vn_cnt: word;
  353. vn_file: longword;
  354. vn_aux: longword;
  355. vn_next: longword;
  356. end;
  357. TElfVernaux=record
  358. vna_hash: longword;
  359. vna_flags: word;
  360. vna_other: word;
  361. vna_name: longword;
  362. vna_next: longword;
  363. end;
  364. const
  365. VERSYM_VERSION = $7FFF;
  366. VERSYM_HIDDEN = $8000;
  367. VER_NDX_LOCAL = 0;
  368. VER_NDX_GLOBAL = 1;
  369. { TElfverdef.vd_version }
  370. VER_DEF_CURRENT = 1;
  371. { TElfverneed.vn_version }
  372. VER_NEED_CURRENT = 1;
  373. { TElfverdef.vn_flags }
  374. VER_FLG_BASE = 1;
  375. VER_FLG_WEAK = 2;
  376. VER_FLG_INFO = 4;
  377. implementation
  378. end.