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