diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp index b47b3ffec0a90..5a3db54846318 100644 --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -591,6 +591,14 @@ std::optional LinkerDriver::findLibIfNew(StringRef filename) { return path; } +void LinkerDriver::setMachine(MachineTypes machine) { + assert(ctx.config.machine == IMAGE_FILE_MACHINE_UNKNOWN); + assert(machine != IMAGE_FILE_MACHINE_UNKNOWN); + + ctx.config.machine = machine; + addWinSysRootLibSearchPaths(); +} + void LinkerDriver::detectWinSysRoot(const opt::InputArgList &Args) { IntrusiveRefCntPtr VFS = vfs::getRealFileSystem(); @@ -1887,10 +1895,10 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { { llvm::TimeTraceScope timeScope2("Machine arg"); if (auto *arg = args.getLastArg(OPT_machine)) { - config->machine = getMachineType(arg->getValue()); - if (config->machine == IMAGE_FILE_MACHINE_UNKNOWN) + MachineTypes machine = getMachineType(arg->getValue()); + if (machine == IMAGE_FILE_MACHINE_UNKNOWN) Fatal(ctx) << "unknown /machine argument: " << arg->getValue(); - addWinSysRootLibSearchPaths(); + setMachine(machine); } } @@ -2298,8 +2306,7 @@ void LinkerDriver::linkerMain(ArrayRef argsArr) { // not we assume x64. if (config->machine == IMAGE_FILE_MACHINE_UNKNOWN) { Warn(ctx) << "/machine is not specified. x64 is assumed"; - config->machine = AMD64; - addWinSysRootLibSearchPaths(); + setMachine(AMD64); } config->wordsize = config->is64() ? 8 : 4; diff --git a/lld/COFF/Driver.h b/lld/COFF/Driver.h index 3889feb7511c0..e94a961953581 100644 --- a/lld/COFF/Driver.h +++ b/lld/COFF/Driver.h @@ -80,9 +80,7 @@ class LinkerDriver { void linkerMain(llvm::ArrayRef args); - // Adds various search paths based on the sysroot. Must only be called once - // config->machine has been set. - void addWinSysRootLibSearchPaths(); + void setMachine(llvm::COFF::MachineTypes machine); void addClangLibSearchPaths(const std::string &argv0); @@ -116,6 +114,10 @@ class LinkerDriver { // Determines the location of the sysroot based on `args`, environment, etc. void detectWinSysRoot(const llvm::opt::InputArgList &args); + // Adds various search paths based on the sysroot. Must only be called once + // config->machine has been set. + void addWinSysRootLibSearchPaths(); + // Symbol names are mangled by prepending "_" on x86. StringRef mangle(StringRef sym); diff --git a/lld/COFF/SymbolTable.cpp b/lld/COFF/SymbolTable.cpp index 942e7ceda1ebe..d6cf10756e296 100644 --- a/lld/COFF/SymbolTable.cpp +++ b/lld/COFF/SymbolTable.cpp @@ -95,8 +95,7 @@ void SymbolTable::addFile(InputFile *file) { if (ctx.config.machine == IMAGE_FILE_MACHINE_UNKNOWN && mt != IMAGE_FILE_MACHINE_UNKNOWN) { ctx.config.machineInferred = true; - ctx.config.machine = mt; - ctx.driver.addWinSysRootLibSearchPaths(); + ctx.driver.setMachine(mt); } ctx.driver.parseDirectives(file);