13 #ifndef LLVM_OBJECT_ELF_H 14 #define LLVM_OBJECT_ELF_H 38 inline std::pair<unsigned char, unsigned char>
55 const typename ELFT::Shdr *Sec) {
58 return "[index " +
std::to_string(Sec - &TableOrErr->front()) +
"]";
64 return "[unknown index]";
123 template <
typename T>
125 template <
typename T>
172 return makeArrayRef<Elf_Sym>(
nullptr,
nullptr);
173 return getSectionContentsAsArray<Elf_Sym>(Sec);
177 return getSectionContentsAsArray<Elf_Rela>(Sec);
181 return getSectionContentsAsArray<Elf_Rel>(Sec);
185 return getSectionContentsAsArray<Elf_Relr>(Sec);
200 return createError(
"program headers are longer than binary of size " +
220 if (Phdr.p_offset + Phdr.p_filesz >
getBufSize()) {
221 Err =
createError(
"PT_NOTE header has invalid offset (0x" +
239 if (Shdr.sh_offset + Shdr.sh_size >
getBufSize()) {
241 " has invalid offset (0x" +
299 template <
typename T>
309 template <
class ELFT>
312 if (Index >= Sections.size())
314 return &Sections[
Index];
317 template <
class ELFT>
320 const typename ELFT::Sym *FirstSym,
323 unsigned Index = Sym - FirstSym;
324 if (Index >= ShndxTable.
size())
326 "extended symbol index (" +
Twine(Index) +
327 ") is past the end of the SHT_SYMTAB_SHNDX section of size " +
331 return ShndxTable[
Index];
334 template <
class ELFT>
340 auto ErrorOrIndex = getExtendedSymbolTableIndex<ELFT>(
341 Sym, Syms.begin(), ShndxTable);
343 return ErrorOrIndex.takeError();
344 return *ErrorOrIndex;
351 template <
class ELFT>
355 auto SymsOrErr =
symbols(SymTab);
357 return SymsOrErr.takeError();
358 return getSection(Sym, *SymsOrErr, ShndxTable);
361 template <
class ELFT>
367 return IndexOrErr.takeError();
374 template <
class ELFT>
379 return SymsOrErr.takeError();
382 if (Index >= Symbols.size())
383 return createError(
"unable to get symbol from section " +
385 ": invalid symbol index (" +
Twine(Index) +
")");
386 return &Symbols[
Index];
389 template <
class ELFT>
390 template <
typename T>
393 if (Sec->sh_entsize !=
sizeof(
T) &&
sizeof(
T) != 1)
395 " has an invalid sh_entsize: " +
Twine(Sec->sh_entsize));
400 if (Size %
sizeof(
T))
402 " has an invalid sh_size (" +
Twine(Size) +
403 ") which is not a multiple of its sh_entsize (" +
404 Twine(Sec->sh_entsize) +
")");
409 ") that cannot be represented");
410 if (Offset + Size > Buf.
size())
414 ") that is greater than the file size (0x" +
417 if (Offset %
alignof(
T))
421 const T *Start =
reinterpret_cast<const T *
>(
base() +
Offset);
425 template <
class ELFT>
428 return getSectionContentsAsArray<uint8_t>(Sec);
431 template <
class ELFT>
436 template <
class ELFT>
449 uint8_t Type1 = (Type >> 0) & 0xFF;
450 uint8_t
Type2 = (Type >> 8) & 0xFF;
451 uint8_t
Type3 = (Type >> 16) & 0xFF;
467 template <
class ELFT>
472 template <
class ELFT>
479 return getEntry<Elf_Sym>(SymTab,
Index);
482 template <
class ELFT>
488 Index = Sections[0].sh_link;
492 if (Index >= Sections.size())
500 template <
class ELFT>
504 ") is smaller than an ELF header (" +
509 template <
class ELFT>
512 if (SectionTableOffset == 0)
516 return createError(
"invalid e_shentsize in ELF header: " +
519 const uint64_t FileSize = Buf.
size();
520 if (SectionTableOffset +
sizeof(
Elf_Shdr) > FileSize ||
523 "section header table goes past the end of the file: e_shoff = 0x" +
527 if (SectionTableOffset & (
alignof(
Elf_Shdr) - 1))
529 return createError(
"invalid alignment of section headers");
532 reinterpret_cast<const Elf_Shdr *
>(
base() + SectionTableOffset);
535 if (NumSections == 0)
536 NumSections = First->sh_size;
539 return createError(
"invalid number of sections specified in the NULL " 540 "section's sh_size field (" +
541 Twine(NumSections) +
")");
543 const uint64_t SectionTableSize = NumSections *
sizeof(
Elf_Shdr);
544 if (SectionTableOffset + SectionTableSize < SectionTableOffset)
546 "invalid section header table offset (e_shoff = 0x" +
548 ") or invalid number of sections specified in the first section " 549 "header's sh_size field (0x" +
553 if (SectionTableOffset + SectionTableSize > FileSize)
554 return createError(
"section table goes past the end of file");
558 template <
class ELFT>
559 template <
typename T>
564 return SecOrErr.takeError();
565 return getEntry<T>(*SecOrErr, Entry);
568 template <
class ELFT>
569 template <
typename T>
572 if (
sizeof(
T) != Section->sh_entsize)
574 " has invalid sh_entsize: expected " +
Twine(
sizeof(
T)) +
575 ", but got " +
Twine(Section->sh_entsize));
576 size_t Pos = Section->sh_offset + Entry *
sizeof(
T);
577 if (Pos +
sizeof(
T) > Buf.
size())
581 ": offset goes past the end of file");
582 return reinterpret_cast<const T *
>(
base() + Pos);
585 template <
class ELFT>
590 return TableOrErr.takeError();
591 return object::getSection<ELFT>(*TableOrErr,
Index);
594 template <
class ELFT>
599 if (
Error E = WarnHandler(
"invalid sh_type for string table section " +
601 ": expected SHT_STRTAB, but got " +
603 getHeader()->e_machine, Section->sh_type)))
606 auto V = getSectionContentsAsArray<char>(
Section);
608 return V.takeError();
611 return createError(
"SHT_STRTAB string table section " +
613 if (Data.
back() !=
'\0')
614 return createError(
"SHT_STRTAB string table section " +
616 " is non-null terminated");
620 template <
class ELFT>
625 return SectionsOrErr.takeError();
629 template <
class ELFT>
634 auto VOrErr = getSectionContentsAsArray<Elf_Word>(&
Section);
636 return VOrErr.takeError();
638 auto SymTableOrErr = object::getSection<ELFT>(Sections, Section.sh_link);
640 return SymTableOrErr.takeError();
641 const Elf_Shdr &SymTable = **SymTableOrErr;
644 return createError(
"SHT_SYMTAB_SHNDX section is linked with " +
647 " section (expected SHT_SYMTAB/SHT_DYNSYM)");
649 uint64_t Syms = SymTable.sh_size /
sizeof(
Elf_Sym);
650 if (V.
size() != Syms)
652 " entries, but the symbol table associated has " +
658 template <
class ELFT>
663 return SectionsOrErr.takeError();
667 template <
class ELFT>
675 "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM");
676 auto SectionOrErr = object::getSection<ELFT>(Sections, Sec.sh_link);
678 return SectionOrErr.takeError();
682 template <
class ELFT>
688 return SectionsOrErr.takeError();
691 return Table.takeError();
695 template <
class ELFT>
701 if (Offset >= DotShstrtab.
size())
703 " has an invalid sh_name (0x" +
705 ") offset which goes past the end of the " 706 "section name string table");
715 for (
char C : SymbolName) {
728 #endif // LLVM_OBJECT_ELF_H typename ELFT::Dyn Elf_Dyn
const T & back() const
back - Get the last element.
typename ELFT::Rel Elf_Rel
iterator_range< Elf_Note_Iterator > notes(const Elf_Phdr &Phdr, Error &Err) const
Get an iterator range over notes of a program header.
Expected< const T * > getEntry(uint32_t Section, uint32_t Entry) const
This class represents lattice values for constants.
typename ELFT::Note Elf_Note
static Expected< ELFFile > create(StringRef Object)
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Expected< StringRef > getStringTable(const Elf_Shdr *Section, WarningHandler WarnHandler=&defaultWarningHandler) const
typename ELFT::Vernaux Elf_Vernaux
StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
StringRef getRelocationTypeName(uint32_t Type) const
An efficient, type-erasing, non-owning reference to a callable.
Expected< std::vector< Elf_Rela > > decode_relrs(Elf_Relr_Range relrs) const
unsigned hashSysV(StringRef SymbolName)
This function returns the hash value for a symbol in the .dynsym section Name of the API remains cons...
typename ELFT::Sym Elf_Sym
Expected< Elf_Phdr_Range > program_headers() const
Iterate over program header table.
Expected< const uint8_t * > toMappedAddr(uint64_t VAddr) const
const uint8_t * base() const
Expected< const Elf_Shdr * > getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab, ArrayRef< Elf_Word > ShndxTable) const
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
iterator_range< Elf_Note_Iterator > notes(const Elf_Shdr &Shdr, Error &Err) const
Get an iterator range over notes of a section.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Expected< Elf_Shdr_Range > sections() const
typename ELFT::PhdrRange Elf_Phdr_Range
typename ELFT::Phdr Elf_Phdr
Tagged union holding either a T or a Error.
uint32_t getELFRelativeRelocationType(uint32_t Machine)
Expected< ArrayRef< uint8_t > > getSectionContents(const Elf_Shdr *Sec) const
static Error createError(const Twine &Err)
typename ELFT::Relr Elf_Relr
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
LLVM_NODISCARD size_t size() const
size - Get the string size.
typename ELFT::GnuHash Elf_GnuHash
static Error defaultWarningHandler(const Twine &Msg)
COFF::MachineTypes Machine
typename ELFT::SymRange Elf_Sym_Range
Elf_Note_Iterator notes_end() const
Get the end iterator for notes.
Expected< ArrayRef< T > > getSectionContentsAsArray(const Elf_Shdr *Sec) const
Expected< Elf_Rel_Range > rels(const Elf_Shdr *Sec) const
Expected< ArrayRef< Elf_Word > > getSHNDXTable(const Elf_Shdr &Section) const
The instances of the Type class are immutable: once they are created, they are never changed...
Elf_Note_Iterator notes_begin(const Elf_Shdr &Shdr, Error &Err) const
Get an iterator over notes in a section.
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Expected< StringRef > getStringTableForSymtab(const Elf_Shdr &Section) const
Expected< Elf_Rela_Range > relas(const Elf_Shdr *Sec) const
typename ELFT::uint uintX_t
Expected< uint32_t > getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms, ArrayRef< Elf_Word > ShndxTable) const
Expected< Elf_Relr_Range > relrs(const Elf_Shdr *Sec) const
typename ELFT::NoteIterator Elf_Note_Iterator
std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const
typename ELFT::Nhdr Elf_Nhdr
Expected< StringRef > getSectionName(const Elf_Shdr *Section, WarningHandler WarnHandler=&defaultWarningHandler) const
typename ELFT::Verdaux Elf_Verdaux
typename ELFT::Ehdr Elf_Ehdr
typename ELFT::RelrRange Elf_Relr_Range
typename ELFT::Rela Elf_Rela
void consumeError(Error Err)
Consume a Error without doing anything.
typename ELFT::Verneed Elf_Verneed
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Expected< uint32_t > getExtendedSymbolTableIndex(const typename ELFT::Sym *Sym, const typename ELFT::Sym *FirstSym, ArrayRef< typename ELFT::Word > ShndxTable)
Align max(MaybeAlign Lhs, Align Rhs)
size_t getBufSize() const
Expected< StringRef > getSectionStringTable(Elf_Shdr_Range Sections, WarningHandler WarnHandler=&defaultWarningHandler) const
static Twine utohexstr(const uint64_t &Val)
Expected< const Elf_Sym * > getRelocationSymbol(const Elf_Rel *Rel, const Elf_Shdr *SymTab) const
Get the symbol for a given relocation.
A range adaptor for a pair of iterators.
Helper for Errors used as out-parameters.
Elf_Note_Iterator notes_begin(const Elf_Phdr &Phdr, Error &Err) const
Get an iterator over notes in a program header.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
typename ELFT::DynRange Elf_Dyn_Range
Expected< std::vector< Elf_Rela > > android_relas(const Elf_Shdr *Sec) const
uint32_t getRelativeRelocationType() const
const unsigned char * bytes_begin() const
Expected< const Elf_Sym * > getSymbol(const Elf_Shdr *Sec, uint32_t Index) const
Expected< Elf_Sym_Range > symbols(const Elf_Shdr *Sec) const
const std::string to_string(const T &Value)
typename ELFT::Verdef Elf_Verdef
Expected< Elf_Dyn_Range > dynamicEntries() const
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
typename ELFT::RelRange Elf_Rel_Range
typename ELFT::Versym Elf_Versym
typename ELFT::RelaRange Elf_Rela_Range
Lightweight error class with error context and mandatory checking.
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
StringRef - Represent a constant reference to a string, i.e.
typename ELFT::ShdrRange Elf_Shdr_Range
const Elf_Ehdr * getHeader() const
std::string getSecIndexForError(const ELFFile< ELFT > *Obj, const typename ELFT::Shdr *Sec)
typename ELFT::Shdr Elf_Shdr
bool empty() const
empty - Check if the array is empty.
typename ELFT::Hash Elf_Hash