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