diff --git a/llvm/test/tools/llvm-readobj/XCOFF/relocations-invalid.test b/llvm/test/tools/llvm-readobj/XCOFF/relocations-invalid.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-readobj/XCOFF/relocations-invalid.test @@ -0,0 +1,39 @@ +## Check we report warnings when relocations are broken. + +# RUN: yaml2obj --docnum=1 %s -o %t1 +# RUN: llvm-readobj --relocs --expand-relocs %t1 2>&1 | \ +# RUN: FileCheck %s -DFILE=%t1 --check-prefix=INVALID-REL + +# INVALID-REL: Relocations [ +# INVALID-REL-NEXT: warning: '[[FILE]]': The end of the file was unexpectedly encountered +# INVALID-REL-NEXT: ] + +--- !XCOFF +FileHeader: + MagicNumber: 0x1DF +Sections: + - Name: .text + Flags: [ STYP_TEXT ] +## Case 1: Incorrect offset to relocation entries. + FileOffsetToRelocations: 0x222 + +# RUN: yaml2obj --docnum=2 %s -o %t2 +# RUN: llvm-readobj --relocs --expand-relocs %t2 2>&1 | \ +# RUN: FileCheck %s -DFILE=%t2 --check-prefix=INVALID-SYM + +# INVALID-SYM: Relocations [ +# INVALID-SYM-NEXT: Section (index: 1) .text { +# INVALID-SYM-NEXT: warning: '[[FILE]]': Invalid symbol index +# INVALID-SYM-NEXT: } +# INVALID-SYM-NEXT: ] + +--- !XCOFF +FileHeader: + MagicNumber: 0x1DF +Sections: + - Name: .text + Flags: [ STYP_TEXT ] + Relocations: + - Address: 0x80 +## Case 2: There is no symbol with index 0x21. + Symbol: 0x21 diff --git a/llvm/tools/llvm-readobj/XCOFFDumper.cpp b/llvm/tools/llvm-readobj/XCOFFDumper.cpp --- a/llvm/tools/llvm-readobj/XCOFFDumper.cpp +++ b/llvm/tools/llvm-readobj/XCOFFDumper.cpp @@ -134,16 +134,27 @@ if (Sec.Flags != XCOFF::STYP_TEXT && Sec.Flags != XCOFF::STYP_DATA && Sec.Flags != XCOFF::STYP_TDATA && Sec.Flags != XCOFF::STYP_DWARF) continue; - auto Relocations = unwrapOrError(Obj.getFileName(), Obj.relocations(Sec)); + auto ErrOrRelocations = Obj.relocations(Sec); + if (Error E = ErrOrRelocations.takeError()) { + reportUniqueWarning(std::move(E)); + continue; + } + + auto Relocations = *ErrOrRelocations; if (Relocations.empty()) continue; W.startLine() << "Section (index: " << Index << ") " << Sec.getName() << " {\n"; for (auto Reloc : Relocations) { - StringRef SymbolName = unwrapOrError( - Obj.getFileName(), Obj.getSymbolNameByIndex(Reloc.SymbolIndex)); + Expected ErrOrSymbolName = + Obj.getSymbolNameByIndex(Reloc.SymbolIndex); + if (Error E = ErrOrSymbolName.takeError()) { + reportUniqueWarning(std::move(E)); + continue; + } + StringRef SymbolName = *ErrOrSymbolName; DictScope RelocScope(W, "Relocation"); W.printHex("Virtual Address", Reloc.VirtualAddress); W.printNumber("Symbol", SymbolName, Reloc.SymbolIndex);