diff --git a/lld/COFF/Driver.h b/lld/COFF/Driver.h --- a/lld/COFF/Driver.h +++ b/lld/COFF/Driver.h @@ -84,6 +84,8 @@ // config->machine has been set. void addWinSysRootLibSearchPaths(); + void addClangLibSearchPaths(const std::string& argv0); + // Used by the resolver to parse .drectve section contents. void parseDirectives(InputFile *file); diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -483,8 +483,7 @@ return filename; }; - bool hasPathSep = (filename.find_first_of("/\\") != StringRef::npos); - if (hasPathSep) + if (sys::path::is_absolute(filename)) return getFilename(filename); bool hasExt = filename.contains('.'); for (StringRef dir : searchPaths) { @@ -638,6 +637,26 @@ } } +void LinkerDriver::addClangLibSearchPaths(const std::string& argv0) { + std::string lldBinary = sys::fs::getMainExecutable(argv0.c_str(), nullptr); + SmallString<128> binDir(lldBinary); + sys::path::remove_filename(binDir); // remove lld-link.exe + StringRef rootDir = sys::path::parent_path(binDir); // remove 'bin' + + SmallString<128> libDir(rootDir); + sys::path::append(libDir, "lib"); + + searchPaths.push_back(saver().save(std::string(libDir))); + + SmallString<128> runtimeLibDir(rootDir); + sys::path::append(runtimeLibDir, "lib", "clang", std::to_string(LLVM_VERSION_MAJOR), "lib"); + searchPaths.push_back(saver().save(std::string(runtimeLibDir))); + + SmallString<128> runtimeLibDirWithOS(runtimeLibDir); + sys::path::append(runtimeLibDirWithOS, "windows"); + searchPaths.push_back(saver().save(std::string(runtimeLibDirWithOS))); +} + void LinkerDriver::addWinSysRootLibSearchPaths() { if (!diaPath.empty()) { // The DIA SDK always uses the legacy vc arch, even in new MSVC versions. @@ -1545,6 +1564,8 @@ if (!args.hasArg(OPT_lldignoreenv) && !args.hasArg(OPT_winsysroot)) addLibSearchPaths(); + addClangLibSearchPaths(argsArr[0]); + // Handle /ignore for (auto *arg : args.filtered(OPT_ignore)) { SmallVector vec; @@ -2076,7 +2097,7 @@ // Handle /RELEASE if (args.hasArg(OPT_release)) config->writeCheckSum = true; - + // Handle /safeseh, x86 only, on by default, except for mingw. if (config->machine == I386) { config->safeSEH = args.hasFlag(OPT_safeseh, OPT_safeseh_no, !config->mingw); diff --git a/lld/test/COFF/print-search-paths.s b/lld/test/COFF/print-search-paths.s --- a/lld/test/COFF/print-search-paths.s +++ b/lld/test/COFF/print-search-paths.s @@ -1,19 +1,22 @@ # REQUIRES: x86 # RUN: llvm-mc -triple x86_64-windows-msvc %s -filetype=obj -o %t.obj -# RUN: lld-link -safeseh:no /dll /noentry /winsysroot:%t.dir/sysroot /vctoolsversion:1.1.1.1 /winsdkversion:10.0.1 %t.obj -print-search-paths | FileCheck -DSYSROOT=%t.dir %s +# RUN: lld-link -safeseh:no /dll /noentry /winsysroot:%t.dir/sysroot /vctoolsversion:1.1.1.1 /winsdkversion:10.0.1 %t.obj -print-search-paths | sort | FileCheck -DSYSROOT=%t.dir %s # RUN: llvm-mc -triple i686-windows-msvc %s -filetype=obj -o %t_32.obj -# RUN: lld-link -safeseh:no /dll /noentry /winsysroot:%t.dir/sysroot /vctoolsversion:1.1.1.1 /winsdkversion:10.0.1 %t_32.obj -print-search-paths | FileCheck -DSYSROOT=%t.dir -check-prefix=X86 %s -# CHECK: Library search paths: -# CHECK: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}lib{{[/\\]}}x64 +# RUN: lld-link -safeseh:no /dll /noentry /winsysroot:%t.dir/sysroot /vctoolsversion:1.1.1.1 /winsdkversion:10.0.1 %t_32.obj -print-search-paths | sort | FileCheck -DSYSROOT=%t.dir -check-prefix=X86 %s # CHECK: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}atlmfc{{[/\\]}}lib{{[/\\]}}x64 +# CHECK: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}lib{{[/\\]}}x64 # CHECK: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}Windows Kits{{[/\\]}}10{{[/\\]}}Lib{{[/\\]}}10.0.1{{[/\\]}}ucrt{{[/\\]}}x64 # CHECK: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}Windows Kits{{[/\\]}}10{{[/\\]}}Lib{{[/\\]}}10.0.1{{[/\\]}}um{{[/\\]}}x64 -# X86: Library search paths: -# X86: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}lib{{[/\\]}}x86 +# CHECK: lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib +# CHECK: lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib{{[/\\]}}windows +# CHECK: Library search paths: # X86: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}atlmfc{{[/\\]}}lib{{[/\\]}}x86 +# X86: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}lib{{[/\\]}}x86 # X86: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}Windows Kits{{[/\\]}}10{{[/\\]}}Lib{{[/\\]}}10.0.1{{[/\\]}}ucrt{{[/\\]}}x86 # X86: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}Windows Kits{{[/\\]}}10{{[/\\]}}Lib{{[/\\]}}10.0.1{{[/\\]}}um{{[/\\]}}x86 - +# X86: lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib +# X86: lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib{{[/\\]}}windows +# X86: Library search paths: .text .globl _main