• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #ifndef LLVM_OBJECT_ELFTYPES_H
11 #define LLVM_OBJECT_ELFTYPES_H
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/Object/Error.h"
15 #include "llvm/Support/ELF.h"
16 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/ErrorOr.h"
18 
19 namespace llvm {
20 namespace object {
21 
22 using support::endianness;
23 
24 template <class ELFT> struct Elf_Ehdr_Impl;
25 template <class ELFT> struct Elf_Shdr_Impl;
26 template <class ELFT> struct Elf_Sym_Impl;
27 template <class ELFT> struct Elf_Dyn_Impl;
28 template <class ELFT> struct Elf_Phdr_Impl;
29 template <class ELFT, bool isRela> struct Elf_Rel_Impl;
30 template <class ELFT> struct Elf_Verdef_Impl;
31 template <class ELFT> struct Elf_Verdaux_Impl;
32 template <class ELFT> struct Elf_Verneed_Impl;
33 template <class ELFT> struct Elf_Vernaux_Impl;
34 template <class ELFT> struct Elf_Versym_Impl;
35 template <class ELFT> struct Elf_Hash_Impl;
36 template <class ELFT> struct Elf_GnuHash_Impl;
37 template <class ELFT> struct Elf_Chdr_Impl;
38 
39 template <endianness E, bool Is64> struct ELFType {
40 private:
41   template <typename Ty>
42   using packed = support::detail::packed_endian_specific_integral<Ty, E, 2>;
43 
44 public:
45   static const endianness TargetEndianness = E;
46   static const bool Is64Bits = Is64;
47 
48   typedef typename std::conditional<Is64, uint64_t, uint32_t>::type uint;
49   typedef Elf_Ehdr_Impl<ELFType<E, Is64>> Ehdr;
50   typedef Elf_Shdr_Impl<ELFType<E, Is64>> Shdr;
51   typedef Elf_Sym_Impl<ELFType<E, Is64>> Sym;
52   typedef Elf_Dyn_Impl<ELFType<E, Is64>> Dyn;
53   typedef Elf_Phdr_Impl<ELFType<E, Is64>> Phdr;
54   typedef Elf_Rel_Impl<ELFType<E, Is64>, false> Rel;
55   typedef Elf_Rel_Impl<ELFType<E, Is64>, true> Rela;
56   typedef Elf_Verdef_Impl<ELFType<E, Is64>> Verdef;
57   typedef Elf_Verdaux_Impl<ELFType<E, Is64>> Verdaux;
58   typedef Elf_Verneed_Impl<ELFType<E, Is64>> Verneed;
59   typedef Elf_Vernaux_Impl<ELFType<E, Is64>> Vernaux;
60   typedef Elf_Versym_Impl<ELFType<E, Is64>> Versym;
61   typedef Elf_Hash_Impl<ELFType<E, Is64>> Hash;
62   typedef Elf_GnuHash_Impl<ELFType<E, Is64>> GnuHash;
63   typedef Elf_Chdr_Impl<ELFType<E, Is64>> Chdr;
64   typedef ArrayRef<Dyn> DynRange;
65   typedef ArrayRef<Shdr> ShdrRange;
66   typedef ArrayRef<Sym> SymRange;
67   typedef ArrayRef<Rel> RelRange;
68   typedef ArrayRef<Rela> RelaRange;
69   typedef ArrayRef<Phdr> PhdrRange;
70 
71   typedef packed<uint16_t> Half;
72   typedef packed<uint32_t> Word;
73   typedef packed<int32_t> Sword;
74   typedef packed<uint64_t> Xword;
75   typedef packed<int64_t> Sxword;
76   typedef packed<uint> Addr;
77   typedef packed<uint> Off;
78 };
79 
80 typedef ELFType<support::little, false> ELF32LE;
81 typedef ELFType<support::big, false> ELF32BE;
82 typedef ELFType<support::little, true> ELF64LE;
83 typedef ELFType<support::big, true> ELF64BE;
84 
85 // Use an alignment of 2 for the typedefs since that is the worst case for
86 // ELF files in archives.
87 
88 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
89 template <endianness target_endianness> struct ELFDataTypeTypedefHelperCommon {
90   typedef support::detail::packed_endian_specific_integral<
91       uint16_t, target_endianness, 2> Elf_Half;
92   typedef support::detail::packed_endian_specific_integral<
93       uint32_t, target_endianness, 2> Elf_Word;
94   typedef support::detail::packed_endian_specific_integral<
95       int32_t, target_endianness, 2> Elf_Sword;
96   typedef support::detail::packed_endian_specific_integral<
97       uint64_t, target_endianness, 2> Elf_Xword;
98   typedef support::detail::packed_endian_specific_integral<
99       int64_t, target_endianness, 2> Elf_Sxword;
100 };
101 
102 template <class ELFT> struct ELFDataTypeTypedefHelper;
103 
104 /// ELF 32bit types.
105 template <endianness TargetEndianness>
106 struct ELFDataTypeTypedefHelper<ELFType<TargetEndianness, false>>
107     : ELFDataTypeTypedefHelperCommon<TargetEndianness> {
108   typedef uint32_t value_type;
109   typedef support::detail::packed_endian_specific_integral<
110       value_type, TargetEndianness, 2> Elf_Addr;
111   typedef support::detail::packed_endian_specific_integral<
112       value_type, TargetEndianness, 2> Elf_Off;
113 };
114 
115 /// ELF 64bit types.
116 template <endianness TargetEndianness>
117 struct ELFDataTypeTypedefHelper<ELFType<TargetEndianness, true>>
118     : ELFDataTypeTypedefHelperCommon<TargetEndianness> {
119   typedef uint64_t value_type;
120   typedef support::detail::packed_endian_specific_integral<
121       value_type, TargetEndianness, 2> Elf_Addr;
122   typedef support::detail::packed_endian_specific_integral<
123       value_type, TargetEndianness, 2> Elf_Off;
124 };
125 
126 // I really don't like doing this, but the alternative is copypasta.
127 #define LLVM_ELF_IMPORT_TYPES(E, W)                                            \
128   typedef typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Addr Elf_Addr; \
129   typedef typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Off Elf_Off;   \
130   typedef typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Half Elf_Half; \
131   typedef typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Word Elf_Word; \
132   typedef                                                                      \
133       typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Sword Elf_Sword;   \
134   typedef                                                                      \
135       typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Xword Elf_Xword;   \
136   typedef                                                                      \
137       typename ELFDataTypeTypedefHelper<ELFType<E, W>>::Elf_Sxword Elf_Sxword;
138 
139 #define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)                                       \
140   LLVM_ELF_IMPORT_TYPES(ELFT::TargetEndianness, ELFT::Is64Bits)
141 
142 // Section header.
143 template <class ELFT> struct Elf_Shdr_Base;
144 
145 template <endianness TargetEndianness>
146 struct Elf_Shdr_Base<ELFType<TargetEndianness, false>> {
147   LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
148   Elf_Word sh_name;      // Section name (index into string table)
149   Elf_Word sh_type;      // Section type (SHT_*)
150   Elf_Word sh_flags;     // Section flags (SHF_*)
151   Elf_Addr sh_addr;      // Address where section is to be loaded
152   Elf_Off sh_offset;     // File offset of section data, in bytes
153   Elf_Word sh_size;      // Size of section, in bytes
154   Elf_Word sh_link;      // Section type-specific header table index link
155   Elf_Word sh_info;      // Section type-specific extra information
156   Elf_Word sh_addralign; // Section address alignment
157   Elf_Word sh_entsize;   // Size of records contained within the section
158 };
159 
160 template <endianness TargetEndianness>
161 struct Elf_Shdr_Base<ELFType<TargetEndianness, true>> {
162   LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
163   Elf_Word sh_name;       // Section name (index into string table)
164   Elf_Word sh_type;       // Section type (SHT_*)
165   Elf_Xword sh_flags;     // Section flags (SHF_*)
166   Elf_Addr sh_addr;       // Address where section is to be loaded
167   Elf_Off sh_offset;      // File offset of section data, in bytes
168   Elf_Xword sh_size;      // Size of section, in bytes
169   Elf_Word sh_link;       // Section type-specific header table index link
170   Elf_Word sh_info;       // Section type-specific extra information
171   Elf_Xword sh_addralign; // Section address alignment
172   Elf_Xword sh_entsize;   // Size of records contained within the section
173 };
174 
175 template <class ELFT>
176 struct Elf_Shdr_Impl : Elf_Shdr_Base<ELFT> {
177   using Elf_Shdr_Base<ELFT>::sh_entsize;
178   using Elf_Shdr_Base<ELFT>::sh_size;
179 
180   /// @brief Get the number of entities this section contains if it has any.
181   unsigned getEntityCount() const {
182     if (sh_entsize == 0)
183       return 0;
184     return sh_size / sh_entsize;
185   }
186 };
187 
188 template <class ELFT> struct Elf_Sym_Base;
189 
190 template <endianness TargetEndianness>
191 struct Elf_Sym_Base<ELFType<TargetEndianness, false>> {
192   LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
193   Elf_Word st_name;       // Symbol name (index into string table)
194   Elf_Addr st_value;      // Value or address associated with the symbol
195   Elf_Word st_size;       // Size of the symbol
196   unsigned char st_info;  // Symbol's type and binding attributes
197   unsigned char st_other; // Must be zero; reserved
198   Elf_Half st_shndx;      // Which section (header table index) it's defined in
199 };
200 
201 template <endianness TargetEndianness>
202 struct Elf_Sym_Base<ELFType<TargetEndianness, true>> {
203   LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
204   Elf_Word st_name;       // Symbol name (index into string table)
205   unsigned char st_info;  // Symbol's type and binding attributes
206   unsigned char st_other; // Must be zero; reserved
207   Elf_Half st_shndx;      // Which section (header table index) it's defined in
208   Elf_Addr st_value;      // Value or address associated with the symbol
209   Elf_Xword st_size;      // Size of the symbol
210 };
211 
212 template <class ELFT>
213 struct Elf_Sym_Impl : Elf_Sym_Base<ELFT> {
214   using Elf_Sym_Base<ELFT>::st_info;
215   using Elf_Sym_Base<ELFT>::st_shndx;
216   using Elf_Sym_Base<ELFT>::st_other;
217   using Elf_Sym_Base<ELFT>::st_value;
218 
219   // These accessors and mutators correspond to the ELF32_ST_BIND,
220   // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
221   unsigned char getBinding() const { return st_info >> 4; }
222   unsigned char getType() const { return st_info & 0x0f; }
223   uint64_t getValue() const { return st_value; }
224   void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
225   void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
226   void setBindingAndType(unsigned char b, unsigned char t) {
227     st_info = (b << 4) + (t & 0x0f);
228   }
229 
230   /// Access to the STV_xxx flag stored in the first two bits of st_other.
231   /// STV_DEFAULT: 0
232   /// STV_INTERNAL: 1
233   /// STV_HIDDEN: 2
234   /// STV_PROTECTED: 3
235   unsigned char getVisibility() const { return st_other & 0x3; }
236   void setVisibility(unsigned char v) {
237     assert(v < 4 && "Invalid value for visibility");
238     st_other = (st_other & ~0x3) | v;
239   }
240 
241   bool isAbsolute() const { return st_shndx == ELF::SHN_ABS; }
242   bool isCommon() const {
243     return getType() == ELF::STT_COMMON || st_shndx == ELF::SHN_COMMON;
244   }
245   bool isDefined() const { return !isUndefined(); }
246   bool isProcessorSpecific() const {
247     return st_shndx >= ELF::SHN_LOPROC && st_shndx <= ELF::SHN_HIPROC;
248   }
249   bool isOSSpecific() const {
250     return st_shndx >= ELF::SHN_LOOS && st_shndx <= ELF::SHN_HIOS;
251   }
252   bool isReserved() const {
253     // ELF::SHN_HIRESERVE is 0xffff so st_shndx <= ELF::SHN_HIRESERVE is always
254     // true and some compilers warn about it.
255     return st_shndx >= ELF::SHN_LORESERVE;
256   }
257   bool isUndefined() const { return st_shndx == ELF::SHN_UNDEF; }
258   bool isExternal() const {
259     return getBinding() != ELF::STB_LOCAL;
260   }
261 
262   Expected<StringRef> getName(StringRef StrTab) const;
263 };
264 
265 template <class ELFT>
266 Expected<StringRef> Elf_Sym_Impl<ELFT>::getName(StringRef StrTab) const {
267   uint32_t Offset = this->st_name;
268   if (Offset >= StrTab.size())
269     return errorCodeToError(object_error::parse_failed);
270   return StringRef(StrTab.data() + Offset);
271 }
272 
273 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
274 /// (.gnu.version). This structure is identical for ELF32 and ELF64.
275 template <class ELFT>
276 struct Elf_Versym_Impl {
277   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
278   Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
279 };
280 
281 template <class ELFT> struct Elf_Verdaux_Impl;
282 
283 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
284 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
285 template <class ELFT>
286 struct Elf_Verdef_Impl {
287   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
288   typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux;
289   Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
290   Elf_Half vd_flags;   // Bitwise flags (VER_DEF_*)
291   Elf_Half vd_ndx;     // Version index, used in .gnu.version entries
292   Elf_Half vd_cnt;     // Number of Verdaux entries
293   Elf_Word vd_hash;    // Hash of name
294   Elf_Word vd_aux;     // Offset to the first Verdaux entry (in bytes)
295   Elf_Word vd_next;    // Offset to the next Verdef entry (in bytes)
296 
297   /// Get the first Verdaux entry for this Verdef.
298   const Elf_Verdaux *getAux() const {
299     return reinterpret_cast<const Elf_Verdaux *>((const char *)this + vd_aux);
300   }
301 };
302 
303 /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
304 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
305 template <class ELFT>
306 struct Elf_Verdaux_Impl {
307   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
308   Elf_Word vda_name; // Version name (offset in string table)
309   Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
310 };
311 
312 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
313 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
314 template <class ELFT>
315 struct Elf_Verneed_Impl {
316   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
317   Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
318   Elf_Half vn_cnt;     // Number of associated Vernaux entries
319   Elf_Word vn_file;    // Library name (string table offset)
320   Elf_Word vn_aux;     // Offset to first Vernaux entry (in bytes)
321   Elf_Word vn_next;    // Offset to next Verneed entry (in bytes)
322 };
323 
324 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
325 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
326 template <class ELFT>
327 struct Elf_Vernaux_Impl {
328   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
329   Elf_Word vna_hash;  // Hash of dependency name
330   Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
331   Elf_Half vna_other; // Version index, used in .gnu.version entries
332   Elf_Word vna_name;  // Dependency name
333   Elf_Word vna_next;  // Offset to next Vernaux entry (in bytes)
334 };
335 
336 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
337 ///               table section (.dynamic) look like.
338 template <class ELFT> struct Elf_Dyn_Base;
339 
340 template <endianness TargetEndianness>
341 struct Elf_Dyn_Base<ELFType<TargetEndianness, false>> {
342   LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
343   Elf_Sword d_tag;
344   union {
345     Elf_Word d_val;
346     Elf_Addr d_ptr;
347   } d_un;
348 };
349 
350 template <endianness TargetEndianness>
351 struct Elf_Dyn_Base<ELFType<TargetEndianness, true>> {
352   LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
353   Elf_Sxword d_tag;
354   union {
355     Elf_Xword d_val;
356     Elf_Addr d_ptr;
357   } d_un;
358 };
359 
360 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters.
361 template <class ELFT>
362 struct Elf_Dyn_Impl : Elf_Dyn_Base<ELFT> {
363   using Elf_Dyn_Base<ELFT>::d_tag;
364   using Elf_Dyn_Base<ELFT>::d_un;
365   typedef typename std::conditional<ELFT::Is64Bits,
366                                     int64_t, int32_t>::type intX_t;
367   typedef typename std::conditional<ELFT::Is64Bits,
368                                     uint64_t, uint32_t>::type uintX_t;
369   intX_t getTag() const { return d_tag; }
370   uintX_t getVal() const { return d_un.d_val; }
371   uintX_t getPtr() const { return d_un.d_ptr; }
372 };
373 
374 template <endianness TargetEndianness>
375 struct Elf_Rel_Impl<ELFType<TargetEndianness, false>, false> {
376   LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
377   static const bool IsRela = false;
378   Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
379   Elf_Word r_info;   // Symbol table index and type of relocation to apply
380 
381   uint32_t getRInfo(bool isMips64EL) const {
382     assert(!isMips64EL);
383     return r_info;
384   }
385   void setRInfo(uint32_t R, bool IsMips64EL) {
386     assert(!IsMips64EL);
387     r_info = R;
388   }
389 
390   // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
391   // and ELF32_R_INFO macros defined in the ELF specification:
392   uint32_t getSymbol(bool isMips64EL) const {
393     return this->getRInfo(isMips64EL) >> 8;
394   }
395   unsigned char getType(bool isMips64EL) const {
396     return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff);
397   }
398   void setSymbol(uint32_t s, bool IsMips64EL) {
399     setSymbolAndType(s, getType(), IsMips64EL);
400   }
401   void setType(unsigned char t, bool IsMips64EL) {
402     setSymbolAndType(getSymbol(), t, IsMips64EL);
403   }
404   void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL) {
405     this->setRInfo((s << 8) + t, IsMips64EL);
406   }
407 };
408 
409 template <endianness TargetEndianness>
410 struct Elf_Rel_Impl<ELFType<TargetEndianness, false>, true>
411     : public Elf_Rel_Impl<ELFType<TargetEndianness, false>, false> {
412   LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
413   static const bool IsRela = true;
414   Elf_Sword r_addend; // Compute value for relocatable field by adding this
415 };
416 
417 template <endianness TargetEndianness>
418 struct Elf_Rel_Impl<ELFType<TargetEndianness, true>, false> {
419   LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
420   static const bool IsRela = false;
421   Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
422   Elf_Xword r_info;  // Symbol table index and type of relocation to apply
423 
424   uint64_t getRInfo(bool isMips64EL) const {
425     uint64_t t = r_info;
426     if (!isMips64EL)
427       return t;
428     // Mips64 little endian has a "special" encoding of r_info. Instead of one
429     // 64 bit little endian number, it is a little endian 32 bit number followed
430     // by a 32 bit big endian number.
431     return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
432            ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
433   }
434   void setRInfo(uint64_t R, bool IsMips64EL) {
435     if (IsMips64EL)
436       r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) |
437                ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56);
438     else
439       r_info = R;
440   }
441 
442   // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
443   // and ELF64_R_INFO macros defined in the ELF specification:
444   uint32_t getSymbol(bool isMips64EL) const {
445     return (uint32_t)(this->getRInfo(isMips64EL) >> 32);
446   }
447   uint32_t getType(bool isMips64EL) const {
448     return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL);
449   }
450   void setSymbol(uint32_t s, bool IsMips64EL) {
451     setSymbolAndType(s, getType(), IsMips64EL);
452   }
453   void setType(uint32_t t, bool IsMips64EL) {
454     setSymbolAndType(getSymbol(), t, IsMips64EL);
455   }
456   void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL) {
457     this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL), IsMips64EL);
458   }
459 };
460 
461 template <endianness TargetEndianness>
462 struct Elf_Rel_Impl<ELFType<TargetEndianness, true>, true>
463     : public Elf_Rel_Impl<ELFType<TargetEndianness, true>, false> {
464   LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
465   static const bool IsRela = true;
466   Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
467 };
468 
469 template <class ELFT>
470 struct Elf_Ehdr_Impl {
471   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
472   unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
473   Elf_Half e_type;                       // Type of file (see ET_*)
474   Elf_Half e_machine;   // Required architecture for this file (see EM_*)
475   Elf_Word e_version;   // Must be equal to 1
476   Elf_Addr e_entry;     // Address to jump to in order to start program
477   Elf_Off e_phoff;      // Program header table's file offset, in bytes
478   Elf_Off e_shoff;      // Section header table's file offset, in bytes
479   Elf_Word e_flags;     // Processor-specific flags
480   Elf_Half e_ehsize;    // Size of ELF header, in bytes
481   Elf_Half e_phentsize; // Size of an entry in the program header table
482   Elf_Half e_phnum;     // Number of entries in the program header table
483   Elf_Half e_shentsize; // Size of an entry in the section header table
484   Elf_Half e_shnum;     // Number of entries in the section header table
485   Elf_Half e_shstrndx;  // Section header table index of section name
486                         // string table
487   bool checkMagic() const {
488     return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
489   }
490   unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
491   unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
492 };
493 
494 template <class ELFT> struct Elf_Phdr_Impl;
495 
496 template <endianness TargetEndianness>
497 struct Elf_Phdr_Impl<ELFType<TargetEndianness, false>> {
498   LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
499   Elf_Word p_type;   // Type of segment
500   Elf_Off p_offset;  // FileOffset where segment is located, in bytes
501   Elf_Addr p_vaddr;  // Virtual Address of beginning of segment
502   Elf_Addr p_paddr;  // Physical address of beginning of segment (OS-specific)
503   Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
504   Elf_Word p_memsz;  // Num. of bytes in mem image of segment (may be zero)
505   Elf_Word p_flags;  // Segment flags
506   Elf_Word p_align;  // Segment alignment constraint
507 };
508 
509 template <endianness TargetEndianness>
510 struct Elf_Phdr_Impl<ELFType<TargetEndianness, true>> {
511   LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
512   Elf_Word p_type;    // Type of segment
513   Elf_Word p_flags;   // Segment flags
514   Elf_Off p_offset;   // FileOffset where segment is located, in bytes
515   Elf_Addr p_vaddr;   // Virtual Address of beginning of segment
516   Elf_Addr p_paddr;   // Physical address of beginning of segment (OS-specific)
517   Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
518   Elf_Xword p_memsz;  // Num. of bytes in mem image of segment (may be zero)
519   Elf_Xword p_align;  // Segment alignment constraint
520 };
521 
522 // ELFT needed for endianess.
523 template <class ELFT>
524 struct Elf_Hash_Impl {
525   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
526   Elf_Word nbucket;
527   Elf_Word nchain;
528 
529   ArrayRef<Elf_Word> buckets() const {
530     return ArrayRef<Elf_Word>(&nbucket + 2, &nbucket + 2 + nbucket);
531   }
532 
533   ArrayRef<Elf_Word> chains() const {
534     return ArrayRef<Elf_Word>(&nbucket + 2 + nbucket,
535                               &nbucket + 2 + nbucket + nchain);
536   }
537 };
538 
539 // .gnu.hash section
540 template <class ELFT>
541 struct Elf_GnuHash_Impl {
542   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
543   Elf_Word nbuckets;
544   Elf_Word symndx;
545   Elf_Word maskwords;
546   Elf_Word shift2;
547 
548   ArrayRef<Elf_Off> filter() const {
549     return ArrayRef<Elf_Off>(reinterpret_cast<const Elf_Off *>(&shift2 + 1),
550                              maskwords);
551   }
552 
553   ArrayRef<Elf_Word> buckets() const {
554     return ArrayRef<Elf_Word>(
555         reinterpret_cast<const Elf_Word *>(filter().end()), nbuckets);
556   }
557 
558   ArrayRef<Elf_Word> values(unsigned DynamicSymCount) const {
559     return ArrayRef<Elf_Word>(buckets().end(), DynamicSymCount - symndx);
560   }
561 };
562 
563 // Compressed section headers.
564 // http://www.sco.com/developers/gabi/latest/ch4.sheader.html#compression_header
565 template <endianness TargetEndianness>
566 struct Elf_Chdr_Impl<ELFType<TargetEndianness, false>> {
567   LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
568   Elf_Word ch_type;
569   Elf_Word ch_size;
570   Elf_Word ch_addralign;
571 };
572 
573 template <endianness TargetEndianness>
574 struct Elf_Chdr_Impl<ELFType<TargetEndianness, true>> {
575   LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
576   Elf_Word ch_type;
577   Elf_Word ch_reserved;
578   Elf_Xword ch_size;
579   Elf_Xword ch_addralign;
580 };
581 
582 // MIPS .reginfo section
583 template <class ELFT>
584 struct Elf_Mips_RegInfo;
585 
586 template <llvm::support::endianness TargetEndianness>
587 struct Elf_Mips_RegInfo<ELFType<TargetEndianness, false>> {
588   LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
589   Elf_Word ri_gprmask;     // bit-mask of used general registers
590   Elf_Word ri_cprmask[4];  // bit-mask of used co-processor registers
591   Elf_Addr ri_gp_value;    // gp register value
592 };
593 
594 template <llvm::support::endianness TargetEndianness>
595 struct Elf_Mips_RegInfo<ELFType<TargetEndianness, true>> {
596   LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
597   Elf_Word ri_gprmask;     // bit-mask of used general registers
598   Elf_Word ri_pad;         // unused padding field
599   Elf_Word ri_cprmask[4];  // bit-mask of used co-processor registers
600   Elf_Addr ri_gp_value;    // gp register value
601 };
602 
603 // .MIPS.options section
604 template <class ELFT> struct Elf_Mips_Options {
605   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
606   uint8_t kind;     // Determines interpretation of variable part of descriptor
607   uint8_t size;     // Byte size of descriptor, including this header
608   Elf_Half section; // Section header index of section affected,
609                     // or 0 for global options
610   Elf_Word info;    // Kind-specific information
611 
612   const Elf_Mips_RegInfo<ELFT> &getRegInfo() const {
613     assert(kind == llvm::ELF::ODK_REGINFO);
614     return *reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(
615                (const uint8_t *)this + sizeof(Elf_Mips_Options));
616   }
617 };
618 
619 // .MIPS.abiflags section content
620 template <class ELFT> struct Elf_Mips_ABIFlags {
621   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
622   Elf_Half version;  // Version of the structure
623   uint8_t isa_level; // ISA level: 1-5, 32, and 64
624   uint8_t isa_rev;   // ISA revision (0 for MIPS I - MIPS V)
625   uint8_t gpr_size;  // General purpose registers size
626   uint8_t cpr1_size; // Co-processor 1 registers size
627   uint8_t cpr2_size; // Co-processor 2 registers size
628   uint8_t fp_abi;    // Floating-point ABI flag
629   Elf_Word isa_ext;  // Processor-specific extension
630   Elf_Word ases;     // ASEs flags
631   Elf_Word flags1;   // General flags
632   Elf_Word flags2;   // General flags
633 };
634 
635 } // end namespace object.
636 } // end namespace llvm.
637 
638 #endif
639