diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -63,15 +63,13 @@ 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 lldMain(). - auto *ctx = new COFFLinkerContext; + COFFLinkerContext ctx; + ctx.e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); + ctx.e.logName = args::getFilenameWithoutExe(args[0]); + ctx.e.errorLimitExceededMsg = "too many errors emitted, stopping now" + " (use /errorlimit:0 to see all errors)"; - ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); - ctx->e.logName = args::getFilenameWithoutExe(args[0]); - ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now" - " (use /errorlimit:0 to see all errors)"; - - ctx->driver.linkerMain(args); + ctx.driver.linkerMain(args); return errorCount() == 0; } diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -109,11 +109,9 @@ bool elf::link(ArrayRef args, llvm::raw_ostream &stdoutOS, llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) { - // This driver-specific context will be freed later by lldMain(). - auto *ctx = new CommonLinkerContext; - - ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); - ctx->e.cleanupCallback = []() { + CommonLinkerContext ctx; + ctx.e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); + ctx.e.cleanupCallback = []() { elf::ctx.reset(); symtab = SymbolTable(); @@ -128,9 +126,9 @@ SharedFile::vernauxNum = 0; }; - ctx->e.logName = args::getFilenameWithoutExe(args[0]); - ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use " - "--error-limit=0 to see all errors)"; + ctx.e.logName = args::getFilenameWithoutExe(args[0]); + ctx.e.errorLimitExceededMsg = "too many errors emitted, stopping now (use " + "--error-limit=0 to see all errors)"; config = ConfigWrapper(); script = std::make_unique(); diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -1365,11 +1365,9 @@ bool macho::link(ArrayRef argsArr, llvm::raw_ostream &stdoutOS, llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) { - // This driver-specific context will be freed later by lldMain(). - auto *ctx = new CommonLinkerContext; - - ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); - ctx->e.cleanupCallback = []() { + CommonLinkerContext ctx; + ctx.e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); + ctx.e.cleanupCallback = []() { resolvedFrameworks.clear(); resolvedLibraries.clear(); cachedReads.clear(); @@ -1392,15 +1390,15 @@ InputFile::resetIdCount(); }; - ctx->e.logName = args::getFilenameWithoutExe(argsArr[0]); + ctx.e.logName = args::getFilenameWithoutExe(argsArr[0]); MachOOptTable parser; InputArgList args = parser.parse(argsArr.slice(1)); - ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now " - "(use --error-limit=0 to see all errors)"; - ctx->e.errorLimit = args::getInteger(args, OPT_error_limit_eq, 20); - ctx->e.verbose = args.hasArg(OPT_verbose); + ctx.e.errorLimitExceededMsg = "too many errors emitted, stopping now " + "(use --error-limit=0 to see all errors)"; + ctx.e.errorLimit = args::getInteger(args, OPT_error_limit_eq, 20); + ctx.e.verbose = args.hasArg(OPT_verbose); if (args.hasArg(OPT_help_hidden)) { parser.printHelp(argsArr[0], /*showHidden=*/true); diff --git a/lld/MinGW/Driver.cpp b/lld/MinGW/Driver.cpp --- a/lld/MinGW/Driver.cpp +++ b/lld/MinGW/Driver.cpp @@ -157,13 +157,13 @@ return ""; } -// Convert Unix-ish command line arguments to Windows-ish ones and -// then call coff::link. -bool mingw::link(ArrayRef argsArr, llvm::raw_ostream &stdoutOS, - llvm::raw_ostream &stderrOS, bool exitEarly, - bool disableOutput) { - auto *ctx = new CommonLinkerContext; - ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); +static std::optional translate(ArrayRef argsArr, + llvm::raw_ostream &stdoutOS, + llvm::raw_ostream &stderrOS, + bool exitEarly, bool disableOutput, + std::vector &vec) { + CommonLinkerContext ctx; + ctx.e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); MinGWOptTable parser; opt::InputArgList args = parser.parse(argsArr.slice(1)); @@ -470,15 +470,22 @@ return true; // Repack vector of strings to vector of const char pointers for coff::link. - std::vector vec; for (const std::string &s : linkArgs) vec.push_back(s.c_str()); // Pass the actual binary name, to make error messages be printed with // the right prefix. vec[0] = argsArr[0]; + return std::nullopt; +} - // The context will be re-created in the COFF driver. - lld::CommonLinkerContext::destroy(); - +// Convert Unix-ish command line arguments to Windows-ish ones and +// then call coff::link. +bool mingw::link(ArrayRef argsArr, llvm::raw_ostream &stdoutOS, + llvm::raw_ostream &stderrOS, bool exitEarly, + bool disableOutput) { + std::vector vec; + if (std::optional ret = + translate(argsArr, stdoutOS, stderrOS, exitEarly, disableOutput, vec)) + return *ret; return coff::link(vec, stdoutOS, stderrOS, exitEarly, disableOutput); } diff --git a/lld/tools/lld/lld.cpp b/lld/tools/lld/lld.cpp --- a/lld/tools/lld/lld.cpp +++ b/lld/tools/lld/lld.cpp @@ -171,10 +171,6 @@ if (exitEarly) exitLld(!r ? 1 : 0); - // Delete the global context and clear the global context pointer, so that it - // cannot be accessed anymore. - CommonLinkerContext::destroy(); - return !r ? 1 : 0; } diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -85,13 +85,11 @@ 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 lldMain(). - auto *ctx = new CommonLinkerContext; - - ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); - ctx->e.logName = args::getFilenameWithoutExe(args[0]); - ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use " - "-error-limit=0 to see all errors)"; + CommonLinkerContext ctx; + ctx.e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); + ctx.e.logName = args::getFilenameWithoutExe(args[0]); + ctx.e.errorLimitExceededMsg = "too many errors emitted, stopping now (use " + "-error-limit=0 to see all errors)"; config = make(); symtab = make();