diff --git a/lld/lib/Driver/WinLinkDriver.cpp b/lld/lib/Driver/WinLinkDriver.cpp index 13d7b7eb0c25..5d8b091b54bd 100644 --- a/lld/lib/Driver/WinLinkDriver.cpp +++ b/lld/lib/Driver/WinLinkDriver.cpp @@ -862,8 +862,8 @@ WinLinkDriver::parse(int argc, const char *argv[], PECOFFLinkingContext &ctx, // has already done. if (isReadingDirectiveSection) for (auto &e : inputElements) - if (error_code ec = e->parse(ctx, diagnostics)) - return ec; + if (e->parse(ctx, diagnostics)) + return false; // Add the input files to the input graph. if (!ctx.hasInputGraph()) diff --git a/lld/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp b/lld/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp index 58baeb7b9fa6..36437ee0f3a0 100644 --- a/lld/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp +++ b/lld/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp @@ -767,7 +767,7 @@ private: // // The section mainly contains /defaultlib (-l in Unix), but can contain any // options as long as they are valid. - void handleDirectiveSection(StringRef directives) const { + error_code handleDirectiveSection(StringRef directives) const { DEBUG({ llvm::dbgs() << ".drectve: " << directives << "\n"; }); @@ -790,13 +790,13 @@ private: // Print error message if error. if (parseFailed) { - auto msg = - Twine("Failed to parse '") + directives + "': " + errorMessage + "\n"; - llvm::report_fatal_error(msg); + llvm::errs() << "Failed to parse '" << directives << "'\n"; + return make_error_code(llvm::object::object_error::invalid_file_type); } if (!errorMessage.empty()) { llvm::errs() << "lld warning: " << errorMessage << "\n"; } + return error_code::success(); } // @@ -919,7 +919,8 @@ private: // Interpret .drectve section if the section has contents. StringRef directives = file->getLinkerDirectives(); if (!directives.empty()) - handleDirectiveSection(directives); + if (error_code ec = handleDirectiveSection(directives)) + return ec; result.push_back(std::move(file)); return error_code::success(); diff --git a/lld/test/pecoff/Inputs/unknown-drectve.obj.yaml b/lld/test/pecoff/Inputs/unknown-drectve.obj.yaml new file mode 100644 index 000000000000..ea97eb8699cb --- /dev/null +++ b/lld/test/pecoff/Inputs/unknown-drectve.obj.yaml @@ -0,0 +1,34 @@ +--- +header: + Machine: IMAGE_FILE_MACHINE_I386 + Characteristics: [ ] +sections: + - Name: .text + Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ] + Alignment: 16 + SectionData: 558BEC56FF15000000008B0D000000008B3103F0FF150000000003C65E5DC3 + - Name: .drectve + Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ] + Alignment: 2147483648 + + # /nosuchoption:foobar + SectionData: 2f6e6f737563686f7074696f6e3a666f6f62617200 + +symbols: + - Name: .text + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_STATIC + NumberOfAuxSymbols: 1 + AuxiliaryData: 1F000000030000008C7450D6000000000000 + - Name: .drectve + Value: 0 + SectionNumber: 2 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_STATIC + NumberOfAuxSymbols: 1 + AuxiliaryData: 0D0000000000000000000000000000000000 +... diff --git a/lld/test/pecoff/unknown-drectve.test b/lld/test/pecoff/unknown-drectve.test new file mode 100644 index 000000000000..341cc9202ed3 --- /dev/null +++ b/lld/test/pecoff/unknown-drectve.test @@ -0,0 +1,6 @@ +# RUN: yaml2obj %p/Inputs/unknown-drectve.obj.yaml > %t.obj +# +# RUN: not lld -flavor link /out:%t.exe -- %t.obj >& %t.log +# RUN: FileCheck -check-prefix=ERROR %s < %t.log + +ERROR: Failed to parse '/nosuchoption:foobar'