cb013b9414bddd9019caa59fab0476bc179841ef
[oota-llvm.git] / include / llvm / Object / ELF.h
1 //===- ELF.h - ELF object file implementation -------------------*- 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 // This file declares the ELFObjectFile template class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_ELF_H
15 #define LLVM_OBJECT_ELF_H
16
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/PointerIntPair.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/ELF.h"
25 #include "llvm/Support/Endian.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include <algorithm>
30 #include <limits>
31 #include <utility>
32
33 namespace llvm {
34 namespace object {
35
36 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
37 template<support::endianness target_endianness>
38 struct ELFDataTypeTypedefHelperCommon {
39   typedef support::detail::packed_endian_specific_integral
40     <uint16_t, target_endianness, support::aligned> Elf_Half;
41   typedef support::detail::packed_endian_specific_integral
42     <uint32_t, target_endianness, support::aligned> Elf_Word;
43   typedef support::detail::packed_endian_specific_integral
44     <int32_t, target_endianness, support::aligned> Elf_Sword;
45   typedef support::detail::packed_endian_specific_integral
46     <uint64_t, target_endianness, support::aligned> Elf_Xword;
47   typedef support::detail::packed_endian_specific_integral
48     <int64_t, target_endianness, support::aligned> Elf_Sxword;
49 };
50
51 template<support::endianness target_endianness, bool is64Bits>
52 struct ELFDataTypeTypedefHelper;
53
54 /// ELF 32bit types.
55 template<support::endianness target_endianness>
56 struct ELFDataTypeTypedefHelper<target_endianness, false>
57   : ELFDataTypeTypedefHelperCommon<target_endianness> {
58   typedef uint32_t value_type;
59   typedef support::detail::packed_endian_specific_integral
60     <value_type, target_endianness, support::aligned> Elf_Addr;
61   typedef support::detail::packed_endian_specific_integral
62     <value_type, target_endianness, support::aligned> Elf_Off;
63 };
64
65 /// ELF 64bit types.
66 template<support::endianness target_endianness>
67 struct ELFDataTypeTypedefHelper<target_endianness, true>
68   : ELFDataTypeTypedefHelperCommon<target_endianness>{
69   typedef uint64_t value_type;
70   typedef support::detail::packed_endian_specific_integral
71     <value_type, target_endianness, support::aligned> Elf_Addr;
72   typedef support::detail::packed_endian_specific_integral
73     <value_type, target_endianness, support::aligned> Elf_Off;
74 };
75
76 // I really don't like doing this, but the alternative is copypasta.
77 #define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \
78 typedef typename \
79   ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Addr Elf_Addr; \
80 typedef typename \
81   ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Off Elf_Off; \
82 typedef typename \
83   ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Half Elf_Half; \
84 typedef typename \
85   ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Word Elf_Word; \
86 typedef typename \
87   ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sword Elf_Sword; \
88 typedef typename \
89   ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Xword Elf_Xword; \
90 typedef typename \
91   ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sxword Elf_Sxword;
92
93   // Section header.
94 template<support::endianness target_endianness, bool is64Bits>
95 struct Elf_Shdr_Base;
96
97 template<support::endianness target_endianness>
98 struct Elf_Shdr_Base<target_endianness, false> {
99   LLVM_ELF_IMPORT_TYPES(target_endianness, false)
100   Elf_Word sh_name;     // Section name (index into string table)
101   Elf_Word sh_type;     // Section type (SHT_*)
102   Elf_Word sh_flags;    // Section flags (SHF_*)
103   Elf_Addr sh_addr;     // Address where section is to be loaded
104   Elf_Off  sh_offset;   // File offset of section data, in bytes
105   Elf_Word sh_size;     // Size of section, in bytes
106   Elf_Word sh_link;     // Section type-specific header table index link
107   Elf_Word sh_info;     // Section type-specific extra information
108   Elf_Word sh_addralign;// Section address alignment
109   Elf_Word sh_entsize;  // Size of records contained within the section
110 };
111
112 template<support::endianness target_endianness>
113 struct Elf_Shdr_Base<target_endianness, true> {
114   LLVM_ELF_IMPORT_TYPES(target_endianness, true)
115   Elf_Word  sh_name;     // Section name (index into string table)
116   Elf_Word  sh_type;     // Section type (SHT_*)
117   Elf_Xword sh_flags;    // Section flags (SHF_*)
118   Elf_Addr  sh_addr;     // Address where section is to be loaded
119   Elf_Off   sh_offset;   // File offset of section data, in bytes
120   Elf_Xword sh_size;     // Size of section, in bytes
121   Elf_Word  sh_link;     // Section type-specific header table index link
122   Elf_Word  sh_info;     // Section type-specific extra information
123   Elf_Xword sh_addralign;// Section address alignment
124   Elf_Xword sh_entsize;  // Size of records contained within the section
125 };
126
127 template<support::endianness target_endianness, bool is64Bits>
128 struct Elf_Shdr_Impl : Elf_Shdr_Base<target_endianness, is64Bits> {
129   using Elf_Shdr_Base<target_endianness, is64Bits>::sh_entsize;
130   using Elf_Shdr_Base<target_endianness, is64Bits>::sh_size;
131
132   /// @brief Get the number of entities this section contains if it has any.
133   unsigned getEntityCount() const {
134     if (sh_entsize == 0)
135       return 0;
136     return sh_size / sh_entsize;
137   }
138 };
139
140 template<support::endianness target_endianness, bool is64Bits>
141 struct Elf_Sym_Base;
142
143 template<support::endianness target_endianness>
144 struct Elf_Sym_Base<target_endianness, false> {
145   LLVM_ELF_IMPORT_TYPES(target_endianness, false)
146   Elf_Word      st_name;  // Symbol name (index into string table)
147   Elf_Addr      st_value; // Value or address associated with the symbol
148   Elf_Word      st_size;  // Size of the symbol
149   unsigned char st_info;  // Symbol's type and binding attributes
150   unsigned char st_other; // Must be zero; reserved
151   Elf_Half      st_shndx; // Which section (header table index) it's defined in
152 };
153
154 template<support::endianness target_endianness>
155 struct Elf_Sym_Base<target_endianness, true> {
156   LLVM_ELF_IMPORT_TYPES(target_endianness, true)
157   Elf_Word      st_name;  // Symbol name (index into string table)
158   unsigned char st_info;  // Symbol's type and binding attributes
159   unsigned char st_other; // Must be zero; reserved
160   Elf_Half      st_shndx; // Which section (header table index) it's defined in
161   Elf_Addr      st_value; // Value or address associated with the symbol
162   Elf_Xword     st_size;  // Size of the symbol
163 };
164
165 template<support::endianness target_endianness, bool is64Bits>
166 struct Elf_Sym_Impl : Elf_Sym_Base<target_endianness, is64Bits> {
167   using Elf_Sym_Base<target_endianness, is64Bits>::st_info;
168
169   // These accessors and mutators correspond to the ELF32_ST_BIND,
170   // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
171   unsigned char getBinding() const { return st_info >> 4; }
172   unsigned char getType() const { return st_info & 0x0f; }
173   void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
174   void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
175   void setBindingAndType(unsigned char b, unsigned char t) {
176     st_info = (b << 4) + (t & 0x0f);
177   }
178 };
179
180 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
181 /// (.gnu.version). This structure is identical for ELF32 and ELF64.
182 template<support::endianness target_endianness, bool is64Bits>
183 struct Elf_Versym_Impl {
184   LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
185   Elf_Half vs_index;   // Version index with flags (e.g. VERSYM_HIDDEN)
186 };
187
188 template<support::endianness target_endianness, bool is64Bits>
189 struct Elf_Verdaux_Impl;
190
191 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
192 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
193 template<support::endianness target_endianness, bool is64Bits>
194 struct Elf_Verdef_Impl {
195   LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
196   typedef Elf_Verdaux_Impl<target_endianness, is64Bits> Elf_Verdaux;
197   Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
198   Elf_Half vd_flags;   // Bitwise flags (VER_DEF_*)
199   Elf_Half vd_ndx;     // Version index, used in .gnu.version entries
200   Elf_Half vd_cnt;     // Number of Verdaux entries
201   Elf_Word vd_hash;    // Hash of name
202   Elf_Word vd_aux;     // Offset to the first Verdaux entry (in bytes)
203   Elf_Word vd_next;    // Offset to the next Verdef entry (in bytes)
204
205   /// Get the first Verdaux entry for this Verdef.
206   const Elf_Verdaux *getAux() const {
207     return reinterpret_cast<const Elf_Verdaux*>((const char*)this + vd_aux);
208   }
209 };
210
211 /// Elf_Verdaux: This is the structure of auxilary data in the SHT_GNU_verdef
212 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
213 template<support::endianness target_endianness, bool is64Bits>
214 struct Elf_Verdaux_Impl {
215   LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
216   Elf_Word vda_name; // Version name (offset in string table)
217   Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
218 };
219
220 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
221 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
222 template<support::endianness target_endianness, bool is64Bits>
223 struct Elf_Verneed_Impl {
224   LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
225   Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
226   Elf_Half vn_cnt;     // Number of associated Vernaux entries
227   Elf_Word vn_file;    // Library name (string table offset)
228   Elf_Word vn_aux;     // Offset to first Vernaux entry (in bytes)
229   Elf_Word vn_next;    // Offset to next Verneed entry (in bytes)
230 };
231
232 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
233 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
234 template<support::endianness target_endianness, bool is64Bits>
235 struct Elf_Vernaux_Impl {
236   LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
237   Elf_Word vna_hash;  // Hash of dependency name
238   Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
239   Elf_Half vna_other; // Version index, used in .gnu.version entries
240   Elf_Word vna_name;  // Dependency name
241   Elf_Word vna_next;  // Offset to next Vernaux entry (in bytes)
242 };
243
244 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
245 ///               table section (.dynamic) look like.
246 template<support::endianness target_endianness, bool is64Bits>
247 struct Elf_Dyn_Base;
248
249 template<support::endianness target_endianness>
250 struct Elf_Dyn_Base<target_endianness, false> {
251   LLVM_ELF_IMPORT_TYPES(target_endianness, false)
252   Elf_Sword d_tag;
253   union {
254     Elf_Word d_val;
255     Elf_Addr d_ptr;
256   } d_un;
257 };
258
259 template<support::endianness target_endianness>
260 struct Elf_Dyn_Base<target_endianness, true> {
261   LLVM_ELF_IMPORT_TYPES(target_endianness, true)
262   Elf_Sxword d_tag;
263   union {
264     Elf_Xword d_val;
265     Elf_Addr d_ptr;
266   } d_un;
267 };
268
269 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
270 template<support::endianness target_endianness, bool is64Bits>
271 struct Elf_Dyn_Impl : Elf_Dyn_Base<target_endianness, is64Bits> {
272   using Elf_Dyn_Base<target_endianness, is64Bits>::d_tag;
273   using Elf_Dyn_Base<target_endianness, is64Bits>::d_un;
274   int64_t getTag() const { return d_tag; }
275   uint64_t getVal() const { return d_un.d_val; }
276   uint64_t getPtr() const { return d_un.ptr; }
277 };
278
279 template<support::endianness target_endianness, bool is64Bits>
280 class ELFObjectFile;
281
282 // DynRefImpl: Reference to an entry in the dynamic table
283 // This is an ELF-specific interface.
284 template<support::endianness target_endianness, bool is64Bits>
285 class DynRefImpl {
286   typedef Elf_Dyn_Impl<target_endianness, is64Bits> Elf_Dyn;
287   typedef ELFObjectFile<target_endianness, is64Bits> OwningType;
288
289   DataRefImpl DynPimpl;
290   const OwningType *OwningObject;
291
292 public:
293   DynRefImpl() : OwningObject(NULL) { }
294
295   DynRefImpl(DataRefImpl DynP, const OwningType *Owner);
296
297   bool operator==(const DynRefImpl &Other) const;
298   bool operator <(const DynRefImpl &Other) const;
299
300   error_code getNext(DynRefImpl &Result) const;
301   int64_t getTag() const;
302   uint64_t getVal() const;
303   uint64_t getPtr() const;
304
305   DataRefImpl getRawDataRefImpl() const;
306 };
307
308 // Elf_Rel: Elf Relocation
309 template<support::endianness target_endianness, bool is64Bits, bool isRela>
310 struct Elf_Rel_Base;
311
312 template<support::endianness target_endianness>
313 struct Elf_Rel_Base<target_endianness, false, false> {
314   LLVM_ELF_IMPORT_TYPES(target_endianness, false)
315   Elf_Addr      r_offset; // Location (file byte offset, or program virtual addr)
316   Elf_Word      r_info;  // Symbol table index and type of relocation to apply
317 };
318
319 template<support::endianness target_endianness>
320 struct Elf_Rel_Base<target_endianness, true, false> {
321   LLVM_ELF_IMPORT_TYPES(target_endianness, true)
322   Elf_Addr      r_offset; // Location (file byte offset, or program virtual addr)
323   Elf_Xword     r_info;   // Symbol table index and type of relocation to apply
324 };
325
326 template<support::endianness target_endianness>
327 struct Elf_Rel_Base<target_endianness, false, true> {
328   LLVM_ELF_IMPORT_TYPES(target_endianness, false)
329   Elf_Addr      r_offset; // Location (file byte offset, or program virtual addr)
330   Elf_Word      r_info;   // Symbol table index and type of relocation to apply
331   Elf_Sword     r_addend; // Compute value for relocatable field by adding this
332 };
333
334 template<support::endianness target_endianness>
335 struct Elf_Rel_Base<target_endianness, true, true> {
336   LLVM_ELF_IMPORT_TYPES(target_endianness, true)
337   Elf_Addr      r_offset; // Location (file byte offset, or program virtual addr)
338   Elf_Xword     r_info;   // Symbol table index and type of relocation to apply
339   Elf_Sxword    r_addend; // Compute value for relocatable field by adding this.
340 };
341
342 template<support::endianness target_endianness, bool is64Bits, bool isRela>
343 struct Elf_Rel_Impl;
344
345 template<support::endianness target_endianness, bool isRela>
346 struct Elf_Rel_Impl<target_endianness, true, isRela>
347        : Elf_Rel_Base<target_endianness, true, isRela> {
348   using Elf_Rel_Base<target_endianness, true, isRela>::r_info;
349   LLVM_ELF_IMPORT_TYPES(target_endianness, true)
350
351   // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
352   // and ELF64_R_INFO macros defined in the ELF specification:
353   uint64_t getSymbol() const { return (r_info >> 32); }
354   unsigned char getType() const {
355     return (unsigned char) (r_info & 0xffffffffL);
356   }
357   void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); }
358   void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
359   void setSymbolAndType(uint64_t s, unsigned char t) {
360     r_info = (s << 32) + (t&0xffffffffL);
361   }
362 };
363
364 template<support::endianness target_endianness, bool isRela>
365 struct Elf_Rel_Impl<target_endianness, false, isRela>
366        : Elf_Rel_Base<target_endianness, false, isRela> {
367   using Elf_Rel_Base<target_endianness, false, isRela>::r_info;
368   LLVM_ELF_IMPORT_TYPES(target_endianness, false)
369
370   // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
371   // and ELF32_R_INFO macros defined in the ELF specification:
372   uint32_t getSymbol() const { return (r_info >> 8); }
373   unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
374   void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
375   void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
376   void setSymbolAndType(uint32_t s, unsigned char t) {
377     r_info = (s << 8) + t;
378   }
379 };
380
381
382 template<support::endianness target_endianness, bool is64Bits>
383 class ELFObjectFile : public ObjectFile {
384   LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
385
386   typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
387   typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
388   typedef Elf_Dyn_Impl<target_endianness, is64Bits> Elf_Dyn;
389   typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
390   typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
391   typedef Elf_Verdef_Impl<target_endianness, is64Bits> Elf_Verdef;
392   typedef Elf_Verdaux_Impl<target_endianness, is64Bits> Elf_Verdaux;
393   typedef Elf_Verneed_Impl<target_endianness, is64Bits> Elf_Verneed;
394   typedef Elf_Vernaux_Impl<target_endianness, is64Bits> Elf_Vernaux;
395   typedef Elf_Versym_Impl<target_endianness, is64Bits> Elf_Versym;
396   typedef DynRefImpl<target_endianness, is64Bits> DynRef;
397   typedef content_iterator<DynRef> dyn_iterator;
398
399 protected:
400   struct Elf_Ehdr {
401     unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
402     Elf_Half e_type;     // Type of file (see ET_*)
403     Elf_Half e_machine;  // Required architecture for this file (see EM_*)
404     Elf_Word e_version;  // Must be equal to 1
405     Elf_Addr e_entry;    // Address to jump to in order to start program
406     Elf_Off  e_phoff;    // Program header table's file offset, in bytes
407     Elf_Off  e_shoff;    // Section header table's file offset, in bytes
408     Elf_Word e_flags;    // Processor-specific flags
409     Elf_Half e_ehsize;   // Size of ELF header, in bytes
410     Elf_Half e_phentsize;// Size of an entry in the program header table
411     Elf_Half e_phnum;    // Number of entries in the program header table
412     Elf_Half e_shentsize;// Size of an entry in the section header table
413     Elf_Half e_shnum;    // Number of entries in the section header table
414     Elf_Half e_shstrndx; // Section header table index of section name
415                                   // string table
416     bool checkMagic() const {
417       return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
418     }
419     unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
420     unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
421   };
422   // This flag is used for classof, to distinguish ELFObjectFile from
423   // its subclass. If more subclasses will be created, this flag will
424   // have to become an enum.
425   bool isDyldELFObject;
426
427 private:
428   typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
429   typedef DenseMap<unsigned, unsigned> IndexMap_t;
430   typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t;
431
432   const Elf_Ehdr *Header;
433   const Elf_Shdr *SectionHeaderTable;
434   const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
435   const Elf_Shdr *dot_strtab_sec;   // Symbol header string table.
436   const Elf_Shdr *dot_dynstr_sec;   // Dynamic symbol string table.
437
438   // SymbolTableSections[0] always points to the dynamic string table section
439   // header, or NULL if there is no dynamic string table.
440   Sections_t SymbolTableSections;
441   IndexMap_t SymbolTableSectionsIndexMap;
442   DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable;
443
444   const Elf_Shdr *dot_dynamic_sec;       // .dynamic
445   const Elf_Shdr *dot_gnu_version_sec;   // .gnu.version
446   const Elf_Shdr *dot_gnu_version_r_sec; // .gnu.version_r
447   const Elf_Shdr *dot_gnu_version_d_sec; // .gnu.version_d
448
449   // Pointer to SONAME entry in dynamic string table
450   // This is set the first time getLoadName is called.
451   mutable const char *dt_soname;
452
453   // Records for each version index the corresponding Verdef or Vernaux entry.
454   // This is filled the first time LoadVersionMap() is called.
455   class VersionMapEntry : public PointerIntPair<const void*, 1> {
456     public:
457     // If the integer is 0, this is an Elf_Verdef*.
458     // If the integer is 1, this is an Elf_Vernaux*.
459     VersionMapEntry() : PointerIntPair<const void*, 1>(NULL, 0) { }
460     VersionMapEntry(const Elf_Verdef *verdef)
461         : PointerIntPair<const void*, 1>(verdef, 0) { }
462     VersionMapEntry(const Elf_Vernaux *vernaux)
463         : PointerIntPair<const void*, 1>(vernaux, 1) { }
464     bool isNull() const { return getPointer() == NULL; }
465     bool isVerdef() const { return !isNull() && getInt() == 0; }
466     bool isVernaux() const { return !isNull() && getInt() == 1; }
467     const Elf_Verdef *getVerdef() const {
468       return isVerdef() ? (const Elf_Verdef*)getPointer() : NULL;
469     }
470     const Elf_Vernaux *getVernaux() const {
471       return isVernaux() ? (const Elf_Vernaux*)getPointer() : NULL;
472     }
473   };
474   mutable SmallVector<VersionMapEntry, 16> VersionMap;
475   void LoadVersionDefs(const Elf_Shdr *sec) const;
476   void LoadVersionNeeds(const Elf_Shdr *ec) const;
477   void LoadVersionMap() const;
478
479   /// @brief Map sections to an array of relocation sections that reference
480   ///        them sorted by section index.
481   RelocMap_t SectionRelocMap;
482
483   /// @brief Get the relocation section that contains \a Rel.
484   const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
485     return getSection(Rel.w.b);
486   }
487
488   bool            isRelocationHasAddend(DataRefImpl Rel) const;
489   template<typename T>
490   const T        *getEntry(uint16_t Section, uint32_t Entry) const;
491   template<typename T>
492   const T        *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
493   const Elf_Shdr *getSection(DataRefImpl index) const;
494   const Elf_Shdr *getSection(uint32_t index) const;
495   const Elf_Rel  *getRel(DataRefImpl Rel) const;
496   const Elf_Rela *getRela(DataRefImpl Rela) const;
497   const char     *getString(uint32_t section, uint32_t offset) const;
498   const char     *getString(const Elf_Shdr *section, uint32_t offset) const;
499   error_code      getSymbolName(const Elf_Shdr *section,
500                                 const Elf_Sym *Symb,
501                                 StringRef &Res) const;
502   error_code      getSymbolVersion(const Elf_Shdr *section,
503                                    const Elf_Sym *Symb,
504                                    StringRef &Version,
505                                    bool &IsDefault) const;
506   void VerifyStrTab(const Elf_Shdr *sh) const;
507
508 protected:
509   const Elf_Sym  *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
510   void            validateSymbol(DataRefImpl Symb) const;
511
512 public:
513   const Elf_Dyn  *getDyn(DataRefImpl DynData) const;
514   error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
515                               bool &IsDefault) const;
516 protected:
517   virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
518   virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
519   virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
520   virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
521   virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
522   virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
523   virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
524   virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
525   virtual error_code getSymbolSection(DataRefImpl Symb,
526                                       section_iterator &Res) const;
527
528   friend class DynRefImpl<target_endianness, is64Bits>;
529   virtual error_code getDynNext(DataRefImpl DynData, DynRef &Result) const;
530
531   virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
532   virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
533
534   virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
535   virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
536   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
537   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
538   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
539   virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
540   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
541   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
542   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
543   virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
544                                            bool &Result) const;
545   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
546   virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
547
548   virtual error_code getRelocationNext(DataRefImpl Rel,
549                                        RelocationRef &Res) const;
550   virtual error_code getRelocationAddress(DataRefImpl Rel,
551                                           uint64_t &Res) const;
552   virtual error_code getRelocationOffset(DataRefImpl Rel,
553                                          uint64_t &Res) const;
554   virtual error_code getRelocationSymbol(DataRefImpl Rel,
555                                          SymbolRef &Res) const;
556   virtual error_code getRelocationType(DataRefImpl Rel,
557                                        uint64_t &Res) const;
558   virtual error_code getRelocationTypeName(DataRefImpl Rel,
559                                            SmallVectorImpl<char> &Result) const;
560   virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
561                                                  int64_t &Res) const;
562   virtual error_code getRelocationValueString(DataRefImpl Rel,
563                                            SmallVectorImpl<char> &Result) const;
564
565 public:
566   ELFObjectFile(MemoryBuffer *Object, error_code &ec);
567   virtual symbol_iterator begin_symbols() const;
568   virtual symbol_iterator end_symbols() const;
569
570   virtual symbol_iterator begin_dynamic_symbols() const;
571   virtual symbol_iterator end_dynamic_symbols() const;
572
573   virtual section_iterator begin_sections() const;
574   virtual section_iterator end_sections() const;
575
576   virtual library_iterator begin_libraries_needed() const;
577   virtual library_iterator end_libraries_needed() const;
578
579   virtual dyn_iterator begin_dynamic_table() const;
580   virtual dyn_iterator end_dynamic_table() const;
581
582   virtual uint8_t getBytesInAddress() const;
583   virtual StringRef getFileFormatName() const;
584   virtual StringRef getObjectType() const { return "ELF"; }
585   virtual unsigned getArch() const;
586   virtual StringRef getLoadName() const;
587
588   uint64_t getNumSections() const;
589   uint64_t getStringTableIndex() const;
590   ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
591   const Elf_Shdr *getSection(const Elf_Sym *symb) const;
592
593   // Methods for type inquiry through isa, cast, and dyn_cast
594   bool isDyldType() const { return isDyldELFObject; }
595   static inline bool classof(const Binary *v) {
596     return v->getType() == getELFType(target_endianness == support::little,
597                                       is64Bits);
598   }
599   static inline bool classof(const ELFObjectFile *v) { return true; }
600 };
601
602 // Iterate through the version definitions, and place each Elf_Verdef
603 // in the VersionMap according to its index.
604 template<support::endianness target_endianness, bool is64Bits>
605 void ELFObjectFile<target_endianness, is64Bits>::
606                   LoadVersionDefs(const Elf_Shdr *sec) const {
607   unsigned vd_size = sec->sh_size; // Size of section in bytes
608   unsigned vd_count = sec->sh_info; // Number of Verdef entries
609   const char *sec_start = (const char*)base() + sec->sh_offset;
610   const char *sec_end = sec_start + vd_size;
611   // The first Verdef entry is at the start of the section.
612   const char *p = sec_start;
613   for (unsigned i = 0; i < vd_count; i++) {
614     if (p + sizeof(Elf_Verdef) > sec_end)
615       report_fatal_error("Section ended unexpectedly while scanning "
616                          "version definitions.");
617     const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
618     if (vd->vd_version != ELF::VER_DEF_CURRENT)
619       report_fatal_error("Unexpected verdef version");
620     size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
621     if (index >= VersionMap.size())
622       VersionMap.resize(index+1);
623     VersionMap[index] = VersionMapEntry(vd);
624     p += vd->vd_next;
625   }
626 }
627
628 // Iterate through the versions needed section, and place each Elf_Vernaux
629 // in the VersionMap according to its index.
630 template<support::endianness target_endianness, bool is64Bits>
631 void ELFObjectFile<target_endianness, is64Bits>::
632                   LoadVersionNeeds(const Elf_Shdr *sec) const {
633   unsigned vn_size = sec->sh_size; // Size of section in bytes
634   unsigned vn_count = sec->sh_info; // Number of Verneed entries
635   const char *sec_start = (const char*)base() + sec->sh_offset;
636   const char *sec_end = sec_start + vn_size;
637   // The first Verneed entry is at the start of the section.
638   const char *p = sec_start;
639   for (unsigned i = 0; i < vn_count; i++) {
640     if (p + sizeof(Elf_Verneed) > sec_end)
641       report_fatal_error("Section ended unexpectedly while scanning "
642                          "version needed records.");
643     const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
644     if (vn->vn_version != ELF::VER_NEED_CURRENT)
645       report_fatal_error("Unexpected verneed version");
646     // Iterate through the Vernaux entries
647     const char *paux = p + vn->vn_aux;
648     for (unsigned j = 0; j < vn->vn_cnt; j++) {
649       if (paux + sizeof(Elf_Vernaux) > sec_end)
650         report_fatal_error("Section ended unexpected while scanning auxiliary "
651                            "version needed records.");
652       const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
653       size_t index = vna->vna_other & ELF::VERSYM_VERSION;
654       if (index >= VersionMap.size())
655         VersionMap.resize(index+1);
656       VersionMap[index] = VersionMapEntry(vna);
657       paux += vna->vna_next;
658     }
659     p += vn->vn_next;
660   }
661 }
662
663 template<support::endianness target_endianness, bool is64Bits>
664 void ELFObjectFile<target_endianness, is64Bits>::LoadVersionMap() const {
665   // If there is no dynamic symtab or version table, there is nothing to do.
666   if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL)
667     return;
668
669   // Has the VersionMap already been loaded?
670   if (VersionMap.size() > 0)
671     return;
672
673   // The first two version indexes are reserved.
674   // Index 0 is LOCAL, index 1 is GLOBAL.
675   VersionMap.push_back(VersionMapEntry());
676   VersionMap.push_back(VersionMapEntry());
677
678   if (dot_gnu_version_d_sec)
679     LoadVersionDefs(dot_gnu_version_d_sec);
680
681   if (dot_gnu_version_r_sec)
682     LoadVersionNeeds(dot_gnu_version_r_sec);
683 }
684
685 template<support::endianness target_endianness, bool is64Bits>
686 void ELFObjectFile<target_endianness, is64Bits>
687                   ::validateSymbol(DataRefImpl Symb) const {
688   const Elf_Sym  *symb = getSymbol(Symb);
689   const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
690   // FIXME: We really need to do proper error handling in the case of an invalid
691   //        input file. Because we don't use exceptions, I think we'll just pass
692   //        an error object around.
693   if (!(  symb
694         && SymbolTableSection
695         && symb >= (const Elf_Sym*)(base()
696                    + SymbolTableSection->sh_offset)
697         && symb <  (const Elf_Sym*)(base()
698                    + SymbolTableSection->sh_offset
699                    + SymbolTableSection->sh_size)))
700     // FIXME: Proper error handling.
701     report_fatal_error("Symb must point to a valid symbol!");
702 }
703
704 template<support::endianness target_endianness, bool is64Bits>
705 error_code ELFObjectFile<target_endianness, is64Bits>
706                         ::getSymbolNext(DataRefImpl Symb,
707                                         SymbolRef &Result) const {
708   validateSymbol(Symb);
709   const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
710
711   ++Symb.d.a;
712   // Check to see if we are at the end of this symbol table.
713   if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
714     // We are at the end. If there are other symbol tables, jump to them.
715     // If the symbol table is .dynsym, we are iterating dynamic symbols,
716     // and there is only one table of these.
717     if (Symb.d.b != 0) {
718       ++Symb.d.b;
719       Symb.d.a = 1; // The 0th symbol in ELF is fake.
720     }
721     // Otherwise return the terminator.
722     if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) {
723       Symb.d.a = std::numeric_limits<uint32_t>::max();
724       Symb.d.b = std::numeric_limits<uint32_t>::max();
725     }
726   }
727
728   Result = SymbolRef(Symb, this);
729   return object_error::success;
730 }
731
732 template<support::endianness target_endianness, bool is64Bits>
733 error_code ELFObjectFile<target_endianness, is64Bits>
734                         ::getSymbolName(DataRefImpl Symb,
735                                         StringRef &Result) const {
736   validateSymbol(Symb);
737   const Elf_Sym *symb = getSymbol(Symb);
738   return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
739 }
740
741 template<support::endianness target_endianness, bool is64Bits>
742 error_code ELFObjectFile<target_endianness, is64Bits>
743                         ::getSymbolVersion(SymbolRef SymRef,
744                                            StringRef &Version,
745                                            bool &IsDefault) const {
746   DataRefImpl Symb = SymRef.getRawDataRefImpl();
747   validateSymbol(Symb);
748   const Elf_Sym *symb = getSymbol(Symb);
749   return getSymbolVersion(SymbolTableSections[Symb.d.b], symb,
750                           Version, IsDefault);
751 }
752
753 template<support::endianness target_endianness, bool is64Bits>
754 ELF::Elf64_Word ELFObjectFile<target_endianness, is64Bits>
755                       ::getSymbolTableIndex(const Elf_Sym *symb) const {
756   if (symb->st_shndx == ELF::SHN_XINDEX)
757     return ExtendedSymbolTable.lookup(symb);
758   return symb->st_shndx;
759 }
760
761 template<support::endianness target_endianness, bool is64Bits>
762 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
763 ELFObjectFile<target_endianness, is64Bits>
764                              ::getSection(const Elf_Sym *symb) const {
765   if (symb->st_shndx == ELF::SHN_XINDEX)
766     return getSection(ExtendedSymbolTable.lookup(symb));
767   if (symb->st_shndx >= ELF::SHN_LORESERVE)
768     return 0;
769   return getSection(symb->st_shndx);
770 }
771
772 template<support::endianness target_endianness, bool is64Bits>
773 error_code ELFObjectFile<target_endianness, is64Bits>
774                         ::getSymbolFileOffset(DataRefImpl Symb,
775                                           uint64_t &Result) const {
776   validateSymbol(Symb);
777   const Elf_Sym  *symb = getSymbol(Symb);
778   const Elf_Shdr *Section;
779   switch (getSymbolTableIndex(symb)) {
780   case ELF::SHN_COMMON:
781    // Unintialized symbols have no offset in the object file
782   case ELF::SHN_UNDEF:
783     Result = UnknownAddressOrSize;
784     return object_error::success;
785   case ELF::SHN_ABS:
786     Result = symb->st_value;
787     return object_error::success;
788   default: Section = getSection(symb);
789   }
790
791   switch (symb->getType()) {
792   case ELF::STT_SECTION:
793     Result = Section ? Section->sh_addr : UnknownAddressOrSize;
794     return object_error::success;
795   case ELF::STT_FUNC:
796   case ELF::STT_OBJECT:
797   case ELF::STT_NOTYPE:
798     Result = symb->st_value +
799              (Section ? Section->sh_offset : 0);
800     return object_error::success;
801   default:
802     Result = UnknownAddressOrSize;
803     return object_error::success;
804   }
805 }
806
807 template<support::endianness target_endianness, bool is64Bits>
808 error_code ELFObjectFile<target_endianness, is64Bits>
809                         ::getSymbolAddress(DataRefImpl Symb,
810                                            uint64_t &Result) const {
811   validateSymbol(Symb);
812   const Elf_Sym  *symb = getSymbol(Symb);
813   const Elf_Shdr *Section;
814   switch (getSymbolTableIndex(symb)) {
815   case ELF::SHN_COMMON:
816   case ELF::SHN_UNDEF:
817     Result = UnknownAddressOrSize;
818     return object_error::success;
819   case ELF::SHN_ABS:
820     Result = symb->st_value;
821     return object_error::success;
822   default: Section = getSection(symb);
823   }
824
825   switch (symb->getType()) {
826   case ELF::STT_SECTION:
827     Result = Section ? Section->sh_addr : UnknownAddressOrSize;
828     return object_error::success;
829   case ELF::STT_FUNC:
830   case ELF::STT_OBJECT:
831   case ELF::STT_NOTYPE:
832     Result = symb->st_value + (Section ? Section->sh_addr : 0);
833     return object_error::success;
834   default:
835     Result = UnknownAddressOrSize;
836     return object_error::success;
837   }
838 }
839
840 template<support::endianness target_endianness, bool is64Bits>
841 error_code ELFObjectFile<target_endianness, is64Bits>
842                         ::getSymbolSize(DataRefImpl Symb,
843                                         uint64_t &Result) const {
844   validateSymbol(Symb);
845   const Elf_Sym  *symb = getSymbol(Symb);
846   if (symb->st_size == 0)
847     Result = UnknownAddressOrSize;
848   Result = symb->st_size;
849   return object_error::success;
850 }
851
852 template<support::endianness target_endianness, bool is64Bits>
853 error_code ELFObjectFile<target_endianness, is64Bits>
854                         ::getSymbolNMTypeChar(DataRefImpl Symb,
855                                               char &Result) const {
856   validateSymbol(Symb);
857   const Elf_Sym  *symb = getSymbol(Symb);
858   const Elf_Shdr *Section = getSection(symb);
859
860   char ret = '?';
861
862   if (Section) {
863     switch (Section->sh_type) {
864     case ELF::SHT_PROGBITS:
865     case ELF::SHT_DYNAMIC:
866       switch (Section->sh_flags) {
867       case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
868         ret = 't'; break;
869       case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
870         ret = 'd'; break;
871       case ELF::SHF_ALLOC:
872       case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
873       case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
874         ret = 'r'; break;
875       }
876       break;
877     case ELF::SHT_NOBITS: ret = 'b';
878     }
879   }
880
881   switch (getSymbolTableIndex(symb)) {
882   case ELF::SHN_UNDEF:
883     if (ret == '?')
884       ret = 'U';
885     break;
886   case ELF::SHN_ABS: ret = 'a'; break;
887   case ELF::SHN_COMMON: ret = 'c'; break;
888   }
889
890   switch (symb->getBinding()) {
891   case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
892   case ELF::STB_WEAK:
893     if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
894       ret = 'w';
895     else
896       if (symb->getType() == ELF::STT_OBJECT)
897         ret = 'V';
898       else
899         ret = 'W';
900   }
901
902   if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
903     StringRef name;
904     if (error_code ec = getSymbolName(Symb, name))
905       return ec;
906     Result = StringSwitch<char>(name)
907       .StartsWith(".debug", 'N')
908       .StartsWith(".note", 'n')
909       .Default('?');
910     return object_error::success;
911   }
912
913   Result = ret;
914   return object_error::success;
915 }
916
917 template<support::endianness target_endianness, bool is64Bits>
918 error_code ELFObjectFile<target_endianness, is64Bits>
919                         ::getSymbolType(DataRefImpl Symb,
920                                         SymbolRef::Type &Result) const {
921   validateSymbol(Symb);
922   const Elf_Sym  *symb = getSymbol(Symb);
923
924   switch (symb->getType()) {
925   case ELF::STT_NOTYPE:
926     Result = SymbolRef::ST_Unknown;
927     break;
928   case ELF::STT_SECTION:
929     Result = SymbolRef::ST_Debug;
930     break;
931   case ELF::STT_FILE:
932     Result = SymbolRef::ST_File;
933     break;
934   case ELF::STT_FUNC:
935     Result = SymbolRef::ST_Function;
936     break;
937   case ELF::STT_OBJECT:
938   case ELF::STT_COMMON:
939   case ELF::STT_TLS:
940     Result = SymbolRef::ST_Data;
941     break;
942   default:
943     Result = SymbolRef::ST_Other;
944     break;
945   }
946   return object_error::success;
947 }
948
949 template<support::endianness target_endianness, bool is64Bits>
950 error_code ELFObjectFile<target_endianness, is64Bits>
951                         ::getSymbolFlags(DataRefImpl Symb,
952                                          uint32_t &Result) const {
953   validateSymbol(Symb);
954   const Elf_Sym  *symb = getSymbol(Symb);
955
956   Result = SymbolRef::SF_None;
957
958   if (symb->getBinding() != ELF::STB_LOCAL)
959     Result |= SymbolRef::SF_Global;
960
961   if (symb->getBinding() == ELF::STB_WEAK)
962     Result |= SymbolRef::SF_Weak;
963
964   if (symb->st_shndx == ELF::SHN_ABS)
965     Result |= SymbolRef::SF_Absolute;
966
967   if (symb->getType() == ELF::STT_FILE ||
968       symb->getType() == ELF::STT_SECTION)
969     Result |= SymbolRef::SF_FormatSpecific;
970
971   if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
972     Result |= SymbolRef::SF_Undefined;
973
974   if (symb->getType() == ELF::STT_COMMON ||
975       getSymbolTableIndex(symb) == ELF::SHN_COMMON)
976     Result |= SymbolRef::SF_Common;
977
978   if (symb->getType() == ELF::STT_TLS)
979     Result |= SymbolRef::SF_ThreadLocal;
980
981   return object_error::success;
982 }
983
984 template<support::endianness target_endianness, bool is64Bits>
985 error_code ELFObjectFile<target_endianness, is64Bits>
986                         ::getSymbolSection(DataRefImpl Symb,
987                                            section_iterator &Res) const {
988   validateSymbol(Symb);
989   const Elf_Sym  *symb = getSymbol(Symb);
990   const Elf_Shdr *sec = getSection(symb);
991   if (!sec)
992     Res = end_sections();
993   else {
994     DataRefImpl Sec;
995     Sec.p = reinterpret_cast<intptr_t>(sec);
996     Res = section_iterator(SectionRef(Sec, this));
997   }
998   return object_error::success;
999 }
1000
1001 template<support::endianness target_endianness, bool is64Bits>
1002 error_code ELFObjectFile<target_endianness, is64Bits>
1003                         ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
1004   const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
1005   sec += Header->e_shentsize;
1006   Sec.p = reinterpret_cast<intptr_t>(sec);
1007   Result = SectionRef(Sec, this);
1008   return object_error::success;
1009 }
1010
1011 template<support::endianness target_endianness, bool is64Bits>
1012 error_code ELFObjectFile<target_endianness, is64Bits>
1013                         ::getSectionName(DataRefImpl Sec,
1014                                          StringRef &Result) const {
1015   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1016   Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
1017   return object_error::success;
1018 }
1019
1020 template<support::endianness target_endianness, bool is64Bits>
1021 error_code ELFObjectFile<target_endianness, is64Bits>
1022                         ::getSectionAddress(DataRefImpl Sec,
1023                                             uint64_t &Result) const {
1024   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1025   Result = sec->sh_addr;
1026   return object_error::success;
1027 }
1028
1029 template<support::endianness target_endianness, bool is64Bits>
1030 error_code ELFObjectFile<target_endianness, is64Bits>
1031                         ::getSectionSize(DataRefImpl Sec,
1032                                          uint64_t &Result) const {
1033   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1034   Result = sec->sh_size;
1035   return object_error::success;
1036 }
1037
1038 template<support::endianness target_endianness, bool is64Bits>
1039 error_code ELFObjectFile<target_endianness, is64Bits>
1040                         ::getSectionContents(DataRefImpl Sec,
1041                                              StringRef &Result) const {
1042   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1043   const char *start = (const char*)base() + sec->sh_offset;
1044   Result = StringRef(start, sec->sh_size);
1045   return object_error::success;
1046 }
1047
1048 template<support::endianness target_endianness, bool is64Bits>
1049 error_code ELFObjectFile<target_endianness, is64Bits>
1050                         ::getSectionAlignment(DataRefImpl Sec,
1051                                               uint64_t &Result) const {
1052   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1053   Result = sec->sh_addralign;
1054   return object_error::success;
1055 }
1056
1057 template<support::endianness target_endianness, bool is64Bits>
1058 error_code ELFObjectFile<target_endianness, is64Bits>
1059                         ::isSectionText(DataRefImpl Sec,
1060                                         bool &Result) const {
1061   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1062   if (sec->sh_flags & ELF::SHF_EXECINSTR)
1063     Result = true;
1064   else
1065     Result = false;
1066   return object_error::success;
1067 }
1068
1069 template<support::endianness target_endianness, bool is64Bits>
1070 error_code ELFObjectFile<target_endianness, is64Bits>
1071                         ::isSectionData(DataRefImpl Sec,
1072                                         bool &Result) const {
1073   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1074   if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1075       && sec->sh_type == ELF::SHT_PROGBITS)
1076     Result = true;
1077   else
1078     Result = false;
1079   return object_error::success;
1080 }
1081
1082 template<support::endianness target_endianness, bool is64Bits>
1083 error_code ELFObjectFile<target_endianness, is64Bits>
1084                         ::isSectionBSS(DataRefImpl Sec,
1085                                        bool &Result) const {
1086   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1087   if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1088       && sec->sh_type == ELF::SHT_NOBITS)
1089     Result = true;
1090   else
1091     Result = false;
1092   return object_error::success;
1093 }
1094
1095 template<support::endianness target_endianness, bool is64Bits>
1096 error_code ELFObjectFile<target_endianness, is64Bits>
1097                           ::sectionContainsSymbol(DataRefImpl Sec,
1098                                                   DataRefImpl Symb,
1099                                                   bool &Result) const {
1100   // FIXME: Unimplemented.
1101   Result = false;
1102   return object_error::success;
1103 }
1104
1105 template<support::endianness target_endianness, bool is64Bits>
1106 relocation_iterator ELFObjectFile<target_endianness, is64Bits>
1107                                  ::getSectionRelBegin(DataRefImpl Sec) const {
1108   DataRefImpl RelData;
1109   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1110   typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1111   if (sec != 0 && ittr != SectionRelocMap.end()) {
1112     RelData.w.a = getSection(ittr->second[0])->sh_info;
1113     RelData.w.b = ittr->second[0];
1114     RelData.w.c = 0;
1115   }
1116   return relocation_iterator(RelocationRef(RelData, this));
1117 }
1118
1119 template<support::endianness target_endianness, bool is64Bits>
1120 relocation_iterator ELFObjectFile<target_endianness, is64Bits>
1121                                  ::getSectionRelEnd(DataRefImpl Sec) const {
1122   DataRefImpl RelData;
1123   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1124   typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1125   if (sec != 0 && ittr != SectionRelocMap.end()) {
1126     // Get the index of the last relocation section for this section.
1127     std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1128     const Elf_Shdr *relocsec = getSection(relocsecindex);
1129     RelData.w.a = relocsec->sh_info;
1130     RelData.w.b = relocsecindex;
1131     RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1132   }
1133   return relocation_iterator(RelocationRef(RelData, this));
1134 }
1135
1136 // Relocations
1137 template<support::endianness target_endianness, bool is64Bits>
1138 error_code ELFObjectFile<target_endianness, is64Bits>
1139                         ::getRelocationNext(DataRefImpl Rel,
1140                                             RelocationRef &Result) const {
1141   ++Rel.w.c;
1142   const Elf_Shdr *relocsec = getSection(Rel.w.b);
1143   if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1144     // We have reached the end of the relocations for this section. See if there
1145     // is another relocation section.
1146     typename RelocMap_t::mapped_type relocseclist =
1147       SectionRelocMap.lookup(getSection(Rel.w.a));
1148
1149     // Do a binary search for the current reloc section index (which must be
1150     // present). Then get the next one.
1151     typename RelocMap_t::mapped_type::const_iterator loc =
1152       std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1153     ++loc;
1154
1155     // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1156     // to the end iterator.
1157     if (loc != relocseclist.end()) {
1158       Rel.w.b = *loc;
1159       Rel.w.a = 0;
1160     }
1161   }
1162   Result = RelocationRef(Rel, this);
1163   return object_error::success;
1164 }
1165
1166 template<support::endianness target_endianness, bool is64Bits>
1167 error_code ELFObjectFile<target_endianness, is64Bits>
1168                         ::getRelocationSymbol(DataRefImpl Rel,
1169                                               SymbolRef &Result) const {
1170   uint32_t symbolIdx;
1171   const Elf_Shdr *sec = getSection(Rel.w.b);
1172   switch (sec->sh_type) {
1173     default :
1174       report_fatal_error("Invalid section type in Rel!");
1175     case ELF::SHT_REL : {
1176       symbolIdx = getRel(Rel)->getSymbol();
1177       break;
1178     }
1179     case ELF::SHT_RELA : {
1180       symbolIdx = getRela(Rel)->getSymbol();
1181       break;
1182     }
1183   }
1184   DataRefImpl SymbolData;
1185   IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1186   if (it == SymbolTableSectionsIndexMap.end())
1187     report_fatal_error("Relocation symbol table not found!");
1188   SymbolData.d.a = symbolIdx;
1189   SymbolData.d.b = it->second;
1190   Result = SymbolRef(SymbolData, this);
1191   return object_error::success;
1192 }
1193
1194 template<support::endianness target_endianness, bool is64Bits>
1195 error_code ELFObjectFile<target_endianness, is64Bits>
1196                         ::getRelocationAddress(DataRefImpl Rel,
1197                                                uint64_t &Result) const {
1198   uint64_t offset;
1199   const Elf_Shdr *sec = getSection(Rel.w.b);
1200   switch (sec->sh_type) {
1201     default :
1202       report_fatal_error("Invalid section type in Rel!");
1203     case ELF::SHT_REL : {
1204       offset = getRel(Rel)->r_offset;
1205       break;
1206     }
1207     case ELF::SHT_RELA : {
1208       offset = getRela(Rel)->r_offset;
1209       break;
1210     }
1211   }
1212
1213   Result = offset;
1214   return object_error::success;
1215 }
1216
1217 template<support::endianness target_endianness, bool is64Bits>
1218 error_code ELFObjectFile<target_endianness, is64Bits>
1219                         ::getRelocationOffset(DataRefImpl Rel,
1220                                               uint64_t &Result) const {
1221   uint64_t offset;
1222   const Elf_Shdr *sec = getSection(Rel.w.b);
1223   switch (sec->sh_type) {
1224     default :
1225       report_fatal_error("Invalid section type in Rel!");
1226     case ELF::SHT_REL : {
1227       offset = getRel(Rel)->r_offset;
1228       break;
1229     }
1230     case ELF::SHT_RELA : {
1231       offset = getRela(Rel)->r_offset;
1232       break;
1233     }
1234   }
1235
1236   Result = offset - sec->sh_addr;
1237   return object_error::success;
1238 }
1239
1240 template<support::endianness target_endianness, bool is64Bits>
1241 error_code ELFObjectFile<target_endianness, is64Bits>
1242                         ::getRelocationType(DataRefImpl Rel,
1243                                             uint64_t &Result) const {
1244   const Elf_Shdr *sec = getSection(Rel.w.b);
1245   switch (sec->sh_type) {
1246     default :
1247       report_fatal_error("Invalid section type in Rel!");
1248     case ELF::SHT_REL : {
1249       Result = getRel(Rel)->getType();
1250       break;
1251     }
1252     case ELF::SHT_RELA : {
1253       Result = getRela(Rel)->getType();
1254       break;
1255     }
1256   }
1257   return object_error::success;
1258 }
1259
1260 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1261   case ELF::enum: res = #enum; break;
1262
1263 template<support::endianness target_endianness, bool is64Bits>
1264 error_code ELFObjectFile<target_endianness, is64Bits>
1265                         ::getRelocationTypeName(DataRefImpl Rel,
1266                                           SmallVectorImpl<char> &Result) const {
1267   const Elf_Shdr *sec = getSection(Rel.w.b);
1268   uint8_t type;
1269   StringRef res;
1270   switch (sec->sh_type) {
1271     default :
1272       return object_error::parse_failed;
1273     case ELF::SHT_REL : {
1274       type = getRel(Rel)->getType();
1275       break;
1276     }
1277     case ELF::SHT_RELA : {
1278       type = getRela(Rel)->getType();
1279       break;
1280     }
1281   }
1282   switch (Header->e_machine) {
1283   case ELF::EM_X86_64:
1284     switch (type) {
1285       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1286       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1287       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1288       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1289       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1290       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1291       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1292       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1293       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1294       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1295       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1296       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1297       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1298       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1299       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1300       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1301       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1302       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1303       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1304       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1305       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1306       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1307       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1308       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1309       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1310       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1311       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1312       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1313       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1314       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1315       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1316       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1317     default:
1318       res = "Unknown";
1319     }
1320     break;
1321   case ELF::EM_386:
1322     switch (type) {
1323       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1324       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1325       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1326       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1327       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1328       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1329       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1330       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1331       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1332       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1333       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1334       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1335       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1336       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1337       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1338       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1339       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1340       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1341       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1342       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1343       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1344       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1345       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1346       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1347       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1348       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1349       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1350       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1351       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1352       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1353       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1354       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1355       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1356       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1357       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1358       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1359       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1360       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1361       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1362       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1363     default:
1364       res = "Unknown";
1365     }
1366     break;
1367   default:
1368     res = "Unknown";
1369   }
1370   Result.append(res.begin(), res.end());
1371   return object_error::success;
1372 }
1373
1374 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1375
1376 template<support::endianness target_endianness, bool is64Bits>
1377 error_code ELFObjectFile<target_endianness, is64Bits>
1378                         ::getRelocationAdditionalInfo(DataRefImpl Rel,
1379                                                       int64_t &Result) const {
1380   const Elf_Shdr *sec = getSection(Rel.w.b);
1381   switch (sec->sh_type) {
1382     default :
1383       report_fatal_error("Invalid section type in Rel!");
1384     case ELF::SHT_REL : {
1385       Result = 0;
1386       return object_error::success;
1387     }
1388     case ELF::SHT_RELA : {
1389       Result = getRela(Rel)->r_addend;
1390       return object_error::success;
1391     }
1392   }
1393 }
1394
1395 template<support::endianness target_endianness, bool is64Bits>
1396 error_code ELFObjectFile<target_endianness, is64Bits>
1397                         ::getRelocationValueString(DataRefImpl Rel,
1398                                           SmallVectorImpl<char> &Result) const {
1399   const Elf_Shdr *sec = getSection(Rel.w.b);
1400   uint8_t type;
1401   StringRef res;
1402   int64_t addend = 0;
1403   uint16_t symbol_index = 0;
1404   switch (sec->sh_type) {
1405     default :
1406       return object_error::parse_failed;
1407     case ELF::SHT_REL : {
1408       type = getRel(Rel)->getType();
1409       symbol_index = getRel(Rel)->getSymbol();
1410       // TODO: Read implicit addend from section data.
1411       break;
1412     }
1413     case ELF::SHT_RELA : {
1414       type = getRela(Rel)->getType();
1415       symbol_index = getRela(Rel)->getSymbol();
1416       addend = getRela(Rel)->r_addend;
1417       break;
1418     }
1419   }
1420   const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1421   StringRef symname;
1422   if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1423     return ec;
1424   switch (Header->e_machine) {
1425   case ELF::EM_X86_64:
1426     switch (type) {
1427     case ELF::R_X86_64_32S:
1428       res = symname;
1429       break;
1430     case ELF::R_X86_64_PC32: {
1431         std::string fmtbuf;
1432         raw_string_ostream fmt(fmtbuf);
1433         fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1434         fmt.flush();
1435         Result.append(fmtbuf.begin(), fmtbuf.end());
1436       }
1437       break;
1438     default:
1439       res = "Unknown";
1440     }
1441     break;
1442   default:
1443     res = "Unknown";
1444   }
1445   if (Result.empty())
1446     Result.append(res.begin(), res.end());
1447   return object_error::success;
1448 }
1449
1450 // Verify that the last byte in the string table in a null.
1451 template<support::endianness target_endianness, bool is64Bits>
1452 void ELFObjectFile<target_endianness, is64Bits>
1453                   ::VerifyStrTab(const Elf_Shdr *sh) const {
1454   const char *strtab = (const char*)base() + sh->sh_offset;
1455   if (strtab[sh->sh_size - 1] != 0)
1456     // FIXME: Proper error handling.
1457     report_fatal_error("String table must end with a null terminator!");
1458 }
1459
1460 template<support::endianness target_endianness, bool is64Bits>
1461 ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
1462                                                           , error_code &ec)
1463   : ObjectFile(getELFType(target_endianness == support::little, is64Bits),
1464                Object, ec)
1465   , isDyldELFObject(false)
1466   , SectionHeaderTable(0)
1467   , dot_shstrtab_sec(0)
1468   , dot_strtab_sec(0)
1469   , dot_dynstr_sec(0)
1470   , dot_dynamic_sec(0)
1471   , dot_gnu_version_sec(0)
1472   , dot_gnu_version_r_sec(0)
1473   , dot_gnu_version_d_sec(0)
1474   , dt_soname(0)
1475  {
1476
1477   const uint64_t FileSize = Data->getBufferSize();
1478
1479   if (sizeof(Elf_Ehdr) > FileSize)
1480     // FIXME: Proper error handling.
1481     report_fatal_error("File too short!");
1482
1483   Header = reinterpret_cast<const Elf_Ehdr *>(base());
1484
1485   if (Header->e_shoff == 0)
1486     return;
1487
1488   const uint64_t SectionTableOffset = Header->e_shoff;
1489
1490   if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
1491     // FIXME: Proper error handling.
1492     report_fatal_error("Section header table goes past end of file!");
1493
1494   // The getNumSections() call below depends on SectionHeaderTable being set.
1495   SectionHeaderTable =
1496     reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
1497   const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
1498
1499   if (SectionTableOffset + SectionTableSize > FileSize)
1500     // FIXME: Proper error handling.
1501     report_fatal_error("Section table goes past end of file!");
1502
1503   // To find the symbol tables we walk the section table to find SHT_SYMTAB.
1504   const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
1505   const Elf_Shdr* sh = SectionHeaderTable;
1506
1507   // Reserve SymbolTableSections[0] for .dynsym
1508   SymbolTableSections.push_back(NULL);
1509
1510   for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
1511     switch (sh->sh_type) {
1512     case ELF::SHT_SYMTAB_SHNDX: {
1513       if (SymbolTableSectionHeaderIndex)
1514         // FIXME: Proper error handling.
1515         report_fatal_error("More than one .symtab_shndx!");
1516       SymbolTableSectionHeaderIndex = sh;
1517       break;
1518     }
1519     case ELF::SHT_SYMTAB: {
1520       SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
1521       SymbolTableSections.push_back(sh);
1522       break;
1523     }
1524     case ELF::SHT_DYNSYM: {
1525       if (SymbolTableSections[0] != NULL)
1526         // FIXME: Proper error handling.
1527         report_fatal_error("More than one .dynsym!");
1528       SymbolTableSectionsIndexMap[i] = 0;
1529       SymbolTableSections[0] = sh;
1530       break;
1531     }
1532     case ELF::SHT_REL:
1533     case ELF::SHT_RELA: {
1534       SectionRelocMap[getSection(sh->sh_info)].push_back(i);
1535       break;
1536     }
1537     case ELF::SHT_DYNAMIC: {
1538       if (dot_dynamic_sec != NULL)
1539         // FIXME: Proper error handling.
1540         report_fatal_error("More than one .dynamic!");
1541       dot_dynamic_sec = sh;
1542       break;
1543     }
1544     case ELF::SHT_GNU_versym: {
1545       if (dot_gnu_version_sec != NULL)
1546         // FIXME: Proper error handling.
1547         report_fatal_error("More than one .gnu.version section!");
1548       dot_gnu_version_sec = sh;
1549       break;
1550     }
1551     case ELF::SHT_GNU_verdef: {
1552       if (dot_gnu_version_d_sec != NULL)
1553         // FIXME: Proper error handling.
1554         report_fatal_error("More than one .gnu.version_d section!");
1555       dot_gnu_version_d_sec = sh;
1556       break;
1557     }
1558     case ELF::SHT_GNU_verneed: {
1559       if (dot_gnu_version_r_sec != NULL)
1560         // FIXME: Proper error handling.
1561         report_fatal_error("More than one .gnu.version_r section!");
1562       dot_gnu_version_r_sec = sh;
1563       break;
1564     }
1565     }
1566     ++sh;
1567   }
1568
1569   // Sort section relocation lists by index.
1570   for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1571                                      e = SectionRelocMap.end(); i != e; ++i) {
1572     std::sort(i->second.begin(), i->second.end());
1573   }
1574
1575   // Get string table sections.
1576   dot_shstrtab_sec = getSection(getStringTableIndex());
1577   if (dot_shstrtab_sec) {
1578     // Verify that the last byte in the string table in a null.
1579     VerifyStrTab(dot_shstrtab_sec);
1580   }
1581
1582   // Merge this into the above loop.
1583   for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
1584                   *e = i + getNumSections() * Header->e_shentsize;
1585                    i != e; i += Header->e_shentsize) {
1586     const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
1587     if (sh->sh_type == ELF::SHT_STRTAB) {
1588       StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
1589       if (SectionName == ".strtab") {
1590         if (dot_strtab_sec != 0)
1591           // FIXME: Proper error handling.
1592           report_fatal_error("Already found section named .strtab!");
1593         dot_strtab_sec = sh;
1594         VerifyStrTab(dot_strtab_sec);
1595       } else if (SectionName == ".dynstr") {
1596         if (dot_dynstr_sec != 0)
1597           // FIXME: Proper error handling.
1598           report_fatal_error("Already found section named .dynstr!");
1599         dot_dynstr_sec = sh;
1600         VerifyStrTab(dot_dynstr_sec);
1601       }
1602     }
1603   }
1604
1605   // Build symbol name side-mapping if there is one.
1606   if (SymbolTableSectionHeaderIndex) {
1607     const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
1608                                       SymbolTableSectionHeaderIndex->sh_offset);
1609     error_code ec;
1610     for (symbol_iterator si = begin_symbols(),
1611                          se = end_symbols(); si != se; si.increment(ec)) {
1612       if (ec)
1613         report_fatal_error("Fewer extended symbol table entries than symbols!");
1614       if (*ShndxTable != ELF::SHN_UNDEF)
1615         ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
1616       ++ShndxTable;
1617     }
1618   }
1619 }
1620
1621 template<support::endianness target_endianness, bool is64Bits>
1622 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1623                              ::begin_symbols() const {
1624   DataRefImpl SymbolData;
1625   if (SymbolTableSections.size() <= 1) {
1626     SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1627     SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1628   } else {
1629     SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1630     SymbolData.d.b = 1; // The 0th table is .dynsym
1631   }
1632   return symbol_iterator(SymbolRef(SymbolData, this));
1633 }
1634
1635 template<support::endianness target_endianness, bool is64Bits>
1636 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1637                              ::end_symbols() const {
1638   DataRefImpl SymbolData;
1639   SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1640   SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1641   return symbol_iterator(SymbolRef(SymbolData, this));
1642 }
1643
1644 template<support::endianness target_endianness, bool is64Bits>
1645 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1646                              ::begin_dynamic_symbols() const {
1647   DataRefImpl SymbolData;
1648   if (SymbolTableSections[0] == NULL) {
1649     SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1650     SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1651   } else {
1652     SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1653     SymbolData.d.b = 0; // The 0th table is .dynsym
1654   }
1655   return symbol_iterator(SymbolRef(SymbolData, this));
1656 }
1657
1658 template<support::endianness target_endianness, bool is64Bits>
1659 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1660                              ::end_dynamic_symbols() const {
1661   DataRefImpl SymbolData;
1662   SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1663   SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1664   return symbol_iterator(SymbolRef(SymbolData, this));
1665 }
1666
1667 template<support::endianness target_endianness, bool is64Bits>
1668 section_iterator ELFObjectFile<target_endianness, is64Bits>
1669                               ::begin_sections() const {
1670   DataRefImpl ret;
1671   ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
1672   return section_iterator(SectionRef(ret, this));
1673 }
1674
1675 template<support::endianness target_endianness, bool is64Bits>
1676 section_iterator ELFObjectFile<target_endianness, is64Bits>
1677                               ::end_sections() const {
1678   DataRefImpl ret;
1679   ret.p = reinterpret_cast<intptr_t>(base()
1680                                      + Header->e_shoff
1681                                      + (Header->e_shentsize*getNumSections()));
1682   return section_iterator(SectionRef(ret, this));
1683 }
1684
1685 template<support::endianness target_endianness, bool is64Bits>
1686 typename ELFObjectFile<target_endianness, is64Bits>::dyn_iterator
1687 ELFObjectFile<target_endianness, is64Bits>::begin_dynamic_table() const {
1688   DataRefImpl DynData;
1689   if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
1690     DynData.d.a = std::numeric_limits<uint32_t>::max();
1691   } else {
1692     DynData.d.a = 0;
1693   }
1694   return dyn_iterator(DynRef(DynData, this));
1695 }
1696
1697 template<support::endianness target_endianness, bool is64Bits>
1698 typename ELFObjectFile<target_endianness, is64Bits>::dyn_iterator
1699 ELFObjectFile<target_endianness, is64Bits>
1700                           ::end_dynamic_table() const {
1701   DataRefImpl DynData;
1702   DynData.d.a = std::numeric_limits<uint32_t>::max();
1703   return dyn_iterator(DynRef(DynData, this));
1704 }
1705
1706 template<support::endianness target_endianness, bool is64Bits>
1707 error_code ELFObjectFile<target_endianness, is64Bits>
1708                         ::getDynNext(DataRefImpl DynData,
1709                                      DynRef &Result) const {
1710   ++DynData.d.a;
1711
1712   // Check to see if we are at the end of .dynamic
1713   if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) {
1714     // We are at the end. Return the terminator.
1715     DynData.d.a = std::numeric_limits<uint32_t>::max();
1716   }
1717
1718   Result = DynRef(DynData, this);
1719   return object_error::success;
1720 }
1721
1722 template<support::endianness target_endianness, bool is64Bits>
1723 StringRef
1724 ELFObjectFile<target_endianness, is64Bits>::getLoadName() const {
1725   if (!dt_soname) {
1726     // Find the DT_SONAME entry
1727     dyn_iterator it = begin_dynamic_table();
1728     dyn_iterator ie = end_dynamic_table();
1729     error_code ec;
1730     while (it != ie) {
1731       if (it->getTag() == ELF::DT_SONAME)
1732         break;
1733       it.increment(ec);
1734       if (ec)
1735         report_fatal_error("dynamic table iteration failed");
1736     }
1737     if (it != ie) {
1738       if (dot_dynstr_sec == NULL)
1739         report_fatal_error("Dynamic string table is missing");
1740       dt_soname = getString(dot_dynstr_sec, it->getVal());
1741     } else {
1742       dt_soname = "";
1743     }
1744   }
1745   return dt_soname;
1746 }
1747
1748 template<support::endianness target_endianness, bool is64Bits>
1749 library_iterator ELFObjectFile<target_endianness, is64Bits>
1750                              ::begin_libraries_needed() const {
1751   // Find the first DT_NEEDED entry
1752   dyn_iterator i = begin_dynamic_table();
1753   dyn_iterator e = end_dynamic_table();
1754   error_code ec;
1755   while (i != e) {
1756     if (i->getTag() == ELF::DT_NEEDED)
1757       break;
1758     i.increment(ec);
1759     if (ec)
1760       report_fatal_error("dynamic table iteration failed");
1761   }
1762   // Use the same DataRefImpl format as DynRef.
1763   return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
1764 }
1765
1766 template<support::endianness target_endianness, bool is64Bits>
1767 error_code ELFObjectFile<target_endianness, is64Bits>
1768                         ::getLibraryNext(DataRefImpl Data,
1769                                          LibraryRef &Result) const {
1770   // Use the same DataRefImpl format as DynRef.
1771   dyn_iterator i = dyn_iterator(DynRef(Data, this));
1772   dyn_iterator e = end_dynamic_table();
1773
1774   // Skip the current dynamic table entry.
1775   error_code ec;
1776   if (i != e) {
1777     i.increment(ec);
1778     // TODO: proper error handling
1779     if (ec)
1780       report_fatal_error("dynamic table iteration failed");
1781   }
1782
1783   // Find the next DT_NEEDED entry.
1784   while (i != e) {
1785     if (i->getTag() == ELF::DT_NEEDED)
1786       break;
1787     i.increment(ec);
1788     if (ec)
1789       report_fatal_error("dynamic table iteration failed");
1790   }
1791   Result = LibraryRef(i->getRawDataRefImpl(), this);
1792   return object_error::success;
1793 }
1794
1795 template<support::endianness target_endianness, bool is64Bits>
1796 error_code ELFObjectFile<target_endianness, is64Bits>
1797          ::getLibraryPath(DataRefImpl Data, StringRef &Res) const {
1798   dyn_iterator i = dyn_iterator(DynRef(Data, this));
1799   if (i == end_dynamic_table())
1800     report_fatal_error("getLibraryPath() called on iterator end");
1801
1802   if (i->getTag() != ELF::DT_NEEDED)
1803     report_fatal_error("Invalid library_iterator");
1804
1805   // This uses .dynstr to lookup the name of the DT_NEEDED entry.
1806   // THis works as long as DT_STRTAB == .dynstr. This is true most of
1807   // the time, but the specification allows exceptions.
1808   // TODO: This should really use DT_STRTAB instead. Doing this requires
1809   // reading the program headers.
1810   if (dot_dynstr_sec == NULL)
1811     report_fatal_error("Dynamic string table is missing");
1812   Res = getString(dot_dynstr_sec, i->getVal());
1813   return object_error::success;
1814 }
1815
1816 template<support::endianness target_endianness, bool is64Bits>
1817 library_iterator ELFObjectFile<target_endianness, is64Bits>
1818                              ::end_libraries_needed() const {
1819   dyn_iterator e = end_dynamic_table();
1820   // Use the same DataRefImpl format as DynRef.
1821   return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
1822 }
1823
1824 template<support::endianness target_endianness, bool is64Bits>
1825 uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
1826   return is64Bits ? 8 : 4;
1827 }
1828
1829 template<support::endianness target_endianness, bool is64Bits>
1830 StringRef ELFObjectFile<target_endianness, is64Bits>
1831                        ::getFileFormatName() const {
1832   switch(Header->e_ident[ELF::EI_CLASS]) {
1833   case ELF::ELFCLASS32:
1834     switch(Header->e_machine) {
1835     case ELF::EM_386:
1836       return "ELF32-i386";
1837     case ELF::EM_X86_64:
1838       return "ELF32-x86-64";
1839     case ELF::EM_ARM:
1840       return "ELF32-arm";
1841     default:
1842       return "ELF32-unknown";
1843     }
1844   case ELF::ELFCLASS64:
1845     switch(Header->e_machine) {
1846     case ELF::EM_386:
1847       return "ELF64-i386";
1848     case ELF::EM_X86_64:
1849       return "ELF64-x86-64";
1850     default:
1851       return "ELF64-unknown";
1852     }
1853   default:
1854     // FIXME: Proper error handling.
1855     report_fatal_error("Invalid ELFCLASS!");
1856   }
1857 }
1858
1859 template<support::endianness target_endianness, bool is64Bits>
1860 unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
1861   switch(Header->e_machine) {
1862   case ELF::EM_386:
1863     return Triple::x86;
1864   case ELF::EM_X86_64:
1865     return Triple::x86_64;
1866   case ELF::EM_ARM:
1867     return Triple::arm;
1868   default:
1869     return Triple::UnknownArch;
1870   }
1871 }
1872
1873 template<support::endianness target_endianness, bool is64Bits>
1874 uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const {
1875   assert(Header && "Header not initialized!");
1876   if (Header->e_shnum == ELF::SHN_UNDEF) {
1877     assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
1878     return SectionHeaderTable->sh_size;
1879   }
1880   return Header->e_shnum;
1881 }
1882
1883 template<support::endianness target_endianness, bool is64Bits>
1884 uint64_t
1885 ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const {
1886   if (Header->e_shnum == ELF::SHN_UNDEF) {
1887     if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
1888       return SectionHeaderTable->sh_link;
1889     if (Header->e_shstrndx >= getNumSections())
1890       return 0;
1891   }
1892   return Header->e_shstrndx;
1893 }
1894
1895
1896 template<support::endianness target_endianness, bool is64Bits>
1897 template<typename T>
1898 inline const T *
1899 ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
1900                                                      uint32_t Entry) const {
1901   return getEntry<T>(getSection(Section), Entry);
1902 }
1903
1904 template<support::endianness target_endianness, bool is64Bits>
1905 template<typename T>
1906 inline const T *
1907 ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
1908                                                      uint32_t Entry) const {
1909   return reinterpret_cast<const T *>(
1910            base()
1911            + Section->sh_offset
1912            + (Entry * Section->sh_entsize));
1913 }
1914
1915 template<support::endianness target_endianness, bool is64Bits>
1916 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
1917 ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
1918   return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
1919 }
1920
1921 template<support::endianness target_endianness, bool is64Bits>
1922 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Dyn *
1923 ELFObjectFile<target_endianness, is64Bits>::getDyn(DataRefImpl DynData) const {
1924   return getEntry<Elf_Dyn>(dot_dynamic_sec, DynData.d.a);
1925 }
1926
1927 template<support::endianness target_endianness, bool is64Bits>
1928 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
1929 ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
1930   return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
1931 }
1932
1933 template<support::endianness target_endianness, bool is64Bits>
1934 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
1935 ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
1936   return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
1937 }
1938
1939 template<support::endianness target_endianness, bool is64Bits>
1940 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1941 ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
1942   const Elf_Shdr *sec = getSection(Symb.d.b);
1943   if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
1944     // FIXME: Proper error handling.
1945     report_fatal_error("Invalid symbol table section!");
1946   return sec;
1947 }
1948
1949 template<support::endianness target_endianness, bool is64Bits>
1950 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1951 ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const {
1952   if (index == 0)
1953     return 0;
1954   if (!SectionHeaderTable || index >= getNumSections())
1955     // FIXME: Proper error handling.
1956     report_fatal_error("Invalid section index!");
1957
1958   return reinterpret_cast<const Elf_Shdr *>(
1959          reinterpret_cast<const char *>(SectionHeaderTable)
1960          + (index * Header->e_shentsize));
1961 }
1962
1963 template<support::endianness target_endianness, bool is64Bits>
1964 const char *ELFObjectFile<target_endianness, is64Bits>
1965                          ::getString(uint32_t section,
1966                                      ELF::Elf32_Word offset) const {
1967   return getString(getSection(section), offset);
1968 }
1969
1970 template<support::endianness target_endianness, bool is64Bits>
1971 const char *ELFObjectFile<target_endianness, is64Bits>
1972                          ::getString(const Elf_Shdr *section,
1973                                      ELF::Elf32_Word offset) const {
1974   assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
1975   if (offset >= section->sh_size)
1976     // FIXME: Proper error handling.
1977     report_fatal_error("Symbol name offset outside of string table!");
1978   return (const char *)base() + section->sh_offset + offset;
1979 }
1980
1981 template<support::endianness target_endianness, bool is64Bits>
1982 error_code ELFObjectFile<target_endianness, is64Bits>
1983                         ::getSymbolName(const Elf_Shdr *section,
1984                                         const Elf_Sym *symb,
1985                                         StringRef &Result) const {
1986   if (symb->st_name == 0) {
1987     const Elf_Shdr *section = getSection(symb);
1988     if (!section)
1989       Result = "";
1990     else
1991       Result = getString(dot_shstrtab_sec, section->sh_name);
1992     return object_error::success;
1993   }
1994
1995   if (section == SymbolTableSections[0]) {
1996     // Symbol is in .dynsym, use .dynstr string table
1997     Result = getString(dot_dynstr_sec, symb->st_name);
1998   } else {
1999     // Use the default symbol table name section.
2000     Result = getString(dot_strtab_sec, symb->st_name);
2001   }
2002   return object_error::success;
2003 }
2004
2005 template<support::endianness target_endianness, bool is64Bits>
2006 error_code ELFObjectFile<target_endianness, is64Bits>
2007                         ::getSymbolVersion(const Elf_Shdr *section,
2008                                            const Elf_Sym *symb,
2009                                            StringRef &Version,
2010                                            bool &IsDefault) const {
2011   // Handle non-dynamic symbols.
2012   if (section != SymbolTableSections[0]) {
2013     // Non-dynamic symbols can have versions in their names
2014     // A name of the form 'foo@V1' indicates version 'V1', non-default.
2015     // A name of the form 'foo@@V2' indicates version 'V2', default version.
2016     StringRef Name;
2017     error_code ec = getSymbolName(section, symb, Name);
2018     if (ec != object_error::success)
2019       return ec;
2020     size_t atpos = Name.find('@');
2021     if (atpos == StringRef::npos) {
2022       Version = "";
2023       IsDefault = false;
2024       return object_error::success;
2025     }
2026     ++atpos;
2027     if (atpos < Name.size() && Name[atpos] == '@') {
2028       IsDefault = true;
2029       ++atpos;
2030     } else {
2031       IsDefault = false;
2032     }
2033     Version = Name.substr(atpos);
2034     return object_error::success;
2035   }
2036
2037   // This is a dynamic symbol. Look in the GNU symbol version table.
2038   if (dot_gnu_version_sec == NULL) {
2039     // No version table.
2040     Version = "";
2041     IsDefault = false;
2042     return object_error::success;
2043   }
2044
2045   // Determine the position in the symbol table of this entry.
2046   const char *sec_start = (const char*)base() + section->sh_offset;
2047   size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
2048
2049   // Get the corresponding version index entry
2050   const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
2051   size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
2052
2053   // Special markers for unversioned symbols.
2054   if (version_index == ELF::VER_NDX_LOCAL ||
2055       version_index == ELF::VER_NDX_GLOBAL) {
2056     Version = "";
2057     IsDefault = false;
2058     return object_error::success;
2059   }
2060
2061   // Lookup this symbol in the version table
2062   LoadVersionMap();
2063   if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2064     report_fatal_error("Symbol has version index without corresponding "
2065                        "define or reference entry");
2066   const VersionMapEntry &entry = VersionMap[version_index];
2067
2068   // Get the version name string
2069   size_t name_offset;
2070   if (entry.isVerdef()) {
2071     // The first Verdaux entry holds the name.
2072     name_offset = entry.getVerdef()->getAux()->vda_name;
2073   } else {
2074     name_offset = entry.getVernaux()->vna_name;
2075   }
2076   Version = getString(dot_dynstr_sec, name_offset);
2077
2078   // Set IsDefault
2079   if (entry.isVerdef()) {
2080     IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
2081   } else {
2082     IsDefault = false;
2083   }
2084
2085   return object_error::success;
2086 }
2087
2088 template<support::endianness target_endianness, bool is64Bits>
2089 inline DynRefImpl<target_endianness, is64Bits>
2090                  ::DynRefImpl(DataRefImpl DynP, const OwningType *Owner)
2091   : DynPimpl(DynP)
2092   , OwningObject(Owner) {}
2093
2094 template<support::endianness target_endianness, bool is64Bits>
2095 inline bool DynRefImpl<target_endianness, is64Bits>
2096                       ::operator==(const DynRefImpl &Other) const {
2097   return DynPimpl == Other.DynPimpl;
2098 }
2099
2100 template<support::endianness target_endianness, bool is64Bits>
2101 inline bool DynRefImpl<target_endianness, is64Bits>
2102                       ::operator <(const DynRefImpl &Other) const {
2103   return DynPimpl < Other.DynPimpl;
2104 }
2105
2106 template<support::endianness target_endianness, bool is64Bits>
2107 inline error_code DynRefImpl<target_endianness, is64Bits>
2108                             ::getNext(DynRefImpl &Result) const {
2109   return OwningObject->getDynNext(DynPimpl, Result);
2110 }
2111
2112 template<support::endianness target_endianness, bool is64Bits>
2113 inline int64_t DynRefImpl<target_endianness, is64Bits>
2114                             ::getTag() const {
2115   return OwningObject->getDyn(DynPimpl)->d_tag;
2116 }
2117
2118 template<support::endianness target_endianness, bool is64Bits>
2119 inline uint64_t DynRefImpl<target_endianness, is64Bits>
2120                             ::getVal() const {
2121   return OwningObject->getDyn(DynPimpl)->d_un.d_val;
2122 }
2123
2124 template<support::endianness target_endianness, bool is64Bits>
2125 inline uint64_t DynRefImpl<target_endianness, is64Bits>
2126                             ::getPtr() const {
2127   return OwningObject->getDyn(DynPimpl)->d_un.d_ptr;
2128 }
2129
2130 template<support::endianness target_endianness, bool is64Bits>
2131 inline DataRefImpl DynRefImpl<target_endianness, is64Bits>
2132                              ::getRawDataRefImpl() const {
2133   return DynPimpl;
2134 }
2135
2136 /// This is a generic interface for retrieving GNU symbol version
2137 /// information from an ELFObjectFile.
2138 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
2139                                              const SymbolRef &Sym,
2140                                              StringRef &Version,
2141                                              bool &IsDefault) {
2142   // Little-endian 32-bit
2143   if (const ELFObjectFile<support::little, false> *ELFObj =
2144           dyn_cast<ELFObjectFile<support::little, false> >(Obj))
2145     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2146
2147   // Big-endian 32-bit
2148   if (const ELFObjectFile<support::big, false> *ELFObj =
2149           dyn_cast<ELFObjectFile<support::big, false> >(Obj))
2150     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2151
2152   // Little-endian 64-bit
2153   if (const ELFObjectFile<support::little, true> *ELFObj =
2154           dyn_cast<ELFObjectFile<support::little, true> >(Obj))
2155     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2156
2157   // Big-endian 64-bit
2158   if (const ELFObjectFile<support::big, true> *ELFObj =
2159           dyn_cast<ELFObjectFile<support::big, true> >(Obj))
2160     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2161
2162   llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");
2163 }
2164
2165 }
2166 }
2167
2168 #endif