Index: lld/COFF/Driver.cpp =================================================================== --- lld/COFF/Driver.cpp +++ lld/COFF/Driver.cpp @@ -1200,13 +1200,6 @@ void LinkerDriver::link(ArrayRef argsArr) { ScopedTimer rootTimer(Timer::root()); - // Needed for LTO. - InitializeAllTargetInfos(); - InitializeAllTargets(); - InitializeAllTargetMCs(); - InitializeAllAsmParsers(); - InitializeAllAsmPrinters(); - // If the first command line argument is "/lib", link.exe acts like lib.exe. // We call our own implementation of lib.exe that understands bitcode files. if (argsArr.size() > 1 && StringRef(argsArr[1]).equals_lower("/lib")) { Index: lld/ELF/Driver.cpp =================================================================== --- lld/ELF/Driver.cpp +++ lld/ELF/Driver.cpp @@ -291,17 +291,6 @@ error("unable to find library -l" + name, ErrorTag::LibNotFound, {name}); } -// This function is called on startup. We need this for LTO since -// LTO calls LLVM functions to compile bitcode files to native code. -// Technically this can be delayed until we read bitcode files, but -// we don't bother to do lazily because the initialization is fast. -static void initLLVM() { - InitializeAllTargets(); - InitializeAllTargetMCs(); - InitializeAllAsmPrinters(); - InitializeAllAsmParsers(); -} - // Some command line options or some combinations of them are not allowed. // This function checks for such errors. static void checkOptions() { @@ -532,7 +521,6 @@ { llvm::TimeTraceScope timeScope("ExecuteLinker"); - initLLVM(); createFiles(args); if (errorCount()) return; Index: lld/MachO/Driver.cpp =================================================================== --- lld/MachO/Driver.cpp +++ lld/MachO/Driver.cpp @@ -503,17 +503,6 @@ return false; } -// This function is called on startup. We need this for LTO since -// LTO calls LLVM functions to compile bitcode files to native code. -// Technically this can be delayed until we read bitcode files, but -// we don't bother to do lazily because the initialization is fast. -static void initLLVM() { - InitializeAllTargets(); - InitializeAllTargetMCs(); - InitializeAllAsmPrinters(); - InitializeAllAsmParsers(); -} - static void compileBitcodeFiles() { auto lto = make(); for (InputFile *file : inputFiles) @@ -748,8 +737,6 @@ return !errorCount(); } - initLLVM(); // must be run before any call to addFile() - for (const auto &arg : args) { const auto &opt = arg->getOption(); warnIfDeprecatedOption(opt); Index: lld/tools/lld/lld.cpp =================================================================== --- lld/tools/lld/lld.cpp +++ lld/tools/lld/lld.cpp @@ -40,6 +40,7 @@ #include "llvm/Support/Path.h" #include "llvm/Support/PluginLoader.h" #include "llvm/Support/Signals.h" +#include "llvm/Support/TargetSelect.h" #include #if !defined(_MSC_VER) && !defined(__MINGW32__) @@ -141,11 +142,25 @@ return parseProgname(arg0); } +// This function is called on startup. We need this for LTO since +// LTO calls LLVM functions to compile bitcode files to native code. +// Technically this can be delayed until we read bitcode files, but +// we don't bother to do lazily because the initialization is fast. +static void initLLVM() { + InitializeAllTargets(); + InitializeAllTargetMCs(); + InitializeAllAsmPrinters(); + InitializeAllAsmParsers(); +} + /// Universal linker main(). This linker emulates the gnu, darwin, or /// windows linker based on the argv[0] or -flavor option. static int lldMain(int argc, const char **argv, llvm::raw_ostream &stdoutOS, llvm::raw_ostream &stderrOS, bool exitEarly = true) { std::vector args(argv, argv + argc); + + initLLVM(); + switch (parseFlavor(args)) { case Gnu: if (isPETarget(args)) Index: lld/wasm/Driver.cpp =================================================================== --- lld/wasm/Driver.cpp +++ lld/wasm/Driver.cpp @@ -53,17 +53,6 @@ #undef OPTION }; -// This function is called on startup. We need this for LTO since -// LTO calls LLVM functions to compile bitcode files to native code. -// Technically this can be delayed until we read bitcode files, but -// we don't bother to do lazily because the initialization is fast. -static void initLLVM() { - InitializeAllTargets(); - InitializeAllTargetMCs(); - InitializeAllAsmPrinters(); - InitializeAllAsmParsers(); -} - class LinkerDriver { public: void link(ArrayRef argsArr); @@ -96,7 +85,6 @@ config = make(); symtab = make(); - initLLVM(); LinkerDriver().link(args); // Exit immediately if we don't need to return to the caller.