template <typename T>
ErrorOr<const T *> getEntry(uint32_t Section, uint32_t Entry) const;
template <typename T>
- const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
+ ErrorOr<const T *> getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
ErrorOr<StringRef> getStringTable(const Elf_Shdr *Section) const;
ErrorOr<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
SmallVectorImpl<char> &Result) const;
/// \brief Get the symbol for a given relocation.
- const Elf_Sym *getRelocationSymbol(const Elf_Rel *Rel,
- const Elf_Shdr *SymTab) const;
+ ErrorOr<const Elf_Sym *> getRelocationSymbol(const Elf_Rel *Rel,
+ const Elf_Shdr *SymTab) const;
ELFFile(StringRef Object, std::error_code &EC);
}
template <class ELFT>
-const typename ELFT::Sym *
+ErrorOr<const typename ELFT::Sym *>
ELFFile<ELFT>::getRelocationSymbol(const Elf_Rel *Rel,
const Elf_Shdr *SymTab) const {
uint32_t Index = Rel->getSymbol(isMips64EL());
template <class ELFT>
template <typename T>
-const T *ELFFile<ELFT>::getEntry(const Elf_Shdr *Section,
- uint32_t Entry) const {
- return reinterpret_cast<const T *>(base() + Section->sh_offset +
- (Entry * Section->sh_entsize));
+ErrorOr<const T *> ELFFile<ELFT>::getEntry(const Elf_Shdr *Section,
+ uint32_t Entry) const {
+ if (sizeof(T) != Section->sh_entsize)
+ return object_error::parse_failed;
+ size_t Pos = Section->sh_offset + Entry * sizeof(T);
+ if (Pos + sizeof(T) > Buf.size())
+ return object_error::parse_failed;
+ return reinterpret_cast<const T *>(base() + Pos);
}
template <class ELFT>
sizeof(Elf_Sym);
// Get the corresponding version index entry
- const Elf_Versym *vs =
- Obj->template getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
+ const Elf_Versym *vs = unwrapOrError(
+ Obj->template getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index));
size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
// Special markers for unversioned symbols.
switch (PLTRelShdr->sh_type) {
case ELF::SHT_REL:
for (const Elf_Rel &Rel : Obj->rels(PLTRelShdr)) {
- const Elf_Sym *Sym = Obj->getRelocationSymbol(&Rel, SymTable);
+ const Elf_Sym *Sym =
+ unwrapOrError(Obj->getRelocationSymbol(&Rel, SymTable));
printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, StrTable, Sym);
if (++It == PLTEnd)
break;
break;
case ELF::SHT_RELA:
for (const Elf_Rela &Rel : Obj->relas(PLTRelShdr)) {
- const Elf_Sym *Sym = Obj->getRelocationSymbol(&Rel, SymTable);
+ const Elf_Sym *Sym =
+ unwrapOrError(Obj->getRelocationSymbol(&Rel, SymTable));
printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, StrTable, Sym);
if (++It == PLTEnd)
break;
const Elf_Shdr *Symtab = unwrapOrError(Obj->getSection(Sec.sh_link));
StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*Symtab));
const Elf_Sym *Signature =
- Obj->template getEntry<Elf_Sym>(Symtab, Sec.sh_info);
+ unwrapOrError(Obj->template getEntry<Elf_Sym>(Symtab, Sec.sh_info));
ArrayRef<Elf_Word> Data = unwrapOrError(
Obj->template getSectionContentsAsArray<Elf_Word>(&Sec));
StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
// fixed width.
Field Fields[5] = {0, 10 + Bias, 19 + 2 * Bias, 42 + 2 * Bias, 53 + 2 * Bias};
Obj->getRelocationTypeName(R.getType(Obj->isMips64EL()), RelocName);
- Sym = Obj->getRelocationSymbol(&R, SymTab);
+ Sym = unwrapOrError(Obj->getRelocationSymbol(&R, SymTab));
if (Sym && Sym->getType() == ELF::STT_SECTION) {
const Elf_Shdr *Sec = unwrapOrError(
Obj->getSection(Sym, SymTab, this->dumper()->getShndxTable()));
HasGroups = true;
const Elf_Shdr *Symtab = unwrapOrError(Obj->getSection(Sec.sh_link));
StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*Symtab));
- const Elf_Sym *Sym = Obj->template getEntry<Elf_Sym>(Symtab, Sec.sh_info);
+ const Elf_Sym *Sym =
+ unwrapOrError(Obj->template getEntry<Elf_Sym>(Symtab, Sec.sh_info));
auto Data = unwrapOrError(
Obj->template getSectionContentsAsArray<Elf_Word>(&Sec));
DictScope D(W, "Group");
SmallString<32> RelocName;
Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
StringRef TargetName;
- const Elf_Sym *Sym = Obj->getRelocationSymbol(&Rel, SymTab);
+ const Elf_Sym *Sym = unwrapOrError(Obj->getRelocationSymbol(&Rel, SymTab));
if (Sym && Sym->getType() == ELF::STT_SECTION) {
const Elf_Shdr *Sec = unwrapOrError(
Obj->getSection(Sym, SymTab, this->dumper()->getShndxTable()));