From 4bfcd41cc29169886fc420ffd0b1039c30710a53 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 12 Dec 2024 23:50:31 -0800 Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20change?= =?UTF-8?q?s=20to=20main=20this=20commit=20is=20based=20on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created using spr 1.3.5-bogner [skip ci] --- lld/wasm/Config.h | 12 +++++++-- lld/wasm/Driver.cpp | 61 ++++++++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/lld/wasm/Config.h b/lld/wasm/Config.h index eb32ce80f4a3d..0c2ba3eebffc4 100644 --- a/lld/wasm/Config.h +++ b/lld/wasm/Config.h @@ -43,7 +43,7 @@ enum class BuildIdKind { None, Fast, Sha1, Hexstring, Uuid }; // Most fields are direct mapping from the command line options // and such fields have the same name as the corresponding options. // Most fields are initialized by the driver. -struct Configuration { +struct Config { bool allowMultipleDefinition; bool bsymbolic; bool checkFeatures; @@ -126,11 +126,18 @@ struct Configuration { llvm::SmallVector buildIdVector; }; +struct ConfigWrapper { + Config c; + Config *operator->() { return &c; } +}; + // The only instance of Configuration struct. -extern Configuration *config; +extern ConfigWrapper config; // The Ctx object hold all other (non-configuration) global state. struct Ctx { + Config &arg; + llvm::SmallVector objectFiles; llvm::SmallVector stubFiles; llvm::SmallVector sharedFiles; @@ -156,6 +163,7 @@ struct Ctx { 0> whyExtractRecords; + Ctx(); void reset(); }; diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp index 00b5c82d9c777..02471950fb519 100644 --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -44,7 +44,7 @@ using namespace llvm::sys; using namespace llvm::wasm; namespace lld::wasm { -Configuration *config; +ConfigWrapper config; Ctx ctx; void errorOrWarn(const llvm::Twine &msg) { @@ -54,7 +54,11 @@ void errorOrWarn(const llvm::Twine &msg) { error(msg); } +Ctx::Ctx() : arg(config.c) {} + void Ctx::reset() { + arg.~Config(); + new (&arg) Config(); objectFiles.clear(); stubFiles.clear(); sharedFiles.clear(); @@ -92,6 +96,7 @@ static void initLLVM() { class LinkerDriver { public: + LinkerDriver(Ctx &); void linkerMain(ArrayRef argsArr); private: @@ -99,6 +104,8 @@ class LinkerDriver { void addFile(StringRef path); void addLibrary(StringRef name); + Ctx &ctx; + // True if we are in --whole-archive and --no-whole-archive. bool inWholeArchive = false; @@ -122,19 +129,19 @@ static bool hasZOption(opt::InputArgList &args, StringRef key) { bool link(ArrayRef args, llvm::raw_ostream &stdoutOS, llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) { // This driver-specific context will be freed later by unsafeLldMain(). - auto *ctx = new CommonLinkerContext; + auto *context = new CommonLinkerContext; - ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); - ctx->e.cleanupCallback = []() { wasm::ctx.reset(); }; - ctx->e.logName = args::getFilenameWithoutExe(args[0]); - ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use " - "-error-limit=0 to see all errors)"; + context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); + context->e.cleanupCallback = []() { ctx.reset(); }; + context->e.logName = args::getFilenameWithoutExe(args[0]); + context->e.errorLimitExceededMsg = + "too many errors emitted, stopping now (use " + "-error-limit=0 to see all errors)"; - config = make(); symtab = make(); initLLVM(); - LinkerDriver().linkerMain(args); + LinkerDriver(ctx).linkerMain(args); return errorCount() == 0; } @@ -1256,6 +1263,8 @@ static void checkZOptions(opt::InputArgList &args) { warn("unknown -z value: " + StringRef(arg->getValue())); } +LinkerDriver::LinkerDriver(Ctx &ctx) : ctx(ctx) {} + void LinkerDriver::linkerMain(ArrayRef argsArr) { WasmOptTable parser; opt::InputArgList args = parser.parse(argsArr.slice(1)); @@ -1324,10 +1333,10 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { // Fail early if the output file or map file is not writable. If a user has a // long link, e.g. due to a large LTO link, they do not wish to run it and // find that it failed because there was a mistake in their command-line. - if (auto e = tryCreateFile(config->outputFile)) - error("cannot open output file " + config->outputFile + ": " + e.message()); - if (auto e = tryCreateFile(config->mapFile)) - error("cannot open map file " + config->mapFile + ": " + e.message()); + if (auto e = tryCreateFile(ctx.arg.outputFile)) + error("cannot open output file " + ctx.arg.outputFile + ": " + e.message()); + if (auto e = tryCreateFile(ctx.arg.mapFile)) + error("cannot open map file " + ctx.arg.mapFile + ": " + e.message()); if (errorCount()) return; @@ -1336,11 +1345,11 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { symtab->trace(arg->getValue()); for (auto *arg : args.filtered(OPT_export_if_defined)) - config->exportedSymbols.insert(arg->getValue()); + ctx.arg.exportedSymbols.insert(arg->getValue()); for (auto *arg : args.filtered(OPT_export)) { - config->exportedSymbols.insert(arg->getValue()); - config->requiredExports.push_back(arg->getValue()); + ctx.arg.exportedSymbols.insert(arg->getValue()); + ctx.arg.requiredExports.push_back(arg->getValue()); } createSyntheticSymbols(); @@ -1358,17 +1367,17 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { // Handle the `--export ` options // This works like --undefined but also exports the symbol if its found - for (auto &iter : config->exportedSymbols) + for (auto &iter : ctx.arg.exportedSymbols) handleUndefined(iter.first(), "--export"); Symbol *entrySym = nullptr; - if (!config->relocatable && !config->entry.empty()) { - entrySym = handleUndefined(config->entry, "--entry"); + if (!ctx.arg.relocatable && !ctx.arg.entry.empty()) { + entrySym = handleUndefined(ctx.arg.entry, "--entry"); if (entrySym && entrySym->isDefined()) entrySym->forceExport = true; else error("entry symbol not defined (pass --no-entry to suppress): " + - config->entry); + ctx.arg.entry); } // If the user code defines a `__wasm_call_dtors` function, remember it so @@ -1376,10 +1385,10 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { // `__wasm_call_ctors` which we synthesize, `__wasm_call_dtors` is defined // by libc/etc., because destructors are registered dynamically with // `__cxa_atexit` and friends. - if (!config->relocatable && !config->shared && + if (!ctx.arg.relocatable && !ctx.arg.shared && !WasmSym::callCtors->isUsedInRegularObj && - WasmSym::callCtors->getName() != config->entry && - !config->exportedSymbols.count(WasmSym::callCtors->getName())) { + WasmSym::callCtors->getName() != ctx.arg.entry && + !ctx.arg.exportedSymbols.count(WasmSym::callCtors->getName())) { if (Symbol *callDtors = handleUndefined("__wasm_call_dtors", "")) { if (auto *callDtorsFunc = dyn_cast(callDtors)) { @@ -1437,7 +1446,7 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { writeWhyExtract(); // Bail out if normal linked output is skipped due to LTO. - if (config->thinLTOIndexOnly) + if (ctx.arg.thinLTOIndexOnly) return; createOptionalSymbols(); @@ -1452,13 +1461,13 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { if (!wrapped.empty()) wrapSymbols(wrapped); - for (auto &iter : config->exportedSymbols) { + for (auto &iter : ctx.arg.exportedSymbols) { Symbol *sym = symtab->find(iter.first()); if (sym && sym->isDefined()) sym->forceExport = true; } - if (!config->relocatable && !ctx.isPic) { + if (!ctx.arg.relocatable && !ctx.isPic) { // Add synthetic dummies for weak undefined functions. Must happen // after LTO otherwise functions may not yet have signatures. symtab->handleWeakUndefines();