Remove dead code.
[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_ELF_OBJECT_FILE_H
15 #define LLVM_OBJECT_ELF_OBJECT_FILE_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 template <class ELFT>
39 class ELFObjectFile : public ObjectFile {
40 public:
41   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
42
43   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
44
45   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
46   typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
47   typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
48   typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
49   typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
50
51   typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
52   typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
53   typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
54
55 protected:
56   ELFFile<ELFT> EF;
57
58   virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
59   virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
60   virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
61   virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
62   virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
63   virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
64   virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
65   virtual error_code getSymbolType(DataRefImpl Symb,
66                                    SymbolRef::Type &Res) const;
67   virtual error_code getSymbolSection(DataRefImpl Symb,
68                                       section_iterator &Res) const;
69   virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
70
71   virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
72   virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
73
74   virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
75   virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
76   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
77   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
78   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
79   virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
80   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
81   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
82   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
83   virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
84                                                    bool &Res) const;
85   virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
86   virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
87   virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
88   virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
89                                            bool &Result) const;
90   virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const;
91   virtual relocation_iterator section_rel_end(DataRefImpl Sec) const;
92   virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
93
94   virtual error_code getRelocationNext(DataRefImpl Rel,
95                                        RelocationRef &Res) const;
96   virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
97   virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
98   virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const;
99   virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
100   virtual error_code getRelocationTypeName(DataRefImpl Rel,
101                                            SmallVectorImpl<char> &Result) const;
102   virtual error_code
103   getRelocationValueString(DataRefImpl Rel,
104                            SmallVectorImpl<char> &Result) const;
105
106   uint64_t getROffset(DataRefImpl Rel) const;
107   StringRef getRelocationTypeName(uint32_t Type) const;
108
109   /// \brief Get the relocation section that contains \a Rel.
110   const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
111     return EF.getSection(Rel.d.a);
112   }
113
114   const Elf_Rel *getRel(DataRefImpl Rel) const;
115   const Elf_Rela *getRela(DataRefImpl Rela) const;
116
117   Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
118     bool IsDynamic = Symb.p & 1;
119     if (IsDynamic)
120       return Elf_Sym_Iter(
121           EF.begin_dynamic_symbols().getEntSize(),
122           reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
123     return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
124                         reinterpret_cast<const char *>(Symb.p), IsDynamic);
125   }
126
127   DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
128     DataRefImpl DRI;
129     DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
130       static_cast<uintptr_t>(Symb.isDynamic());
131     return DRI;
132   }
133
134   Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
135     return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
136                          reinterpret_cast<const char *>(Sec.p));
137   }
138
139   DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
140     DataRefImpl DRI;
141     DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
142     return DRI;
143   }
144
145   DataRefImpl toDRI(const Elf_Shdr *Sec) const {
146     DataRefImpl DRI;
147     DRI.p = reinterpret_cast<uintptr_t>(Sec);
148     return DRI;
149   }
150
151   Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
152     return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
153                         reinterpret_cast<const char *>(Dyn.p));
154   }
155
156   DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
157     DataRefImpl DRI;
158     DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
159     return DRI;
160   }
161
162   // This flag is used for classof, to distinguish ELFObjectFile from
163   // its subclass. If more subclasses will be created, this flag will
164   // have to become an enum.
165   bool isDyldELFObject;
166
167 public:
168   ELFObjectFile(MemoryBuffer *Object, error_code &EC, bool BufferOwned = true);
169
170   const Elf_Sym *getSymbol(DataRefImpl Symb) const;
171
172   virtual symbol_iterator begin_symbols() const;
173   virtual symbol_iterator end_symbols() const;
174
175   virtual symbol_iterator begin_dynamic_symbols() const;
176   virtual symbol_iterator end_dynamic_symbols() const;
177
178   virtual section_iterator begin_sections() const;
179   virtual section_iterator end_sections() const;
180
181   virtual library_iterator begin_libraries_needed() const;
182   virtual library_iterator end_libraries_needed() const;
183
184   error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
185   error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
186                               bool &IsDefault) const;
187
188   virtual uint8_t getBytesInAddress() const;
189   virtual StringRef getFileFormatName() const;
190   virtual unsigned getArch() const;
191   virtual StringRef getLoadName() const;
192
193   const ELFFile<ELFT> *getELFFile() const { return &EF; }
194
195   bool isDyldType() const { return isDyldELFObject; }
196   static inline bool classof(const Binary *v) {
197     return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
198                                       ELFT::Is64Bits);
199   }
200 };
201
202 // Use an alignment of 2 for the typedefs since that is the worst case for
203 // ELF files in archives.
204 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
205 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
206 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
207 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
208
209 template <class ELFT>
210 error_code ELFObjectFile<ELFT>::getSymbolNext(DataRefImpl Symb,
211                                               SymbolRef &Result) const {
212   Result = SymbolRef(toDRI(++toELFSymIter(Symb)), this);
213   return object_error::success;
214 }
215
216 template <class ELFT>
217 error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
218                                               StringRef &Result) const {
219   ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
220   if (!Name)
221     return Name.getError();
222   Result = *Name;
223   return object_error::success;
224 }
225
226 template <class ELFT>
227 error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
228                                                  StringRef &Version,
229                                                  bool &IsDefault) const {
230   DataRefImpl Symb = SymRef.getRawDataRefImpl();
231   const Elf_Sym *symb = getSymbol(Symb);
232   ErrorOr<StringRef> Ver =
233       EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
234   if (!Ver)
235     return Ver.getError();
236   Version = *Ver;
237   return object_error::success;
238 }
239
240 template <class ELFT>
241 error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb,
242                                                     uint64_t &Result) const {
243   const Elf_Sym *ESym = getSymbol(Symb);
244   const Elf_Shdr *ESec;
245   switch (EF.getSymbolTableIndex(ESym)) {
246   case ELF::SHN_COMMON:
247   // Unintialized symbols have no offset in the object file
248   case ELF::SHN_UNDEF:
249     Result = UnknownAddressOrSize;
250     return object_error::success;
251   case ELF::SHN_ABS:
252     Result = ESym->st_value;
253     return object_error::success;
254   default:
255     ESec = EF.getSection(ESym);
256   }
257
258   switch (ESym->getType()) {
259   case ELF::STT_SECTION:
260     Result = ESec ? ESec->sh_offset : UnknownAddressOrSize;
261     return object_error::success;
262   case ELF::STT_FUNC:
263   case ELF::STT_OBJECT:
264   case ELF::STT_NOTYPE:
265     Result = ESym->st_value + (ESec ? ESec->sh_offset : 0);
266     return object_error::success;
267   default:
268     Result = UnknownAddressOrSize;
269     return object_error::success;
270   }
271 }
272
273 template <class ELFT>
274 error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
275                                                  uint64_t &Result) const {
276   const Elf_Sym *ESym = getSymbol(Symb);
277   const Elf_Shdr *ESec;
278   switch (EF.getSymbolTableIndex(ESym)) {
279   case ELF::SHN_COMMON:
280   case ELF::SHN_UNDEF:
281     Result = UnknownAddressOrSize;
282     return object_error::success;
283   case ELF::SHN_ABS:
284     Result = ESym->st_value;
285     return object_error::success;
286   default:
287     ESec = EF.getSection(ESym);
288   }
289
290   switch (ESym->getType()) {
291   case ELF::STT_SECTION:
292     Result = ESec ? ESec->sh_addr : UnknownAddressOrSize;
293     return object_error::success;
294   case ELF::STT_FUNC:
295   case ELF::STT_OBJECT:
296   case ELF::STT_NOTYPE:
297   case ELF::STT_TLS:
298     bool IsRelocatable;
299     switch (EF.getHeader()->e_type) {
300     case ELF::ET_EXEC:
301     case ELF::ET_DYN:
302       IsRelocatable = false;
303       break;
304     default:
305       IsRelocatable = true;
306     }
307     Result = ESym->st_value;
308
309     // Clear the ARM/Thumb indicator flag.
310     if (EF.getHeader()->e_machine == ELF::EM_ARM)
311       Result &= ~1;
312
313     if (IsRelocatable && ESec != 0)
314       Result += ESec->sh_addr;
315     return object_error::success;
316   default:
317     Result = UnknownAddressOrSize;
318     return object_error::success;
319   }
320 }
321
322 template <class ELFT>
323 error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
324                                                    uint32_t &Res) const {
325   Elf_Sym_Iter Sym = toELFSymIter(Symb);
326   if (Sym->st_shndx == ELF::SHN_COMMON)
327     Res = Sym->st_value;
328   else
329     Res = 0;
330   return object_error::success;
331 }
332
333 template <class ELFT>
334 error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
335                                               uint64_t &Result) const {
336   Result = toELFSymIter(Symb)->st_size;
337   return object_error::success;
338 }
339
340 template <class ELFT>
341 error_code ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
342                                               SymbolRef::Type &Result) const {
343   const Elf_Sym *ESym = getSymbol(Symb);
344
345   switch (ESym->getType()) {
346   case ELF::STT_NOTYPE:
347     Result = SymbolRef::ST_Unknown;
348     break;
349   case ELF::STT_SECTION:
350     Result = SymbolRef::ST_Debug;
351     break;
352   case ELF::STT_FILE:
353     Result = SymbolRef::ST_File;
354     break;
355   case ELF::STT_FUNC:
356     Result = SymbolRef::ST_Function;
357     break;
358   case ELF::STT_OBJECT:
359   case ELF::STT_COMMON:
360   case ELF::STT_TLS:
361     Result = SymbolRef::ST_Data;
362     break;
363   default:
364     Result = SymbolRef::ST_Other;
365     break;
366   }
367   return object_error::success;
368 }
369
370 template <class ELFT>
371 error_code ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb,
372                                                uint32_t &Result) const {
373   const Elf_Sym *ESym = getSymbol(Symb);
374
375   Result = SymbolRef::SF_None;
376
377   if (ESym->getBinding() != ELF::STB_LOCAL)
378     Result |= SymbolRef::SF_Global;
379
380   if (ESym->getBinding() == ELF::STB_WEAK)
381     Result |= SymbolRef::SF_Weak;
382
383   if (ESym->st_shndx == ELF::SHN_ABS)
384     Result |= SymbolRef::SF_Absolute;
385
386   if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
387       ESym == &*EF.begin_symbols())
388     Result |= SymbolRef::SF_FormatSpecific;
389
390   if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
391     Result |= SymbolRef::SF_Undefined;
392
393   if (ESym->getType() == ELF::STT_COMMON ||
394       EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
395     Result |= SymbolRef::SF_Common;
396
397   if (ESym->getType() == ELF::STT_TLS)
398     Result |= SymbolRef::SF_ThreadLocal;
399
400   return object_error::success;
401 }
402
403 template <class ELFT>
404 error_code ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
405                                                  section_iterator &Res) const {
406   const Elf_Sym *ESym = getSymbol(Symb);
407   const Elf_Shdr *ESec = EF.getSection(ESym);
408   if (!ESec)
409     Res = end_sections();
410   else {
411     DataRefImpl Sec;
412     Sec.p = reinterpret_cast<intptr_t>(ESec);
413     Res = section_iterator(SectionRef(Sec, this));
414   }
415   return object_error::success;
416 }
417
418 template <class ELFT>
419 error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb,
420                                                uint64_t &Val) const {
421   const Elf_Sym *ESym = getSymbol(Symb);
422   Val = ESym->st_value;
423   return object_error::success;
424 }
425
426 template <class ELFT>
427 error_code ELFObjectFile<ELFT>::getSectionNext(DataRefImpl Sec,
428                                                SectionRef &Result) const {
429   Result = SectionRef(toDRI(++toELFShdrIter(Sec)), this);
430   return object_error::success;
431 }
432
433 template <class ELFT>
434 error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
435                                                StringRef &Result) const {
436   ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
437   if (!Name)
438     return Name.getError();
439   Result = *Name;
440   return object_error::success;
441 }
442
443 template <class ELFT>
444 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
445                                                   uint64_t &Result) const {
446   Result = toELFShdrIter(Sec)->sh_addr;
447   return object_error::success;
448 }
449
450 template <class ELFT>
451 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
452                                                uint64_t &Result) const {
453   Result = toELFShdrIter(Sec)->sh_size;
454   return object_error::success;
455 }
456
457 template <class ELFT>
458 error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
459                                                    StringRef &Result) const {
460   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
461   Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
462   return object_error::success;
463 }
464
465 template <class ELFT>
466 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
467                                                     uint64_t &Result) const {
468   Result = toELFShdrIter(Sec)->sh_addralign;
469   return object_error::success;
470 }
471
472 template <class ELFT>
473 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
474                                               bool &Result) const {
475   Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
476   return object_error::success;
477 }
478
479 template <class ELFT>
480 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
481                                               bool &Result) const {
482   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
483   Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
484            EShdr->sh_type == ELF::SHT_PROGBITS;
485   return object_error::success;
486 }
487
488 template <class ELFT>
489 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
490                                              bool &Result) const {
491   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
492   Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
493            EShdr->sh_type == ELF::SHT_NOBITS;
494   return object_error::success;
495 }
496
497 template <class ELFT>
498 error_code
499 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
500                                                    bool &Result) const {
501   Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
502   return object_error::success;
503 }
504
505 template <class ELFT>
506 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
507                                                  bool &Result) const {
508   Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
509   return object_error::success;
510 }
511
512 template <class ELFT>
513 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
514                                                   bool &Result) const {
515   Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
516   return object_error::success;
517 }
518
519 template <class ELFT>
520 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
521                                                       bool &Result) const {
522   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
523   Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
524   return object_error::success;
525 }
526
527 template <class ELFT>
528 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
529                                                       DataRefImpl Symb,
530                                                       bool &Result) const {
531   Elf_Sym_Iter ESym = toELFSymIter(Symb);
532
533   uintX_t Index = ESym->st_shndx;
534   bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
535
536   Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
537   return object_error::success;
538 }
539
540 template <class ELFT>
541 relocation_iterator
542 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
543   DataRefImpl RelData;
544   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
545   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
546   RelData.d.b = 0;
547   return relocation_iterator(RelocationRef(RelData, this));
548 }
549
550 template <class ELFT>
551 relocation_iterator
552 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
553   DataRefImpl RelData;
554   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
555   const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
556   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
557   if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
558     RelData.d.b = 0;
559   else
560     RelData.d.b = S->sh_size / S->sh_entsize;
561
562   return relocation_iterator(RelocationRef(RelData, this));
563 }
564
565 template <class ELFT>
566 section_iterator
567 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
568   if (EF.getHeader()->e_type != ELF::ET_REL)
569     return end_sections();
570
571   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
572   uintX_t Type = EShdr->sh_type;
573   if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
574     return end_sections();
575
576   const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
577   return section_iterator(SectionRef(toDRI(R), this));
578 }
579
580 // Relocations
581 template <class ELFT>
582 error_code ELFObjectFile<ELFT>::getRelocationNext(DataRefImpl Rel,
583                                                   RelocationRef &Result) const {
584   ++Rel.d.b;
585   Result = RelocationRef(Rel, this);
586   return object_error::success;
587 }
588
589 template <class ELFT>
590 symbol_iterator
591 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
592   uint32_t symbolIdx;
593   const Elf_Shdr *sec = getRelSection(Rel);
594   switch (sec->sh_type) {
595   default:
596     report_fatal_error("Invalid section type in Rel!");
597   case ELF::SHT_REL: {
598     symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
599     break;
600   }
601   case ELF::SHT_RELA: {
602     symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
603     break;
604   }
605   }
606   if (!symbolIdx)
607     return end_symbols();
608
609   const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
610
611   DataRefImpl SymbolData;
612   switch (SymSec->sh_type) {
613   default:
614     report_fatal_error("Invalid symbol table section type!");
615   case ELF::SHT_SYMTAB:
616     SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
617     break;
618   case ELF::SHT_DYNSYM:
619     SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
620     break;
621   }
622
623   return symbol_iterator(SymbolRef(SymbolData, this));
624 }
625
626 template <class ELFT>
627 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
628                                                      uint64_t &Result) const {
629   Result = getROffset(Rel);
630   return object_error::success;
631 }
632
633 template <class ELFT>
634 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
635                                                     uint64_t &Result) const {
636   Result = getROffset(Rel);
637   return object_error::success;
638 }
639
640 template <class ELFT>
641 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
642   const Elf_Shdr *sec = getRelSection(Rel);
643   switch (sec->sh_type) {
644   default:
645     report_fatal_error("Invalid section type in Rel!");
646   case ELF::SHT_REL:
647     return getRel(Rel)->r_offset;
648   case ELF::SHT_RELA:
649     return getRela(Rel)->r_offset;
650   }
651 }
652
653 template <class ELFT>
654 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
655                                                   uint64_t &Result) const {
656   const Elf_Shdr *sec = getRelSection(Rel);
657   switch (sec->sh_type) {
658   default:
659     report_fatal_error("Invalid section type in Rel!");
660   case ELF::SHT_REL: {
661     Result = getRel(Rel)->getType(EF.isMips64EL());
662     break;
663   }
664   case ELF::SHT_RELA: {
665     Result = getRela(Rel)->getType(EF.isMips64EL());
666     break;
667   }
668   }
669   return object_error::success;
670 }
671
672 template <class ELFT>
673 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
674   return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
675 }
676
677 template <class ELFT>
678 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
679     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
680   const Elf_Shdr *sec = getRelSection(Rel);
681   uint32_t type;
682   switch (sec->sh_type) {
683   default:
684     return object_error::parse_failed;
685   case ELF::SHT_REL: {
686     type = getRel(Rel)->getType(EF.isMips64EL());
687     break;
688   }
689   case ELF::SHT_RELA: {
690     type = getRela(Rel)->getType(EF.isMips64EL());
691     break;
692   }
693   }
694
695   EF.getRelocationTypeName(type, Result);
696   return object_error::success;
697 }
698
699 template <class ELFT>
700 error_code ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
701                                                     int64_t &Result) const {
702   const Elf_Shdr *sec = getRelSection(Rel);
703   switch (sec->sh_type) {
704   default:
705     report_fatal_error("Invalid section type in Rel!");
706   case ELF::SHT_REL: {
707     Result = 0;
708     return object_error::success;
709   }
710   case ELF::SHT_RELA: {
711     Result = getRela(Rel)->r_addend;
712     return object_error::success;
713   }
714   }
715 }
716
717 template <class ELFT>
718 error_code ELFObjectFile<ELFT>::getRelocationValueString(
719     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
720   const Elf_Shdr *sec = getRelSection(Rel);
721   uint8_t type;
722   StringRef res;
723   int64_t addend = 0;
724   uint16_t symbol_index = 0;
725   switch (sec->sh_type) {
726   default:
727     return object_error::parse_failed;
728   case ELF::SHT_REL: {
729     type = getRel(Rel)->getType(EF.isMips64EL());
730     symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
731     // TODO: Read implicit addend from section data.
732     break;
733   }
734   case ELF::SHT_RELA: {
735     type = getRela(Rel)->getType(EF.isMips64EL());
736     symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
737     addend = getRela(Rel)->r_addend;
738     break;
739   }
740   }
741   const Elf_Sym *symb =
742       EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
743   ErrorOr<StringRef> SymName =
744       EF.getSymbolName(EF.getSection(sec->sh_link), symb);
745   if (!SymName)
746     return SymName.getError();
747   switch (EF.getHeader()->e_machine) {
748   case ELF::EM_X86_64:
749     switch (type) {
750     case ELF::R_X86_64_PC8:
751     case ELF::R_X86_64_PC16:
752     case ELF::R_X86_64_PC32: {
753       std::string fmtbuf;
754       raw_string_ostream fmt(fmtbuf);
755       fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
756       fmt.flush();
757       Result.append(fmtbuf.begin(), fmtbuf.end());
758     } break;
759     case ELF::R_X86_64_8:
760     case ELF::R_X86_64_16:
761     case ELF::R_X86_64_32:
762     case ELF::R_X86_64_32S:
763     case ELF::R_X86_64_64: {
764       std::string fmtbuf;
765       raw_string_ostream fmt(fmtbuf);
766       fmt << *SymName << (addend < 0 ? "" : "+") << addend;
767       fmt.flush();
768       Result.append(fmtbuf.begin(), fmtbuf.end());
769     } break;
770     default:
771       res = "Unknown";
772     }
773     break;
774   case ELF::EM_AARCH64: {
775     std::string fmtbuf;
776     raw_string_ostream fmt(fmtbuf);
777     fmt << *SymName;
778     if (addend != 0)
779       fmt << (addend < 0 ? "" : "+") << addend;
780     fmt.flush();
781     Result.append(fmtbuf.begin(), fmtbuf.end());
782     break;
783   }
784   case ELF::EM_ARM:
785   case ELF::EM_HEXAGON:
786   case ELF::EM_MIPS:
787     res = *SymName;
788     break;
789   default:
790     res = "Unknown";
791   }
792   if (Result.empty())
793     Result.append(res.begin(), res.end());
794   return object_error::success;
795 }
796
797 template <class ELFT>
798 const typename ELFFile<ELFT>::Elf_Sym *
799 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
800   return &*toELFSymIter(Symb);
801 }
802
803 template <class ELFT>
804 const typename ELFObjectFile<ELFT>::Elf_Rel *
805 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
806   return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
807 }
808
809 template <class ELFT>
810 const typename ELFObjectFile<ELFT>::Elf_Rela *
811 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
812   return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
813 }
814
815 template <class ELFT>
816 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec,
817                                    bool BufferOwned)
818     : ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
819                                 support::little,
820                             ELFT::Is64Bits),
821                  Object, BufferOwned),
822       EF(Object, ec) {}
823
824 template <class ELFT>
825 symbol_iterator ELFObjectFile<ELFT>::begin_symbols() const {
826   return symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
827 }
828
829 template <class ELFT>
830 symbol_iterator ELFObjectFile<ELFT>::end_symbols() const {
831   return symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
832 }
833
834 template <class ELFT>
835 symbol_iterator ELFObjectFile<ELFT>::begin_dynamic_symbols() const {
836   return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
837 }
838
839 template <class ELFT>
840 symbol_iterator ELFObjectFile<ELFT>::end_dynamic_symbols() const {
841   return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
842 }
843
844 template <class ELFT>
845 section_iterator ELFObjectFile<ELFT>::begin_sections() const {
846   return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
847 }
848
849 template <class ELFT>
850 section_iterator ELFObjectFile<ELFT>::end_sections() const {
851   return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
852 }
853
854 template <class ELFT>
855 StringRef ELFObjectFile<ELFT>::getLoadName() const {
856   Elf_Dyn_Iter DI = EF.begin_dynamic_table();
857   Elf_Dyn_Iter DE = EF.end_dynamic_table();
858
859   while (DI != DE && DI->getTag() != ELF::DT_SONAME)
860     ++DI;
861
862   if (DI != DE)
863     return EF.getDynamicString(DI->getVal());
864   return "";
865 }
866
867 template <class ELFT>
868 library_iterator ELFObjectFile<ELFT>::begin_libraries_needed() const {
869   Elf_Dyn_Iter DI = EF.begin_dynamic_table();
870   Elf_Dyn_Iter DE = EF.end_dynamic_table();
871
872   while (DI != DE && DI->getTag() != ELF::DT_SONAME)
873     ++DI;
874
875   return library_iterator(LibraryRef(toDRI(DI), this));
876 }
877
878 template <class ELFT>
879 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
880                                                LibraryRef &Result) const {
881   Elf_Dyn_Iter DI = toELFDynIter(Data);
882   Elf_Dyn_Iter DE = EF.end_dynamic_table();
883
884   // Skip to the next DT_NEEDED entry.
885   do
886     ++DI;
887   while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
888
889   Result = LibraryRef(toDRI(DI), this);
890   return object_error::success;
891 }
892
893 template <class ELFT>
894 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
895                                                StringRef &Res) const {
896   Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
897   return object_error::success;
898 }
899
900 template <class ELFT>
901 library_iterator ELFObjectFile<ELFT>::end_libraries_needed() const {
902   return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
903 }
904
905 template <class ELFT>
906 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
907   return ELFT::Is64Bits ? 8 : 4;
908 }
909
910 template <class ELFT>
911 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
912   switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
913   case ELF::ELFCLASS32:
914     switch (EF.getHeader()->e_machine) {
915     case ELF::EM_386:
916       return "ELF32-i386";
917     case ELF::EM_X86_64:
918       return "ELF32-x86-64";
919     case ELF::EM_ARM:
920       return "ELF32-arm";
921     case ELF::EM_HEXAGON:
922       return "ELF32-hexagon";
923     case ELF::EM_MIPS:
924       return "ELF32-mips";
925     case ELF::EM_PPC:
926       return "ELF32-ppc";
927     case ELF::EM_SPARC:
928     case ELF::EM_SPARC32PLUS:
929       return "ELF32-sparc";
930     default:
931       return "ELF32-unknown";
932     }
933   case ELF::ELFCLASS64:
934     switch (EF.getHeader()->e_machine) {
935     case ELF::EM_386:
936       return "ELF64-i386";
937     case ELF::EM_X86_64:
938       return "ELF64-x86-64";
939     case ELF::EM_AARCH64:
940       return "ELF64-aarch64";
941     case ELF::EM_PPC64:
942       return "ELF64-ppc64";
943     case ELF::EM_S390:
944       return "ELF64-s390";
945     case ELF::EM_SPARCV9:
946       return "ELF64-sparc";
947     default:
948       return "ELF64-unknown";
949     }
950   default:
951     // FIXME: Proper error handling.
952     report_fatal_error("Invalid ELFCLASS!");
953   }
954 }
955
956 template <class ELFT>
957 unsigned ELFObjectFile<ELFT>::getArch() const {
958   switch (EF.getHeader()->e_machine) {
959   case ELF::EM_386:
960     return Triple::x86;
961   case ELF::EM_X86_64:
962     return Triple::x86_64;
963   case ELF::EM_AARCH64:
964     return Triple::aarch64;
965   case ELF::EM_ARM:
966     return Triple::arm;
967   case ELF::EM_HEXAGON:
968     return Triple::hexagon;
969   case ELF::EM_MIPS:
970     return (ELFT::TargetEndianness == support::little) ? Triple::mipsel
971                                                        : Triple::mips;
972   case ELF::EM_PPC64:
973     return (ELFT::TargetEndianness == support::little) ? Triple::ppc64le
974                                                        : Triple::ppc64;
975   case ELF::EM_S390:
976     return Triple::systemz;
977
978   case ELF::EM_SPARC:
979   case ELF::EM_SPARC32PLUS:
980     return Triple::sparc;
981   case ELF::EM_SPARCV9:
982     return Triple::sparcv9;
983
984   default:
985     return Triple::UnknownArch;
986   }
987 }
988
989 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
990 /// and make these member functions?
991 static inline error_code getELFRelocationAddend(const RelocationRef R,
992                                                 int64_t &Addend) {
993   const ObjectFile *Obj = R.getObjectFile();
994   DataRefImpl DRI = R.getRawDataRefImpl();
995   // Little-endian 32-bit
996   if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
997     return ELFObj->getRelocationAddend(DRI, Addend);
998
999   // Big-endian 32-bit
1000   if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1001     return ELFObj->getRelocationAddend(DRI, Addend);
1002
1003   // Little-endian 64-bit
1004   if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1005     return ELFObj->getRelocationAddend(DRI, Addend);
1006
1007   // Big-endian 64-bit
1008   if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1009     return ELFObj->getRelocationAddend(DRI, Addend);
1010
1011   llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
1012 }
1013
1014 /// This is a generic interface for retrieving GNU symbol version
1015 /// information from an ELFObjectFile.
1016 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
1017                                              const SymbolRef &Sym,
1018                                              StringRef &Version,
1019                                              bool &IsDefault) {
1020   // Little-endian 32-bit
1021   if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1022     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1023
1024   // Big-endian 32-bit
1025   if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1026     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1027
1028   // Little-endian 64-bit
1029   if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1030     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1031
1032   // Big-endian 64-bit
1033   if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1034     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1035
1036   llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");
1037 }
1038 }
1039 }
1040
1041 #endif