[llvm-branch-commits] [lld] b338a28 - [LLD] [COFF] Restore the current dir as the first entry in the search path (#67857)

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Oct 2 09:15:34 PDT 2023


Author: Martin Storsjö
Date: 2023-10-02T18:12:54+02:00
New Revision: b338a2830a2cc547566bc4bccf60ae78ed266299

URL: https://github.com/llvm/llvm-project/commit/b338a2830a2cc547566bc4bccf60ae78ed266299
DIFF: https://github.com/llvm/llvm-project/commit/b338a2830a2cc547566bc4bccf60ae78ed266299.diff

LOG: [LLD] [COFF] Restore the current dir as the first entry in the search path (#67857)

Before af744f0b84e2b6410be65277068b9033124c73b2, the first entry
among the search paths was the empty string, indicating searching
in (or starting from) the current directory. After
af744f0b84e2b6410be65277068b9033124c73b2, the toolchain/clang
specific lib directories were added at the head of the search path.

This would cause lookups of literal file names or relative paths
to match paths in the toolchain, if there are coincidental files
with similar names there, even if they would be find in the current
directory as well.

Change addClangLibSearchPaths to append to the list like all other
operations on searchPaths - but move the invocation of the
function to the right place in the sequence.

This fixes #67779.

(cherry picked from commit f906fd53b5ce12f07aec394ddf900c7f9f583405)

Added: 
    

Modified: 
    lld/COFF/Driver.cpp
    lld/test/COFF/print-search-paths.s

Removed: 
    


################################################################################
diff  --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 65309168440add2..e2f414f78ecb7bc 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -645,23 +645,19 @@ void LinkerDriver::addClangLibSearchPaths(const std::string &argv0) {
 
   SmallString<128> libDir(rootDir);
   sys::path::append(libDir, "lib");
-  // We need to prepend the paths here in order to make sure that we always
-  // try to link the clang versions of the builtins over the ones supplied by
-  // MSVC.
-  searchPaths.insert(searchPaths.begin(), saver().save(libDir.str()));
 
   // Add the resource dir library path
   SmallString<128> runtimeLibDir(rootDir);
   sys::path::append(runtimeLibDir, "lib", "clang",
                     std::to_string(LLVM_VERSION_MAJOR), "lib");
-  searchPaths.insert(searchPaths.begin(), saver().save(runtimeLibDir.str()));
-
   // Resource dir + osname, which is hardcoded to windows since we are in the
   // COFF driver.
   SmallString<128> runtimeLibDirWithOS(runtimeLibDir);
   sys::path::append(runtimeLibDirWithOS, "windows");
-  searchPaths.insert(searchPaths.begin(),
-                     saver().save(runtimeLibDirWithOS.str()));
+
+  searchPaths.push_back(saver().save(runtimeLibDirWithOS.str()));
+  searchPaths.push_back(saver().save(runtimeLibDir.str()));
+  searchPaths.push_back(saver().save(libDir.str()));
 }
 
 void LinkerDriver::addWinSysRootLibSearchPaths() {
@@ -1564,12 +1560,13 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
 
   // Construct search path list.
   searchPaths.emplace_back("");
+  // Prefer the Clang provided builtins over the ones bundled with MSVC.
+  addClangLibSearchPaths(argsArr[0]);
   for (auto *arg : args.filtered(OPT_libpath))
     searchPaths.push_back(arg->getValue());
   detectWinSysRoot(args);
   if (!args.hasArg(OPT_lldignoreenv) && !args.hasArg(OPT_winsysroot))
     addLibSearchPaths();
-  addClangLibSearchPaths(argsArr[0]);
 
   // Handle /ignore
   for (auto *arg : args.filtered(OPT_ignore)) {

diff  --git a/lld/test/COFF/print-search-paths.s b/lld/test/COFF/print-search-paths.s
index a8c8047fd74ae38..463cc55a793740c 100644
--- a/lld/test/COFF/print-search-paths.s
+++ b/lld/test/COFF/print-search-paths.s
@@ -4,20 +4,20 @@
 # 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-NEXT:   (cwd)
 # CHECK-NEXT:   [[CPATH:.*]]lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib{{[/\\]}}windows
 # CHECK-NEXT:   [[CPATH]]lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib
 # CHECK-NEXT:   [[CPATH]]lib
-# CHECK-NEXT:   (cwd)
 # CHECK-NEXT:   [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}DIA SDK{{[/\\]}}lib{{[/\\]}}amd64
 # CHECK-NEXT:   [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}lib{{[/\\]}}x64
 # CHECK-NEXT:   [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}atlmfc{{[/\\]}}lib{{[/\\]}}x64
 # CHECK-NEXT:   [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}Windows Kits{{[/\\]}}10{{[/\\]}}Lib{{[/\\]}}10.0.1{{[/\\]}}ucrt{{[/\\]}}x64
 # CHECK-NEXT:   [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}Windows Kits{{[/\\]}}10{{[/\\]}}Lib{{[/\\]}}10.0.1{{[/\\]}}um{{[/\\]}}x64
 # X86: Library search paths:
+# X86-NEXT:   (cwd)
 # X86-NEXT:   [[CPATH:.*]]lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib{{[/\\]}}windows
 # X86-NEXT:   [[CPATH]]lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib
 # X86-NEXT:   [[CPATH]]lib
-# X86-NEXT:   (cwd)
 # X86-NEXT:   [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}DIA SDK{{[/\\]}}lib
 # X86-NEXT:   [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}lib{{[/\\]}}x86
 # X86-NEXT:   [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}atlmfc{{[/\\]}}lib{{[/\\]}}x86


        


More information about the llvm-branch-commits mailing list