14 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H 15 #define LLVM_OBJECT_ELFOBJECTFILE_H 40 #include <system_error> 45 class elf_symbol_iterator;
88 virtual uint16_t
getEType()
const = 0;
102 return getObject()->getSectionType(getRawDataRefImpl());
106 return getObject()->getSectionFlags(getRawDataRefImpl());
110 return getObject()->getSectionOffset(getRawDataRefImpl());
117 assert(isa<ELFObjectFileBase>(B->getObject()));
140 return getObject()->getSymbolSize(getRawDataRefImpl());
144 return getObject()->getSymbolOther(getRawDataRefImpl());
148 return getObject()->getSymbolELFType(getRawDataRefImpl());
178 return getObject()->getRelocationAddend(getRawDataRefImpl());
280 auto RelSecOrErr = EF.getSection(Rel.d.a);
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;
312 return reinterpret_cast<const Elf_Shdr *
>(Sec.
p);
317 DRI.
p =
reinterpret_cast<uintptr_t
>(Sec);
323 DRI.
p =
reinterpret_cast<uintptr_t
>(Dyn);
328 unsigned char Binding = ESym->getBinding();
329 unsigned char Visibility = ESym->getVisibility();
352 auto Ret = EF.template getEntry<Elf_Sym>(Sym.
d.
a, Sym.
d.
b);
359 return reinterpret_cast<const Elf_Shdr *
>(Sec.
p);
381 auto SectionsOrErr = EF.sections();
385 for (
const Elf_Shdr &Sec : *SectionsOrErr) {
387 auto ErrorOrContents = EF.getSectionContents(&Sec);
388 if (!ErrorOrContents)
391 auto Contents = ErrorOrContents.get();
393 return std::error_code();
399 return std::error_code();
420 template <
class ELFT>
425 template <
class ELFT>
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>
447 template <
class ELFT>
452 template <
class ELFT>
457 template <
class ELFT>
460 uint64_t
Ret = ESym->st_value;
464 const Elf_Ehdr *Header = EF.getHeader();
473 template <
class ELFT>
478 switch (ESym->st_shndx) {
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;
492 auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable);
494 return SectionOrErr.takeError();
497 Result += Section->sh_addr;
503 template <
class ELFT>
507 return Sym->st_value;
511 template <
class ELFT>
513 return EF.getHeader()->e_machine;
517 return EF.getHeader()->e_type;
520 template <
class ELFT>
525 template <
class ELFT>
530 template <
class ELFT>
535 template <
class ELFT>
540 template <
class ELFT>
545 switch (ESym->getType()) {
563 template <
class ELFT>
581 auto DotSymtabSecSyms = EF.symbols(DotSymtabSec);
582 if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
584 auto DotDynSymSecSyms = EF.symbols(DotDynSymSec);
585 if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
598 if (ESym->getType() ==
ELF::STT_FUNC && (ESym->st_value & 1) == 1)
608 if (isExportedToOtherDSO(ESym))
617 template <
class ELFT>
621 auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
623 return ESecOrErr.takeError();
630 Sec.
p =
reinterpret_cast<intptr_t>(ESec);
634 template <
class ELFT>
638 auto SymTabOrErr = EF.getSection(Symb.
d.
a);
640 return SymTabOrErr.takeError();
641 const Elf_Shdr *SymTab = *SymTabOrErr;
645 template <
class ELFT>
651 template <
class ELFT>
658 return std::error_code();
661 template <
class ELFT>
666 template <
class ELFT>
668 auto SectionsOrErr = EF.sections();
673 const Elf_Shdr *First = SectionsOrErr->begin();
677 template <
class ELFT>
682 template <
class ELFT>
687 if (std::error_code EC =
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>
700 template <
class ELFT>
705 template <
class ELFT>
710 template <
class ELFT>
718 template <
class ELFT>
725 template <
class ELFT>
726 std::vector<SectionRef>
728 std::vector<SectionRef> Res;
729 std::vector<uintptr_t>
Offsets;
731 auto SectionsOrErr = EF.sections();
735 for (
const Elf_Shdr &Sec : *SectionsOrErr) {
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) {
749 Res.emplace_back(toDRI(&Sec),
this);
754 template <
class ELFT>
759 template <
class ELFT>
763 auto SectionsOrErr = EF.sections();
766 uintptr_t SHT =
reinterpret_cast<uintptr_t
>((*SectionsOrErr).begin());
767 RelData.
d.
a = (Sec.
p - SHT) / EF.getHeader()->e_shentsize;
772 template <
class ELFT>
780 const Elf_Shdr *RelSec = getRelSection(RelData);
783 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
787 RelData.
d.
b += S->sh_size / S->sh_entsize;
791 template <
class ELFT>
802 auto R = EF.getSection(EShdr->sh_info);
809 template <
class ELFT>
814 template <
class ELFT>
818 const Elf_Shdr *sec = getRelSection(Rel);
820 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
822 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
828 SymbolData.
d.
a = sec->sh_link;
829 SymbolData.
d.
b = symbolIdx;
833 template <
class ELFT>
835 const Elf_Shdr *sec = getRelSection(Rel);
837 return getRel(Rel)->r_offset;
839 return getRela(Rel)->r_offset;
842 template <
class ELFT>
844 const Elf_Shdr *sec = getRelSection(Rel);
846 return getRel(Rel)->getType(EF.isMips64EL());
848 return getRela(Rel)->getType(EF.isMips64EL());
851 template <
class ELFT>
856 template <
class ELFT>
860 EF.getRelocationTypeName(type, Result);
863 template <
class ELFT>
868 return (int64_t)getRela(Rel)->r_addend;
871 template <
class ELFT>
875 auto Ret = EF.template getEntry<Elf_Rel>(Rel.
d.
a, Rel.
d.
b);
881 template <
class ELFT>
885 auto Ret = EF.template getEntry<Elf_Rela>(Rela.
d.
a, Rela.
d.
b);
891 template <
class ELFT>
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;
906 for (
const Elf_Shdr &Sec : *SectionsOrErr) {
907 switch (Sec.sh_type) {
910 return createError(
"More than one dynamic symbol table!");
916 return createError(
"More than one static symbol table!");
921 auto TableOrErr = EF.getSHNDXTable(Sec);
923 return TableOrErr.takeError();
924 ShndxTable = *TableOrErr;
933 template <
class ELFT>
941 EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
942 ShndxTable(ShndxTable) {}
944 template <
class ELFT>
949 template <
class ELFT>
955 template <
class ELFT>
964 template <
class ELFT>
970 template <
class ELFT>
979 template <
class ELFT>
987 template <
class ELFT>
995 template <
class ELFT>
997 return ELFT::Is64Bits ? 8 : 4;
1000 template <
class ELFT>
1007 return "ELF32-i386";
1009 return "ELF32-iamcu";
1011 return "ELF32-x86-64";
1013 return (IsLittleEndian ?
"ELF32-arm-little" :
"ELF32-arm-big");
1017 return "ELF32-hexagon";
1019 return "ELF32-lanai";
1021 return "ELF32-mips";
1025 return "ELF32-riscv";
1028 return "ELF32-sparc";
1030 return "ELF32-amdgpu";
1032 return "ELF32-unknown";
1037 return "ELF64-i386";
1039 return "ELF64-x86-64";
1041 return (IsLittleEndian ?
"ELF64-aarch64-little" :
"ELF64-aarch64-big");
1043 return "ELF64-ppc64";
1045 return "ELF64-riscv";
1047 return "ELF64-s390";
1049 return "ELF64-sparc";
1051 return "ELF64-mips";
1053 return "ELF64-amdgpu";
1057 return "ELF64-unknown";
1115 if (!IsLittleEndian)
1137 template <
class ELFT>
1142 template <
class ELFT>
1155 #endif // LLVM_OBJECT_ELFOBJECTFILE_H const content_type & operator*() const
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
uint64_t getSectionSize(DataRefImpl Sec) const override
virtual uint16_t getEMachine() const =0
static bool classof(const Binary *v)
Expected< uint64_t > getStartAddress() const override
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Compute iterated dominance frontiers using a linear time algorithm.
static Expected< ELFFile > create(StringRef Object)
SubtargetFeatures getFeatures() const override
const Elf_Shdr * toELFShdrIter(DataRefImpl Sec) const
std::vector< SectionRef > dynamic_relocation_sections() const override
bool isSectionBSS(DataRefImpl Sec) const override
bool isSectionText(DataRefImpl Sec) const override
elf_relocation_iterator(const relocation_iterator &B)
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
const ELFObjectFileBase * getObject() const
static std::error_code getObject(const T *&Obj, MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
void moveSymbolNext(DataRefImpl &Symb) const override
static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
virtual bool isSectionBSS(DataRefImpl Sec) const =0
This class is the base class for all object file types.
elf_symbol_iterator_range symbols() const
typename ELFT::Rel Elf_Rel
virtual uint64_t getRelocationOffset(DataRefImpl Rel) const =0
const uint8_t * base() const
ELFYAML::ELF_STV Visibility
virtual std::error_code getSectionName(DataRefImpl Sec, StringRef &Res) const =0
static Error createError(StringRef Err)
const ObjectFile * getObject() const
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const
Base class for error info classes.
bool isSectionVirtual(DataRefImpl Sec) const override
virtual Expected< StringRef > getSymbolName(DataRefImpl Symb) const =0
uint32_t getSectionType(DataRefImpl Sec) const override
virtual Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const =0
void moveRelocationNext(DataRefImpl &Rel) const override
virtual relocation_iterator section_rel_end(DataRefImpl Sec) const =0
basic_symbol_iterator symbol_begin() const override
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
virtual basic_symbol_iterator symbol_begin() const =0
void setARMSubArch(Triple &TheTriple) const override
uint8_t getSymbolOther(DataRefImpl Symb) const override
const Elf_Shdr * DotSymtabSec
uint64_t getSectionAlignment(DataRefImpl Sec) const override
virtual std::error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const =0
Expected< Elf_Shdr_Range > sections() const
StringRef getBuffer() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a value type class that represents a single relocation in the list of relocations in the obje...
Expected< section_iterator > getSymbolSection(const Elf_Sym *Symb, const Elf_Shdr *SymTab) const
elf_section_iterator(const section_iterator &B)
typename ELFT::Ehdr Elf_Ehdr
virtual uint64_t getSectionIndex(DataRefImpl Sec) const =0
const ELFObjectFileBase * getObject() const
Tagged union holding either a T or a Error.
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const =0
content_iterator< SectionRef > section_iterator
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
const ObjectFile * getObject() const
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const
virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const =0
section_iterator section_begin() const override
std::error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const override
MemoryBufferRef getMemoryBufferRef() const
void moveSectionNext(DataRefImpl &Sec) const override
elf_symbol_iterator_range getDynamicSymbolIterators() const override
virtual uint64_t getSectionAlignment(DataRefImpl Sec) const =0
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
virtual Expected< uint64_t > getStartAddress() const
DataRefImpl toDRI(const Elf_Dyn *Dyn) const
std::error_code getBuildAttributes(ARMAttributeParser &Attributes) const override
const ObjectFile * getObject() const
virtual uint64_t getSectionOffset(DataRefImpl Sec) const =0
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type cast(const Y &Val)
elf_symbol_iterator dynamic_symbol_begin() const
relocation_iterator section_rel_end(DataRefImpl Sec) const override
typename ELFT::Shdr Elf_Shdr
Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const override
const content_type * operator->() const
unsigned getPlatformFlags() const override
Returns platform-specific object flags, if any.
const Elf_Shdr * getSection(DataRefImpl Sec) const
virtual Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const =0
virtual uint32_t getSymbolFlags(DataRefImpl Symb) const =0
const Elf_Sym * getSymbol(DataRefImpl Sym) const
ELFRelocationRef(const RelocationRef &B)
uint64_t getFlags() const
static bool classof(const Binary *v)
const ELFObjectFileBase * getObject() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
const Elf_Rela * getRela(DataRefImpl Rela) const
virtual uint8_t getBytesInAddress() const =0
The number of bytes used to represent an address in this object file format.
virtual void moveRelocationNext(DataRefImpl &Rel) const =0
The instances of the Type class are immutable: once they are created, they are never changed...
unsigned int getType() const
uint32_t getSymbolFlags(DataRefImpl Symb) const override
virtual uint64_t getSectionFlags(DataRefImpl Sec) const =0
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
typename ELFT::Sym Elf_Sym
const Elf_Shdr * DotDynSymSec
Expected< int64_t > getAddend() const
static unsigned int getELFType(bool isLE, bool is64Bits)
section_iterator getRelocatedSection(DataRefImpl Sec) const override
const Elf_Rel * getRel(DataRefImpl Rel) const
virtual std::vector< SectionRef > dynamic_relocation_sections() const
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
bool isSectionData(DataRefImpl Sec) const override
typename ELFT::Dyn Elf_Dyn
virtual basic_symbol_iterator symbol_end() const =0
static Expected< ELFObjectFile< ELFT > > create(MemoryBufferRef Object)
bool isExportedToOtherDSO(const Elf_Sym *ESym) const
uint8_t getSymbolELFType(DataRefImpl Symb) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
StringRef getFileFormatName() const override
const ELFSymbolRef & operator*() const
void consumeError(Error Err)
Consume a Error without doing anything.
const ELFSectionRef & operator*() const
DataRefImpl toDRI(const Elf_Shdr *Sec) const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const ELFSectionRef * operator->() const
ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
Triple - Helper class for working with autoconf configuration names.
uint64_t getSectionAddress(DataRefImpl Sec) const override
std::error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
typename ELFT::Rela Elf_Rela
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
virtual Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const =0
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
friend class RelocationRef
const ELFFile< ELFT > * getELFFile() const
virtual section_iterator section_begin() const =0
const SymbolicFile * getObject() const
const SymbolRef & operator*() const
virtual uint64_t getSectionAddress(DataRefImpl Sec) const =0
virtual Triple::ArchType getArch() const =0
uint64_t getSymbolValue(DataRefImpl Symb) const
virtual unsigned getPlatformFlags() const =0
Returns platform-specific object flags, if any.
virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const =0
uint64_t getSectionFlags(DataRefImpl Sec) const override
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
Triple::ArchType getArch() const override
content_iterator< BasicSymbolRef > basic_symbol_iterator
elf_symbol_iterator dynamic_symbol_end() const
A range adaptor for a pair of iterators.
Manages the enabling and disabling of subtarget specific features.
uint64_t getOffset() const
virtual bool isSectionText(DataRefImpl Sec) const =0
virtual uint64_t getSymbolSize(DataRefImpl Symb) const =0
SubtargetFeatures getMIPSFeatures() const
This is a value type class that represents a single symbol in the list of symbols in the object file...
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
uint8_t getELFType() const
virtual void moveSectionNext(DataRefImpl &Sec) const =0
virtual section_iterator section_end() const =0
basic_symbol_iterator symbol_end() const override
virtual uint16_t getEType() const =0
bool isSectionCompressed(DataRefImpl Sec) const override
virtual bool isSectionData(DataRefImpl Sec) const =0
ELFSectionRef(const SectionRef &B)
const ELFSymbolRef * operator->() const
uint64_t getSectionOffset(DataRefImpl Sec) const override
virtual section_iterator getRelocatedSection(DataRefImpl Sec) const
virtual uint32_t getSectionType(DataRefImpl Sec) const =0
virtual void moveSymbolNext(DataRefImpl &Symb) const =0
virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const =0
virtual bool isRelocatableObject() const =0
True if this is a relocatable object (.o/.obj).
section_iterator section_end() const override
const SymbolRef * operator->() const
virtual bool isSectionCompressed(DataRefImpl Sec) const =0
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const =0
virtual elf_symbol_iterator_range getDynamicSymbolIterators() const =0
virtual bool isSectionVirtual(DataRefImpl Sec) const =0
void Parse(ArrayRef< uint8_t > Section, bool isLittle)
virtual uint64_t getSectionSize(DataRefImpl Sec) const =0
Lightweight error class with error context and mandatory checking.
virtual void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const =0
const ELFRelocationRef & operator*() const
content_iterator< RelocationRef > relocation_iterator
SubtargetFeatures getARMFeatures() const
uint64_t getRelocationType(DataRefImpl Rel) const override
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
struct llvm::object::DataRefImpl::@261 d
StringRef - Represent a constant reference to a string, i.e.
virtual uint8_t getSymbolELFType(DataRefImpl Symb) const =0
SubtargetFeatures getRISCVFeatures() const
const Elf_Ehdr * getHeader() const
virtual uint8_t getSymbolOther(DataRefImpl Symb) const =0
const ELFRelocationRef * operator->() const
elf_symbol_iterator(const basic_symbol_iterator &B)
virtual StringRef getFileFormatName() const =0
virtual uint64_t getRelocationType(DataRefImpl Rel) const =0
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
typename ELFT::uint uintX_t
This is a value type class that represents a single section in the list of sections in the object fil...
ELFSymbolRef(const SymbolRef &B)
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.