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