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