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