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