elf.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #ifndef _LINUX_ELF_H
  2. #define _LINUX_ELF_H
  3. #include <linux/types.h>
  4. #include <linux/auxvec.h>
  5. #include <linux/elf-em.h>
  6. #include <asm/elf.h>
  7. #ifndef elf_read_implies_exec
  8. /* Executables for which elf_read_implies_exec() returns TRUE will
  9. have the READ_IMPLIES_EXEC personality flag set automatically.
  10. Override in asm/elf.h as needed. */
  11. # define elf_read_implies_exec(ex, have_pt_gnu_stack) 0
  12. #endif
  13. /* 32-bit ELF base types. */
  14. typedef __u32 Elf32_Addr;
  15. typedef __u16 Elf32_Half;
  16. typedef __u32 Elf32_Off;
  17. typedef __s32 Elf32_Sword;
  18. typedef __u32 Elf32_Word;
  19. /* 64-bit ELF base types. */
  20. typedef __u64 Elf64_Addr;
  21. typedef __u16 Elf64_Half;
  22. typedef __s16 Elf64_SHalf;
  23. typedef __u64 Elf64_Off;
  24. typedef __s32 Elf64_Sword;
  25. typedef __u32 Elf64_Word;
  26. typedef __u64 Elf64_Xword;
  27. typedef __s64 Elf64_Sxword;
  28. /* These constants are for the segment types stored in the image headers */
  29. #define PT_NULL 0
  30. #define PT_LOAD 1
  31. #define PT_DYNAMIC 2
  32. #define PT_INTERP 3
  33. #define PT_NOTE 4
  34. #define PT_SHLIB 5
  35. #define PT_PHDR 6
  36. #define PT_TLS 7 /* Thread local storage segment */
  37. #define PT_LOOS 0x60000000 /* OS-specific */
  38. #define PT_HIOS 0x6fffffff /* OS-specific */
  39. #define PT_LOPROC 0x70000000
  40. #define PT_HIPROC 0x7fffffff
  41. #define PT_GNU_EH_FRAME 0x6474e550
  42. #define PT_GNU_STACK (PT_LOOS + 0x474e551)
  43. /* These constants define the different elf file types */
  44. #define ET_NONE 0
  45. #define ET_REL 1
  46. #define ET_EXEC 2
  47. #define ET_DYN 3
  48. #define ET_CORE 4
  49. #define ET_LOPROC 0xff00
  50. #define ET_HIPROC 0xffff
  51. /* This is the info that is needed to parse the dynamic section of the file */
  52. #define DT_NULL 0
  53. #define DT_NEEDED 1
  54. #define DT_PLTRELSZ 2
  55. #define DT_PLTGOT 3
  56. #define DT_HASH 4
  57. #define DT_STRTAB 5
  58. #define DT_SYMTAB 6
  59. #define DT_RELA 7
  60. #define DT_RELASZ 8
  61. #define DT_RELAENT 9
  62. #define DT_STRSZ 10
  63. #define DT_SYMENT 11
  64. #define DT_INIT 12
  65. #define DT_FINI 13
  66. #define DT_SONAME 14
  67. #define DT_RPATH 15
  68. #define DT_SYMBOLIC 16
  69. #define DT_REL 17
  70. #define DT_RELSZ 18
  71. #define DT_RELENT 19
  72. #define DT_PLTREL 20
  73. #define DT_DEBUG 21
  74. #define DT_TEXTREL 22
  75. #define DT_JMPREL 23
  76. #define DT_LOPROC 0x70000000
  77. #define DT_HIPROC 0x7fffffff
  78. /* This info is needed when parsing the symbol table */
  79. #define STB_LOCAL 0
  80. #define STB_GLOBAL 1
  81. #define STB_WEAK 2
  82. #define STT_NOTYPE 0
  83. #define STT_OBJECT 1
  84. #define STT_FUNC 2
  85. #define STT_SECTION 3
  86. #define STT_FILE 4
  87. #define STT_COMMON 5
  88. #define STT_TLS 6
  89. #define ELF_ST_BIND(x) ((x) >> 4)
  90. #define ELF_ST_TYPE(x) (((unsigned int) x) & 0xf)
  91. #define ELF32_ST_BIND(x) ELF_ST_BIND(x)
  92. #define ELF32_ST_TYPE(x) ELF_ST_TYPE(x)
  93. #define ELF64_ST_BIND(x) ELF_ST_BIND(x)
  94. #define ELF64_ST_TYPE(x) ELF_ST_TYPE(x)
  95. typedef struct dynamic{
  96. Elf32_Sword d_tag;
  97. union{
  98. Elf32_Sword d_val;
  99. Elf32_Addr d_ptr;
  100. } d_un;
  101. } Elf32_Dyn;
  102. typedef struct {
  103. Elf64_Sxword d_tag; /* entry tag value */
  104. union {
  105. Elf64_Xword d_val;
  106. Elf64_Addr d_ptr;
  107. } d_un;
  108. } Elf64_Dyn;
  109. /* The following are used with relocations */
  110. #define ELF32_R_SYM(x) ((x) >> 8)
  111. #define ELF32_R_TYPE(x) ((x) & 0xff)
  112. #define ELF64_R_SYM(i) ((i) >> 32)
  113. #define ELF64_R_TYPE(i) ((i) & 0xffffffff)
  114. typedef struct elf32_rel {
  115. Elf32_Addr r_offset;
  116. Elf32_Word r_info;
  117. } Elf32_Rel;
  118. typedef struct elf64_rel {
  119. Elf64_Addr r_offset; /* Location at which to apply the action */
  120. Elf64_Xword r_info; /* index and type of relocation */
  121. } Elf64_Rel;
  122. typedef struct elf32_rela{
  123. Elf32_Addr r_offset;
  124. Elf32_Word r_info;
  125. Elf32_Sword r_addend;
  126. } Elf32_Rela;
  127. typedef struct elf64_rela {
  128. Elf64_Addr r_offset; /* Location at which to apply the action */
  129. Elf64_Xword r_info; /* index and type of relocation */
  130. Elf64_Sxword r_addend; /* Constant addend used to compute value */
  131. } Elf64_Rela;
  132. typedef struct elf32_sym{
  133. Elf32_Word st_name;
  134. Elf32_Addr st_value;
  135. Elf32_Word st_size;
  136. unsigned char st_info;
  137. unsigned char st_other;
  138. Elf32_Half st_shndx;
  139. } Elf32_Sym;
  140. typedef struct elf64_sym {
  141. Elf64_Word st_name; /* Symbol name, index in string tbl */
  142. unsigned char st_info; /* Type and binding attributes */
  143. unsigned char st_other; /* No defined meaning, 0 */
  144. Elf64_Half st_shndx; /* Associated section index */
  145. Elf64_Addr st_value; /* Value of the symbol */
  146. Elf64_Xword st_size; /* Associated symbol size */
  147. } Elf64_Sym;
  148. #define EI_NIDENT 16
  149. typedef struct elf32_hdr{
  150. unsigned char e_ident[EI_NIDENT];
  151. Elf32_Half e_type;
  152. Elf32_Half e_machine;
  153. Elf32_Word e_version;
  154. Elf32_Addr e_entry; /* Entry point */
  155. Elf32_Off e_phoff;
  156. Elf32_Off e_shoff;
  157. Elf32_Word e_flags;
  158. Elf32_Half e_ehsize;
  159. Elf32_Half e_phentsize;
  160. Elf32_Half e_phnum;
  161. Elf32_Half e_shentsize;
  162. Elf32_Half e_shnum;
  163. Elf32_Half e_shstrndx;
  164. } Elf32_Ehdr;
  165. typedef struct elf64_hdr {
  166. unsigned char e_ident[16]; /* ELF "magic number" */
  167. Elf64_Half e_type;
  168. Elf64_Half e_machine;
  169. Elf64_Word e_version;
  170. Elf64_Addr e_entry; /* Entry point virtual address */
  171. Elf64_Off e_phoff; /* Program header table file offset */
  172. Elf64_Off e_shoff; /* Section header table file offset */
  173. Elf64_Word e_flags;
  174. Elf64_Half e_ehsize;
  175. Elf64_Half e_phentsize;
  176. Elf64_Half e_phnum;
  177. Elf64_Half e_shentsize;
  178. Elf64_Half e_shnum;
  179. Elf64_Half e_shstrndx;
  180. } Elf64_Ehdr;
  181. /* These constants define the permissions on sections in the program
  182. header, p_flags. */
  183. #define PF_R 0x4
  184. #define PF_W 0x2
  185. #define PF_X 0x1
  186. typedef struct elf32_phdr{
  187. Elf32_Word p_type;
  188. Elf32_Off p_offset;
  189. Elf32_Addr p_vaddr;
  190. Elf32_Addr p_paddr;
  191. Elf32_Word p_filesz;
  192. Elf32_Word p_memsz;
  193. Elf32_Word p_flags;
  194. Elf32_Word p_align;
  195. } Elf32_Phdr;
  196. typedef struct elf64_phdr {
  197. Elf64_Word p_type;
  198. Elf64_Word p_flags;
  199. Elf64_Off p_offset; /* Segment file offset */
  200. Elf64_Addr p_vaddr; /* Segment virtual address */
  201. Elf64_Addr p_paddr; /* Segment physical address */
  202. Elf64_Xword p_filesz; /* Segment size in file */
  203. Elf64_Xword p_memsz; /* Segment size in memory */
  204. Elf64_Xword p_align; /* Segment alignment, file & memory */
  205. } Elf64_Phdr;
  206. /* sh_type */
  207. #define SHT_NULL 0
  208. #define SHT_PROGBITS 1
  209. #define SHT_SYMTAB 2
  210. #define SHT_STRTAB 3
  211. #define SHT_RELA 4
  212. #define SHT_HASH 5
  213. #define SHT_DYNAMIC 6
  214. #define SHT_NOTE 7
  215. #define SHT_NOBITS 8
  216. #define SHT_REL 9
  217. #define SHT_SHLIB 10
  218. #define SHT_DYNSYM 11
  219. #define SHT_NUM 12
  220. #define SHT_LOPROC 0x70000000
  221. #define SHT_HIPROC 0x7fffffff
  222. #define SHT_LOUSER 0x80000000
  223. #define SHT_HIUSER 0xffffffff
  224. /* sh_flags */
  225. #define SHF_WRITE 0x1
  226. #define SHF_ALLOC 0x2
  227. #define SHF_EXECINSTR 0x4
  228. #define SHF_MASKPROC 0xf0000000
  229. /* special section indexes */
  230. #define SHN_UNDEF 0
  231. #define SHN_LORESERVE 0xff00
  232. #define SHN_LOPROC 0xff00
  233. #define SHN_HIPROC 0xff1f
  234. #define SHN_ABS 0xfff1
  235. #define SHN_COMMON 0xfff2
  236. #define SHN_HIRESERVE 0xffff
  237. typedef struct {
  238. Elf32_Word sh_name;
  239. Elf32_Word sh_type;
  240. Elf32_Word sh_flags;
  241. Elf32_Addr sh_addr;
  242. Elf32_Off sh_offset;
  243. Elf32_Word sh_size;
  244. Elf32_Word sh_link;
  245. Elf32_Word sh_info;
  246. Elf32_Word sh_addralign;
  247. Elf32_Word sh_entsize;
  248. } Elf32_Shdr;
  249. typedef struct elf64_shdr {
  250. Elf64_Word sh_name; /* Section name, index in string tbl */
  251. Elf64_Word sh_type; /* Type of section */
  252. Elf64_Xword sh_flags; /* Miscellaneous section attributes */
  253. Elf64_Addr sh_addr; /* Section virtual addr at execution */
  254. Elf64_Off sh_offset; /* Section file offset */
  255. Elf64_Xword sh_size; /* Size of section in bytes */
  256. Elf64_Word sh_link; /* Index of another section */
  257. Elf64_Word sh_info; /* Additional section information */
  258. Elf64_Xword sh_addralign; /* Section alignment */
  259. Elf64_Xword sh_entsize; /* Entry size if section holds table */
  260. } Elf64_Shdr;
  261. #define EI_MAG0 0 /* e_ident[] indexes */
  262. #define EI_MAG1 1
  263. #define EI_MAG2 2
  264. #define EI_MAG3 3
  265. #define EI_CLASS 4
  266. #define EI_DATA 5
  267. #define EI_VERSION 6
  268. #define EI_OSABI 7
  269. #define EI_PAD 8
  270. #define ELFMAG0 0x7f /* EI_MAG */
  271. #define ELFMAG1 'E'
  272. #define ELFMAG2 'L'
  273. #define ELFMAG3 'F'
  274. #define ELFMAG "\177ELF"
  275. #define SELFMAG 4
  276. #define ELFCLASSNONE 0 /* EI_CLASS */
  277. #define ELFCLASS32 1
  278. #define ELFCLASS64 2
  279. #define ELFCLASSNUM 3
  280. #define ELFDATANONE 0 /* e_ident[EI_DATA] */
  281. #define ELFDATA2LSB 1
  282. #define ELFDATA2MSB 2
  283. #define EV_NONE 0 /* e_version, EI_VERSION */
  284. #define EV_CURRENT 1
  285. #define EV_NUM 2
  286. #define ELFOSABI_NONE 0
  287. #define ELFOSABI_LINUX 3
  288. #ifndef ELF_OSABI
  289. #define ELF_OSABI ELFOSABI_NONE
  290. #endif
  291. /* Notes used in ET_CORE */
  292. #define NT_PRSTATUS 1
  293. #define NT_PRFPREG 2
  294. #define NT_PRPSINFO 3
  295. #define NT_TASKSTRUCT 4
  296. #define NT_AUXV 6
  297. #define NT_PRXFPREG 0x46e62b7f /* copied from gdb5.1/include/elf/common.h */
  298. /* Note header in a PT_NOTE section */
  299. typedef struct elf32_note {
  300. Elf32_Word n_namesz; /* Name size */
  301. Elf32_Word n_descsz; /* Content size */
  302. Elf32_Word n_type; /* Content type */
  303. } Elf32_Nhdr;
  304. /* Note header in a PT_NOTE section */
  305. typedef struct elf64_note {
  306. Elf64_Word n_namesz; /* Name size */
  307. Elf64_Word n_descsz; /* Content size */
  308. Elf64_Word n_type; /* Content type */
  309. } Elf64_Nhdr;
  310. #if ELF_CLASS == ELFCLASS32
  311. extern Elf32_Dyn _DYNAMIC [];
  312. #define elfhdr elf32_hdr
  313. #define elf_phdr elf32_phdr
  314. #define elf_note elf32_note
  315. #else
  316. extern Elf64_Dyn _DYNAMIC [];
  317. #define elfhdr elf64_hdr
  318. #define elf_phdr elf64_phdr
  319. #define elf_note elf64_note
  320. #endif
  321. #endif /* _LINUX_ELF_H */