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