llvm.org GIT mirror llvm / 2436e45
[llvm-readobj] Improve errors on invalid binary 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 Sam Clegg 3 years ago
3 changed file(s) with 12 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
5252
5353
5454 RUN: not llvm-readobj -t %p/Inputs/invalid-xindex-size.elf 2>&1 | FileCheck --check-prefix=INVALID-XINDEX-SIZE %s
55 INVALID-XINDEX-SIZE: Invalid data was encountered while parsing the file.
55 INVALID-XINDEX-SIZE: Invalid data was encountered while parsing the file
5656
5757 RUN: not llvm-readobj -t %p/Inputs/invalid-e_shnum.elf 2>&1 | FileCheck --check-prefix=INVALID-SH-NUM %s
5858 INVALID-SH-NUM: invalid e_phentsize
7676 INVALID-SECTION-SIZE2: invalid section offset
7777
7878 RUN: not llvm-readobj -t %p/Inputs/invalid-sections-num.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-NUM %s
79 INVALID-SECTION-NUM: Invalid data was encountered while parsing the file.
79 INVALID-SECTION-NUM: Invalid data was encountered while parsing the file
8080
8181 RUN: not llvm-readobj -r %p/Inputs/invalid-rel-sym.elf 2>&1 | FileCheck --check-prefix=INVALID-REL-SYM %s
8282 INVALID-REL-SYM: invalid section offset
0 # RUN: yaml2obj %s | not llvm-readobj -t - 2>&1 | FileCheck %s
1
2 --- !WASM
3 FileHeader:
4 Version: 0x0000000c
5
6 # CHECK: Error reading file: : Bad version number
308308 Input = "";
309309
310310 reportError(Twine(Input) + ": " + EC.message());
311 }
312
313 static void reportError(StringRef Input, StringRef Message) {
314 if (Input == "-")
315 Input = "";
316
317 reportError(Twine(Input) + ": " + Message);
318311 }
319312
320313 static void reportError(StringRef Input, Error Err) {
480473 Expected> ChildOrErr = Child.getAsBinary();
481474 if (!ChildOrErr) {
482475 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
483 std::string Buf;
484 raw_string_ostream OS(Buf);
485 logAllUnhandledErrors(ChildOrErr.takeError(), OS, "");
486 OS.flush();
487 reportError(Arc->getFileName(), Buf);
476 reportError(Arc->getFileName(), ChildOrErr.takeError());
488477 }
489478 continue;
490479 }
506495 if (ObjOrErr)
507496 dumpObject(&*ObjOrErr.get());
508497 else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
509 std::string Buf;
510 raw_string_ostream OS(Buf);
511 logAllUnhandledErrors(ObjOrErr.takeError(), OS, "");
512 OS.flush();
513 reportError(UBinary->getFileName(), Buf);
498 reportError(UBinary->getFileName(), ObjOrErr.takeError());
514499 }
515500 else if (Expected> AOrErr = Obj.getAsArchive())
516501 dumpArchive(&*AOrErr.get());
523508 // Attempt to open the binary.
524509 Expected> BinaryOrErr = createBinary(File);
525510 if (!BinaryOrErr)
526 reportError(File, errorToErrorCode(BinaryOrErr.takeError()));
511 reportError(File, BinaryOrErr.takeError());
527512 Binary &Binary = *BinaryOrErr.get().getBinary();
528513
529514 if (Archive *Arc = dyn_cast(&Binary))