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

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 10 08:08:53 PDT 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/3] [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 9ef907a0895b783622a1fa4bb6c6f528b99abaf5 Mon Sep 17 00:00:00 2001
From: A2uria <a2uria at qq.com>
Date: Sun, 9 Mar 2025 10:36:55 +0800
Subject: [PATCH 2/3] [LLD][COFF] Add test for situations where /nodelaultlib
 doesn't take effect

---
 lld/test/COFF/Inputs/defaultlib.yaml | 24 ++++++++++++++++++++++++
 lld/test/COFF/nodefaultlib.test      |  5 +++++
 2 files changed, 29 insertions(+)
 create mode 100644 lld/test/COFF/Inputs/defaultlib.yaml

diff --git a/lld/test/COFF/Inputs/defaultlib.yaml b/lld/test/COFF/Inputs/defaultlib.yaml
new file mode 100644
index 0000000000000..5b61095d65822
--- /dev/null
+++ b/lld/test/COFF/Inputs/defaultlib.yaml
@@ -0,0 +1,24 @@
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .drectve
+    Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ]
+    Alignment:       1
+    SectionData:     202F44454641554C544C49423A64656661756C742E6C6962
+    SizeOfRawData:   24
+symbols:
+  - Name:            .drectve
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          24
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        2526009418
+      Number:          1
+...
diff --git a/lld/test/COFF/nodefaultlib.test b/lld/test/COFF/nodefaultlib.test
index ca8f09bdfc76c..a383c41a73423 100644
--- a/lld/test/COFF/nodefaultlib.test
+++ b/lld/test/COFF/nodefaultlib.test
@@ -36,3 +36,8 @@ MSVC stamps uppercase references in OBJ directives, thus ensure that passing low
 
 UPPERCASE-NOT: OLDNAMES
 UPPERCASE-NOT: LIBCMT
+
+# RUN: yaml2obj -o %T/defaultlib.obj %p/Inputs/defaultlib.yaml
+# RUN: mkdir -p %t.dir/sysroot/VC/Tools/MSVC/1.1.1.1/lib/x64
+# RUN: cp %p/Inputs/ret42.lib %t.dir/sysroot/VC/Tools/MSVC/1.1.1.1/lib/x64/default.lib
+# RUN: not lld-link /winsysroot:%t.dir/sysroot /out:%t.exe /entry:main /subsystem:console /nodefaultlib:default.lib %T/defaultlib.obj

>From 9028f86a508dc40c15fdaa582fbb6af171f14891 Mon Sep 17 00:00:00 2001
From: A2uria <a2uria at qq.com>
Date: Mon, 10 Mar 2025 23:08:42 +0800
Subject: [PATCH 3/3] [LLD][COFF] Check successful linking without
 /nodefaultlib

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

diff --git a/lld/test/COFF/nodefaultlib.test b/lld/test/COFF/nodefaultlib.test
index a383c41a73423..447acddab85a7 100644
--- a/lld/test/COFF/nodefaultlib.test
+++ b/lld/test/COFF/nodefaultlib.test
@@ -40,4 +40,6 @@ UPPERCASE-NOT: LIBCMT
 # RUN: yaml2obj -o %T/defaultlib.obj %p/Inputs/defaultlib.yaml
 # RUN: mkdir -p %t.dir/sysroot/VC/Tools/MSVC/1.1.1.1/lib/x64
 # RUN: cp %p/Inputs/ret42.lib %t.dir/sysroot/VC/Tools/MSVC/1.1.1.1/lib/x64/default.lib
+
+# RUN: lld-link /winsysroot:%t.dir/sysroot /out:%t.exe /entry:main /subsystem:console %T/defaultlib.obj
 # RUN: not lld-link /winsysroot:%t.dir/sysroot /out:%t.exe /entry:main /subsystem:console /nodefaultlib:default.lib %T/defaultlib.obj



More information about the llvm-commits mailing list