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