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