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