14 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H
15 #define LLVM_OBJECT_ELFOBJECTFILE_H
38 class elf_symbol_iterator;
40 class ELFRelocationRef;
90 assert(isa<ELFObjectFileBase>(B->getObject()));
178 uint64_t getSymbolSize(
DataRefImpl Sym)
const override;
240 return *EF.getSection(Rel.d.a);
244 return EF.template getEntry<Elf_Sym>(Sym.
d.
a, Sym.
d.
b);
257 uintptr_t SHT =
reinterpret_cast<uintptr_t
>(
EF.section_begin());
258 unsigned SymTableIndex =
259 (
reinterpret_cast<uintptr_t
>(SymTable) - SHT) /
sizeof(
Elf_Shdr);
261 DRI.
d.
a = SymTableIndex;
267 return reinterpret_cast<const Elf_Shdr *
>(Sec.
p);
272 DRI.
p =
reinterpret_cast<uintptr_t
>(Sec);
278 DRI.
p =
reinterpret_cast<uintptr_t
>(Dyn.
get());
322 unsigned getArch()
const override;
326 Result =
EF.getHeader()->e_flags;
327 return std::error_code();
348 template <
class ELFT>
353 template <
class ELFT>
355 const Elf_Sym *ESym = toELFSymIter(Sym);
356 const Elf_Shdr *SymTableSec = *EF.getSection(Sym.
d.
a);
357 const Elf_Shdr *StringTableSec = *EF.getSection(SymTableSec->sh_link);
358 StringRef SymTable = *EF.getStringTable(StringTableSec);
359 return ESym->
getName(SymTable);
362 template <
class ELFT>
364 return toELFShdrIter(Sec)->sh_flags;
367 template <
class ELFT>
369 return toELFShdrIter(Sec)->sh_type;
372 template <
class ELFT>
374 const Elf_Sym *ESym = getSymbol(Symb);
375 uint64_t
Ret = ESym->st_value;
379 const Elf_Ehdr *Header = EF.getHeader();
388 template <
class ELFT>
392 const Elf_Sym *ESym = getSymbol(Symb);
393 switch (ESym->st_shndx) {
400 const Elf_Ehdr *Header = EF.getHeader();
404 if (std::error_code EC = SectionOrErr.
getError())
408 Result += Section->sh_addr;
414 template <
class ELFT>
416 const Elf_Sym *Sym = toELFSymIter(Symb);
418 return Sym->st_value;
422 template <
class ELFT>
424 return toELFSymIter(Sym)->st_size;
427 template <
class ELFT>
429 return toELFSymIter(Symb)->st_size;
432 template <
class ELFT>
434 return toELFSymIter(Symb)->st_other;
437 template <
class ELFT>
439 return toELFSymIter(Symb)->getType();
442 template <
class ELFT>
444 const Elf_Sym *ESym = getSymbol(Symb);
464 template <
class ELFT>
466 const Elf_Sym *ESym = toELFSymIter(Sym);
480 ESym == EF.symbol_begin() || ESym == EF.dynamic_symbol_begin())
498 if (isExportedToOtherDSO(ESym))
507 template <
class ELFT>
511 if (std::error_code EC = ESecOrErr.
getError())
516 return section_end();
519 Sec.
p =
reinterpret_cast<intptr_t>(ESec);
523 template <
class ELFT>
527 Res = getSymbolSection(getSymbol(Symb));
528 return std::error_code();
531 template <
class ELFT>
533 const Elf_Shdr *ESec = toELFShdrIter(Sec);
537 template <
class ELFT>
544 return std::error_code();
547 template <
class ELFT>
549 return toELFShdrIter(Sec)->sh_addr;
552 template <
class ELFT>
554 return toELFShdrIter(Sec)->sh_size;
557 template <
class ELFT>
561 const Elf_Shdr *EShdr = toELFShdrIter(Sec);
562 Result =
StringRef((
const char *)base() + EShdr->sh_offset, EShdr->sh_size);
563 return std::error_code();
566 template <
class ELFT>
568 return toELFShdrIter(Sec)->sh_addralign;
571 template <
class ELFT>
576 template <
class ELFT>
578 const Elf_Shdr *EShdr = toELFShdrIter(Sec);
583 template <
class ELFT>
585 const Elf_Shdr *EShdr = toELFShdrIter(Sec);
590 template <
class ELFT>
595 template <
class ELFT>
599 uintptr_t SHT =
reinterpret_cast<uintptr_t
>(EF.section_begin());
600 RelData.
d.
a = (Sec.
p - SHT) / EF.getHeader()->e_shentsize;
607 const Elf_Shdr *RelSec = getRelSection(RelData);
609 if (std::error_code EC = SymSecOrErr.
getError())
611 const Elf_Shdr *SymSec = *SymSecOrErr;
612 uint32_t SymSecType = SymSec->sh_type;
621 template <
class ELFT>
629 RelData.
d.
b += (S->sh_size / S->sh_entsize) << 1;
633 template <
class ELFT>
637 return section_end();
639 const Elf_Shdr *EShdr = toELFShdrIter(Sec);
642 return section_end();
645 if (std::error_code EC = R.
getError())
651 template <
class ELFT>
656 template <
class ELFT>
660 const Elf_Shdr *sec = getRelSection(Rel);
662 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
664 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
668 bool IsDyn = Rel.
d.
b & 1;
671 SymbolData = toDRI(EF.getDotDynSymSec(), symbolIdx);
673 SymbolData = toDRI(EF.getDotSymtabSec(), symbolIdx);
677 template <
class ELFT>
680 "Only relocatable object files have relocation offsets");
681 const Elf_Shdr *sec = getRelSection(Rel);
683 return getRel(Rel)->r_offset;
685 return getRela(Rel)->r_offset;
688 template <
class ELFT>
690 const Elf_Shdr *sec = getRelSection(Rel);
692 return getRel(Rel)->getType(EF.isMips64EL());
694 return getRela(Rel)->getType(EF.isMips64EL());
697 template <
class ELFT>
702 template <
class ELFT>
705 uint32_t type = getRelocationType(Rel);
706 EF.getRelocationTypeName(type, Result);
709 template <
class ELFT>
714 return (int64_t)getRela(Rel)->r_addend;
717 template <
class ELFT>
720 return &*toELFSymIter(Symb);
723 template <
class ELFT>
727 return EF.template getEntry<Elf_Rel>(Rel.
d.
a, Rel.
d.
b >> 1);
730 template <
class ELFT>
734 return EF.template getEntry<Elf_Rela>(Rela.
d.
a, Rela.
d.
b >> 1);
737 template <
class ELFT>
740 getELFType(static_cast<
endianness>(ELFT::TargetEndianness) ==
744 EF(
Data.getBuffer(), EC) {}
746 template <
class ELFT>
752 template <
class ELFT>
754 const Elf_Shdr *SymTab = EF.getDotSymtabSec();
756 return symbol_begin_impl();
761 template <
class ELFT>
767 template <
class ELFT>
769 const Elf_Shdr *SymTab = EF.getDotDynSymSec();
774 template <
class ELFT>
779 template <
class ELFT>
784 template <
class ELFT>
793 return EF.getDynamicString(DI->getVal());
797 template <
class ELFT>
799 return ELFT::Is64Bits ? 8 : 4;
802 template <
class ELFT>
807 switch (EF.getHeader()->e_machine) {
811 return "ELF32-x86-64";
813 return (IsLittleEndian ?
"ELF32-arm-little" :
"ELF32-arm-big");
815 return "ELF32-hexagon";
822 return "ELF32-sparc";
824 return "ELF32-unknown";
827 switch (EF.getHeader()->e_machine) {
831 return "ELF64-x86-64";
833 return (IsLittleEndian ?
"ELF64-aarch64-little" :
"ELF64-aarch64-big");
835 return "ELF64-ppc64";
839 return "ELF64-sparc";
843 return "ELF64-unknown";
851 template <
class ELFT>
854 switch (EF.getHeader()->e_machine) {
892 template <
class ELFT>
895 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
unsigned char getType() const
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
section_iterator getSymbolSection(const Elf_Sym *Symb) const
std::error_code getError() const
uint64_t getSectionSize(DataRefImpl Sec) const override
Represents either an error or a value T.
static uint64_t getSymbolValue(const MCSymbol &Symbol, const MCAsmLayout &Layout)
ELFObjectFile< ELFType< support::little, false > > ELF32LEObjectFile
static bool classof(const Binary *v)
DataRefImpl getRawDataRefImpl() const
Iterate over constant sized entities.
bool isSectionBSS(DataRefImpl Sec) const override
const ObjectFile * getObject() const
bool isSectionText(DataRefImpl Sec) const override
elf_relocation_iterator(const relocation_iterator &B)
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).
This class is the base class for all object file types.
const Elf_Shdr * toELFShdrIter(DataRefImpl Sec) const
ELFYAML::ELF_STV Visibility
DataRefImpl getRawDataRefImpl() const
bool isSectionVirtual(DataRefImpl Sec) const override
uint32_t getSectionType(DataRefImpl Sec) const override
DataRefImpl toDRI(const Elf_Shdr *Sec) const
Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
const ELFObjectFileBase * getObject() const
ELFObjectFile< ELFType< support::little, true > > ELF64LEObjectFile
SymbolRef::Type getSymbolType(DataRefImpl Symb) const override
void moveRelocationNext(DataRefImpl &Rel) const override
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const
ELFObjectFile< ELFType< support::big, false > > ELF32BEObjectFile
uint8_t getSymbolOther(DataRefImpl Symb) const override
ErrorOr< int64_t > getRelocationAddend(DataRefImpl Rel) const override
uint8_t getELFType() const
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
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
const Elf_Rela * getRela(DataRefImpl Rela) const
basic_symbol_iterator symbol_begin() const
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
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
elf_symbol_iterator dynamic_symbol_begin() const
const Elf_Rel * getRel(DataRefImpl Rel) const
const ELFFile< ELFT > * getELFFile() const
const SymbolRef * operator->() const
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
content_iterator< SectionRef > section_iterator
DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const
ELFRelocationRef(const RelocationRef &B)
unsigned int getType() const
static bool classof(const Binary *v)
ErrorOr< StringRef > getName(StringRef StrTab) const
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
unsigned char getVisibility() const
Access to the STV_xxx flag stored in the first two bits of st_other.
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
virtual ErrorOr< int64_t > getRelocationAddend(DataRefImpl Rel) const =0
const ELFSectionRef * operator->() const
ErrorOr< StringRef > getSymbolName(DataRefImpl Symb) const override
content_iterator< RelocationRef > relocation_iterator
uint8_t getSymbolELFType(DataRefImpl Symb) const override
StringRef getFileFormatName() const override
const ELFSectionRef & operator*() const
std::error_code getPlatformFlags(unsigned &Result) const override
Returns platform-specific object flags, if any.
const ObjectFile * getObject() const
uint64_t getSectionAddress(DataRefImpl Sec) const override
std::error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
basic_symbol_iterator symbol_end() const
content_iterator< BasicSymbolRef > basic_symbol_iterator
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
StringRef getLoadName() const
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
const ObjectFile * getObject() const
ErrorOr< int64_t > getAddend() const
uint64_t getSectionFlags(DataRefImpl Sec) const override
const ELFRelocationRef & operator*() const
const Elf_Sym * getSymbol(DataRefImpl Symb) const
ELFObjectFile< ELFType< support::big, true > > ELF64BEObjectFile
const ELFSymbolRef * operator->() const
ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
A range adaptor for a pair of iterators.
ErrorOr< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
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
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
bool isExportedToOtherDSO(const Elf_Sym *ESym) const
ELFSectionRef(const SectionRef &B)
struct llvm::object::DataRefImpl::@97 d
const SymbolicFile * getObject() const
basic_symbol_iterator symbol_end_impl() const override
virtual uint32_t getSectionType(DataRefImpl Sec) const =0
basic_symbol_iterator symbol_begin_impl() const override
ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
const SymbolRef & operator*() const
section_iterator section_end() const override
const content_type & operator*() const
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 Elf_Sym * toELFSymIter(DataRefImpl Sym) const
const ELFObjectFileBase * getObject() const
ELFFile< ELFT >::Elf_Shdr Elf_Shdr
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
elf_symbol_iterator(const basic_symbol_iterator &B)
ELFFile< ELFT >::uintX_t uintX_t
unsigned char getBinding() const
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