[lld] [LLD][COFF] Apply /pdbsourcepath to the linker's own path (PR #219928)
Marc Delorme via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 31 02:54:17 PDT 2026
https://github.com/Dragnalith updated https://github.com/llvm/llvm-project/pull/219928
>From 29732ed5bcd82d1d116d4a547416bf47d961b7b4 Mon Sep 17 00:00:00 2001
From: Marc Delorme <delormemarco at gmail.com>
Date: Sun, 30 Aug 2026 21:14:40 +0900
Subject: [PATCH] [LLD][COFF] Apply /pdbsourcepath to the linker's own path
/pdbsourcepath sets the root that PDB paths are expressed against. The cwd
entry of the linker module's env block honours it, reporting that root
instead of the directory the link ran in. The exe entry does not:
pdbMakeAbsolute leaves an absolute argv[0] alone.
Apply the option to exe too. When argv[0] is absolute but sits inside the
current directory, that prefix is replaced by the /pdbsourcepath root, so exe
is expressed like the rest of the PDB.
Some build systems spawn the linker by absolute path. Without this, exe keeps
that directory, and under /Brepro the same inputs linked from two directories
produce two different binaries.
---
lld/COFF/PDB.cpp | 9 +++++++++
lld/test/COFF/pdb-linker-exe-path.test | 26 ++++++++++++++++++++++++++
2 files changed, 35 insertions(+)
create mode 100644 lld/test/COFF/pdb-linker-exe-path.test
diff --git a/lld/COFF/PDB.cpp b/lld/COFF/PDB.cpp
index fd54d20da3ccd..de5c2f1cdfa8c 100644
--- a/lld/COFF/PDB.cpp
+++ b/lld/COFF/PDB.cpp
@@ -1437,6 +1437,15 @@ void PDBLinker::addCommonLinkerModuleSymbols(
ebs.Fields.push_back(cwd);
ebs.Fields.push_back("exe");
SmallString<64> exe = ctx.config.argv[0];
+ if (!ctx.config.pdbSourcePath.empty()) {
+ SmallString<64> currentPath;
+ if (!sys::fs::current_path(currentPath)) {
+ sys::path::native(exe);
+ sys::path::native(currentPath);
+ sys::path::remove_dots(exe, /*remove_dot_dot=*/true);
+ sys::path::replace_path_prefix(exe, currentPath, ".");
+ }
+ }
pdbMakeAbsolute(exe);
ebs.Fields.push_back(exe);
ebs.Fields.push_back("pdb");
diff --git a/lld/test/COFF/pdb-linker-exe-path.test b/lld/test/COFF/pdb-linker-exe-path.test
new file mode 100644
index 0000000000000..4fafcffe08c0a
--- /dev/null
+++ b/lld/test/COFF/pdb-linker-exe-path.test
@@ -0,0 +1,26 @@
+Check that /pdbsourcepath reaches the linker's own path in the linker module's
+env block, so a build that spawns lld-link by absolute path from inside the
+directory it is normalizing away does not record where it ran.
+
+RUN: rm -rf %t
+RUN: mkdir %t
+RUN: cp lld-link %t/lld-link
+RUN: yaml2obj %p/Inputs/pdb1.yaml -o %t/pdb1.obj
+RUN: yaml2obj %p/Inputs/pdb2.yaml -o %t/pdb2.obj
+RUN: cd %t
+
+The linker is named by an absolute path, the way a build system spawns it.
+
+RUN: %t/lld-link /debug /pdbsourcepath:. /entry:main /nodefaultlib \
+RUN: /out:out.exe /pdb:out.pdb pdb1.obj pdb2.obj
+RUN: llvm-pdbutil dump -symbols out.pdb | FileCheck %s
+
+RUN: rm -f %t/lld-link
+
+CHECK: S_ENVBLOCK
+CHECK-NEXT: - cwd
+CHECK-NEXT: - .
+CHECK-NEXT: - exe
+CHECK-NEXT: - lld-link
+CHECK-NEXT: - pdb
+CHECK-NEXT: - out.pdb
More information about the llvm-commits
mailing list