Skip to content

Instantly share code, notes, and snippets.

@zeroomega
Created January 12, 2022 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeroomega/3d19c04cdc489d99f5ff0d0974b61772 to your computer and use it in GitHub Desktop.
Save zeroomega/3d19c04cdc489d99f5ff0d0974b61772 to your computer and use it in GitHub Desktop.
/// This function looks for and return the section header of the .dynsym
/// table. If it is not found, an error will be returned.
template <class ELFT>
Expected<Elf_Shdr> ELFFile<ELFT>::getDynSymtabHeader() const {
Expected<Elf_Shdr_Range> SectionsOrError = ElfFile.sections();
if (!SectionsOrError)
return Shdrs.takeError();
for (const Elf_Shdr &Sec : *SectionsOrError) {
if (Sec.sh_type == ELF::SHT_DYNSYM) {
// If multiple .dynsym present, use the first one.
// This behavior aligns with llvm::object::ELFFile::getDynSymtabSize()
return getStringTableForSymtab(Sec, *Shdrs);
}
}
return createStringError(object_error::section_not_found, ".dynsym section not found");
}
// ---------------
/// Returns the StringRef to the DynStr from an ElfFile
/// @param ElfFile Reference to the ELFFile object.
/// @param DynEnt Reference to the DynamicEntries object.
template <class ELFT>
static Expected<StringRef> getDynStr(const ELFFile<ELFT> &ElfFile,
const DynamicEntries &DynEnt) {
using Elf_Shdr_Range = typename ELFT::ShdrRange;
using Elf_Shdr = typename ELFT::Shdr;
// Read offset from section headers.
Expected<Elf_Shdr> DynSymShdr = ElfFile.getDynSymtabHeader();
if (!DynSymShdr) {
Error E = DynSymShdr.takeError();
if (E.convertToErrorCode() != object_error::section_not_found) {
return E;
} else {
// Fallback to use offsets from .dynamic as .dynsym was not found.
Expected<const uint8_t *> DynStrPtr = ElfFile.toMappedAddr(DynEnt.StrTabAddr);
if (!DynStrPtr)
return appendToError(DynStrPtr.takeError(),
"when locating .dynstr section contents");
return StringRef(reinterpret_cast<const char *>(*DynStrPtr), DynEnt.StrSize);
}
}
Expected<Elf_Shdr_Range> Shdrs = ElfFile.sections();
if (!Shdrs)
return Shdrs.takeError();
return ElfFile.getStringTableForSymtab(Sec, *Shdrs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment