14 #ifndef LLVM_OBJECT_ELF_H
15 #define LLVM_OBJECT_ELF_H
38 inline std::pair<unsigned char, unsigned char>
78 return reinterpret_cast<const uint8_t *
>(Buf.
data());
131 return makeArrayRef<Elf_Sym>(
nullptr,
nullptr);
132 return getSectionContentsAsArray<Elf_Sym>(Sec);
136 return getSectionContentsAsArray<Elf_Rela>(Sec);
140 return getSectionContentsAsArray<Elf_Rel>(Sec);
169 template <
typename T>
179 template <
class ELFT>
182 if (Index >= Sections.size())
184 return &Sections[Index];
187 template <
class ELFT>
190 const typename ELFT::Sym *FirstSym,
193 unsigned Index = Sym - FirstSym;
194 if (Index >= ShndxTable.
size())
195 return createError(
"index past the end of the symbol table");
198 return ShndxTable[Index];
201 template <
class ELFT>
207 auto ErrorOrIndex = getExtendedSymbolTableIndex<ELFT>(
208 Sym, Syms.begin(), ShndxTable);
210 return ErrorOrIndex.takeError();
211 return *ErrorOrIndex;
218 template <
class ELFT>
222 auto SymsOrErr = symbols(SymTab);
224 return SymsOrErr.takeError();
225 return getSection(Sym, *SymsOrErr, ShndxTable);
228 template <
class ELFT>
232 auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable);
234 return IndexOrErr.takeError();
241 template <
class ELFT>
244 if (Index >= Symbols.size())
246 return &Symbols[Index];
249 template <
class ELFT>
252 auto SymtabOrErr = symbols(Sec);
254 return SymtabOrErr.takeError();
255 return object::getSymbol<ELFT>(*SymtabOrErr, Index);
258 template <
class ELFT>
259 template <
typename T>
262 if (Sec->sh_entsize !=
sizeof(
T) &&
sizeof(
T) != 1)
268 if (Size %
sizeof(
T))
269 return createError(
"size is not a multiple of sh_entsize");
271 Offset + Size > Buf.size())
274 const T *Start =
reinterpret_cast<const T *
>(base() +
Offset);
278 template <
class ELFT>
281 return getSectionContentsAsArray<uint8_t>(Sec);
284 template <
class ELFT>
289 template <
class ELFT>
292 if (!isMipsELF64()) {
302 uint8_t Type1 = (Type >> 0) & 0xFF;
303 uint8_t
Type2 = (Type >> 8) & 0xFF;
304 uint8_t
Type3 = (Type >> 16) & 0xFF;
310 Name = getRelocationTypeName(Type2);
314 Name = getRelocationTypeName(Type3);
320 template <
class ELFT>
324 uint32_t Index = Rel->getSymbol(isMips64EL());
327 return getEntry<Elf_Sym>(SymTab, Index);
330 template <
class ELFT>
333 uint32_t Index = getHeader()->e_shstrndx;
335 Index = Sections[0].sh_link;
339 if (Index >= Sections.size())
341 return getStringTable(&Sections[Index]);
344 template <
class ELFT>
349 template <
class ELFT>
351 return VAddr < Phdr->p_vaddr;
354 template <
class ELFT>
356 const uintX_t SectionTableOffset = getHeader()->e_shoff;
357 if (SectionTableOffset == 0)
360 if (getHeader()->e_shentsize !=
sizeof(
Elf_Shdr))
362 "invalid section header entry size (e_shentsize) in ELF header");
364 const uint64_t FileSize = Buf.size();
366 if (SectionTableOffset +
sizeof(
Elf_Shdr) > FileSize)
367 return createError(
"section header table goes past the end of the file");
370 if (SectionTableOffset & (
alignof(
Elf_Shdr) - 1))
371 return createError(
"invalid alignment of section headers");
374 reinterpret_cast<const Elf_Shdr *
>(base() + SectionTableOffset);
376 uintX_t NumSections = getHeader()->e_shnum;
377 if (NumSections == 0)
378 NumSections = First->sh_size;
380 if (NumSections > UINT64_MAX /
sizeof(
Elf_Shdr))
381 return createError(
"section table goes past the end of file");
383 const uint64_t SectionTableSize = NumSections *
sizeof(
Elf_Shdr);
386 if (SectionTableOffset + SectionTableSize > FileSize)
387 return createError(
"section table goes past the end of file");
392 template <
class ELFT>
393 template <
typename T>
398 return SecOrErr.takeError();
399 return getEntry<T>(*SecOrErr, Entry);
402 template <
class ELFT>
403 template <
typename T>
406 if (
sizeof(
T) != Section->sh_entsize)
408 size_t Pos = Section->sh_offset + Entry *
sizeof(
T);
409 if (Pos +
sizeof(
T) > Buf.size())
411 return reinterpret_cast<const T *
>(base() + Pos);
414 template <
class ELFT>
417 auto TableOrErr = sections();
419 return TableOrErr.takeError();
420 return object::getSection<ELFT>(*TableOrErr, Index);
423 template <
class ELFT>
427 return createError(
"invalid sh_type for string table, expected SHT_STRTAB");
428 auto V = getSectionContentsAsArray<char>(
Section);
430 return V.takeError();
434 if (Data.
back() !=
'\0')
435 return createError(
"string table non-null terminated");
439 template <
class ELFT>
442 auto SectionsOrErr = sections();
444 return SectionsOrErr.takeError();
445 return getSHNDXTable(Section, *SectionsOrErr);
448 template <
class ELFT>
453 auto VOrErr = getSectionContentsAsArray<Elf_Word>(&
Section);
455 return VOrErr.takeError();
457 auto SymTableOrErr = object::getSection<ELFT>(Sections, Section.sh_link);
459 return SymTableOrErr.takeError();
460 const Elf_Shdr &SymTable = **SymTableOrErr;
464 if (V.
size() != (SymTable.sh_size /
sizeof(
Elf_Sym)))
465 return createError(
"invalid section contents size");
469 template <
class ELFT>
472 auto SectionsOrErr = sections();
474 return SectionsOrErr.takeError();
475 return getStringTableForSymtab(Sec, *SectionsOrErr);
478 template <
class ELFT>
485 "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM");
486 auto SectionOrErr = object::getSection<ELFT>(Sections, Sec.sh_link);
488 return SectionOrErr.takeError();
489 return getStringTable(*SectionOrErr);
492 template <
class ELFT>
495 auto SectionsOrErr = sections();
497 return SectionsOrErr.takeError();
498 auto Table = getSectionStringTable(*SectionsOrErr);
500 return Table.takeError();
501 return getSectionName(Section, *Table);
504 template <
class ELFT>
510 if (Offset >= DotShstrtab.
size())
520 for (
char C : SymbolName) {
533 #endif // LLVM_OBJECT_ELF_H
typename ELFT::Dyn Elf_Dyn
StringRef getRelocationTypeName(uint32_t Type) const
Expected< StringRef > getSectionName(const Elf_Shdr *Section) const
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
typename ELFT::Rel Elf_Rel
size_t getBufSize() const
typename ELFT::Vernaux Elf_Vernaux
StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
const uint8_t * base() const
static Error createError(StringRef Err)
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< StringRef > getSectionStringTable(Elf_Shdr_Range Sections) const
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
typename ELFT::PhdrRange Elf_Phdr_Range
typename ELFT::Phdr Elf_Phdr
Tagged union holding either a T or a Error.
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Expected< const Elf_Sym * > getSymbol(const Elf_Shdr *Sec, uint32_t Index) const
Expected< const Elf_Sym * > getRelocationSymbol(const Elf_Rel *Rel, const Elf_Shdr *SymTab) const
Get the symbol for a given relocation.
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
typename ELFT::GnuHash Elf_GnuHash
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
size_t size() const
size - Get the array size.
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
typename ELFT::SymRange Elf_Sym_Range
Expected< uint32_t > getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms, ArrayRef< Elf_Word > ShndxTable) const
The instances of the Type class are immutable: once they are created, they are never changed...
Expected< const T * > getEntry(uint32_t Section, uint32_t Entry) const
typename ELFT::uint uintX_t
Expected< ArrayRef< Elf_Word > > getSHNDXTable(const Elf_Shdr &Section) const
Expected< StringRef > getStringTable(const Elf_Shdr *Section) const
typename ELFT::Verdaux Elf_Verdaux
typename ELFT::Ehdr Elf_Ehdr
bool empty() const
empty - Check if the array is empty.
typename ELFT::Rela Elf_Rela
Expected< Elf_Sym_Range > symbols(const Elf_Shdr *Sec) const
Expected< ArrayRef< T > > getSectionContentsAsArray(const Elf_Shdr *Sec) const
const T & back() const
back - Get the last element.
typename ELFT::Verneed Elf_Verneed
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)
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
ELFFile(StringRef Object)
typename ELFT::DynRange Elf_Dyn_Range
Expected< const Elf_Shdr * > getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab, ArrayRef< Elf_Word > ShndxTable) const
void VerifyStrTab(const Elf_Shdr *sh) const
Expected< Elf_Shdr_Range > sections() const
typename ELFT::Verdef Elf_Verdef
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
typename ELFT::RelRange Elf_Rel_Range
Expected< ArrayRef< uint8_t > > getSectionContents(const Elf_Shdr *Sec) const
bool compareAddr(uint64_t VAddr, const Elf_Phdr_Impl< ELFT > *Phdr)
Expected< Elf_Phdr_Range > program_headers() const
Iterate over program header table.
Expected< Elf_Rela_Range > relas(const Elf_Shdr *Sec) const
typename ELFT::Versym Elf_Versym
typename ELFT::RelaRange Elf_Rela_Range
Lightweight error class with error context and mandatory checking.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
std::string Hash(const Unit &U)
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
StringRef - Represent a constant reference to a string, i.e.
typename ELFT::ShdrRange Elf_Shdr_Range
Expected< StringRef > getStringTableForSymtab(const Elf_Shdr &Section) const
const Elf_Ehdr * getHeader() const
Expected< Elf_Rel_Range > rels(const Elf_Shdr *Sec) const
typename ELFT::Shdr Elf_Shdr
typename ELFT::Hash Elf_Hash