ELFTypes.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. //===- ELFTypes.h - Endian specific types for ELF ---------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_OBJECT_ELFTYPES_H
  10. #define LLVM_OBJECT_ELFTYPES_H
  11. #include "llvm/ADT/ArrayRef.h"
  12. #include "llvm/Object/Error.h"
  13. #include "llvm/Support/DataTypes.h"
  14. #include "llvm/Support/ELF.h"
  15. #include "llvm/Support/Endian.h"
  16. #include "llvm/Support/ErrorOr.h"
  17. namespace llvm {
  18. namespace object {
  19. using support::endianness;
  20. template <endianness target_endianness, bool is64Bits> struct ELFType {
  21. static const endianness TargetEndianness = target_endianness;
  22. static const bool Is64Bits = is64Bits;
  23. };
  24. typedef ELFType<support::little, false> ELF32LE;
  25. typedef ELFType<support::big, false> ELF32BE;
  26. typedef ELFType<support::little, true> ELF64LE;
  27. typedef ELFType<support::big, true> ELF64BE;
  28. // Use an alignment of 2 for the typedefs since that is the worst case for
  29. // ELF files in archives.
  30. // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
  31. template <endianness target_endianness> struct ELFDataTypeTypedefHelperCommon {
  32. typedef support::detail::packed_endian_specific_integral<
  33. uint16_t, target_endianness, 2> Elf_Half;
  34. typedef support::detail::packed_endian_specific_integral<
  35. uint32_t, target_endianness, 2> Elf_Word;
  36. typedef support::detail::packed_endian_specific_integral<
  37. int32_t, target_endianness, 2> Elf_Sword;
  38. typedef support::detail::packed_endian_specific_integral<
  39. uint64_t, target_endianness, 2> Elf_Xword;
  40. typedef support::detail::packed_endian_specific_integral<
  41. int64_t, target_endianness, 2> Elf_Sxword;
  42. };
  43. template <class ELFT> struct ELFDataTypeTypedefHelper;
  44. /// ELF 32bit types.
  45. template <endianness TargetEndianness>
  46. struct ELFDataTypeTypedefHelper<ELFType<TargetEndianness, false>>
  47. : ELFDataTypeTypedefHelperCommon<TargetEndianness> {
  48. typedef uint32_t value_type;
  49. typedef support::detail::packed_endian_specific_integral<
  50. value_type, TargetEndianness, 2> Elf_Addr;
  51. typedef support::detail::packed_endian_specific_integral<
  52. value_type, TargetEndianness, 2> Elf_Off;
  53. };
  54. /// ELF 64bit types.
  55. template <endianness TargetEndianness>
  56. struct ELFDataTypeTypedefHelper<ELFType<TargetEndianness, true>>
  57. : ELFDataTypeTypedefHelperCommon<TargetEndianness> {
  58. typedef uint64_t value_type;
  59. typedef support::detail::packed_endian_specific_integral<
  60. value_type, TargetEndianness, 2> Elf_Addr;
  61. typedef support::detail::packed_endian_specific_integral<
  62. value_type, TargetEndianness, 2> Elf_Off;
  63. };
  64. // I really don't like doing this, but the alternative is copypasta.
  65. #define LLVM_ELF_IMPORT_TYPES(E, W) \
  66. typedef typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Addr Elf_Addr; \
  67. typedef typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Off Elf_Off; \
  68. typedef typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Half Elf_Half; \
  69. typedef typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Word Elf_Word; \
  70. typedef \
  71. typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Sword Elf_Sword; \
  72. typedef \
  73. typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Xword Elf_Xword; \
  74. typedef \
  75. typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Sxword Elf_Sxword;
  76. #define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) \
  77. LLVM_ELF_IMPORT_TYPES(ELFT::TargetEndianness, ELFT::Is64Bits)
  78. // Section header.
  79. template <class ELFT> struct Elf_Shdr_Base;
  80. template <endianness TargetEndianness>
  81. struct Elf_Shdr_Base<ELFType<TargetEndianness, false>> {
  82. LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
  83. Elf_Word sh_name; // Section name (index into string table)
  84. Elf_Word sh_type; // Section type (SHT_*)
  85. Elf_Word sh_flags; // Section flags (SHF_*)
  86. Elf_Addr sh_addr; // Address where section is to be loaded
  87. Elf_Off sh_offset; // File offset of section data, in bytes
  88. Elf_Word sh_size; // Size of section, in bytes
  89. Elf_Word sh_link; // Section type-specific header table index link
  90. Elf_Word sh_info; // Section type-specific extra information
  91. Elf_Word sh_addralign; // Section address alignment
  92. Elf_Word sh_entsize; // Size of records contained within the section
  93. };
  94. template <endianness TargetEndianness>
  95. struct Elf_Shdr_Base<ELFType<TargetEndianness, true>> {
  96. LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
  97. Elf_Word sh_name; // Section name (index into string table)
  98. Elf_Word sh_type; // Section type (SHT_*)
  99. Elf_Xword sh_flags; // Section flags (SHF_*)
  100. Elf_Addr sh_addr; // Address where section is to be loaded
  101. Elf_Off sh_offset; // File offset of section data, in bytes
  102. Elf_Xword sh_size; // Size of section, in bytes
  103. Elf_Word sh_link; // Section type-specific header table index link
  104. Elf_Word sh_info; // Section type-specific extra information
  105. Elf_Xword sh_addralign; // Section address alignment
  106. Elf_Xword sh_entsize; // Size of records contained within the section
  107. };
  108. template <class ELFT>
  109. struct Elf_Shdr_Impl : Elf_Shdr_Base<ELFT> {
  110. using Elf_Shdr_Base<ELFT>::sh_entsize;
  111. using Elf_Shdr_Base<ELFT>::sh_size;
  112. /// @brief Get the number of entities this section contains if it has any.
  113. unsigned getEntityCount() const {
  114. if (sh_entsize == 0)
  115. return 0;
  116. return sh_size / sh_entsize;
  117. }
  118. };
  119. template <class ELFT> struct Elf_Sym_Base;
  120. template <endianness TargetEndianness>
  121. struct Elf_Sym_Base<ELFType<TargetEndianness, false>> {
  122. LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
  123. Elf_Word st_name; // Symbol name (index into string table)
  124. Elf_Addr st_value; // Value or address associated with the symbol
  125. Elf_Word st_size; // Size of the symbol
  126. unsigned char st_info; // Symbol's type and binding attributes
  127. unsigned char st_other; // Must be zero; reserved
  128. Elf_Half st_shndx; // Which section (header table index) it's defined in
  129. };
  130. template <endianness TargetEndianness>
  131. struct Elf_Sym_Base<ELFType<TargetEndianness, true>> {
  132. LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
  133. Elf_Word st_name; // Symbol name (index into string table)
  134. unsigned char st_info; // Symbol's type and binding attributes
  135. unsigned char st_other; // Must be zero; reserved
  136. Elf_Half st_shndx; // Which section (header table index) it's defined in
  137. Elf_Addr st_value; // Value or address associated with the symbol
  138. Elf_Xword st_size; // Size of the symbol
  139. };
  140. template <class ELFT>
  141. struct Elf_Sym_Impl : Elf_Sym_Base<ELFT> {
  142. using Elf_Sym_Base<ELFT>::st_info;
  143. using Elf_Sym_Base<ELFT>::st_shndx;
  144. using Elf_Sym_Base<ELFT>::st_other;
  145. using Elf_Sym_Base<ELFT>::st_value;
  146. // These accessors and mutators correspond to the ELF32_ST_BIND,
  147. // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
  148. unsigned char getBinding() const { return st_info >> 4; }
  149. unsigned char getType() const { return st_info & 0x0f; }
  150. uint64_t getValue() const { return st_value; }
  151. void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
  152. void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
  153. void setBindingAndType(unsigned char b, unsigned char t) {
  154. st_info = (b << 4) + (t & 0x0f);
  155. }
  156. /// Access to the STV_xxx flag stored in the first two bits of st_other.
  157. /// STV_DEFAULT: 0
  158. /// STV_INTERNAL: 1
  159. /// STV_HIDDEN: 2
  160. /// STV_PROTECTED: 3
  161. unsigned char getVisibility() const { return st_other & 0x3; }
  162. void setVisibility(unsigned char v) {
  163. assert(v < 4 && "Invalid value for visibility");
  164. st_other = (st_other & ~0x3) | v;
  165. }
  166. bool isAbsolute() const { return st_shndx == ELF::SHN_ABS; }
  167. bool isCommon() const {
  168. return getType() == ELF::STT_COMMON || st_shndx == ELF::SHN_COMMON;
  169. }
  170. bool isDefined() const { return !isUndefined(); }
  171. bool isProcessorSpecific() const {
  172. return st_shndx >= ELF::SHN_LOPROC && st_shndx <= ELF::SHN_HIPROC;
  173. }
  174. bool isOSSpecific() const {
  175. return st_shndx >= ELF::SHN_LOOS && st_shndx <= ELF::SHN_HIOS;
  176. }
  177. bool isReserved() const {
  178. // ELF::SHN_HIRESERVE is 0xffff so st_shndx <= ELF::SHN_HIRESERVE is always
  179. // true and some compilers warn about it.
  180. return st_shndx >= ELF::SHN_LORESERVE;
  181. }
  182. bool isUndefined() const { return st_shndx == ELF::SHN_UNDEF; }
  183. bool isExternal() const {
  184. return getBinding() != ELF::STB_LOCAL;
  185. }
  186. ErrorOr<StringRef> getName(StringRef StrTab) const;
  187. };
  188. template <class ELFT>
  189. ErrorOr<StringRef> Elf_Sym_Impl<ELFT>::getName(StringRef StrTab) const {
  190. uint32_t Offset = this->st_name;
  191. if (Offset >= StrTab.size())
  192. return object_error::parse_failed;
  193. return StringRef(StrTab.data() + Offset);
  194. }
  195. /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
  196. /// (.gnu.version). This structure is identical for ELF32 and ELF64.
  197. template <class ELFT>
  198. struct Elf_Versym_Impl {
  199. LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  200. Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
  201. };
  202. template <class ELFT> struct Elf_Verdaux_Impl;
  203. /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
  204. /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
  205. template <class ELFT>
  206. struct Elf_Verdef_Impl {
  207. LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  208. typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux;
  209. Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
  210. Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
  211. Elf_Half vd_ndx; // Version index, used in .gnu.version entries
  212. Elf_Half vd_cnt; // Number of Verdaux entries
  213. Elf_Word vd_hash; // Hash of name
  214. Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
  215. Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
  216. /// Get the first Verdaux entry for this Verdef.
  217. const Elf_Verdaux *getAux() const {
  218. return reinterpret_cast<const Elf_Verdaux *>((const char *)this + vd_aux);
  219. }
  220. };
  221. /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
  222. /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
  223. template <class ELFT>
  224. struct Elf_Verdaux_Impl {
  225. LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  226. Elf_Word vda_name; // Version name (offset in string table)
  227. Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
  228. };
  229. /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
  230. /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
  231. template <class ELFT>
  232. struct Elf_Verneed_Impl {
  233. LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  234. Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
  235. Elf_Half vn_cnt; // Number of associated Vernaux entries
  236. Elf_Word vn_file; // Library name (string table offset)
  237. Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
  238. Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
  239. };
  240. /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
  241. /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
  242. template <class ELFT>
  243. struct Elf_Vernaux_Impl {
  244. LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  245. Elf_Word vna_hash; // Hash of dependency name
  246. Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
  247. Elf_Half vna_other; // Version index, used in .gnu.version entries
  248. Elf_Word vna_name; // Dependency name
  249. Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
  250. };
  251. /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
  252. /// table section (.dynamic) look like.
  253. template <class ELFT> struct Elf_Dyn_Base;
  254. template <endianness TargetEndianness>
  255. struct Elf_Dyn_Base<ELFType<TargetEndianness, false>> {
  256. LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
  257. Elf_Sword d_tag;
  258. union {
  259. Elf_Word d_val;
  260. Elf_Addr d_ptr;
  261. } d_un;
  262. };
  263. template <endianness TargetEndianness>
  264. struct Elf_Dyn_Base<ELFType<TargetEndianness, true>> {
  265. LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
  266. Elf_Sxword d_tag;
  267. union {
  268. Elf_Xword d_val;
  269. Elf_Addr d_ptr;
  270. } d_un;
  271. };
  272. /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
  273. template <class ELFT>
  274. struct Elf_Dyn_Impl : Elf_Dyn_Base<ELFT> {
  275. using Elf_Dyn_Base<ELFT>::d_tag;
  276. using Elf_Dyn_Base<ELFT>::d_un;
  277. int64_t getTag() const { return d_tag; }
  278. uint64_t getVal() const { return d_un.d_val; }
  279. uint64_t getPtr() const { return d_un.d_ptr; }
  280. };
  281. // Elf_Rel: Elf Relocation
  282. template <class ELFT, bool isRela> struct Elf_Rel_Impl;
  283. template <endianness TargetEndianness>
  284. struct Elf_Rel_Impl<ELFType<TargetEndianness, false>, false> {
  285. LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
  286. Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
  287. Elf_Word r_info; // Symbol table index and type of relocation to apply
  288. uint32_t getRInfo(bool isMips64EL) const {
  289. assert(!isMips64EL);
  290. return r_info;
  291. }
  292. void setRInfo(uint32_t R, bool IsMips64EL) {
  293. assert(!IsMips64EL);
  294. r_info = R;
  295. }
  296. // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
  297. // and ELF32_R_INFO macros defined in the ELF specification:
  298. uint32_t getSymbol(bool isMips64EL) const {
  299. return this->getRInfo(isMips64EL) >> 8;
  300. }
  301. unsigned char getType(bool isMips64EL) const {
  302. return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff);
  303. }
  304. void setSymbol(uint32_t s, bool IsMips64EL) {
  305. setSymbolAndType(s, getType(), IsMips64EL);
  306. }
  307. void setType(unsigned char t, bool IsMips64EL) {
  308. setSymbolAndType(getSymbol(), t, IsMips64EL);
  309. }
  310. void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL) {
  311. this->setRInfo((s << 8) + t, IsMips64EL);
  312. }
  313. };
  314. template <endianness TargetEndianness>
  315. struct Elf_Rel_Impl<ELFType<TargetEndianness, false>, true>
  316. : public Elf_Rel_Impl<ELFType<TargetEndianness, false>, false> {
  317. LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
  318. Elf_Sword r_addend; // Compute value for relocatable field by adding this
  319. };
  320. template <endianness TargetEndianness>
  321. struct Elf_Rel_Impl<ELFType<TargetEndianness, true>, false> {
  322. LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
  323. Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
  324. Elf_Xword r_info; // Symbol table index and type of relocation to apply
  325. uint64_t getRInfo(bool isMips64EL) const {
  326. uint64_t t = r_info;
  327. if (!isMips64EL)
  328. return t;
  329. // Mips64 little endian has a "special" encoding of r_info. Instead of one
  330. // 64 bit little endian number, it is a little endian 32 bit number followed
  331. // by a 32 bit big endian number.
  332. return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
  333. ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
  334. }
  335. void setRInfo(uint64_t R, bool IsMips64EL) {
  336. if (IsMips64EL)
  337. r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) |
  338. ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56);
  339. else
  340. r_info = R;
  341. }
  342. // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
  343. // and ELF64_R_INFO macros defined in the ELF specification:
  344. uint32_t getSymbol(bool isMips64EL) const {
  345. return (uint32_t)(this->getRInfo(isMips64EL) >> 32);
  346. }
  347. uint32_t getType(bool isMips64EL) const {
  348. return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL);
  349. }
  350. void setSymbol(uint32_t s, bool IsMips64EL) {
  351. setSymbolAndType(s, getType(), IsMips64EL);
  352. }
  353. void setType(uint32_t t, bool IsMips64EL) {
  354. setSymbolAndType(getSymbol(), t, IsMips64EL);
  355. }
  356. void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL) {
  357. this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL), IsMips64EL);
  358. }
  359. };
  360. template <endianness TargetEndianness>
  361. struct Elf_Rel_Impl<ELFType<TargetEndianness, true>, true>
  362. : public Elf_Rel_Impl<ELFType<TargetEndianness, true>, false> {
  363. LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
  364. Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
  365. };
  366. template <class ELFT>
  367. struct Elf_Ehdr_Impl {
  368. LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  369. unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
  370. Elf_Half e_type; // Type of file (see ET_*)
  371. Elf_Half e_machine; // Required architecture for this file (see EM_*)
  372. Elf_Word e_version; // Must be equal to 1
  373. Elf_Addr e_entry; // Address to jump to in order to start program
  374. Elf_Off e_phoff; // Program header table's file offset, in bytes
  375. Elf_Off e_shoff; // Section header table's file offset, in bytes
  376. Elf_Word e_flags; // Processor-specific flags
  377. Elf_Half e_ehsize; // Size of ELF header, in bytes
  378. Elf_Half e_phentsize; // Size of an entry in the program header table
  379. Elf_Half e_phnum; // Number of entries in the program header table
  380. Elf_Half e_shentsize; // Size of an entry in the section header table
  381. Elf_Half e_shnum; // Number of entries in the section header table
  382. Elf_Half e_shstrndx; // Section header table index of section name
  383. // string table
  384. bool checkMagic() const {
  385. return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
  386. }
  387. unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
  388. unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
  389. };
  390. template <class ELFT> struct Elf_Phdr_Impl;
  391. template <endianness TargetEndianness>
  392. struct Elf_Phdr_Impl<ELFType<TargetEndianness, false>> {
  393. LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
  394. Elf_Word p_type; // Type of segment
  395. Elf_Off p_offset; // FileOffset where segment is located, in bytes
  396. Elf_Addr p_vaddr; // Virtual Address of beginning of segment
  397. Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
  398. Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
  399. Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
  400. Elf_Word p_flags; // Segment flags
  401. Elf_Word p_align; // Segment alignment constraint
  402. };
  403. template <endianness TargetEndianness>
  404. struct Elf_Phdr_Impl<ELFType<TargetEndianness, true>> {
  405. LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
  406. Elf_Word p_type; // Type of segment
  407. Elf_Word p_flags; // Segment flags
  408. Elf_Off p_offset; // FileOffset where segment is located, in bytes
  409. Elf_Addr p_vaddr; // Virtual Address of beginning of segment
  410. Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
  411. Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
  412. Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero)
  413. Elf_Xword p_align; // Segment alignment constraint
  414. };
  415. // ELFT needed for endianess.
  416. template <class ELFT>
  417. struct Elf_Hash_Impl {
  418. LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  419. Elf_Word nbucket;
  420. Elf_Word nchain;
  421. ArrayRef<Elf_Word> buckets() const {
  422. return ArrayRef<Elf_Word>(&nbucket + 2, &nbucket + 2 + nbucket);
  423. }
  424. ArrayRef<Elf_Word> chains() const {
  425. return ArrayRef<Elf_Word>(&nbucket + 2 + nbucket,
  426. &nbucket + 2 + nbucket + nchain);
  427. }
  428. };
  429. // MIPS .reginfo section
  430. template <class ELFT>
  431. struct Elf_Mips_RegInfo;
  432. template <llvm::support::endianness TargetEndianness>
  433. struct Elf_Mips_RegInfo<ELFType<TargetEndianness, false>> {
  434. LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
  435. Elf_Word ri_gprmask; // bit-mask of used general registers
  436. Elf_Word ri_cprmask[4]; // bit-mask of used co-processor registers
  437. Elf_Addr ri_gp_value; // gp register value
  438. };
  439. template <llvm::support::endianness TargetEndianness>
  440. struct Elf_Mips_RegInfo<ELFType<TargetEndianness, true>> {
  441. LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
  442. Elf_Word ri_gprmask; // bit-mask of used general registers
  443. Elf_Word ri_pad; // unused padding field
  444. Elf_Word ri_cprmask[4]; // bit-mask of used co-processor registers
  445. Elf_Addr ri_gp_value; // gp register value
  446. };
  447. // .MIPS.options section
  448. template <class ELFT> struct Elf_Mips_Options {
  449. LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  450. uint8_t kind; // Determines interpretation of variable part of descriptor
  451. uint8_t size; // Byte size of descriptor, including this header
  452. Elf_Half section; // Section header index of section affected,
  453. // or 0 for global options
  454. Elf_Word info; // Kind-specific information
  455. const Elf_Mips_RegInfo<ELFT> &getRegInfo() const {
  456. assert(kind == llvm::ELF::ODK_REGINFO);
  457. return *reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(
  458. (const uint8_t *)this + sizeof(Elf_Mips_Options));
  459. }
  460. };
  461. // .MIPS.abiflags section content
  462. template <class ELFT> struct Elf_Mips_ABIFlags {
  463. LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  464. Elf_Half version; // Version of the structure
  465. uint8_t isa_level; // ISA level: 1-5, 32, and 64
  466. uint8_t isa_rev; // ISA revision (0 for MIPS I - MIPS V)
  467. uint8_t gpr_size; // General purpose registers size
  468. uint8_t cpr1_size; // Co-processor 1 registers size
  469. uint8_t cpr2_size; // Co-processor 2 registers size
  470. uint8_t fp_abi; // Floating-point ABI flag
  471. Elf_Word isa_ext; // Processor-specific extension
  472. Elf_Word ases; // ASEs flags
  473. Elf_Word flags1; // General flags
  474. Elf_Word flags2; // General flags
  475. };
  476. } // end namespace object.
  477. } // end namespace llvm.
  478. #endif