Convert getSymbolSection to return an ErrorOr.
[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 protected:
193   ELFFile<ELFT> EF;
194
195   const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
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 getSymbolValueImpl(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   ErrorOr<section_iterator> getSymbolSection(const Elf_Sym *Symb) const;
208   ErrorOr<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
209
210   void moveSectionNext(DataRefImpl &Sec) const override;
211   std::error_code getSectionName(DataRefImpl Sec,
212                                  StringRef &Res) const override;
213   uint64_t getSectionAddress(DataRefImpl Sec) const override;
214   uint64_t getSectionSize(DataRefImpl Sec) const override;
215   std::error_code getSectionContents(DataRefImpl Sec,
216                                      StringRef &Res) const override;
217   uint64_t getSectionAlignment(DataRefImpl Sec) const override;
218   bool isSectionText(DataRefImpl Sec) const override;
219   bool isSectionData(DataRefImpl Sec) const override;
220   bool isSectionBSS(DataRefImpl Sec) const override;
221   bool isSectionVirtual(DataRefImpl Sec) const override;
222   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
223   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
224   section_iterator getRelocatedSection(DataRefImpl Sec) const override;
225
226   void moveRelocationNext(DataRefImpl &Rel) const override;
227   uint64_t getRelocationOffset(DataRefImpl Rel) const override;
228   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
229   uint64_t getRelocationType(DataRefImpl Rel) const override;
230   void getRelocationTypeName(DataRefImpl Rel,
231                              SmallVectorImpl<char> &Result) const override;
232
233   uint32_t getSectionType(DataRefImpl Sec) const override;
234   uint64_t getSectionFlags(DataRefImpl Sec) const override;
235   StringRef getRelocationTypeName(uint32_t Type) const;
236
237   /// \brief Get the relocation section that contains \a Rel.
238   const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
239     return *EF.getSection(Rel.d.a);
240   }
241
242   const Elf_Sym *toELFSymIter(DataRefImpl Sym) const {
243     return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
244   }
245
246   DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
247     DataRefImpl DRI;
248     if (!SymTable) {
249       DRI.d.a = 0;
250       DRI.d.b = 0;
251       return DRI;
252     }
253     assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
254            SymTable->sh_type == ELF::SHT_DYNSYM);
255
256     uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
257     unsigned SymTableIndex =
258         (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
259
260     DRI.d.a = SymTableIndex;
261     DRI.d.b = SymbolNum;
262     return DRI;
263   }
264
265   const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
266     return reinterpret_cast<const Elf_Shdr *>(Sec.p);
267   }
268
269   DataRefImpl toDRI(const Elf_Shdr *Sec) const {
270     DataRefImpl DRI;
271     DRI.p = reinterpret_cast<uintptr_t>(Sec);
272     return DRI;
273   }
274
275   DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
276     DataRefImpl DRI;
277     DRI.p = reinterpret_cast<uintptr_t>(Dyn);
278     return DRI;
279   }
280
281   bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
282     unsigned char Binding = ESym->getBinding();
283     unsigned char Visibility = ESym->getVisibility();
284
285     // A symbol is exported if its binding is either GLOBAL or WEAK, and its
286     // visibility is either DEFAULT or PROTECTED. All other symbols are not
287     // exported.
288     if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
289         (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
290       return true;
291
292     return false;
293   }
294
295   // This flag is used for classof, to distinguish ELFObjectFile from
296   // its subclass. If more subclasses will be created, this flag will
297   // have to become an enum.
298   bool isDyldELFObject;
299
300 public:
301   ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
302
303   const Elf_Rel *getRel(DataRefImpl Rel) const;
304   const Elf_Rela *getRela(DataRefImpl Rela) const;
305
306   const Elf_Sym *getSymbol(DataRefImpl Symb) const;
307
308   basic_symbol_iterator symbol_begin_impl() const override;
309   basic_symbol_iterator symbol_end_impl() const override;
310
311   elf_symbol_iterator dynamic_symbol_begin() const;
312   elf_symbol_iterator dynamic_symbol_end() const;
313
314   section_iterator section_begin() const override;
315   section_iterator section_end() const override;
316
317   ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
318
319   uint8_t getBytesInAddress() const override;
320   StringRef getFileFormatName() const override;
321   unsigned getArch() const override;
322
323   std::error_code getPlatformFlags(unsigned &Result) const override {
324     Result = EF.getHeader()->e_flags;
325     return std::error_code();
326   }
327
328   const ELFFile<ELFT> *getELFFile() const { return &EF; }
329
330   bool isDyldType() const { return isDyldELFObject; }
331   static inline bool classof(const Binary *v) {
332     return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
333                                       ELFT::Is64Bits);
334   }
335
336   elf_symbol_iterator_range getDynamicSymbolIterators() const override;
337
338   bool isRelocatableObject() const override;
339 };
340
341 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
342 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
343 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
344 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
345
346 template <class ELFT>
347 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
348   ++Sym.d.b;
349 }
350
351 template <class ELFT>
352 ErrorOr<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const {
353   const Elf_Sym *ESym = toELFSymIter(Sym);
354   const Elf_Shdr *SymTableSec = *EF.getSection(Sym.d.a);
355   const Elf_Shdr *StringTableSec = *EF.getSection(SymTableSec->sh_link);
356   StringRef SymTable = *EF.getStringTable(StringTableSec);
357   return ESym->getName(SymTable);
358 }
359
360 template <class ELFT>
361 uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
362   return toELFShdrIter(Sec)->sh_flags;
363 }
364
365 template <class ELFT>
366 uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
367   return toELFShdrIter(Sec)->sh_type;
368 }
369
370 template <class ELFT>
371 uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const {
372   const Elf_Sym *ESym = getSymbol(Symb);
373   uint64_t Ret = ESym->st_value;
374   if (ESym->st_shndx == ELF::SHN_ABS)
375     return Ret;
376
377   const Elf_Ehdr *Header = EF.getHeader();
378   // Clear the ARM/Thumb or microMIPS indicator flag.
379   if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
380       ESym->getType() == ELF::STT_FUNC)
381     Ret &= ~1;
382
383   return Ret;
384 }
385
386 template <class ELFT>
387 ErrorOr<uint64_t>
388 ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
389   uint64_t Result = getSymbolValue(Symb);
390   const Elf_Sym *ESym = getSymbol(Symb);
391   switch (ESym->st_shndx) {
392   case ELF::SHN_COMMON:
393   case ELF::SHN_UNDEF:
394   case ELF::SHN_ABS:
395     return Result;
396   }
397
398   const Elf_Ehdr *Header = EF.getHeader();
399
400   if (Header->e_type == ELF::ET_REL) {
401     ErrorOr<const Elf_Shdr *> SectionOrErr = EF.getSection(ESym);
402     if (std::error_code EC = SectionOrErr.getError())
403       return EC;
404     const Elf_Shdr *Section = *SectionOrErr;
405     if (Section)
406       Result += Section->sh_addr;
407   }
408
409   return Result;
410 }
411
412 template <class ELFT>
413 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
414   const Elf_Sym *Sym = toELFSymIter(Symb);
415   if (Sym->st_shndx == ELF::SHN_COMMON)
416     return Sym->st_value;
417   return 0;
418 }
419
420 template <class ELFT>
421 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
422   return toELFSymIter(Sym)->st_size;
423 }
424
425 template <class ELFT>
426 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
427   return toELFSymIter(Symb)->st_size;
428 }
429
430 template <class ELFT>
431 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
432   return toELFSymIter(Symb)->st_other;
433 }
434
435 template <class ELFT>
436 uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
437   return toELFSymIter(Symb)->getType();
438 }
439
440 template <class ELFT>
441 SymbolRef::Type ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
442   const Elf_Sym *ESym = getSymbol(Symb);
443
444   switch (ESym->getType()) {
445   case ELF::STT_NOTYPE:
446     return SymbolRef::ST_Unknown;
447   case ELF::STT_SECTION:
448     return SymbolRef::ST_Debug;
449   case ELF::STT_FILE:
450     return SymbolRef::ST_File;
451   case ELF::STT_FUNC:
452     return SymbolRef::ST_Function;
453   case ELF::STT_OBJECT:
454   case ELF::STT_COMMON:
455   case ELF::STT_TLS:
456     return SymbolRef::ST_Data;
457   default:
458     return SymbolRef::ST_Other;
459   }
460 }
461
462 template <class ELFT>
463 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
464   const Elf_Sym *ESym = toELFSymIter(Sym);
465
466   uint32_t Result = SymbolRef::SF_None;
467
468   if (ESym->getBinding() != ELF::STB_LOCAL)
469     Result |= SymbolRef::SF_Global;
470
471   if (ESym->getBinding() == ELF::STB_WEAK)
472     Result |= SymbolRef::SF_Weak;
473
474   if (ESym->st_shndx == ELF::SHN_ABS)
475     Result |= SymbolRef::SF_Absolute;
476
477   if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
478       ESym == EF.symbol_begin(EF.getDotSymtabSec()) ||
479       ESym == EF.symbol_begin(DotDynSymSec))
480     Result |= SymbolRef::SF_FormatSpecific;
481
482   if (EF.getHeader()->e_machine == ELF::EM_ARM) {
483     if (ErrorOr<StringRef> NameOrErr = getSymbolName(Sym)) {
484       StringRef Name = *NameOrErr;
485       if (Name.startswith("$d") || Name.startswith("$t") ||
486           Name.startswith("$a"))
487         Result |= SymbolRef::SF_FormatSpecific;
488     }
489   }
490
491   if (ESym->st_shndx == ELF::SHN_UNDEF)
492     Result |= SymbolRef::SF_Undefined;
493
494   if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
495     Result |= SymbolRef::SF_Common;
496
497   if (isExportedToOtherDSO(ESym))
498     Result |= SymbolRef::SF_Exported;
499
500   if (ESym->getVisibility() == ELF::STV_HIDDEN)
501     Result |= SymbolRef::SF_Hidden;
502
503   return Result;
504 }
505
506 template <class ELFT>
507 ErrorOr<section_iterator>
508 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
509   ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym);
510   if (std::error_code EC = ESecOrErr.getError())
511     return EC;
512
513   const Elf_Shdr *ESec = *ESecOrErr;
514   if (!ESec)
515     return section_end();
516
517   DataRefImpl Sec;
518   Sec.p = reinterpret_cast<intptr_t>(ESec);
519   return section_iterator(SectionRef(Sec, this));
520 }
521
522 template <class ELFT>
523 ErrorOr<section_iterator>
524 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb) const {
525   return getSymbolSection(getSymbol(Symb));
526 }
527
528 template <class ELFT>
529 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
530   const Elf_Shdr *ESec = toELFShdrIter(Sec);
531   Sec = toDRI(++ESec);
532 }
533
534 template <class ELFT>
535 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
536                                                     StringRef &Result) const {
537   ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
538   if (!Name)
539     return Name.getError();
540   Result = *Name;
541   return std::error_code();
542 }
543
544 template <class ELFT>
545 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
546   return toELFShdrIter(Sec)->sh_addr;
547 }
548
549 template <class ELFT>
550 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
551   return toELFShdrIter(Sec)->sh_size;
552 }
553
554 template <class ELFT>
555 std::error_code
556 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
557                                         StringRef &Result) const {
558   const Elf_Shdr *EShdr = toELFShdrIter(Sec);
559   Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
560   return std::error_code();
561 }
562
563 template <class ELFT>
564 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
565   return toELFShdrIter(Sec)->sh_addralign;
566 }
567
568 template <class ELFT>
569 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
570   return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
571 }
572
573 template <class ELFT>
574 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
575   const Elf_Shdr *EShdr = toELFShdrIter(Sec);
576   return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
577          EShdr->sh_type == ELF::SHT_PROGBITS;
578 }
579
580 template <class ELFT>
581 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
582   const Elf_Shdr *EShdr = toELFShdrIter(Sec);
583   return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
584          EShdr->sh_type == ELF::SHT_NOBITS;
585 }
586
587 template <class ELFT>
588 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
589   return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
590 }
591
592 template <class ELFT>
593 relocation_iterator
594 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
595   DataRefImpl RelData;
596   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
597   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
598   RelData.d.b = 0;
599
600   const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
601   if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
602     return relocation_iterator(RelocationRef(RelData, this));
603
604   const Elf_Shdr *RelSec = getRelSection(RelData);
605   ErrorOr<const Elf_Shdr *> SymSecOrErr = EF.getSection(RelSec->sh_link);
606   if (std::error_code EC = SymSecOrErr.getError())
607     report_fatal_error(EC.message());
608   const Elf_Shdr *SymSec = *SymSecOrErr;
609   uint32_t SymSecType = SymSec->sh_type;
610   if (SymSecType != ELF::SHT_SYMTAB && SymSecType != ELF::SHT_DYNSYM)
611     report_fatal_error("Invalid symbol table section type!");
612   if (SymSecType == ELF::SHT_DYNSYM)
613     RelData.d.b = 1;
614
615   return relocation_iterator(RelocationRef(RelData, this));
616 }
617
618 template <class ELFT>
619 relocation_iterator
620 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
621   const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
622   relocation_iterator Begin = section_rel_begin(Sec);
623   if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
624     return Begin;
625   DataRefImpl RelData = Begin->getRawDataRefImpl();
626   RelData.d.b += (S->sh_size / S->sh_entsize) << 1;
627   return relocation_iterator(RelocationRef(RelData, this));
628 }
629
630 template <class ELFT>
631 section_iterator
632 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
633   if (EF.getHeader()->e_type != ELF::ET_REL)
634     return section_end();
635
636   const Elf_Shdr *EShdr = toELFShdrIter(Sec);
637   uintX_t Type = EShdr->sh_type;
638   if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
639     return section_end();
640
641   ErrorOr<const Elf_Shdr *> R = EF.getSection(EShdr->sh_info);
642   if (std::error_code EC = R.getError())
643     report_fatal_error(EC.message());
644   return section_iterator(SectionRef(toDRI(*R), this));
645 }
646
647 // Relocations
648 template <class ELFT>
649 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
650   Rel.d.b += 2;
651 }
652
653 template <class ELFT>
654 symbol_iterator
655 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
656   uint32_t symbolIdx;
657   const Elf_Shdr *sec = getRelSection(Rel);
658   if (sec->sh_type == ELF::SHT_REL)
659     symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
660   else
661     symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
662   if (!symbolIdx)
663     return symbol_end();
664
665   bool IsDyn = Rel.d.b & 1;
666   DataRefImpl SymbolData;
667   if (IsDyn)
668     SymbolData = toDRI(DotDynSymSec, symbolIdx);
669   else
670     SymbolData = toDRI(EF.getDotSymtabSec(), symbolIdx);
671   return symbol_iterator(SymbolRef(SymbolData, this));
672 }
673
674 template <class ELFT>
675 uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
676   assert(EF.getHeader()->e_type == ELF::ET_REL &&
677          "Only relocatable object files have relocation offsets");
678   const Elf_Shdr *sec = getRelSection(Rel);
679   if (sec->sh_type == ELF::SHT_REL)
680     return getRel(Rel)->r_offset;
681
682   return getRela(Rel)->r_offset;
683 }
684
685 template <class ELFT>
686 uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
687   const Elf_Shdr *sec = getRelSection(Rel);
688   if (sec->sh_type == ELF::SHT_REL)
689     return getRel(Rel)->getType(EF.isMips64EL());
690   else
691     return getRela(Rel)->getType(EF.isMips64EL());
692 }
693
694 template <class ELFT>
695 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
696   return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
697 }
698
699 template <class ELFT>
700 void ELFObjectFile<ELFT>::getRelocationTypeName(
701     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
702   uint32_t type = getRelocationType(Rel);
703   EF.getRelocationTypeName(type, Result);
704 }
705
706 template <class ELFT>
707 ErrorOr<int64_t>
708 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
709   if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
710     return object_error::parse_failed;
711   return (int64_t)getRela(Rel)->r_addend;
712 }
713
714 template <class ELFT>
715 const typename ELFFile<ELFT>::Elf_Sym *
716 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
717   return &*toELFSymIter(Symb);
718 }
719
720 template <class ELFT>
721 const typename ELFObjectFile<ELFT>::Elf_Rel *
722 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
723   assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
724   return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b >> 1);
725 }
726
727 template <class ELFT>
728 const typename ELFObjectFile<ELFT>::Elf_Rela *
729 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
730   assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
731   return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b >> 1);
732 }
733
734 template <class ELFT>
735 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
736     : ELFObjectFileBase(
737           getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
738           Object),
739       EF(Data.getBuffer(), EC) {
740   for (const Elf_Shdr &Sec : EF.sections()) {
741     switch (Sec.sh_type) {
742     case ELF::SHT_DYNSYM: {
743       if (DotDynSymSec) {
744         // More than one .dynsym!
745         EC = object_error::parse_failed;
746         return;
747       }
748       DotDynSymSec = &Sec;
749       break;
750     }
751     }
752   }
753 }
754
755 template <class ELFT>
756 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
757   DataRefImpl Sym = toDRI(EF.getDotSymtabSec(), 0);
758   return basic_symbol_iterator(SymbolRef(Sym, this));
759 }
760
761 template <class ELFT>
762 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
763   const Elf_Shdr *SymTab = EF.getDotSymtabSec();
764   if (!SymTab)
765     return symbol_begin_impl();
766   DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
767   return basic_symbol_iterator(SymbolRef(Sym, this));
768 }
769
770 template <class ELFT>
771 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
772   DataRefImpl Sym = toDRI(DotDynSymSec, 0);
773   return symbol_iterator(SymbolRef(Sym, this));
774 }
775
776 template <class ELFT>
777 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
778   const Elf_Shdr *SymTab = DotDynSymSec;
779   DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
780   return basic_symbol_iterator(SymbolRef(Sym, this));
781 }
782
783 template <class ELFT>
784 section_iterator ELFObjectFile<ELFT>::section_begin() const {
785   return section_iterator(SectionRef(toDRI(EF.section_begin()), this));
786 }
787
788 template <class ELFT>
789 section_iterator ELFObjectFile<ELFT>::section_end() const {
790   return section_iterator(SectionRef(toDRI(EF.section_end()), this));
791 }
792
793 template <class ELFT>
794 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
795   return ELFT::Is64Bits ? 8 : 4;
796 }
797
798 template <class ELFT>
799 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
800   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
801   switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
802   case ELF::ELFCLASS32:
803     switch (EF.getHeader()->e_machine) {
804     case ELF::EM_386:
805       return "ELF32-i386";
806     case ELF::EM_X86_64:
807       return "ELF32-x86-64";
808     case ELF::EM_ARM:
809       return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
810     case ELF::EM_HEXAGON:
811       return "ELF32-hexagon";
812     case ELF::EM_MIPS:
813       return "ELF32-mips";
814     case ELF::EM_PPC:
815       return "ELF32-ppc";
816     case ELF::EM_SPARC:
817     case ELF::EM_SPARC32PLUS:
818       return "ELF32-sparc";
819     default:
820       return "ELF32-unknown";
821     }
822   case ELF::ELFCLASS64:
823     switch (EF.getHeader()->e_machine) {
824     case ELF::EM_386:
825       return "ELF64-i386";
826     case ELF::EM_X86_64:
827       return "ELF64-x86-64";
828     case ELF::EM_AARCH64:
829       return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
830     case ELF::EM_PPC64:
831       return "ELF64-ppc64";
832     case ELF::EM_S390:
833       return "ELF64-s390";
834     case ELF::EM_SPARCV9:
835       return "ELF64-sparc";
836     case ELF::EM_MIPS:
837       return "ELF64-mips";
838     default:
839       return "ELF64-unknown";
840     }
841   default:
842     // FIXME: Proper error handling.
843     report_fatal_error("Invalid ELFCLASS!");
844   }
845 }
846
847 template <class ELFT>
848 unsigned ELFObjectFile<ELFT>::getArch() const {
849   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
850   switch (EF.getHeader()->e_machine) {
851   case ELF::EM_386:
852     return Triple::x86;
853   case ELF::EM_X86_64:
854     return Triple::x86_64;
855   case ELF::EM_AARCH64:
856     return Triple::aarch64;
857   case ELF::EM_ARM:
858     return Triple::arm;
859   case ELF::EM_HEXAGON:
860     return Triple::hexagon;
861   case ELF::EM_MIPS:
862     switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
863     case ELF::ELFCLASS32:
864       return IsLittleEndian ? Triple::mipsel : Triple::mips;
865     case ELF::ELFCLASS64:
866       return IsLittleEndian ? Triple::mips64el : Triple::mips64;
867     default:
868       report_fatal_error("Invalid ELFCLASS!");
869     }
870   case ELF::EM_PPC:
871     return Triple::ppc;
872   case ELF::EM_PPC64:
873     return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
874   case ELF::EM_S390:
875     return Triple::systemz;
876
877   case ELF::EM_SPARC:
878   case ELF::EM_SPARC32PLUS:
879     return IsLittleEndian ? Triple::sparcel : Triple::sparc;
880   case ELF::EM_SPARCV9:
881     return Triple::sparcv9;
882
883   default:
884     return Triple::UnknownArch;
885   }
886 }
887
888 template <class ELFT>
889 ELFObjectFileBase::elf_symbol_iterator_range
890 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
891   return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
892 }
893
894 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
895   return EF.getHeader()->e_type == ELF::ET_REL;
896 }
897
898 }
899 }
900
901 #endif