14 #ifndef LLVM_OBJECT_ELF_OBJECT_FILE_H
15 #define LLVM_OBJECT_ELF_OBJECT_FILE_H
111 return EF.getSection(Rel.d.a);
118 bool IsDynamic = Symb.
p & 1;
121 EF.begin_dynamic_symbols().getEntSize(),
122 reinterpret_cast<const char *
>(Symb.
p & ~uintptr_t(1)), IsDynamic);
124 reinterpret_cast<const char *
>(Symb.
p), IsDynamic);
129 DRI.
p =
reinterpret_cast<uintptr_t
>(Symb.
get()) |
130 static_cast<uintptr_t>(Symb.
isDynamic());
136 reinterpret_cast<const char *
>(Sec.
p));
141 DRI.
p =
reinterpret_cast<uintptr_t
>(Sec.
get());
147 DRI.
p =
reinterpret_cast<uintptr_t
>(Sec);
153 reinterpret_cast<const char *
>(Dyn.
p));
158 DRI.
p =
reinterpret_cast<uintptr_t
>(Dyn.
get());
186 bool &IsDefault)
const;
191 virtual unsigned getArch()
const;
210 template <
class ELFT>
213 Result =
SymbolRef(toDRI(++toELFSymIter(Symb)),
this);
217 template <
class ELFT>
227 template <
class ELFT>
230 bool &IsDefault)
const {
232 const Elf_Sym *symb = getSymbol(Symb);
234 EF.getSymbolVersion(EF.getSection(Symb.
d.
b), symb, IsDefault);
241 template <
class ELFT>
243 uint64_t &Result)
const {
244 const Elf_Sym *ESym = getSymbol(Symb);
246 switch (EF.getSymbolTableIndex(ESym)) {
253 Result = ESym->st_value;
256 ESec = EF.getSection(ESym);
266 Result = ESym->st_value + (ESec ? ESec->sh_offset : 0);
274 template <
class ELFT>
276 uint64_t &Result)
const {
277 const Elf_Sym *ESym = getSymbol(Symb);
279 switch (EF.getSymbolTableIndex(ESym)) {
285 Result = ESym->st_value;
288 ESec = EF.getSection(ESym);
299 switch (EF.getHeader()->e_type) {
302 IsRelocatable =
false;
305 IsRelocatable =
true;
307 Result = ESym->st_value;
313 if (IsRelocatable && ESec != 0)
314 Result += ESec->sh_addr;
322 template <
class ELFT>
324 uint32_t &Res)
const {
333 template <
class ELFT>
335 uint64_t &Result)
const {
336 Result = toELFSymIter(Symb)->st_size;
340 template <
class ELFT>
343 const Elf_Sym *ESym = getSymbol(Symb);
370 template <
class ELFT>
372 uint32_t &Result)
const {
373 const Elf_Sym *ESym = getSymbol(Symb);
375 Result = SymbolRef::SF_None;
378 Result |= SymbolRef::SF_Global;
381 Result |= SymbolRef::SF_Weak;
384 Result |= SymbolRef::SF_Absolute;
387 ESym == &*EF.begin_symbols())
388 Result |= SymbolRef::SF_FormatSpecific;
391 Result |= SymbolRef::SF_Undefined;
395 Result |= SymbolRef::SF_Common;
398 Result |= SymbolRef::SF_ThreadLocal;
403 template <
class ELFT>
406 const Elf_Sym *ESym = getSymbol(Symb);
407 const Elf_Shdr *ESec = EF.getSection(ESym);
409 Res = end_sections();
412 Sec.
p =
reinterpret_cast<intptr_t>(ESec);
418 template <
class ELFT>
420 uint64_t &Val)
const {
421 const Elf_Sym *ESym = getSymbol(Symb);
422 Val = ESym->st_value;
426 template <
class ELFT>
429 Result =
SectionRef(toDRI(++toELFShdrIter(Sec)),
this);
433 template <
class ELFT>
443 template <
class ELFT>
445 uint64_t &Result)
const {
446 Result = toELFShdrIter(Sec)->sh_addr;
450 template <
class ELFT>
452 uint64_t &Result)
const {
453 Result = toELFShdrIter(Sec)->sh_size;
457 template <
class ELFT>
461 Result =
StringRef((
const char *)base() + EShdr->sh_offset, EShdr->sh_size);
465 template <
class ELFT>
467 uint64_t &Result)
const {
468 Result = toELFShdrIter(Sec)->sh_addralign;
472 template <
class ELFT>
474 bool &Result)
const {
475 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
479 template <
class ELFT>
481 bool &Result)
const {
483 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
484 EShdr->sh_type == ELF::SHT_PROGBITS;
488 template <
class ELFT>
490 bool &Result)
const {
492 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
493 EShdr->sh_type == ELF::SHT_NOBITS;
497 template <
class ELFT>
500 bool &Result)
const {
501 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
505 template <
class ELFT>
507 bool &Result)
const {
508 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
512 template <
class ELFT>
514 bool &Result)
const {
515 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
519 template <
class ELFT>
521 bool &Result)
const {
523 Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
527 template <
class ELFT>
530 bool &Result)
const {
533 uintX_t Index = ESym->st_shndx;
536 Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
540 template <
class ELFT>
544 uintptr_t SHT =
reinterpret_cast<uintptr_t
>(EF.begin_sections().get());
545 RelData.
d.
a = (Sec.
p - SHT) / EF.getHeader()->e_shentsize;
550 template <
class ELFT>
554 uintptr_t SHT =
reinterpret_cast<uintptr_t
>(EF.begin_sections().get());
556 RelData.
d.
a = (Sec.
p - SHT) / EF.getHeader()->e_shentsize;
557 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
560 RelData.
d.
b = S->sh_size / S->sh_entsize;
565 template <
class ELFT>
569 return end_sections();
573 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
574 return end_sections();
576 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
581 template <
class ELFT>
589 template <
class ELFT>
593 const Elf_Shdr *sec = getRelSection(Rel);
594 switch (sec->sh_type) {
598 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
601 case ELF::SHT_RELA: {
602 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
607 return end_symbols();
609 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
612 switch (SymSec->sh_type) {
615 case ELF::SHT_SYMTAB:
616 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
618 case ELF::SHT_DYNSYM:
619 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
626 template <
class ELFT>
628 uint64_t &Result)
const {
629 Result = getROffset(Rel);
633 template <
class ELFT>
635 uint64_t &Result)
const {
636 Result = getROffset(Rel);
640 template <
class ELFT>
642 const Elf_Shdr *sec = getRelSection(Rel);
643 switch (sec->sh_type) {
647 return getRel(Rel)->r_offset;
649 return getRela(Rel)->r_offset;
653 template <
class ELFT>
655 uint64_t &Result)
const {
656 const Elf_Shdr *sec = getRelSection(Rel);
657 switch (sec->sh_type) {
661 Result = getRel(Rel)->getType(EF.isMips64EL());
664 case ELF::SHT_RELA: {
665 Result = getRela(Rel)->getType(EF.isMips64EL());
672 template <
class ELFT>
677 template <
class ELFT>
680 const Elf_Shdr *sec = getRelSection(Rel);
682 switch (sec->sh_type) {
686 type = getRel(Rel)->getType(EF.isMips64EL());
689 case ELF::SHT_RELA: {
690 type = getRela(Rel)->getType(EF.isMips64EL());
695 EF.getRelocationTypeName(type, Result);
699 template <
class ELFT>
701 int64_t &Result)
const {
702 const Elf_Shdr *sec = getRelSection(Rel);
703 switch (sec->sh_type) {
710 case ELF::SHT_RELA: {
711 Result = getRela(Rel)->r_addend;
717 template <
class ELFT>
720 const Elf_Shdr *sec = getRelSection(Rel);
724 uint16_t symbol_index = 0;
725 switch (sec->sh_type) {
729 type = getRel(Rel)->getType(EF.isMips64EL());
730 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
734 case ELF::SHT_RELA: {
735 type = getRela(Rel)->getType(EF.isMips64EL());
736 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
737 addend = getRela(Rel)->r_addend;
742 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
744 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
747 switch (EF.getHeader()->e_machine) {
755 fmt << *SymName << (addend < 0 ?
"" :
"+") << addend <<
"-P";
757 Result.
append(fmtbuf.begin(), fmtbuf.end());
766 fmt << *SymName << (addend < 0 ?
"" :
"+") << addend;
768 Result.
append(fmtbuf.begin(), fmtbuf.end());
779 fmt << (addend < 0 ?
"" :
"+") << addend;
781 Result.
append(fmtbuf.begin(), fmtbuf.end());
796 template <
class ELFT>
799 return &*toELFSymIter(Symb);
802 template <
class ELFT>
805 return EF.template getEntry<Elf_Rel>(Rel.
d.
a, Rel.
d.
b);
808 template <
class ELFT>
811 return EF.template getEntry<Elf_Rela>(Rela.
d.
a, Rela.
d.
b);
814 template <
class ELFT>
822 template <
class ELFT>
827 template <
class ELFT>
832 template <
class ELFT>
837 template <
class ELFT>
842 template <
class ELFT>
847 template <
class ELFT>
852 template <
class ELFT>
861 return EF.getDynamicString(DI->getVal());
865 template <
class ELFT>
876 template <
class ELFT>
891 template <
class ELFT>
894 Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
898 template <
class ELFT>
903 template <
class ELFT>
905 return ELFT::Is64Bits ? 8 : 4;
908 template <
class ELFT>
912 switch (EF.getHeader()->e_machine) {
916 return "ELF32-x86-64";
920 return "ELF32-hexagon";
926 return "ELF32-unknown";
929 switch (EF.getHeader()->e_machine) {
933 return "ELF64-x86-64";
935 return "ELF64-aarch64";
937 return "ELF64-ppc64";
941 return "ELF64-unknown";
949 template <
class ELFT>
951 switch (EF.getHeader()->e_machine) {
983 return ELFObj->getRelocationAddend(DRI, Addend);
987 return ELFObj->getRelocationAddend(DRI, Addend);
991 return ELFObj->getRelocationAddend(DRI, Addend);
995 return ELFObj->getRelocationAddend(DRI, Addend);
1008 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1012 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1016 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1020 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
unsigned char getType() const
COFF::RelocationTypeX86 Type
Represents either an error or a value T.
DataRefImpl getRawDataRefImpl() const
virtual symbol_iterator end_dynamic_symbols() const
virtual library_iterator begin_libraries_needed() const
static bool classof(const Binary *v)
virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const
Iterate over constant sized entities.
ELFFile< ELFT >::Elf_Rela Elf_Rela
Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const
virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const
virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const
virtual symbol_iterator begin_symbols() const
virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const
ELFFile< ELFT >::Elf_Dyn_Iter Elf_Dyn_Iter
DataRefImpl toDRI(const Elf_Shdr *Sec) const
Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const
DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const
virtual symbol_iterator begin_dynamic_symbols() const
#define llvm_unreachable(msg)
virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, bool &Result) const
virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const
error_code getSymbolVersion(SymbolRef Symb, StringRef &Version, bool &IsDefault) const
virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const
virtual StringRef getObjectType() const
virtual error_code getRelocationValueString(DataRefImpl Rel, SmallVectorImpl< char > &Result) const
virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const
virtual symbol_iterator end_symbols() const
const Elf_Rela * getRela(DataRefImpl Rela) const
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
const Elf_Rel * getRel(DataRefImpl Rel) const
const ELFFile< ELFT > * getELFFile() const
ELFObjectFile< ELFType< support::little, 2, true > > ELF64LEObjectFile
virtual section_iterator getRelocatedSection(DataRefImpl Sec) const
virtual section_iterator begin_sections() const
content_iterator< SectionRef > section_iterator
unsigned int getType() const
virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const
static error_code getELFRelocationAddend(const RelocationRef R, int64_t &Addend)
virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const
virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const
static error_code GetELFSymbolVersion(const ObjectFile *Obj, const SymbolRef &Sym, StringRef &Version, bool &IsDefault)
content_iterator< SymbolRef > symbol_iterator
virtual uint8_t getBytesInAddress() const
The number of bytes used to represent an address in this object file format.
virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const
virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const
static unsigned int getELFType(bool isLE, bool is64Bits)
virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const
virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const
virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const
content_iterator< LibraryRef > library_iterator
virtual error_code isSectionRequiredForExecution(DataRefImpl Sec, bool &Res) const
void append(in_iter in_start, in_iter in_end)
content_iterator< RelocationRef > relocation_iterator
virtual section_iterator end_sections() const
ELFObjectFile< ELFType< support::big, 2, true > > ELF64BEObjectFile
virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const
Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const
virtual StringRef getFileFormatName() const
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
virtual StringRef getLoadName() const
uint64_t getROffset(DataRefImpl Rel) const
ELFObjectFile(MemoryBuffer *Object, error_code &ec)
virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const
virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const
virtual relocation_iterator section_rel_end(DataRefImpl Sec) const
const Elf_Sym * getSymbol(DataRefImpl Symb) const
const uint64_t UnknownAddressOrSize
virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const
struct llvm::object::DataRefImpl::@77 d
virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const
virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const
virtual error_code getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const
ELFFile< ELFT >::Elf_Shdr_Iter Elf_Shdr_Iter
Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const
virtual error_code getRelocationNext(DataRefImpl Rel, RelocationRef &Res) const
virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const
ELFFile< ELFT >::Elf_Sym_Iter Elf_Sym_Iter
const Elf_Shdr * getRelSection(DataRefImpl Rel) const
Get the relocation section that contains Rel.
virtual unsigned getArch() const
ELFObjectFile< ELFType< support::big, 2, false > > ELF32BEObjectFile
const ObjectFile * getObjectFile() const
virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const
DataRefImpl toDRI(Elf_Shdr_Iter Sec) const
virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const
virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const
virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const
virtual library_iterator end_libraries_needed() const
virtual error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const
error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const
ELFFile< ELFT >::uintX_t uintX_t
ELFFile< ELFT >::Elf_Rel Elf_Rel
unsigned char getBinding() const
ELFObjectFile< ELFType< support::little, 2, false > > ELF32LEObjectFile
DataRefImpl toDRI(Elf_Sym_Iter Symb) const
DataRefImpl getRawDataRefImpl() const