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