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