14 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H
15 #define LLVM_OBJECT_ELFOBJECTFILE_H
40 #include <system_error>
45 class elf_symbol_iterator;
110 assert(isa<ELFObjectFileBase>(B->getObject()));
198 uint16_t getEMachine()
const override;
199 uint64_t getSymbolSize(
DataRefImpl Sym)
const override;
266 auto RelSecOrErr = EF.getSection(Rel.d.a);
282 auto SectionsOrErr =
EF.sections();
283 if (!SectionsOrErr) {
288 uintptr_t SHT =
reinterpret_cast<uintptr_t
>((*SectionsOrErr).begin());
289 unsigned SymTableIndex =
290 (
reinterpret_cast<uintptr_t
>(SymTable) - SHT) /
sizeof(
Elf_Shdr);
292 DRI.
d.
a = SymTableIndex;
298 return reinterpret_cast<const Elf_Shdr *
>(Sec.
p);
303 DRI.
p =
reinterpret_cast<uintptr_t
>(Sec);
309 DRI.
p =
reinterpret_cast<uintptr_t
>(Dyn);
314 unsigned char Binding = ESym->getBinding();
315 unsigned char Visibility = ESym->getVisibility();
337 auto Ret =
EF.template getEntry<Elf_Sym>(Sym.
d.
a, Sym.
d.
b);
344 return reinterpret_cast<const Elf_Shdr *
>(Sec.
p);
360 unsigned getArch()
const override;
363 Result =
EF.getHeader()->e_flags;
364 return std::error_code();
368 auto SectionsOrErr =
EF.sections();
372 for (
const Elf_Shdr &Sec : *SectionsOrErr) {
374 auto ErrorOrContents =
EF.getSectionContents(&Sec);
375 if (!ErrorOrContents)
378 auto Contents = ErrorOrContents.get();
380 return std::error_code();
386 return std::error_code();
407 template <
class ELFT>
412 template <
class ELFT>
415 auto SymTabOrErr =
EF.getSection(Sym.
d.
a);
417 return SymTabOrErr.takeError();
418 const Elf_Shdr *SymTableSec = *SymTabOrErr;
419 auto StrTabOrErr =
EF.getSection(SymTableSec->sh_link);
421 return StrTabOrErr.takeError();
422 const Elf_Shdr *StringTableSec = *StrTabOrErr;
423 auto SymStrTabOrErr =
EF.getStringTable(StringTableSec);
425 return SymStrTabOrErr.takeError();
426 return ESym->getName(*SymStrTabOrErr);
429 template <
class ELFT>
434 template <
class ELFT>
439 template <
class ELFT>
444 template <
class ELFT>
447 uint64_t
Ret = ESym->st_value;
460 template <
class ELFT>
465 switch (ESym->st_shndx) {
473 auto SymTabOrErr =
EF.getSection(Symb.
d.
a);
475 return SymTabOrErr.takeError();
476 const Elf_Shdr *SymTab = *SymTabOrErr;
479 auto SectionOrErr =
EF.getSection(ESym, SymTab, ShndxTable);
481 return SectionOrErr.takeError();
484 Result += Section->sh_addr;
490 template <
class ELFT>
494 return Sym->st_value;
498 template <
class ELFT>
500 return EF.getHeader()->e_machine;
503 template <
class ELFT>
504 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym)
const {
508 template <
class ELFT>
513 template <
class ELFT>
518 template <
class ELFT>
523 template <
class ELFT>
528 switch (ESym->getType()) {
546 template <
class ELFT>
564 auto DotSymtabSecSyms =
EF.symbols(DotSymtabSec);
565 if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
567 auto DotDynSymSecSyms =
EF.symbols(DotDynSymSec);
568 if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
581 if (ESym->getType() ==
ELF::STT_FUNC && (ESym->st_value & 1) == 1)
591 if (isExportedToOtherDSO(ESym))
600 template <
class ELFT>
604 auto ESecOrErr =
EF.getSection(ESym, SymTab, ShndxTable);
606 return ESecOrErr.takeError();
610 return section_end();
613 Sec.
p =
reinterpret_cast<intptr_t>(ESec);
617 template <
class ELFT>
621 auto SymTabOrErr =
EF.getSection(Symb.
d.
a);
623 return SymTabOrErr.takeError();
624 const Elf_Shdr *SymTab = *SymTabOrErr;
625 return getSymbolSection(Sym, SymTab);
628 template <
class ELFT>
634 template <
class ELFT>
641 return std::error_code();
644 template <
class ELFT>
649 template <
class ELFT>
651 auto SectionsOrErr =
EF.sections();
656 const Elf_Shdr *First = SectionsOrErr->begin();
660 template <
class ELFT>
665 template <
class ELFT>
670 Result =
StringRef((
const char *)base() + EShdr->sh_offset, EShdr->sh_size);
671 return std::error_code();
674 template <
class ELFT>
679 template <
class ELFT>
684 template <
class ELFT>
689 template <
class ELFT>
696 template <
class ELFT>
703 template <
class ELFT>
708 template <
class ELFT>
712 auto SectionsOrErr =
EF.sections();
715 uintptr_t SHT =
reinterpret_cast<uintptr_t
>((*SectionsOrErr).begin());
716 RelData.
d.
a = (Sec.
p - SHT) /
EF.getHeader()->e_shentsize;
721 template <
class ELFT>
729 const Elf_Shdr *RelSec = getRelSection(RelData);
732 auto SymSecOrErr =
EF.getSection(RelSec->sh_link);
736 RelData.
d.
b += S->sh_size / S->sh_entsize;
740 template <
class ELFT>
744 return section_end();
749 return section_end();
751 auto R =
EF.getSection(EShdr->sh_info);
758 template <
class ELFT>
763 template <
class ELFT>
767 const Elf_Shdr *sec = getRelSection(Rel);
769 symbolIdx = getRel(Rel)->getSymbol(
EF.isMips64EL());
771 symbolIdx = getRela(Rel)->getSymbol(
EF.isMips64EL());
777 SymbolData.
d.
a = sec->sh_link;
778 SymbolData.
d.
b = symbolIdx;
782 template <
class ELFT>
785 "Only relocatable object files have relocation offsets");
786 const Elf_Shdr *sec = getRelSection(Rel);
788 return getRel(Rel)->r_offset;
790 return getRela(Rel)->r_offset;
793 template <
class ELFT>
795 const Elf_Shdr *sec = getRelSection(Rel);
797 return getRel(Rel)->getType(
EF.isMips64EL());
799 return getRela(Rel)->getType(
EF.isMips64EL());
802 template <
class ELFT>
807 template <
class ELFT>
810 uint32_t type = getRelocationType(Rel);
811 EF.getRelocationTypeName(type, Result);
814 template <
class ELFT>
819 return (int64_t)getRela(Rel)->r_addend;
822 template <
class ELFT>
826 auto Ret =
EF.template getEntry<Elf_Rel>(Rel.
d.
a, Rel.
d.
b);
832 template <
class ELFT>
836 auto Ret =
EF.template getEntry<Elf_Rela>(Rela.
d.
a, Rela.
d.
b);
842 template <
class ELFT>
845 getELFType(ELFT::TargetEndianness == support::
little, ELFT::Is64Bits),
848 auto SectionsOrErr =
EF.sections();
849 if (!SectionsOrErr) {
853 for (
const Elf_Shdr &Sec : *SectionsOrErr) {
854 switch (Sec.sh_type) {
874 auto TableOrErr =
EF.getSHNDXTable(Sec);
886 template <
class ELFT>
892 template <
class ELFT>
894 const Elf_Shdr *SymTab = DotSymtabSec;
896 return symbol_begin();
901 template <
class ELFT>
907 template <
class ELFT>
909 const Elf_Shdr *SymTab = DotDynSymSec;
911 return dynamic_symbol_begin();
916 template <
class ELFT>
918 auto SectionsOrErr =
EF.sections();
924 template <
class ELFT>
926 auto SectionsOrErr =
EF.sections();
932 template <
class ELFT>
934 return ELFT::Is64Bits ? 8 : 4;
937 template <
class ELFT>
942 switch (
EF.getHeader()->e_machine) {
946 return "ELF32-iamcu";
948 return "ELF32-x86-64";
950 return (IsLittleEndian ?
"ELF32-arm-little" :
"ELF32-arm-big");
954 return "ELF32-hexagon";
956 return "ELF32-lanai";
962 return "ELF32-riscv";
965 return "ELF32-sparc";
969 return "ELF32-amdgpu";
971 return "ELF32-unknown";
974 switch (
EF.getHeader()->e_machine) {
978 return "ELF64-x86-64";
980 return (IsLittleEndian ?
"ELF64-aarch64-little" :
"ELF64-aarch64-big");
982 return "ELF64-ppc64";
984 return "ELF64-riscv";
988 return "ELF64-sparc";
996 "ELF64-amdgpu-hsacobj" :
"ELF64-amdgpu";
1000 return "ELF64-unknown";
1008 template <
class ELFT>
1011 switch (
EF.getHeader()->e_machine) {
1067 && IsLittleEndian) ?
1078 template <
class ELFT>
1081 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
1091 #endif // LLVM_OBJECT_ELFOBJECTFILE_H
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
uint64_t getSectionSize(DataRefImpl Sec) const override
Represents either an error or a value T.
virtual uint16_t getEMachine() const =0
static uint64_t getSymbolValue(const MCSymbol &Symbol, const MCAsmLayout &Layout)
static bool classof(const Binary *v)
DataRefImpl getRawDataRefImpl() const
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
SubtargetFeatures getFeatures() const override
bool isSectionBSS(DataRefImpl Sec) const override
const ObjectFile * getObject() const
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...
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
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
This class is the base class for all object file types.
typename ELFFile< ELFT >::Elf_Sym Elf_Sym
const Elf_Shdr * toELFShdrIter(DataRefImpl Sec) const
ELFYAML::ELF_STV Visibility
DataRefImpl getRawDataRefImpl() const
Base class for error info classes.
bool isSectionVirtual(DataRefImpl Sec) const override
uint32_t getSectionType(DataRefImpl Sec) const override
SubtargetFeatures getARMFeatures() const
DataRefImpl toDRI(const Elf_Shdr *Sec) const
const ELFObjectFileBase * getObject() const
void moveRelocationNext(DataRefImpl &Rel) const override
basic_symbol_iterator symbol_begin() const override
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
virtual basic_symbol_iterator symbol_begin() const =0
typename ELFFile< ELFT >::uintX_t uintX_t
void setARMSubArch(Triple &TheTriple) const override
uint8_t getSymbolOther(DataRefImpl Symb) const override
ErrorOr< int64_t > getRelocationAddend(DataRefImpl Rel) const override
uint8_t getELFType() const
const Elf_Shdr * DotSymtabSec
uint64_t getSectionAlignment(DataRefImpl Sec) const override
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...
elf_section_iterator(const section_iterator &B)
const ELFRelocationRef * operator->() const
Tagged union holding either a T or a Error.
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
typename ELFFile< ELFT >::Elf_Rela Elf_Rela
content_iterator< SectionRef > section_iterator
SubtargetFeatures getMIPSFeatures() const
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
const Elf_Rela * getRela(DataRefImpl Rela) const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
typename ELFFile< ELFT >::Elf_Ehdr Elf_Ehdr
section_iterator section_begin() const override
std::error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const override
void moveSectionNext(DataRefImpl &Sec) const override
elf_symbol_iterator_range getDynamicSymbolIterators() const override
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
elf_symbol_iterator dynamic_symbol_begin() const
const Elf_Rel * getRel(DataRefImpl Rel) const
const Elf_Sym * getSymbol(DataRefImpl Sym) const
const ELFFile< ELFT > * getELFFile() const
std::error_code getBuildAttributes(ARMAttributeParser &Attributes) const override
typename ELFFile< ELFT >::Elf_Rel Elf_Rel
ArrayRef< Elf_Word > ShndxTable
typename ELFFile< ELFT >::Elf_Dyn Elf_Dyn
const SymbolRef * operator->() 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)
relocation_iterator section_rel_end(DataRefImpl Sec) const override
DataRefImpl toDRI(const Elf_Dyn *Dyn) const
DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const
ELFRelocationRef(const RelocationRef &B)
unsigned int getType() const
static bool classof(const Binary *v)
elf_symbol_iterator_range symbols() const
const ELFSymbolRef & operator*() const
The instances of the Type class are immutable: once they are created, they are never changed...
uint32_t getSymbolFlags(DataRefImpl Symb) const override
virtual uint64_t getSectionFlags(DataRefImpl Sec) const =0
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
const Elf_Shdr * DotDynSymSec
static unsigned int getELFType(bool isLE, bool is64Bits)
section_iterator getRelocatedSection(DataRefImpl Sec) const override
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
bool isSectionData(DataRefImpl Sec) const override
void handleAllErrors(Error E, HandlerTs &&...Handlers)
Behaves the same as handleErrors, except that it requires that all errors be handled by the given han...
virtual basic_symbol_iterator symbol_end() const =0
virtual ErrorOr< int64_t > getRelocationAddend(DataRefImpl Rel) const =0
const ELFSectionRef * operator->() const
uint8_t getSymbolELFType(DataRefImpl Symb) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
struct llvm::object::DataRefImpl::@230 d
StringRef getFileFormatName() const override
void consumeError(Error Err)
Consume a Error without doing anything.
const ELFSectionRef & operator*() const
std::error_code getPlatformFlags(unsigned &Result) const override
Returns platform-specific object flags, if any.
const ObjectFile * getObject() 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
const Elf_Shdr * getSection(DataRefImpl Sec) const
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
unsigned getArch() const override
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
Expected< section_iterator > getSymbolSection(const Elf_Sym *Symb, const Elf_Shdr *SymTab) const
const ObjectFile * getObject() const
ErrorOr< int64_t > getAddend() const
uint64_t getSectionFlags(DataRefImpl Sec) const override
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
const ELFRelocationRef & operator*() const
const ELFSymbolRef * operator->() const
ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
content_iterator< BasicSymbolRef > basic_symbol_iterator
A range adaptor for a pair of iterators.
Manages the enabling and disabling of subtarget specific features.
virtual uint64_t getSymbolSize(DataRefImpl Symb) const =0
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
llvm_unreachable("Unknown RegBank!")
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
bool isExportedToOtherDSO(const Elf_Sym *ESym) const
basic_symbol_iterator symbol_end() const override
bool isSectionCompressed(DataRefImpl Sec) const override
ELFSectionRef(const SectionRef &B)
const SymbolicFile * getObject() const
uint64_t getSectionOffset(DataRefImpl Sec) const override
virtual uint32_t getSectionType(DataRefImpl Sec) const =0
typename ELFFile< ELFT >::Elf_Shdr Elf_Shdr
const SymbolRef & operator*() const
section_iterator section_end() const override
Provides ErrorOr<T> smart pointer.
const content_type & operator*() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const Elf_Shdr * getRelSection(DataRefImpl Rel) const
Get the relocation section that contains Rel.
const content_type * operator->() const
virtual elf_symbol_iterator_range getDynamicSymbolIterators() const =0
const ELFObjectFileBase * getObject() const
void Parse(ArrayRef< uint8_t > Section, bool isLittle)
content_iterator< RelocationRef > relocation_iterator
uint64_t getRelocationType(DataRefImpl Rel) const override
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
StringRef - Represent a constant reference to a string, i.e.
virtual uint8_t getSymbolELFType(DataRefImpl Symb) const =0
const ELFObjectFileBase * getObject() const
virtual uint8_t getSymbolOther(DataRefImpl Symb) const =0
uint64_t getOffset() const
elf_symbol_iterator(const basic_symbol_iterator &B)
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
This is a value type class that represents a single section in the list of sections in the object fil...
uint64_t getFlags() const
ELFSymbolRef(const SymbolRef &B)
elf_symbol_iterator dynamic_symbol_end() const
DataRefImpl getRawDataRefImpl() const