llvm.org GIT mirror llvm / 081a194
[Object] Split the ELF interface into 3 parts. * ELFTypes.h contains template magic for defining types based on endianess, size, and alignment. * ELFFile.h defines the ELFFile class which provides low level ELF specific access. * ELFObjectFile.h contains ELFObjectFile which uses ELFFile to implement the ObjectFile interface. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188022 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 6 years ago
17 changed file(s) with 2897 addition(s) and 2813 deletion(s). Raw diff Collapse all Expand all
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This file declares the ELFObjectFile template class.
9 // This file declares the ELFFile template class.
1010 //
1111 //===----------------------------------------------------------------------===//
1212
1313 #ifndef LLVM_OBJECT_ELF_H
1414 #define LLVM_OBJECT_ELF_H
1515
16 #include "llvm/ADT/ArrayRef.h"
1617 #include "llvm/ADT/DenseMap.h"
1718 #include "llvm/ADT/PointerIntPair.h"
1819 #include "llvm/ADT/SmallVector.h"
1920 #include "llvm/ADT/StringSwitch.h"
2021 #include "llvm/ADT/Triple.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Object/ELFTypes.h"
23 #include "llvm/Object/Error.h"
2224 #include "llvm/Support/Casting.h"
2325 #include "llvm/Support/ELF.h"
2426 #include "llvm/Support/Endian.h"
2527 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/ErrorOr.h"
2629 #include "llvm/Support/MemoryBuffer.h"
2730 #include "llvm/Support/raw_ostream.h"
2831 #include
3235 namespace llvm {
3336 namespace object {
3437
35 using support::endianness;
36
37 template
38 struct ELFType {
39 static const endianness TargetEndianness = target_endianness;
40 static const std::size_t MaxAlignment = max_alignment;
41 static const bool Is64Bits = is64Bits;
42 };
43
44 template
45 struct MaximumAlignment {
46 enum {value = AlignOf::Alignment > max_align ? max_align
47 : AlignOf::Alignment};
48 };
49
50 // Subclasses of ELFObjectFile may need this for template instantiation
38 StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type);
39
40 // Subclasses of ELFFile may need this for template instantiation
5141 inline std::pair
5242 getElfArchType(MemoryBuffer *Object) {
5343 if (Object->getBufferSize() < ELF::EI_NIDENT)
5646 (uint8_t) Object->getBufferStart()[ELF::EI_DATA]);
5747 }
5848
59 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
60 template
61 struct ELFDataTypeTypedefHelperCommon {
62 typedef support::detail::packed_endian_specific_integral
63
64 MaximumAlignment::value> Elf_Half;
65 typedef support::detail::packed_endian_specific_integral
66
67 MaximumAlignment::value> Elf_Word;
68 typedef support::detail::packed_endian_specific_integral
69
70 MaximumAlignment::value> Elf_Sword;
71 typedef support::detail::packed_endian_specific_integral
72
73 MaximumAlignment::value> Elf_Xword;
74 typedef support::detail::packed_endian_specific_integral
75
76 MaximumAlignment::value> Elf_Sxword;
77 };
78
79 template
80 struct ELFDataTypeTypedefHelper;
81
82 /// ELF 32bit types.
83 template
84 struct ELFDataTypeTypedefHelper >
85 : ELFDataTypeTypedefHelperCommon {
86 typedef uint32_t value_type;
87 typedef support::detail::packed_endian_specific_integral
88
89 MaximumAlignment::value> Elf_Addr;
90 typedef support::detail::packed_endian_specific_integral
91
92 MaximumAlignment::value> Elf_Off;
93 };
94
95 /// ELF 64bit types.
96 template
97 struct ELFDataTypeTypedefHelper >
98 : ELFDataTypeTypedefHelperCommon {
99 typedef uint64_t value_type;
100 typedef support::detail::packed_endian_specific_integral
101
102 MaximumAlignment::value> Elf_Addr;
103 typedef support::detail::packed_endian_specific_integral
104
105 MaximumAlignment::value> Elf_Off;
106 };
107
108 // I really don't like doing this, but the alternative is copypasta.
109 #define LLVM_ELF_IMPORT_TYPES(E, M, W) \
110 typedef typename ELFDataTypeTypedefHelper >::Elf_Addr Elf_Addr; \
111 typedef typename ELFDataTypeTypedefHelper >::Elf_Off Elf_Off; \
112 typedef typename ELFDataTypeTypedefHelper >::Elf_Half Elf_Half; \
113 typedef typename ELFDataTypeTypedefHelper >::Elf_Word Elf_Word; \
114 typedef typename \
115 ELFDataTypeTypedefHelper >::Elf_Sword Elf_Sword; \
116 typedef typename \
117 ELFDataTypeTypedefHelper >::Elf_Xword Elf_Xword; \
118 typedef typename \
119 ELFDataTypeTypedefHelper >::Elf_Sxword Elf_Sxword;
120
121 #define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) \
122 LLVM_ELF_IMPORT_TYPES(ELFT::TargetEndianness, ELFT::MaxAlignment, \
123 ELFT::Is64Bits)
124
125 // Section header.
126 template
127 struct Elf_Shdr_Base;
128
129 template
130 struct Elf_Shdr_Base > {
131 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
132 Elf_Word sh_name; // Section name (index into string table)
133 Elf_Word sh_type; // Section type (SHT_*)
134 Elf_Word sh_flags; // Section flags (SHF_*)
135 Elf_Addr sh_addr; // Address where section is to be loaded
136 Elf_Off sh_offset; // File offset of section data, in bytes
137 Elf_Word sh_size; // Size of section, in bytes
138 Elf_Word sh_link; // Section type-specific header table index link
139 Elf_Word sh_info; // Section type-specific extra information
140 Elf_Word sh_addralign;// Section address alignment
141 Elf_Word sh_entsize; // Size of records contained within the section
142 };
143
144 template
145 struct Elf_Shdr_Base > {
146 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
147 Elf_Word sh_name; // Section name (index into string table)
148 Elf_Word sh_type; // Section type (SHT_*)
149 Elf_Xword sh_flags; // Section flags (SHF_*)
150 Elf_Addr sh_addr; // Address where section is to be loaded
151 Elf_Off sh_offset; // File offset of section data, in bytes
152 Elf_Xword sh_size; // Size of section, in bytes
153 Elf_Word sh_link; // Section type-specific header table index link
154 Elf_Word sh_info; // Section type-specific extra information
155 Elf_Xword sh_addralign;// Section address alignment
156 Elf_Xword sh_entsize; // Size of records contained within the section
157 };
158
159 template
160 struct Elf_Shdr_Impl : Elf_Shdr_Base {
161 using Elf_Shdr_Base::sh_entsize;
162 using Elf_Shdr_Base::sh_size;
163
164 /// @brief Get the number of entities this section contains if it has any.
165 unsigned getEntityCount() const {
166 if (sh_entsize == 0)
167 return 0;
168 return sh_size / sh_entsize;
169 }
170 };
171
172 template
173 struct Elf_Sym_Base;
174
175 template
176 struct Elf_Sym_Base > {
177 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
178 Elf_Word st_name; // Symbol name (index into string table)
179 Elf_Addr st_value; // Value or address associated with the symbol
180 Elf_Word st_size; // Size of the symbol
181 unsigned char st_info; // Symbol's type and binding attributes
182 unsigned char st_other; // Must be zero; reserved
183 Elf_Half st_shndx; // Which section (header table index) it's defined in
184 };
185
186 template
187 struct Elf_Sym_Base > {
188 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
189 Elf_Word st_name; // Symbol name (index into string table)
190 unsigned char st_info; // Symbol's type and binding attributes
191 unsigned char st_other; // Must be zero; reserved
192 Elf_Half st_shndx; // Which section (header table index) it's defined in
193 Elf_Addr st_value; // Value or address associated with the symbol
194 Elf_Xword st_size; // Size of the symbol
195 };
196
197 template
198 struct Elf_Sym_Impl : Elf_Sym_Base {
199 using Elf_Sym_Base::st_info;
200
201 // These accessors and mutators correspond to the ELF32_ST_BIND,
202 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
203 unsigned char getBinding() const { return st_info >> 4; }
204 unsigned char getType() const { return st_info & 0x0f; }
205 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
206 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
207 void setBindingAndType(unsigned char b, unsigned char t) {
208 st_info = (b << 4) + (t & 0x0f);
209 }
210 };
211
212 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
213 /// (.gnu.version). This structure is identical for ELF32 and ELF64.
214 template
215 struct Elf_Versym_Impl {
49 template
50 class ELFFile {
51 public:
21652 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
217 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
218 };
219
220 template
221 struct Elf_Verdaux_Impl;
222
223 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
224 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
225 template
226 struct Elf_Verdef_Impl {
227 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
228 typedef Elf_Verdaux_Impl Elf_Verdaux;
229 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
230 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
231 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
232 Elf_Half vd_cnt; // Number of Verdaux entries
233 Elf_Word vd_hash; // Hash of name
234 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
235 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
236
237 /// Get the first Verdaux entry for this Verdef.
238 const Elf_Verdaux *getAux() const {
239 return reinterpret_cast((const char*)this + vd_aux);
240 }
241 };
242
243 /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
244 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
245 template
246 struct Elf_Verdaux_Impl {
247 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
248 Elf_Word vda_name; // Version name (offset in string table)
249 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
250 };
251
252 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
253 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
254 template
255 struct Elf_Verneed_Impl {
256 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
257 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
258 Elf_Half vn_cnt; // Number of associated Vernaux entries
259 Elf_Word vn_file; // Library name (string table offset)
260 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
261 Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
262 };
263
264 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
265 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
266 template
267 struct Elf_Vernaux_Impl {
268 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
269 Elf_Word vna_hash; // Hash of dependency name
270 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
271 Elf_Half vna_other; // Version index, used in .gnu.version entries
272 Elf_Word vna_name; // Dependency name
273 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
274 };
275
276 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
277 /// table section (.dynamic) look like.
278 template
279 struct Elf_Dyn_Base;
280
281 template
282 struct Elf_Dyn_Base > {
283 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
284 Elf_Sword d_tag;
285 union {
286 Elf_Word d_val;
287 Elf_Addr d_ptr;
288 } d_un;
289 };
290
291 template
292 struct Elf_Dyn_Base > {
293 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
294 Elf_Sxword d_tag;
295 union {
296 Elf_Xword d_val;
297 Elf_Addr d_ptr;
298 } d_un;
299 };
300
301 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
302 template
303 struct Elf_Dyn_Impl : Elf_Dyn_Base {
304 using Elf_Dyn_Base::d_tag;
305 using Elf_Dyn_Base::d_un;
306 int64_t getTag() const { return d_tag; }
307 uint64_t getVal() const { return d_un.d_val; }
308 uint64_t getPtr() const { return d_un.ptr; }
309 };
310
311 // Elf_Rel: Elf Relocation
312 template
313 struct Elf_Rel_Base;
314
315 template
316 struct Elf_Rel_Base, false> {
317 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
318 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
319 Elf_Word r_info; // Symbol table index and type of relocation to apply
320
321 uint32_t getRInfo(bool isMips64EL) const {
322 assert(!isMips64EL);
323 return r_info;
324 }
325 void setRInfo(uint32_t R) {
326 r_info = R;
327 }
328 };
329
330 template
331 struct Elf_Rel_Base, false> {
332 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
333 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
334 Elf_Xword r_info; // Symbol table index and type of relocation to apply
335
336 uint64_t getRInfo(bool isMips64EL) const {
337 uint64_t t = r_info;
338 if (!isMips64EL)
339 return t;
340 // Mips64 little endian has a "special" encoding of r_info. Instead of one
341 // 64 bit little endian number, it is a little endian 32 bit number followed
342 // by a 32 bit big endian number.
343 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
344 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
345 }
346 void setRInfo(uint64_t R) {
347 // FIXME: Add mips64el support.
348 r_info = R;
349 }
350 };
351
352 template
353 struct Elf_Rel_Base, true> {
354 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
355 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
356 Elf_Word r_info; // Symbol table index and type of relocation to apply
357 Elf_Sword r_addend; // Compute value for relocatable field by adding this
358
359 uint32_t getRInfo(bool isMips64EL) const {
360 assert(!isMips64EL);
361 return r_info;
362 }
363 void setRInfo(uint32_t R) {
364 r_info = R;
365 }
366 };
367
368 template
369 struct Elf_Rel_Base, true> {
370 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
371 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
372 Elf_Xword r_info; // Symbol table index and type of relocation to apply
373 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
374
375 uint64_t getRInfo(bool isMips64EL) const {
376 // Mips64 little endian has a "special" encoding of r_info. Instead of one
377 // 64 bit little endian number, it is a little endian 32 bit number followed
378 // by a 32 bit big endian number.
379 uint64_t t = r_info;
380 if (!isMips64EL)
381 return t;
382 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
383 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
384 }
385 void setRInfo(uint64_t R) {
386 // FIXME: Add mips64el support.
387 r_info = R;
388 }
389 };
390
391 template
392 struct Elf_Rel_Impl;
393
394 template
395 struct Elf_Rel_Impl, isRela>
396 : Elf_Rel_Base, isRela> {
397 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
398
399 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
400 // and ELF64_R_INFO macros defined in the ELF specification:
401 uint32_t getSymbol(bool isMips64EL) const {
402 return (uint32_t) (this->getRInfo(isMips64EL) >> 32);
403 }
404 uint32_t getType(bool isMips64EL) const {
405 return (uint32_t) (this->getRInfo(isMips64EL) & 0xffffffffL);
406 }
407 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
408 void setType(uint32_t t) { setSymbolAndType(getSymbol(), t); }
409 void setSymbolAndType(uint32_t s, uint32_t t) {
410 this->setRInfo(((uint64_t)s << 32) + (t&0xffffffffL));
411 }
412 };
413
414 template
415 struct Elf_Rel_Impl, isRela>
416 : Elf_Rel_Base, isRela> {
417 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
418
419 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
420 // and ELF32_R_INFO macros defined in the ELF specification:
421 uint32_t getSymbol(bool isMips64EL) const {
422 return this->getRInfo(isMips64EL) >> 8;
423 }
424 unsigned char getType(bool isMips64EL) const {
425 return (unsigned char) (this->getRInfo(isMips64EL) & 0x0ff);
426 }
427 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
428 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
429 void setSymbolAndType(uint32_t s, unsigned char t) {
430 this->setRInfo((s << 8) + t);
431 }
432 };
433
434 template
435 struct Elf_Ehdr_Impl {
436 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
437 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
438 Elf_Half e_type; // Type of file (see ET_*)
439 Elf_Half e_machine; // Required architecture for this file (see EM_*)
440 Elf_Word e_version; // Must be equal to 1
441 Elf_Addr e_entry; // Address to jump to in order to start program
442 Elf_Off e_phoff; // Program header table's file offset, in bytes
443 Elf_Off e_shoff; // Section header table's file offset, in bytes
444 Elf_Word e_flags; // Processor-specific flags
445 Elf_Half e_ehsize; // Size of ELF header, in bytes
446 Elf_Half e_phentsize;// Size of an entry in the program header table
447 Elf_Half e_phnum; // Number of entries in the program header table
448 Elf_Half e_shentsize;// Size of an entry in the section header table
449 Elf_Half e_shnum; // Number of entries in the section header table
450 Elf_Half e_shstrndx; // Section header table index of section name
451 // string table
452 bool checkMagic() const {
453 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
454 }
455 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
456 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
457 };
458
459 template
460 struct Elf_Phdr_Impl;
461
462 template
463 struct Elf_Phdr_Impl > {
464 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
465 Elf_Word p_type; // Type of segment
466 Elf_Off p_offset; // FileOffset where segment is located, in bytes
467 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
468 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
469 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
470 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
471 Elf_Word p_flags; // Segment flags
472 Elf_Word p_align; // Segment alignment constraint
473 };
474
475 template
476 struct Elf_Phdr_Impl > {
477 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
478 Elf_Word p_type; // Type of segment
479 Elf_Word p_flags; // Segment flags
480 Elf_Off p_offset; // FileOffset where segment is located, in bytes
481 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
482 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
483 Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
484 Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero)
485 Elf_Xword p_align; // Segment alignment constraint
486 };
487
488 template
489 class ELFObjectFile : public ObjectFile {
490 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
491
492 public:
53 typedef typename conditional
54 uint64_t, uint32_t>::type uintX_t;
55
49356 /// \brief Iterate over constant sized entities.
494 template
57 template
49558 class ELFEntityIterator {
49659 public:
49760 typedef ptrdiff_t difference_type;
50265
50366 /// \brief Default construct iterator.
50467 ELFEntityIterator() : EntitySize(0), Current(0) {}
505 ELFEntityIterator(uint64_t EntSize, const char *Start)
506 : EntitySize(EntSize)
507 , Current(Start) {}
68 ELFEntityIterator(uintX_t EntSize, const char *Start)
69 : EntitySize(EntSize), Current(Start) {}
50870
50971 reference operator *() {
51072 assert(Current && "Attempted to dereference an invalid iterator!");
544106
545107 difference_type operator -(const ELFEntityIterator &Other) const {
546108 assert(EntitySize == Other.EntitySize &&
547 "Subtracting iterators of different EntitiySize!");
109 "Subtracting iterators of different EntitySize!");
548110 return (Current - Other.Current) / EntitySize;
549111 }
550112
551113 const char *get() const { return Current; }
552114
115 uintX_t getEntSize() const { return EntitySize; }
116
553117 private:
554 uint64_t EntitySize;
118 uintX_t EntitySize;
555119 const char *Current;
556120 };
557121
567131 typedef Elf_Verneed_Impl Elf_Verneed;
568132 typedef Elf_Vernaux_Impl Elf_Vernaux;
569133 typedef Elf_Versym_Impl Elf_Versym;
570 typedef ELFEntityIterator Elf_Dyn_iterator;
571 typedef ELFEntityIteratorSym> Elf_Sym_iterator;
134 typedef ELFEntityIteratorDyn> Elf_Dyn_Iter;
572135 typedef ELFEntityIterator Elf_Rela_Iter;
573136 typedef ELFEntityIterator Elf_Rel_Iter;
574
575 protected:
576 // This flag is used for classof, to distinguish ELFObjectFile from
577 // its subclass. If more subclasses will be created, this flag will
578 // have to become an enum.
579 bool isDyldELFObject;
137 typedef ELFEntityIterator Elf_Shdr_Iter;
138
139 /// \brief Archive files are 2 byte aligned, so we need this for
140 /// PointerIntPair to work.
141 template
142 class ArchivePointerTypeTraits {
143 public:
144 static inline const void *getAsVoidPointer(T *P) { return P; }
145 static inline T *getFromVoidPointer(const void *P) {
146 return static_cast(P);
147 }
148 enum { NumLowBitsAvailable = 1 };
149 };
150
151 class Elf_Sym_Iter {
152 public:
153 typedef ptrdiff_t difference_type;
154 typedef const Elf_Sym value_type;
155 typedef std::random_access_iterator_tag iterator_category;
156 typedef value_type &reference;
157 typedef value_type *pointer;
158
159 /// \brief Default construct iterator.
160 Elf_Sym_Iter() : EntitySize(0), Current(0, false) {}
161 Elf_Sym_Iter(uintX_t EntSize, const char *Start, bool IsDynamic)
162 : EntitySize(EntSize), Current(Start, IsDynamic) {}
163
164 reference operator*() {
165 assert(Current.getPointer() &&
166 "Attempted to dereference an invalid iterator!");
167 return *reinterpret_cast(Current.getPointer());
168 }
169
170 pointer operator->() {
171 assert(Current.getPointer() &&
172 "Attempted to dereference an invalid iterator!");
173 return reinterpret_cast(Current.getPointer());
174 }
175
176 bool operator==(const Elf_Sym_Iter &Other) {
177 return Current == Other.Current;
178 }
179
180 bool operator!=(const Elf_Sym_Iter &Other) { return !(*this == Other); }
181
182 Elf_Sym_Iter &operator++() {
183 assert(Current.getPointer() &&
184 "Attempted to increment an invalid iterator!");
185 Current.setPointer(Current.getPointer() + EntitySize);
186 return *this;
187 }
188
189 Elf_Sym_Iter operator++(int) {
190 Elf_Sym_Iter Tmp = *this;
191 ++*this;
192 return Tmp;
193 }
194
195 Elf_Sym_Iter operator+(difference_type Dist) {
196 assert(Current.getPointer() &&
197 "Attempted to increment an invalid iterator!");
198 Current.setPointer(Current.getPointer() + EntitySize * Dist);
199 return *this;
200 }
201
202 Elf_Sym_Iter &operator=(const Elf_Sym_Iter &Other) {
203 EntitySize = Other.EntitySize;
204 Current = Other.Current;
205 return *this;
206 }
207
208 difference_type operator-(const Elf_Sym_Iter &Other) const {
209 assert(EntitySize == Other.EntitySize &&
210 "Subtracting iterators of different EntitySize!");
211 return (Current.getPointer() - Other.Current.getPointer()) / EntitySize;
212 }
213
214 const char *get() const { return Current.getPointer(); }
215
216 bool isDynamic() const { return Current.getInt(); }
217
218 uintX_t getEntSize() const { return EntitySize; }
219
220 private:
221 uintX_t EntitySize;
222 PointerIntPair
223 ArchivePointerTypeTraits > Current;
224 };
580225
581226 private:
227 typedef SmallVector Sections_t;
228 typedef DenseMap IndexMap_t;
229
230 MemoryBuffer *Buf;
231
232 const uint8_t *base() const {
233 return reinterpret_cast(Buf->getBufferStart());
234 }
235
582236 const Elf_Ehdr *Header;
583237 const Elf_Shdr *SectionHeaderTable;
584238 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
585239 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
586 const Elf_Shdr *dot_dynstr_sec; // Dynamic symbol string table.
587
588 int SymbolTableIndex;
589 int DynamicSymbolTableIndex;
590 DenseMap ExtendedSymbolTable;
591
592 const Elf_Shdr *dot_dynamic_sec; // .dynamic
240 const Elf_Shdr *dot_symtab_sec; // Symbol table section.
241
242 const Elf_Shdr *SymbolTableSectionHeaderIndex;
243 DenseMap ExtendedSymbolTable;
244
593245 const Elf_Shdr *dot_gnu_version_sec; // .gnu.version
594246 const Elf_Shdr *dot_gnu_version_r_sec; // .gnu.version_r
595247 const Elf_Shdr *dot_gnu_version_d_sec; // .gnu.version_d
596248
249 /// \brief Represents a region described by entries in the .dynamic table.
250 struct DynRegionInfo {
251 DynRegionInfo() : Addr(nullptr), Size(0), EntSize(0) {}
252 /// \brief Address in current address space.
253 const void *Addr;
254 /// \brief Size in bytes of the region.
255 uintX_t Size;
256 /// \brief Size of each entity in the region.
257 uintX_t EntSize;
258 };
259
260 DynRegionInfo DynamicRegion;
261 DynRegionInfo DynHashRegion;
262 DynRegionInfo DynStrRegion;
263 DynRegionInfo DynSymRegion;
264
597265 // Pointer to SONAME entry in dynamic string table
598266 // This is set the first time getLoadName is called.
599267 mutable const char *dt_soname;
600
601 private:
602 uint64_t getROffset(DataRefImpl Rel) const;
603268
604269 // Records for each version index the corresponding Verdef or Vernaux entry.
605270 // This is filled the first time LoadVersionMap() is called.
627292 void LoadVersionNeeds(const Elf_Shdr *ec) const;
628293 void LoadVersionMap() const;
629294
630 /// @brief Get the relocation section that contains \a Rel.
631 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
632 return getSection(Rel.d.a);
633 }
634
635295 public:
636 bool isRelocationHasAddend(DataRefImpl Rel) const;
637296 template
638297 const T *getEntry(uint32_t Section, uint32_t Entry) const;
639 template
640 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
641 const Elf_Shdr *getSection(DataRefImpl index) const;
642 const Elf_Shdr *getSection(uint32_t index) const;
643 const Elf_Rel *getRel(DataRefImpl Rel) const;
644 const Elf_Rela *getRela(DataRefImpl Rela) const;
298 template
299 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
645300 const char *getString(uint32_t section, uint32_t offset) const;
646301 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
647 error_code getSymbolVersion(const Elf_Shdr *section,
648 const Elf_Sym *Symb,
649 StringRef &Version,
650 bool &IsDefault) const;
302 const char *getDynamicString(uintX_t Offset) const;
303 ErrorOr getSymbolVersion(const Elf_Shdr *section,
304 const Elf_Sym *Symb,
305 bool &IsDefault) const;
651306 void VerifyStrTab(const Elf_Shdr *sh) const;
652307
653 protected:
654 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
655 void validateSymbol(DataRefImpl Symb) const;
656 StringRef getRelocationTypeName(uint32_t Type) const;
657
658 public:
659 error_code getSymbolName(const Elf_Shdr *section,
660 const Elf_Sym *Symb,
661 StringRef &Res) const;
662 error_code getSectionName(const Elf_Shdr *section,
663 StringRef &Res) const;
664 const Elf_Dyn *getDyn(DataRefImpl DynData) const;
665 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
666 bool &IsDefault) const;
667 uint64_t getSymbolIndex(const Elf_Sym *sym) const;
668 error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
669 protected:
670 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
671 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
672 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
673 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
674 virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
675 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
676 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
677 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
678 virtual error_code getSymbolType(DataRefImpl Symb,
679 SymbolRef::Type &Res) const;
680 virtual error_code getSymbolSection(DataRefImpl Symb,
681 section_iterator &Res) const;
682 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
683
684 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
685 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
686
687 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
688 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
689 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
690 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
691 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
692 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
693 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
694 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
695 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
696 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
697 bool &Res) const;
698 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
699 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
700 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
701 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
702 bool &Result) const;
703 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
704 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
705 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
706
707 virtual error_code getRelocationNext(DataRefImpl Rel,
708 RelocationRef &Res) const;
709 virtual error_code getRelocationAddress(DataRefImpl Rel,
710 uint64_t &Res) const;
711 virtual error_code getRelocationOffset(DataRefImpl Rel,
712 uint64_t &Res) const;
713 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const;
714 virtual error_code getRelocationType(DataRefImpl Rel,
715 uint64_t &Res) const;
716 virtual error_code getRelocationTypeName(DataRefImpl Rel,
717 SmallVectorImpl &Result) const;
718 virtual error_code getRelocationValueString(DataRefImpl Rel,
719 SmallVectorImpl &Result) const;
720
721 public:
722 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
308 StringRef getRelocationTypeName(uint32_t Type) const;
309 void getRelocationTypeName(uint32_t Type,
310 SmallVectorImpl &Result) const;
311
312 /// \brief Get the symbol table section and symbol for a given relocation.
313 template
314 std::pair
315 getRelocationSymbol(const Elf_Shdr *RelSec, const RelT *Rel) const;
316
317 ELFFile(MemoryBuffer *Object, error_code &ec);
723318
724319 bool isMips64EL() const {
725320 return Header->e_machine == ELF::EM_MIPS &&
727322 Header->getDataEncoding() == ELF::ELFDATA2LSB;
728323 }
729324
730 virtual symbol_iterator begin_symbols() const;
731 virtual symbol_iterator end_symbols() const;
732
733 virtual symbol_iterator begin_dynamic_symbols() const;
734 virtual symbol_iterator end_dynamic_symbols() const;
735
736 virtual section_iterator begin_sections() const;
737 virtual section_iterator end_sections() const;
738
739 virtual library_iterator begin_libraries_needed() const;
740 virtual library_iterator end_libraries_needed() const;
741
742 const Elf_Shdr *getDynamicSymbolTableSectionHeader() const {
743 return getSection(DynamicSymbolTableIndex);
744 }
745
746 const Elf_Shdr *getDynamicStringTableSectionHeader() const {
747 return dot_dynstr_sec;
748 }
749
750 Elf_Dyn_iterator begin_dynamic_table() const;
325 Elf_Shdr_Iter begin_sections() const;
326 Elf_Shdr_Iter end_sections() const;
327
328 Elf_Sym_Iter begin_symbols() const;
329 Elf_Sym_Iter end_symbols() const;
330
331 Elf_Dyn_Iter begin_dynamic_table() const;
751332 /// \param NULLEnd use one past the first DT_NULL entry as the end instead of
752333 /// the section size.
753 Elf_Dyn_iterator end_dynamic_table(bool NULLEnd = false) const;
754
755 Elf_Sym_iterator begin_elf_dynamic_symbols() const {
756 const Elf_Shdr *DynSymtab = getDynamicSymbolTableSectionHeader();
757 if (DynSymtab)
758 return Elf_Sym_iterator(DynSymtab->sh_entsize,
759 (const char *)base() + DynSymtab->sh_offset);
760 return Elf_Sym_iterator(0, 0);
761 }
762
763 Elf_Sym_iterator end_elf_dynamic_symbols() const {
764 const Elf_Shdr *DynSymtab = getDynamicSymbolTableSectionHeader();
765 if (DynSymtab)
766 return Elf_Sym_iterator(DynSymtab->sh_entsize, (const char *)base() +
767 DynSymtab->sh_offset + DynSymtab->sh_size);
768 return Elf_Sym_iterator(0, 0);
769 }
770
771 Elf_Rela_Iter beginELFRela(const Elf_Shdr *sec) const {
334 Elf_Dyn_Iter end_dynamic_table(bool NULLEnd = false) const;
335
336 Elf_Sym_Iter begin_dynamic_symbols() const {
337 if (DynSymRegion.Addr)
338 return Elf_Sym_Iter(DynSymRegion.EntSize, (const char *)DynSymRegion.Addr,
339 true);
340 return Elf_Sym_Iter(0, 0, true);
341 }
342
343 Elf_Sym_Iter end_dynamic_symbols() const {
344 if (DynSymRegion.Addr)
345 return Elf_Sym_Iter(DynSymRegion.EntSize,
346 (const char *)DynSymRegion.Addr + DynSymRegion.Size,
347 true);
348 return Elf_Sym_Iter(0, 0, true);
349 }
350
351 Elf_Rela_Iter begin_rela(const Elf_Shdr *sec) const {
772352 return Elf_Rela_Iter(sec->sh_entsize,
773353 (const char *)(base() + sec->sh_offset));
774354 }
775355
776 Elf_Rela_Iter endELFRela(const Elf_Shdr *sec) const {
777 return Elf_Rela_Iter(sec->sh_entsize, (const char *)
778 (base() + sec->sh_offset + sec->sh_size));
779 }
780
781 Elf_Rel_Iter beginELFRel(const Elf_Shdr *sec) const {
356 Elf_Rela_Iter end_rela(const Elf_Shdr *sec) const {
357 return Elf_Rela_Iter(
358 sec->sh_entsize,
359 (const char *)(base() + sec->sh_offset + sec->sh_size));
360 }
361
362 Elf_Rel_Iter begin_rel(const Elf_Shdr *sec) const {
782363 return Elf_Rel_Iter(sec->sh_entsize,
783364 (const char *)(base() + sec->sh_offset));
784365 }
785366
786 Elf_Rel_Iter endELFRel(const Elf_Shdr *sec) const {
787 return Elf_Rel_Iter(sec->sh_entsize, (const char *)
788 (base() + sec->sh_offset + sec->sh_size));
367 Elf_Rel_Iter end_rel(const Elf_Shdr *sec) const {
368 return Elf_Rel_Iter(sec->sh_entsize,
369 (const char *)(base() + sec->sh_offset + sec->sh_size));
789370 }
790371
791372 /// \brief Iterate over program header table.
803384 (Header->e_phnum * Header->e_phentsize));
804385 }
805386
806 virtual uint8_t getBytesInAddress() const;
807 virtual StringRef getFileFormatName() const;
808 virtual StringRef getObjectType() const { return "ELF"; }
809 virtual unsigned getArch() const;
810 virtual StringRef getLoadName() const;
811 virtual error_code getSectionContents(const Elf_Shdr *sec,
812 StringRef &Res) const;
813
814387 uint64_t getNumSections() const;
815 uint64_t getStringTableIndex() const;
388 uintX_t getStringTableIndex() const;
816389 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
817 const Elf_Ehdr *getElfHeader() const;
390 const Elf_Ehdr *getHeader() const { return Header; }
818391 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
819 const Elf_Shdr *getElfSection(section_iterator &It) const;
820 const Elf_Sym *getElfSymbol(symbol_iterator &It) const;
821 const Elf_Sym *getElfSymbol(uint32_t index) const;
822
823 // Methods for type inquiry through isa, cast, and dyn_cast
824 bool isDyldType() const { return isDyldELFObject; }
825 static inline bool classof(const Binary *v) {
826 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
827 ELFT::Is64Bits);
828 }
392 const Elf_Shdr *getSection(uint32_t Index) const;
393 const Elf_Sym *getSymbol(uint32_t index) const;
394
395 ErrorOr getSymbolName(Elf_Sym_Iter Sym) const;
396
397 /// \brief Get the name of \p Symb.
398 /// \param SymTab The symbol table section \p Symb is contained in.
399 /// \param Symb The symbol to get the name of.
400 ///
401 /// \p SymTab is used to lookup the string table to use to get the symbol's
402 /// name.
403 ErrorOr getSymbolName(const Elf_Shdr *SymTab,
404 const Elf_Sym *Symb) const;
405 ErrorOr getSectionName(const Elf_Shdr *Section) const;
406 uint64_t getSymbolIndex(const Elf_Sym *sym) const;
407 ErrorOr > getSectionContents(const Elf_Shdr *Sec) const;
408 StringRef getLoadName() const;
829409 };
830410
831411 // Use an alignment of 2 for the typedefs since that is the worst case for
832412 // ELF files in archives.
833 typedef ELFObjectFile > ELF32LEObjectFile;
834 typedef ELFObjectFile > ELF64LEObjectFile;
835 typedef ELFObjectFile > ELF32BEObjectFile;
836 typedef ELFObjectFile > ELF64BEObjectFile;
413 typedef ELFFile > ELF32LEFile;
414 typedef ELFFile > ELF64LEFile;
415 typedef ELFFile > ELF32BEFile;
416 typedef ELFFile > ELF64BEFile;
837417
838418 // Iterate through the version definitions, and place each Elf_Verdef
839419 // in the VersionMap according to its index.
840 template
841 void ELFObjectFile::LoadVersionDefs(const Elf_Shdr *sec) const {
842 unsigned vd_size = sec->sh_size; // Size of section in bytes
420 template
421 void ELFFile::LoadVersionDefs(const Elf_Shdr *sec) const {
422 unsigned vd_size = sec->sh_size; // Size of section in bytes
843423 unsigned vd_count = sec->sh_info; // Number of Verdef entries
844424 const char *sec_start = (const char*)base() + sec->sh_offset;
845425 const char *sec_end = sec_start + vd_size;
854434 report_fatal_error("Unexpected verdef version");
855435 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
856436 if (index >= VersionMap.size())
857 VersionMap.resize(index+1);
437 VersionMap.resize(index + 1);
858438 VersionMap[index] = VersionMapEntry(vd);
859439 p += vd->vd_next;
860440 }
862442
863443 // Iterate through the versions needed section, and place each Elf_Vernaux
864444 // in the VersionMap according to its index.
865 template
866 void ELFObjectFile::LoadVersionNeeds(const Elf_Shdr *sec) const {
867 unsigned vn_size = sec->sh_size; // Size of section in bytes
445 template
446 void ELFFile::LoadVersionNeeds(const Elf_Shdr *sec) const {
447 unsigned vn_size = sec->sh_size; // Size of section in bytes
868448 unsigned vn_count = sec->sh_info; // Number of Verneed entries
869 const char *sec_start = (const char*)base() + sec->sh_offset;
449 const char *sec_start = (const char *)base() + sec->sh_offset;
870450 const char *sec_end = sec_start + vn_size;
871451 // The first Verneed entry is at the start of the section.
872452 const char *p = sec_start;
886466 const Elf_Vernaux *vna = reinterpret_cast(paux);
887467 size_t index = vna->vna_other & ELF::VERSYM_VERSION;
888468 if (index >= VersionMap.size())
889 VersionMap.resize(index+1);
469 VersionMap.resize(index + 1);
890470 VersionMap[index] = VersionMapEntry(vna);
891471 paux += vna->vna_next;
892472 }
894474 }
895475 }
896476
897 template
898 void ELFObjectFile::LoadVersionMap() const {
477 template
478 void ELFFile::LoadVersionMap() const {
899479 // If there is no dynamic symtab or version table, there is nothing to do.
900 if (getDynamicStringTableSectionHeader() == NULL ||
901 dot_gnu_version_sec == NULL)
480 if (DynSymRegion.Addr == NULL || dot_gnu_version_sec == NULL)
902481 return;
903482
904483 // Has the VersionMap already been loaded?
917496 LoadVersionNeeds(dot_gnu_version_r_sec);
918497 }
919498
920 template
921 void ELFObjectFile::validateSymbol(DataRefImpl Symb) const {
922 #ifndef NDEBUG
923 const Elf_Sym *symb = getSymbol(Symb);
924 const Elf_Shdr *SymbolTableSection = getSection(Symb.d.b);
925 // FIXME: We really need to do proper error handling in the case of an invalid
926 // input file. Because we don't use exceptions, I think we'll just pass
927 // an error object around.
928 if (!( symb
929 && SymbolTableSection
930 && symb >= (const Elf_Sym*)(base()
931 + SymbolTableSection->sh_offset)
932 && symb < (const Elf_Sym*)(base()
933 + SymbolTableSection->sh_offset
934 + SymbolTableSection->sh_size)))
935 // FIXME: Proper error handling.
936 report_fatal_error("Symb must point to a valid symbol!");
937 #endif
938 }
939
940 template
941 error_code ELFObjectFile::getSymbolNext(DataRefImpl Symb,
942 SymbolRef &Result) const {
943 validateSymbol(Symb);
944 ++Symb.d.a;
945 Result = SymbolRef(Symb, this);
946 return object_error::success;
947 }
948
949 template
950 error_code ELFObjectFile::getSymbolName(DataRefImpl Symb,
951 StringRef &Result) const {
952 validateSymbol(Symb);
953 const Elf_Sym *symb = getSymbol(Symb);
954 return getSymbolName(getSection(Symb.d.b), symb, Result);
955 }
956
957 template
958 error_code ELFObjectFile::getSymbolVersion(SymbolRef SymRef,
959 StringRef &Version,
960 bool &IsDefault) const {
961 DataRefImpl Symb = SymRef.getRawDataRefImpl();
962 validateSymbol(Symb);
963 const Elf_Sym *symb = getSymbol(Symb);
964 return getSymbolVersion(getSection(Symb.d.b), symb, Version, IsDefault);
965 }
966
967 template
968 ELF::Elf64_Word ELFObjectFile
969 ::getSymbolTableIndex(const Elf_Sym *symb) const {
499 template
500 ELF::Elf64_Word ELFFile::getSymbolTableIndex(const Elf_Sym *symb) const {
970501 if (symb->st_shndx == ELF::SHN_XINDEX)
971502 return ExtendedSymbolTable.lookup(symb);
972503 return symb->st_shndx;
973504 }
974505
975 template
976 const typename ELFObjectFile::Elf_Shdr *
977 ELFObjectFile::getSection(const Elf_Sym *symb) const {
506 template
507 const typename ELFFile::Elf_Shdr *
508 ELFFile::getSection(const Elf_Sym *symb) const {
978509 if (symb->st_shndx == ELF::SHN_XINDEX)
979510 return getSection(ExtendedSymbolTable.lookup(symb));
980511 if (symb->st_shndx >= ELF::SHN_LORESERVE)
982513 return getSection(symb->st_shndx);
983514 }
984515
985 template
986 const typename ELFObjectFile::Elf_Ehdr *
987 ELFObjectFile::getElfHeader() const {
988 return Header;
989 }
990
991 template
992 const typename ELFObjectFile::Elf_Shdr *
993 ELFObjectFile::getElfSection(section_iterator &It) const {
994 llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl();
995 return reinterpret_cast(ShdrRef.p);
996 }
997
998 template
999 const typename ELFObjectFile::Elf_Sym *
1000 ELFObjectFile::getElfSymbol(symbol_iterator &It) const {
1001 return getSymbol(It->getRawDataRefImpl());
1002 }
1003
1004 template
1005 const typename ELFObjectFile::Elf_Sym *
1006 ELFObjectFile::getElfSymbol(uint32_t index) const {
1007 DataRefImpl SymbolData;
1008 SymbolData.d.a = index;
1009 SymbolData.d.b = SymbolTableIndex;
1010 return getSymbol(SymbolData);
1011 }
1012
1013 template
1014 error_code ELFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
1015 uint64_t &Result) const {
1016 validateSymbol(Symb);
1017 const Elf_Sym *symb = getSymbol(Symb);
1018 const Elf_Shdr *Section;
1019 switch (getSymbolTableIndex(symb)) {
1020 case ELF::SHN_COMMON:
1021 // Unintialized symbols have no offset in the object file
1022 case ELF::SHN_UNDEF:
1023 Result = UnknownAddressOrSize;
1024 return object_error::success;
1025 case ELF::SHN_ABS:
1026 Result = symb->st_value;
1027 return object_error::success;
1028 default: Section = getSection(symb);
1029 }
1030
1031 switch (symb->getType()) {
1032 case ELF::STT_SECTION:
1033 Result = Section ? Section->sh_offset : UnknownAddressOrSize;
1034 return object_error::success;
1035 case ELF::STT_FUNC:
1036 case ELF::STT_OBJECT:
1037 case ELF::STT_NOTYPE:
1038 Result = symb->st_value +
1039 (Section ? Section->sh_offset : 0);
1040 return object_error::success;
1041 default:
1042 Result = UnknownAddressOrSize;
1043 return object_error::success;
1044 }
1045 }
1046
1047 template
1048 error_code ELFObjectFile::getSymbolAddress(DataRefImpl Symb,
1049 uint64_t &Result) const {
1050 validateSymbol(Symb);
1051 const Elf_Sym *symb = getSymbol(Symb);
1052 const Elf_Shdr *Section;
1053 switch (getSymbolTableIndex(symb)) {
1054 case ELF::SHN_COMMON:
1055 case ELF::SHN_UNDEF:
1056 Result = UnknownAddressOrSize;
1057 return object_error::success;
1058 case ELF::SHN_ABS:
1059 Result = symb->st_value;
1060 return object_error::success;
1061 default: Section = getSection(symb);
1062 }
1063
1064 switch (symb->getType()) {
1065 case ELF::STT_SECTION:
1066 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
1067 return object_error::success;
1068 case ELF::STT_FUNC:
1069 case ELF::STT_OBJECT:
1070 case ELF::STT_NOTYPE:
1071 bool IsRelocatable;
1072 switch(Header->e_type) {
1073 case ELF::ET_EXEC:
1074 case ELF::ET_DYN:
1075 IsRelocatable = false;
1076 break;
1077 default:
1078 IsRelocatable = true;
1079 }
1080 Result = symb->st_value;
1081
1082 // Clear the ARM/Thumb indicator flag.
1083 if (Header->e_machine == ELF::EM_ARM)
1084 Result &= ~1;
1085
1086 if (IsRelocatable && Section != 0)
1087 Result += Section->sh_addr;
1088 return object_error::success;
1089 default:
1090 Result = UnknownAddressOrSize;
1091 return object_error::success;
1092 }
1093 }
1094
1095 template
1096 error_code ELFObjectFile::getSymbolAlignment(DataRefImpl Symb,
1097 uint32_t &Res) const {
1098 uint32_t flags;
1099 getSymbolFlags(Symb, flags);
1100 if (flags & SymbolRef::SF_Common) {
1101 uint64_t Value;
1102 getSymbolValue(Symb, Value);
1103 Res = Value;
1104 } else {
1105 Res = 0;
1106 }
1107 return object_error::success;
1108 }
1109
1110 template
1111 error_code ELFObjectFile::getSymbolSize(DataRefImpl Symb,
1112 uint64_t &Result) const {
1113 validateSymbol(Symb);
1114 const Elf_Sym *symb = getSymbol(Symb);
1115 if (symb->st_size == 0)
1116 Result = UnknownAddressOrSize;
1117 Result = symb->st_size;
1118 return object_error::success;
1119 }
1120
1121 template
1122 error_code ELFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
1123 char &Result) const {
1124 validateSymbol(Symb);
1125 const Elf_Sym *symb = getSymbol(Symb);
1126 const Elf_Shdr *Section = getSection(symb);
1127
1128 char ret = '?';
1129
1130 if (Section) {
1131 switch (Section->sh_type) {
1132 case ELF::SHT_PROGBITS:
1133 case ELF::SHT_DYNAMIC:
1134 switch (Section->sh_flags) {
1135 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
1136 ret = 't'; break;
1137 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
1138 ret = 'd'; break;
1139 case ELF::SHF_ALLOC:
1140 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
1141 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
1142 ret = 'r'; break;
1143 }
1144 break;
1145 case ELF::SHT_NOBITS: ret = 'b';
1146 }
1147 }
1148
1149 switch (getSymbolTableIndex(symb)) {
1150 case ELF::SHN_UNDEF:
1151 if (ret == '?')
1152 ret = 'U';
1153 break;
1154 case ELF::SHN_ABS: ret = 'a'; break;
1155 case ELF::SHN_COMMON: ret = 'c'; break;
1156 }
1157
1158 switch (symb->getBinding()) {
1159 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
1160 case ELF::STB_WEAK:
1161 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1162 ret = 'w';
1163 else
1164 if (symb->getType() == ELF::STT_OBJECT)
1165 ret = 'V';
1166 else
1167 ret = 'W';
1168 }
1169
1170 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
1171 StringRef name;
1172 if (error_code ec = getSymbolName(Symb, name))
1173 return ec;
1174 Result = StringSwitch(name)
1175 .StartsWith(".debug", 'N')
1176 .StartsWith(".note", 'n')
1177 .Default('?');
1178 return object_error::success;
1179 }
1180
1181 Result = ret;
1182 return object_error::success;
1183 }
1184
1185 template
1186 error_code ELFObjectFile::getSymbolType(DataRefImpl Symb,
1187 SymbolRef::Type &Result) const {
1188 validateSymbol(Symb);
1189 const Elf_Sym *symb = getSymbol(Symb);
1190
1191 switch (symb->getType()) {
1192 case ELF::STT_NOTYPE:
1193 Result = SymbolRef::ST_Unknown;
1194 break;
1195 case ELF::STT_SECTION:
1196 Result = SymbolRef::ST_Debug;
1197 break;
1198 case ELF::STT_FILE:
1199 Result = SymbolRef::ST_File;
1200 break;
1201 case ELF::STT_FUNC:
1202 Result = SymbolRef::ST_Function;
1203 break;
1204 case ELF::STT_OBJECT:
1205 case ELF::STT_COMMON:
1206 case ELF::STT_TLS:
1207 Result = SymbolRef::ST_Data;
1208 break;
1209 default:
1210 Result = SymbolRef::ST_Other;
1211 break;
1212 }
1213 return object_error::success;
1214 }
1215
1216 template
1217 error_code ELFObjectFile::getSymbolFlags(DataRefImpl Symb,
1218 uint32_t &Result) const {
1219 validateSymbol(Symb);
1220 const Elf_Sym *symb = getSymbol(Symb);
1221
1222 Result = SymbolRef::SF_None;
1223
1224 if (symb->getBinding() != ELF::STB_LOCAL)
1225 Result |= SymbolRef::SF_Global;
1226
1227 if (symb->getBinding() == ELF::STB_WEAK)
1228 Result |= SymbolRef::SF_Weak;
1229
1230 if (symb->st_shndx == ELF::SHN_ABS)
1231 Result |= SymbolRef::SF_Absolute;
1232
1233 if (symb->getType() == ELF::STT_FILE ||
1234 symb->getType() == ELF::STT_SECTION ||
1235 Symb == begin_symbols()->getRawDataRefImpl())
1236 Result |= SymbolRef::SF_FormatSpecific;
1237
1238 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1239 Result |= SymbolRef::SF_Undefined;
1240
1241 if (symb->getType() == ELF::STT_COMMON ||
1242 getSymbolTableIndex(symb) == ELF::SHN_COMMON)
1243 Result |= SymbolRef::SF_Common;
1244
1245 if (symb->getType() == ELF::STT_TLS)
1246 Result |= SymbolRef::SF_ThreadLocal;
1247
1248 return object_error::success;
1249 }
1250
1251 template
1252 error_code ELFObjectFile::getSymbolSection(DataRefImpl Symb,
1253 section_iterator &Res) const {
1254 validateSymbol(Symb);
1255 const Elf_Sym *symb = getSymbol(Symb);
1256 const Elf_Shdr *sec = getSection(symb);
1257 if (!sec)
1258 Res = end_sections();
1259 else {
1260 DataRefImpl Sec;
1261 Sec.p = reinterpret_cast(sec);
1262 Res = section_iterator(SectionRef(Sec, this));
1263 }
1264 return object_error::success;
1265 }
1266
1267 template
1268 error_code ELFObjectFile::getSymbolValue(DataRefImpl Symb,
1269 uint64_t &Val) const {
1270 validateSymbol(Symb);
1271 const Elf_Sym *symb = getSymbol(Symb);
1272 Val = symb->st_value;
1273 return object_error::success;
1274 }
1275
1276 template
1277 error_code ELFObjectFile::getSectionNext(DataRefImpl Sec,
1278 SectionRef &Result) const {
1279 const uint8_t *sec = reinterpret_cast(Sec.p);
1280 sec += Header->e_shentsize;
1281 Sec.p = reinterpret_cast(sec);
1282 Result = SectionRef(Sec, this);
1283 return object_error::success;
1284 }
1285
1286 template
1287 error_code ELFObjectFile::getSectionName(DataRefImpl Sec,
1288 StringRef &Result) const {
1289 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1290 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
1291 return object_error::success;
1292 }
1293
1294 template
1295 error_code ELFObjectFile::getSectionAddress(DataRefImpl Sec,
1296 uint64_t &Result) const {
1297 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1298 Result = sec->sh_addr;
1299 return object_error::success;
1300 }
1301
1302 template
1303 error_code ELFObjectFile::getSectionSize(DataRefImpl Sec,
1304 uint64_t &Result) const {
1305 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1306 Result = sec->sh_size;
1307 return object_error::success;
1308 }
1309
1310 template
1311 error_code ELFObjectFile::getSectionContents(DataRefImpl Sec,
1312 StringRef &Result) const {
1313 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1314 const char *start = (const char*)base() + sec->sh_offset;
1315 Result = StringRef(start, sec->sh_size);
1316 return object_error::success;
1317 }
1318
1319 template
1320 error_code ELFObjectFile::getSectionContents(const Elf_Shdr *Sec,
1321 StringRef &Result) const {
1322 const char *start = (const char*)base() + Sec->sh_offset;
1323 Result = StringRef(start, Sec->sh_size);
1324 return object_error::success;
1325 }
1326
1327 template
1328 error_code ELFObjectFile::getSectionAlignment(DataRefImpl Sec,
1329 uint64_t &Result) const {
1330 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1331 Result = sec->sh_addralign;
1332 return object_error::success;
1333 }
1334
1335 template
1336 error_code ELFObjectFile::isSectionText(DataRefImpl Sec,
1337 bool &Result) const {
1338 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1339 if (sec->sh_flags & ELF::SHF_EXECINSTR)
1340 Result = true;
1341 else
1342 Result = false;
1343 return object_error::success;
1344 }
1345
1346 template
1347 error_code ELFObjectFile::isSectionData(DataRefImpl Sec,
1348 bool &Result) const {
1349 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1350 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1351 && sec->sh_type == ELF::SHT_PROGBITS)
1352 Result = true;
1353 else
1354 Result = false;
1355 return object_error::success;
1356 }
1357
1358 template
1359 error_code ELFObjectFile::isSectionBSS(DataRefImpl Sec,
1360 bool &Result) const {
1361 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1362 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1363 && sec->sh_type == ELF::SHT_NOBITS)
1364 Result = true;
1365 else
1366 Result = false;
1367 return object_error::success;
1368 }
1369
1370 template
1371 error_code ELFObjectFile::isSectionRequiredForExecution(
1372 DataRefImpl Sec, bool &Result) const {
1373 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1374 if (sec->sh_flags & ELF::SHF_ALLOC)
1375 Result = true;
1376 else
1377 Result = false;
1378 return object_error::success;
1379 }
1380
1381 template
1382 error_code ELFObjectFile::isSectionVirtual(DataRefImpl Sec,
1383 bool &Result) const {
1384 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1385 if (sec->sh_type == ELF::SHT_NOBITS)
1386 Result = true;
1387 else
1388 Result = false;
1389 return object_error::success;
1390 }
1391
1392 template
1393 error_code ELFObjectFile::isSectionZeroInit(DataRefImpl Sec,
1394 bool &Result) const {
1395 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1396 // For ELF, all zero-init sections are virtual (that is, they occupy no space
1397 // in the object image) and vice versa.
1398 Result = sec->sh_type == ELF::SHT_NOBITS;
1399 return object_error::success;
1400 }
1401
1402 template
1403 error_code ELFObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
1404 bool &Result) const {
1405 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1406 if (sec->sh_flags & ELF::SHF_WRITE || sec->sh_flags & ELF::SHF_EXECINSTR)
1407 Result = false;
1408 else
1409 Result = true;
1410 return object_error::success;
1411 }
1412
1413 template
1414 error_code ELFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
1415 DataRefImpl Symb,
1416 bool &Result) const {
1417 validateSymbol(Symb);
1418
1419 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
1420 const Elf_Sym *symb = getSymbol(Symb);
1421
1422 unsigned shndx = symb->st_shndx;
1423 bool Reserved = shndx >= ELF::SHN_LORESERVE
1424 && shndx <= ELF::SHN_HIRESERVE;
1425
1426 Result = !Reserved && (sec == getSection(symb->st_shndx));
1427 return object_error::success;
1428 }
1429
1430 template
1431 relocation_iterator
1432 ELFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
1433 DataRefImpl RelData;
1434 uintptr_t SHT = reinterpret_cast(SectionHeaderTable);
1435 RelData.d.a = (Sec.p - SHT) / Header->e_shentsize;
1436 RelData.d.b = 0;
1437 return relocation_iterator(RelocationRef(RelData, this));
1438 }
1439
1440 template
1441 relocation_iterator
1442 ELFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
1443 DataRefImpl RelData;
1444 uintptr_t SHT = reinterpret_cast(SectionHeaderTable);
1445 const Elf_Shdr *S = reinterpret_cast(Sec.p);
1446 RelData.d.a = (Sec.p - SHT) / Header->e_shentsize;
1447 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
1448 RelData.d.b = 0;
1449 else
1450 RelData.d.b = S->sh_size / S->sh_entsize;
1451
1452 return relocation_iterator(RelocationRef(RelData, this));
1453 }
1454
1455 template
1456 section_iterator
1457 ELFObjectFile::getRelocatedSection(DataRefImpl Sec) const {
1458 if (Header->e_type != ELF::ET_REL)
1459 return end_sections();
1460
1461 const Elf_Shdr *S = reinterpret_cast(Sec.p);
1462 unsigned sh_type = S->sh_type;
1463 if (sh_type != ELF::SHT_RELA && sh_type != ELF::SHT_REL)
1464 return end_sections();
1465
1466 assert(S->sh_info != 0);
1467 const Elf_Shdr *R = getSection(S->sh_info);
1468 DataRefImpl D;
1469 D.p = reinterpret_cast(R);
1470 return section_iterator(SectionRef(D, this));
1471 }
1472
1473 // Relocations
1474 template
1475 error_code ELFObjectFile::getRelocationNext(DataRefImpl Rel,
1476 RelocationRef &Result) const {
1477 ++Rel.d.b;
1478 Result = RelocationRef(Rel, this);
1479 return object_error::success;
1480 }
1481
1482 template
1483 symbol_iterator
1484 ELFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
1485 uint32_t symbolIdx;
1486 const Elf_Shdr *sec = getRelSection(Rel);
1487 switch (sec->sh_type) {
1488 default :
1489 report_fatal_error("Invalid section type in Rel!");
1490 case ELF::SHT_REL : {
1491 symbolIdx = getRel(Rel)->getSymbol(isMips64EL());
1492 break;
1493 }
1494 case ELF::SHT_RELA : {
1495 symbolIdx = getRela(Rel)->getSymbol(isMips64EL());
1496 break;
1497 }
1498 }
1499 if (!symbolIdx)
1500 return end_symbols();
1501
1502 DataRefImpl SymbolData;
1503 SymbolData.d.a = symbolIdx;
1504 SymbolData.d.b = sec->sh_link;
1505 return symbol_iterator(SymbolRef(SymbolData, this));
1506 }
1507
1508 template
1509 error_code ELFObjectFile::getRelocationAddress(DataRefImpl Rel,
1510 uint64_t &Result) const {
1511 assert((Header->e_type == ELF::ET_EXEC || Header->e_type == ELF::ET_DYN) &&
1512 "Only executable and shared objects files have addresses");
1513 Result = getROffset(Rel);
1514 return object_error::success;
1515 }
1516
1517 template
1518 error_code ELFObjectFile::getRelocationOffset(DataRefImpl Rel,
1519 uint64_t &Result) const {
1520 assert(Header->e_type == ELF::ET_REL &&
1521 "Only relocatable object files have relocation offsets");
1522 Result = getROffset(Rel);
1523 return object_error::success;
1524 }
1525
1526 template
1527 uint64_t ELFObjectFile::getROffset(DataRefImpl Rel) const {
1528 const Elf_Shdr *sec = getRelSection(Rel);
1529 switch (sec->sh_type) {
1530 default:
1531 report_fatal_error("Invalid section type in Rel!");
1532 case ELF::SHT_REL:
1533 return getRel(Rel)->r_offset;
1534 case ELF::SHT_RELA:
1535 return getRela(Rel)->r_offset;
1536 }
1537 }
1538
1539 template
1540 error_code ELFObjectFile::getRelocationType(DataRefImpl Rel,
1541 uint64_t &Result) const {
1542 const Elf_Shdr *sec = getRelSection(Rel);
1543 switch (sec->sh_type) {
1544 default :
1545 report_fatal_error("Invalid section type in Rel!");
1546 case ELF::SHT_REL : {
1547 Result = getRel(Rel)->getType(isMips64EL());
1548 break;
1549 }
1550 case ELF::SHT_RELA : {
1551 Result = getRela(Rel)->getType(isMips64EL());
1552 break;
1553 }
1554 }
1555 return object_error::success;
1556 }
1557
1558 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1559 case ELF::enum: Res = #enum; break;
1560
1561 template
1562 StringRef ELFObjectFile::getRelocationTypeName(uint32_t Type) const {
1563 StringRef Res = "Unknown";
1564 switch (Header->e_machine) {
1565 case ELF::EM_X86_64:
1566 switch (Type) {
1567 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1568 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1569 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1570 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1571 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1572 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1573 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1574 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1575 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1576 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1577 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1578 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1579 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1580 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1581 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1582 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1583 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1584 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1585 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1586 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1587 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1588 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1589 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1590 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1591 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1592 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1593 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1594 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT64);
1595 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL64);
1596 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC64);
1597 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPLT64);
1598 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLTOFF64);
1599 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1600 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1601 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1602 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1603 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1604 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_IRELATIVE);
1605 default: break;
1606 }
1607 break;
1608 case ELF::EM_386:
1609 switch (Type) {
1610 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1611 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1612 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1613 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1614 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1615 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1616 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1617 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1618 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1619 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1620 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1621 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1622 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1623 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1624 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1625 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1626 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1627 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1628 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1629 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1630 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1631 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1632 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1633 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1634 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1635 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1636 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1637 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1638 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1639 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1640 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1641 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1642 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1643 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1644 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1645 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1646 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1647 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1648 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1649 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1650 default: break;
1651 }
1652 break;
1653 case ELF::EM_MIPS:
1654 switch (Type) {
1655 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_NONE);
1656 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_16);
1657 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_32);
1658 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_REL32);
1659 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_26);
1660 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_HI16);
1661 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_LO16);
1662 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GPREL16);
1663 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_LITERAL);
1664 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GOT16);
1665 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_PC16);
1666 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_CALL16);
1667 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GPREL32);
1668 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_UNUSED1);
1669 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_UNUSED2);
1670 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_SHIFT5);
1671 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_SHIFT6);
1672 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_64);
1673 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GOT_DISP);
1674 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GOT_PAGE);
1675 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GOT_OFST);
1676 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GOT_HI16);
1677 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GOT_LO16);
1678 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_SUB);
1679 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_INSERT_A);
1680 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_INSERT_B);
1681 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_DELETE);
1682 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_HIGHER);
1683 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_HIGHEST);
1684 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_CALL_HI16);
1685 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_CALL_LO16);
1686 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_SCN_DISP);
1687 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_REL16);
1688 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_ADD_IMMEDIATE);
1689 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_PJUMP);
1690 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_RELGOT);
1691 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_JALR);
1692 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_DTPMOD32);
1693 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_DTPREL32);
1694 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_DTPMOD64);
1695 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_DTPREL64);
1696 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_GD);
1697 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_LDM);
1698 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_DTPREL_HI16);
1699 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_DTPREL_LO16);
1700 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_GOTTPREL);
1701 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_TPREL32);
1702 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_TPREL64);
1703 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_TPREL_HI16);
1704 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_TPREL_LO16);
1705 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GLOB_DAT);
1706 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_COPY);
1707 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_JUMP_SLOT);
1708 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_NUM);
1709 default: break;
1710 }
1711 break;
1712 case ELF::EM_AARCH64:
1713 switch (Type) {
1714 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_NONE);
1715 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ABS64);
1716 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ABS32);
1717 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ABS16);
1718 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_PREL64);
1719 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_PREL32);
1720 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_PREL16);
1721 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G0);
1722 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G0_NC);
1723 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G1);
1724 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G1_NC);
1725 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G2);
1726 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G2_NC);
1727 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G3);
1728 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_SABS_G0);
1729 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_SABS_G1);
1730 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_SABS_G2);
1731 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LD_PREL_LO19);
1732 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ADR_PREL_LO21);
1733 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ADR_PREL_PG_HI21);
1734 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ADD_ABS_LO12_NC);
1735 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST8_ABS_LO12_NC);
1736 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TSTBR14);
1737 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_CONDBR19);
1738 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_JUMP26);
1739 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_CALL26);
1740 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST16_ABS_LO12_NC);
1741 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST32_ABS_LO12_NC);
1742 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST64_ABS_LO12_NC);
1743 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST128_ABS_LO12_NC);
1744 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ADR_GOT_PAGE);
1745 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LD64_GOT_LO12_NC);
1746 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G2);
1747 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G1);
1748 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC);
1749 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G0);
1750 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC);
1751 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_ADD_DTPREL_HI12);
1752 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_ADD_DTPREL_LO12);
1753 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC);
1754 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST8_DTPREL_LO12);
1755 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC);
1756 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST16_DTPREL_LO12);
1757 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC);
1758 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST32_DTPREL_LO12);
1759 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC);
1760 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST64_DTPREL_LO12);
1761 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC);
1762 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_MOVW_GOTTPREL_G1);
1763 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC);
1764 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21);
1765 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1766 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
1767 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G2);
1768 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G1);
1769 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G1_NC);
1770 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G0);
1771 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G0_NC);
1772 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_ADD_TPREL_HI12);
1773 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_ADD_TPREL_LO12);
1774 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_ADD_TPREL_LO12_NC);
1775 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST8_TPREL_LO12);
1776 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC);
1777 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST16_TPREL_LO12);
1778 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC);
1779 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST32_TPREL_LO12);
1780 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC);
1781 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST64_TPREL_LO12);
1782 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC);
1783 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSDESC_ADR_PAGE);
1784 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSDESC_LD64_LO12_NC);
1785 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSDESC_ADD_LO12_NC);
1786 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSDESC_CALL);
1787 default: break;
1788 }
1789 break;
1790 case ELF::EM_ARM:
1791 switch (Type) {
1792 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE);
1793 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24);
1794 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32);
1795 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32);
1796 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0);
1797 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16);
1798 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12);
1799 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5);
1800 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8);
1801 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32);
1802 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL);
1803 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8);
1804 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ);
1805 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC);
1806 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8);
1807 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25);
1808 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22);
1809 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32);
1810 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32);
1811 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32);
1812 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY);
1813 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT);
1814 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT);
1815 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE);
1816 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32);
1817 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL);
1818 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL);
1819 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32);
1820 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL);
1821 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24);
1822 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24);
1823 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS);
1824 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0);
1825 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8);
1826 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15);
1827 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC);
1828 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC);
1829 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK);
1830 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1);
1831 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31);
1832 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX);
1833 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2);
1834 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31);
1835 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC);
1836 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS);
1837 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC);
1838 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL);
1839 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC);
1840 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS);
1841 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC);
1842 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL);
1843 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19);
1844 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6);
1845 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0);
1846 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12);
1847 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI);
1848 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI);
1849 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC);
1850 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0);
1851 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC);
1852 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1);
1853 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2);
1854 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1);
1855 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2);
1856 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0);
1857 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1);
1858 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2);
1859 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0);
1860 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1);
1861 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2);
1862 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC);
1863 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0);
1864 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC);
1865 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1);
1866 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2);
1867 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0);
1868 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1);
1869 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2);
1870 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0);
1871 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1);
1872 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2);
1873 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0);
1874 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1);
1875 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2);
1876 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC);
1877 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL);
1878 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL);
1879 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC);
1880 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL);
1881 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL);
1882 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC);
1883 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL);
1884 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ);
1885 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL);
1886 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS);
1887 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS);
1888 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL);
1889 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12);
1890 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12);
1891 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX);
1892 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY);
1893 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT);
1894 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11);
1895 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8);
1896 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32);
1897 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32);
1898 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32);
1899 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32);
1900 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32);
1901 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12);
1902 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12);
1903 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP);
1904 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0);
1905 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1);
1906 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2);
1907 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3);
1908 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4);
1909 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5);
1910 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6);
1911 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7);
1912 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8);
1913 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9);
1914 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10);
1915 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11);
1916 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12);
1917 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13);
1918 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14);
1919 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15);
1920 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO);
1921 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16);
1922 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32);
1923 default: break;
1924 }
1925 break;
1926 case ELF::EM_HEXAGON:
1927 switch (Type) {
1928 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE);
1929 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL);
1930 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL);
1931 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL);
1932 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16);
1933 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16);
1934 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32);
1935 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16);
1936 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8);
1937 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0);
1938 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1);
1939 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2);
1940 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3);
1941 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16);
1942 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL);
1943 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL);
1944 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X);
1945 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X);
1946 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X);
1947 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X);
1948 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X);
1949 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X);
1950 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X);
1951 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X);
1952 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X);
1953 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X);
1954 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X);
1955 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X);
1956 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X);
1957 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X);
1958 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X);
1959 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL);
1960 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY);
1961 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT);
1962 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT);
1963 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE);
1964 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL);
1965 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16);
1966 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16);
1967 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32);
1968 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16);
1969 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16);
1970 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32);
1971 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16);
1972 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32);
1973 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16);
1974 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16);
1975 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32);
1976 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16);
1977 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL);
1978 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16);
1979 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16);
1980 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32);
1981 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16);
1982 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16);
1983 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16);
1984 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32);
1985 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16);
1986 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16);
1987 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32);
1988 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16);
1989 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16);
1990 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16);
1991 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32);
1992 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16);
1993 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X);
1994 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X);
1995 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X);
1996 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X);
1997 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X);
1998 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X);
1999 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X);
2000 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X);
2001 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X);
2002 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X);
2003 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X);
2004 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X);
2005 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X);
2006 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X);
2007 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X);
2008 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X);
2009 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X);
2010 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X);
2011 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X);
2012 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X);
2013 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X);
2014 default: break;
2015 }
2016 break;
2017 case ELF::EM_PPC:
2018 switch (Type) {
2019 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_NONE);
2020 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR32);
2021 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR24);
2022 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR16);
2023 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR16_LO);
2024 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR16_HI);
2025 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR16_HA);
2026 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR14);
2027 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR14_BRTAKEN);
2028 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR14_BRNTAKEN);
2029 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL24);
2030 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL14);
2031 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL14_BRTAKEN);
2032 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL14_BRNTAKEN);
2033 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT16);
2034 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT16_LO);
2035 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT16_HI);
2036 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT16_HA);
2037 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL32);
2038 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TLS);
2039 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_DTPMOD32);
2040 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TPREL16);
2041 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TPREL16_LO);
2042 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TPREL16_HI);
2043 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TPREL16_HA);
2044 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TPREL32);
2045 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_DTPREL16);
2046 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_DTPREL16_LO);
2047 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_DTPREL16_HI);
2048 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_DTPREL16_HA);
2049 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_DTPREL32);
2050 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSGD16);
2051 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSGD16_LO);
2052 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSGD16_HI);
2053 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSGD16_HA);
2054 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSLD16);
2055 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSLD16_LO);
2056 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSLD16_HI);
2057 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSLD16_HA);
2058 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TPREL16);
2059 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TPREL16_LO);
2060 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TPREL16_HI);
2061 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TPREL16_HA);
2062 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_DTPREL16);
2063 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_DTPREL16_LO);
2064 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_DTPREL16_HI);
2065 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_DTPREL16_HA);
2066 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TLSGD);
2067 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TLSLD);
2068 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL16);
2069 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL16_LO);
2070 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL16_HI);
2071 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL16_HA);
2072 default: break;
2073 }
2074 break;
2075 case ELF::EM_PPC64:
2076 switch (Type) {
2077 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_NONE);
2078 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR32);
2079 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR24);
2080 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16);
2081 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_LO);
2082 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_HI);
2083 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_HA);
2084 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR14);
2085 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR14_BRTAKEN);
2086 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR14_BRNTAKEN);
2087 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL24);
2088 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL14);
2089 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL14_BRTAKEN);
2090 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL14_BRNTAKEN);
2091 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT16);
2092 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT16_LO);
2093 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT16_HI);
2094 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT16_HA);
2095 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL32);
2096 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR64);
2097 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_HIGHER);
2098 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_HIGHERA);
2099 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_HIGHEST);
2100 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_HIGHESTA);
2101 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL64);
2102 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC16);
2103 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC16_LO);
2104 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC16_HI);
2105 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC16_HA);
2106 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC);
2107 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_DS);
2108 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_LO_DS);
2109 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT16_DS);
2110 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT16_LO_DS);
2111 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC16_DS);
2112 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC16_LO_DS);
2113 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TLS);
2114 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPMOD64);
2115 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16);
2116 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_LO);
2117 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_HI);
2118 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_HA);
2119 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL64);
2120 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16);
2121 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_LO);
2122 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_HI);
2123 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_HA);
2124 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL64);
2125 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSGD16);
2126 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSGD16_LO);
2127 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSGD16_HI);
2128 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSGD16_HA);
2129 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSLD16);
2130 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSLD16_LO);
2131 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSLD16_HI);
2132 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSLD16_HA);
2133 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TPREL16_DS);
2134 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TPREL16_LO_DS);
2135 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TPREL16_HI);
2136 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TPREL16_HA);
2137 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_DTPREL16_DS);
2138 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_DTPREL16_LO_DS);
2139 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_DTPREL16_HI);
2140 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_DTPREL16_HA);
2141 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_DS);
2142 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_LO_DS);
2143 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_HIGHER);
2144 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_HIGHERA);
2145 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_HIGHEST);
2146 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_HIGHESTA);
2147 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_DS);
2148 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_LO_DS);
2149 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_HIGHER);
2150 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_HIGHERA);
2151 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_HIGHEST);
2152 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_HIGHESTA);
2153 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TLSGD);
2154 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TLSLD);
2155 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL16);
2156 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL16_LO);
2157 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL16_HI);
2158 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL16_HA);
2159 default: break;
2160 }
2161 break;
2162 case ELF::EM_S390:
2163 switch (Type) {
2164 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_NONE);
2165 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_8);
2166 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_12);
2167 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_16);
2168 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_32);
2169 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PC32);
2170 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOT12);
2171 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOT32);
2172 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLT32);
2173 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_COPY);
2174 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GLOB_DAT);
2175 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_JMP_SLOT);
2176 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_RELATIVE);
2177 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTOFF);
2178 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPC);
2179 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOT16);
2180 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PC16);
2181 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PC16DBL);
2182 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLT16DBL);
2183 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PC32DBL);
2184 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLT32DBL);
2185 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPCDBL);
2186 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_64);
2187 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PC64);
2188 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOT64);
2189 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLT64);
2190 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTENT);
2191 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTOFF16);
2192 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTOFF64);
2193 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPLT12);
2194 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPLT16);
2195 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPLT32);
2196 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPLT64);
2197 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPLTENT);
2198 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLTOFF16);
2199 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLTOFF32);
2200 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLTOFF64);
2201 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LOAD);
2202 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GDCALL);
2203 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LDCALL);
2204 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GD32);
2205 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GD64);
2206 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GOTIE12);
2207 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GOTIE32);
2208 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GOTIE64);
2209 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LDM32);
2210 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LDM64);
2211 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_IE32);
2212 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_IE64);
2213 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_IEENT);
2214 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LE32);
2215 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LE64);
2216 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LDO32);
2217 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LDO64);
2218 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_DTPMOD);
2219 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_DTPOFF);
2220 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_TPOFF);
2221 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_20);
2222 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOT20);
2223 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPLT20);
2224 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GOTIE20);
2225 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_IRELATIVE);
2226 default: break;
2227 }
2228 break;
2229 default: break;
2230 }
2231 return Res;
2232 }
2233
2234 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
2235
2236 template
2237 error_code ELFObjectFile::getRelocationTypeName(
2238 DataRefImpl Rel, SmallVectorImpl &Result) const {
2239 const Elf_Shdr *sec = getRelSection(Rel);
2240 uint32_t type;
2241 switch (sec->sh_type) {
2242 default :
2243 return object_error::parse_failed;
2244 case ELF::SHT_REL : {
2245 type = getRel(Rel)->getType(isMips64EL());
2246 break;
2247 }
2248 case ELF::SHT_RELA : {
2249 type = getRela(Rel)->getType(isMips64EL());
2250 break;
2251 }
2252 }
2253
516 template
517 const typename ELFFile::Elf_Sym *
518 ELFFile::getSymbol(uint32_t Index) const {
519 return &*(begin_symbols() + Index);
520 }
521
522 template
523 ErrorOr >
524 ELFFile::getSectionContents(const Elf_Shdr *Sec) const {
525 if (Sec->sh_offset + Sec->sh_size > Buf->getBufferSize())
526 return object_error::parse_failed;
527 const uint8_t *Start = base() + Sec->sh_offset;
528 return ArrayRef(Start, Sec->sh_size);
529 }
530
531 template
532 StringRef ELFFile::getRelocationTypeName(uint32_t Type) const {
533 return getELFRelocationTypeName(Header->e_machine, Type);
534 }
535
536 template
537 void ELFFile::getRelocationTypeName(uint32_t Type,
538 SmallVectorImpl &Result) const {
2254539 if (!isMips64EL()) {
2255 StringRef Name = getRelocationTypeName(type);
540 StringRef Name = getRelocationTypeName(Type);
2256541 Result.append(Name.begin(), Name.end());
2257542 } else {
2258 uint8_t Type1 = (type >> 0) & 0xFF;
2259 uint8_t Type2 = (type >> 8) & 0xFF;
2260 uint8_t Type3 = (type >> 16) & 0xFF;
543 uint8_t Type1 = (Type >> 0) & 0xFF;
544 uint8_t Type2 = (Type >> 8) & 0xFF;
545 uint8_t Type3 = (Type >> 16) & 0xFF;
2261546
2262547 // Concat all three relocation type names.
2263548 StringRef Name = getRelocationTypeName(Type1);
2271556 Result.append(1, '/');
2272557 Result.append(Name.begin(), Name.end());
2273558 }
2274
2275 return object_error::success;
2276 }
2277
2278 template
2279 error_code ELFObjectFile::getRelocationAddend(
2280 DataRefImpl Rel, int64_t &Result) const {
2281 const Elf_Shdr *sec = getRelSection(Rel);
2282 switch (sec->sh_type) {
2283 default :
2284 report_fatal_error("Invalid section type in Rel!");
2285 case ELF::SHT_REL : {
2286 Result = 0;
2287 return object_error::success;
2288 }
2289 case ELF::SHT_RELA : {
2290 Result = getRela(Rel)->r_addend;
2291 return object_error::success;
2292 }
2293 }
2294 }
2295
2296 template
2297 error_code ELFObjectFile::getRelocationValueString(
2298 DataRefImpl Rel, SmallVectorImpl &Result) const {
2299 const Elf_Shdr *sec = getRelSection(Rel);
2300 uint8_t type;
2301 StringRef res;
2302 int64_t addend = 0;
2303 uint16_t symbol_index = 0;
2304 switch (sec->sh_type) {
2305 default:
2306 return object_error::parse_failed;
2307 case ELF::SHT_REL: {
2308 type = getRel(Rel)->getType(isMips64EL());
2309 symbol_index = getRel(Rel)->getSymbol(isMips64EL());
2310 // TODO: Read implicit addend from section data.
2311 break;
2312 }
2313 case ELF::SHT_RELA: {
2314 type = getRela(Rel)->getType(isMips64EL());
2315 symbol_index = getRela(Rel)->getSymbol(isMips64EL());
2316 addend = getRela(Rel)->r_addend;
2317 break;
2318 }
2319 }
2320 const Elf_Sym *symb = getEntry(sec->sh_link, symbol_index);
2321 StringRef symname;
2322 if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
2323 return ec;
2324 switch (Header->e_machine) {
2325 case ELF::EM_X86_64:
2326 switch (type) {
2327 case ELF::R_X86_64_PC8:
2328 case ELF::R_X86_64_PC16:
2329 case ELF::R_X86_64_PC32: {
2330 std::string fmtbuf;
2331 raw_string_ostream fmt(fmtbuf);
2332 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
2333 fmt.flush();
2334 Result.append(fmtbuf.begin(), fmtbuf.end());
2335 }
2336 break;
2337 case ELF::R_X86_64_8:
2338 case ELF::R_X86_64_16:
2339 case ELF::R_X86_64_32:
2340 case ELF::R_X86_64_32S:
2341 case ELF::R_X86_64_64: {
2342 std::string fmtbuf;
2343 raw_string_ostream fmt(fmtbuf);
2344 fmt << symname << (addend < 0 ? "" : "+") << addend;
2345 fmt.flush();
2346 Result.append(fmtbuf.begin(), fmtbuf.end());
2347 }
2348 break;
2349 default:
2350 res = "Unknown";
2351 }
2352 break;
2353 case ELF::EM_AARCH64: {
2354 std::string fmtbuf;
2355 raw_string_ostream fmt(fmtbuf);
2356 fmt << symname;
2357 if (addend != 0)
2358 fmt << (addend < 0 ? "" : "+") << addend;
2359 fmt.flush();
2360 Result.append(fmtbuf.begin(), fmtbuf.end());
2361 break;
2362 }
2363 case ELF::EM_ARM:
2364 case ELF::EM_HEXAGON:
2365 res = symname;
2366 break;
2367 default:
2368 res = "Unknown";
2369 }
2370 if (Result.empty())
2371 Result.append(res.begin(), res.end());
2372 return object_error::success;
559 }
560
561 template
562 template
563 std::pair::Elf_Shdr *,
564 const typename ELFFile::Elf_Sym *>
565 ELFFile::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const {
566 if (!Sec->sh_link)
567 return std::pair(0, 0);
568 const Elf_Shdr *SymTable = getSection(Sec->sh_link);
569 return std::make_pair(
570 SymTable, getEntry(SymTable, Rel->getSymbol(isMips64EL())));
2373571 }
2374572
2375573 // Verify that the last byte in the string table in a null.
2376 template
2377 void ELFObjectFile::VerifyStrTab(const Elf_Shdr *sh) const {
2378 const char *strtab = (const char*)base() + sh->sh_offset;
574 template
575 void ELFFile::VerifyStrTab(const Elf_Shdr *sh) const {
576 const char *strtab = (const char *)base() + sh->sh_offset;
2379577 if (strtab[sh->sh_size - 1] != 0)
2380578 // FIXME: Proper error handling.
2381579 report_fatal_error("String table must end with a null terminator!");
2382580 }
2383581
2384 template
2385 ELFObjectFile::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
2386 : ObjectFile(getELFType(
2387 static_cast(ELFT::TargetEndianness) == support::little,
2388 ELFT::Is64Bits),
2389 Object)
2390 , isDyldELFObject(false)
2391 , SectionHeaderTable(0)
2392 , dot_shstrtab_sec(0)
2393 , dot_strtab_sec(0)
2394 , dot_dynstr_sec(0)
2395 , dot_dynamic_sec(0)
2396 , dot_gnu_version_sec(0)
2397 , dot_gnu_version_r_sec(0)
2398 , dot_gnu_version_d_sec(0)
2399 , dt_soname(0)
2400 {
2401
2402 const uint64_t FileSize = Data->getBufferSize();
582 template
583 uint64_t ELFFile::getNumSections() const {
584 assert(Header && "Header not initialized!");
585 if (Header->e_shnum == ELF::SHN_UNDEF) {
586 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
587 return SectionHeaderTable->sh_size;
588 }
589 return Header->e_shnum;
590 }
591
592 template
593 typename ELFFile::uintX_t ELFFile::getStringTableIndex() const {
594 if (Header->e_shnum == ELF::SHN_UNDEF) {
595 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
596 return SectionHeaderTable->sh_link;
597 if (Header->e_shstrndx >= getNumSections())
598 return 0;
599 }
600 return Header->e_shstrndx;
601 }
602
603 template
604 ELFFile::ELFFile(MemoryBuffer *Object, error_code &ec)
605 : Buf(Object),
606 SectionHeaderTable(0),
607 dot_shstrtab_sec(0),
608 dot_strtab_sec(0),
609 dot_symtab_sec(0),
610 SymbolTableSectionHeaderIndex(0),
611 dot_gnu_version_sec(0),
612 dot_gnu_version_r_sec(0),
613 dot_gnu_version_d_sec(0),
614 dt_soname(0) {
615 const uint64_t FileSize = Buf->getBufferSize();
2403616
2404617 if (sizeof(Elf_Ehdr) > FileSize)
2405618 // FIXME: Proper error handling.
2425638 // FIXME: Proper error handling.
2426639 report_fatal_error("Section table goes past end of file!");
2427640
2428 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
2429 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
2430 const Elf_Shdr* sh = SectionHeaderTable;
2431
2432 SymbolTableIndex = -1;
2433 DynamicSymbolTableIndex = -1;
2434
2435 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
2436 switch (sh->sh_type) {
2437 case ELF::SHT_SYMTAB_SHNDX: {
641 // Scan sections for special sections.
642
643 for (Elf_Shdr_Iter SecI = begin_sections(), SecE = end_sections();
644 SecI != SecE; ++SecI) {
645 switch (SecI->sh_type) {
646 case ELF::SHT_SYMTAB_SHNDX:
2438647 if (SymbolTableSectionHeaderIndex)
2439648 // FIXME: Proper error handling.
2440649 report_fatal_error("More than one .symtab_shndx!");
2441 SymbolTableSectionHeaderIndex = sh;
650 SymbolTableSectionHeaderIndex = &*SecI;
2442651 break;
2443 }
2444 case ELF::SHT_SYMTAB: {
2445 if (SymbolTableIndex != -1)
2446 report_fatal_error("More than one SHT_SYMTAB!");
2447 SymbolTableIndex = i;
652 case ELF::SHT_SYMTAB:
653 if (dot_symtab_sec)
654 // FIXME: Proper error handling.
655 report_fatal_error("More than one .symtab!");
656 dot_symtab_sec = &*SecI;
657 dot_strtab_sec = getSection(SecI->sh_link);
2448658 break;
2449 }
2450659 case ELF::SHT_DYNSYM: {
2451 if (DynamicSymbolTableIndex != -1)
660 if (DynSymRegion.Addr)
2452661 // FIXME: Proper error handling.
2453 report_fatal_error("More than one SHT_DYNSYM!");
2454 DynamicSymbolTableIndex = i;
662 report_fatal_error("More than one .dynsym!");
663 DynSymRegion.Addr = base() + SecI->sh_offset;
664 DynSymRegion.Size = SecI->sh_size;
665 DynSymRegion.EntSize = SecI->sh_entsize;
666 const Elf_Shdr *DynStr = getSection(SecI->sh_link);
667 DynStrRegion.Addr = base() + DynStr->sh_offset;
668 DynStrRegion.Size = DynStr->sh_size;
669 DynStrRegion.EntSize = DynStr->sh_entsize;
2455670 break;
2456671 }
2457 case ELF::SHT_REL:
2458 case ELF::SHT_RELA:
2459 break;
2460 case ELF::SHT_DYNAMIC: {
2461 if (dot_dynamic_sec != NULL)
672 case ELF::SHT_DYNAMIC:
673 if (DynamicRegion.Addr)
2462674 // FIXME: Proper error handling.
2463675 report_fatal_error("More than one .dynamic!");
2464 dot_dynamic_sec = sh;
676 DynamicRegion.Addr = base() + SecI->sh_offset;
677 DynamicRegion.Size = SecI->sh_size;
678 DynamicRegion.EntSize = SecI->sh_entsize;
2465679 break;
2466 }
2467 case ELF::SHT_GNU_versym: {
680 case ELF::SHT_GNU_versym:
2468681 if (dot_gnu_version_sec != NULL)
2469682 // FIXME: Proper error handling.
2470683 report_fatal_error("More than one .gnu.version section!");
2471 dot_gnu_version_sec = sh;
684 dot_gnu_version_sec = &*SecI;
2472685 break;
2473 }
2474 case ELF::SHT_GNU_verdef: {
686 case ELF::SHT_GNU_verdef:
2475687 if (dot_gnu_version_d_sec != NULL)
2476688 // FIXME: Proper error handling.
2477689 report_fatal_error("More than one .gnu.version_d section!");
2478 dot_gnu_version_d_sec = sh;
690 dot_gnu_version_d_sec = &*SecI;
2479691 break;
2480 }
2481 case ELF::SHT_GNU_verneed: {
692 case ELF::SHT_GNU_verneed:
2482693 if (dot_gnu_version_r_sec != NULL)
2483694 // FIXME: Proper error handling.
2484695 report_fatal_error("More than one .gnu.version_r section!");
2485 dot_gnu_version_r_sec = sh;
696 dot_gnu_version_r_sec = &*SecI;
2486697 break;
2487698 }
2488 }
2489 ++sh;
2490699 }
2491700
2492701 // Get string table sections.
2496705 VerifyStrTab(dot_shstrtab_sec);
2497706 }
2498707
2499 // Merge this into the above loop.
2500 for (const char *i = reinterpret_cast(SectionHeaderTable),
2501 *e = i + getNumSections() * Header->e_shentsize;
2502 i != e; i += Header->e_shentsize) {
2503 const Elf_Shdr *sh = reinterpret_cast(i);
2504 if (sh->sh_type == ELF::SHT_STRTAB) {
2505 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
2506 if (SectionName == ".strtab") {
2507 if (dot_strtab_sec != 0)
2508 // FIXME: Proper error handling.
2509 report_fatal_error("Already found section named .strtab!");
2510 dot_strtab_sec = sh;
2511 VerifyStrTab(dot_strtab_sec);
2512 } else if (SectionName == ".dynstr") {
2513 if (dot_dynstr_sec != 0)
2514 // FIXME: Proper error handling.
2515 report_fatal_error("Already found section named .dynstr!");
2516 dot_dynstr_sec = sh;
2517 VerifyStrTab(dot_dynstr_sec);
2518 }
2519 }
2520 }
2521
2522708 // Build symbol name side-mapping if there is one.
2523709 if (SymbolTableSectionHeaderIndex) {
2524710 const Elf_Word *ShndxTable = reinterpret_cast(base() +
2525711 SymbolTableSectionHeaderIndex->sh_offset);
2526 error_code ec;
2527 for (symbol_iterator si = begin_symbols(),
2528 se = end_symbols(); si != se; si.increment(ec)) {
2529 if (ec)
2530 report_fatal_error("Fewer extended symbol table entries than symbols!");
712 for (Elf_Sym_Iter SI = begin_symbols(), SE = end_symbols(); SI != SE;
713 ++SI) {
2531714 if (*ShndxTable != ELF::SHN_UNDEF)
2532 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
715 ExtendedSymbolTable[&*SI] = *ShndxTable;
2533716 ++ShndxTable;
2534717 }
2535718 }
719
720 // Scan program headers.
721 for (Elf_Phdr_Iter PhdrI = begin_program_headers(),
722 PhdrE = end_program_headers();
723 PhdrI != PhdrE; ++PhdrI) {
724 if (PhdrI->p_type == ELF::PT_DYNAMIC) {
725 DynamicRegion.Addr = base() + PhdrI->p_offset;
726 DynamicRegion.Size = PhdrI->p_filesz;
727 DynamicRegion.EntSize = sizeof(Elf_Dyn);
728 break;
729 }
730 }
731
732 ec = error_code::success();
2536733 }
2537734
2538735 // Get the symbol table index in the symtab section given a symbol
2539 template
2540 uint64_t ELFObjectFile::getSymbolIndex(const Elf_Sym *Sym) const {
2541 const Elf_Shdr *SymTab = getSection(SymbolTableIndex);
736 template
737 uint64_t ELFFile::getSymbolIndex(const Elf_Sym *Sym) const {
2542738 uintptr_t SymLoc = uintptr_t(Sym);
2543 uintptr_t SymTabLoc = uintptr_t(base() + SymTab->sh_offset);
739 uintptr_t SymTabLoc = uintptr_t(base() + dot_symtab_sec->sh_offset);
2544740 assert(SymLoc > SymTabLoc && "Symbol not in symbol table!");
2545741 uint64_t SymOffset = SymLoc - SymTabLoc;
2546 assert(SymOffset % SymTab->sh_entsize == 0 &&
742 assert(SymOffset % dot_symtab_sec->sh_entsize == 0 &&
2547743 "Symbol not multiple of symbol size!");
2548 return SymOffset / SymTab->sh_entsize;
2549 }
2550
2551 template
2552 symbol_iterator ELFObjectFile::begin_symbols() const {
2553 DataRefImpl SymbolData;
2554 if (SymbolTableIndex == -1) {
2555 SymbolData.d.a = 0;
2556 SymbolData.d.b = 0;
2557 } else {
2558 SymbolData.d.a = 0;
2559 SymbolData.d.b = SymbolTableIndex;
2560 }
2561 return symbol_iterator(SymbolRef(SymbolData, this));
2562 }
2563
2564 template
2565 symbol_iterator ELFObjectFile::end_symbols() const {
2566 DataRefImpl SymbolData;
2567 if (SymbolTableIndex == -1) {
2568 SymbolData.d.a = 0;
2569 SymbolData.d.b = 0;
2570 } else {
2571 const Elf_Shdr *SymbolTableSection = getSection(SymbolTableIndex);
2572 SymbolData.d.a = SymbolTableSection->getEntityCount();
2573 SymbolData.d.b = SymbolTableIndex;
2574 }
2575 return symbol_iterator(SymbolRef(SymbolData, this));
2576 }
2577
2578 template
2579 symbol_iterator ELFObjectFile::begin_dynamic_symbols() const {
2580 DataRefImpl SymbolData;
2581 if (DynamicSymbolTableIndex == -1) {
2582 SymbolData.d.a = 0;
2583 SymbolData.d.b = 0;
2584 } else {
2585 SymbolData.d.a = 0;
2586 SymbolData.d.b = DynamicSymbolTableIndex;
2587 }
2588 return symbol_iterator(SymbolRef(SymbolData, this));
2589 }
2590
2591 template
2592 symbol_iterator ELFObjectFile::end_dynamic_symbols() const {
2593 DataRefImpl SymbolData;
2594 if (DynamicSymbolTableIndex == -1) {
2595 SymbolData.d.a = 0;
2596 SymbolData.d.b = 0;
2597 } else {
2598 const Elf_Shdr *SymbolTableSection = getSection(DynamicSymbolTableIndex);
2599 SymbolData.d.a = SymbolTableSection->getEntityCount();
2600 SymbolData.d.b = DynamicSymbolTableIndex;
2601 }
2602 return symbol_iterator(SymbolRef(SymbolData, this));
2603 }
2604
2605 template
2606 section_iterator ELFObjectFile::begin_sections() const {
2607 DataRefImpl ret;
2608 ret.p = reinterpret_cast(base() + Header->e_shoff);
2609 return section_iterator(SectionRef(ret, this));
2610 }
2611
2612 template
2613 section_iterator ELFObjectFile::end_sections() const {
2614 DataRefImpl ret;
2615 ret.p = reinterpret_cast(base()
2616 + Header->e_shoff
2617 + (Header->e_shentsize*getNumSections()));
2618 return section_iterator(SectionRef(ret, this));
2619 }
2620
2621 template
2622 typename ELFObjectFile::Elf_Dyn_iterator
2623 ELFObjectFile::begin_dynamic_table() const {
2624 if (dot_dynamic_sec)
2625 return Elf_Dyn_iterator(dot_dynamic_sec->sh_entsize,
2626 (const char *)base() + dot_dynamic_sec->sh_offset);
2627 return Elf_Dyn_iterator(0, 0);
2628 }
2629
2630 template
2631 typename ELFObjectFile::Elf_Dyn_iterator
2632 ELFObjectFile::end_dynamic_table(bool NULLEnd) const {
2633 if (dot_dynamic_sec) {
2634 Elf_Dyn_iterator Ret(dot_dynamic_sec->sh_entsize,
2635 (const char *)base() + dot_dynamic_sec->sh_offset +
2636 dot_dynamic_sec->sh_size);
2637
2638 if (NULLEnd) {
2639 Elf_Dyn_iterator Start = begin_dynamic_table();
2640 while (Start != Ret && Start->getTag() != ELF::DT_NULL)
2641 ++Start;
2642
2643 // Include the DT_NULL.
2644 if (Start != Ret)
2645 ++Start;
2646 Ret = Start;
2647 }
2648 return Ret;
2649 }
2650 return Elf_Dyn_iterator(0, 0);
2651 }
2652
2653 template
2654 StringRef ELFObjectFile::getLoadName() const {
744 return SymOffset / dot_symtab_sec->sh_entsize;
745 }
746
747 template
748 typename ELFFile::Elf_Shdr_Iter ELFFile::begin_sections() const {
749 return Elf_Shdr_Iter(Header->e_shentsize,
750 (const char *)base() + Header->e_shoff);
751 }
752
753 template
754 typename ELFFile::Elf_Shdr_Iter ELFFile::end_sections() const {
755 return Elf_Shdr_Iter(Header->e_shentsize,
756 (const char *)base() + Header->e_shoff +
757 (getNumSections() * Header->e_shentsize));
758 }
759
760 template
761 typename ELFFile::Elf_Sym_Iter ELFFile::begin_symbols() const {
762 if (!dot_symtab_sec)
763 return Elf_Sym_Iter(0, 0, false);
764 return Elf_Sym_Iter(dot_symtab_sec->sh_entsize,
765 (const char *)base() + dot_symtab_sec->sh_offset, false);
766 }
767
768 template
769 typename ELFFile::Elf_Sym_Iter ELFFile::end_symbols() const {
770 if (!dot_symtab_sec)
771 return Elf_Sym_Iter(0, 0, false);
772 return Elf_Sym_Iter(dot_symtab_sec->sh_entsize,
773 (const char *)base() + dot_symtab_sec->sh_offset +
774 dot_symtab_sec->sh_size,
775 false);
776 }
777
778 template
779 typename ELFFile::Elf_Dyn_Iter
780 ELFFile::begin_dynamic_table() const {
781 if (DynamicRegion.Addr)
782 return Elf_Dyn_Iter(DynamicRegion.EntSize,
783 (const char *)DynamicRegion.Addr);
784 return Elf_Dyn_Iter(0, 0);
785 }
786
787 template
788 typename ELFFile::Elf_Dyn_Iter
789 ELFFile::end_dynamic_table(bool NULLEnd) const {
790 if (!DynamicRegion.Addr)
791 return Elf_Dyn_Iter(0, 0);
792 Elf_Dyn_Iter Ret(DynamicRegion.EntSize,
793 (const char *)DynamicRegion.Addr + DynamicRegion.Size);
794
795 if (NULLEnd) {
796 Elf_Dyn_Iter Start = begin_dynamic_table();
797 while (Start != Ret && Start->getTag() != ELF::DT_NULL)
798 ++Start;
799
800 // Include the DT_NULL.
801 if (Start != Ret)
802 ++Start;
803 Ret = Start;
804 }
805 return Ret;
806 }
807
808 template
809 StringRef ELFFile::getLoadName() const {
2655810 if (!dt_soname) {
2656811 // Find the DT_SONAME entry
2657 Elf_Dyn_iterator it = begin_dynamic_table();
2658 Elf_Dyn_iterator ie = end_dynamic_table();
812 Elf_Dyn_Iter it = begin_dynamic_table();
813 Elf_Dyn_Iter ie = end_dynamic_table();
2659814 while (it != ie && it->getTag() != ELF::DT_SONAME)
2660815 ++it;
2661816
2662817 if (it != ie) {
2663 if (dot_dynstr_sec == NULL)
2664 report_fatal_error("Dynamic string table is missing");
2665 dt_soname = getString(dot_dynstr_sec, it->getVal());
818 dt_soname = getDynamicString(it->getVal());
2666819 } else {
2667820 dt_soname = "";
2668821 }
2670823 return dt_soname;
2671824 }
2672825
2673 template
2674 library_iterator ELFObjectFile::begin_libraries_needed() const {
2675 // Find the first DT_NEEDED entry
2676 Elf_Dyn_iterator i = begin_dynamic_table();
2677 Elf_Dyn_iterator e = end_dynamic_table();
2678 while (i != e && i->getTag() != ELF::DT_NEEDED)
2679 ++i;
2680
2681 DataRefImpl DRI;
2682 DRI.p = reinterpret_cast(i.get());
2683 return library_iterator(LibraryRef(DRI, this));
2684 }
2685
2686 template
2687 error_code ELFObjectFile::getLibraryNext(DataRefImpl Data,
2688 LibraryRef &Result) const {
2689 // Use the same DataRefImpl format as DynRef.
2690 Elf_Dyn_iterator i = Elf_Dyn_iterator(dot_dynamic_sec->sh_entsize,
2691 reinterpret_cast(Data.p));
2692 Elf_Dyn_iterator e = end_dynamic_table();
2693
2694 // Skip the current dynamic table entry and find the next DT_NEEDED entry.
2695 do
2696 ++i;
2697 while (i != e && i->getTag() != ELF::DT_NEEDED);
2698
2699 DataRefImpl DRI;
2700 DRI.p = reinterpret_cast(i.get());
2701 Result = LibraryRef(DRI, this);
2702 return object_error::success;
2703 }
2704
2705 template
2706 error_code ELFObjectFile::getLibraryPath(DataRefImpl Data,
2707 StringRef &Res) const {
2708 Elf_Dyn_iterator i = Elf_Dyn_iterator(dot_dynamic_sec->sh_entsize,
2709 reinterpret_cast(Data.p));
2710 if (i == end_dynamic_table())
2711 report_fatal_error("getLibraryPath() called on iterator end");
2712
2713 if (i->getTag() != ELF::DT_NEEDED)
2714 report_fatal_error("Invalid library_iterator");
2715
2716 // This uses .dynstr to lookup the name of the DT_NEEDED entry.
2717 // THis works as long as DT_STRTAB == .dynstr. This is true most of
2718 // the time, but the specification allows exceptions.
2719 // TODO: This should really use DT_STRTAB instead. Doing this requires
2720 // reading the program headers.
2721 if (dot_dynstr_sec == NULL)
2722 report_fatal_error("Dynamic string table is missing");
2723 Res = getString(dot_dynstr_sec, i->getVal());
2724 return object_error::success;
2725 }
2726
2727 template
2728 library_iterator ELFObjectFile::end_libraries_needed() const {
2729 Elf_Dyn_iterator e = end_dynamic_table();
2730 DataRefImpl DRI;
2731 DRI.p = reinterpret_cast(e.get());
2732 return library_iterator(LibraryRef(DRI, this));
2733 }
2734
2735 template
2736 uint8_t ELFObjectFile::getBytesInAddress() const {
2737 return ELFT::Is64Bits ? 8 : 4;
2738 }
2739
2740 template
2741 StringRef ELFObjectFile::getFileFormatName() const {
2742 switch(Header->e_ident[ELF::EI_CLASS]) {
2743 case ELF::ELFCLASS32:
2744 switch(Header->e_machine) {
2745 case ELF::EM_386:
2746 return "ELF32-i386";
2747 case ELF::EM_X86_64:
2748 return "ELF32-x86-64";
2749 case ELF::EM_ARM:
2750 return "ELF32-arm";
2751 case ELF::EM_HEXAGON:
2752 return "ELF32-hexagon";
2753 case ELF::EM_MIPS:
2754 return "ELF32-mips";
2755 case ELF::EM_PPC:
2756 return "ELF32-ppc";
2757 default:
2758 return "ELF32-unknown";
2759 }
2760 case ELF::ELFCLASS64:
2761 switch(Header->e_machine) {
2762 case ELF::EM_386:
2763 return "ELF64-i386";
2764 case ELF::EM_X86_64:
2765 return "ELF64-x86-64";
2766 case ELF::EM_AARCH64:
2767 return "ELF64-aarch64";
2768 case ELF::EM_PPC64:
2769 return "ELF64-ppc64";
2770 case ELF::EM_S390:
2771 return "ELF64-s390";
2772 default:
2773 return "ELF64-unknown";
2774 }
2775 default:
2776 // FIXME: Proper error handling.
2777 report_fatal_error("Invalid ELFCLASS!");
2778 }
2779 }
2780
2781 template
2782 unsigned ELFObjectFile::getArch() const {
2783 switch(Header->e_machine) {
2784 case ELF::EM_386:
2785 return Triple::x86;
2786 case ELF::EM_X86_64:
2787 return Triple::x86_64;
2788 case ELF::EM_AARCH64:
2789 return Triple::aarch64;
2790 case ELF::EM_ARM:
2791 return Triple::arm;
2792 case ELF::EM_HEXAGON:
2793 return Triple::hexagon;
2794 case ELF::EM_MIPS:
2795 return (ELFT::TargetEndianness == support::little) ?
2796 Triple::mipsel : Triple::mips;
2797 case ELF::EM_PPC64:
2798 return (ELFT::TargetEndianness == support::little) ?
2799 Triple::ppc64le : Triple::ppc64;
2800 case ELF::EM_S390:
2801 return Triple::systemz;
2802 default:
2803 return Triple::UnknownArch;
2804 }
2805 }
2806
2807 template
2808 uint64_t ELFObjectFile::getNumSections() const {
2809 assert(Header && "Header not initialized!");
2810 if (Header->e_shnum == ELF::SHN_UNDEF) {
2811 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
2812 return SectionHeaderTable->sh_size;
2813 }
2814 return Header->e_shnum;
2815 }
2816
2817 template
2818 uint64_t
2819 ELFObjectFile::getStringTableIndex() const {
2820 if (Header->e_shnum == ELF::SHN_UNDEF) {
2821 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
2822 return SectionHeaderTable->sh_link;
2823 if (Header->e_shstrndx >= getNumSections())
2824 return 0;
2825 }
2826 return Header->e_shstrndx;
2827 }
2828
2829 template
2830 template
2831 inline const T *
2832 ELFObjectFile::getEntry(uint32_t Section, uint32_t Entry) const {
826 template
827 template
828 const T *ELFFile::getEntry(uint32_t Section, uint32_t Entry) const {
2833829 return getEntry(getSection(Section), Entry);
2834830 }
2835831
2836 template
2837 template
2838 inline const T *
2839 ELFObjectFile::getEntry(const Elf_Shdr * Section, uint32_t Entry) const {
2840 return reinterpret_cast(
2841 base()
2842 + Section->sh_offset
2843 + (Entry * Section->sh_entsize));
2844 }
2845
2846 template
2847 const typename ELFObjectFile::Elf_Sym *
2848 ELFObjectFile::getSymbol(DataRefImpl Symb) const {
2849 return getEntry(Symb.d.b, Symb.d.a);
2850 }
2851
2852 template
2853 const typename ELFObjectFile::Elf_Rel *
2854 ELFObjectFile::getRel(DataRefImpl Rel) const {
2855 return getEntry(Rel.d.a, Rel.d.b);
2856 }
2857
2858 template
2859 const typename ELFObjectFile::Elf_Rela *
2860 ELFObjectFile::getRela(DataRefImpl Rela) const {
2861 return getEntry(Rela.d.a, Rela.d.b);
2862 }
2863
2864 template
2865 const typename ELFObjectFile::Elf_Shdr *
2866 ELFObjectFile::getSection(DataRefImpl Symb) const {
2867 const Elf_Shdr *sec = getSection(Symb.d.b);
2868 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
2869 // FIXME: Proper error handling.
2870 report_fatal_error("Invalid symbol table section!");
2871 return sec;
2872 }
2873
2874 template
2875 const typename ELFObjectFile::Elf_Shdr *
2876 ELFObjectFile::getSection(uint32_t index) const {
832 template
833 template
834 const T *ELFFile::getEntry(const Elf_Shdr *Section,
835 uint32_t Entry) const {
836 return reinterpret_cast(base() + Section->sh_offset +
837 (Entry * Section->sh_entsize));
838 }
839
840 template
841 const typename ELFFile::Elf_Shdr *
842 ELFFile::getSection(uint32_t index) const {
2877843 if (index == 0)
2878844 return 0;
2879845 if (!SectionHeaderTable || index >= getNumSections())
2885851 + (index * Header->e_shentsize));
2886852 }
2887853
2888 template
2889 const char *ELFObjectFile::getString(uint32_t section,
2890 ELF::Elf32_Word offset) const {
854 template
855 const char *ELFFile::getString(uint32_t section,
856 ELF::Elf32_Word offset) const {
2891857 return getString(getSection(section), offset);
2892858 }
2893859
2894 template
2895 const char *ELFObjectFile::getString(const Elf_Shdr *section,
2896 ELF::Elf32_Word offset) const {
860 template
861 const char *ELFFile::getString(const Elf_Shdr *section,
862 ELF::Elf32_Word offset) const {
2897863 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
2898864 if (offset >= section->sh_size)
2899865 // FIXME: Proper error handling.
2901867 return (const char *)base() + section->sh_offset + offset;
2902868 }
2903869
2904 template
2905 error_code ELFObjectFile::getSymbolName(const Elf_Shdr *section,
2906 const Elf_Sym *symb,
2907 StringRef &Result) const {
2908 if (symb->st_name == 0) {
2909 const Elf_Shdr *section = getSection(symb);
2910 if (!section)
2911 Result = "";
2912 else
2913 Result = getString(dot_shstrtab_sec, section->sh_name);
2914 return object_error::success;
2915 }
2916
2917 if (DynamicSymbolTableIndex != -1 &&
2918 section == getSection(DynamicSymbolTableIndex)) {
2919 // Symbol is in .dynsym, use .dynstr string table
2920 Result = getString(dot_dynstr_sec, symb->st_name);
2921 } else {
2922 // Use the default symbol table name section.
2923 Result = getString(dot_strtab_sec, symb->st_name);
2924 }
2925 return object_error::success;
2926 }
2927
2928 template
2929 error_code ELFObjectFile::getSectionName(const Elf_Shdr *section,
2930 StringRef &Result) const {
2931 Result = StringRef(getString(dot_shstrtab_sec, section->sh_name));
2932 return object_error::success;
2933 }
2934
2935 template
2936 error_code ELFObjectFile::getSymbolVersion(const Elf_Shdr *section,
2937 const Elf_Sym *symb,
2938 StringRef &Version,
2939 bool &IsDefault) const {
870 template
871 const char *ELFFile::getDynamicString(uintX_t Offset) const {
872 if (!DynStrRegion.Addr || Offset >= DynStrRegion.Size)
873 return 0;
874 return (const char *)DynStrRegion.Addr + Offset;
875 }
876
877 template
878 ErrorOr ELFFile::getSymbolName(Elf_Sym_Iter Sym) const {
879 if (!Sym.isDynamic())
880 return getSymbolName(dot_symtab_sec, &*Sym);
881
882 if (!DynStrRegion.Addr || Sym->st_name >= DynStrRegion.Size)
883 return object_error::parse_failed;
884 return StringRef(getDynamicString(Sym->st_name));
885 }
886
887 template
888 ErrorOr ELFFile::getSymbolName(const Elf_Shdr *Section,
889 const Elf_Sym *Symb) const {
890 if (Symb->st_name == 0) {
891 const Elf_Shdr *ContainingSec = getSection(Symb);
892 if (ContainingSec)
893 return getSectionName(ContainingSec);
894 }
895
896 const Elf_Shdr *StrTab = getSection(Section->sh_link);
897 if (Symb->st_name >= StrTab->sh_size)
898 return object_error::parse_failed;
899 return StringRef(getString(StrTab, Symb->st_name));
900 }
901
902 template
903 ErrorOr
904 ELFFile::getSectionName(const Elf_Shdr *Section) const {
905 if (Section->sh_name >= dot_shstrtab_sec->sh_size)
906 return object_error::parse_failed;
907 return StringRef(getString(dot_shstrtab_sec, Section->sh_name));
908 }
909
910 template
911 ErrorOr ELFFile::getSymbolVersion(const Elf_Shdr *section,
912 const Elf_Sym *symb,
913 bool &IsDefault) const {
2940914 // Handle non-dynamic symbols.
2941 if (section != getSection(DynamicSymbolTableIndex)) {
915 if (section != DynSymRegion.Addr && section != 0) {
2942916 // Non-dynamic symbols can have versions in their names
2943917 // A name of the form 'foo@V1' indicates version 'V1', non-default.
2944918 // A name of the form 'foo@@V2' indicates version 'V2', default version.
2945 StringRef Name;
2946 error_code ec = getSymbolName(section, symb, Name);
2947 if (ec != object_error::success)
2948 return ec;
919 ErrorOr SymName = getSymbolName(section, symb);
920 if (!SymName)
921 return SymName;
922 StringRef Name = *SymName;
2949923 size_t atpos = Name.find('@');
2950924 if (atpos == StringRef::npos) {
2951 Version = "";
2952925 IsDefault = false;
2953 return object_error::success;
926 return StringRef("");
2954927 }
2955928 ++atpos;
2956929 if (atpos < Name.size() && Name[atpos] == '@') {
2959932 } else {
2960933 IsDefault = false;
2961934 }
2962 Version = Name.substr(atpos);
2963 return object_error::success;
935 return Name.substr(atpos);
2964936 }
2965937
2966938 // This is a dynamic symbol. Look in the GNU symbol version table.
2967939 if (dot_gnu_version_sec == NULL) {
2968940 // No version table.
2969 Version = "";
2970941 IsDefault = false;
2971 return object_error::success;
942 return StringRef("");
2972943 }
2973944
2974945 // Determine the position in the symbol table of this entry.
2975 const char *sec_start = (const char*)base() + section->sh_offset;
2976 size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
946 size_t entry_index = ((const char *)symb - (const char *)DynSymRegion.Addr) /
947 DynSymRegion.EntSize;
2977948
2978949 // Get the corresponding version index entry
2979950 const Elf_Versym *vs = getEntry(dot_gnu_version_sec, entry_index);
2982953 // Special markers for unversioned symbols.
2983954 if (version_index == ELF::VER_NDX_LOCAL ||
2984955 version_index == ELF::VER_NDX_GLOBAL) {
2985 Version = "";
2986956 IsDefault = false;
2987 return object_error::success;
957 return StringRef("");
2988958 }
2989959
2990960 // Lookup this symbol in the version table
2991961 LoadVersionMap();
2992962 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2993 report_fatal_error("Symbol has version index without corresponding "
2994 "define or reference entry");
963 return object_error::parse_failed;
2995964 const VersionMapEntry &entry = VersionMap[version_index];
2996965
2997966 // Get the version name string
3002971 } else {
3003972 name_offset = entry.getVernaux()->vna_name;
3004973 }
3005 Version = getString(dot_dynstr_sec, name_offset);
3006974
3007975 // Set IsDefault
3008976 if (entry.isVerdef()) {
3011979 IsDefault = false;
3012980 }
3013981
3014 return object_error::success;
3015 }
3016
3017 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
3018 /// and make these member functions?
3019 static inline error_code getELFRelocationAddend(const RelocationRef R,
3020 int64_t &Addend) {
3021 const ObjectFile *Obj = R.getObjectFile();
3022 DataRefImpl DRI = R.getRawDataRefImpl();
3023 // Little-endian 32-bit
3024 if (const ELF32LEObjectFile *ELFObj = dyn_cast(Obj))
3025 return ELFObj->getRelocationAddend(DRI, Addend);
3026
3027 // Big-endian 32-bit
3028 if (const ELF32BEObjectFile *ELFObj = dyn_cast(Obj))
3029 return ELFObj->getRelocationAddend(DRI, Addend);
3030
3031 // Little-endian 64-bit
3032 if (const ELF64LEObjectFile *ELFObj = dyn_cast(Obj))
3033 return ELFObj->getRelocationAddend(DRI, Addend);
3034
3035 // Big-endian 64-bit
3036 if (const ELF64BEObjectFile *ELFObj = dyn_cast(Obj))
3037 return ELFObj->getRelocationAddend(DRI, Addend);
3038
3039 llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
3040 }
3041
3042 /// This is a generic interface for retrieving GNU symbol version
3043 /// information from an ELFObjectFile.
3044 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
3045 const SymbolRef &Sym,
3046 StringRef &Version,
3047 bool &IsDefault) {
3048 // Little-endian 32-bit
3049 if (const ELF32LEObjectFile *ELFObj = dyn_cast(Obj))
3050 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
3051
3052 // Big-endian 32-bit
3053 if (const ELF32BEObjectFile *ELFObj = dyn_cast(Obj))
3054 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
3055
3056 // Little-endian 64-bit
3057 if (const ELF64LEObjectFile *ELFObj = dyn_cast(Obj))
3058 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
3059
3060 // Big-endian 64-bit
3061 if (const ELF64BEObjectFile *ELFObj = dyn_cast(Obj))
3062 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
3063
3064 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");
982 if (name_offset >= DynStrRegion.Size)
983 return object_error::parse_failed;
984 return StringRef(getDynamicString(name_offset));
3065985 }
3066986
3067987 /// This function returns the hash value for a symbol in the .dynsym section
3078998 }
3079999 return h;
30801000 }
3081
3082 }
3083 }
1001 } // end namespace object
1002 } // end namespace llvm
30841003
30851004 #endif
0 //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the ELFObjectFile template class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_OBJECT_ELF_OBJECT_FILE_H
14 #define LLVM_OBJECT_ELF_OBJECT_FILE_H
15
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/PointerIntPair.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Object/ELF.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
30 #include
31 #include
32
33 namespace llvm {
34 namespace object {
35
36 template
37 class ELFObjectFile : public ObjectFile {
38 public:
39 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
40
41 typedef typename ELFFile::uintX_t uintX_t;
42
43 typedef typename ELFFile::Elf_Sym Elf_Sym;
44 typedef typename ELFFile::Elf_Shdr Elf_Shdr;
45 typedef typename ELFFile::Elf_Rel Elf_Rel;
46 typedef typename ELFFile::Elf_Rela Elf_Rela;
47 typedef typename ELFFile::Elf_Dyn Elf_Dyn;
48
49 typedef typename ELFFile::Elf_Sym_Iter Elf_Sym_Iter;
50 typedef typename ELFFile::Elf_Shdr_Iter Elf_Shdr_Iter;
51 typedef typename ELFFile::Elf_Dyn_Iter Elf_Dyn_Iter;
52
53 protected:
54 ELFFile EF;
55
56 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
57 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
58 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
59 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
60 virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
61 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
62 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
63 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
64 virtual error_code getSymbolType(DataRefImpl Symb,
65 SymbolRef::Type &Res) const;
66 virtual error_code getSymbolSection(DataRefImpl Symb,
67 section_iterator &Res) const;
68 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
69
70 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
71 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
72
73 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
74 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
75 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
76 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
77 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
78 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
79 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
80 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
81 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
82 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
83 bool &Res) const;
84 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
85 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
86 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
87 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
88 bool &Result) const;
89 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
90 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
91 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
92
93 virtual error_code getRelocationNext(DataRefImpl Rel,
94 RelocationRef &Res) const;
95 virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
96 virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
97 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const;
98 virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
99 virtual error_code getRelocationTypeName(DataRefImpl Rel,
100 SmallVectorImpl &Result) const;
101 virtual error_code
102 getRelocationValueString(DataRefImpl Rel,
103 SmallVectorImpl &Result) const;
104
105 protected: // ELF specific protected members.