elf.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #ifndef _LINUX_ELF_H
  2. #define _LINUX_ELF_H
  3. #include <linux/types.h>
  4. #include <linux/elf-em.h>
  5. #ifdef __KERNEL__
  6. #include <asm/elf.h>
  7. #endif
  8. /* 32-bit ELF base types. */
  9. typedef __u32 Elf32_Addr;
  10. typedef __u16 Elf32_Half;
  11. typedef __u32 Elf32_Off;
  12. typedef __s32 Elf32_Sword;
  13. typedef __u32 Elf32_Word;
  14. /* 64-bit ELF base types. */
  15. typedef __u64 Elf64_Addr;
  16. typedef __u16 Elf64_Half;
  17. typedef __s16 Elf64_SHalf;
  18. typedef __u64 Elf64_Off;
  19. typedef __s32 Elf64_Sword;
  20. typedef __u32 Elf64_Word;
  21. typedef __u64 Elf64_Xword;
  22. typedef __s64 Elf64_Sxword;
  23. /* These constants are for the segment types stored in the image headers */
  24. #define PT_NULL 0
  25. #define PT_LOAD 1
  26. #define PT_DYNAMIC 2
  27. #define PT_INTERP 3
  28. #define PT_NOTE 4
  29. #define PT_SHLIB 5
  30. #define PT_PHDR 6
  31. #define PT_TLS 7 /* Thread local storage segment */
  32. #define PT_LOOS 0x60000000 /* OS-specific */
  33. #define PT_HIOS 0x6fffffff /* OS-specific */
  34. #define PT_LOPROC 0x70000000
  35. #define PT_HIPROC 0x7fffffff
  36. #define PT_GNU_EH_FRAME 0x6474e550
  37. #define PT_GNU_STACK (PT_LOOS + 0x474e551)
  38. /*
  39. * Extended Numbering
  40. *
  41. * If the real number of program header table entries is larger than
  42. * or equal to PN_XNUM(0xffff), it is set to sh_info field of the
  43. * section header at index 0, and PN_XNUM is set to e_phnum
  44. * field. Otherwise, the section header at index 0 is zero
  45. * initialized, if it exists.
  46. *
  47. * Specifications are available in:
  48. *
  49. * - Sun microsystems: Linker and Libraries.
  50. * Part No: 817-1984-17, September 2008.
  51. * URL: http://docs.sun.com/app/docs/doc/817-1984
  52. *
  53. * - System V ABI AMD64 Architecture Processor Supplement
  54. * Draft Version 0.99.,
  55. * May 11, 2009.
  56. * URL: http://www.x86-64.org/
  57. */
  58. #define PN_XNUM 0xffff
  59. /* These constants define the different elf file types */
  60. #define ET_NONE 0
  61. #define ET_REL 1
  62. #define ET_EXEC 2
  63. #define ET_DYN 3
  64. #define ET_CORE 4
  65. #define ET_LOPROC 0xff00
  66. #define ET_HIPROC 0xffff
  67. /* This is the info that is needed to parse the dynamic section of the file */
  68. #define DT_NULL 0
  69. #define DT_NEEDED 1
  70. #define DT_PLTRELSZ 2
  71. #define DT_PLTGOT 3
  72. #define DT_HASH 4
  73. #define DT_STRTAB 5
  74. #define DT_SYMTAB 6
  75. #define DT_RELA 7
  76. #define DT_RELASZ 8
  77. #define DT_RELAENT 9
  78. #define DT_STRSZ 10
  79. #define DT_SYMENT 11
  80. #define DT_INIT 12
  81. #define DT_FINI 13
  82. #define DT_SONAME 14
  83. #define DT_RPATH 15
  84. #define DT_SYMBOLIC 16
  85. #define DT_REL 17
  86. #define DT_RELSZ 18
  87. #define DT_RELENT 19
  88. #define DT_PLTREL 20
  89. #define DT_DEBUG 21
  90. #define DT_TEXTREL 22
  91. #define DT_JMPREL 23
  92. #define DT_ENCODING 32
  93. #define OLD_DT_LOOS 0x60000000
  94. #define DT_LOOS 0x6000000d
  95. #define DT_HIOS 0x6ffff000
  96. #define DT_VALRNGLO 0x6ffffd00
  97. #define DT_VALRNGHI 0x6ffffdff
  98. #define DT_ADDRRNGLO 0x6ffffe00
  99. #define DT_ADDRRNGHI 0x6ffffeff
  100. #define DT_VERSYM 0x6ffffff0
  101. #define DT_RELACOUNT 0x6ffffff9
  102. #define DT_RELCOUNT 0x6ffffffa
  103. #define DT_FLAGS_1 0x6ffffffb
  104. #define DT_VERDEF 0x6ffffffc
  105. #define DT_VERDEFNUM 0x6ffffffd
  106. #define DT_VERNEED 0x6ffffffe
  107. #define DT_VERNEEDNUM 0x6fffffff
  108. #define OLD_DT_HIOS 0x6fffffff
  109. #define DT_LOPROC 0x70000000
  110. #define DT_HIPROC 0x7fffffff
  111. /* This info is needed when parsing the symbol table */
  112. #define STB_LOCAL 0
  113. #define STB_GLOBAL 1
  114. #define STB_WEAK 2
  115. #define STT_NOTYPE 0
  116. #define STT_OBJECT 1
  117. #define STT_FUNC 2
  118. #define STT_SECTION 3
  119. #define STT_FILE 4
  120. #define STT_COMMON 5
  121. #define STT_TLS 6
  122. #define ELF_ST_BIND(x) ((x) >> 4)
  123. #define ELF_ST_TYPE(x) (((unsigned int) x) & 0xf)
  124. #define ELF32_ST_BIND(x) ELF_ST_BIND(x)
  125. #define ELF32_ST_TYPE(x) ELF_ST_TYPE(x)
  126. #define ELF64_ST_BIND(x) ELF_ST_BIND(x)
  127. #define ELF64_ST_TYPE(x) ELF_ST_TYPE(x)
  128. typedef struct dynamic{
  129. Elf32_Sword d_tag;
  130. union{
  131. Elf32_Sword d_val;
  132. Elf32_Addr d_ptr;
  133. } d_un;
  134. } Elf32_Dyn;
  135. typedef struct {
  136. Elf64_Sxword d_tag; /* entry tag value */
  137. union {
  138. Elf64_Xword d_val;
  139. Elf64_Addr d_ptr;
  140. } d_un;
  141. } Elf64_Dyn;
  142. /* The following are used with relocations */
  143. #define ELF32_R_SYM(x) ((x) >> 8)
  144. #define ELF32_R_TYPE(x) ((x) & 0xff)
  145. #define ELF64_R_SYM(i) ((i) >> 32)
  146. #define ELF64_R_TYPE(i) ((i) & 0xffffffff)
  147. typedef struct elf32_rel {
  148. Elf32_Addr r_offset;
  149. Elf32_Word r_info;
  150. } Elf32_Rel;
  151. typedef struct elf64_rel {
  152. Elf64_Addr r_offset; /* Location at which to apply the action */
  153. Elf64_Xword r_info; /* index and type of relocation */
  154. } Elf64_Rel;
  155. typedef struct elf32_rela{
  156. Elf32_Addr r_offset;
  157. Elf32_Word r_info;
  158. Elf32_Sword r_addend;
  159. } Elf32_Rela;
  160. typedef struct elf64_rela {
  161. Elf64_Addr r_offset; /* Location at which to apply the action */
  162. Elf64_Xword r_info; /* index and type of relocation */
  163. Elf64_Sxword r_addend; /* Constant addend used to compute value */
  164. } Elf64_Rela;
  165. typedef struct elf32_sym{
  166. Elf32_Word st_name;
  167. Elf32_Addr st_value;
  168. Elf32_Word st_size;
  169. unsigned char st_info;
  170. unsigned char st_other;
  171. Elf32_Half st_shndx;
  172. } Elf32_Sym;
  173. typedef struct elf64_sym {
  174. Elf64_Word st_name; /* Symbol name, index in string tbl */
  175. unsigned char st_info; /* Type and binding attributes */
  176. unsigned char st_other; /* No defined meaning, 0 */
  177. Elf64_Half st_shndx; /* Associated section index */
  178. Elf64_Addr st_value; /* Value of the symbol */
  179. Elf64_Xword st_size; /* Associated symbol size */
  180. } Elf64_Sym;
  181. #define EI_NIDENT 16
  182. typedef struct elf32_hdr{
  183. unsigned char e_ident[EI_NIDENT];
  184. Elf32_Half e_type;
  185. Elf32_Half e_machine;
  186. Elf32_Word e_version;
  187. Elf32_Addr e_entry; /* Entry point */
  188. Elf32_Off e_phoff;
  189. Elf32_Off e_shoff;
  190. Elf32_Word e_flags;
  191. Elf32_Half e_ehsize;
  192. Elf32_Half e_phentsize;
  193. Elf32_Half e_phnum;
  194. Elf32_Half e_shentsize;
  195. Elf32_Half e_shnum;
  196. Elf32_Half e_shstrndx;
  197. } Elf32_Ehdr;
  198. typedef struct elf64_hdr {
  199. unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */
  200. Elf64_Half e_type;
  201. Elf64_Half e_machine;
  202. Elf64_Word e_version;
  203. Elf64_Addr e_entry; /* Entry point virtual address */
  204. Elf64_Off e_phoff; /* Program header table file offset */
  205. Elf64_Off e_shoff; /* Section header table file offset */
  206. Elf64_Word e_flags;
  207. Elf64_Half e_ehsize;
  208. Elf64_Half e_phentsize;
  209. Elf64_Half e_phnum;
  210. Elf64_Half e_shentsize;
  211. Elf64_Half e_shnum;
  212. Elf64_Half e_shstrndx;
  213. } Elf64_Ehdr;
  214. /* These constants define the permissions on sections in the program
  215. header, p_flags. */
  216. #define PF_R 0x4
  217. #define PF_W 0x2
  218. #define PF_X 0x1
  219. typedef struct elf32_phdr{
  220. Elf32_Word p_type;
  221. Elf32_Off p_offset;
  222. Elf32_Addr p_vaddr;
  223. Elf32_Addr p_paddr;
  224. Elf32_Word p_filesz;
  225. Elf32_Word p_memsz;
  226. Elf32_Word p_flags;
  227. Elf32_Word p_align;
  228. } Elf32_Phdr;
  229. typedef struct elf64_phdr {
  230. Elf64_Word p_type;
  231. Elf64_Word p_flags;
  232. Elf64_Off p_offset; /* Segment file offset */
  233. Elf64_Addr p_vaddr; /* Segment virtual address */
  234. Elf64_Addr p_paddr; /* Segment physical address */
  235. Elf64_Xword p_filesz; /* Segment size in file */
  236. Elf64_Xword p_memsz; /* Segment size in memory */
  237. Elf64_Xword p_align; /* Segment alignment, file & memory */
  238. } Elf64_Phdr;
  239. /* sh_type */
  240. #define SHT_NULL 0
  241. #define SHT_PROGBITS 1
  242. #define SHT_SYMTAB 2
  243. #define SHT_STRTAB 3
  244. #define SHT_RELA 4
  245. #define SHT_HASH 5
  246. #define SHT_DYNAMIC 6
  247. #define SHT_NOTE 7
  248. #define SHT_NOBITS 8
  249. #define SHT_REL 9
  250. #define SHT_SHLIB 10
  251. #define SHT_DYNSYM 11
  252. #define SHT_NUM 12
  253. #define SHT_LOPROC 0x70000000
  254. #define SHT_HIPROC 0x7fffffff
  255. #define SHT_LOUSER 0x80000000
  256. #define SHT_HIUSER 0xffffffff
  257. /* sh_flags */
  258. #define SHF_WRITE 0x1
  259. #define SHF_ALLOC 0x2
  260. #define SHF_EXECINSTR 0x4
  261. #define SHF_MASKPROC 0xf0000000
  262. /* special section indexes */
  263. #define SHN_UNDEF 0
  264. #define SHN_LORESERVE 0xff00
  265. #define SHN_LOPROC 0xff00
  266. #define SHN_HIPROC 0xff1f
  267. #define SHN_ABS 0xfff1
  268. #define SHN_COMMON 0xfff2
  269. #define SHN_HIRESERVE 0xffff
  270. typedef struct elf32_shdr {
  271. Elf32_Word sh_name;
  272. Elf32_Word sh_type;
  273. Elf32_Word sh_flags;
  274. Elf32_Addr sh_addr;
  275. Elf32_Off sh_offset;
  276. Elf32_Word sh_size;
  277. Elf32_Word sh_link;
  278. Elf32_Word sh_info;
  279. Elf32_Word sh_addralign;
  280. Elf32_Word sh_entsize;
  281. } Elf32_Shdr;
  282. typedef struct elf64_shdr {
  283. Elf64_Word sh_name; /* Section name, index in string tbl */
  284. Elf64_Word sh_type; /* Type of section */
  285. Elf64_Xword sh_flags; /* Miscellaneous section attributes */
  286. Elf64_Addr sh_addr; /* Section virtual addr at execution */
  287. Elf64_Off sh_offset; /* Section file offset */
  288. Elf64_Xword sh_size; /* Size of section in bytes */
  289. Elf64_Word sh_link; /* Index of another section */
  290. Elf64_Word sh_info; /* Additional section information */
  291. Elf64_Xword sh_addralign; /* Section alignment */
  292. Elf64_Xword sh_entsize; /* Entry size if section holds table */
  293. } Elf64_Shdr;
  294. #define EI_MAG0 0 /* e_ident[] indexes */
  295. #define EI_MAG1 1
  296. #define EI_MAG2 2
  297. #define EI_MAG3 3
  298. #define EI_CLASS 4
  299. #define EI_DATA 5
  300. #define EI_VERSION 6
  301. #define EI_OSABI 7
  302. #define EI_PAD 8
  303. #define ELFMAG0 0x7f /* EI_MAG */
  304. #define ELFMAG1 'E'
  305. #define ELFMAG2 'L'
  306. #define ELFMAG3 'F'
  307. #define ELFMAG "\177ELF"
  308. #define SELFMAG 4
  309. #define ELFCLASSNONE 0 /* EI_CLASS */
  310. #define ELFCLASS32 1
  311. #define ELFCLASS64 2
  312. #define ELFCLASSNUM 3
  313. #define ELFDATANONE 0 /* e_ident[EI_DATA] */
  314. #define ELFDATA2LSB 1
  315. #define ELFDATA2MSB 2
  316. #define EV_NONE 0 /* e_version, EI_VERSION */
  317. #define EV_CURRENT 1
  318. #define EV_NUM 2
  319. #define ELFOSABI_NONE 0
  320. #define ELFOSABI_LINUX 3
  321. #ifndef ELF_OSABI
  322. #define ELF_OSABI ELFOSABI_NONE
  323. #endif
  324. /*
  325. * Notes used in ET_CORE. Architectures export some of the arch register sets
  326. * using the corresponding note types via the PTRACE_GETREGSET and
  327. * PTRACE_SETREGSET requests.
  328. */
  329. #define NT_PRSTATUS 1
  330. #define NT_PRFPREG 2
  331. #define NT_PRPSINFO 3
  332. #define NT_TASKSTRUCT 4
  333. #define NT_AUXV 6
  334. #define NT_PRXFPREG 0x46e62b7f /* copied from gdb5.1/include/elf/common.h */
  335. #define NT_PPC_VMX 0x100 /* PowerPC Altivec/VMX registers */
  336. #define NT_PPC_SPE 0x101 /* PowerPC SPE/EVR registers */
  337. #define NT_PPC_VSX 0x102 /* PowerPC VSX registers */
  338. #define NT_386_TLS 0x200 /* i386 TLS slots (struct user_desc) */
  339. #define NT_386_IOPERM 0x201 /* x86 io permission bitmap (1=deny) */
  340. #define NT_X86_XSTATE 0x202 /* x86 extended state using xsave */
  341. #define NT_S390_HIGH_GPRS 0x300 /* s390 upper register halves */
  342. #define NT_S390_TIMER 0x301 /* s390 timer register */
  343. #define NT_S390_TODCMP 0x302 /* s390 TOD clock comparator register */
  344. #define NT_S390_TODPREG 0x303 /* s390 TOD programmable register */
  345. #define NT_S390_CTRS 0x304 /* s390 control registers */
  346. #define NT_S390_PREFIX 0x305 /* s390 prefix register */
  347. #define NT_S390_LAST_BREAK 0x306 /* s390 breaking event address */
  348. #define NT_S390_SYSTEM_CALL 0x307 /* s390 system call restart data */
  349. #define NT_ARM_VFP 0x400 /* ARM VFP/NEON registers */
  350. /* Note header in a PT_NOTE section */
  351. typedef struct elf32_note {
  352. Elf32_Word n_namesz; /* Name size */
  353. Elf32_Word n_descsz; /* Content size */
  354. Elf32_Word n_type; /* Content type */
  355. } Elf32_Nhdr;
  356. /* Note header in a PT_NOTE section */
  357. typedef struct elf64_note {
  358. Elf64_Word n_namesz; /* Name size */
  359. Elf64_Word n_descsz; /* Content size */
  360. Elf64_Word n_type; /* Content type */
  361. } Elf64_Nhdr;
  362. #ifdef __KERNEL__
  363. #ifndef elf_read_implies_exec
  364. /* Executables for which elf_read_implies_exec() returns TRUE will
  365. have the READ_IMPLIES_EXEC personality flag set automatically.
  366. Override in asm/elf.h as needed. */
  367. # define elf_read_implies_exec(ex, have_pt_gnu_stack) 0
  368. #endif
  369. #if ELF_CLASS == ELFCLASS32
  370. extern Elf32_Dyn _DYNAMIC [];
  371. #define elfhdr elf32_hdr
  372. #define elf_phdr elf32_phdr
  373. #define elf_shdr elf32_shdr
  374. #define elf_note elf32_note
  375. #define elf_addr_t Elf32_Off
  376. #define Elf_Half Elf32_Half
  377. #else
  378. extern Elf64_Dyn _DYNAMIC [];
  379. #define elfhdr elf64_hdr
  380. #define elf_phdr elf64_phdr
  381. #define elf_shdr elf64_shdr
  382. #define elf_note elf64_note
  383. #define elf_addr_t Elf64_Off
  384. #define Elf_Half Elf64_Half
  385. #endif
  386. /* Optional callbacks to write extra ELF notes. */
  387. struct file;
  388. #ifndef ARCH_HAVE_EXTRA_ELF_NOTES
  389. static inline int elf_coredump_extra_notes_size(void) { return 0; }
  390. static inline int elf_coredump_extra_notes_write(struct file *file,
  391. loff_t *foffset) { return 0; }
  392. #else
  393. extern int elf_coredump_extra_notes_size(void);
  394. extern int elf_coredump_extra_notes_write(struct file *file, loff_t *foffset);
  395. #endif
  396. #endif /* __KERNEL__ */
  397. #endif /* _LINUX_ELF_H */