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