elfcpp_internal.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // elfcpp_internal.h -- internals for elfcpp -*- C++ -*-
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of elfcpp.
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Library General Public License
  7. // as published by the Free Software Foundation; either version 2, or
  8. // (at your option) any later version.
  9. // In addition to the permissions in the GNU Library General Public
  10. // License, the Free Software Foundation gives you unlimited
  11. // permission to link the compiled version of this file into
  12. // combinations with other programs, and to distribute those
  13. // combinations without any restriction coming from the use of this
  14. // file. (The Library Public License restrictions do apply in other
  15. // respects; for example, they cover modification of the file, and
  16. /// distribution when not linked into a combined executable.)
  17. // This program is distributed in the hope that it will be useful, but
  18. // WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. // Library General Public License for more details.
  21. // You should have received a copy of the GNU Library General Public
  22. // License along with this program; if not, write to the Free Software
  23. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  24. // 02110-1301, USA.
  25. // This is included by elfcpp.h, the external interface, but holds
  26. // information which we want to keep private.
  27. #ifndef ELFCPP_INTERNAL_H
  28. #define ELFCPP_INTERNAL_H
  29. namespace elfcpp
  30. {
  31. namespace internal
  32. {
  33. // The ELF file header.
  34. template<int size>
  35. struct Ehdr_data
  36. {
  37. unsigned char e_ident[EI_NIDENT];
  38. Elf_Half e_type;
  39. Elf_Half e_machine;
  40. Elf_Word e_version;
  41. typename Elf_types<size>::Elf_Addr e_entry;
  42. typename Elf_types<size>::Elf_Off e_phoff;
  43. typename Elf_types<size>::Elf_Off e_shoff;
  44. Elf_Word e_flags;
  45. Elf_Half e_ehsize;
  46. Elf_Half e_phentsize;
  47. Elf_Half e_phnum;
  48. Elf_Half e_shentsize;
  49. Elf_Half e_shnum;
  50. Elf_Half e_shstrndx;
  51. };
  52. // An ELF section header.
  53. template<int size>
  54. struct Shdr_data
  55. {
  56. Elf_Word sh_name;
  57. Elf_Word sh_type;
  58. typename Elf_types<size>::Elf_WXword sh_flags;
  59. typename Elf_types<size>::Elf_Addr sh_addr;
  60. typename Elf_types<size>::Elf_Off sh_offset;
  61. typename Elf_types<size>::Elf_WXword sh_size;
  62. Elf_Word sh_link;
  63. Elf_Word sh_info;
  64. typename Elf_types<size>::Elf_WXword sh_addralign;
  65. typename Elf_types<size>::Elf_WXword sh_entsize;
  66. };
  67. // An ELF compression header.
  68. template<int size>
  69. struct Chdr_data;
  70. template<>
  71. struct Chdr_data<32>
  72. {
  73. Elf_Word ch_type;
  74. Elf_Word ch_size;
  75. Elf_Word ch_addralign;
  76. };
  77. template<>
  78. struct Chdr_data<64>
  79. {
  80. Elf_Word ch_type;
  81. Elf_Word ch_reserved;
  82. Elf_Xword ch_size;
  83. Elf_Xword ch_addralign;
  84. };
  85. // An ELF segment header. We use template specialization for the
  86. // 32-bit and 64-bit versions because the fields are in a different
  87. // order.
  88. template<int size>
  89. struct Phdr_data;
  90. template<>
  91. struct Phdr_data<32>
  92. {
  93. Elf_Word p_type;
  94. Elf_types<32>::Elf_Off p_offset;
  95. Elf_types<32>::Elf_Addr p_vaddr;
  96. Elf_types<32>::Elf_Addr p_paddr;
  97. Elf_Word p_filesz;
  98. Elf_Word p_memsz;
  99. Elf_Word p_flags;
  100. Elf_Word p_align;
  101. };
  102. template<>
  103. struct Phdr_data<64>
  104. {
  105. Elf_Word p_type;
  106. Elf_Word p_flags;
  107. Elf_types<64>::Elf_Off p_offset;
  108. Elf_types<64>::Elf_Addr p_vaddr;
  109. Elf_types<64>::Elf_Addr p_paddr;
  110. Elf_Xword p_filesz;
  111. Elf_Xword p_memsz;
  112. Elf_Xword p_align;
  113. };
  114. // An ELF symbol table entry. We use template specialization for the
  115. // 32-bit and 64-bit versions because the fields are in a different
  116. // order.
  117. template<int size>
  118. struct Sym_data;
  119. template<>
  120. struct Sym_data<32>
  121. {
  122. Elf_Word st_name;
  123. Elf_types<32>::Elf_Addr st_value;
  124. Elf_Word st_size;
  125. unsigned char st_info;
  126. unsigned char st_other;
  127. Elf_Half st_shndx;
  128. };
  129. template<>
  130. struct Sym_data<64>
  131. {
  132. Elf_Word st_name;
  133. unsigned char st_info;
  134. unsigned char st_other;
  135. Elf_Half st_shndx;
  136. Elf_types<64>::Elf_Addr st_value;
  137. Elf_Xword st_size;
  138. };
  139. // ELF relocation table entries.
  140. template<int size>
  141. struct Rel_data
  142. {
  143. typename Elf_types<size>::Elf_Addr r_offset;
  144. typename Elf_types<size>::Elf_WXword r_info;
  145. };
  146. template<int size>
  147. struct Rela_data
  148. {
  149. typename Elf_types<size>::Elf_Addr r_offset;
  150. typename Elf_types<size>::Elf_WXword r_info;
  151. typename Elf_types<size>::Elf_Swxword r_addend;
  152. };
  153. // MIPS-64 has a non-standard layout for relocations.
  154. struct Mips64_rel_data
  155. {
  156. Elf_types<64>::Elf_Addr r_offset;
  157. Elf_Word r_sym;
  158. unsigned char r_ssym;
  159. unsigned char r_type3;
  160. unsigned char r_type2;
  161. unsigned char r_type;
  162. };
  163. struct Mips64_rela_data
  164. {
  165. Elf_types<64>::Elf_Addr r_offset;
  166. Elf_Word r_sym;
  167. unsigned char r_ssym;
  168. unsigned char r_type3;
  169. unsigned char r_type2;
  170. unsigned char r_type;
  171. Elf_types<64>::Elf_Swxword r_addend;
  172. };
  173. // An entry in the ELF SHT_DYNAMIC section aka PT_DYNAMIC segment.
  174. template<int size>
  175. struct Dyn_data
  176. {
  177. typename Elf_types<size>::Elf_Swxword d_tag;
  178. typename Elf_types<size>::Elf_WXword d_val;
  179. };
  180. // An entry in a SHT_GNU_verdef section. This structure is the same
  181. // in 32-bit and 64-bit ELF files.
  182. struct Verdef_data
  183. {
  184. // Version number of structure (VER_DEF_*).
  185. Elf_Half vd_version;
  186. // Bit flags (VER_FLG_*).
  187. Elf_Half vd_flags;
  188. // Version index.
  189. Elf_Half vd_ndx;
  190. // Number of auxiliary Verdaux entries.
  191. Elf_Half vd_cnt;
  192. // Hash of name.
  193. Elf_Word vd_hash;
  194. // Byte offset to first Verdaux entry.
  195. Elf_Word vd_aux;
  196. // Byte offset to next Verdef entry.
  197. Elf_Word vd_next;
  198. };
  199. // An auxiliary entry in a SHT_GNU_verdef section. This structure is
  200. // the same in 32-bit and 64-bit ELF files.
  201. struct Verdaux_data
  202. {
  203. // Offset in string table of version name.
  204. Elf_Word vda_name;
  205. // Byte offset to next Verdaux entry.
  206. Elf_Word vda_next;
  207. };
  208. // An entry in a SHT_GNU_verneed section. This structure is the same
  209. // in 32-bit and 64-bit ELF files.
  210. struct Verneed_data
  211. {
  212. // Version number of structure (VER_NEED_*).
  213. Elf_Half vn_version;
  214. // Number of auxiliary Vernaux entries.
  215. Elf_Half vn_cnt;
  216. // Offset in string table of library name.
  217. Elf_Word vn_file;
  218. // Byte offset to first Vernaux entry.
  219. Elf_Word vn_aux;
  220. // Byt eoffset to next Verneed entry.
  221. Elf_Word vn_next;
  222. };
  223. // An auxiliary entry in a SHT_GNU_verneed section. This structure is
  224. // the same in 32-bit and 64-bit ELF files.
  225. struct Vernaux_data
  226. {
  227. // Hash of dependency name.
  228. Elf_Word vna_hash;
  229. // Bit flags (VER_FLG_*).
  230. Elf_Half vna_flags;
  231. // Version index used in SHT_GNU_versym entries.
  232. Elf_Half vna_other;
  233. // Offset in string table of version name.
  234. Elf_Word vna_name;
  235. // Byte offset to next Vernaux entry.
  236. Elf_Word vna_next;
  237. };
  238. } // End namespace internal.
  239. } // End namespace elfcpp.
  240. #endif // !defined(ELFCPP_INTERNAL_H)