Add error handling to getEntry.

Issue found by inspection.

llvm-svn: 285951
This commit is contained in:
Rafael Espindola 2016-11-03 18:05:33 +00:00
parent a667ace90c
commit ed1395a792
6 changed files with 32 additions and 18 deletions

View File

@ -75,7 +75,7 @@ public:
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;
@ -93,8 +93,8 @@ public:
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);
@ -310,7 +310,7 @@ void ELFFile<ELFT>::getRelocationTypeName(uint32_t Type,
}
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());
@ -413,10 +413,14 @@ ErrorOr<const T *> ELFFile<ELFT>::getEntry(uint32_t Section,
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>

Binary file not shown.

View File

@ -78,3 +78,6 @@ INVALID-SECTION-SIZE2: Invalid data was encountered while parsing the file.
RUN: not llvm-readobj -t %p/Inputs/invalid-sections-num.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-NUM %s
INVALID-SECTION-NUM: Invalid data was encountered while parsing the file.
RUN: not llvm-readobj -r %p/Inputs/invalid-rel-sym.elf 2>&1 | FileCheck --check-prefix=INVALID-REL-SYM %s
INVALID-REL-SYM: Invalid data was encountered while parsing the file.

View File

@ -396,7 +396,8 @@ PrinterContext<ET>::FindExceptionTable(unsigned IndexSectionIndex,
RelA.r_info = R.r_info;
RelA.r_addend = 0;
const Elf_Sym *Symbol = ELF->getRelocationSymbol(&RelA, SymTab);
const Elf_Sym *Symbol =
unwrapOrError(ELF->getRelocationSymbol(&RelA, SymTab));
ErrorOr<const Elf_Shdr *> Ret =
ELF->getSection(Symbol, SymTab, ShndxTable);

View File

@ -647,8 +647,8 @@ StringRef ELFDumper<ELFT>::getSymbolVersion(StringRef StrTab,
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.
@ -2068,7 +2068,8 @@ template <class ELFT> void MipsGOTParser<ELFT>::parsePLT() {
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;
@ -2076,7 +2077,8 @@ template <class ELFT> void MipsGOTParser<ELFT>::parsePLT() {
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;
@ -2428,7 +2430,7 @@ template <class ELFT> void GNUStyle<ELFT>::printGroupSections(const ELFO *Obj) {
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));
@ -2465,7 +2467,7 @@ void GNUStyle<ELFT>::printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab,
// 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()));
@ -3332,7 +3334,8 @@ void LLVMStyle<ELFT>::printGroupSections(const ELFO *Obj) {
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");
@ -3406,7 +3409,7 @@ void LLVMStyle<ELFT>::printRelocation(const ELFO *Obj, Elf_Rela Rel,
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()));

View File

@ -216,7 +216,10 @@ std::error_code ELFDumper<ELFT>::dumpRelocation(const RelT *Rel,
R.Offset = Rel->r_offset;
R.Addend = 0;
const Elf_Sym *Sym = Obj.getRelocationSymbol(Rel, SymTab);
auto SymOrErr = Obj.getRelocationSymbol(Rel, SymTab);
if (std::error_code EC = SymOrErr.getError())
return EC;
const Elf_Sym *Sym = *SymOrErr;
ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
if (std::error_code EC = StrTabSec.getError())
return EC;