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