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