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