13#ifndef LLVM_OBJECT_ELFOBJECTFILE_H
14#define LLVM_OBJECT_ELFOBJECTFILE_H
42template <
typename T>
class SmallVectorImpl;
116 readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt)
const;
145 assert(isa<ELFObjectFileBase>(
B->getObject()));
186 if (EE.Value ==
Type) {
247 uint16_t getEMachine()
const override;
269 bool ContentValid =
false;
292 const Elf_Shdr *SymTab)
const;
339 auto SectionsOrErr =
EF.sections();
340 if (!SectionsOrErr) {
345 uintptr_t SHT =
reinterpret_cast<uintptr_t
>((*SectionsOrErr).begin());
346 unsigned SymTableIndex =
347 (
reinterpret_cast<uintptr_t
>(SymTable) - SHT) /
sizeof(Elf_Shdr);
349 DRI.
d.
a = SymTableIndex;
355 return reinterpret_cast<const Elf_Shdr *
>(Sec.
p);
360 DRI.
p =
reinterpret_cast<uintptr_t
>(Sec);
366 DRI.
p =
reinterpret_cast<uintptr_t
>(Dyn);
371 unsigned char Binding = ESym->getBinding();
372 unsigned char Visibility = ESym->getVisibility();
384 auto SectionsOrErr =
EF.sections();
386 return SectionsOrErr.takeError();
388 for (
const Elf_Shdr &Sec : *SectionsOrErr) {
391 auto ErrorOrContents =
EF.getSectionContents(Sec);
392 if (!ErrorOrContents)
393 return ErrorOrContents.takeError();
395 auto Contents = ErrorOrContents.get();
415 bool InitContent =
true);
421 return EF.template getEntry<Elf_Sym>(
Sym.d.a,
Sym.d.b);
426 auto RelSecOrErr =
EF.getSection(Rel.
d.
a);
434 return reinterpret_cast<const Elf_Shdr *
>(Sec.
p);
483 auto SectionsOrErr = EF.sections();
485 return SectionsOrErr.takeError();
487 for (
const Elf_Shdr &Sec : *SectionsOrErr) {
488 switch (Sec.sh_type) {
500 if (!DotSymtabShndxSec)
501 DotSymtabShndxSec = &Sec;
516 auto SymTabOrErr = EF.getSection(
Sym.d.a);
518 return SymTabOrErr.takeError();
519 const Elf_Shdr *SymTableSec = *SymTabOrErr;
520 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
522 return StrTabOrErr.takeError();
523 const Elf_Shdr *StringTableSec = *StrTabOrErr;
524 auto SymStrTabOrErr = EF.getStringTable(*StringTableSec);
526 return SymStrTabOrErr.takeError();
535 return (*SecOrErr)->getName();
562 uint64_t Ret = (*SymOrErr)->st_value;
566 const Elf_Ehdr &Header = EF.getHeader();
579 if (!SymbolValueOrErr)
583 uint64_t Result = *SymbolValueOrErr;
588 switch ((*SymOrErr)->st_shndx) {
595 auto SymTabOrErr = EF.getSection(Symb.
d.
a);
597 return SymTabOrErr.takeError();
601 if (DotSymtabShndxSec) {
604 EF.getSHNDXTable(*DotSymtabShndxSec))
605 ShndxTable = *ShndxTableOrErr;
607 return ShndxTableOrErr.takeError();
611 EF.getSection(**SymOrErr, *SymTabOrErr, ShndxTable);
614 const Elf_Shdr *Section = *SectionOrErr;
616 Result += Section->sh_addr;
628 return (*SymOrErr)->st_value;
634 return EF.getHeader().e_machine;
637template <
class ELFT>
uint16_t ELFObjectFile<ELFT>::getEType()
const {
638 return EF.getHeader().e_type;
642uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl
Sym)
const {
643 Expected<const Elf_Sym *> SymOrErr = getSymbol(
Sym);
646 return (*SymOrErr)->st_size;
651 return getSymbolSize(Symb);
659 return (*SymOrErr)->getBinding();
667 return (*SymOrErr)->st_other;
675 return (*SymOrErr)->getType();
685 switch ((*SymOrErr)->getType()) {
709 const Elf_Sym *ESym = *SymOrErr;
725 EF.symbols(DotSymtabSec)) {
727 if (ESym == SymbolsOrErr->begin())
731 return SymbolsOrErr.takeError();
734 EF.symbols(DotDynSymSec)) {
736 if (ESym == SymbolsOrErr->begin())
740 return SymbolsOrErr.takeError();
745 if (
Name.startswith(
"$d") ||
Name.startswith(
"$x"))
751 }
else if (EF.getHeader().e_machine ==
ELF::EM_ARM) {
755 if (
Name.empty() ||
Name.startswith(
"$d") ||
Name.startswith(
"$t") ||
756 Name.startswith(
"$a"))
762 if (ESym->getType() ==
ELF::STT_FUNC && (ESym->st_value & 1) == 1)
767 if (
Name.startswith(
"$d") ||
Name.startswith(
"$t"))
778 if (
Name.empty() ||
Name.startswith(
"$d") ||
Name.startswith(
"$x"))
792 if (isExportedToOtherDSO(ESym))
807 const Elf_Shdr *SymTab)
const {
809 if (DotSymtabShndxSec) {
812 EF.getSHNDXTable(*DotSymtabShndxSec);
813 if (!ShndxTableOrErr)
815 ShndxTable = *ShndxTableOrErr;
818 auto ESecOrErr = EF.getSection(*ESym, SymTab, ShndxTable);
822 const Elf_Shdr *ESec = *ESecOrErr;
824 return section_end();
827 Sec.
p =
reinterpret_cast<intptr_t
>(ESec);
838 auto SymTabOrErr = EF.getSection(Symb.
d.
a);
840 return SymTabOrErr.takeError();
841 return getSymbolSection(*SymOrErr, *SymTabOrErr);
862 auto SectionsOrErr = EF.sections();
865 llvm_unreachable(
"unable to get section index");
867 const Elf_Shdr *
First = SectionsOrErr->begin();
881 return ArrayRef((
const uint8_t *)base(), (
size_t)0);
883 checkOffset(getMemoryBufferRef(),
884 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
886 return ArrayRef((
const uint8_t *)base() + EShdr->sh_offset, EShdr->sh_size);
920std::vector<SectionRef>
922 std::vector<SectionRef> Res;
923 std::vector<uintptr_t> Offsets;
925 auto SectionsOrErr = EF.sections();
929 for (
const Elf_Shdr &Sec : *SectionsOrErr) {
933 reinterpret_cast<Elf_Dyn *
>((uintptr_t)base() + Sec.sh_offset);
935 if (
Dynamic->d_tag == ELF::DT_REL ||
Dynamic->d_tag == ELF::DT_RELA ||
936 Dynamic->d_tag == ELF::DT_JMPREL) {
937 Offsets.push_back(
Dynamic->d_un.d_val);
941 for (
const Elf_Shdr &Sec : *SectionsOrErr) {
943 Res.emplace_back(toDRI(&Sec),
this);
970 if (!SectionNameOrErr) {
984 auto SectionsOrErr = EF.sections();
987 uintptr_t SHT =
reinterpret_cast<uintptr_t
>((*SectionsOrErr).begin());
988 RelData.
d.
a = (Sec.
p - SHT) / EF.getHeader().e_shentsize;
996 const Elf_Shdr *S =
reinterpret_cast<const Elf_Shdr *
>(Sec.
p);
1001 const Elf_Shdr *RelSec = getRelSection(RelData);
1004 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
1009 RelData.
d.
b += S->sh_size / S->sh_entsize;
1013template <
class ELFT>
1017 uintX_t
Type = EShdr->sh_type;
1019 return section_end();
1028template <
class ELFT>
1033template <
class ELFT>
1037 const Elf_Shdr *sec = getRelSection(Rel);
1039 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
1041 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
1043 return symbol_end();
1047 SymbolData.
d.
a = sec->sh_link;
1048 SymbolData.
d.
b = symbolIdx;
1052template <
class ELFT>
1054 const Elf_Shdr *sec = getRelSection(Rel);
1056 return getRel(Rel)->r_offset;
1058 return getRela(Rel)->r_offset;
1061template <
class ELFT>
1063 const Elf_Shdr *sec = getRelSection(Rel);
1065 return getRel(Rel)->getType(EF.isMips64EL());
1067 return getRela(Rel)->getType(EF.isMips64EL());
1070template <
class ELFT>
1075template <
class ELFT>
1078 uint32_t type = getRelocationType(Rel);
1079 EF.getRelocationTypeName(type, Result);
1082template <
class ELFT>
1087 return (int64_t)getRela(Rel)->r_addend;
1090template <
class ELFT>
1094 auto Ret = EF.template getEntry<Elf_Rel>(Rel.
d.
a, Rel.
d.
b);
1100template <
class ELFT>
1104 auto Ret = EF.template getEntry<Elf_Rela>(Rela.
d.
a, Rela.
d.
b);
1110template <
class ELFT>
1114 if (
Error E = EFOrErr.takeError())
1115 return std::move(
E);
1121 return std::move(
E);
1122 return std::move(Obj);
1125template <
class ELFT>
1127 const Elf_Shdr *DotDynSymSec,
1128 const Elf_Shdr *DotSymtabSec,
1129 const Elf_Shdr *DotSymtabShndx)
1131 getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
1133 EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
1134 DotSymtabShndxSec(DotSymtabShndx) {}
1136template <
class ELFT>
1139 Other.DotSymtabSec,
Other.DotSymtabShndxSec) {}
1141template <
class ELFT>
1145 DotSymtabSec && DotSymtabSec->sh_size >=
sizeof(Elf_Sym) ? 1 : 0);
1149template <
class ELFT>
1151 const Elf_Shdr *SymTab = DotSymtabSec;
1153 return symbol_begin();
1154 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size /
sizeof(Elf_Sym));
1158template <
class ELFT>
1160 if (!DotDynSymSec || DotDynSymSec->sh_size <
sizeof(Elf_Sym))
1168template <
class ELFT>
1170 const Elf_Shdr *SymTab = DotDynSymSec;
1172 return dynamic_symbol_begin();
1173 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size /
sizeof(Elf_Sym));
1177template <
class ELFT>
1179 auto SectionsOrErr = EF.sections();
1185template <
class ELFT>
1187 auto SectionsOrErr = EF.sections();
1193template <
class ELFT>
1195 return ELFT::Is64Bits ? 8 : 4;
1198template <
class ELFT>
1200 constexpr bool IsLittleEndian = ELFT::TargetEndianness ==
support::little;
1203 switch (EF.getHeader().e_machine) {
1205 return "elf32-m68k";
1207 return "elf32-i386";
1209 return "elf32-iamcu";
1211 return "elf32-x86-64";
1213 return (IsLittleEndian ?
"elf32-littlearm" :
"elf32-bigarm");
1217 return "elf32-hexagon";
1219 return "elf32-lanai";
1221 return "elf32-mips";
1223 return "elf32-msp430";
1225 return (IsLittleEndian ?
"elf32-powerpcle" :
"elf32-powerpc");
1227 return "elf32-littleriscv";
1229 return "elf32-csky";
1232 return "elf32-sparc";
1234 return "elf32-amdgpu";
1236 return "elf32-loongarch";
1238 return "elf32-xtensa";
1240 return "elf32-unknown";
1243 switch (EF.getHeader().e_machine) {
1245 return "elf64-i386";
1247 return "elf64-x86-64";
1249 return (IsLittleEndian ?
"elf64-littleaarch64" :
"elf64-bigaarch64");
1251 return (IsLittleEndian ?
"elf64-powerpcle" :
"elf64-powerpc");
1253 return "elf64-littleriscv";
1255 return "elf64-s390";
1257 return "elf64-sparc";
1259 return "elf64-mips";
1261 return "elf64-amdgpu";
1267 return "elf64-loongarch";
1269 return "elf64-unknown";
1279 switch (EF.getHeader().e_machine) {
1331 if (!IsLittleEndian)
1371template <
class ELFT>
1373 return EF.getHeader().e_entry;
1376template <
class ELFT>
1379 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static StringRef getSymbolName(SymbolKind SymKind)
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static uint64_t getSymbolValue(const MCSymbol &Symbol, const MCAsmLayout &Layout)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Base class for error info classes.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
StringRef - Represent a constant reference to a string, i.e.
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
A range adaptor for a pair of iterators.
const SymbolicFile * getObject() const
DataRefImpl getRawDataRefImpl() const
static unsigned int getELFType(bool isLE, bool is64Bits)
static Expected< ELFFile > create(StringRef Object)
virtual uint64_t getSymbolSize(DataRefImpl Symb) const =0
Expected< std::vector< BBAddrMap > > readBBAddrMap(std::optional< unsigned > TextSectionIndex=std::nullopt) const
Returns a vector of all BB address maps in the object file. When.
virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const =0
virtual uint64_t getSectionFlags(DataRefImpl Sec) const =0
virtual uint16_t getEType() const =0
virtual uint8_t getSymbolELFType(DataRefImpl Symb) const =0
Expected< std::vector< VersionEntry > > readDynsymVersions() const
Returns a vector containing a symbol version for each dynamic symbol.
virtual uint8_t getSymbolOther(DataRefImpl Symb) const =0
virtual elf_symbol_iterator_range getDynamicSymbolIterators() const =0
virtual uint32_t getSectionType(DataRefImpl Sec) const =0
friend class ELFSymbolRef
elf_symbol_iterator_range symbols() const
virtual Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const =0
std::vector< ELFPltEntry > getPltEntries() const
Expected< SubtargetFeatures > getFeatures() const override
virtual uint8_t getSymbolBinding(DataRefImpl Symb) const =0
std::optional< StringRef > tryGetCPUName() const override
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
virtual uint16_t getEMachine() const =0
static bool classof(const Binary *v)
virtual unsigned getPlatformFlags() const =0
Returns platform-specific object flags, if any.
void setARMSubArch(Triple &TheTriple) const override
virtual uint64_t getSectionOffset(DataRefImpl Sec) const =0
static bool classof(const Binary *v)
const ELFFile< ELFT > & getELFFile() const
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
std::vector< SectionRef > dynamic_relocation_sections() const override
uint64_t getRelocationType(DataRefImpl Rel) const override
bool isSectionText(DataRefImpl Sec) const override
uint8_t getSymbolELFType(DataRefImpl Symb) const override
uint64_t getSectionAlignment(DataRefImpl Sec) const override
bool is64Bit() const override
DataRefImpl toDRI(const Elf_Dyn *Dyn) const
bool isSectionVirtual(DataRefImpl Sec) const override
SectionRef toSectionRef(const Elf_Shdr *Sec) const
bool IsContentValid() const
uint32_t getSectionType(DataRefImpl Sec) const override
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
elf_symbol_iterator_range getDynamicSymbolIterators() const override
void createFakeSections()
Expected< const Elf_Sym * > getSymbol(DataRefImpl Sym) const
const Elf_Rel * getRel(DataRefImpl Rel) const
Expected< section_iterator > getSymbolSection(const Elf_Sym *Symb, const Elf_Shdr *SymTab) const
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
basic_symbol_iterator symbol_begin() const override
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const
const Elf_Rela * getRela(DataRefImpl Rela) const
static Expected< ELFObjectFile< ELFT > > create(MemoryBufferRef Object, bool InitContent=true)
bool isExportedToOtherDSO(const Elf_Sym *ESym) const
uint64_t getSectionAddress(DataRefImpl Sec) const override
Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const override
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
bool isSectionData(DataRefImpl Sec) const override
const Elf_Shdr * DotSymtabSec
const Elf_Shdr * DotDynSymSec
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
const Elf_Shdr * toELFShdrIter(DataRefImpl Sec) const
Triple::ArchType getArch() const override
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
uint64_t getSectionSize(DataRefImpl Sec) const override
bool isBerkeleyData(DataRefImpl Sec) const override
StringRef getFileFormatName() const override
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
void moveSymbolNext(DataRefImpl &Symb) const override
uint8_t getSymbolOther(DataRefImpl Symb) const override
section_iterator section_end() const override
const Elf_Shdr * getRelSection(DataRefImpl Rel) const
Get the relocation section that contains Rel.
Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
Expected< section_iterator > getRelocatedSection(DataRefImpl Sec) const override
void moveSectionNext(DataRefImpl &Sec) const override
bool isSectionBSS(DataRefImpl Sec) const override
unsigned getPlatformFlags() const override
Returns platform-specific object flags, if any.
uint64_t getSectionFlags(DataRefImpl Sec) const override
void moveRelocationNext(DataRefImpl &Rel) const override
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
Error initContent() override
basic_symbol_iterator symbol_end() const override
uint8_t getSymbolBinding(DataRefImpl Symb) const override
uint64_t getSectionOffset(DataRefImpl Sec) const override
relocation_iterator section_rel_end(DataRefImpl Sec) const override
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
const Elf_Shdr * getSection(DataRefImpl Sec) const
Error getBuildAttributes(ELFAttributeParser &Attributes) const override
DataRefImpl toDRI(const Elf_Shdr *Sec) const
elf_symbol_iterator dynamic_symbol_begin() const
const Elf_Shdr * DotSymtabShndxSec
elf_symbol_iterator dynamic_symbol_end() const
DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const
bool isDebugSection(DataRefImpl Sec) const override
section_iterator section_begin() const override
bool isSectionCompressed(DataRefImpl Sec) const override
bool isBerkeleyText(DataRefImpl Sec) const override
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
Expected< uint64_t > getStartAddress() const override
Expected< int64_t > getAddend() const
const ELFObjectFileBase * getObject() const
ELFRelocationRef(const RelocationRef &B)
uint64_t getFlags() const
const ELFObjectFileBase * getObject() const
uint64_t getOffset() const
ELFSectionRef(const SectionRef &B)
const ELFObjectFileBase * getObject() const
uint8_t getELFType() const
ELFSymbolRef(const SymbolRef &B)
uint8_t getBinding() const
StringRef getELFTypeName() const
This class is the base class for all object file types.
This is a value type class that represents a single relocation in the list of relocations in the obje...
const ObjectFile * getObject() const
DataRefImpl getRawDataRefImpl() const
This is a value type class that represents a single section in the list of sections in the object fil...
DataRefImpl getRawDataRefImpl() const
const ObjectFile * getObject() const
This is a value type class that represents a single symbol in the list of symbols in the object file.
const ObjectFile * getObject() const
virtual basic_symbol_iterator symbol_begin() const =0
virtual basic_symbol_iterator symbol_end() const =0
const SectionRef & operator*() const
const SectionRef * operator->() const
const ELFRelocationRef & operator*() const
elf_relocation_iterator(const relocation_iterator &B)
const ELFRelocationRef * operator->() const
elf_section_iterator(const section_iterator &B)
const ELFSectionRef * operator->() const
const ELFSectionRef & operator*() const
const ELFSymbolRef & operator*() const
elf_symbol_iterator(const basic_symbol_iterator &B)
const ELFSymbolRef * operator->() const
const SymbolRef * operator->() const
const SymbolRef & operator*() const
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
@ EF_AMDGPU_MACH_AMDGCN_LAST
@ EF_AMDGPU_MACH_R600_LAST
@ EF_AMDGPU_MACH_AMDGCN_FIRST
@ EF_AMDGPU_MACH_R600_FIRST
static Expected< const T * > getObject(MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Error createError(const Twine &Err)
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
constexpr int NumElfSymbolTypes
content_iterator< SectionRef > section_iterator
content_iterator< RelocationRef > relocation_iterator
content_iterator< BasicSymbolRef > basic_symbol_iterator
const llvm::EnumEntry< unsigned > ElfSymbolTypes[NumElfSymbolTypes]
This is an optimization pass for GlobalISel generic memory operations.
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
@ Dynamic
Denotes mode unknown at compile time.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
void consumeError(Error Err)
Consume a Error without doing anything.
std::optional< DataRefImpl > Symbol
struct llvm::object::DataRefImpl::@351 d