[lld] [LLD][COFF] Update nodefaultlibs after updating search paths (PR #128813)

via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 8 07:07:15 PST 2025


https://github.com/A2uria updated https://github.com/llvm/llvm-project/pull/128813

>From e95aef0f942fd7194b1933facfe28ea5e886151a Mon Sep 17 00:00:00 2001
From: A2uria <a2uria at qq.com>
Date: Wed, 26 Feb 2025 12:13:00 +0800
Subject: [PATCH 1/2] [LLD][COFF] Update nodefaultlibs after updating search
 paths

---
 lld/COFF/Driver.cpp | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index c20c049017018..bb64a5c0b9e96 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -786,6 +786,13 @@ void LinkerDriver::addWinSysRootLibSearchPaths() {
                                       path))
       searchPaths.push_back(saver().save(path));
   }
+
+  // Libraries specified by `/nodefaultlib:` may not be found in incomplete
+  // search paths before lld infers a machine type from input files.
+  std::set<std::string> noDefaultLibs;
+  for (const std::string &path : ctx.config.noDefaultLibs)
+    noDefaultLibs.insert(findLib(path).lower());
+  ctx.config.noDefaultLibs = noDefaultLibs;
 }
 
 // Parses LIB environment which contains a list of search paths.

>From 5caf763a332b39975406b49a7d00b329555c75d2 Mon Sep 17 00:00:00 2001
From: A2uria <a2uria at qq.com>
Date: Sat, 8 Mar 2025 20:22:12 +0800
Subject: [PATCH 2/2] [LLD][COFF] Add test for situations where /nodelaultlib
 doesn't take effect

---
 lld/test/COFF/nodefaultlib.test | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lld/test/COFF/nodefaultlib.test b/lld/test/COFF/nodefaultlib.test
index ca8f09bdfc76c..86cf7846a92be 100644
--- a/lld/test/COFF/nodefaultlib.test
+++ b/lld/test/COFF/nodefaultlib.test
@@ -36,3 +36,10 @@ MSVC stamps uppercase references in OBJ directives, thus ensure that passing low
 
 UPPERCASE-NOT: OLDNAMES
 UPPERCASE-NOT: LIBCMT
+
+# RUN: mkdir -p %T/VC/Tools/MSVC/14.43.34808/lib/x64
+# RUN: cp %p/Inputs/std64.lib %T/VC/Tools/MSVC/14.43.34808/lib/x64/libcmt.lib
+# RUN: env -u LIB lld-link /winsysroot:%T /out:%t.exe /entry:main /subsystem:console /nodefaultlib:libcmt /nodefaultlib:oldnames /verbose %S/Inputs/precomp.obj %S/Inputs/precomp-a.obj %S/Inputs/precomp-b.obj >& %t.log
+# RUN: FileCheck -check-prefix=CHECK4 %s < %t.log
+
+CHECK4-NOT: Reading {{.*}}LIBCMT.lib



More information about the llvm-commits mailing list