Remove a report_fatal_error that should be unreachable.
[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   std::error_code getSymbolName(DataRefImpl Symb,
199                                 StringRef &Res) const override;
200   std::error_code getSymbolAddress(DataRefImpl Symb,
201                                    uint64_t &Res) const override;
202   uint64_t getSymbolValue(DataRefImpl Symb) const override;
203   uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
204   uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
205   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
206   uint8_t getSymbolOther(DataRefImpl Symb) const override;
207   uint8_t getSymbolELFType(DataRefImpl Symb) const override;
208   SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
209   section_iterator getSymbolSection(const Elf_Sym *Symb) const;
210   std::error_code getSymbolSection(DataRefImpl Symb,
211                                    section_iterator &Res) const override;
212
213   void moveSectionNext(DataRefImpl &Sec) const override;
214   std::error_code getSectionName(DataRefImpl Sec,
215                                  StringRef &Res) const override;
216   uint64_t getSectionAddress(DataRefImpl Sec) const override;
217   uint64_t getSectionSize(DataRefImpl Sec) const override;
218   std::error_code getSectionContents(DataRefImpl Sec,
219                                      StringRef &Res) const override;
220   uint64_t getSectionAlignment(DataRefImpl Sec) const override;
221   bool isSectionText(DataRefImpl Sec) const override;
222   bool isSectionData(DataRefImpl Sec) const override;
223   bool isSectionBSS(DataRefImpl Sec) const override;
224   bool isSectionVirtual(DataRefImpl Sec) const override;
225   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
226   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
227   section_iterator getRelocatedSection(DataRefImpl Sec) const override;
228
229   void moveRelocationNext(DataRefImpl &Rel) const override;
230   ErrorOr<uint64_t> getRelocationAddress(DataRefImpl Rel) const override;
231   uint64_t getRelocationOffset(DataRefImpl Rel) const override;
232   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
233   uint64_t getRelocationType(DataRefImpl Rel) const override;
234   void getRelocationTypeName(DataRefImpl Rel,
235                              SmallVectorImpl<char> &Result) const override;
236
237   uint32_t getSectionType(DataRefImpl Sec) const override;
238   uint64_t getSectionFlags(DataRefImpl Sec) const override;
239   uint64_t getROffset(DataRefImpl Rel) const;
240   StringRef getRelocationTypeName(uint32_t Type) const;
241
242   /// \brief Get the relocation section that contains \a Rel.
243   const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
244     return *EF.getSection(Rel.d.a);
245   }
246
247   const Elf_Sym *toELFSymIter(DataRefImpl Sym) const {
248     return reinterpret_cast<const Elf_Sym *>(Sym.p & ~uintptr_t(1));
249   }
250
251   DataRefImpl toDRI(const Elf_Sym *Sym, bool IsDynamic) const {
252     DataRefImpl DRI;
253     DRI.p =
254         reinterpret_cast<uintptr_t>(Sym) | static_cast<uintptr_t>(IsDynamic);
255     return DRI;
256   }
257
258   const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
259     return reinterpret_cast<const Elf_Shdr *>(Sec.p);
260   }
261
262   DataRefImpl toDRI(const Elf_Shdr *Sec) const {
263     DataRefImpl DRI;
264     DRI.p = reinterpret_cast<uintptr_t>(Sec);
265     return DRI;
266   }
267
268   Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
269     return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
270                         reinterpret_cast<const char *>(Dyn.p));
271   }
272
273   DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
274     DataRefImpl DRI;
275     DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
276     return DRI;
277   }
278
279   bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
280     unsigned char Binding = ESym->getBinding();
281     unsigned char Visibility = ESym->getVisibility();
282
283     // A symbol is exported if its binding is either GLOBAL or WEAK, and its
284     // visibility is either DEFAULT or PROTECTED. All other symbols are not
285     // exported.
286     if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
287         (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
288       return true;
289
290     return false;
291   }
292
293   // This flag is used for classof, to distinguish ELFObjectFile from
294   // its subclass. If more subclasses will be created, this flag will
295   // have to become an enum.
296   bool isDyldELFObject;
297
298 public:
299   ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
300
301   const Elf_Rel *getRel(DataRefImpl Rel) const;
302   const Elf_Rela *getRela(DataRefImpl Rela) const;
303
304   const Elf_Sym *getSymbol(DataRefImpl Symb) const;
305
306   basic_symbol_iterator symbol_begin_impl() const override;
307   basic_symbol_iterator symbol_end_impl() const override;
308
309   elf_symbol_iterator dynamic_symbol_begin() const;
310   elf_symbol_iterator dynamic_symbol_end() const;
311
312   section_iterator section_begin() const override;
313   section_iterator section_end() const override;
314
315   ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
316
317   uint8_t getBytesInAddress() const override;
318   StringRef getFileFormatName() const override;
319   unsigned getArch() const override;
320   StringRef getLoadName() const;
321
322   std::error_code getPlatformFlags(unsigned &Result) const override {
323     Result = EF.getHeader()->e_flags;
324     return std::error_code();
325   }
326
327   const ELFFile<ELFT> *getELFFile() const { return &EF; }
328
329   bool isDyldType() const { return isDyldELFObject; }
330   static inline bool classof(const Binary *v) {
331     return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
332                                       ELFT::Is64Bits);
333   }
334
335   elf_symbol_iterator_range getDynamicSymbolIterators() const override;
336
337   bool isRelocatableObject() const override;
338 };
339
340 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
341 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
342 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
343 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
344
345 template <class ELFT>
346 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
347   const Elf_Sym *S = toELFSymIter(Sym);
348   Sym = toDRI(++S, Sym.p & 1);
349 }
350
351 template <class ELFT>
352 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym,
353                                                    StringRef &Result) const {
354   const Elf_Sym *ESym = toELFSymIter(Sym);
355   ErrorOr<StringRef> Name = EF.getSymbolName(ESym, Sym.p & 1);
356   if (!Name)
357     return Name.getError();
358   Result = *Name;
359   return std::error_code();
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 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
396                                                       uint64_t &Result) const {
397   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 std::error_code();
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 std::error_code();
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     ErrorOr<StringRef> NameOrErr = EF.getSymbolName(ESym, Sym.p & 1);
491     if (NameOrErr) {
492       StringRef Name = *NameOrErr;
493       if (Name.startswith("$d") || Name.startswith("$t") ||
494           Name.startswith("$a"))
495         Result |= SymbolRef::SF_FormatSpecific;
496     }
497   }
498
499   if (ESym->st_shndx == ELF::SHN_UNDEF)
500     Result |= SymbolRef::SF_Undefined;
501
502   if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
503     Result |= SymbolRef::SF_Common;
504
505   if (isExportedToOtherDSO(ESym))
506     Result |= SymbolRef::SF_Exported;
507
508   if (ESym->getVisibility() == ELF::STV_HIDDEN)
509     Result |= SymbolRef::SF_Hidden;
510
511   return Result;
512 }
513
514 template <class ELFT>
515 section_iterator
516 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
517   ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym);
518   if (std::error_code EC = ESecOrErr.getError())
519     report_fatal_error(EC.message());
520
521   const Elf_Shdr *ESec = *ESecOrErr;
522   if (!ESec)
523     return section_end();
524
525   DataRefImpl Sec;
526   Sec.p = reinterpret_cast<intptr_t>(ESec);
527   return section_iterator(SectionRef(Sec, this));
528 }
529
530 template <class ELFT>
531 std::error_code
532 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
533                                       section_iterator &Res) const {
534   Res = getSymbolSection(getSymbol(Symb));
535   return std::error_code();
536 }
537
538 template <class ELFT>
539 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
540   const Elf_Shdr *ESec = toELFShdrIter(Sec);
541   Sec = toDRI(++ESec);
542 }
543
544 template <class ELFT>
545 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
546                                                     StringRef &Result) const {
547   ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
548   if (!Name)
549     return Name.getError();
550   Result = *Name;
551   return std::error_code();
552 }
553
554 template <class ELFT>
555 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
556   return toELFShdrIter(Sec)->sh_addr;
557 }
558
559 template <class ELFT>
560 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
561   return toELFShdrIter(Sec)->sh_size;
562 }
563
564 template <class ELFT>
565 std::error_code
566 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
567                                         StringRef &Result) const {
568   const Elf_Shdr *EShdr = toELFShdrIter(Sec);
569   Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
570   return std::error_code();
571 }
572
573 template <class ELFT>
574 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
575   return toELFShdrIter(Sec)->sh_addralign;
576 }
577
578 template <class ELFT>
579 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
580   return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
581 }
582
583 template <class ELFT>
584 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
585   const Elf_Shdr *EShdr = toELFShdrIter(Sec);
586   return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
587          EShdr->sh_type == ELF::SHT_PROGBITS;
588 }
589
590 template <class ELFT>
591 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
592   const Elf_Shdr *EShdr = toELFShdrIter(Sec);
593   return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
594          EShdr->sh_type == ELF::SHT_NOBITS;
595 }
596
597 template <class ELFT>
598 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
599   return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
600 }
601
602 template <class ELFT>
603 relocation_iterator
604 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
605   DataRefImpl RelData;
606   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
607   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
608   RelData.d.b = 0;
609
610   const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
611   if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
612     return relocation_iterator(RelocationRef(RelData, this));
613
614   const Elf_Shdr *RelSec = getRelSection(RelData);
615   ErrorOr<const Elf_Shdr *> SymSecOrErr = EF.getSection(RelSec->sh_link);
616   if (std::error_code EC = SymSecOrErr.getError())
617     report_fatal_error(EC.message());
618   const Elf_Shdr *SymSec = *SymSecOrErr;
619   uint32_t SymSecType = SymSec->sh_type;
620   if (SymSecType != ELF::SHT_SYMTAB && SymSecType != ELF::SHT_DYNSYM)
621     report_fatal_error("Invalid symbol table section type!");
622   if (SymSecType == ELF::SHT_DYNSYM)
623     RelData.d.b = 1;
624
625   return relocation_iterator(RelocationRef(RelData, this));
626 }
627
628 template <class ELFT>
629 relocation_iterator
630 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
631   const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
632   relocation_iterator Begin = section_rel_begin(Sec);
633   if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
634     return Begin;
635   DataRefImpl RelData = Begin->getRawDataRefImpl();
636   RelData.d.b += (S->sh_size / S->sh_entsize) << 1;
637   return relocation_iterator(RelocationRef(RelData, this));
638 }
639
640 template <class ELFT>
641 section_iterator
642 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
643   if (EF.getHeader()->e_type != ELF::ET_REL)
644     return section_end();
645
646   const Elf_Shdr *EShdr = toELFShdrIter(Sec);
647   uintX_t Type = EShdr->sh_type;
648   if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
649     return section_end();
650
651   ErrorOr<const Elf_Shdr *> R = EF.getSection(EShdr->sh_info);
652   if (std::error_code EC = R.getError())
653     report_fatal_error(EC.message());
654   return section_iterator(SectionRef(toDRI(*R), this));
655 }
656
657 // Relocations
658 template <class ELFT>
659 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
660   Rel.d.b += 2;
661 }
662
663 template <class ELFT>
664 symbol_iterator
665 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
666   uint32_t symbolIdx;
667   const Elf_Shdr *sec = getRelSection(Rel);
668   if (sec->sh_type == ELF::SHT_REL)
669     symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
670   else
671     symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
672   if (!symbolIdx)
673     return symbol_end();
674
675   bool IsDyn = Rel.d.b & 1;
676   DataRefImpl SymbolData;
677   if (IsDyn)
678     SymbolData = toDRI(EF.dynamic_symbol_begin() + symbolIdx, true);
679   else
680     SymbolData = toDRI(EF.symbol_begin() + symbolIdx, false);
681   return symbol_iterator(SymbolRef(SymbolData, this));
682 }
683
684 template <class ELFT>
685 ErrorOr<uint64_t>
686 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel) const {
687   uint64_t ROffset = getROffset(Rel);
688   const Elf_Ehdr *Header = EF.getHeader();
689
690   if (Header->e_type == ELF::ET_REL) {
691     const Elf_Shdr *RelocationSec = getRelSection(Rel);
692     ErrorOr<const Elf_Shdr *> RelocatedSec =
693         EF.getSection(RelocationSec->sh_info);
694     if (std::error_code EC = RelocatedSec.getError())
695       return EC;
696     return ROffset + (*RelocatedSec)->sh_addr;
697   }
698   return ROffset;
699 }
700
701 template <class ELFT>
702 uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
703   assert(EF.getHeader()->e_type == ELF::ET_REL &&
704          "Only relocatable object files have relocation offsets");
705   return getROffset(Rel);
706 }
707
708 template <class ELFT>
709 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
710   const Elf_Shdr *sec = getRelSection(Rel);
711   if (sec->sh_type == ELF::SHT_REL)
712     return getRel(Rel)->r_offset;
713
714   return getRela(Rel)->r_offset;
715 }
716
717 template <class ELFT>
718 uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
719   const Elf_Shdr *sec = getRelSection(Rel);
720   if (sec->sh_type == ELF::SHT_REL)
721     return getRel(Rel)->getType(EF.isMips64EL());
722   else
723     return getRela(Rel)->getType(EF.isMips64EL());
724 }
725
726 template <class ELFT>
727 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
728   return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
729 }
730
731 template <class ELFT>
732 void ELFObjectFile<ELFT>::getRelocationTypeName(
733     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
734   uint32_t type = getRelocationType(Rel);
735   EF.getRelocationTypeName(type, Result);
736 }
737
738 template <class ELFT>
739 ErrorOr<int64_t>
740 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
741   if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
742     return object_error::parse_failed;
743   return (int64_t)getRela(Rel)->r_addend;
744 }
745
746 template <class ELFT>
747 const typename ELFFile<ELFT>::Elf_Sym *
748 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
749   return &*toELFSymIter(Symb);
750 }
751
752 template <class ELFT>
753 const typename ELFObjectFile<ELFT>::Elf_Rel *
754 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
755   assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
756   return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b >> 1);
757 }
758
759 template <class ELFT>
760 const typename ELFObjectFile<ELFT>::Elf_Rela *
761 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
762   assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
763   return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b >> 1);
764 }
765
766 template <class ELFT>
767 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
768     : ELFObjectFileBase(
769           getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
770                          support::little,
771                      ELFT::Is64Bits),
772           Object),
773       EF(Data.getBuffer(), EC) {}
774
775 template <class ELFT>
776 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
777   DataRefImpl Sym = toDRI(EF.symbol_begin(), false);
778   return basic_symbol_iterator(SymbolRef(Sym, this));
779 }
780
781 template <class ELFT>
782 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
783   DataRefImpl Sym = toDRI(EF.symbol_end(), false);
784   return basic_symbol_iterator(SymbolRef(Sym, this));
785 }
786
787 template <class ELFT>
788 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
789   DataRefImpl Sym = toDRI(EF.dynamic_symbol_begin(), true);
790   return symbol_iterator(SymbolRef(Sym, this));
791 }
792
793 template <class ELFT>
794 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
795   DataRefImpl Sym = toDRI(EF.dynamic_symbol_end(), true);
796   return symbol_iterator(SymbolRef(Sym, this));
797 }
798
799 template <class ELFT>
800 section_iterator ELFObjectFile<ELFT>::section_begin() const {
801   return section_iterator(SectionRef(toDRI(EF.section_begin()), this));
802 }
803
804 template <class ELFT>
805 section_iterator ELFObjectFile<ELFT>::section_end() const {
806   return section_iterator(SectionRef(toDRI(EF.section_end()), this));
807 }
808
809 template <class ELFT>
810 StringRef ELFObjectFile<ELFT>::getLoadName() const {
811   Elf_Dyn_Iter DI = EF.dynamic_table_begin();
812   Elf_Dyn_Iter DE = EF.dynamic_table_end();
813
814   while (DI != DE && DI->getTag() != ELF::DT_SONAME)
815     ++DI;
816
817   if (DI != DE)
818     return EF.getDynamicString(DI->getVal());
819   return "";
820 }
821
822 template <class ELFT>
823 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
824   return ELFT::Is64Bits ? 8 : 4;
825 }
826
827 template <class ELFT>
828 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
829   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
830   switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
831   case ELF::ELFCLASS32:
832     switch (EF.getHeader()->e_machine) {
833     case ELF::EM_386:
834       return "ELF32-i386";
835     case ELF::EM_X86_64:
836       return "ELF32-x86-64";
837     case ELF::EM_ARM:
838       return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
839     case ELF::EM_HEXAGON:
840       return "ELF32-hexagon";
841     case ELF::EM_MIPS:
842       return "ELF32-mips";
843     case ELF::EM_PPC:
844       return "ELF32-ppc";
845     case ELF::EM_SPARC:
846     case ELF::EM_SPARC32PLUS:
847       return "ELF32-sparc";
848     default:
849       return "ELF32-unknown";
850     }
851   case ELF::ELFCLASS64:
852     switch (EF.getHeader()->e_machine) {
853     case ELF::EM_386:
854       return "ELF64-i386";
855     case ELF::EM_X86_64:
856       return "ELF64-x86-64";
857     case ELF::EM_AARCH64:
858       return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
859     case ELF::EM_PPC64:
860       return "ELF64-ppc64";
861     case ELF::EM_S390:
862       return "ELF64-s390";
863     case ELF::EM_SPARCV9:
864       return "ELF64-sparc";
865     case ELF::EM_MIPS:
866       return "ELF64-mips";
867     default:
868       return "ELF64-unknown";
869     }
870   default:
871     // FIXME: Proper error handling.
872     report_fatal_error("Invalid ELFCLASS!");
873   }
874 }
875
876 template <class ELFT>
877 unsigned ELFObjectFile<ELFT>::getArch() const {
878   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
879   switch (EF.getHeader()->e_machine) {
880   case ELF::EM_386:
881     return Triple::x86;
882   case ELF::EM_X86_64:
883     return Triple::x86_64;
884   case ELF::EM_AARCH64:
885     return Triple::aarch64;
886   case ELF::EM_ARM:
887     return Triple::arm;
888   case ELF::EM_HEXAGON:
889     return Triple::hexagon;
890   case ELF::EM_MIPS:
891     switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
892     case ELF::ELFCLASS32:
893       return IsLittleEndian ? Triple::mipsel : Triple::mips;
894     case ELF::ELFCLASS64:
895       return IsLittleEndian ? Triple::mips64el : Triple::mips64;
896     default:
897       report_fatal_error("Invalid ELFCLASS!");
898     }
899   case ELF::EM_PPC:
900     return Triple::ppc;
901   case ELF::EM_PPC64:
902     return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
903   case ELF::EM_S390:
904     return Triple::systemz;
905
906   case ELF::EM_SPARC:
907   case ELF::EM_SPARC32PLUS:
908     return IsLittleEndian ? Triple::sparcel : Triple::sparc;
909   case ELF::EM_SPARCV9:
910     return Triple::sparcv9;
911
912   default:
913     return Triple::UnknownArch;
914   }
915 }
916
917 template <class ELFT>
918 ELFObjectFileBase::elf_symbol_iterator_range
919 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
920   return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
921 }
922
923 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
924   return EF.getHeader()->e_type == ELF::ET_REL;
925 }
926
927 }
928 }
929
930 #endif