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_ELFOBJECTFILE_H
15 #define LLVM_OBJECT_ELFOBJECTFILE_H
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/BinaryFormat/ELF.h"
24 #include "llvm/MC/SubtargetFeature.h"
25 #include "llvm/Object/Binary.h"
26 #include "llvm/Object/ELF.h"
27 #include "llvm/Object/ELFTypes.h"
28 #include "llvm/Object/Error.h"
29 #include "llvm/Object/ObjectFile.h"
30 #include "llvm/Object/SymbolicFile.h"
31 #include "llvm/Support/ARMAttributeParser.h"
32 #include "llvm/Support/ARMBuildAttributes.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/Error.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/MemoryBuffer.h"
40 #include <system_error>
45 class elf_symbol_iterator;
47 class ELFObjectFileBase : public ObjectFile {
48 friend class ELFRelocationRef;
49 friend class ELFSectionRef;
50 friend class ELFSymbolRef;
53 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
55 virtual uint16_t getEMachine() const = 0;
56 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
57 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
58 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
60 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
61 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
62 virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
64 virtual Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
67 using elf_symbol_iterator_range = iterator_range<elf_symbol_iterator>;
69 virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
71 /// Returns platform-specific object flags, if any.
72 virtual unsigned getPlatformFlags() const = 0;
74 elf_symbol_iterator_range symbols() const;
76 static bool classof(const Binary *v) { return v->isELF(); }
78 SubtargetFeatures getFeatures() const override;
80 SubtargetFeatures getMIPSFeatures() const;
82 SubtargetFeatures getARMFeatures() const;
84 SubtargetFeatures getRISCVFeatures() const;
86 void setARMSubArch(Triple &TheTriple) const override;
88 virtual uint16_t getEType() const = 0;
91 class ELFSectionRef : public SectionRef {
93 ELFSectionRef(const SectionRef &B) : SectionRef(B) {
94 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
97 const ELFObjectFileBase *getObject() const {
98 return cast<ELFObjectFileBase>(SectionRef::getObject());
101 uint32_t getType() const {
102 return getObject()->getSectionType(getRawDataRefImpl());
105 uint64_t getFlags() const {
106 return getObject()->getSectionFlags(getRawDataRefImpl());
109 uint64_t getOffset() const {
110 return getObject()->getSectionOffset(getRawDataRefImpl());
114 class elf_section_iterator : public section_iterator {
116 elf_section_iterator(const section_iterator &B) : section_iterator(B) {
117 assert(isa<ELFObjectFileBase>(B->getObject()));
120 const ELFSectionRef *operator->() const {
121 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
124 const ELFSectionRef &operator*() const {
125 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
129 class ELFSymbolRef : public SymbolRef {
131 ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
132 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
135 const ELFObjectFileBase *getObject() const {
136 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
139 uint64_t getSize() const {
140 return getObject()->getSymbolSize(getRawDataRefImpl());
143 uint8_t getOther() const {
144 return getObject()->getSymbolOther(getRawDataRefImpl());
147 uint8_t getELFType() const {
148 return getObject()->getSymbolELFType(getRawDataRefImpl());
152 class elf_symbol_iterator : public symbol_iterator {
154 elf_symbol_iterator(const basic_symbol_iterator &B)
155 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
156 cast<ELFObjectFileBase>(B->getObject()))) {}
158 const ELFSymbolRef *operator->() const {
159 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
162 const ELFSymbolRef &operator*() const {
163 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
167 class ELFRelocationRef : public RelocationRef {
169 ELFRelocationRef(const RelocationRef &B) : RelocationRef(B) {
170 assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
173 const ELFObjectFileBase *getObject() const {
174 return cast<ELFObjectFileBase>(RelocationRef::getObject());
177 Expected<int64_t> getAddend() const {
178 return getObject()->getRelocationAddend(getRawDataRefImpl());
182 class elf_relocation_iterator : public relocation_iterator {
184 elf_relocation_iterator(const relocation_iterator &B)
185 : relocation_iterator(RelocationRef(
186 B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
188 const ELFRelocationRef *operator->() const {
189 return static_cast<const ELFRelocationRef *>(
190 relocation_iterator::operator->());
193 const ELFRelocationRef &operator*() const {
194 return static_cast<const ELFRelocationRef &>(
195 relocation_iterator::operator*());
199 inline ELFObjectFileBase::elf_symbol_iterator_range
200 ELFObjectFileBase::symbols() const {
201 return elf_symbol_iterator_range(symbol_begin(), symbol_end());
204 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
205 uint16_t getEMachine() const override;
206 uint16_t getEType() const override;
207 uint64_t getSymbolSize(DataRefImpl Sym) const override;
210 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
212 using uintX_t = typename ELFT::uint;
214 using Elf_Sym = typename ELFT::Sym;
215 using Elf_Shdr = typename ELFT::Shdr;
216 using Elf_Ehdr = typename ELFT::Ehdr;
217 using Elf_Rel = typename ELFT::Rel;
218 using Elf_Rela = typename ELFT::Rela;
219 using Elf_Dyn = typename ELFT::Dyn;
222 ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
223 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
224 ArrayRef<Elf_Word> ShndxTable);
229 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
230 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
231 ArrayRef<Elf_Word> ShndxTable;
233 void moveSymbolNext(DataRefImpl &Symb) const override;
234 Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
235 Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
236 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
237 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
238 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
239 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
240 uint8_t getSymbolOther(DataRefImpl Symb) const override;
241 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
242 Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
243 Expected<section_iterator> getSymbolSection(const Elf_Sym *Symb,
244 const Elf_Shdr *SymTab) const;
245 Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
247 void moveSectionNext(DataRefImpl &Sec) const override;
248 std::error_code getSectionName(DataRefImpl Sec,
249 StringRef &Res) const override;
250 uint64_t getSectionAddress(DataRefImpl Sec) const override;
251 uint64_t getSectionIndex(DataRefImpl Sec) const override;
252 uint64_t getSectionSize(DataRefImpl Sec) const override;
253 std::error_code getSectionContents(DataRefImpl Sec,
254 StringRef &Res) const override;
255 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
256 bool isSectionCompressed(DataRefImpl Sec) const override;
257 bool isSectionText(DataRefImpl Sec) const override;
258 bool isSectionData(DataRefImpl Sec) const override;
259 bool isSectionBSS(DataRefImpl Sec) const override;
260 bool isSectionVirtual(DataRefImpl Sec) const override;
261 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
262 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
263 std::vector<SectionRef> dynamic_relocation_sections() const override;
264 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
266 void moveRelocationNext(DataRefImpl &Rel) const override;
267 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
268 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
269 uint64_t getRelocationType(DataRefImpl Rel) const override;
270 void getRelocationTypeName(DataRefImpl Rel,
271 SmallVectorImpl<char> &Result) const override;
273 uint32_t getSectionType(DataRefImpl Sec) const override;
274 uint64_t getSectionFlags(DataRefImpl Sec) const override;
275 uint64_t getSectionOffset(DataRefImpl Sec) const override;
276 StringRef getRelocationTypeName(uint32_t Type) const;
278 /// Get the relocation section that contains \a Rel.
279 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
280 auto RelSecOrErr = EF.getSection(Rel.d.a);
282 report_fatal_error(errorToErrorCode(RelSecOrErr.takeError()).message());
286 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
293 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
294 SymTable->sh_type == ELF::SHT_DYNSYM);
296 auto SectionsOrErr = EF.sections();
297 if (!SectionsOrErr) {
302 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
303 unsigned SymTableIndex =
304 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
306 DRI.d.a = SymTableIndex;
311 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
312 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
315 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
317 DRI.p = reinterpret_cast<uintptr_t>(Sec);
321 DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
323 DRI.p = reinterpret_cast<uintptr_t>(Dyn);
327 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
328 unsigned char Binding = ESym->getBinding();
329 unsigned char Visibility = ESym->getVisibility();
331 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
332 // visibility is either DEFAULT or PROTECTED. All other symbols are not
334 return ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
335 (Visibility == ELF::STV_DEFAULT ||
336 Visibility == ELF::STV_PROTECTED));
339 // This flag is used for classof, to distinguish ELFObjectFile from
340 // its subclass. If more subclasses will be created, this flag will
341 // have to become an enum.
342 bool isDyldELFObject;
345 ELFObjectFile(ELFObjectFile<ELFT> &&Other);
346 static Expected<ELFObjectFile<ELFT>> create(MemoryBufferRef Object);
348 const Elf_Rel *getRel(DataRefImpl Rel) const;
349 const Elf_Rela *getRela(DataRefImpl Rela) const;
351 const Elf_Sym *getSymbol(DataRefImpl Sym) const {
352 auto Ret = EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
354 report_fatal_error(errorToErrorCode(Ret.takeError()).message());
358 const Elf_Shdr *getSection(DataRefImpl Sec) const {
359 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
362 basic_symbol_iterator symbol_begin() const override;
363 basic_symbol_iterator symbol_end() const override;
365 elf_symbol_iterator dynamic_symbol_begin() const;
366 elf_symbol_iterator dynamic_symbol_end() const;
368 section_iterator section_begin() const override;
369 section_iterator section_end() const override;
371 Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
373 uint8_t getBytesInAddress() const override;
374 StringRef getFileFormatName() const override;
375 Triple::ArchType getArch() const override;
376 Expected<uint64_t> getStartAddress() const override;
378 unsigned getPlatformFlags() const override { return EF.getHeader()->e_flags; }
380 std::error_code getBuildAttributes(ARMAttributeParser &Attributes) const override {
381 auto SectionsOrErr = EF.sections();
383 return errorToErrorCode(SectionsOrErr.takeError());
385 for (const Elf_Shdr &Sec : *SectionsOrErr) {
386 if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES) {
387 auto ErrorOrContents = EF.getSectionContents(&Sec);
388 if (!ErrorOrContents)
389 return errorToErrorCode(ErrorOrContents.takeError());
391 auto Contents = ErrorOrContents.get();
392 if (Contents[0] != ARMBuildAttrs::Format_Version || Contents.size() == 1)
393 return std::error_code();
395 Attributes.Parse(Contents, ELFT::TargetEndianness == support::little);
399 return std::error_code();
402 const ELFFile<ELFT> *getELFFile() const { return &EF; }
404 bool isDyldType() const { return isDyldELFObject; }
405 static bool classof(const Binary *v) {
406 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
410 elf_symbol_iterator_range getDynamicSymbolIterators() const override;
412 bool isRelocatableObject() const override;
415 using ELF32LEObjectFile = ELFObjectFile<ELF32LE>;
416 using ELF64LEObjectFile = ELFObjectFile<ELF64LE>;
417 using ELF32BEObjectFile = ELFObjectFile<ELF32BE>;
418 using ELF64BEObjectFile = ELFObjectFile<ELF64BE>;
420 template <class ELFT>
421 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
425 template <class ELFT>
426 Expected<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const {
427 const Elf_Sym *ESym = getSymbol(Sym);
428 auto SymTabOrErr = EF.getSection(Sym.d.a);
430 return SymTabOrErr.takeError();
431 const Elf_Shdr *SymTableSec = *SymTabOrErr;
432 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
434 return StrTabOrErr.takeError();
435 const Elf_Shdr *StringTableSec = *StrTabOrErr;
436 auto SymStrTabOrErr = EF.getStringTable(StringTableSec);
438 return SymStrTabOrErr.takeError();
439 return ESym->getName(*SymStrTabOrErr);
442 template <class ELFT>
443 uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
444 return getSection(Sec)->sh_flags;
447 template <class ELFT>
448 uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
449 return getSection(Sec)->sh_type;
452 template <class ELFT>
453 uint64_t ELFObjectFile<ELFT>::getSectionOffset(DataRefImpl Sec) const {
454 return getSection(Sec)->sh_offset;
457 template <class ELFT>
458 uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const {
459 const Elf_Sym *ESym = getSymbol(Symb);
460 uint64_t Ret = ESym->st_value;
461 if (ESym->st_shndx == ELF::SHN_ABS)
464 const Elf_Ehdr *Header = EF.getHeader();
465 // Clear the ARM/Thumb or microMIPS indicator flag.
466 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
467 ESym->getType() == ELF::STT_FUNC)
473 template <class ELFT>
475 ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
476 uint64_t Result = getSymbolValue(Symb);
477 const Elf_Sym *ESym = getSymbol(Symb);
478 switch (ESym->st_shndx) {
479 case ELF::SHN_COMMON:
485 const Elf_Ehdr *Header = EF.getHeader();
486 auto SymTabOrErr = EF.getSection(Symb.d.a);
488 return SymTabOrErr.takeError();
489 const Elf_Shdr *SymTab = *SymTabOrErr;
491 if (Header->e_type == ELF::ET_REL) {
492 auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable);
494 return SectionOrErr.takeError();
495 const Elf_Shdr *Section = *SectionOrErr;
497 Result += Section->sh_addr;
503 template <class ELFT>
504 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
505 const Elf_Sym *Sym = getSymbol(Symb);
506 if (Sym->st_shndx == ELF::SHN_COMMON)
507 return Sym->st_value;
511 template <class ELFT>
512 uint16_t ELFObjectFile<ELFT>::getEMachine() const {
513 return EF.getHeader()->e_machine;
516 template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
517 return EF.getHeader()->e_type;
520 template <class ELFT>
521 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
522 return getSymbol(Sym)->st_size;
525 template <class ELFT>
526 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
527 return getSymbol(Symb)->st_size;
530 template <class ELFT>
531 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
532 return getSymbol(Symb)->st_other;
535 template <class ELFT>
536 uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
537 return getSymbol(Symb)->getType();
540 template <class ELFT>
541 Expected<SymbolRef::Type>
542 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
543 const Elf_Sym *ESym = getSymbol(Symb);
545 switch (ESym->getType()) {
546 case ELF::STT_NOTYPE:
547 return SymbolRef::ST_Unknown;
548 case ELF::STT_SECTION:
549 return SymbolRef::ST_Debug;
551 return SymbolRef::ST_File;
553 return SymbolRef::ST_Function;
554 case ELF::STT_OBJECT:
555 case ELF::STT_COMMON:
557 return SymbolRef::ST_Data;
559 return SymbolRef::ST_Other;
563 template <class ELFT>
564 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
565 const Elf_Sym *ESym = getSymbol(Sym);
567 uint32_t Result = SymbolRef::SF_None;
569 if (ESym->getBinding() != ELF::STB_LOCAL)
570 Result |= SymbolRef::SF_Global;
572 if (ESym->getBinding() == ELF::STB_WEAK)
573 Result |= SymbolRef::SF_Weak;
575 if (ESym->st_shndx == ELF::SHN_ABS)
576 Result |= SymbolRef::SF_Absolute;
578 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
579 Result |= SymbolRef::SF_FormatSpecific;
581 auto DotSymtabSecSyms = EF.symbols(DotSymtabSec);
582 if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
583 Result |= SymbolRef::SF_FormatSpecific;
584 auto DotDynSymSecSyms = EF.symbols(DotDynSymSec);
585 if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
586 Result |= SymbolRef::SF_FormatSpecific;
588 if (EF.getHeader()->e_machine == ELF::EM_ARM) {
589 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
590 StringRef Name = *NameOrErr;
591 if (Name.startswith("$d") || Name.startswith("$t") ||
592 Name.startswith("$a"))
593 Result |= SymbolRef::SF_FormatSpecific;
595 // TODO: Actually report errors helpfully.
596 consumeError(NameOrErr.takeError());
598 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
599 Result |= SymbolRef::SF_Thumb;
602 if (ESym->st_shndx == ELF::SHN_UNDEF)
603 Result |= SymbolRef::SF_Undefined;
605 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
606 Result |= SymbolRef::SF_Common;
608 if (isExportedToOtherDSO(ESym))
609 Result |= SymbolRef::SF_Exported;
611 if (ESym->getVisibility() == ELF::STV_HIDDEN)
612 Result |= SymbolRef::SF_Hidden;
617 template <class ELFT>
618 Expected<section_iterator>
619 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym,
620 const Elf_Shdr *SymTab) const {
621 auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
623 return ESecOrErr.takeError();
625 const Elf_Shdr *ESec = *ESecOrErr;
627 return section_end();
630 Sec.p = reinterpret_cast<intptr_t>(ESec);
631 return section_iterator(SectionRef(Sec, this));
634 template <class ELFT>
635 Expected<section_iterator>
636 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb) const {
637 const Elf_Sym *Sym = getSymbol(Symb);
638 auto SymTabOrErr = EF.getSection(Symb.d.a);
640 return SymTabOrErr.takeError();
641 const Elf_Shdr *SymTab = *SymTabOrErr;
642 return getSymbolSection(Sym, SymTab);
645 template <class ELFT>
646 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
647 const Elf_Shdr *ESec = getSection(Sec);
651 template <class ELFT>
652 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
653 StringRef &Result) const {
654 auto Name = EF.getSectionName(&*getSection(Sec));
656 return errorToErrorCode(Name.takeError());
658 return std::error_code();
661 template <class ELFT>
662 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
663 return getSection(Sec)->sh_addr;
666 template <class ELFT>
667 uint64_t ELFObjectFile<ELFT>::getSectionIndex(DataRefImpl Sec) const {
668 auto SectionsOrErr = EF.sections();
669 handleAllErrors(std::move(SectionsOrErr.takeError()),
670 [](const ErrorInfoBase &) {
671 llvm_unreachable("unable to get section index");
673 const Elf_Shdr *First = SectionsOrErr->begin();
674 return getSection(Sec) - First;
677 template <class ELFT>
678 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
679 return getSection(Sec)->sh_size;
682 template <class ELFT>
684 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
685 StringRef &Result) const {
686 const Elf_Shdr *EShdr = getSection(Sec);
687 if (std::error_code EC =
688 checkOffset(getMemoryBufferRef(),
689 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
691 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
692 return std::error_code();
695 template <class ELFT>
696 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
697 return getSection(Sec)->sh_addralign;
700 template <class ELFT>
701 bool ELFObjectFile<ELFT>::isSectionCompressed(DataRefImpl Sec) const {
702 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
705 template <class ELFT>
706 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
707 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
710 template <class ELFT>
711 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
712 const Elf_Shdr *EShdr = getSection(Sec);
713 return EShdr->sh_type == ELF::SHT_PROGBITS &&
714 EShdr->sh_flags & ELF::SHF_ALLOC &&
715 !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
718 template <class ELFT>
719 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
720 const Elf_Shdr *EShdr = getSection(Sec);
721 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
722 EShdr->sh_type == ELF::SHT_NOBITS;
725 template <class ELFT>
726 std::vector<SectionRef>
727 ELFObjectFile<ELFT>::dynamic_relocation_sections() const {
728 std::vector<SectionRef> Res;
729 std::vector<uintptr_t> Offsets;
731 auto SectionsOrErr = EF.sections();
735 for (const Elf_Shdr &Sec : *SectionsOrErr) {
736 if (Sec.sh_type != ELF::SHT_DYNAMIC)
739 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
740 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
741 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
742 Dynamic->d_tag == ELF::DT_JMPREL) {
743 Offsets.push_back(Dynamic->d_un.d_val);
747 for (const Elf_Shdr &Sec : *SectionsOrErr) {
748 if (is_contained(Offsets, Sec.sh_offset))
749 Res.emplace_back(toDRI(&Sec), this);
754 template <class ELFT>
755 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
756 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
759 template <class ELFT>
761 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
763 auto SectionsOrErr = EF.sections();
765 return relocation_iterator(RelocationRef());
766 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
767 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
769 return relocation_iterator(RelocationRef(RelData, this));
772 template <class ELFT>
774 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
775 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
776 relocation_iterator Begin = section_rel_begin(Sec);
777 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
779 DataRefImpl RelData = Begin->getRawDataRefImpl();
780 const Elf_Shdr *RelSec = getRelSection(RelData);
782 // Error check sh_link here so that getRelocationSymbol can just use it.
783 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
785 report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message());
787 RelData.d.b += S->sh_size / S->sh_entsize;
788 return relocation_iterator(RelocationRef(RelData, this));
791 template <class ELFT>
793 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
794 if (EF.getHeader()->e_type != ELF::ET_REL)
795 return section_end();
797 const Elf_Shdr *EShdr = getSection(Sec);
798 uintX_t Type = EShdr->sh_type;
799 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
800 return section_end();
802 auto R = EF.getSection(EShdr->sh_info);
804 report_fatal_error(errorToErrorCode(R.takeError()).message());
805 return section_iterator(SectionRef(toDRI(*R), this));
809 template <class ELFT>
810 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
814 template <class ELFT>
816 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
818 const Elf_Shdr *sec = getRelSection(Rel);
819 if (sec->sh_type == ELF::SHT_REL)
820 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
822 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
826 // FIXME: error check symbolIdx
827 DataRefImpl SymbolData;
828 SymbolData.d.a = sec->sh_link;
829 SymbolData.d.b = symbolIdx;
830 return symbol_iterator(SymbolRef(SymbolData, this));
833 template <class ELFT>
834 uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
835 const Elf_Shdr *sec = getRelSection(Rel);
836 if (sec->sh_type == ELF::SHT_REL)
837 return getRel(Rel)->r_offset;
839 return getRela(Rel)->r_offset;
842 template <class ELFT>
843 uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
844 const Elf_Shdr *sec = getRelSection(Rel);
845 if (sec->sh_type == ELF::SHT_REL)
846 return getRel(Rel)->getType(EF.isMips64EL());
848 return getRela(Rel)->getType(EF.isMips64EL());
851 template <class ELFT>
852 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
853 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
856 template <class ELFT>
857 void ELFObjectFile<ELFT>::getRelocationTypeName(
858 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
859 uint32_t type = getRelocationType(Rel);
860 EF.getRelocationTypeName(type, Result);
863 template <class ELFT>
865 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
866 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
867 return createError("Section is not SHT_RELA");
868 return (int64_t)getRela(Rel)->r_addend;
871 template <class ELFT>
872 const typename ELFObjectFile<ELFT>::Elf_Rel *
873 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
874 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
875 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
877 report_fatal_error(errorToErrorCode(Ret.takeError()).message());
881 template <class ELFT>
882 const typename ELFObjectFile<ELFT>::Elf_Rela *
883 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
884 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
885 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
887 report_fatal_error(errorToErrorCode(Ret.takeError()).message());
891 template <class ELFT>
892 Expected<ELFObjectFile<ELFT>>
893 ELFObjectFile<ELFT>::create(MemoryBufferRef Object) {
894 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
895 if (Error E = EFOrErr.takeError())
897 auto EF = std::move(*EFOrErr);
899 auto SectionsOrErr = EF.sections();
901 return SectionsOrErr.takeError();
903 const Elf_Shdr *DotDynSymSec = nullptr;
904 const Elf_Shdr *DotSymtabSec = nullptr;
905 ArrayRef<Elf_Word> ShndxTable;
906 for (const Elf_Shdr &Sec : *SectionsOrErr) {
907 switch (Sec.sh_type) {
908 case ELF::SHT_DYNSYM: {
910 return createError("More than one dynamic symbol table!");
914 case ELF::SHT_SYMTAB: {
916 return createError("More than one static symbol table!");
920 case ELF::SHT_SYMTAB_SHNDX: {
921 auto TableOrErr = EF.getSHNDXTable(Sec);
923 return TableOrErr.takeError();
924 ShndxTable = *TableOrErr;
929 return ELFObjectFile<ELFT>(Object, EF, DotDynSymSec, DotSymtabSec,
933 template <class ELFT>
934 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
935 const Elf_Shdr *DotDynSymSec,
936 const Elf_Shdr *DotSymtabSec,
937 ArrayRef<Elf_Word> ShndxTable)
939 getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
941 EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
942 ShndxTable(ShndxTable) {}
944 template <class ELFT>
945 ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
946 : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
947 Other.DotSymtabSec, Other.ShndxTable) {}
949 template <class ELFT>
950 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin() const {
951 DataRefImpl Sym = toDRI(DotSymtabSec, 0);
952 return basic_symbol_iterator(SymbolRef(Sym, this));
955 template <class ELFT>
956 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end() const {
957 const Elf_Shdr *SymTab = DotSymtabSec;
959 return symbol_begin();
960 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
961 return basic_symbol_iterator(SymbolRef(Sym, this));
964 template <class ELFT>
965 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
966 DataRefImpl Sym = toDRI(DotDynSymSec, 0);
967 return symbol_iterator(SymbolRef(Sym, this));
970 template <class ELFT>
971 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
972 const Elf_Shdr *SymTab = DotDynSymSec;
974 return dynamic_symbol_begin();
975 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
976 return basic_symbol_iterator(SymbolRef(Sym, this));
979 template <class ELFT>
980 section_iterator ELFObjectFile<ELFT>::section_begin() const {
981 auto SectionsOrErr = EF.sections();
983 return section_iterator(SectionRef());
984 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
987 template <class ELFT>
988 section_iterator ELFObjectFile<ELFT>::section_end() const {
989 auto SectionsOrErr = EF.sections();
991 return section_iterator(SectionRef());
992 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
995 template <class ELFT>
996 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
997 return ELFT::Is64Bits ? 8 : 4;
1000 template <class ELFT>
1001 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
1002 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1003 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1004 case ELF::ELFCLASS32:
1005 switch (EF.getHeader()->e_machine) {
1007 return "ELF32-i386";
1009 return "ELF32-iamcu";
1010 case ELF::EM_X86_64:
1011 return "ELF32-x86-64";
1013 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
1016 case ELF::EM_HEXAGON:
1017 return "ELF32-hexagon";
1019 return "ELF32-lanai";
1021 return "ELF32-mips";
1025 return "ELF32-riscv";
1027 case ELF::EM_SPARC32PLUS:
1028 return "ELF32-sparc";
1029 case ELF::EM_AMDGPU:
1030 return "ELF32-amdgpu";
1032 return "ELF32-unknown";
1034 case ELF::ELFCLASS64:
1035 switch (EF.getHeader()->e_machine) {
1037 return "ELF64-i386";
1038 case ELF::EM_X86_64:
1039 return "ELF64-x86-64";
1040 case ELF::EM_AARCH64:
1041 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
1043 return "ELF64-ppc64";
1045 return "ELF64-riscv";
1047 return "ELF64-s390";
1048 case ELF::EM_SPARCV9:
1049 return "ELF64-sparc";
1051 return "ELF64-mips";
1052 case ELF::EM_AMDGPU:
1053 return "ELF64-amdgpu";
1057 return "ELF64-unknown";
1060 // FIXME: Proper error handling.
1061 report_fatal_error("Invalid ELFCLASS!");
1065 template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1066 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1067 switch (EF.getHeader()->e_machine) {
1071 case ELF::EM_X86_64:
1072 return Triple::x86_64;
1073 case ELF::EM_AARCH64:
1074 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1079 case ELF::EM_HEXAGON:
1080 return Triple::hexagon;
1082 return Triple::lanai;
1084 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1085 case ELF::ELFCLASS32:
1086 return IsLittleEndian ? Triple::mipsel : Triple::mips;
1087 case ELF::ELFCLASS64:
1088 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1090 report_fatal_error("Invalid ELFCLASS!");
1095 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1097 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1098 case ELF::ELFCLASS32:
1099 return Triple::riscv32;
1100 case ELF::ELFCLASS64:
1101 return Triple::riscv64;
1103 report_fatal_error("Invalid ELFCLASS!");
1106 return Triple::systemz;
1109 case ELF::EM_SPARC32PLUS:
1110 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1111 case ELF::EM_SPARCV9:
1112 return Triple::sparcv9;
1114 case ELF::EM_AMDGPU: {
1115 if (!IsLittleEndian)
1116 return Triple::UnknownArch;
1118 unsigned MACH = EF.getHeader()->e_flags & ELF::EF_AMDGPU_MACH;
1119 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1120 MACH <= ELF::EF_AMDGPU_MACH_R600_LAST)
1121 return Triple::r600;
1122 if (MACH >= ELF::EF_AMDGPU_MACH_AMDGCN_FIRST &&
1123 MACH <= ELF::EF_AMDGPU_MACH_AMDGCN_LAST)
1124 return Triple::amdgcn;
1126 return Triple::UnknownArch;
1130 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1133 return Triple::UnknownArch;
1137 template <class ELFT>
1138 Expected<uint64_t> ELFObjectFile<ELFT>::getStartAddress() const {
1139 return EF.getHeader()->e_entry;
1142 template <class ELFT>
1143 ELFObjectFileBase::elf_symbol_iterator_range
1144 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
1145 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
1148 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1149 return EF.getHeader()->e_type == ELF::ET_REL;
1152 } // end namespace object
1153 } // end namespace llvm
1155 #endif // LLVM_OBJECT_ELFOBJECTFILE_H