Add PowerPC64 definitions for ELF.h
[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     bool IsRelocatable;
955     switch(Header->e_type) {
956     case ELF::ET_EXEC:
957     case ELF::ET_DYN:
958       IsRelocatable = false;
959       break;
960     default:
961       IsRelocatable = true;
962     }
963     Result = symb->st_value;
964     if (IsRelocatable && Section != 0)
965       Result += Section->sh_addr;
966     return object_error::success;
967   default:
968     Result = UnknownAddressOrSize;
969     return object_error::success;
970   }
971 }
972
973 template<support::endianness target_endianness, bool is64Bits>
974 error_code ELFObjectFile<target_endianness, is64Bits>
975                         ::getSymbolSize(DataRefImpl Symb,
976                                         uint64_t &Result) const {
977   validateSymbol(Symb);
978   const Elf_Sym  *symb = getSymbol(Symb);
979   if (symb->st_size == 0)
980     Result = UnknownAddressOrSize;
981   Result = symb->st_size;
982   return object_error::success;
983 }
984
985 template<support::endianness target_endianness, bool is64Bits>
986 error_code ELFObjectFile<target_endianness, is64Bits>
987                         ::getSymbolNMTypeChar(DataRefImpl Symb,
988                                               char &Result) const {
989   validateSymbol(Symb);
990   const Elf_Sym  *symb = getSymbol(Symb);
991   const Elf_Shdr *Section = getSection(symb);
992
993   char ret = '?';
994
995   if (Section) {
996     switch (Section->sh_type) {
997     case ELF::SHT_PROGBITS:
998     case ELF::SHT_DYNAMIC:
999       switch (Section->sh_flags) {
1000       case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
1001         ret = 't'; break;
1002       case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
1003         ret = 'd'; break;
1004       case ELF::SHF_ALLOC:
1005       case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
1006       case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
1007         ret = 'r'; break;
1008       }
1009       break;
1010     case ELF::SHT_NOBITS: ret = 'b';
1011     }
1012   }
1013
1014   switch (getSymbolTableIndex(symb)) {
1015   case ELF::SHN_UNDEF:
1016     if (ret == '?')
1017       ret = 'U';
1018     break;
1019   case ELF::SHN_ABS: ret = 'a'; break;
1020   case ELF::SHN_COMMON: ret = 'c'; break;
1021   }
1022
1023   switch (symb->getBinding()) {
1024   case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
1025   case ELF::STB_WEAK:
1026     if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1027       ret = 'w';
1028     else
1029       if (symb->getType() == ELF::STT_OBJECT)
1030         ret = 'V';
1031       else
1032         ret = 'W';
1033   }
1034
1035   if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
1036     StringRef name;
1037     if (error_code ec = getSymbolName(Symb, name))
1038       return ec;
1039     Result = StringSwitch<char>(name)
1040       .StartsWith(".debug", 'N')
1041       .StartsWith(".note", 'n')
1042       .Default('?');
1043     return object_error::success;
1044   }
1045
1046   Result = ret;
1047   return object_error::success;
1048 }
1049
1050 template<support::endianness target_endianness, bool is64Bits>
1051 error_code ELFObjectFile<target_endianness, is64Bits>
1052                         ::getSymbolType(DataRefImpl Symb,
1053                                         SymbolRef::Type &Result) const {
1054   validateSymbol(Symb);
1055   const Elf_Sym  *symb = getSymbol(Symb);
1056
1057   switch (symb->getType()) {
1058   case ELF::STT_NOTYPE:
1059     Result = SymbolRef::ST_Unknown;
1060     break;
1061   case ELF::STT_SECTION:
1062     Result = SymbolRef::ST_Debug;
1063     break;
1064   case ELF::STT_FILE:
1065     Result = SymbolRef::ST_File;
1066     break;
1067   case ELF::STT_FUNC:
1068     Result = SymbolRef::ST_Function;
1069     break;
1070   case ELF::STT_OBJECT:
1071   case ELF::STT_COMMON:
1072   case ELF::STT_TLS:
1073     Result = SymbolRef::ST_Data;
1074     break;
1075   default:
1076     Result = SymbolRef::ST_Other;
1077     break;
1078   }
1079   return object_error::success;
1080 }
1081
1082 template<support::endianness target_endianness, bool is64Bits>
1083 error_code ELFObjectFile<target_endianness, is64Bits>
1084                         ::getSymbolFlags(DataRefImpl Symb,
1085                                          uint32_t &Result) const {
1086   validateSymbol(Symb);
1087   const Elf_Sym  *symb = getSymbol(Symb);
1088
1089   Result = SymbolRef::SF_None;
1090
1091   if (symb->getBinding() != ELF::STB_LOCAL)
1092     Result |= SymbolRef::SF_Global;
1093
1094   if (symb->getBinding() == ELF::STB_WEAK)
1095     Result |= SymbolRef::SF_Weak;
1096
1097   if (symb->st_shndx == ELF::SHN_ABS)
1098     Result |= SymbolRef::SF_Absolute;
1099
1100   if (symb->getType() == ELF::STT_FILE ||
1101       symb->getType() == ELF::STT_SECTION)
1102     Result |= SymbolRef::SF_FormatSpecific;
1103
1104   if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1105     Result |= SymbolRef::SF_Undefined;
1106
1107   if (symb->getType() == ELF::STT_COMMON ||
1108       getSymbolTableIndex(symb) == ELF::SHN_COMMON)
1109     Result |= SymbolRef::SF_Common;
1110
1111   if (symb->getType() == ELF::STT_TLS)
1112     Result |= SymbolRef::SF_ThreadLocal;
1113
1114   return object_error::success;
1115 }
1116
1117 template<support::endianness target_endianness, bool is64Bits>
1118 error_code ELFObjectFile<target_endianness, is64Bits>
1119                         ::getSymbolSection(DataRefImpl Symb,
1120                                            section_iterator &Res) const {
1121   validateSymbol(Symb);
1122   const Elf_Sym  *symb = getSymbol(Symb);
1123   const Elf_Shdr *sec = getSection(symb);
1124   if (!sec)
1125     Res = end_sections();
1126   else {
1127     DataRefImpl Sec;
1128     Sec.p = reinterpret_cast<intptr_t>(sec);
1129     Res = section_iterator(SectionRef(Sec, this));
1130   }
1131   return object_error::success;
1132 }
1133
1134 template<support::endianness target_endianness, bool is64Bits>
1135 error_code ELFObjectFile<target_endianness, is64Bits>
1136                         ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
1137   const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
1138   sec += Header->e_shentsize;
1139   Sec.p = reinterpret_cast<intptr_t>(sec);
1140   Result = SectionRef(Sec, this);
1141   return object_error::success;
1142 }
1143
1144 template<support::endianness target_endianness, bool is64Bits>
1145 error_code ELFObjectFile<target_endianness, is64Bits>
1146                         ::getSectionName(DataRefImpl Sec,
1147                                          StringRef &Result) const {
1148   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1149   Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
1150   return object_error::success;
1151 }
1152
1153 template<support::endianness target_endianness, bool is64Bits>
1154 error_code ELFObjectFile<target_endianness, is64Bits>
1155                         ::getSectionAddress(DataRefImpl Sec,
1156                                             uint64_t &Result) const {
1157   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1158   Result = sec->sh_addr;
1159   return object_error::success;
1160 }
1161
1162 template<support::endianness target_endianness, bool is64Bits>
1163 error_code ELFObjectFile<target_endianness, is64Bits>
1164                         ::getSectionSize(DataRefImpl Sec,
1165                                          uint64_t &Result) const {
1166   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1167   Result = sec->sh_size;
1168   return object_error::success;
1169 }
1170
1171 template<support::endianness target_endianness, bool is64Bits>
1172 error_code ELFObjectFile<target_endianness, is64Bits>
1173                         ::getSectionContents(DataRefImpl Sec,
1174                                              StringRef &Result) const {
1175   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1176   const char *start = (const char*)base() + sec->sh_offset;
1177   Result = StringRef(start, sec->sh_size);
1178   return object_error::success;
1179 }
1180
1181 template<support::endianness target_endianness, bool is64Bits>
1182 error_code ELFObjectFile<target_endianness, is64Bits>
1183                         ::getSectionContents(const Elf_Shdr *Sec,
1184                                              StringRef &Result) const {
1185   const char *start = (const char*)base() + Sec->sh_offset;
1186   Result = StringRef(start, Sec->sh_size);
1187   return object_error::success;
1188 }
1189
1190 template<support::endianness target_endianness, bool is64Bits>
1191 error_code ELFObjectFile<target_endianness, is64Bits>
1192                         ::getSectionAlignment(DataRefImpl Sec,
1193                                               uint64_t &Result) const {
1194   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1195   Result = sec->sh_addralign;
1196   return object_error::success;
1197 }
1198
1199 template<support::endianness target_endianness, bool is64Bits>
1200 error_code ELFObjectFile<target_endianness, is64Bits>
1201                         ::isSectionText(DataRefImpl Sec,
1202                                         bool &Result) const {
1203   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1204   if (sec->sh_flags & ELF::SHF_EXECINSTR)
1205     Result = true;
1206   else
1207     Result = false;
1208   return object_error::success;
1209 }
1210
1211 template<support::endianness target_endianness, bool is64Bits>
1212 error_code ELFObjectFile<target_endianness, is64Bits>
1213                         ::isSectionData(DataRefImpl Sec,
1214                                         bool &Result) const {
1215   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1216   if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1217       && sec->sh_type == ELF::SHT_PROGBITS)
1218     Result = true;
1219   else
1220     Result = false;
1221   return object_error::success;
1222 }
1223
1224 template<support::endianness target_endianness, bool is64Bits>
1225 error_code ELFObjectFile<target_endianness, is64Bits>
1226                         ::isSectionBSS(DataRefImpl Sec,
1227                                        bool &Result) const {
1228   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1229   if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1230       && sec->sh_type == ELF::SHT_NOBITS)
1231     Result = true;
1232   else
1233     Result = false;
1234   return object_error::success;
1235 }
1236
1237 template<support::endianness target_endianness, bool is64Bits>
1238 error_code ELFObjectFile<target_endianness, is64Bits>
1239                         ::isSectionRequiredForExecution(DataRefImpl Sec,
1240                                                         bool &Result) const {
1241   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1242   if (sec->sh_flags & ELF::SHF_ALLOC)
1243     Result = true;
1244   else
1245     Result = false;
1246   return object_error::success;
1247 }
1248
1249 template<support::endianness target_endianness, bool is64Bits>
1250 error_code ELFObjectFile<target_endianness, is64Bits>
1251                         ::isSectionVirtual(DataRefImpl Sec,
1252                                            bool &Result) const {
1253   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1254   if (sec->sh_type == ELF::SHT_NOBITS)
1255     Result = true;
1256   else
1257     Result = false;
1258   return object_error::success;
1259 }
1260
1261 template<support::endianness target_endianness, bool is64Bits>
1262 error_code ELFObjectFile<target_endianness, is64Bits>::isSectionZeroInit(DataRefImpl Sec,
1263                                             bool &Result) const {
1264   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1265   // For ELF, all zero-init sections are virtual (that is, they occupy no space
1266   //   in the object image) and vice versa.
1267   if (sec->sh_flags & ELF::SHT_NOBITS)
1268     Result = true;
1269   else
1270     Result = false;
1271   return object_error::success;
1272 }
1273
1274 template<support::endianness target_endianness, bool is64Bits>
1275 error_code ELFObjectFile<target_endianness, is64Bits>
1276                           ::sectionContainsSymbol(DataRefImpl Sec,
1277                                                   DataRefImpl Symb,
1278                                                   bool &Result) const {
1279   // FIXME: Unimplemented.
1280   Result = false;
1281   return object_error::success;
1282 }
1283
1284 template<support::endianness target_endianness, bool is64Bits>
1285 relocation_iterator ELFObjectFile<target_endianness, is64Bits>
1286                                  ::getSectionRelBegin(DataRefImpl Sec) const {
1287   DataRefImpl RelData;
1288   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1289   typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1290   if (sec != 0 && ittr != SectionRelocMap.end()) {
1291     RelData.w.a = getSection(ittr->second[0])->sh_info;
1292     RelData.w.b = ittr->second[0];
1293     RelData.w.c = 0;
1294   }
1295   return relocation_iterator(RelocationRef(RelData, this));
1296 }
1297
1298 template<support::endianness target_endianness, bool is64Bits>
1299 relocation_iterator ELFObjectFile<target_endianness, is64Bits>
1300                                  ::getSectionRelEnd(DataRefImpl Sec) const {
1301   DataRefImpl RelData;
1302   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1303   typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1304   if (sec != 0 && ittr != SectionRelocMap.end()) {
1305     // Get the index of the last relocation section for this section.
1306     std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1307     const Elf_Shdr *relocsec = getSection(relocsecindex);
1308     RelData.w.a = relocsec->sh_info;
1309     RelData.w.b = relocsecindex;
1310     RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1311   }
1312   return relocation_iterator(RelocationRef(RelData, this));
1313 }
1314
1315 // Relocations
1316 template<support::endianness target_endianness, bool is64Bits>
1317 error_code ELFObjectFile<target_endianness, is64Bits>
1318                         ::getRelocationNext(DataRefImpl Rel,
1319                                             RelocationRef &Result) const {
1320   ++Rel.w.c;
1321   const Elf_Shdr *relocsec = getSection(Rel.w.b);
1322   if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1323     // We have reached the end of the relocations for this section. See if there
1324     // is another relocation section.
1325     typename RelocMap_t::mapped_type relocseclist =
1326       SectionRelocMap.lookup(getSection(Rel.w.a));
1327
1328     // Do a binary search for the current reloc section index (which must be
1329     // present). Then get the next one.
1330     typename RelocMap_t::mapped_type::const_iterator loc =
1331       std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1332     ++loc;
1333
1334     // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1335     // to the end iterator.
1336     if (loc != relocseclist.end()) {
1337       Rel.w.b = *loc;
1338       Rel.w.a = 0;
1339     }
1340   }
1341   Result = RelocationRef(Rel, this);
1342   return object_error::success;
1343 }
1344
1345 template<support::endianness target_endianness, bool is64Bits>
1346 error_code ELFObjectFile<target_endianness, is64Bits>
1347                         ::getRelocationSymbol(DataRefImpl Rel,
1348                                               SymbolRef &Result) const {
1349   uint32_t symbolIdx;
1350   const Elf_Shdr *sec = getSection(Rel.w.b);
1351   switch (sec->sh_type) {
1352     default :
1353       report_fatal_error("Invalid section type in Rel!");
1354     case ELF::SHT_REL : {
1355       symbolIdx = getRel(Rel)->getSymbol();
1356       break;
1357     }
1358     case ELF::SHT_RELA : {
1359       symbolIdx = getRela(Rel)->getSymbol();
1360       break;
1361     }
1362   }
1363   DataRefImpl SymbolData;
1364   IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1365   if (it == SymbolTableSectionsIndexMap.end())
1366     report_fatal_error("Relocation symbol table not found!");
1367   SymbolData.d.a = symbolIdx;
1368   SymbolData.d.b = it->second;
1369   Result = SymbolRef(SymbolData, this);
1370   return object_error::success;
1371 }
1372
1373 template<support::endianness target_endianness, bool is64Bits>
1374 error_code ELFObjectFile<target_endianness, is64Bits>
1375                         ::getRelocationAddress(DataRefImpl Rel,
1376                                                uint64_t &Result) const {
1377   uint64_t offset;
1378   const Elf_Shdr *sec = getSection(Rel.w.b);
1379   switch (sec->sh_type) {
1380     default :
1381       report_fatal_error("Invalid section type in Rel!");
1382     case ELF::SHT_REL : {
1383       offset = getRel(Rel)->r_offset;
1384       break;
1385     }
1386     case ELF::SHT_RELA : {
1387       offset = getRela(Rel)->r_offset;
1388       break;
1389     }
1390   }
1391
1392   Result = offset;
1393   return object_error::success;
1394 }
1395
1396 template<support::endianness target_endianness, bool is64Bits>
1397 error_code ELFObjectFile<target_endianness, is64Bits>
1398                         ::getRelocationOffset(DataRefImpl Rel,
1399                                               uint64_t &Result) const {
1400   uint64_t offset;
1401   const Elf_Shdr *sec = getSection(Rel.w.b);
1402   switch (sec->sh_type) {
1403     default :
1404       report_fatal_error("Invalid section type in Rel!");
1405     case ELF::SHT_REL : {
1406       offset = getRel(Rel)->r_offset;
1407       break;
1408     }
1409     case ELF::SHT_RELA : {
1410       offset = getRela(Rel)->r_offset;
1411       break;
1412     }
1413   }
1414
1415   Result = offset - sec->sh_addr;
1416   return object_error::success;
1417 }
1418
1419 template<support::endianness target_endianness, bool is64Bits>
1420 error_code ELFObjectFile<target_endianness, is64Bits>
1421                         ::getRelocationType(DataRefImpl Rel,
1422                                             uint64_t &Result) const {
1423   const Elf_Shdr *sec = getSection(Rel.w.b);
1424   switch (sec->sh_type) {
1425     default :
1426       report_fatal_error("Invalid section type in Rel!");
1427     case ELF::SHT_REL : {
1428       Result = getRel(Rel)->getType();
1429       break;
1430     }
1431     case ELF::SHT_RELA : {
1432       Result = getRela(Rel)->getType();
1433       break;
1434     }
1435   }
1436   return object_error::success;
1437 }
1438
1439 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1440   case ELF::enum: res = #enum; break;
1441
1442 template<support::endianness target_endianness, bool is64Bits>
1443 error_code ELFObjectFile<target_endianness, is64Bits>
1444                         ::getRelocationTypeName(DataRefImpl Rel,
1445                                           SmallVectorImpl<char> &Result) const {
1446   const Elf_Shdr *sec = getSection(Rel.w.b);
1447   uint8_t type;
1448   StringRef res;
1449   switch (sec->sh_type) {
1450     default :
1451       return object_error::parse_failed;
1452     case ELF::SHT_REL : {
1453       type = getRel(Rel)->getType();
1454       break;
1455     }
1456     case ELF::SHT_RELA : {
1457       type = getRela(Rel)->getType();
1458       break;
1459     }
1460   }
1461   switch (Header->e_machine) {
1462   case ELF::EM_X86_64:
1463     switch (type) {
1464       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1465       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1466       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1467       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1468       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1469       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1470       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1471       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1472       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1473       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1474       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1475       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1476       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1477       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1478       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1479       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1480       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1481       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1482       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1483       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1484       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1485       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1486       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1487       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1488       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1489       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1490       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1491       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1492       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1493       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1494       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1495       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1496     default:
1497       res = "Unknown";
1498     }
1499     break;
1500   case ELF::EM_386:
1501     switch (type) {
1502       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1503       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1504       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1505       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1506       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1507       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1508       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1509       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1510       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1511       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1512       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1513       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1514       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1515       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1516       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1517       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1518       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1519       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1520       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1521       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1522       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1523       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1524       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1525       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1526       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1527       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1528       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1529       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1530       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1531       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1532       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1533       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1534       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1535       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1536       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1537       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1538       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1539       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1540       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1541       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1542     default:
1543       res = "Unknown";
1544     }
1545     break;
1546   case ELF::EM_ARM:
1547     switch (type) {
1548       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE);
1549       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24);
1550       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32);
1551       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32);
1552       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0);
1553       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16);
1554       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12);
1555       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5);
1556       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8);
1557       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32);
1558       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL);
1559       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8);
1560       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ);
1561       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC);
1562       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8);
1563       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25);
1564       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22);
1565       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32);
1566       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32);
1567       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32);
1568       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY);
1569       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT);
1570       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT);
1571       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE);
1572       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32);
1573       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL);
1574       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL);
1575       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32);
1576       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL);
1577       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24);
1578       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24);
1579       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS);
1580       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0);
1581       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8);
1582       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15);
1583       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC);
1584       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC);
1585       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK);
1586       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1);
1587       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31);
1588       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX);
1589       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2);
1590       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31);
1591       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC);
1592       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS);
1593       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC);
1594       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL);
1595       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC);
1596       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS);
1597       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC);
1598       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL);
1599       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19);
1600       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6);
1601       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0);
1602       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12);
1603       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI);
1604       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI);
1605       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC);
1606       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0);
1607       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC);
1608       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1);
1609       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2);
1610       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1);
1611       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2);
1612       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0);
1613       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1);
1614       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2);
1615       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0);
1616       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1);
1617       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2);
1618       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC);
1619       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0);
1620       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC);
1621       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1);
1622       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2);
1623       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0);
1624       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1);
1625       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2);
1626       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0);
1627       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1);
1628       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2);
1629       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0);
1630       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1);
1631       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2);
1632       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC);
1633       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL);
1634       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL);
1635       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC);
1636       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL);
1637       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL);
1638       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC);
1639       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL);
1640       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ);
1641       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL);
1642       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS);
1643       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS);
1644       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL);
1645       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12);
1646       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12);
1647       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX);
1648       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY);
1649       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT);
1650       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11);
1651       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8);
1652       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32);
1653       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32);
1654       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32);
1655       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32);
1656       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32);
1657       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12);
1658       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12);
1659       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP);
1660       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0);
1661       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1);
1662       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2);
1663       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3);
1664       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4);
1665       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5);
1666       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6);
1667       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7);
1668       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8);
1669       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9);
1670       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10);
1671       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11);
1672       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12);
1673       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13);
1674       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14);
1675       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15);
1676       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO);
1677       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16);
1678       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32);
1679     default:
1680       res = "Unknown";
1681     }
1682     break;
1683   case ELF::EM_HEXAGON:
1684     switch (type) {
1685       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE);
1686       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL);
1687       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL);
1688       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL);
1689       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16);
1690       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16);
1691       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32);
1692       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16);
1693       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8);
1694       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0);
1695       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1);
1696       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2);
1697       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3);
1698       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16);
1699       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL);
1700       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL);
1701       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X);
1702       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X);
1703       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X);
1704       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X);
1705       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X);
1706       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X);
1707       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X);
1708       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X);
1709       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X);
1710       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X);
1711       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X);
1712       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X);
1713       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X);
1714       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X);
1715       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X);
1716       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL);
1717       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY);
1718       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT);
1719       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT);
1720       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE);
1721       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL);
1722       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16);
1723       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16);
1724       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32);
1725       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16);
1726       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16);
1727       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32);
1728       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16);
1729       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32);
1730       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16);
1731       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16);
1732       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32);
1733       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16);
1734       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL);
1735       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16);
1736       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16);
1737       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32);
1738       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16);
1739       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16);
1740       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16);
1741       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32);
1742       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16);
1743       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16);
1744       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32);
1745       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16);
1746       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16);
1747       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16);
1748       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32);
1749       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16);
1750       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X);
1751       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X);
1752       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X);
1753       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X);
1754       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X);
1755       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X);
1756       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X);
1757       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X);
1758       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X);
1759       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X);
1760       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X);
1761       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X);
1762       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X);
1763       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X);
1764       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X);
1765       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X);
1766       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X);
1767       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X);
1768       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X);
1769       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X);
1770       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X);
1771     default:
1772       res = "Unknown";
1773     }
1774     break;
1775   default:
1776     res = "Unknown";
1777   }
1778   Result.append(res.begin(), res.end());
1779   return object_error::success;
1780 }
1781
1782 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1783
1784 template<support::endianness target_endianness, bool is64Bits>
1785 error_code ELFObjectFile<target_endianness, is64Bits>
1786                         ::getRelocationAdditionalInfo(DataRefImpl Rel,
1787                                                       int64_t &Result) const {
1788   const Elf_Shdr *sec = getSection(Rel.w.b);
1789   switch (sec->sh_type) {
1790     default :
1791       report_fatal_error("Invalid section type in Rel!");
1792     case ELF::SHT_REL : {
1793       Result = 0;
1794       return object_error::success;
1795     }
1796     case ELF::SHT_RELA : {
1797       Result = getRela(Rel)->r_addend;
1798       return object_error::success;
1799     }
1800   }
1801 }
1802
1803 template<support::endianness target_endianness, bool is64Bits>
1804 error_code ELFObjectFile<target_endianness, is64Bits>
1805                         ::getRelocationValueString(DataRefImpl Rel,
1806                                           SmallVectorImpl<char> &Result) const {
1807   const Elf_Shdr *sec = getSection(Rel.w.b);
1808   uint8_t type;
1809   StringRef res;
1810   int64_t addend = 0;
1811   uint16_t symbol_index = 0;
1812   switch (sec->sh_type) {
1813     default:
1814       return object_error::parse_failed;
1815     case ELF::SHT_REL: {
1816       type = getRel(Rel)->getType();
1817       symbol_index = getRel(Rel)->getSymbol();
1818       // TODO: Read implicit addend from section data.
1819       break;
1820     }
1821     case ELF::SHT_RELA: {
1822       type = getRela(Rel)->getType();
1823       symbol_index = getRela(Rel)->getSymbol();
1824       addend = getRela(Rel)->r_addend;
1825       break;
1826     }
1827   }
1828   const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1829   StringRef symname;
1830   if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1831     return ec;
1832   switch (Header->e_machine) {
1833   case ELF::EM_X86_64:
1834     switch (type) {
1835     case ELF::R_X86_64_PC8:
1836     case ELF::R_X86_64_PC16:
1837     case ELF::R_X86_64_PC32: {
1838         std::string fmtbuf;
1839         raw_string_ostream fmt(fmtbuf);
1840         fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1841         fmt.flush();
1842         Result.append(fmtbuf.begin(), fmtbuf.end());
1843       }
1844       break;
1845     case ELF::R_X86_64_8:
1846     case ELF::R_X86_64_16:
1847     case ELF::R_X86_64_32:
1848     case ELF::R_X86_64_32S:
1849     case ELF::R_X86_64_64: {
1850         std::string fmtbuf;
1851         raw_string_ostream fmt(fmtbuf);
1852         fmt << symname << (addend < 0 ? "" : "+") << addend;
1853         fmt.flush();
1854         Result.append(fmtbuf.begin(), fmtbuf.end());
1855       }
1856       break;
1857     default:
1858       res = "Unknown";
1859     }
1860     break;
1861   case ELF::EM_ARM:
1862   case ELF::EM_HEXAGON:
1863     res = symname;
1864     break;
1865   default:
1866     res = "Unknown";
1867   }
1868   if (Result.empty())
1869     Result.append(res.begin(), res.end());
1870   return object_error::success;
1871 }
1872
1873 // Verify that the last byte in the string table in a null.
1874 template<support::endianness target_endianness, bool is64Bits>
1875 void ELFObjectFile<target_endianness, is64Bits>
1876                   ::VerifyStrTab(const Elf_Shdr *sh) const {
1877   const char *strtab = (const char*)base() + sh->sh_offset;
1878   if (strtab[sh->sh_size - 1] != 0)
1879     // FIXME: Proper error handling.
1880     report_fatal_error("String table must end with a null terminator!");
1881 }
1882
1883 template<support::endianness target_endianness, bool is64Bits>
1884 ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
1885                                                           , error_code &ec)
1886   : ObjectFile(getELFType(target_endianness == support::little, is64Bits),
1887                Object, ec)
1888   , isDyldELFObject(false)
1889   , SectionHeaderTable(0)
1890   , dot_shstrtab_sec(0)
1891   , dot_strtab_sec(0)
1892   , dot_dynstr_sec(0)
1893   , dot_dynamic_sec(0)
1894   , dot_gnu_version_sec(0)
1895   , dot_gnu_version_r_sec(0)
1896   , dot_gnu_version_d_sec(0)
1897   , dt_soname(0)
1898  {
1899
1900   const uint64_t FileSize = Data->getBufferSize();
1901
1902   if (sizeof(Elf_Ehdr) > FileSize)
1903     // FIXME: Proper error handling.
1904     report_fatal_error("File too short!");
1905
1906   Header = reinterpret_cast<const Elf_Ehdr *>(base());
1907
1908   if (Header->e_shoff == 0)
1909     return;
1910
1911   const uint64_t SectionTableOffset = Header->e_shoff;
1912
1913   if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
1914     // FIXME: Proper error handling.
1915     report_fatal_error("Section header table goes past end of file!");
1916
1917   // The getNumSections() call below depends on SectionHeaderTable being set.
1918   SectionHeaderTable =
1919     reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
1920   const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
1921
1922   if (SectionTableOffset + SectionTableSize > FileSize)
1923     // FIXME: Proper error handling.
1924     report_fatal_error("Section table goes past end of file!");
1925
1926   // To find the symbol tables we walk the section table to find SHT_SYMTAB.
1927   const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
1928   const Elf_Shdr* sh = SectionHeaderTable;
1929
1930   // Reserve SymbolTableSections[0] for .dynsym
1931   SymbolTableSections.push_back(NULL);
1932
1933   for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
1934     switch (sh->sh_type) {
1935     case ELF::SHT_SYMTAB_SHNDX: {
1936       if (SymbolTableSectionHeaderIndex)
1937         // FIXME: Proper error handling.
1938         report_fatal_error("More than one .symtab_shndx!");
1939       SymbolTableSectionHeaderIndex = sh;
1940       break;
1941     }
1942     case ELF::SHT_SYMTAB: {
1943       SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
1944       SymbolTableSections.push_back(sh);
1945       break;
1946     }
1947     case ELF::SHT_DYNSYM: {
1948       if (SymbolTableSections[0] != NULL)
1949         // FIXME: Proper error handling.
1950         report_fatal_error("More than one .dynsym!");
1951       SymbolTableSectionsIndexMap[i] = 0;
1952       SymbolTableSections[0] = sh;
1953       break;
1954     }
1955     case ELF::SHT_REL:
1956     case ELF::SHT_RELA: {
1957       SectionRelocMap[getSection(sh->sh_info)].push_back(i);
1958       break;
1959     }
1960     case ELF::SHT_DYNAMIC: {
1961       if (dot_dynamic_sec != NULL)
1962         // FIXME: Proper error handling.
1963         report_fatal_error("More than one .dynamic!");
1964       dot_dynamic_sec = sh;
1965       break;
1966     }
1967     case ELF::SHT_GNU_versym: {
1968       if (dot_gnu_version_sec != NULL)
1969         // FIXME: Proper error handling.
1970         report_fatal_error("More than one .gnu.version section!");
1971       dot_gnu_version_sec = sh;
1972       break;
1973     }
1974     case ELF::SHT_GNU_verdef: {
1975       if (dot_gnu_version_d_sec != NULL)
1976         // FIXME: Proper error handling.
1977         report_fatal_error("More than one .gnu.version_d section!");
1978       dot_gnu_version_d_sec = sh;
1979       break;
1980     }
1981     case ELF::SHT_GNU_verneed: {
1982       if (dot_gnu_version_r_sec != NULL)
1983         // FIXME: Proper error handling.
1984         report_fatal_error("More than one .gnu.version_r section!");
1985       dot_gnu_version_r_sec = sh;
1986       break;
1987     }
1988     }
1989     ++sh;
1990   }
1991
1992   // Sort section relocation lists by index.
1993   for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1994                                      e = SectionRelocMap.end(); i != e; ++i) {
1995     std::sort(i->second.begin(), i->second.end());
1996   }
1997
1998   // Get string table sections.
1999   dot_shstrtab_sec = getSection(getStringTableIndex());
2000   if (dot_shstrtab_sec) {
2001     // Verify that the last byte in the string table in a null.
2002     VerifyStrTab(dot_shstrtab_sec);
2003   }
2004
2005   // Merge this into the above loop.
2006   for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
2007                   *e = i + getNumSections() * Header->e_shentsize;
2008                    i != e; i += Header->e_shentsize) {
2009     const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
2010     if (sh->sh_type == ELF::SHT_STRTAB) {
2011       StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
2012       if (SectionName == ".strtab") {
2013         if (dot_strtab_sec != 0)
2014           // FIXME: Proper error handling.
2015           report_fatal_error("Already found section named .strtab!");
2016         dot_strtab_sec = sh;
2017         VerifyStrTab(dot_strtab_sec);
2018       } else if (SectionName == ".dynstr") {
2019         if (dot_dynstr_sec != 0)
2020           // FIXME: Proper error handling.
2021           report_fatal_error("Already found section named .dynstr!");
2022         dot_dynstr_sec = sh;
2023         VerifyStrTab(dot_dynstr_sec);
2024       }
2025     }
2026   }
2027
2028   // Build symbol name side-mapping if there is one.
2029   if (SymbolTableSectionHeaderIndex) {
2030     const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
2031                                       SymbolTableSectionHeaderIndex->sh_offset);
2032     error_code ec;
2033     for (symbol_iterator si = begin_symbols(),
2034                          se = end_symbols(); si != se; si.increment(ec)) {
2035       if (ec)
2036         report_fatal_error("Fewer extended symbol table entries than symbols!");
2037       if (*ShndxTable != ELF::SHN_UNDEF)
2038         ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
2039       ++ShndxTable;
2040     }
2041   }
2042 }
2043
2044 template<support::endianness target_endianness, bool is64Bits>
2045 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
2046                              ::begin_symbols() const {
2047   DataRefImpl SymbolData;
2048   if (SymbolTableSections.size() <= 1) {
2049     SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2050     SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2051   } else {
2052     SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2053     SymbolData.d.b = 1; // The 0th table is .dynsym
2054   }
2055   return symbol_iterator(SymbolRef(SymbolData, this));
2056 }
2057
2058 template<support::endianness target_endianness, bool is64Bits>
2059 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
2060                              ::end_symbols() const {
2061   DataRefImpl SymbolData;
2062   SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2063   SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2064   return symbol_iterator(SymbolRef(SymbolData, this));
2065 }
2066
2067 template<support::endianness target_endianness, bool is64Bits>
2068 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
2069                              ::begin_dynamic_symbols() const {
2070   DataRefImpl SymbolData;
2071   if (SymbolTableSections[0] == NULL) {
2072     SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2073     SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2074   } else {
2075     SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2076     SymbolData.d.b = 0; // The 0th table is .dynsym
2077   }
2078   return symbol_iterator(SymbolRef(SymbolData, this));
2079 }
2080
2081 template<support::endianness target_endianness, bool is64Bits>
2082 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
2083                              ::end_dynamic_symbols() const {
2084   DataRefImpl SymbolData;
2085   SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2086   SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2087   return symbol_iterator(SymbolRef(SymbolData, this));
2088 }
2089
2090 template<support::endianness target_endianness, bool is64Bits>
2091 section_iterator ELFObjectFile<target_endianness, is64Bits>
2092                               ::begin_sections() const {
2093   DataRefImpl ret;
2094   ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
2095   return section_iterator(SectionRef(ret, this));
2096 }
2097
2098 template<support::endianness target_endianness, bool is64Bits>
2099 section_iterator ELFObjectFile<target_endianness, is64Bits>
2100                               ::end_sections() const {
2101   DataRefImpl ret;
2102   ret.p = reinterpret_cast<intptr_t>(base()
2103                                      + Header->e_shoff
2104                                      + (Header->e_shentsize*getNumSections()));
2105   return section_iterator(SectionRef(ret, this));
2106 }
2107
2108 template<support::endianness target_endianness, bool is64Bits>
2109 typename ELFObjectFile<target_endianness, is64Bits>::dyn_iterator
2110 ELFObjectFile<target_endianness, is64Bits>::begin_dynamic_table() const {
2111   DataRefImpl DynData;
2112   if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
2113     DynData.d.a = std::numeric_limits<uint32_t>::max();
2114   } else {
2115     DynData.d.a = 0;
2116   }
2117   return dyn_iterator(DynRef(DynData, this));
2118 }
2119
2120 template<support::endianness target_endianness, bool is64Bits>
2121 typename ELFObjectFile<target_endianness, is64Bits>::dyn_iterator
2122 ELFObjectFile<target_endianness, is64Bits>
2123                           ::end_dynamic_table() const {
2124   DataRefImpl DynData;
2125   DynData.d.a = std::numeric_limits<uint32_t>::max();
2126   return dyn_iterator(DynRef(DynData, this));
2127 }
2128
2129 template<support::endianness target_endianness, bool is64Bits>
2130 error_code ELFObjectFile<target_endianness, is64Bits>
2131                         ::getDynNext(DataRefImpl DynData,
2132                                      DynRef &Result) const {
2133   ++DynData.d.a;
2134
2135   // Check to see if we are at the end of .dynamic
2136   if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) {
2137     // We are at the end. Return the terminator.
2138     DynData.d.a = std::numeric_limits<uint32_t>::max();
2139   }
2140
2141   Result = DynRef(DynData, this);
2142   return object_error::success;
2143 }
2144
2145 template<support::endianness target_endianness, bool is64Bits>
2146 StringRef
2147 ELFObjectFile<target_endianness, is64Bits>::getLoadName() const {
2148   if (!dt_soname) {
2149     // Find the DT_SONAME entry
2150     dyn_iterator it = begin_dynamic_table();
2151     dyn_iterator ie = end_dynamic_table();
2152     error_code ec;
2153     while (it != ie) {
2154       if (it->getTag() == ELF::DT_SONAME)
2155         break;
2156       it.increment(ec);
2157       if (ec)
2158         report_fatal_error("dynamic table iteration failed");
2159     }
2160     if (it != ie) {
2161       if (dot_dynstr_sec == NULL)
2162         report_fatal_error("Dynamic string table is missing");
2163       dt_soname = getString(dot_dynstr_sec, it->getVal());
2164     } else {
2165       dt_soname = "";
2166     }
2167   }
2168   return dt_soname;
2169 }
2170
2171 template<support::endianness target_endianness, bool is64Bits>
2172 library_iterator ELFObjectFile<target_endianness, is64Bits>
2173                              ::begin_libraries_needed() const {
2174   // Find the first DT_NEEDED entry
2175   dyn_iterator i = begin_dynamic_table();
2176   dyn_iterator e = end_dynamic_table();
2177   error_code ec;
2178   while (i != e) {
2179     if (i->getTag() == ELF::DT_NEEDED)
2180       break;
2181     i.increment(ec);
2182     if (ec)
2183       report_fatal_error("dynamic table iteration failed");
2184   }
2185   // Use the same DataRefImpl format as DynRef.
2186   return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
2187 }
2188
2189 template<support::endianness target_endianness, bool is64Bits>
2190 error_code ELFObjectFile<target_endianness, is64Bits>
2191                         ::getLibraryNext(DataRefImpl Data,
2192                                          LibraryRef &Result) const {
2193   // Use the same DataRefImpl format as DynRef.
2194   dyn_iterator i = dyn_iterator(DynRef(Data, this));
2195   dyn_iterator e = end_dynamic_table();
2196
2197   // Skip the current dynamic table entry.
2198   error_code ec;
2199   if (i != e) {
2200     i.increment(ec);
2201     // TODO: proper error handling
2202     if (ec)
2203       report_fatal_error("dynamic table iteration failed");
2204   }
2205
2206   // Find the next DT_NEEDED entry.
2207   while (i != e) {
2208     if (i->getTag() == ELF::DT_NEEDED)
2209       break;
2210     i.increment(ec);
2211     if (ec)
2212       report_fatal_error("dynamic table iteration failed");
2213   }
2214   Result = LibraryRef(i->getRawDataRefImpl(), this);
2215   return object_error::success;
2216 }
2217
2218 template<support::endianness target_endianness, bool is64Bits>
2219 error_code ELFObjectFile<target_endianness, is64Bits>
2220          ::getLibraryPath(DataRefImpl Data, StringRef &Res) const {
2221   dyn_iterator i = dyn_iterator(DynRef(Data, this));
2222   if (i == end_dynamic_table())
2223     report_fatal_error("getLibraryPath() called on iterator end");
2224
2225   if (i->getTag() != ELF::DT_NEEDED)
2226     report_fatal_error("Invalid library_iterator");
2227
2228   // This uses .dynstr to lookup the name of the DT_NEEDED entry.
2229   // THis works as long as DT_STRTAB == .dynstr. This is true most of
2230   // the time, but the specification allows exceptions.
2231   // TODO: This should really use DT_STRTAB instead. Doing this requires
2232   // reading the program headers.
2233   if (dot_dynstr_sec == NULL)
2234     report_fatal_error("Dynamic string table is missing");
2235   Res = getString(dot_dynstr_sec, i->getVal());
2236   return object_error::success;
2237 }
2238
2239 template<support::endianness target_endianness, bool is64Bits>
2240 library_iterator ELFObjectFile<target_endianness, is64Bits>
2241                              ::end_libraries_needed() const {
2242   dyn_iterator e = end_dynamic_table();
2243   // Use the same DataRefImpl format as DynRef.
2244   return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
2245 }
2246
2247 template<support::endianness target_endianness, bool is64Bits>
2248 uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
2249   return is64Bits ? 8 : 4;
2250 }
2251
2252 template<support::endianness target_endianness, bool is64Bits>
2253 StringRef ELFObjectFile<target_endianness, is64Bits>
2254                        ::getFileFormatName() const {
2255   switch(Header->e_ident[ELF::EI_CLASS]) {
2256   case ELF::ELFCLASS32:
2257     switch(Header->e_machine) {
2258     case ELF::EM_386:
2259       return "ELF32-i386";
2260     case ELF::EM_X86_64:
2261       return "ELF32-x86-64";
2262     case ELF::EM_ARM:
2263       return "ELF32-arm";
2264     case ELF::EM_HEXAGON:
2265       return "ELF32-hexagon";
2266     default:
2267       return "ELF32-unknown";
2268     }
2269   case ELF::ELFCLASS64:
2270     switch(Header->e_machine) {
2271     case ELF::EM_386:
2272       return "ELF64-i386";
2273     case ELF::EM_X86_64:
2274       return "ELF64-x86-64";
2275     case ELF::EM_PPC64:
2276       return "ELF64-ppc64";
2277     default:
2278       return "ELF64-unknown";
2279     }
2280   default:
2281     // FIXME: Proper error handling.
2282     report_fatal_error("Invalid ELFCLASS!");
2283   }
2284 }
2285
2286 template<support::endianness target_endianness, bool is64Bits>
2287 unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
2288   switch(Header->e_machine) {
2289   case ELF::EM_386:
2290     return Triple::x86;
2291   case ELF::EM_X86_64:
2292     return Triple::x86_64;
2293   case ELF::EM_ARM:
2294     return Triple::arm;
2295   case ELF::EM_HEXAGON:
2296     return Triple::hexagon;
2297   case ELF::EM_MIPS:
2298     return (target_endianness == support::little) ?
2299            Triple::mipsel : Triple::mips;
2300   case ELF::EM_PPC64:
2301     return Triple::ppc64;
2302   default:
2303     return Triple::UnknownArch;
2304   }
2305 }
2306
2307 template<support::endianness target_endianness, bool is64Bits>
2308 uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const {
2309   assert(Header && "Header not initialized!");
2310   if (Header->e_shnum == ELF::SHN_UNDEF) {
2311     assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
2312     return SectionHeaderTable->sh_size;
2313   }
2314   return Header->e_shnum;
2315 }
2316
2317 template<support::endianness target_endianness, bool is64Bits>
2318 uint64_t
2319 ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const {
2320   if (Header->e_shnum == ELF::SHN_UNDEF) {
2321     if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
2322       return SectionHeaderTable->sh_link;
2323     if (Header->e_shstrndx >= getNumSections())
2324       return 0;
2325   }
2326   return Header->e_shstrndx;
2327 }
2328
2329
2330 template<support::endianness target_endianness, bool is64Bits>
2331 template<typename T>
2332 inline const T *
2333 ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
2334                                                      uint32_t Entry) const {
2335   return getEntry<T>(getSection(Section), Entry);
2336 }
2337
2338 template<support::endianness target_endianness, bool is64Bits>
2339 template<typename T>
2340 inline const T *
2341 ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
2342                                                      uint32_t Entry) const {
2343   return reinterpret_cast<const T *>(
2344            base()
2345            + Section->sh_offset
2346            + (Entry * Section->sh_entsize));
2347 }
2348
2349 template<support::endianness target_endianness, bool is64Bits>
2350 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
2351 ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
2352   return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
2353 }
2354
2355 template<support::endianness target_endianness, bool is64Bits>
2356 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Dyn *
2357 ELFObjectFile<target_endianness, is64Bits>::getDyn(DataRefImpl DynData) const {
2358   return getEntry<Elf_Dyn>(dot_dynamic_sec, DynData.d.a);
2359 }
2360
2361 template<support::endianness target_endianness, bool is64Bits>
2362 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
2363 ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
2364   return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
2365 }
2366
2367 template<support::endianness target_endianness, bool is64Bits>
2368 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
2369 ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
2370   return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
2371 }
2372
2373 template<support::endianness target_endianness, bool is64Bits>
2374 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
2375 ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
2376   const Elf_Shdr *sec = getSection(Symb.d.b);
2377   if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
2378     // FIXME: Proper error handling.
2379     report_fatal_error("Invalid symbol table section!");
2380   return sec;
2381 }
2382
2383 template<support::endianness target_endianness, bool is64Bits>
2384 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
2385 ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const {
2386   if (index == 0)
2387     return 0;
2388   if (!SectionHeaderTable || index >= getNumSections())
2389     // FIXME: Proper error handling.
2390     report_fatal_error("Invalid section index!");
2391
2392   return reinterpret_cast<const Elf_Shdr *>(
2393          reinterpret_cast<const char *>(SectionHeaderTable)
2394          + (index * Header->e_shentsize));
2395 }
2396
2397 template<support::endianness target_endianness, bool is64Bits>
2398 const char *ELFObjectFile<target_endianness, is64Bits>
2399                          ::getString(uint32_t section,
2400                                      ELF::Elf32_Word offset) const {
2401   return getString(getSection(section), offset);
2402 }
2403
2404 template<support::endianness target_endianness, bool is64Bits>
2405 const char *ELFObjectFile<target_endianness, is64Bits>
2406                          ::getString(const Elf_Shdr *section,
2407                                      ELF::Elf32_Word offset) const {
2408   assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
2409   if (offset >= section->sh_size)
2410     // FIXME: Proper error handling.
2411     report_fatal_error("Symbol name offset outside of string table!");
2412   return (const char *)base() + section->sh_offset + offset;
2413 }
2414
2415 template<support::endianness target_endianness, bool is64Bits>
2416 error_code ELFObjectFile<target_endianness, is64Bits>
2417                         ::getSymbolName(const Elf_Shdr *section,
2418                                         const Elf_Sym *symb,
2419                                         StringRef &Result) const {
2420   if (symb->st_name == 0) {
2421     const Elf_Shdr *section = getSection(symb);
2422     if (!section)
2423       Result = "";
2424     else
2425       Result = getString(dot_shstrtab_sec, section->sh_name);
2426     return object_error::success;
2427   }
2428
2429   if (section == SymbolTableSections[0]) {
2430     // Symbol is in .dynsym, use .dynstr string table
2431     Result = getString(dot_dynstr_sec, symb->st_name);
2432   } else {
2433     // Use the default symbol table name section.
2434     Result = getString(dot_strtab_sec, symb->st_name);
2435   }
2436   return object_error::success;
2437 }
2438
2439 template<support::endianness target_endianness, bool is64Bits>
2440 error_code ELFObjectFile<target_endianness, is64Bits>
2441                         ::getSectionName(const Elf_Shdr *section,
2442                                         StringRef &Result) const {
2443   Result = StringRef(getString(dot_shstrtab_sec, section->sh_name));
2444   return object_error::success;
2445 }
2446
2447 template<support::endianness target_endianness, bool is64Bits>
2448 error_code ELFObjectFile<target_endianness, is64Bits>
2449                         ::getSymbolVersion(const Elf_Shdr *section,
2450                                            const Elf_Sym *symb,
2451                                            StringRef &Version,
2452                                            bool &IsDefault) const {
2453   // Handle non-dynamic symbols.
2454   if (section != SymbolTableSections[0]) {
2455     // Non-dynamic symbols can have versions in their names
2456     // A name of the form 'foo@V1' indicates version 'V1', non-default.
2457     // A name of the form 'foo@@V2' indicates version 'V2', default version.
2458     StringRef Name;
2459     error_code ec = getSymbolName(section, symb, Name);
2460     if (ec != object_error::success)
2461       return ec;
2462     size_t atpos = Name.find('@');
2463     if (atpos == StringRef::npos) {
2464       Version = "";
2465       IsDefault = false;
2466       return object_error::success;
2467     }
2468     ++atpos;
2469     if (atpos < Name.size() && Name[atpos] == '@') {
2470       IsDefault = true;
2471       ++atpos;
2472     } else {
2473       IsDefault = false;
2474     }
2475     Version = Name.substr(atpos);
2476     return object_error::success;
2477   }
2478
2479   // This is a dynamic symbol. Look in the GNU symbol version table.
2480   if (dot_gnu_version_sec == NULL) {
2481     // No version table.
2482     Version = "";
2483     IsDefault = false;
2484     return object_error::success;
2485   }
2486
2487   // Determine the position in the symbol table of this entry.
2488   const char *sec_start = (const char*)base() + section->sh_offset;
2489   size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
2490
2491   // Get the corresponding version index entry
2492   const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
2493   size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
2494
2495   // Special markers for unversioned symbols.
2496   if (version_index == ELF::VER_NDX_LOCAL ||
2497       version_index == ELF::VER_NDX_GLOBAL) {
2498     Version = "";
2499     IsDefault = false;
2500     return object_error::success;
2501   }
2502
2503   // Lookup this symbol in the version table
2504   LoadVersionMap();
2505   if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2506     report_fatal_error("Symbol has version index without corresponding "
2507                        "define or reference entry");
2508   const VersionMapEntry &entry = VersionMap[version_index];
2509
2510   // Get the version name string
2511   size_t name_offset;
2512   if (entry.isVerdef()) {
2513     // The first Verdaux entry holds the name.
2514     name_offset = entry.getVerdef()->getAux()->vda_name;
2515   } else {
2516     name_offset = entry.getVernaux()->vna_name;
2517   }
2518   Version = getString(dot_dynstr_sec, name_offset);
2519
2520   // Set IsDefault
2521   if (entry.isVerdef()) {
2522     IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
2523   } else {
2524     IsDefault = false;
2525   }
2526
2527   return object_error::success;
2528 }
2529
2530 template<support::endianness target_endianness, bool is64Bits>
2531 inline DynRefImpl<target_endianness, is64Bits>
2532                  ::DynRefImpl(DataRefImpl DynP, const OwningType *Owner)
2533   : DynPimpl(DynP)
2534   , OwningObject(Owner) {}
2535
2536 template<support::endianness target_endianness, bool is64Bits>
2537 inline bool DynRefImpl<target_endianness, is64Bits>
2538                       ::operator==(const DynRefImpl &Other) const {
2539   return DynPimpl == Other.DynPimpl;
2540 }
2541
2542 template<support::endianness target_endianness, bool is64Bits>
2543 inline bool DynRefImpl<target_endianness, is64Bits>
2544                       ::operator <(const DynRefImpl &Other) const {
2545   return DynPimpl < Other.DynPimpl;
2546 }
2547
2548 template<support::endianness target_endianness, bool is64Bits>
2549 inline error_code DynRefImpl<target_endianness, is64Bits>
2550                             ::getNext(DynRefImpl &Result) const {
2551   return OwningObject->getDynNext(DynPimpl, Result);
2552 }
2553
2554 template<support::endianness target_endianness, bool is64Bits>
2555 inline int64_t DynRefImpl<target_endianness, is64Bits>
2556                             ::getTag() const {
2557   return OwningObject->getDyn(DynPimpl)->d_tag;
2558 }
2559
2560 template<support::endianness target_endianness, bool is64Bits>
2561 inline uint64_t DynRefImpl<target_endianness, is64Bits>
2562                             ::getVal() const {
2563   return OwningObject->getDyn(DynPimpl)->d_un.d_val;
2564 }
2565
2566 template<support::endianness target_endianness, bool is64Bits>
2567 inline uint64_t DynRefImpl<target_endianness, is64Bits>
2568                             ::getPtr() const {
2569   return OwningObject->getDyn(DynPimpl)->d_un.d_ptr;
2570 }
2571
2572 template<support::endianness target_endianness, bool is64Bits>
2573 inline DataRefImpl DynRefImpl<target_endianness, is64Bits>
2574                              ::getRawDataRefImpl() const {
2575   return DynPimpl;
2576 }
2577
2578 /// This is a generic interface for retrieving GNU symbol version
2579 /// information from an ELFObjectFile.
2580 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
2581                                              const SymbolRef &Sym,
2582                                              StringRef &Version,
2583                                              bool &IsDefault) {
2584   // Little-endian 32-bit
2585   if (const ELFObjectFile<support::little, false> *ELFObj =
2586           dyn_cast<ELFObjectFile<support::little, false> >(Obj))
2587     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2588
2589   // Big-endian 32-bit
2590   if (const ELFObjectFile<support::big, false> *ELFObj =
2591           dyn_cast<ELFObjectFile<support::big, false> >(Obj))
2592     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2593
2594   // Little-endian 64-bit
2595   if (const ELFObjectFile<support::little, true> *ELFObj =
2596           dyn_cast<ELFObjectFile<support::little, true> >(Obj))
2597     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2598
2599   // Big-endian 64-bit
2600   if (const ELFObjectFile<support::big, true> *ELFObj =
2601           dyn_cast<ELFObjectFile<support::big, true> >(Obj))
2602     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2603
2604   llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");
2605 }
2606
2607 }
2608 }
2609
2610 #endif