Skip to content

Commit

Permalink
[llvm-readobj] Improve errors on invalid binary
Browse files Browse the repository at this point in the history
The previous code was discarding the error message from
createBinary() by calling errorToErrorCode().
This meant that such error were always reported unhelpfully
as "Invalid data was encountered while parsing the file".

Other tools such as llvm-objdump already produce a more
the error message in this case.

Differential Revision: https://reviews.llvm.org/D32985

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302664 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
sbc100 committed May 10, 2017
1 parent 102d6ff commit 2436e45
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
4 changes: 2 additions & 2 deletions test/Object/invalid.test
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ INVALID-SYMTAB-SIZE: size is not a multiple of sh_entsize


RUN: not llvm-readobj -t %p/Inputs/invalid-xindex-size.elf 2>&1 | FileCheck --check-prefix=INVALID-XINDEX-SIZE %s
INVALID-XINDEX-SIZE: Invalid data was encountered while parsing the file.
INVALID-XINDEX-SIZE: Invalid data was encountered while parsing the file

RUN: not llvm-readobj -t %p/Inputs/invalid-e_shnum.elf 2>&1 | FileCheck --check-prefix=INVALID-SH-NUM %s
INVALID-SH-NUM: invalid e_phentsize
Expand All @@ -77,7 +77,7 @@ RUN: FileCheck --check-prefix=INVALID-SECTION-SIZE2 %s
INVALID-SECTION-SIZE2: invalid section offset

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.
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 section offset
7 changes: 7 additions & 0 deletions test/tools/llvm-readobj/wasm-invalid.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# RUN: yaml2obj %s | not llvm-readobj -t - 2>&1 | FileCheck %s

--- !WASM
FileHeader:
Version: 0x0000000c

# CHECK: Error reading file: <stdin>: Bad version number
21 changes: 3 additions & 18 deletions tools/llvm-readobj/llvm-readobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,6 @@ static void reportError(StringRef Input, std::error_code EC) {
reportError(Twine(Input) + ": " + EC.message());
}

static void reportError(StringRef Input, StringRef Message) {
if (Input == "-")
Input = "<stdin>";

reportError(Twine(Input) + ": " + Message);
}

static void reportError(StringRef Input, Error Err) {
if (Input == "-")
Input = "<stdin>";
Expand Down Expand Up @@ -481,11 +474,7 @@ static void dumpArchive(const Archive *Arc) {
Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
if (!ChildOrErr) {
if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(ChildOrErr.takeError(), OS, "");
OS.flush();
reportError(Arc->getFileName(), Buf);
reportError(Arc->getFileName(), ChildOrErr.takeError());
}
continue;
}
Expand All @@ -507,11 +496,7 @@ static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
if (ObjOrErr)
dumpObject(&*ObjOrErr.get());
else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(ObjOrErr.takeError(), OS, "");
OS.flush();
reportError(UBinary->getFileName(), Buf);
reportError(UBinary->getFileName(), ObjOrErr.takeError());
}
else if (Expected<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
dumpArchive(&*AOrErr.get());
Expand All @@ -524,7 +509,7 @@ static void dumpInput(StringRef File) {
// Attempt to open the binary.
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
if (!BinaryOrErr)
reportError(File, errorToErrorCode(BinaryOrErr.takeError()));
reportError(File, BinaryOrErr.takeError());
Binary &Binary = *BinaryOrErr.get().getBinary();

if (Archive *Arc = dyn_cast<Archive>(&Binary))
Expand Down

0 comments on commit 2436e45

Please sign in to comment.