external.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* ELF support for BFD.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. Written by Fred Fish @ Cygnus Support, from information published
  4. in "UNIX System V Release 4, Programmers Guide: ANSI C and
  5. Programming Support Tools".
  6. This file is part of BFD, the Binary File Descriptor library.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  18. MA 02110-1301, USA. */
  19. /* This file is part of ELF support for BFD, and contains the portions
  20. that describe how ELF is represented externally by the BFD library.
  21. I.E. it describes the in-file representation of ELF. It requires
  22. the elf/common.h file which contains the portions that are common to
  23. both the internal and external representations. */
  24. /* The 64-bit stuff is kind of random. Perhaps someone will publish a
  25. spec someday. */
  26. #ifndef _ELF_EXTERNAL_H
  27. #define _ELF_EXTERNAL_H
  28. /* Special section indices, which may show up in st_shndx fields, among
  29. other places. */
  30. #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
  31. #define SHN_LOPROC 0xFF00 /* Begin range of appl-specific */
  32. #define SHN_HIPROC 0xFF1F /* End range of appl-specific */
  33. #define SHN_LOOS 0xFF20 /* OS specific semantics, lo */
  34. #define SHN_HIOS 0xFF3F /* OS specific semantics, hi */
  35. #define SHN_ABS 0xFFF1 /* Associated symbol is absolute */
  36. #define SHN_COMMON 0xFFF2 /* Associated symbol is in common */
  37. #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
  38. #define SHN_HIRESERVE 0xFFFF /* End range of reserved indices */
  39. /* ELF Header (32-bit implementations) */
  40. typedef struct {
  41. unsigned char e_ident[16]; /* ELF "magic number" */
  42. unsigned char e_type[2]; /* Identifies object file type */
  43. unsigned char e_machine[2]; /* Specifies required architecture */
  44. unsigned char e_version[4]; /* Identifies object file version */
  45. unsigned char e_entry[4]; /* Entry point virtual address */
  46. unsigned char e_phoff[4]; /* Program header table file offset */
  47. unsigned char e_shoff[4]; /* Section header table file offset */
  48. unsigned char e_flags[4]; /* Processor-specific flags */
  49. unsigned char e_ehsize[2]; /* ELF header size in bytes */
  50. unsigned char e_phentsize[2]; /* Program header table entry size */
  51. unsigned char e_phnum[2]; /* Program header table entry count */
  52. unsigned char e_shentsize[2]; /* Section header table entry size */
  53. unsigned char e_shnum[2]; /* Section header table entry count */
  54. unsigned char e_shstrndx[2]; /* Section header string table index */
  55. } Elf32_External_Ehdr;
  56. typedef struct {
  57. unsigned char e_ident[16]; /* ELF "magic number" */
  58. unsigned char e_type[2]; /* Identifies object file type */
  59. unsigned char e_machine[2]; /* Specifies required architecture */
  60. unsigned char e_version[4]; /* Identifies object file version */
  61. unsigned char e_entry[8]; /* Entry point virtual address */
  62. unsigned char e_phoff[8]; /* Program header table file offset */
  63. unsigned char e_shoff[8]; /* Section header table file offset */
  64. unsigned char e_flags[4]; /* Processor-specific flags */
  65. unsigned char e_ehsize[2]; /* ELF header size in bytes */
  66. unsigned char e_phentsize[2]; /* Program header table entry size */
  67. unsigned char e_phnum[2]; /* Program header table entry count */
  68. unsigned char e_shentsize[2]; /* Section header table entry size */
  69. unsigned char e_shnum[2]; /* Section header table entry count */
  70. unsigned char e_shstrndx[2]; /* Section header string table index */
  71. } Elf64_External_Ehdr;
  72. /* Program header */
  73. typedef struct {
  74. unsigned char p_type[4]; /* Identifies program segment type */
  75. unsigned char p_offset[4]; /* Segment file offset */
  76. unsigned char p_vaddr[4]; /* Segment virtual address */
  77. unsigned char p_paddr[4]; /* Segment physical address */
  78. unsigned char p_filesz[4]; /* Segment size in file */
  79. unsigned char p_memsz[4]; /* Segment size in memory */
  80. unsigned char p_flags[4]; /* Segment flags */
  81. unsigned char p_align[4]; /* Segment alignment, file & memory */
  82. } Elf32_External_Phdr;
  83. typedef struct {
  84. unsigned char p_type[4]; /* Identifies program segment type */
  85. unsigned char p_flags[4]; /* Segment flags */
  86. unsigned char p_offset[8]; /* Segment file offset */
  87. unsigned char p_vaddr[8]; /* Segment virtual address */
  88. unsigned char p_paddr[8]; /* Segment physical address */
  89. unsigned char p_filesz[8]; /* Segment size in file */
  90. unsigned char p_memsz[8]; /* Segment size in memory */
  91. unsigned char p_align[8]; /* Segment alignment, file & memory */
  92. } Elf64_External_Phdr;
  93. /* Section header */
  94. typedef struct {
  95. unsigned char sh_name[4]; /* Section name, index in string tbl */
  96. unsigned char sh_type[4]; /* Type of section */
  97. unsigned char sh_flags[4]; /* Miscellaneous section attributes */
  98. unsigned char sh_addr[4]; /* Section virtual addr at execution */
  99. unsigned char sh_offset[4]; /* Section file offset */
  100. unsigned char sh_size[4]; /* Size of section in bytes */
  101. unsigned char sh_link[4]; /* Index of another section */
  102. unsigned char sh_info[4]; /* Additional section information */
  103. unsigned char sh_addralign[4]; /* Section alignment */
  104. unsigned char sh_entsize[4]; /* Entry size if section holds table */
  105. } Elf32_External_Shdr;
  106. typedef struct {
  107. unsigned char sh_name[4]; /* Section name, index in string tbl */
  108. unsigned char sh_type[4]; /* Type of section */
  109. unsigned char sh_flags[8]; /* Miscellaneous section attributes */
  110. unsigned char sh_addr[8]; /* Section virtual addr at execution */
  111. unsigned char sh_offset[8]; /* Section file offset */
  112. unsigned char sh_size[8]; /* Size of section in bytes */
  113. unsigned char sh_link[4]; /* Index of another section */
  114. unsigned char sh_info[4]; /* Additional section information */
  115. unsigned char sh_addralign[8]; /* Section alignment */
  116. unsigned char sh_entsize[8]; /* Entry size if section holds table */
  117. } Elf64_External_Shdr;
  118. /* Compression header */
  119. typedef struct {
  120. unsigned char ch_type[4]; /* Type of compression */
  121. unsigned char ch_size[4]; /* Size of uncompressed data in bytes */
  122. unsigned char ch_addralign[4]; /* Alignment of uncompressed data */
  123. } Elf32_External_Chdr;
  124. typedef struct {
  125. unsigned char ch_type[4]; /* Type of compression */
  126. unsigned char ch_reserved[4]; /* Padding */
  127. unsigned char ch_size[8]; /* Size of uncompressed data in bytes */
  128. unsigned char ch_addralign[8]; /* Alignment of uncompressed data */
  129. } Elf64_External_Chdr;
  130. /* Symbol table entry */
  131. typedef struct {
  132. unsigned char st_name[4]; /* Symbol name, index in string tbl */
  133. unsigned char st_value[4]; /* Value of the symbol */
  134. unsigned char st_size[4]; /* Associated symbol size */
  135. unsigned char st_info[1]; /* Type and binding attributes */
  136. unsigned char st_other[1]; /* No defined meaning, 0 */
  137. unsigned char st_shndx[2]; /* Associated section index */
  138. } Elf32_External_Sym;
  139. typedef struct {
  140. unsigned char st_name[4]; /* Symbol name, index in string tbl */
  141. unsigned char st_info[1]; /* Type and binding attributes */
  142. unsigned char st_other[1]; /* No defined meaning, 0 */
  143. unsigned char st_shndx[2]; /* Associated section index */
  144. unsigned char st_value[8]; /* Value of the symbol */
  145. unsigned char st_size[8]; /* Associated symbol size */
  146. } Elf64_External_Sym;
  147. typedef struct {
  148. unsigned char est_shndx[4]; /* Section index */
  149. } Elf_External_Sym_Shndx;
  150. /* Note segments */
  151. typedef struct {
  152. unsigned char namesz[4]; /* Size of entry's owner string */
  153. unsigned char descsz[4]; /* Size of the note descriptor */
  154. unsigned char type[4]; /* Interpretation of the descriptor */
  155. char name[1]; /* Start of the name+desc data */
  156. } Elf_External_Note;
  157. /* Align an address upward to a boundary, expressed as a number of bytes.
  158. E.g. align to an 8-byte boundary with argument of 8. */
  159. #define ELF_ALIGN_UP(addr, boundary) \
  160. (((bfd_vma) (addr) + ((boundary) - 1)) & ~ (bfd_vma) ((boundary) -1))
  161. /* Compute the offset of the note descriptor from size of note entry's
  162. owner string and note alignment. */
  163. #define ELF_NOTE_DESC_OFFSET(namesz, align) \
  164. ELF_ALIGN_UP (offsetof (Elf_External_Note, name) + (namesz), (align))
  165. /* Compute the offset of the next note entry from size of note entry's
  166. owner string, size of the note descriptor and note alignment. */
  167. #define ELF_NOTE_NEXT_OFFSET(namesz, descsz, align) \
  168. ELF_ALIGN_UP (ELF_NOTE_DESC_OFFSET ((namesz), (align)) + (descsz), \
  169. (align))
  170. /* Relocation Entries */
  171. typedef struct {
  172. unsigned char r_offset[4]; /* Location at which to apply the action */
  173. unsigned char r_info[4]; /* index and type of relocation */
  174. } Elf32_External_Rel;
  175. typedef struct {
  176. unsigned char r_offset[4]; /* Location at which to apply the action */
  177. unsigned char r_info[4]; /* index and type of relocation */
  178. unsigned char r_addend[4]; /* Constant addend used to compute value */
  179. } Elf32_External_Rela;
  180. typedef struct {
  181. unsigned char r_data[4]; /* RELR entry */
  182. } Elf32_External_Relr;
  183. typedef struct {
  184. unsigned char r_offset[8]; /* Location at which to apply the action */
  185. unsigned char r_info[8]; /* index and type of relocation */
  186. } Elf64_External_Rel;
  187. typedef struct {
  188. unsigned char r_offset[8]; /* Location at which to apply the action */
  189. unsigned char r_info[8]; /* index and type of relocation */
  190. unsigned char r_addend[8]; /* Constant addend used to compute value */
  191. } Elf64_External_Rela;
  192. typedef struct {
  193. unsigned char r_data[8]; /* RELR entry */
  194. } Elf64_External_Relr;
  195. /* dynamic section structure */
  196. typedef struct {
  197. unsigned char d_tag[4]; /* entry tag value */
  198. union {
  199. unsigned char d_val[4];
  200. unsigned char d_ptr[4];
  201. } d_un;
  202. } Elf32_External_Dyn;
  203. typedef struct {
  204. unsigned char d_tag[8]; /* entry tag value */
  205. union {
  206. unsigned char d_val[8];
  207. unsigned char d_ptr[8];
  208. } d_un;
  209. } Elf64_External_Dyn;
  210. /* The version structures are currently size independent. They are
  211. named without a 32 or 64. If that ever changes, these structures
  212. will need to be renamed. */
  213. /* This structure appears in a SHT_GNU_verdef section. */
  214. typedef struct {
  215. unsigned char vd_version[2];
  216. unsigned char vd_flags[2];
  217. unsigned char vd_ndx[2];
  218. unsigned char vd_cnt[2];
  219. unsigned char vd_hash[4];
  220. unsigned char vd_aux[4];
  221. unsigned char vd_next[4];
  222. } Elf_External_Verdef;
  223. /* This structure appears in a SHT_GNU_verdef section. */
  224. typedef struct {
  225. unsigned char vda_name[4];
  226. unsigned char vda_next[4];
  227. } Elf_External_Verdaux;
  228. /* This structure appears in a SHT_GNU_verneed section. */
  229. typedef struct {
  230. unsigned char vn_version[2];
  231. unsigned char vn_cnt[2];
  232. unsigned char vn_file[4];
  233. unsigned char vn_aux[4];
  234. unsigned char vn_next[4];
  235. } Elf_External_Verneed;
  236. /* This structure appears in a SHT_GNU_verneed section. */
  237. typedef struct {
  238. unsigned char vna_hash[4];
  239. unsigned char vna_flags[2];
  240. unsigned char vna_other[2];
  241. unsigned char vna_name[4];
  242. unsigned char vna_next[4];
  243. } Elf_External_Vernaux;
  244. /* This structure appears in a SHT_GNU_versym section. This is not a
  245. standard ELF structure; ELF just uses Elf32_Half. */
  246. typedef struct {
  247. unsigned char vs_vers[2];
  248. } ATTRIBUTE_PACKED Elf_External_Versym;
  249. /* Structure for syminfo section. */
  250. typedef struct
  251. {
  252. unsigned char si_boundto[2];
  253. unsigned char si_flags[2];
  254. } Elf_External_Syminfo;
  255. /* This structure appears on the stack and in NT_AUXV core file notes. */
  256. typedef struct
  257. {
  258. unsigned char a_type[4];
  259. unsigned char a_val[4];
  260. } Elf32_External_Auxv;
  261. typedef struct
  262. {
  263. unsigned char a_type[8];
  264. unsigned char a_val[8];
  265. } Elf64_External_Auxv;
  266. /* Size of SHT_GROUP section entry. */
  267. #define GRP_ENTRY_SIZE 4
  268. #endif /* _ELF_EXTERNAL_H */