Skip to content

Commit f906fd5

Browse files
authored
[LLD] [COFF] Restore the current dir as the first entry in the search path (#67857)
Before af744f0, the first entry among the search paths was the empty string, indicating searching in (or starting from) the current directory. After af744f0, 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.
1 parent 7d7d9e4 commit f906fd5

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

lld/COFF/Driver.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -645,23 +645,19 @@ void LinkerDriver::addClangLibSearchPaths(const std::string &argv0) {
645645

646646
SmallString<128> libDir(rootDir);
647647
sys::path::append(libDir, "lib");
648-
// We need to prepend the paths here in order to make sure that we always
649-
// try to link the clang versions of the builtins over the ones supplied by
650-
// MSVC.
651-
searchPaths.insert(searchPaths.begin(), saver().save(libDir.str()));
652648

653649
// Add the resource dir library path
654650
SmallString<128> runtimeLibDir(rootDir);
655651
sys::path::append(runtimeLibDir, "lib", "clang",
656652
std::to_string(LLVM_VERSION_MAJOR), "lib");
657-
searchPaths.insert(searchPaths.begin(), saver().save(runtimeLibDir.str()));
658-
659653
// Resource dir + osname, which is hardcoded to windows since we are in the
660654
// COFF driver.
661655
SmallString<128> runtimeLibDirWithOS(runtimeLibDir);
662656
sys::path::append(runtimeLibDirWithOS, "windows");
663-
searchPaths.insert(searchPaths.begin(),
664-
saver().save(runtimeLibDirWithOS.str()));
657+
658+
searchPaths.push_back(saver().save(runtimeLibDirWithOS.str()));
659+
searchPaths.push_back(saver().save(runtimeLibDir.str()));
660+
searchPaths.push_back(saver().save(libDir.str()));
665661
}
666662

667663
void LinkerDriver::addWinSysRootLibSearchPaths() {
@@ -1564,12 +1560,13 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
15641560

15651561
// Construct search path list.
15661562
searchPaths.emplace_back("");
1563+
// Prefer the Clang provided builtins over the ones bundled with MSVC.
1564+
addClangLibSearchPaths(argsArr[0]);
15671565
for (auto *arg : args.filtered(OPT_libpath))
15681566
searchPaths.push_back(arg->getValue());
15691567
detectWinSysRoot(args);
15701568
if (!args.hasArg(OPT_lldignoreenv) && !args.hasArg(OPT_winsysroot))
15711569
addLibSearchPaths();
1572-
addClangLibSearchPaths(argsArr[0]);
15731570

15741571
// Handle /ignore
15751572
for (auto *arg : args.filtered(OPT_ignore)) {

lld/test/COFF/print-search-paths.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
# RUN: llvm-mc -triple i686-windows-msvc %s -filetype=obj -o %t_32.obj
55
# 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
66
# CHECK: Library search paths:
7+
# CHECK-NEXT: (cwd)
78
# CHECK-NEXT: [[CPATH:.*]]lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib{{[/\\]}}windows
89
# CHECK-NEXT: [[CPATH]]lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib
910
# CHECK-NEXT: [[CPATH]]lib
10-
# CHECK-NEXT: (cwd)
1111
# CHECK-NEXT: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}DIA SDK{{[/\\]}}lib{{[/\\]}}amd64
1212
# CHECK-NEXT: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}lib{{[/\\]}}x64
1313
# CHECK-NEXT: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}atlmfc{{[/\\]}}lib{{[/\\]}}x64
1414
# CHECK-NEXT: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}Windows Kits{{[/\\]}}10{{[/\\]}}Lib{{[/\\]}}10.0.1{{[/\\]}}ucrt{{[/\\]}}x64
1515
# CHECK-NEXT: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}Windows Kits{{[/\\]}}10{{[/\\]}}Lib{{[/\\]}}10.0.1{{[/\\]}}um{{[/\\]}}x64
1616
# X86: Library search paths:
17+
# X86-NEXT: (cwd)
1718
# X86-NEXT: [[CPATH:.*]]lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib{{[/\\]}}windows
1819
# X86-NEXT: [[CPATH]]lib{{[/\\]}}clang{{[/\\]}}{{[0-9]+}}{{[/\\]}}lib
1920
# X86-NEXT: [[CPATH]]lib
20-
# X86-NEXT: (cwd)
2121
# X86-NEXT: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}DIA SDK{{[/\\]}}lib
2222
# X86-NEXT: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}lib{{[/\\]}}x86
2323
# X86-NEXT: [[SYSROOT]]{{[/\\]}}sysroot{{[/\\]}}VC{{[/\\]}}Tools{{[/\\]}}MSVC{{[/\\]}}1.1.1.1{{[/\\]}}atlmfc{{[/\\]}}lib{{[/\\]}}x86

0 commit comments

Comments
 (0)