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