ELFABI.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /* $OpenBSD: exec_elf.h,v 1.83 2019/01/22 23:23:18 jsg Exp $ */
  2. /*
  3. * Copyright (c) 1995, 1996 Erik Theisen. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * This is the ELF ABI header file
  29. * formerly known as "elf_abi.h".
  30. */
  31. #pragma once
  32. #ifndef KERNEL
  33. # include <stdint.h>
  34. # include <sys/types.h>
  35. #else
  36. # include <AK/Types.h>
  37. #endif
  38. #define ElfW(type) Elf64_##type
  39. #define ELFSIZE 64
  40. typedef uint8_t Elf_Byte;
  41. typedef uint32_t Elf32_Addr; /* Unsigned program address */
  42. typedef uint32_t Elf32_Off; /* Unsigned file offset */
  43. typedef int32_t Elf32_Sword; /* Signed large integer */
  44. typedef uint32_t Elf32_Word; /* Unsigned large integer */
  45. typedef uint16_t Elf32_Half; /* Unsigned medium integer */
  46. typedef uint64_t Elf32_Lword;
  47. typedef uint64_t Elf64_Addr;
  48. typedef uint64_t Elf64_Off;
  49. typedef int32_t Elf64_Shalf;
  50. #ifdef __alpha__
  51. typedef int64_t Elf64_Sword;
  52. typedef uint64_t Elf64_Word;
  53. #else
  54. typedef int32_t Elf64_Sword;
  55. typedef uint32_t Elf64_Word;
  56. #endif
  57. typedef int64_t Elf64_Sxword;
  58. typedef uint64_t Elf64_Xword;
  59. typedef uint64_t Elf64_Lword;
  60. typedef uint32_t Elf64_Half;
  61. typedef uint16_t Elf64_Quarter;
  62. /*
  63. * e_ident[] identification indices
  64. * See http://www.sco.com/developers/gabi/latest/ch4.eheader.html
  65. */
  66. #define EI_MAG0 0 /* file ID */
  67. #define EI_MAG1 1 /* file ID */
  68. #define EI_MAG2 2 /* file ID */
  69. #define EI_MAG3 3 /* file ID */
  70. #define EI_CLASS 4 /* file class */
  71. #define EI_DATA 5 /* data encoding */
  72. #define EI_VERSION 6 /* ELF header version */
  73. #define EI_OSABI 7 /* OS/ABI ID */
  74. #define EI_ABIVERSION 8 /* ABI version */
  75. #define EI_PAD 9 /* start of pad bytes */
  76. #define EI_NIDENT 16 /* Gfx::Size of e_ident[] */
  77. /* e_ident[] magic number */
  78. #define ELFMAG0 0x7f /* e_ident[EI_MAG0] */
  79. #define ELFMAG1 'E' /* e_ident[EI_MAG1] */
  80. #define ELFMAG2 'L' /* e_ident[EI_MAG2] */
  81. #define ELFMAG3 'F' /* e_ident[EI_MAG3] */
  82. #define ELFMAG "\177ELF" /* magic */
  83. #define SELFMAG 4 /* size of magic */
  84. /* e_ident[] file class */
  85. #define ELFCLASSNONE 0 /* invalid */
  86. #define ELFCLASS32 1 /* 32-bit objs */
  87. #define ELFCLASS64 2 /* 64-bit objs */
  88. #define ELFCLASSNUM 3 /* number of classes */
  89. /* e_ident[] data encoding */
  90. #define ELFDATANONE 0 /* invalid */
  91. #define ELFDATA2LSB 1 /* Little-Endian */
  92. #define ELFDATA2MSB 2 /* Big-Endian */
  93. #define ELFDATANUM 3 /* number of data encode defines */
  94. /* e_ident[] Operating System/ABI */
  95. #define ELFOSABI_SYSV 0 /* UNIX System V ABI */
  96. #define ELFOSABI_HPUX 1 /* HP-UX operating system */
  97. #define ELFOSABI_NETBSD 2 /* NetBSD */
  98. #define ELFOSABI_LINUX 3 /* GNU/Linux */
  99. #define ELFOSABI_GNU ELFOSABI_LINUX
  100. #define ELFOSABI_HURD 4 /* GNU/Hurd */
  101. #define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */
  102. #define ELFOSABI_SOLARIS 6 /* Solaris */
  103. #define ELFOSABI_MONTEREY 7 /* Monterey */
  104. #define ELFOSABI_AIX ELFOSABI_MONTEREY
  105. #define ELFOSABI_IRIX 8 /* IRIX */
  106. #define ELFOSABI_FREEBSD 9 /* FreeBSD */
  107. #define ELFOSABI_TRU64 10 /* TRU64 UNIX */
  108. #define ELFOSABI_MODESTO 11 /* Novell Modesto */
  109. #define ELFOSABI_OPENBSD 12 /* OpenBSD */
  110. #define ELFOSABI_ARM 97 /* ARM */
  111. #define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
  112. /* e_ident */
  113. #define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && (ehdr).e_ident[EI_MAG1] == ELFMAG1 && (ehdr).e_ident[EI_MAG2] == ELFMAG2 && (ehdr).e_ident[EI_MAG3] == ELFMAG3)
  114. /* ELF Header */
  115. typedef struct elfhdr {
  116. unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
  117. Elf32_Half e_type; /* object file type */
  118. Elf32_Half e_machine; /* machine */
  119. Elf32_Word e_version; /* object file version */
  120. Elf32_Addr e_entry; /* virtual entry point */
  121. Elf32_Off e_phoff; /* program header table offset */
  122. Elf32_Off e_shoff; /* section header table offset */
  123. Elf32_Word e_flags; /* processor-specific flags */
  124. Elf32_Half e_ehsize; /* ELF header size */
  125. Elf32_Half e_phentsize; /* program header entry size */
  126. Elf32_Half e_phnum; /* number of program header entries */
  127. Elf32_Half e_shentsize; /* section header entry size */
  128. Elf32_Half e_shnum; /* number of section header entries */
  129. Elf32_Half e_shstrndx; /* section header table's "section
  130. header string table" entry offset */
  131. } Elf32_Ehdr;
  132. typedef struct {
  133. unsigned char e_ident[EI_NIDENT]; /* Id bytes */
  134. Elf64_Quarter e_type; /* file type */
  135. Elf64_Quarter e_machine; /* machine type */
  136. Elf64_Half e_version; /* version number */
  137. Elf64_Addr e_entry; /* entry point */
  138. Elf64_Off e_phoff; /* Program hdr offset */
  139. Elf64_Off e_shoff; /* Section hdr offset */
  140. Elf64_Half e_flags; /* Processor flags */
  141. Elf64_Quarter e_ehsize; /* sizeof ehdr */
  142. Elf64_Quarter e_phentsize; /* Program header entry size */
  143. Elf64_Quarter e_phnum; /* Number of program headers */
  144. Elf64_Quarter e_shentsize; /* Section header entry size */
  145. Elf64_Quarter e_shnum; /* Number of section headers */
  146. Elf64_Quarter e_shstrndx; /* String table index */
  147. } Elf64_Ehdr;
  148. /* e_type */
  149. #define ET_NONE 0 /* No file type */
  150. #define ET_REL 1 /* relocatable file */
  151. #define ET_EXEC 2 /* executable file */
  152. #define ET_DYN 3 /* shared object file */
  153. #define ET_CORE 4 /* core file */
  154. #define ET_NUM 5 /* number of types */
  155. #define ET_LOPROC 0xff00 /* reserved range for processor */
  156. #define ET_HIPROC 0xffff /* specific e_type */
  157. /* e_machine */
  158. #define EM_NONE 0 /* No Machine */
  159. #define EM_M32 1 /* AT&T WE 32100 */
  160. #define EM_SPARC 2 /* SPARC */
  161. #define EM_386 3 /* Intel 80386 */
  162. #define EM_68K 4 /* Motorola 68000 */
  163. #define EM_88K 5 /* Motorola 88000 */
  164. #define EM_486 6 /* Intel 80486 - unused? */
  165. #define EM_860 7 /* Intel 80860 */
  166. #define EM_MIPS 8 /* MIPS R3000 Big-Endian only */
  167. /*
  168. * Don't know if EM_MIPS_RS4_BE,
  169. * EM_SPARC64, EM_PARISC,
  170. * or EM_PPC are ABI compliant
  171. */
  172. #define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */
  173. #define EM_SPARC64 11 /* SPARC v9 64-bit unofficial */
  174. #define EM_PARISC 15 /* HPPA */
  175. #define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */
  176. #define EM_PPC 20 /* PowerPC */
  177. #define EM_PPC64 21 /* PowerPC 64 */
  178. #define EM_S390 22 /* IBM S390 */
  179. #define EM_ARM 40 /* Advanced RISC Machines ARM */
  180. #define EM_ALPHA 41 /* DEC ALPHA */
  181. #define EM_SH 42 /* Hitachi/Renesas Super-H */
  182. #define EM_SPARCV9 43 /* SPARC version 9 */
  183. #define EM_IA_64 50 /* Intel IA-64 Processor */
  184. #define EM_AMD64 62 /* AMD64 architecture */
  185. #define EM_X86_64 EM_AMD64
  186. #define EM_VAX 75 /* DEC VAX */
  187. #define EM_AARCH64 183 /* ARM 64-bit architecture (AArch64) */
  188. #define EM_RISCV 243 /* RISC-V */
  189. /* Non-standard */
  190. #define EM_ALPHA_EXP 0x9026 /* DEC ALPHA */
  191. #define EM__LAST__ (EM_ALPHA_EXP + 1)
  192. #define EM_NUM 22 /* number of machine types */
  193. /* Version */
  194. #define EV_NONE 0 /* Invalid */
  195. #define EV_CURRENT 1 /* Current */
  196. #define EV_NUM 2 /* number of versions */
  197. /* Magic for e_phnum: get real value from sh_info of first section header */
  198. #define PN_XNUM 0xffff
  199. /* RISC-V specific e_flags */
  200. /* See: RISC-V ABI Specification 1.0, Section 8.1: File Header */
  201. #define EF_RISCV_RVC 0x0001 /* Binary targets the Compressed ABI */
  202. #define EF_RISCV_FLOAT_ABI_SOFT 0x0000 /* No hardware float ABI in use */
  203. #define EF_RISCV_FLOAT_ABI_SINGLE 0x0002 /* 32-bit hardware float ABI */
  204. #define EF_RISCV_FLOAT_ABI_DOUBLE 0x0004 /* 64-bit hardware float ABI */
  205. #define EF_RISCV_FLOAT_ABI_QUAD 0x0006 /* 128-bit hardware float ABI */
  206. #define EF_RISCV_FLOAT_ABI 0x0006 /* Test mask for float ABIs */
  207. #define EF_RISCV_RVE 0x0008 /* Binary targets the Embedded ABI */
  208. #define EF_RISCV_TSO 0x0010 /* Binary requires Ztso extension */
  209. /* Section Header */
  210. typedef struct {
  211. Elf32_Word sh_name; /* name - index into section header
  212. string table section */
  213. Elf32_Word sh_type; /* type */
  214. Elf32_Word sh_flags; /* flags */
  215. Elf32_Addr sh_addr; /* address */
  216. Elf32_Off sh_offset; /* file offset */
  217. Elf32_Word sh_size; /* section size */
  218. Elf32_Word sh_link; /* section header table index link */
  219. Elf32_Word sh_info; /* extra information */
  220. Elf32_Word sh_addralign; /* address alignment */
  221. Elf32_Word sh_entsize; /* section entry size */
  222. } Elf32_Shdr;
  223. typedef struct {
  224. Elf64_Half sh_name; /* section name */
  225. Elf64_Half sh_type; /* section type */
  226. Elf64_Xword sh_flags; /* section flags */
  227. Elf64_Addr sh_addr; /* virtual address */
  228. Elf64_Off sh_offset; /* file offset */
  229. Elf64_Xword sh_size; /* section size */
  230. Elf64_Half sh_link; /* link to another */
  231. Elf64_Half sh_info; /* misc info */
  232. Elf64_Xword sh_addralign; /* memory alignment */
  233. Elf64_Xword sh_entsize; /* table entry size */
  234. } Elf64_Shdr;
  235. /* Special Section Indices */
  236. #define SHN_UNDEF 0 /* undefined */
  237. #define SHN_LORESERVE 0xff00 /* lower bounds of reserved indices */
  238. #define SHN_LOPROC 0xff00 /* reserved range for processor */
  239. #define SHN_HIPROC 0xff1f /* specific section indices */
  240. #define SHN_ABS 0xfff1 /* absolute value */
  241. #define SHN_COMMON 0xfff2 /* common symbol */
  242. #define SHN_XINDEX 0xffff /* Escape -- index stored elsewhere. */
  243. #define SHN_HIRESERVE 0xffff /* upper bounds of reserved indices */
  244. /* sh_type */
  245. #define SHT_NULL 0 /* inactive */
  246. #define SHT_PROGBITS 1 /* program defined information */
  247. #define SHT_SYMTAB 2 /* symbol table section */
  248. #define SHT_STRTAB 3 /* string table section */
  249. #define SHT_RELA 4 /* relocation section with addends*/
  250. #define SHT_HASH 5 /* symbol hash table section */
  251. #define SHT_DYNAMIC 6 /* dynamic section */
  252. #define SHT_NOTE 7 /* note section */
  253. #define SHT_NOBITS 8 /* no space section */
  254. #define SHT_REL 9 /* relation section without addends */
  255. #define SHT_SHLIB 10 /* reserved - purpose unknown */
  256. #define SHT_DYNSYM 11 /* dynamic symbol table section */
  257. #define SHT_NUM 12 /* number of section types */
  258. #define SHT_INIT_ARRAY 14 /* pointers to init functions */
  259. #define SHT_FINI_ARRAY 15 /* pointers to termination functions */
  260. #define SHT_PREINIT_ARRAY 16 /* ptrs to funcs called before init */
  261. #define SHT_GROUP 17 /* defines a section group */
  262. #define SHT_SYMTAB_SHNDX 18 /* Section indices (see SHN_XINDEX). */
  263. #define SHT_RELR 19 /* relative-only relocation section */
  264. #define SHT_LOOS 0x60000000 /* reserved range for OS specific */
  265. #define SHT_SUNW_dof 0x6ffffff4 /* used by dtrace */
  266. #define SHT_GNU_LIBLIST 0x6ffffff7 /* libraries to be prelinked */
  267. #define SHT_SUNW_move 0x6ffffffa /* inf for partially init'ed symbols */
  268. #define SHT_SUNW_syminfo 0x6ffffffc /* ad symbol information */
  269. #define SHT_SUNW_verdef 0x6ffffffd /* symbol versioning inf */
  270. #define SHT_SUNW_verneed 0x6ffffffe /* symbol versioning req */
  271. #define SHT_SUNW_versym 0x6fffffff /* symbol versioning table */
  272. #define SHT_HIOS 0x6fffffff /* section header types */
  273. #define SHT_LOPROC 0x70000000 /* reserved range for processor */
  274. #define SHT_HIPROC 0x7fffffff /* specific section header types */
  275. #define SHT_LOUSER 0x80000000 /* reserved range for application */
  276. #define SHT_HIUSER 0xffffffff /* specific indices */
  277. #define SHT_GNU_HASH 0x6ffffff6 /* GNU-style hash table section */
  278. /* Section names */
  279. #define ELF_BSS ".bss" /* uninitialized data */
  280. #define ELF_DATA ".data" /* initialized data */
  281. #define ELF_CTF ".SUNW_ctf" /* CTF data */
  282. #define ELF_DEBUG ".debug" /* debug */
  283. #define ELF_DYNAMIC ".dynamic" /* dynamic linking information */
  284. #define ELF_DYNSTR ".dynstr" /* dynamic string table */
  285. #define ELF_DYNSYM ".dynsym" /* dynamic symbol table */
  286. #define ELF_FINI ".fini" /* termination code */
  287. #define ELF_GOT ".got" /* global offset table */
  288. #define ELF_HASH ".hash" /* symbol hash table */
  289. #define ELF_INIT ".init" /* initialization code */
  290. #define ELF_REL_DATA ".rel.data" /* relocation data */
  291. #define ELF_REL_FINI ".rel.fini" /* relocation termination code */
  292. #define ELF_REL_INIT ".rel.init" /* relocation initialization code */
  293. #define ELF_REL_DYN ".rel.dyn" /* relocation dynamic link info */
  294. #define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */
  295. #define ELF_REL_TEXT ".rel.text" /* relocation code */
  296. #define ELF_RODATA ".rodata" /* read-only data */
  297. #define ELF_SHSTRTAB ".shstrtab" /* section header string table */
  298. #define ELF_STRTAB ".strtab" /* string table */
  299. #define ELF_SYMTAB ".symtab" /* symbol table */
  300. #define ELF_TEXT ".text" /* code */
  301. #define ELF_OPENBSDRANDOMDATA ".openbsd.randomdata" /* constant randomdata */
  302. /* Section Attribute Flags - sh_flags */
  303. #define SHF_WRITE 0x1 /* Writable */
  304. #define SHF_ALLOC 0x2 /* occupies memory */
  305. #define SHF_EXECINSTR 0x4 /* executable */
  306. #define SHF_MERGE 0x10 /* may be merged */
  307. #define SHF_STRINGS 0x20 /* contains strings */
  308. #define SHF_INFO_LINK 0x40 /* sh_info holds section index */
  309. #define SHF_LINK_ORDER 0x80 /* ordering requirements */
  310. #define SHF_OS_NONCONFORMING 0x100 /* OS-specific processing required */
  311. #define SHF_GROUP 0x200 /* member of section group */
  312. #define SHF_TLS 0x400 /* thread local storage */
  313. #define SHF_COMPRESSED 0x800 /* contains compressed data */
  314. #define SHF_MASKOS 0x0ff00000 /* OS-specific semantics */
  315. #define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
  316. /* specific section attributes */
  317. /* Symbol Table Entry */
  318. typedef struct elf32_sym {
  319. Elf32_Word st_name; /* name - index into string table */
  320. Elf32_Addr st_value; /* symbol value */
  321. Elf32_Word st_size; /* symbol size */
  322. unsigned char st_info; /* type and binding */
  323. unsigned char st_other; /* 0 - no defined meaning */
  324. Elf32_Half st_shndx; /* section header index */
  325. } Elf32_Sym;
  326. typedef struct {
  327. Elf64_Half st_name; /* Symbol name index in str table */
  328. Elf_Byte st_info; /* type / binding attrs */
  329. Elf_Byte st_other; /* unused */
  330. Elf64_Quarter st_shndx; /* section index of symbol */
  331. Elf64_Xword st_value; /* value of symbol */
  332. Elf64_Xword st_size; /* size of symbol */
  333. } Elf64_Sym;
  334. /* Symbol table index */
  335. #define STN_UNDEF 0 /* undefined */
  336. /* Extract symbol info - st_info */
  337. #define ELF32_ST_BIND(x) ((x) >> 4)
  338. #define ELF32_ST_TYPE(x) (((unsigned int)x) & 0xf)
  339. #define ELF32_ST_INFO(b, t) (((b) << 4) + ((t)&0xf))
  340. #define ELF64_ST_BIND(x) ((x) >> 4)
  341. #define ELF64_ST_TYPE(x) (((unsigned int)x) & 0xf)
  342. #define ELF64_ST_INFO(b, t) (((b) << 4) + ((t)&0xf))
  343. /* Symbol Binding - ELF32_ST_BIND - st_info */
  344. #define STB_LOCAL 0 /* Local symbol */
  345. #define STB_GLOBAL 1 /* Global symbol */
  346. #define STB_WEAK 2 /* like global - lower precedence */
  347. #define STB_NUM 3 /* number of symbol bindings */
  348. #define STB_LOPROC 13 /* reserved range for processor */
  349. #define STB_HIPROC 15 /* specific symbol bindings */
  350. /* Symbol type - ELF32_ST_TYPE - st_info */
  351. #define STT_NOTYPE 0 /* not specified */
  352. #define STT_OBJECT 1 /* data object */
  353. #define STT_FUNC 2 /* function */
  354. #define STT_SECTION 3 /* section */
  355. #define STT_FILE 4 /* file */
  356. #define STT_TLS 6 /* thread local storage */
  357. #define STT_GNU_IFUNC 10
  358. #define STT_LOPROC 13 /* reserved range for processor */
  359. #define STT_HIPROC 15 /* specific symbol types */
  360. /* Extract symbol visibility - st_other */
  361. #define ELF_ST_VISIBILITY(v) ((v)&0x3)
  362. #define ELF32_ST_VISIBILITY ELF_ST_VISIBILITY
  363. #define ELF64_ST_VISIBILITY ELF_ST_VISIBILITY
  364. #define STV_DEFAULT 0 /* Visibility set by binding type */
  365. #define STV_INTERNAL 1 /* OS specific version of STV_HIDDEN */
  366. #define STV_HIDDEN 2 /* can only be seen inside own .so */
  367. #define STV_PROTECTED 3 /* HIDDEN inside, DEFAULT outside */
  368. /* Relocation entry with implicit addend */
  369. typedef struct {
  370. Elf32_Addr r_offset; /* offset of relocation */
  371. Elf32_Word r_info; /* symbol table index and type */
  372. } Elf32_Rel;
  373. /* Relocation entry with explicit addend */
  374. typedef struct {
  375. Elf32_Addr r_offset; /* offset of relocation */
  376. Elf32_Word r_info; /* symbol table index and type */
  377. Elf32_Sword r_addend;
  378. } Elf32_Rela;
  379. /* Extract relocation info - r_info */
  380. #define ELF32_R_SYM(i) ((i) >> 8)
  381. #define ELF32_R_TYPE(i) ((unsigned char)(i))
  382. #define ELF32_R_INFO(s, t) (((s) << 8) + (unsigned char)(t))
  383. typedef struct {
  384. Elf64_Xword r_offset; /* where to do it */
  385. Elf64_Xword r_info; /* index & type of relocation */
  386. } Elf64_Rel;
  387. typedef struct {
  388. Elf64_Xword r_offset; /* where to do it */
  389. Elf64_Xword r_info; /* index & type of relocation */
  390. Elf64_Sxword r_addend; /* adjustment value */
  391. } Elf64_Rela;
  392. #define ELF64_R_SYM(info) ((info) >> 32)
  393. #define ELF64_R_TYPE(info) ((info)&0xFFFFFFFF)
  394. #define ELF64_R_INFO(s, t) (((s) << 32) + (uint32_t)(t))
  395. #if defined(__mips64__) && defined(__MIPSEL__)
  396. /*
  397. * The 64-bit MIPS ELF ABI uses a slightly different relocation format
  398. * than the regular ELF ABI: the r_info field is split into several
  399. * pieces (see gnu/usr.bin/binutils-2.17/include/elf/mips.h for details).
  400. */
  401. # undef ELF64_R_SYM
  402. # undef ELF64_R_TYPE
  403. # undef ELF64_R_INFO
  404. # define ELF64_R_TYPE(info) ((uint64_t)swap32((info) >> 32))
  405. # define ELF64_R_SYM(info) ((info)&0xFFFFFFFF)
  406. # define ELF64_R_INFO(s, t) (((uint64_t)swap32(t) << 32) + (uint32_t)(s))
  407. #endif /* __mips64__ && __MIPSEL__ */
  408. typedef Elf32_Word Elf32_Relr;
  409. typedef Elf64_Xword Elf64_Relr;
  410. /* Program Header */
  411. typedef struct {
  412. Elf32_Word p_type; /* segment type */
  413. Elf32_Off p_offset; /* segment offset */
  414. Elf32_Addr p_vaddr; /* virtual address of segment */
  415. Elf32_Addr p_paddr; /* physical address - ignored? */
  416. Elf32_Word p_filesz; /* number of bytes in file for seg. */
  417. Elf32_Word p_memsz; /* number of bytes in mem. for seg. */
  418. Elf32_Word p_flags; /* flags */
  419. Elf32_Word p_align; /* memory alignment */
  420. } Elf32_Phdr;
  421. typedef struct {
  422. Elf64_Half p_type; /* entry type */
  423. Elf64_Half p_flags; /* flags */
  424. Elf64_Off p_offset; /* offset */
  425. Elf64_Addr p_vaddr; /* virtual address */
  426. Elf64_Addr p_paddr; /* physical address */
  427. Elf64_Xword p_filesz; /* file size */
  428. Elf64_Xword p_memsz; /* memory size */
  429. Elf64_Xword p_align; /* memory & file alignment */
  430. } Elf64_Phdr;
  431. /* Segment types - p_type */
  432. #define PT_NULL 0 /* unused */
  433. #define PT_LOAD 1 /* loadable segment */
  434. #define PT_DYNAMIC 2 /* dynamic linking section */
  435. #define PT_INTERP 3 /* the RTLD */
  436. #define PT_NOTE 4 /* auxiliary information */
  437. #define PT_SHLIB 5 /* reserved - purpose undefined */
  438. #define PT_PHDR 6 /* program header */
  439. #define PT_TLS 7 /* thread local storage */
  440. #define PT_LOOS 0x60000000 /* reserved range for OS */
  441. #define PT_HIOS 0x6fffffff /* specific segment types */
  442. #define PT_LOPROC 0x70000000 /* start of reserved range for processor specific segment types */
  443. #define PT_RISCV_ATTRIBUTES 0x70000003 /* RISC-V ELF attribute section */
  444. #define PT_HIPROC 0x7fffffff /* end of reserved range for processor specific segment types */
  445. #define PT_GNU_EH_FRAME 0x6474e550 /* Exception handling info */
  446. #define PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */
  447. #define PT_GNU_STACK 0x6474e551 /* Stack permissions & size info */
  448. #define PT_OPENBSD_RANDOMIZE 0x65a3dbe6 /* fill with random data */
  449. #define PT_OPENBSD_WXNEEDED 0x65a3dbe7 /* program performs W^X violations */
  450. #define PT_OPENBSD_BOOTDATA 0x65a41be6 /* section for boot arguments */
  451. /* Segment flags - p_flags */
  452. #define PF_X 0x1 /* Executable */
  453. #define PF_W 0x2 /* Writable */
  454. #define PF_R 0x4 /* Readable */
  455. #define PF_MASKPROC 0xf0000000 /* reserved bits for processor */
  456. /* specific segment flags */
  457. /* Dynamic structure */
  458. typedef struct {
  459. Elf32_Sword d_tag; /* controls meaning of d_val */
  460. union {
  461. Elf32_Word d_val; /* Multiple meanings - see d_tag */
  462. Elf32_Addr d_ptr; /* program virtual address */
  463. } d_un;
  464. } Elf32_Dyn;
  465. typedef struct {
  466. Elf64_Xword d_tag; /* controls meaning of d_val */
  467. union {
  468. Elf64_Addr d_ptr;
  469. Elf64_Xword d_val;
  470. } d_un;
  471. } Elf64_Dyn;
  472. /* Dynamic Array Tags - d_tag */
  473. #define DT_NULL 0 /* marks end of _DYNAMIC array */
  474. #define DT_NEEDED 1 /* string table offset of needed lib */
  475. #define DT_PLTRELSZ 2 /* size of relocation entries in PLT */
  476. #define DT_PLTGOT 3 /* address PLT/GOT */
  477. #define DT_HASH 4 /* address of symbol hash table */
  478. #define DT_STRTAB 5 /* address of string table */
  479. #define DT_SYMTAB 6 /* address of symbol table */
  480. #define DT_RELA 7 /* address of relocation table */
  481. #define DT_RELASZ 8 /* size of relocation table */
  482. #define DT_RELAENT 9 /* size of relocation entry */
  483. #define DT_STRSZ 10 /* size of string table */
  484. #define DT_SYMENT 11 /* size of symbol table entry */
  485. #define DT_INIT 12 /* address of initialization func. */
  486. #define DT_FINI 13 /* address of termination function */
  487. #define DT_SONAME 14 /* string table offset of shared obj */
  488. #define DT_RPATH 15 /* string table offset of library \
  489. search path */
  490. #define DT_SYMBOLIC 16 /* start sym search in shared obj. */
  491. #define DT_REL 17 /* address of rel. tbl. w addends */
  492. #define DT_RELSZ 18 /* size of DT_REL relocation table */
  493. #define DT_RELENT 19 /* size of DT_REL relocation entry */
  494. #define DT_PLTREL 20 /* PLT referenced relocation entry */
  495. #define DT_DEBUG 21 /* bugger */
  496. #define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */
  497. #define DT_JMPREL 23 /* add. of PLT's relocation entries */
  498. #define DT_BIND_NOW 24 /* Bind now regardless of env setting */
  499. #define DT_INIT_ARRAY 25 /* address of array of init func */
  500. #define DT_FINI_ARRAY 26 /* address of array of term func */
  501. #define DT_INIT_ARRAYSZ 27 /* size of array of init func */
  502. #define DT_FINI_ARRAYSZ 28 /* size of array of term func */
  503. #define DT_RUNPATH 29 /* strtab offset of lib search path */
  504. #define DT_FLAGS 30 /* Set of DF_* flags */
  505. #define DT_ENCODING 31 /* further DT_* follow encoding rules */
  506. #define DT_PREINIT_ARRAY 32 /* address of array of preinit func */
  507. #define DT_PREINIT_ARRAYSZ 33 /* size of array of preinit func */
  508. #define DT_RELRSZ 35 /* size of DT_RELR relocation table */
  509. #define DT_RELR 36 /* addr of DT_RELR relocation table */
  510. #define DT_RELRENT 37 /* size of DT_RELR relocation entry */
  511. #define DT_LOOS 0x6000000d /* reserved range for OS */
  512. #define DT_HIOS 0x6ffff000 /* specific dynamic array tags */
  513. #define DT_LOPROC 0x70000000 /* reserved range for processor */
  514. #define DT_HIPROC 0x7fffffff /* specific dynamic array tags */
  515. /* some other useful tags */
  516. #define DT_GNU_HASH 0x6ffffef5 /* address of GNU hash table */
  517. #define DT_VERSYM 0x6ffffff0 /* address of table provided by .gnu.version */
  518. #define DT_RELACOUNT 0x6ffffff9 /* if present, number of RELATIVE */
  519. #define DT_RELCOUNT 0x6ffffffa /* relocs, which must come first */
  520. #define DT_FLAGS_1 0x6ffffffb
  521. #define DT_VERDEF 0x6ffffffc /* address of version definition table */
  522. #define DT_VERDEFNUM 0x6ffffffd /* number of version definitions */
  523. #define DT_VERNEEDED 0x6ffffffe /* address of the dependency table */
  524. #define DT_VERNEEDEDNUM 0x6fffffff /* number of entries in VERNEEDED */
  525. /* Dynamic Flags - DT_FLAGS .dynamic entry */
  526. #define DF_ORIGIN 0x00000001
  527. #define DF_SYMBOLIC 0x00000002
  528. #define DF_TEXTREL 0x00000004
  529. #define DF_BIND_NOW 0x00000008
  530. #define DF_STATIC_TLS 0x00000010
  531. /* Dynamic Flags - DT_FLAGS_1 .dynamic entry */
  532. #define DF_1_NOW 0x00000001
  533. #define DF_1_GLOBAL 0x00000002
  534. #define DF_1_GROUP 0x00000004
  535. #define DF_1_NODELETE 0x00000008
  536. #define DF_1_LOADFLTR 0x00000010
  537. #define DF_1_INITFIRST 0x00000020
  538. #define DF_1_NOOPEN 0x00000040
  539. #define DF_1_ORIGIN 0x00000080
  540. #define DF_1_DIRECT 0x00000100
  541. #define DF_1_TRANS 0x00000200
  542. #define DF_1_INTERPOSE 0x00000400
  543. #define DF_1_NODEFLIB 0x00000800
  544. #define DF_1_NODUMP 0x00001000
  545. #define DF_1_CONLFAT 0x00002000
  546. /*
  547. * Note header
  548. */
  549. typedef struct {
  550. Elf32_Word n_namesz;
  551. Elf32_Word n_descsz;
  552. Elf32_Word n_type;
  553. } Elf32_Nhdr;
  554. typedef struct {
  555. Elf64_Half n_namesz;
  556. Elf64_Half n_descsz;
  557. Elf64_Half n_type;
  558. } Elf64_Nhdr;
  559. /*
  560. * Note Definitions
  561. */
  562. typedef struct {
  563. Elf32_Word namesz;
  564. Elf32_Word descsz;
  565. Elf32_Word type;
  566. } Elf32_Note;
  567. typedef struct {
  568. Elf64_Half namesz;
  569. Elf64_Half descsz;
  570. Elf64_Half type;
  571. } Elf64_Note;
  572. /* Values for n_type. */
  573. #define NT_PRSTATUS 1 /* Process status. */
  574. #define NT_FPREGSET 2 /* Floating point registers. */
  575. #define NT_PRPSINFO 3 /* Process state info. */
  576. /*
  577. * OpenBSD-specific core file information.
  578. *
  579. * OpenBSD ELF core files use notes to provide information about
  580. * the process's state. The note name is "OpenBSD" for information
  581. * that is global to the process, and "OpenBSD@nn", where "nn" is the
  582. * thread ID of the thread that the information belongs to (such as
  583. * register state).
  584. *
  585. * We use the following note identifiers:
  586. *
  587. * NT_OPENBSD_PROCINFO
  588. * Note is a "elfcore_procinfo" structure.
  589. * NT_OPENBSD_AUXV
  590. * Note is a a bunch of Auxiliary Vectors, terminated by
  591. * an AT_NULL entry.
  592. * NT_OPENBSD_REGS
  593. * Note is a "reg" structure.
  594. * NT_OPENBSD_FPREGS
  595. * Note is a "fpreg" structure.
  596. *
  597. * Please try to keep the members of the "elfcore_procinfo" structure
  598. * nicely aligned, and if you add elements, add them to the end and
  599. * bump the version.
  600. */
  601. #define NT_OPENBSD_PROCINFO 10
  602. #define NT_OPENBSD_AUXV 11
  603. #define NT_OPENBSD_REGS 20
  604. #define NT_OPENBSD_FPREGS 21
  605. #define NT_OPENBSD_XFPREGS 22
  606. #define NT_OPENBSD_WCOOKIE 23
  607. struct elfcore_procinfo {
  608. /* Version 1 fields start here. */
  609. uint32_t cpi_version; /* netbsd_elfcore_procinfo version */
  610. #define ELFCORE_PROCINFO_VERSION 1
  611. uint32_t cpi_cpisize; /* sizeof(netbsd_elfcore_procinfo) */
  612. uint32_t cpi_signo; /* killing signal */
  613. uint32_t cpi_sigcode; /* signal code */
  614. uint32_t cpi_sigpend; /* pending signals */
  615. uint32_t cpi_sigmask; /* blocked signals */
  616. uint32_t cpi_sigignore; /* ignored signals */
  617. uint32_t cpi_sigcatch; /* signals being caught by user */
  618. int32_t cpi_pid; /* process ID */
  619. int32_t cpi_ppid; /* parent process ID */
  620. int32_t cpi_pgrp; /* process group ID */
  621. int32_t cpi_sid; /* session ID */
  622. uint32_t cpi_ruid; /* real user ID */
  623. uint32_t cpi_euid; /* effective user ID */
  624. uint32_t cpi_svuid; /* saved user ID */
  625. uint32_t cpi_rgid; /* real group ID */
  626. uint32_t cpi_egid; /* effective group ID */
  627. uint32_t cpi_svgid; /* saved group ID */
  628. int8_t cpi_name[32]; /* copy of pr->ps_comm */
  629. };
  630. /*
  631. * FIXME - these _KERNEL items aren't part of the ABI!
  632. */
  633. #if defined(_KERNEL) || defined(_DYN_LOADER)
  634. # define ELF32_NO_ADDR ((uint32_t)~0) /* Indicates addr. not yet filled in */
  635. typedef struct {
  636. Elf32_Sword au_id; /* 32-bit id */
  637. Elf32_Word au_v; /* 32-bit value */
  638. } Aux32Info;
  639. # define ELF64_NO_ADDR ((uint64_t)~0) /* Indicates addr. not yet filled in */
  640. typedef struct {
  641. Elf64_Shalf au_id; /* 32-bit id */
  642. Elf64_Xword au_v; /* 64-bit value */
  643. } Aux64Info;
  644. enum AuxID {
  645. AUX_null = 0,
  646. AUX_ignore = 1,
  647. AUX_execfd = 2,
  648. AUX_phdr = 3, /* &phdr[0] */
  649. AUX_phent = 4, /* sizeof(phdr[0]) */
  650. AUX_phnum = 5, /* # phdr entries */
  651. AUX_pagesz = 6, /* PAGESIZE */
  652. AUX_base = 7, /* ld.so base addr */
  653. AUX_flags = 8, /* processor flags */
  654. AUX_entry = 9, /* a.out entry */
  655. AUX_sun_uid = 2000, /* euid */
  656. AUX_sun_ruid = 2001, /* ruid */
  657. AUX_sun_gid = 2002, /* egid */
  658. AUX_sun_rgid = 2003 /* rgid */
  659. };
  660. struct elf_args {
  661. u_long arg_entry; /* program entry point */
  662. u_long arg_interp; /* Interpreter load address */
  663. u_long arg_phaddr; /* program header address */
  664. u_long arg_phentsize; /* Gfx::Size of program header */
  665. u_long arg_phnum; /* Number of program headers */
  666. };
  667. #endif
  668. #if !defined(ELFSIZE) && defined(ARCH_ELFSIZE)
  669. # define ELFSIZE ARCH_ELFSIZE
  670. #endif
  671. #if defined(ELFSIZE)
  672. # define CONCAT(x, y) __CONCAT(x, y)
  673. # define ELFNAME(x) CONCAT(elf, CONCAT(ELFSIZE, CONCAT(_, x)))
  674. # define ELFDEFNNAME(x) CONCAT(ELF, CONCAT(ELFSIZE, CONCAT(_, x)))
  675. #endif
  676. #if defined(ELFSIZE) && (ELFSIZE == 32)
  677. # define Elf_Ehdr Elf32_Ehdr
  678. # define Elf_Phdr Elf32_Phdr
  679. # define Elf_Shdr Elf32_Shdr
  680. # define Elf_Sym Elf32_Sym
  681. # define Elf_Rel Elf32_Rel
  682. # define Elf_RelA Elf32_Rela
  683. # define Elf_Rela Elf32_Rela
  684. # define Elf_Relr Elf32_Relr
  685. # define Elf_Dyn Elf32_Dyn
  686. # define Elf_Half Elf32_Half
  687. # define Elf_Word Elf32_Word
  688. # define Elf_Sword Elf32_Sword
  689. # define Elf_Addr Elf32_Addr
  690. # define Elf_Off Elf32_Off
  691. # define Elf_Nhdr Elf32_Nhdr
  692. # define Elf_Note Elf32_Note
  693. # define ELF_R_SYM ELF32_R_SYM
  694. # define ELF_R_TYPE ELF32_R_TYPE
  695. # define ELF_R_INFO ELF32_R_INFO
  696. # define ELFCLASS ELFCLASS32
  697. # define ELF_ST_BIND ELF32_ST_BIND
  698. # define ELF_ST_TYPE ELF32_ST_TYPE
  699. # define ELF_ST_INFO ELF32_ST_INFO
  700. # define ELF_NO_ADDR ELF32_NO_ADDR
  701. # define AuxInfo Aux32Info
  702. #elif defined(ELFSIZE) && (ELFSIZE == 64)
  703. # define Elf_Ehdr Elf64_Ehdr
  704. # define Elf_Phdr Elf64_Phdr
  705. # define Elf_Shdr Elf64_Shdr
  706. # define Elf_Sym Elf64_Sym
  707. # define Elf_Rel Elf64_Rel
  708. # define Elf_RelA Elf64_Rela
  709. # define Elf_Rela Elf64_Rela
  710. # define Elf_Relr Elf64_Relr
  711. # define Elf_Dyn Elf64_Dyn
  712. # define Elf_Half Elf64_Half
  713. # define Elf_Word Elf64_Word
  714. # define Elf_Sword Elf64_Sword
  715. # define Elf_Addr Elf64_Addr
  716. # define Elf_Off Elf64_Off
  717. # define Elf_Nhdr Elf64_Nhdr
  718. # define Elf_Note Elf64_Note
  719. # define ELF_R_SYM ELF64_R_SYM
  720. # define ELF_R_TYPE ELF64_R_TYPE
  721. # define ELF_R_INFO ELF64_R_INFO
  722. # define ELFCLASS ELFCLASS64
  723. # define ELF_ST_BIND ELF64_ST_BIND
  724. # define ELF_ST_TYPE ELF64_ST_TYPE
  725. # define ELF_ST_INFO ELF64_ST_INFO
  726. # define ELF_NO_ADDR ELF64_NO_ADDR
  727. # define AuxInfo Aux64Info
  728. #endif
  729. #define ELF_TARG_VER 1 /* The ver for which this code is intended */
  730. /* Relocation types */
  731. #define R_386_NONE 0
  732. #define R_386_32 1 /* Symbol + Addend */
  733. #define R_386_PC32 2 /* Symbol + Addend - Section offset */
  734. #define R_386_GOT32 3 /* Used by build-time linker to create GOT entry */
  735. #define R_386_PLT32 4 /* Used by build-time linker to create PLT entry */
  736. #define R_386_COPY 5 /* https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter4-10454.html#chapter4-84604 */
  737. #define R_386_GLOB_DAT 6 /* Relation b/w GOT entry and symbol */
  738. #define R_386_JMP_SLOT 7 /* Fixed up by dynamic loader */
  739. #define R_386_RELATIVE 8 /* Base address + Addned */
  740. #define R_386_TLS_TPOFF 14 /* Negative offset into the static TLS storage */
  741. #define R_386_TLS_TPOFF32 37
  742. #define R_386_IRELATIVE 42 /* PLT entry resolved indirectly at runtime */
  743. #define R_X86_64_NONE 0
  744. #define R_X86_64_64 1
  745. #define R_X86_64_COPY 5
  746. #define R_X86_64_GLOB_DAT 6
  747. #define R_X86_64_JUMP_SLOT 7
  748. #define R_X86_64_RELATIVE 8
  749. #define R_X86_64_DTPMOD64 16
  750. #define R_X86_64_DTPOFF64 17
  751. #define R_X86_64_TPOFF64 18
  752. #define R_X86_64_TLSDESC 36
  753. #define R_X86_64_IRELATIVE 37
  754. #define __ENUMERATE_X86_64_DYNAMIC_RELOCS(R) \
  755. R(R_X86_64_NONE) \
  756. R(R_X86_64_64) \
  757. R(R_X86_64_COPY) \
  758. R(R_X86_64_GLOB_DAT) \
  759. R(R_X86_64_JUMP_SLOT) \
  760. R(R_X86_64_RELATIVE) \
  761. R(R_X86_64_DTPMOD64) \
  762. R(R_X86_64_DTPOFF64) \
  763. R(R_X86_64_TPOFF64) \
  764. R(R_X86_64_TLSDESC) \
  765. R(R_X86_64_IRELATIVE)
  766. /* 5.7.12 Dynamic relocations https://github.com/ARM-software/abi-aa/blob/2023q1-release/aaelf64/aaelf64.rst#dynamic-relocations */
  767. #define R_AARCH64_NONE 0
  768. #define R_AARCH64_ABS64 257
  769. #define R_AARCH64_COPY 1024
  770. #define R_AARCH64_GLOB_DAT 1025
  771. #define R_AARCH64_JUMP_SLOT 1026
  772. #define R_AARCH64_RELATIVE 1027
  773. #define R_AARCH64_TLS_DTPMOD 1028
  774. #define R_AARCH64_TLS_DTPREL 1029
  775. #define R_AARCH64_TLS_TPREL 1030
  776. #define R_AARCH64_TLSDESC 1031
  777. #define R_AARCH64_IRELATIVE 1032
  778. #define __ENUMERATE_AARCH64_DYNAMIC_RELOCS(R) \
  779. R(R_AARCH64_NONE) \
  780. R(R_AARCH64_ABS64) \
  781. R(R_AARCH64_COPY) \
  782. R(R_AARCH64_GLOB_DAT) \
  783. R(R_AARCH64_JUMP_SLOT) \
  784. R(R_AARCH64_RELATIVE) \
  785. R(R_AARCH64_TLS_DTPMOD) \
  786. R(R_AARCH64_TLS_DTPREL) \
  787. R(R_AARCH64_TLS_TPREL) \
  788. R(R_AARCH64_TLSDESC) \
  789. R(R_AARCH64_IRELATIVE)