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