-
-
Save zeroomega/3d19c04cdc489d99f5ff0d0974b61772 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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