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