From faab70b7838b1d47067e72ed7ba8313a389cb314 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Tue, 15 Feb 2022 13:04:13 -0800 Subject: [PATCH] [lld][WebAssemlby] Warn on unknown -z flags This code mirrors that in lld/ELF/Driver.cpp, as does the new test code. Differential Revision: https://reviews.llvm.org/D119888 --- lld/ELF/Driver.cpp | 2 +- lld/test/wasm/driver.ll | 17 ++++++++++++++++- lld/wasm/Driver.cpp | 22 ++++++++++++++++++---- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index aee700d..cb8e6d6 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -477,7 +477,7 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { ELFOptTable parser; opt::InputArgList args = parser.parse(argsArr.slice(1)); - // Interpret the flags early because error()/warn() depend on them. + // Interpret these flags early because error()/warn() depend on them. errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20); errorHandler().fatalWarnings = args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false); diff --git a/lld/test/wasm/driver.ll b/lld/test/wasm/driver.ll index 995b10a..e5aecf8 100644 --- a/lld/test/wasm/driver.ll +++ b/lld/test/wasm/driver.ll @@ -2,7 +2,7 @@ target triple = "wasm32-unknown-unknown" -define hidden void @entry() local_unnamed_addr #0 { +define hidden void @_start() local_unnamed_addr #0 { entry: ret void } @@ -24,3 +24,18 @@ entry: ; RUN: not wasm-ld --relocatable --shared-memory %t.o 2>&1 \ ; RUN: | FileCheck -check-prefix=SHARED-MEM %s ; SHARED-MEM: error: -r and --shared-memory may not be used together + +; RUN: wasm-ld %t.o -z foo -o /dev/null 2>&1 | FileCheck -check-prefix=ERR10 %s +; RUN: wasm-ld %t.o -z foo -o /dev/null --version 2>&1 | FileCheck -check-prefix=ERR10 %s +; ERR10: warning: unknown -z value: foo + +;; Check we report "unknown -z value" error even with -v. +; RUN: wasm-ld %t.o -z foo -o /dev/null -v 2>&1 | FileCheck -check-prefix=ERR10 %s + +;; Note: in GNU ld, --fatal-warning still leads to a warning. +; RUN: not wasm-ld %t.o -z foo --fatal-warnings 2>&1 | FileCheck --check-prefix=ERR10-FATAL %s +; ERR10-FATAL: error: unknown -z value: foo + +;; stack-size without an = is also an error +; RUN: not wasm-ld %t.o -z stack-size 2>&1 | FileCheck -check-prefix=ERR11 %s +; ERR11: unknown -z value: stack-size diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp index cb7ca13..8ef8620 100644 --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -357,8 +357,6 @@ static void readConfigs(opt::InputArgList &args) { config->exportAll = args.hasArg(OPT_export_all); config->exportTable = args.hasArg(OPT_export_table); config->growableTable = args.hasArg(OPT_growable_table); - errorHandler().fatalWarnings = - args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false); config->importMemory = args.hasArg(OPT_import_memory); config->sharedMemory = args.hasArg(OPT_shared_memory); config->importTable = args.hasArg(OPT_import_table); @@ -808,10 +806,28 @@ static void splitSections() { }); } +static bool isKnownZFlag(StringRef s) { + // For now, we only support a very limited set of -z flags + return s.startswith("stack-size="); +} + +// Report a warning for an unknown -z option. +static void checkZOptions(opt::InputArgList &args) { + for (auto *arg : args.filtered(OPT_z)) + if (!isKnownZFlag(arg->getValue())) + warn("unknown -z value: " + StringRef(arg->getValue())); +} + void LinkerDriver::linkerMain(ArrayRef argsArr) { WasmOptTable parser; opt::InputArgList args = parser.parse(argsArr.slice(1)); + // Interpret these flags early because error()/warn() depend on them. + errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20); + errorHandler().fatalWarnings = + args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false); + checkZOptions(args); + // Handle --help if (args.hasArg(OPT_help)) { parser.printHelp(lld::outs(), @@ -848,8 +864,6 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { cl::ResetAllOptionOccurrences(); cl::ParseCommandLineOptions(v.size(), v.data()); - errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20); - readConfigs(args); createFiles(args); -- 2.7.4