[llvm] r285951 - Add error handling to getEntry.
Rafael Espindola via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 3 11:05:33 PDT 2016
Author: rafael
Date: Thu Nov 3 13:05:33 2016
New Revision: 285951
URL: http://llvm.org/viewvc/llvm-project?rev=285951&view=rev
Log:
Add error handling to getEntry.
Issue found by inspection.
Added:
llvm/trunk/test/Object/Inputs/invalid-rel-sym.elf
Modified:
llvm/trunk/include/llvm/Object/ELF.h
llvm/trunk/test/Object/invalid.test
llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h
llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
llvm/trunk/tools/obj2yaml/elf2yaml.cpp
Modified: llvm/trunk/include/llvm/Object/ELF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELF.h?rev=285951&r1=285950&r2=285951&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELF.h (original)
+++ llvm/trunk/include/llvm/Object/ELF.h Thu Nov 3 13:05:33 2016
@@ -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>::getRelocationTypeNam
}
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>::getEnt
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>
Added: llvm/trunk/test/Object/Inputs/invalid-rel-sym.elf
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/Inputs/invalid-rel-sym.elf?rev=285951&view=auto
==============================================================================
Binary files llvm/trunk/test/Object/Inputs/invalid-rel-sym.elf (added) and llvm/trunk/test/Object/Inputs/invalid-rel-sym.elf Thu Nov 3 13:05:33 2016 differ
Modified: llvm/trunk/test/Object/invalid.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/invalid.test?rev=285951&r1=285950&r2=285951&view=diff
==============================================================================
--- llvm/trunk/test/Object/invalid.test (original)
+++ llvm/trunk/test/Object/invalid.test Thu Nov 3 13:05:33 2016
@@ -78,3 +78,6 @@ INVALID-SECTION-SIZE2: Invalid data was
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.
Modified: llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h?rev=285951&r1=285950&r2=285951&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h (original)
+++ llvm/trunk/tools/llvm-readobj/ARMEHABIPrinter.h Thu Nov 3 13:05:33 2016
@@ -396,7 +396,8 @@ PrinterContext<ET>::FindExceptionTable(u
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);
Modified: llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ELFDumper.cpp?rev=285951&r1=285950&r2=285951&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ELFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/ELFDumper.cpp Thu Nov 3 13:05:33 2016
@@ -647,8 +647,8 @@ StringRef ELFDumper<ELFT>::getSymbolVers
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
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
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
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(con
// 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
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(co
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()));
Modified: llvm/trunk/tools/obj2yaml/elf2yaml.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/obj2yaml/elf2yaml.cpp?rev=285951&r1=285950&r2=285951&view=diff
==============================================================================
--- llvm/trunk/tools/obj2yaml/elf2yaml.cpp (original)
+++ llvm/trunk/tools/obj2yaml/elf2yaml.cpp Thu Nov 3 13:05:33 2016
@@ -216,7 +216,10 @@ std::error_code ELFDumper<ELFT>::dumpRel
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;
More information about the llvm-commits
mailing list