ed87621ff2ac5d6b6486d161fe2553ea20fa9ba8
[oota-llvm.git] / include / llvm / Object / ELFObjectFile.h
1 //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the ELFObjectFile template class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H
15 #define LLVM_OBJECT_ELFOBJECTFILE_H
16
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Object/ELF.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/ELF.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <algorithm>
31 #include <cctype>
32 #include <limits>
33 #include <utility>
34
35 namespace llvm {
36 namespace object {
37
38 class elf_symbol_iterator;
39 class ELFSymbolRef;
40 class ELFRelocationRef;
41
42 class ELFObjectFileBase : public ObjectFile {
43   friend class ELFSymbolRef;
44   friend class ELFSectionRef;
45   friend class ELFRelocationRef;
46
47 protected:
48   ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
49
50   virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
51   virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
52   virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
53
54   virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
55   virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
56
57   virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
58 public:
59
60   typedef iterator_range<elf_symbol_iterator> elf_symbol_iterator_range;
61   virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
62
63   elf_symbol_iterator_range symbols() const;
64
65   static inline bool classof(const Binary *v) { return v->isELF(); }
66 };
67
68 class ELFSectionRef : public SectionRef {
69 public:
70   ELFSectionRef(const SectionRef &B) : SectionRef(B) {
71     assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
72   }
73
74   const ELFObjectFileBase *getObject() const {
75     return cast<ELFObjectFileBase>(SectionRef::getObject());
76   }
77
78   uint32_t getType() const {
79     return getObject()->getSectionType(getRawDataRefImpl());
80   }
81
82   uint64_t getFlags() const {
83     return getObject()->getSectionFlags(getRawDataRefImpl());
84   }
85 };
86
87 class elf_section_iterator : public section_iterator {
88 public:
89   elf_section_iterator(const section_iterator &B) : section_iterator(B) {
90     assert(isa<ELFObjectFileBase>(B->getObject()));
91   }
92
93   const ELFSectionRef *operator->() const {
94     return static_cast<const ELFSectionRef *>(section_iterator::operator->());
95   }
96
97   const ELFSectionRef &operator*() const {
98     return static_cast<const ELFSectionRef &>(section_iterator::operator*());
99   }
100 };
101
102 class ELFSymbolRef : public SymbolRef {
103 public:
104   ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
105     assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
106   }
107
108   const ELFObjectFileBase *getObject() const {
109     return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
110   }
111
112   uint64_t getSize() const {
113     return getObject()->getSymbolSize(getRawDataRefImpl());
114   }
115
116   uint8_t getOther() const {
117     return getObject()->getSymbolOther(getRawDataRefImpl());
118   }
119
120   uint8_t getELFType() const {
121     return getObject()->getSymbolELFType(getRawDataRefImpl());
122   }
123 };
124
125 class elf_symbol_iterator : public symbol_iterator {
126 public:
127   elf_symbol_iterator(const basic_symbol_iterator &B)
128       : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
129                                   cast<ELFObjectFileBase>(B->getObject()))) {}
130
131   const ELFSymbolRef *operator->() const {
132     return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
133   }
134
135   const ELFSymbolRef &operator*() const {
136     return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
137   }
138 };
139
140 class ELFRelocationRef : public RelocationRef {
141 public:
142   ELFRelocationRef(const RelocationRef &B) : RelocationRef(B) {
143     assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
144   }
145
146   const ELFObjectFileBase *getObject() const {
147     return cast<ELFObjectFileBase>(RelocationRef::getObject());
148   }
149
150   ErrorOr<int64_t> getAddend() const {
151     return getObject()->getRelocationAddend(getRawDataRefImpl());
152   }
153 };
154
155 class elf_relocation_iterator : public relocation_iterator {
156 public:
157   elf_relocation_iterator(const relocation_iterator &B)
158       : relocation_iterator(RelocationRef(
159             B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
160
161   const ELFRelocationRef *operator->() const {
162     return static_cast<const ELFRelocationRef *>(
163         relocation_iterator::operator->());
164   }
165
166   const ELFRelocationRef &operator*() const {
167     return static_cast<const ELFRelocationRef &>(
168         relocation_iterator::operator*());
169   }
170 };
171
172 inline ELFObjectFileBase::elf_symbol_iterator_range
173 ELFObjectFileBase::symbols() const {
174   return elf_symbol_iterator_range(symbol_begin(), symbol_end());
175 }
176
177 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
178   uint64_t getSymbolSize(DataRefImpl Sym) const override;
179
180 public:
181   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
182
183   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
184
185   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
186   typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
187   typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
188   typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
189   typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
190   typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
191
192   typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
193
194 protected:
195   ELFFile<ELFT> EF;
196
197   void moveSymbolNext(DataRefImpl &Symb) const override;
198   ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const override;
199   ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
200   uint64_t getSymbolValue(DataRefImpl Symb) const override;
201   uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
202   uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
203   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
204   uint8_t getSymbolOther(DataRefImpl Symb) const override;
205   uint8_t getSymbolELFType(DataRefImpl Symb) const override;
206   SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
207   section_iterator getSymbolSection(const Elf_Sym *Symb) const;
208   std::error_code getSymbolSection(DataRefImpl Symb,
209                                    section_iterator &Res) const override;
210
211   void moveSectionNext(DataRefImpl &Sec) const override;
212   std::error_code getSectionName(DataRefImpl Sec,
213                                  StringRef &Res) const override;
214   uint64_t getSectionAddress(DataRefImpl Sec) const override;
215   uint64_t getSectionSize(DataRefImpl Sec) const override;
216   std::error_code getSectionContents(DataRefImpl Sec,
217                                      StringRef &Res) const override;
218   uint64_t getSectionAlignment(DataRefImpl Sec) const override;
219   bool isSectionText(DataRefImpl Sec) const override;
220   bool isSectionData(DataRefImpl Sec) const override;
221   bool isSectionBSS(DataRefImpl Sec) const override;
222   bool isSectionVirtual(DataRefImpl Sec) const override;
223   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
224   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
225   section_iterator getRelocatedSection(DataRefImpl Sec) const override;
226
227   void moveRelocationNext(DataRefImpl &Rel) const override;
228   uint64_t getRelocationOffset(DataRefImpl Rel) const override;
229   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
230   uint64_t getRelocationType(DataRefImpl Rel) const override;
231   void getRelocationTypeName(DataRefImpl Rel,
232                              SmallVectorImpl<char> &Result) const override;
233
234   uint32_t getSectionType(DataRefImpl Sec) const override;
235   uint64_t getSectionFlags(DataRefImpl Sec) const override;
236   StringRef getRelocationTypeName(uint32_t Type) const;
237
238   /// \brief Get the relocation section that contains \a Rel.
239   const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
240     return *EF.getSection(Rel.d.a);
241   }
242
243   const Elf_Sym *toELFSymIter(DataRefImpl Sym) const {
244     return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
245   }
246
247   DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
248     DataRefImpl DRI;
249     if (!SymTable) {
250       DRI.d.a = 0;
251       DRI.d.b = 0;
252       return DRI;
253     }
254     assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
255            SymTable->sh_type == ELF::SHT_DYNSYM);
256
257     uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
258     unsigned SymTableIndex =
259         (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
260
261     DRI.d.a = SymTableIndex;
262     DRI.d.b = SymbolNum;
263     return DRI;
264   }
265
266   const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
267     return reinterpret_cast<const Elf_Shdr *>(Sec.p);
268   }
269
270   DataRefImpl toDRI(const Elf_Shdr *Sec) const {
271     DataRefImpl DRI;
272     DRI.p = reinterpret_cast<uintptr_t>(Sec);
273     return DRI;
274   }
275
276   DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
277     DataRefImpl DRI;
278     DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
279     return DRI;
280   }
281
282   bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
283     unsigned char Binding = ESym->getBinding();
284     unsigned char Visibility = ESym->getVisibility();
285
286     // A symbol is exported if its binding is either GLOBAL or WEAK, and its
287     // visibility is either DEFAULT or PROTECTED. All other symbols are not
288     // exported.
289     if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
290         (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
291       return true;
292
293     return false;
294   }
295
296   // This flag is used for classof, to distinguish ELFObjectFile from
297   // its subclass. If more subclasses will be created, this flag will
298   // have to become an enum.
299   bool isDyldELFObject;
300
301 public:
302   ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
303
304   const Elf_Rel *getRel(DataRefImpl Rel) const;
305   const Elf_Rela *getRela(DataRefImpl Rela) const;
306
307   const Elf_Sym *getSymbol(DataRefImpl Symb) const;
308
309   basic_symbol_iterator symbol_begin_impl() const override;
310   basic_symbol_iterator symbol_end_impl() const override;
311
312   elf_symbol_iterator dynamic_symbol_begin() const;
313   elf_symbol_iterator dynamic_symbol_end() const;
314
315   section_iterator section_begin() const override;
316   section_iterator section_end() const override;
317
318   ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
319
320   uint8_t getBytesInAddress() const override;
321   StringRef getFileFormatName() const override;
322   unsigned getArch() const override;
323   StringRef getLoadName() const;
324
325   std::error_code getPlatformFlags(unsigned &Result) const override {
326     Result = EF.getHeader()->e_flags;
327     return std::error_code();
328   }
329
330   const ELFFile<ELFT> *getELFFile() const { return &EF; }
331
332   bool isDyldType() const { return isDyldELFObject; }
333   static inline bool classof(const Binary *v) {
334     return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
335                                       ELFT::Is64Bits);
336   }
337
338   elf_symbol_iterator_range getDynamicSymbolIterators() const override;
339
340   bool isRelocatableObject() const override;
341 };
342
343 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
344 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
345 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
346 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
347
348 template <class ELFT>
349 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
350   ++Sym.d.b;
351 }
352
353 template <class ELFT>
354 ErrorOr<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const {
355   const Elf_Sym *ESym = toELFSymIter(Sym);
356   const Elf_Shdr *SymTableSec = *EF.getSection(Sym.d.a);
357   const Elf_Shdr *StringTableSec = *EF.getSection(SymTableSec->sh_link);
358   StringRef SymTable = *EF.getStringTable(StringTableSec);
359   return ESym->getName(SymTable);
360 }
361
362 template <class ELFT>
363 uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
364   return toELFShdrIter(Sec)->sh_flags;
365 }
366
367 template <class ELFT>
368 uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
369   return toELFShdrIter(Sec)->sh_type;
370 }
371
372 template <class ELFT>
373 uint64_t ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb) const {
374   const Elf_Sym *ESym = getSymbol(Symb);
375   switch (ESym->st_shndx) {
376   case ELF::SHN_COMMON:
377   case ELF::SHN_UNDEF:
378     return UnknownAddress;
379   case ELF::SHN_ABS:
380     return ESym->st_value;
381   }
382
383   const Elf_Ehdr *Header = EF.getHeader();
384   uint64_t Ret = ESym->st_value;
385
386   // Clear the ARM/Thumb or microMIPS indicator flag.
387   if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
388       ESym->getType() == ELF::STT_FUNC)
389     Ret &= ~1;
390
391   return Ret;
392 }
393
394 template <class ELFT>
395 ErrorOr<uint64_t>
396 ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
397   uint64_t Result = getSymbolValue(Symb);
398   const Elf_Sym *ESym = getSymbol(Symb);
399   switch (ESym->st_shndx) {
400   case ELF::SHN_COMMON:
401   case ELF::SHN_UNDEF:
402   case ELF::SHN_ABS:
403     return Result;
404   }
405
406   const Elf_Ehdr *Header = EF.getHeader();
407
408   if (Header->e_type == ELF::ET_REL) {
409     ErrorOr<const Elf_Shdr *> SectionOrErr = EF.getSection(ESym);
410     if (std::error_code EC = SectionOrErr.getError())
411       return EC;
412     const Elf_Shdr *Section = *SectionOrErr;
413     if (Section)
414       Result += Section->sh_addr;
415   }
416
417   return Result;
418 }
419
420 template <class ELFT>
421 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
422   const Elf_Sym *Sym = toELFSymIter(Symb);
423   if (Sym->st_shndx == ELF::SHN_COMMON)
424     return Sym->st_value;
425   return 0;
426 }
427
428 template <class ELFT>
429 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
430   return toELFSymIter(Sym)->st_size;
431 }
432
433 template <class ELFT>
434 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
435   return toELFSymIter(Symb)->st_size;
436 }
437
438 template <class ELFT>
439 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
440   return toELFSymIter(Symb)->st_other;
441 }
442
443 template <class ELFT>
444 uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
445   return toELFSymIter(Symb)->getType();
446 }
447
448 template <class ELFT>
449 SymbolRef::Type ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
450   const Elf_Sym *ESym = getSymbol(Symb);
451
452   switch (ESym->getType()) {
453   case ELF::STT_NOTYPE:
454     return SymbolRef::ST_Unknown;
455   case ELF::STT_SECTION:
456     return SymbolRef::ST_Debug;
457   case ELF::STT_FILE:
458     return SymbolRef::ST_File;
459   case ELF::STT_FUNC:
460     return SymbolRef::ST_Function;
461   case ELF::STT_OBJECT:
462   case ELF::STT_COMMON:
463   case ELF::STT_TLS:
464     return SymbolRef::ST_Data;
465   default:
466     return SymbolRef::ST_Other;
467   }
468 }
469
470 template <class ELFT>
471 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
472   const Elf_Sym *ESym = toELFSymIter(Sym);
473
474   uint32_t Result = SymbolRef::SF_None;
475
476   if (ESym->getBinding() != ELF::STB_LOCAL)
477     Result |= SymbolRef::SF_Global;
478
479   if (ESym->getBinding() == ELF::STB_WEAK)
480     Result |= SymbolRef::SF_Weak;
481
482   if (ESym->st_shndx == ELF::SHN_ABS)
483     Result |= SymbolRef::SF_Absolute;
484
485   if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
486       ESym == EF.symbol_begin() || ESym == EF.dynamic_symbol_begin())
487     Result |= SymbolRef::SF_FormatSpecific;
488
489   if (EF.getHeader()->e_machine == ELF::EM_ARM) {
490     if (ErrorOr<StringRef> NameOrErr = getSymbolName(Sym)) {
491       StringRef Name = *NameOrErr;
492       if (Name.startswith("$d") || Name.startswith("$t") ||
493           Name.startswith("$a"))
494         Result |= SymbolRef::SF_FormatSpecific;
495     }
496   }
497
498   if (ESym->st_shndx == ELF::SHN_UNDEF)
499     Result |= SymbolRef::SF_Undefined;
500
501   if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
502     Result |= SymbolRef::SF_Common;
503
504   if (isExportedToOtherDSO(ESym))
505     Result |= SymbolRef::SF_Exported;
506
507   if (ESym->getVisibility() == ELF::STV_HIDDEN)
508     Result |= SymbolRef::SF_Hidden;
509
510   return Result;
511 }
512
513 template <class ELFT>
514 section_iterator
515 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
516   ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym);
517   if (std::error_code EC = ESecOrErr.getError())
518     report_fatal_error(EC.message());
519
520   const Elf_Shdr *ESec = *ESecOrErr;
521   if (!ESec)
522     return section_end();
523
524   DataRefImpl Sec;
525   Sec.p = reinterpret_cast<intptr_t>(ESec);
526   return section_iterator(SectionRef(Sec, this));
527 }
528
529 template <class ELFT>
530 std::error_code
531 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
532                                       section_iterator &Res) const {
533   Res = getSymbolSection(getSymbol(Symb));
534   return std::error_code();
535 }
536
537 template <class ELFT>
538 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
539   const Elf_Shdr *ESec = toELFShdrIter(Sec);
540   Sec = toDRI(++ESec);
541 }
542
543 template <class ELFT>
544 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
545                                                     StringRef &Result) const {
546   ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
547   if (!Name)
548     return Name.getError();
549   Result = *Name;
550   return std::error_code();
551 }
552
553 template <class ELFT>
554 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
555   return toELFShdrIter(Sec)->sh_addr;
556 }
557
558 template <class ELFT>
559 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
560   return toELFShdrIter(Sec)->sh_size;
561 }
562
563 template <class ELFT>
564 std::error_code
565 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
566                                         StringRef &Result) const {
567   const Elf_Shdr *EShdr = toELFShdrIter(Sec);
568   Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
569   return std::error_code();
570 }
571
572 template <class ELFT>
573 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
574   return toELFShdrIter(Sec)->sh_addralign;
575 }
576
577 template <class ELFT>
578 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
579   return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
580 }
581
582 template <class ELFT>
583 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
584   const Elf_Shdr *EShdr = toELFShdrIter(Sec);
585   return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
586          EShdr->sh_type == ELF::SHT_PROGBITS;
587 }
588
589 template <class ELFT>
590 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
591   const Elf_Shdr *EShdr = toELFShdrIter(Sec);
592   return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
593          EShdr->sh_type == ELF::SHT_NOBITS;
594 }
595
596 template <class ELFT>
597 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
598   return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
599 }
600
601 template <class ELFT>
602 relocation_iterator
603 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
604   DataRefImpl RelData;
605   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
606   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
607   RelData.d.b = 0;
608
609   const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
610   if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
611     return relocation_iterator(RelocationRef(RelData, this));
612
613   const Elf_Shdr *RelSec = getRelSection(RelData);
614   ErrorOr<const Elf_Shdr *> SymSecOrErr = EF.getSection(RelSec->sh_link);
615   if (std::error_code EC = SymSecOrErr.getError())
616     report_fatal_error(EC.message());
617   const Elf_Shdr *SymSec = *SymSecOrErr;
618   uint32_t SymSecType = SymSec->sh_type;
619   if (SymSecType != ELF::SHT_SYMTAB && SymSecType != ELF::SHT_DYNSYM)
620     report_fatal_error("Invalid symbol table section type!");
621   if (SymSecType == ELF::SHT_DYNSYM)
622     RelData.d.b = 1;
623
624   return relocation_iterator(RelocationRef(RelData, this));
625 }
626
627 template <class ELFT>
628 relocation_iterator
629 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
630   const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
631   relocation_iterator Begin = section_rel_begin(Sec);
632   if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
633     return Begin;
634   DataRefImpl RelData = Begin->getRawDataRefImpl();
635   RelData.d.b += (S->sh_size / S->sh_entsize) << 1;
636   return relocation_iterator(RelocationRef(RelData, this));
637 }
638
639 template <class ELFT>
640 section_iterator
641 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
642   if (EF.getHeader()->e_type != ELF::ET_REL)
643     return section_end();
644
645   const Elf_Shdr *EShdr = toELFShdrIter(Sec);
646   uintX_t Type = EShdr->sh_type;
647   if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
648     return section_end();
649
650   ErrorOr<const Elf_Shdr *> R = EF.getSection(EShdr->sh_info);
651   if (std::error_code EC = R.getError())
652     report_fatal_error(EC.message());
653   return section_iterator(SectionRef(toDRI(*R), this));
654 }
655
656 // Relocations
657 template <class ELFT>
658 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
659   Rel.d.b += 2;
660 }
661
662 template <class ELFT>
663 symbol_iterator
664 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
665   uint32_t symbolIdx;
666   const Elf_Shdr *sec = getRelSection(Rel);
667   if (sec->sh_type == ELF::SHT_REL)
668     symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
669   else
670     symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
671   if (!symbolIdx)
672     return symbol_end();
673
674   bool IsDyn = Rel.d.b & 1;
675   DataRefImpl SymbolData;
676   if (IsDyn)
677     SymbolData = toDRI(EF.getDotDynSymSec(), symbolIdx);
678   else
679     SymbolData = toDRI(EF.getDotSymtabSec(), symbolIdx);
680   return symbol_iterator(SymbolRef(SymbolData, this));
681 }
682
683 template <class ELFT>
684 uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
685   assert(EF.getHeader()->e_type == ELF::ET_REL &&
686          "Only relocatable object files have relocation offsets");
687   const Elf_Shdr *sec = getRelSection(Rel);
688   if (sec->sh_type == ELF::SHT_REL)
689     return getRel(Rel)->r_offset;
690
691   return getRela(Rel)->r_offset;
692 }
693
694 template <class ELFT>
695 uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
696   const Elf_Shdr *sec = getRelSection(Rel);
697   if (sec->sh_type == ELF::SHT_REL)
698     return getRel(Rel)->getType(EF.isMips64EL());
699   else
700     return getRela(Rel)->getType(EF.isMips64EL());
701 }
702
703 template <class ELFT>
704 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
705   return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
706 }
707
708 template <class ELFT>
709 void ELFObjectFile<ELFT>::getRelocationTypeName(
710     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
711   uint32_t type = getRelocationType(Rel);
712   EF.getRelocationTypeName(type, Result);
713 }
714
715 template <class ELFT>
716 ErrorOr<int64_t>
717 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
718   if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
719     return object_error::parse_failed;
720   return (int64_t)getRela(Rel)->r_addend;
721 }
722
723 template <class ELFT>
724 const typename ELFFile<ELFT>::Elf_Sym *
725 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
726   return &*toELFSymIter(Symb);
727 }
728
729 template <class ELFT>
730 const typename ELFObjectFile<ELFT>::Elf_Rel *
731 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
732   assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
733   return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b >> 1);
734 }
735
736 template <class ELFT>
737 const typename ELFObjectFile<ELFT>::Elf_Rela *
738 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
739   assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
740   return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b >> 1);
741 }
742
743 template <class ELFT>
744 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
745     : ELFObjectFileBase(
746           getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
747                          support::little,
748                      ELFT::Is64Bits),
749           Object),
750       EF(Data.getBuffer(), EC) {}
751
752 template <class ELFT>
753 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
754   DataRefImpl Sym = toDRI(EF.getDotSymtabSec(), 0);
755   return basic_symbol_iterator(SymbolRef(Sym, this));
756 }
757
758 template <class ELFT>
759 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
760   const Elf_Shdr *SymTab = EF.getDotSymtabSec();
761   if (!SymTab)
762     return symbol_begin_impl();
763   DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
764   return basic_symbol_iterator(SymbolRef(Sym, this));
765 }
766
767 template <class ELFT>
768 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
769   DataRefImpl Sym = toDRI(EF.getDotDynSymSec(), 0);
770   return symbol_iterator(SymbolRef(Sym, this));
771 }
772
773 template <class ELFT>
774 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
775   const Elf_Shdr *SymTab = EF.getDotDynSymSec();
776   DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
777   return basic_symbol_iterator(SymbolRef(Sym, this));
778 }
779
780 template <class ELFT>
781 section_iterator ELFObjectFile<ELFT>::section_begin() const {
782   return section_iterator(SectionRef(toDRI(EF.section_begin()), this));
783 }
784
785 template <class ELFT>
786 section_iterator ELFObjectFile<ELFT>::section_end() const {
787   return section_iterator(SectionRef(toDRI(EF.section_end()), this));
788 }
789
790 template <class ELFT>
791 StringRef ELFObjectFile<ELFT>::getLoadName() const {
792   Elf_Dyn_Iter DI = EF.dynamic_table_begin();
793   Elf_Dyn_Iter DE = EF.dynamic_table_end();
794
795   while (DI != DE && DI->getTag() != ELF::DT_SONAME)
796     ++DI;
797
798   if (DI != DE)
799     return EF.getDynamicString(DI->getVal());
800   return "";
801 }
802
803 template <class ELFT>
804 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
805   return ELFT::Is64Bits ? 8 : 4;
806 }
807
808 template <class ELFT>
809 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
810   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
811   switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
812   case ELF::ELFCLASS32:
813     switch (EF.getHeader()->e_machine) {
814     case ELF::EM_386:
815       return "ELF32-i386";
816     case ELF::EM_X86_64:
817       return "ELF32-x86-64";
818     case ELF::EM_ARM:
819       return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
820     case ELF::EM_HEXAGON:
821       return "ELF32-hexagon";
822     case ELF::EM_MIPS:
823       return "ELF32-mips";
824     case ELF::EM_PPC:
825       return "ELF32-ppc";
826     case ELF::EM_SPARC:
827     case ELF::EM_SPARC32PLUS:
828       return "ELF32-sparc";
829     default:
830       return "ELF32-unknown";
831     }
832   case ELF::ELFCLASS64:
833     switch (EF.getHeader()->e_machine) {
834     case ELF::EM_386:
835       return "ELF64-i386";
836     case ELF::EM_X86_64:
837       return "ELF64-x86-64";
838     case ELF::EM_AARCH64:
839       return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
840     case ELF::EM_PPC64:
841       return "ELF64-ppc64";
842     case ELF::EM_S390:
843       return "ELF64-s390";
844     case ELF::EM_SPARCV9:
845       return "ELF64-sparc";
846     case ELF::EM_MIPS:
847       return "ELF64-mips";
848     default:
849       return "ELF64-unknown";
850     }
851   default:
852     // FIXME: Proper error handling.
853     report_fatal_error("Invalid ELFCLASS!");
854   }
855 }
856
857 template <class ELFT>
858 unsigned ELFObjectFile<ELFT>::getArch() const {
859   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
860   switch (EF.getHeader()->e_machine) {
861   case ELF::EM_386:
862     return Triple::x86;
863   case ELF::EM_X86_64:
864     return Triple::x86_64;
865   case ELF::EM_AARCH64:
866     return Triple::aarch64;
867   case ELF::EM_ARM:
868     return Triple::arm;
869   case ELF::EM_HEXAGON:
870     return Triple::hexagon;
871   case ELF::EM_MIPS:
872     switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
873     case ELF::ELFCLASS32:
874       return IsLittleEndian ? Triple::mipsel : Triple::mips;
875     case ELF::ELFCLASS64:
876       return IsLittleEndian ? Triple::mips64el : Triple::mips64;
877     default:
878       report_fatal_error("Invalid ELFCLASS!");
879     }
880   case ELF::EM_PPC:
881     return Triple::ppc;
882   case ELF::EM_PPC64:
883     return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
884   case ELF::EM_S390:
885     return Triple::systemz;
886
887   case ELF::EM_SPARC:
888   case ELF::EM_SPARC32PLUS:
889     return IsLittleEndian ? Triple::sparcel : Triple::sparc;
890   case ELF::EM_SPARCV9:
891     return Triple::sparcv9;
892
893   default:
894     return Triple::UnknownArch;
895   }
896 }
897
898 template <class ELFT>
899 ELFObjectFileBase::elf_symbol_iterator_range
900 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
901   return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
902 }
903
904 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
905   return EF.getHeader()->e_type == ELF::ET_REL;
906 }
907
908 }
909 }
910
911 #endif