1 //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the ELFObjectFile template class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_ELF_OBJECT_FILE_H
15 #define LLVM_OBJECT_ELF_OBJECT_FILE_H
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"
39 class ELFObjectFile : public ObjectFile {
41 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
43 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
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;
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;
59 void moveSymbolNext(DataRefImpl &Symb) const override;
60 std::error_code getSymbolName(DataRefImpl Symb,
61 StringRef &Res) const override;
62 std::error_code getSymbolAddress(DataRefImpl Symb,
63 uint64_t &Res) const override;
64 std::error_code getSymbolAlignment(DataRefImpl Symb,
65 uint32_t &Res) const override;
66 std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
67 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
68 std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override;
69 std::error_code getSymbolType(DataRefImpl Symb,
70 SymbolRef::Type &Res) const override;
71 std::error_code getSymbolSection(DataRefImpl Symb,
72 section_iterator &Res) const override;
74 std::error_code getLibraryNext(DataRefImpl Data,
75 LibraryRef &Result) const override;
76 std::error_code getLibraryPath(DataRefImpl Data,
77 StringRef &Res) const override;
79 void moveSectionNext(DataRefImpl &Sec) const override;
80 std::error_code getSectionName(DataRefImpl Sec,
81 StringRef &Res) const override;
82 std::error_code getSectionAddress(DataRefImpl Sec,
83 uint64_t &Res) const override;
84 std::error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
85 std::error_code getSectionContents(DataRefImpl Sec,
86 StringRef &Res) const override;
87 std::error_code getSectionAlignment(DataRefImpl Sec,
88 uint64_t &Res) const override;
89 std::error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
90 std::error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
91 std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
92 std::error_code isSectionRequiredForExecution(DataRefImpl Sec,
93 bool &Res) const override;
94 std::error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
95 std::error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
96 std::error_code isSectionReadOnlyData(DataRefImpl Sec,
97 bool &Res) const override;
98 std::error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
99 bool &Result) const override;
100 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
101 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
102 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
104 void moveRelocationNext(DataRefImpl &Rel) const override;
105 std::error_code getRelocationAddress(DataRefImpl Rel,
106 uint64_t &Res) const override;
107 std::error_code getRelocationOffset(DataRefImpl Rel,
108 uint64_t &Res) const override;
109 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
110 std::error_code getRelocationType(DataRefImpl Rel,
111 uint64_t &Res) const override;
113 getRelocationTypeName(DataRefImpl Rel,
114 SmallVectorImpl<char> &Result) const override;
116 getRelocationValueString(DataRefImpl Rel,
117 SmallVectorImpl<char> &Result) const override;
119 uint64_t getROffset(DataRefImpl Rel) const;
120 StringRef getRelocationTypeName(uint32_t Type) const;
122 /// \brief Get the relocation section that contains \a Rel.
123 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
124 return EF.getSection(Rel.d.a);
127 const Elf_Rel *getRel(DataRefImpl Rel) const;
128 const Elf_Rela *getRela(DataRefImpl Rela) const;
130 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
131 bool IsDynamic = Symb.p & 1;
134 EF.begin_dynamic_symbols().getEntSize(),
135 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
136 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
137 reinterpret_cast<const char *>(Symb.p), IsDynamic);
140 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
142 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
143 static_cast<uintptr_t>(Symb.isDynamic());
147 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
148 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
149 reinterpret_cast<const char *>(Sec.p));
152 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
154 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
158 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
160 DRI.p = reinterpret_cast<uintptr_t>(Sec);
164 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
165 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
166 reinterpret_cast<const char *>(Dyn.p));
169 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
171 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
175 // This flag is used for classof, to distinguish ELFObjectFile from
176 // its subclass. If more subclasses will be created, this flag will
177 // have to become an enum.
178 bool isDyldELFObject;
181 ELFObjectFile(std::unique_ptr<MemoryBuffer> Object, std::error_code &EC);
183 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
185 basic_symbol_iterator symbol_begin_impl() const override;
186 basic_symbol_iterator symbol_end_impl() const override;
188 symbol_iterator dynamic_symbol_begin() const;
189 symbol_iterator dynamic_symbol_end() const;
191 section_iterator section_begin() const override;
192 section_iterator section_end() const override;
194 library_iterator needed_library_begin() const override;
195 library_iterator needed_library_end() const override;
197 std::error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
198 std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
199 bool &IsDefault) const;
201 uint8_t getBytesInAddress() const override;
202 StringRef getFileFormatName() const override;
203 unsigned getArch() const override;
204 StringRef getLoadName() const;
206 std::error_code getPlatformFlags(unsigned &Result) const override {
207 Result = EF.getHeader()->e_flags;
208 return object_error::success;
211 const ELFFile<ELFT> *getELFFile() const { return &EF; }
213 bool isDyldType() const { return isDyldELFObject; }
214 static inline bool classof(const Binary *v) {
215 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
220 // Use an alignment of 2 for the typedefs since that is the worst case for
221 // ELF files in archives.
222 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
223 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
224 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
225 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
227 template <class ELFT>
228 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
229 Symb = toDRI(++toELFSymIter(Symb));
232 template <class ELFT>
233 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
234 StringRef &Result) const {
235 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
237 return Name.getError();
239 return object_error::success;
242 template <class ELFT>
243 std::error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
245 bool &IsDefault) const {
246 DataRefImpl Symb = SymRef.getRawDataRefImpl();
247 const Elf_Sym *symb = getSymbol(Symb);
248 ErrorOr<StringRef> Ver =
249 EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
251 return Ver.getError();
253 return object_error::success;
256 template <class ELFT>
257 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
258 uint64_t &Result) const {
259 const Elf_Sym *ESym = getSymbol(Symb);
260 switch (EF.getSymbolTableIndex(ESym)) {
261 case ELF::SHN_COMMON:
263 Result = UnknownAddressOrSize;
264 return object_error::success;
266 Result = ESym->st_value;
267 return object_error::success;
272 const Elf_Ehdr *Header = EF.getHeader();
273 Result = ESym->st_value;
275 // Clear the ARM/Thumb indicator flag.
276 if (Header->e_machine == ELF::EM_ARM && ESym->getType() == ELF::STT_FUNC)
279 if (Header->e_type == ELF::ET_REL)
280 Result += EF.getSection(ESym)->sh_addr;
282 return object_error::success;
285 template <class ELFT>
286 std::error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
287 uint32_t &Res) const {
288 Elf_Sym_Iter Sym = toELFSymIter(Symb);
289 if (Sym->st_shndx == ELF::SHN_COMMON)
293 return object_error::success;
296 template <class ELFT>
297 std::error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
298 uint64_t &Result) const {
299 Result = toELFSymIter(Symb)->st_size;
300 return object_error::success;
303 template <class ELFT>
304 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
305 uint8_t &Result) const {
306 Result = toELFSymIter(Symb)->st_other;
307 return object_error::success;
310 template <class ELFT>
312 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
313 SymbolRef::Type &Result) const {
314 const Elf_Sym *ESym = getSymbol(Symb);
316 switch (ESym->getType()) {
317 case ELF::STT_NOTYPE:
318 Result = SymbolRef::ST_Unknown;
320 case ELF::STT_SECTION:
321 Result = SymbolRef::ST_Debug;
324 Result = SymbolRef::ST_File;
327 Result = SymbolRef::ST_Function;
329 case ELF::STT_OBJECT:
330 case ELF::STT_COMMON:
332 Result = SymbolRef::ST_Data;
335 Result = SymbolRef::ST_Other;
338 return object_error::success;
341 template <class ELFT>
342 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
343 Elf_Sym_Iter EIter = toELFSymIter(Symb);
344 const Elf_Sym *ESym = &*EIter;
346 uint32_t Result = SymbolRef::SF_None;
348 if (ESym->getBinding() != ELF::STB_LOCAL)
349 Result |= SymbolRef::SF_Global;
351 if (ESym->getBinding() == ELF::STB_WEAK)
352 Result |= SymbolRef::SF_Weak;
354 if (ESym->st_shndx == ELF::SHN_ABS)
355 Result |= SymbolRef::SF_Absolute;
357 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
358 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
359 Result |= SymbolRef::SF_FormatSpecific;
361 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
362 Result |= SymbolRef::SF_Undefined;
364 if (ESym->getType() == ELF::STT_COMMON ||
365 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
366 Result |= SymbolRef::SF_Common;
371 template <class ELFT>
373 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
374 section_iterator &Res) const {
375 const Elf_Sym *ESym = getSymbol(Symb);
376 const Elf_Shdr *ESec = EF.getSection(ESym);
381 Sec.p = reinterpret_cast<intptr_t>(ESec);
382 Res = section_iterator(SectionRef(Sec, this));
384 return object_error::success;
387 template <class ELFT>
388 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
389 Sec = toDRI(++toELFShdrIter(Sec));
392 template <class ELFT>
393 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
394 StringRef &Result) const {
395 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
397 return Name.getError();
399 return object_error::success;
402 template <class ELFT>
403 std::error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
404 uint64_t &Result) const {
405 Result = toELFShdrIter(Sec)->sh_addr;
406 return object_error::success;
409 template <class ELFT>
410 std::error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
411 uint64_t &Result) const {
412 Result = toELFShdrIter(Sec)->sh_size;
413 return object_error::success;
416 template <class ELFT>
418 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
419 StringRef &Result) const {
420 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
421 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
422 return object_error::success;
425 template <class ELFT>
427 ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
428 uint64_t &Result) const {
429 Result = toELFShdrIter(Sec)->sh_addralign;
430 return object_error::success;
433 template <class ELFT>
434 std::error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
435 bool &Result) const {
436 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
437 return object_error::success;
440 template <class ELFT>
441 std::error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
442 bool &Result) const {
443 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
444 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
445 EShdr->sh_type == ELF::SHT_PROGBITS;
446 return object_error::success;
449 template <class ELFT>
450 std::error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
451 bool &Result) const {
452 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
453 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
454 EShdr->sh_type == ELF::SHT_NOBITS;
455 return object_error::success;
458 template <class ELFT>
460 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
461 bool &Result) const {
462 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
463 return object_error::success;
466 template <class ELFT>
467 std::error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
468 bool &Result) const {
469 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
470 return object_error::success;
473 template <class ELFT>
474 std::error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
475 bool &Result) const {
476 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
477 return object_error::success;
480 template <class ELFT>
481 std::error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
482 bool &Result) const {
483 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
484 Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
485 return object_error::success;
488 template <class ELFT>
489 std::error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
491 bool &Result) const {
492 Elf_Sym_Iter ESym = toELFSymIter(Symb);
494 uintX_t Index = ESym->st_shndx;
495 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
497 Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
498 return object_error::success;
501 template <class ELFT>
503 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
505 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
506 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
508 return relocation_iterator(RelocationRef(RelData, this));
511 template <class ELFT>
513 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
515 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
516 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
517 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
518 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
521 RelData.d.b = S->sh_size / S->sh_entsize;
523 return relocation_iterator(RelocationRef(RelData, this));
526 template <class ELFT>
528 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
529 if (EF.getHeader()->e_type != ELF::ET_REL)
530 return section_end();
532 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
533 uintX_t Type = EShdr->sh_type;
534 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
535 return section_end();
537 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
538 return section_iterator(SectionRef(toDRI(R), this));
542 template <class ELFT>
543 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
547 template <class ELFT>
549 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
551 const Elf_Shdr *sec = getRelSection(Rel);
552 switch (sec->sh_type) {
554 report_fatal_error("Invalid section type in Rel!");
556 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
559 case ELF::SHT_RELA: {
560 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
567 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
569 DataRefImpl SymbolData;
570 switch (SymSec->sh_type) {
572 report_fatal_error("Invalid symbol table section type!");
573 case ELF::SHT_SYMTAB:
574 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
576 case ELF::SHT_DYNSYM:
577 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
581 return symbol_iterator(SymbolRef(SymbolData, this));
584 template <class ELFT>
586 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
587 uint64_t &Result) const {
588 uint64_t ROffset = getROffset(Rel);
589 const Elf_Ehdr *Header = EF.getHeader();
591 if (Header->e_type == ELF::ET_REL) {
592 const Elf_Shdr *RelocationSec = getRelSection(Rel);
593 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
594 Result = ROffset + RelocatedSec->sh_addr;
599 return object_error::success;
602 template <class ELFT>
604 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
605 uint64_t &Result) const {
606 assert(EF.getHeader()->e_type == ELF::ET_REL &&
607 "Only relocatable object files have relocation offsets");
608 Result = getROffset(Rel);
609 return object_error::success;
612 template <class ELFT>
613 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
614 const Elf_Shdr *sec = getRelSection(Rel);
615 switch (sec->sh_type) {
617 report_fatal_error("Invalid section type in Rel!");
619 return getRel(Rel)->r_offset;
621 return getRela(Rel)->r_offset;
625 template <class ELFT>
626 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
627 uint64_t &Result) const {
628 const Elf_Shdr *sec = getRelSection(Rel);
629 switch (sec->sh_type) {
631 report_fatal_error("Invalid section type in Rel!");
633 Result = getRel(Rel)->getType(EF.isMips64EL());
636 case ELF::SHT_RELA: {
637 Result = getRela(Rel)->getType(EF.isMips64EL());
641 return object_error::success;
644 template <class ELFT>
645 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
646 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
649 template <class ELFT>
650 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
651 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
652 const Elf_Shdr *sec = getRelSection(Rel);
654 switch (sec->sh_type) {
656 return object_error::parse_failed;
658 type = getRel(Rel)->getType(EF.isMips64EL());
661 case ELF::SHT_RELA: {
662 type = getRela(Rel)->getType(EF.isMips64EL());
667 EF.getRelocationTypeName(type, Result);
668 return object_error::success;
671 template <class ELFT>
673 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
674 int64_t &Result) const {
675 const Elf_Shdr *sec = getRelSection(Rel);
676 switch (sec->sh_type) {
678 report_fatal_error("Invalid section type in Rel!");
681 return object_error::success;
683 case ELF::SHT_RELA: {
684 Result = getRela(Rel)->r_addend;
685 return object_error::success;
690 template <class ELFT>
691 std::error_code ELFObjectFile<ELFT>::getRelocationValueString(
692 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
693 const Elf_Shdr *sec = getRelSection(Rel);
697 uint16_t symbol_index = 0;
698 switch (sec->sh_type) {
700 return object_error::parse_failed;
702 type = getRel(Rel)->getType(EF.isMips64EL());
703 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
704 // TODO: Read implicit addend from section data.
707 case ELF::SHT_RELA: {
708 type = getRela(Rel)->getType(EF.isMips64EL());
709 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
710 addend = getRela(Rel)->r_addend;
714 const Elf_Sym *symb =
715 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
716 ErrorOr<StringRef> SymName =
717 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
719 return SymName.getError();
720 switch (EF.getHeader()->e_machine) {
723 case ELF::R_X86_64_PC8:
724 case ELF::R_X86_64_PC16:
725 case ELF::R_X86_64_PC32: {
727 raw_string_ostream fmt(fmtbuf);
728 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
730 Result.append(fmtbuf.begin(), fmtbuf.end());
732 case ELF::R_X86_64_8:
733 case ELF::R_X86_64_16:
734 case ELF::R_X86_64_32:
735 case ELF::R_X86_64_32S:
736 case ELF::R_X86_64_64: {
738 raw_string_ostream fmt(fmtbuf);
739 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
741 Result.append(fmtbuf.begin(), fmtbuf.end());
747 case ELF::EM_AARCH64: {
749 raw_string_ostream fmt(fmtbuf);
752 fmt << (addend < 0 ? "" : "+") << addend;
754 Result.append(fmtbuf.begin(), fmtbuf.end());
758 case ELF::EM_HEXAGON:
766 Result.append(res.begin(), res.end());
767 return object_error::success;
770 template <class ELFT>
771 const typename ELFFile<ELFT>::Elf_Sym *
772 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
773 return &*toELFSymIter(Symb);
776 template <class ELFT>
777 const typename ELFObjectFile<ELFT>::Elf_Rel *
778 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
779 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
782 template <class ELFT>
783 const typename ELFObjectFile<ELFT>::Elf_Rela *
784 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
785 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
788 template <class ELFT>
789 ELFObjectFile<ELFT>::ELFObjectFile(std::unique_ptr<MemoryBuffer> Object,
791 : ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
795 EF(Data->getBuffer(), EC) {}
797 template <class ELFT>
798 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
799 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
802 template <class ELFT>
803 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
804 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
807 template <class ELFT>
808 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
809 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
812 template <class ELFT>
813 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
814 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
817 template <class ELFT>
818 section_iterator ELFObjectFile<ELFT>::section_begin() const {
819 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
822 template <class ELFT>
823 section_iterator ELFObjectFile<ELFT>::section_end() const {
824 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
827 template <class ELFT>
828 StringRef ELFObjectFile<ELFT>::getLoadName() const {
829 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
830 Elf_Dyn_Iter DE = EF.end_dynamic_table();
832 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
836 return EF.getDynamicString(DI->getVal());
840 template <class ELFT>
841 library_iterator ELFObjectFile<ELFT>::needed_library_begin() const {
842 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
843 Elf_Dyn_Iter DE = EF.end_dynamic_table();
845 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
848 return library_iterator(LibraryRef(toDRI(DI), this));
851 template <class ELFT>
852 std::error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
853 LibraryRef &Result) const {
854 Elf_Dyn_Iter DI = toELFDynIter(Data);
855 Elf_Dyn_Iter DE = EF.end_dynamic_table();
857 // Skip to the next DT_NEEDED entry.
860 while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
862 Result = LibraryRef(toDRI(DI), this);
863 return object_error::success;
866 template <class ELFT>
867 std::error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
868 StringRef &Res) const {
869 Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
870 return object_error::success;
873 template <class ELFT>
874 library_iterator ELFObjectFile<ELFT>::needed_library_end() const {
875 return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
878 template <class ELFT>
879 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
880 return ELFT::Is64Bits ? 8 : 4;
883 template <class ELFT>
884 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
885 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
886 case ELF::ELFCLASS32:
887 switch (EF.getHeader()->e_machine) {
891 return "ELF32-x86-64";
894 case ELF::EM_HEXAGON:
895 return "ELF32-hexagon";
901 case ELF::EM_SPARC32PLUS:
902 return "ELF32-sparc";
904 return "ELF32-unknown";
906 case ELF::ELFCLASS64:
907 switch (EF.getHeader()->e_machine) {
911 return "ELF64-x86-64";
912 case ELF::EM_AARCH64:
913 return "ELF64-aarch64";
915 return "ELF64-ppc64";
918 case ELF::EM_SPARCV9:
919 return "ELF64-sparc";
923 return "ELF64-unknown";
926 // FIXME: Proper error handling.
927 report_fatal_error("Invalid ELFCLASS!");
931 template <class ELFT>
932 unsigned ELFObjectFile<ELFT>::getArch() const {
933 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
934 switch (EF.getHeader()->e_machine) {
938 return Triple::x86_64;
939 case ELF::EM_AARCH64:
940 return Triple::aarch64;
943 case ELF::EM_HEXAGON:
944 return Triple::hexagon;
946 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
947 case ELF::ELFCLASS32:
948 return IsLittleEndian ? Triple::mipsel : Triple::mips;
949 case ELF::ELFCLASS64:
950 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
952 report_fatal_error("Invalid ELFCLASS!");
955 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
957 return Triple::systemz;
960 case ELF::EM_SPARC32PLUS:
961 return Triple::sparc;
962 case ELF::EM_SPARCV9:
963 return Triple::sparcv9;
966 return Triple::UnknownArch;
970 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
971 /// and make these member functions?
972 inline std::error_code getELFRelocationAddend(const RelocationRef R,
974 const ObjectFile *Obj = R.getObjectFile();
975 DataRefImpl DRI = R.getRawDataRefImpl();
976 // Little-endian 32-bit
977 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
978 return ELFObj->getRelocationAddend(DRI, Addend);
981 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
982 return ELFObj->getRelocationAddend(DRI, Addend);
984 // Little-endian 64-bit
985 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
986 return ELFObj->getRelocationAddend(DRI, Addend);
989 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
990 return ELFObj->getRelocationAddend(DRI, Addend);
992 llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
995 inline std::pair<symbol_iterator, symbol_iterator>
996 getELFDynamicSymbolIterators(SymbolicFile *Obj) {
997 if (const ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
998 return std::make_pair(ELF->dynamic_symbol_begin(),
999 ELF->dynamic_symbol_end());
1000 if (const ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
1001 return std::make_pair(ELF->dynamic_symbol_begin(),
1002 ELF->dynamic_symbol_end());
1003 if (const ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
1004 return std::make_pair(ELF->dynamic_symbol_begin(),
1005 ELF->dynamic_symbol_end());
1006 if (const ELF64BEObjectFile *ELF = cast<ELF64BEObjectFile>(Obj))
1007 return std::make_pair(ELF->dynamic_symbol_begin(),
1008 ELF->dynamic_symbol_end());
1011 "Object passed to getELFDynamicSymbolIterators() is not ELF");
1014 /// This is a generic interface for retrieving GNU symbol version
1015 /// information from an ELFObjectFile.
1016 inline std::error_code GetELFSymbolVersion(const ObjectFile *Obj,
1017 const SymbolRef &Sym,
1020 // Little-endian 32-bit
1021 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1022 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1024 // Big-endian 32-bit
1025 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1026 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1028 // Little-endian 64-bit
1029 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1030 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1032 // Big-endian 64-bit
1033 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1034 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1036 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");