Don't get confused with sections whose section number is reserved.
[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 std::pair<symbol_iterator, symbol_iterator>
50   getELFDynamicSymbolIterators() const = 0;
51
52   virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
53   virtual uint32_t getSectionType(SectionRef Sec) const = 0;
54
55   virtual uint64_t getSymbolSize(SymbolRef Symb) const = 0;
56
57   static inline bool classof(const Binary *v) { return v->isELF(); }
58 };
59
60 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
61 public:
62   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
63
64   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
65
66   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
67   typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
68   typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
69   typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
70   typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
71   typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
72
73   typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
74   typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
75   typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
76
77   uint64_t getSymbolSize(SymbolRef Symb) const override;
78
79 protected:
80   ELFFile<ELFT> EF;
81
82   void moveSymbolNext(DataRefImpl &Symb) const override;
83   std::error_code getSymbolName(DataRefImpl Symb,
84                                 StringRef &Res) const override;
85   std::error_code getSymbolAddress(DataRefImpl Symb,
86                                    uint64_t &Res) 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   std::pair<symbol_iterator, symbol_iterator>
240   getELFDynamicSymbolIterators() const override;
241
242   bool isRelocatableObject() const override;
243 };
244
245 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
246 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
247 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
248 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
249
250 template <class ELFT>
251 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
252   Symb = toDRI(++toELFSymIter(Symb));
253 }
254
255 template <class ELFT>
256 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
257                                                    StringRef &Result) const {
258   ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
259   if (!Name)
260     return Name.getError();
261   Result = *Name;
262   return std::error_code();
263 }
264
265 template <class ELFT>
266 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
267   DataRefImpl DRI = Sec.getRawDataRefImpl();
268   return toELFShdrIter(DRI)->sh_flags;
269 }
270
271 template <class ELFT>
272 uint32_t ELFObjectFile<ELFT>::getSectionType(SectionRef Sec) const {
273   DataRefImpl DRI = Sec.getRawDataRefImpl();
274   return toELFShdrIter(DRI)->sh_type;
275 }
276
277 template <class ELFT>
278 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
279                                                       uint64_t &Result) const {
280   const Elf_Sym *ESym = getSymbol(Symb);
281   switch (ESym->st_shndx) {
282   case ELF::SHN_COMMON:
283   case ELF::SHN_UNDEF:
284     Result = UnknownAddress;
285     return std::error_code();
286   case ELF::SHN_ABS:
287     Result = ESym->st_value;
288     return std::error_code();
289   default:
290     break;
291   }
292
293   const Elf_Ehdr *Header = EF.getHeader();
294   Result = ESym->st_value;
295
296   // Clear the ARM/Thumb or microMIPS indicator flag.
297   if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
298       ESym->getType() == ELF::STT_FUNC)
299     Result &= ~1;
300
301   if (Header->e_type == ELF::ET_REL) {
302     const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
303     if (Section != nullptr)
304       Result += Section->sh_addr;
305   }
306
307   return std::error_code();
308 }
309
310 template <class ELFT>
311 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
312   Elf_Sym_Iter Sym = toELFSymIter(Symb);
313   if (Sym->st_shndx == ELF::SHN_COMMON)
314     return Sym->st_value;
315   return 0;
316 }
317
318 template <class ELFT>
319 uint64_t ELFObjectFile<ELFT>::getSymbolSize(SymbolRef Symb) const {
320   return toELFSymIter(Symb.getRawDataRefImpl())->st_size;
321 }
322
323 template <class ELFT>
324 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
325   return toELFSymIter(Symb)->st_size;
326 }
327
328 template <class ELFT>
329 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
330                                                     uint8_t &Result) const {
331   Result = toELFSymIter(Symb)->st_other;
332   return std::error_code();
333 }
334
335 template <class ELFT>
336 std::error_code
337 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
338                                    SymbolRef::Type &Result) const {
339   const Elf_Sym *ESym = getSymbol(Symb);
340
341   switch (ESym->getType()) {
342   case ELF::STT_NOTYPE:
343     Result = SymbolRef::ST_Unknown;
344     break;
345   case ELF::STT_SECTION:
346     Result = SymbolRef::ST_Debug;
347     break;
348   case ELF::STT_FILE:
349     Result = SymbolRef::ST_File;
350     break;
351   case ELF::STT_FUNC:
352     Result = SymbolRef::ST_Function;
353     break;
354   case ELF::STT_OBJECT:
355   case ELF::STT_COMMON:
356   case ELF::STT_TLS:
357     Result = SymbolRef::ST_Data;
358     break;
359   default:
360     Result = SymbolRef::ST_Other;
361     break;
362   }
363   return std::error_code();
364 }
365
366 template <class ELFT>
367 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
368   Elf_Sym_Iter EIter = toELFSymIter(Symb);
369   const Elf_Sym *ESym = &*EIter;
370
371   uint32_t Result = SymbolRef::SF_None;
372
373   if (ESym->getBinding() != ELF::STB_LOCAL)
374     Result |= SymbolRef::SF_Global;
375
376   if (ESym->getBinding() == ELF::STB_WEAK)
377     Result |= SymbolRef::SF_Weak;
378
379   if (ESym->st_shndx == ELF::SHN_ABS)
380     Result |= SymbolRef::SF_Absolute;
381
382   if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
383       EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
384     Result |= SymbolRef::SF_FormatSpecific;
385
386   if (ESym->st_shndx == ELF::SHN_UNDEF)
387     Result |= SymbolRef::SF_Undefined;
388
389   if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
390     Result |= SymbolRef::SF_Common;
391
392   if (isExportedToOtherDSO(ESym))
393     Result |= SymbolRef::SF_Exported;
394
395   if (ESym->getVisibility() == ELF::STV_HIDDEN)
396     Result |= SymbolRef::SF_Hidden;
397
398   return Result;
399 }
400
401 template <class ELFT>
402 section_iterator
403 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
404   const Elf_Shdr *ESec = EF.getSection(ESym);
405   if (!ESec)
406     return section_end();
407   else {
408     DataRefImpl Sec;
409     Sec.p = reinterpret_cast<intptr_t>(ESec);
410     return section_iterator(SectionRef(Sec, this));
411   }
412 }
413
414 template <class ELFT>
415 std::error_code
416 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
417                                       section_iterator &Res) const {
418   Res = getSymbolSection(getSymbol(Symb));
419   return std::error_code();
420 }
421
422 template <class ELFT>
423 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
424   Sec = toDRI(++toELFShdrIter(Sec));
425 }
426
427 template <class ELFT>
428 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
429                                                     StringRef &Result) const {
430   ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
431   if (!Name)
432     return Name.getError();
433   Result = *Name;
434   return std::error_code();
435 }
436
437 template <class ELFT>
438 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
439   return toELFShdrIter(Sec)->sh_addr;
440 }
441
442 template <class ELFT>
443 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
444   return toELFShdrIter(Sec)->sh_size;
445 }
446
447 template <class ELFT>
448 std::error_code
449 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
450                                         StringRef &Result) const {
451   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
452   Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
453   return std::error_code();
454 }
455
456 template <class ELFT>
457 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
458   return toELFShdrIter(Sec)->sh_addralign;
459 }
460
461 template <class ELFT>
462 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
463   return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
464 }
465
466 template <class ELFT>
467 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
468   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
469   return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
470          EShdr->sh_type == ELF::SHT_PROGBITS;
471 }
472
473 template <class ELFT>
474 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
475   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
476   return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
477          EShdr->sh_type == ELF::SHT_NOBITS;
478 }
479
480 template <class ELFT>
481 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
482   return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
483 }
484
485 template <class ELFT>
486 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
487                                                 DataRefImpl Symb) const {
488   Elf_Sym_Iter ESym = toELFSymIter(Symb);
489
490   uintX_t Index = ESym->st_shndx;
491   bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
492
493   return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
494 }
495
496 template <class ELFT>
497 relocation_iterator
498 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
499   DataRefImpl RelData;
500   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
501   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
502   RelData.d.b = 0;
503   return relocation_iterator(RelocationRef(RelData, this));
504 }
505
506 template <class ELFT>
507 relocation_iterator
508 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
509   DataRefImpl RelData;
510   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
511   const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
512   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
513   if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
514     RelData.d.b = 0;
515   else
516     RelData.d.b = S->sh_size / S->sh_entsize;
517
518   return relocation_iterator(RelocationRef(RelData, this));
519 }
520
521 template <class ELFT>
522 section_iterator
523 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
524   if (EF.getHeader()->e_type != ELF::ET_REL)
525     return section_end();
526
527   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
528   uintX_t Type = EShdr->sh_type;
529   if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
530     return section_end();
531
532   const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
533   return section_iterator(SectionRef(toDRI(R), this));
534 }
535
536 // Relocations
537 template <class ELFT>
538 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
539   ++Rel.d.b;
540 }
541
542 template <class ELFT>
543 symbol_iterator
544 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
545   uint32_t symbolIdx;
546   const Elf_Shdr *sec = getRelSection(Rel);
547   switch (sec->sh_type) {
548   default:
549     report_fatal_error("Invalid section type in Rel!");
550   case ELF::SHT_REL: {
551     symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
552     break;
553   }
554   case ELF::SHT_RELA: {
555     symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
556     break;
557   }
558   }
559   if (!symbolIdx)
560     return symbol_end();
561
562   const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
563
564   DataRefImpl SymbolData;
565   switch (SymSec->sh_type) {
566   default:
567     report_fatal_error("Invalid symbol table section type!");
568   case ELF::SHT_SYMTAB:
569     SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
570     break;
571   case ELF::SHT_DYNSYM:
572     SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
573     break;
574   }
575
576   return symbol_iterator(SymbolRef(SymbolData, this));
577 }
578
579 template <class ELFT>
580 std::error_code
581 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
582                                           uint64_t &Result) const {
583   uint64_t ROffset = getROffset(Rel);
584   const Elf_Ehdr *Header = EF.getHeader();
585
586   if (Header->e_type == ELF::ET_REL) {
587     const Elf_Shdr *RelocationSec = getRelSection(Rel);
588     const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
589     Result = ROffset + RelocatedSec->sh_addr;
590   } else {
591     Result = ROffset;
592   }
593
594   return std::error_code();
595 }
596
597 template <class ELFT>
598 std::error_code
599 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
600                                          uint64_t &Result) const {
601   assert(EF.getHeader()->e_type == ELF::ET_REL &&
602          "Only relocatable object files have relocation offsets");
603   Result = getROffset(Rel);
604   return std::error_code();
605 }
606
607 template <class ELFT>
608 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
609   const Elf_Shdr *sec = getRelSection(Rel);
610   switch (sec->sh_type) {
611   default:
612     report_fatal_error("Invalid section type in Rel!");
613   case ELF::SHT_REL:
614     return getRel(Rel)->r_offset;
615   case ELF::SHT_RELA:
616     return getRela(Rel)->r_offset;
617   }
618 }
619
620 template <class ELFT>
621 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
622                                                        uint64_t &Result) const {
623   const Elf_Shdr *sec = getRelSection(Rel);
624   switch (sec->sh_type) {
625   default:
626     report_fatal_error("Invalid section type in Rel!");
627   case ELF::SHT_REL: {
628     Result = getRel(Rel)->getType(EF.isMips64EL());
629     break;
630   }
631   case ELF::SHT_RELA: {
632     Result = getRela(Rel)->getType(EF.isMips64EL());
633     break;
634   }
635   }
636   return std::error_code();
637 }
638
639 template <class ELFT>
640 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
641   return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
642 }
643
644 template <class ELFT>
645 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
646     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
647   const Elf_Shdr *sec = getRelSection(Rel);
648   uint32_t type;
649   switch (sec->sh_type) {
650   default:
651     return object_error::parse_failed;
652   case ELF::SHT_REL: {
653     type = getRel(Rel)->getType(EF.isMips64EL());
654     break;
655   }
656   case ELF::SHT_RELA: {
657     type = getRela(Rel)->getType(EF.isMips64EL());
658     break;
659   }
660   }
661
662   EF.getRelocationTypeName(type, Result);
663   return std::error_code();
664 }
665
666 template <class ELFT>
667 ErrorOr<int64_t>
668 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
669   if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
670     return object_error::parse_failed;
671   return (int64_t)getRela(Rel)->r_addend;
672 }
673
674 template <class ELFT>
675 bool ELFObjectFile<ELFT>::hasRelocationAddend(DataRefImpl Rel) const {
676   return getRelSection(Rel)->sh_type == ELF::SHT_RELA;
677 }
678
679 template <class ELFT>
680 const typename ELFFile<ELFT>::Elf_Sym *
681 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
682   return &*toELFSymIter(Symb);
683 }
684
685 template <class ELFT>
686 const typename ELFObjectFile<ELFT>::Elf_Rel *
687 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
688   return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
689 }
690
691 template <class ELFT>
692 const typename ELFObjectFile<ELFT>::Elf_Rela *
693 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
694   return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
695 }
696
697 template <class ELFT>
698 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
699     : ELFObjectFileBase(
700           getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
701                          support::little,
702                      ELFT::Is64Bits),
703           Object),
704       EF(Data.getBuffer(), EC) {}
705
706 template <class ELFT>
707 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
708   return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
709 }
710
711 template <class ELFT>
712 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
713   return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
714 }
715
716 template <class ELFT>
717 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
718   return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
719 }
720
721 template <class ELFT>
722 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
723   return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
724 }
725
726 template <class ELFT>
727 section_iterator ELFObjectFile<ELFT>::section_begin() const {
728   return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
729 }
730
731 template <class ELFT>
732 section_iterator ELFObjectFile<ELFT>::section_end() const {
733   return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
734 }
735
736 template <class ELFT>
737 StringRef ELFObjectFile<ELFT>::getLoadName() const {
738   Elf_Dyn_Iter DI = EF.begin_dynamic_table();
739   Elf_Dyn_Iter DE = EF.end_dynamic_table();
740
741   while (DI != DE && DI->getTag() != ELF::DT_SONAME)
742     ++DI;
743
744   if (DI != DE)
745     return EF.getDynamicString(DI->getVal());
746   return "";
747 }
748
749 template <class ELFT>
750 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
751   return ELFT::Is64Bits ? 8 : 4;
752 }
753
754 template <class ELFT>
755 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
756   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
757   switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
758   case ELF::ELFCLASS32:
759     switch (EF.getHeader()->e_machine) {
760     case ELF::EM_386:
761       return "ELF32-i386";
762     case ELF::EM_X86_64:
763       return "ELF32-x86-64";
764     case ELF::EM_ARM:
765       return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
766     case ELF::EM_HEXAGON:
767       return "ELF32-hexagon";
768     case ELF::EM_MIPS:
769       return "ELF32-mips";
770     case ELF::EM_PPC:
771       return "ELF32-ppc";
772     case ELF::EM_SPARC:
773     case ELF::EM_SPARC32PLUS:
774       return "ELF32-sparc";
775     default:
776       return "ELF32-unknown";
777     }
778   case ELF::ELFCLASS64:
779     switch (EF.getHeader()->e_machine) {
780     case ELF::EM_386:
781       return "ELF64-i386";
782     case ELF::EM_X86_64:
783       return "ELF64-x86-64";
784     case ELF::EM_AARCH64:
785       return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
786     case ELF::EM_PPC64:
787       return "ELF64-ppc64";
788     case ELF::EM_S390:
789       return "ELF64-s390";
790     case ELF::EM_SPARCV9:
791       return "ELF64-sparc";
792     case ELF::EM_MIPS:
793       return "ELF64-mips";
794     default:
795       return "ELF64-unknown";
796     }
797   default:
798     // FIXME: Proper error handling.
799     report_fatal_error("Invalid ELFCLASS!");
800   }
801 }
802
803 template <class ELFT>
804 unsigned ELFObjectFile<ELFT>::getArch() const {
805   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
806   switch (EF.getHeader()->e_machine) {
807   case ELF::EM_386:
808     return Triple::x86;
809   case ELF::EM_X86_64:
810     return Triple::x86_64;
811   case ELF::EM_AARCH64:
812     return Triple::aarch64;
813   case ELF::EM_ARM:
814     return Triple::arm;
815   case ELF::EM_HEXAGON:
816     return Triple::hexagon;
817   case ELF::EM_MIPS:
818     switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
819     case ELF::ELFCLASS32:
820       return IsLittleEndian ? Triple::mipsel : Triple::mips;
821     case ELF::ELFCLASS64:
822       return IsLittleEndian ? Triple::mips64el : Triple::mips64;
823     default:
824       report_fatal_error("Invalid ELFCLASS!");
825     }
826   case ELF::EM_PPC:
827     return Triple::ppc;
828   case ELF::EM_PPC64:
829     return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
830   case ELF::EM_S390:
831     return Triple::systemz;
832
833   case ELF::EM_SPARC:
834   case ELF::EM_SPARC32PLUS:
835     return IsLittleEndian ? Triple::sparcel : Triple::sparc;
836   case ELF::EM_SPARCV9:
837     return Triple::sparcv9;
838
839   default:
840     return Triple::UnknownArch;
841   }
842 }
843
844 template <class ELFT>
845 std::pair<symbol_iterator, symbol_iterator>
846 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const {
847   return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end());
848 }
849
850 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
851   return EF.getHeader()->e_type == ELF::ET_REL;
852 }
853
854 inline std::pair<symbol_iterator, symbol_iterator>
855 getELFDynamicSymbolIterators(const SymbolicFile *Obj) {
856   return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();
857 }
858
859 }
860 }
861
862 #endif