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