elf.h 12 KB

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