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