[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:48:10 PDT 2026
https://github.com/Dragnalith created https://github.com/llvm/llvm-project/pull/219928
### Problem
It is not possible to make `lld-link.exe` reproducible when the parent build system command is using an absolute path as `argv[0]`. (e.g Bazel)
### Description
`/pdbsourcepath:<my-path>` sets the root that relative PDB paths are expressed against.
The _cwd_ entry of the linker module's env block honours it, reporting `<my-path>` instead of the current directory absolute path used by the linker execution. Before this commit the same idea is not applied to the _exe_ entry when `argv[0]` is an absolute path, even when the `argy[0]` path is inside the current directory.
With this commit, the `/pdbsourcepath` applies to absolute `argv[0]` too. When `argv[0]` is absolute but sits inside the current directory, the current directory prefix is replaced by the `/pdbsourcepath` value, 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.
### Example
Let's say without `/pdbsourcepath` the env block looks like this:
```
cwd D:/project/build/
exe D:/project/build/bin/lld-link.exe
```
Before this commit, specifying `/pdbsourepath:.` will give the following block:
```
cwd .
exe D:/project/build/bin/lld-link.exe
```
With this commit you would get:
```
cwd .
exe ./bin/lld-link.exe
```
>From 49546cfc2af1f3a26908f4b62e9832a68171efc5 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 | 13 +++++++++++++
lld/test/COFF/pdb-linker-exe-path.test | 26 ++++++++++++++++++++++++++
2 files changed, 39 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..688eecc6964b2 100644
--- a/lld/COFF/PDB.cpp
+++ b/lld/COFF/PDB.cpp
@@ -1437,6 +1437,19 @@ void PDBLinker::addCommonLinkerModuleSymbols(
ebs.Fields.push_back(cwd);
ebs.Fields.push_back("exe");
SmallString<64> exe = ctx.config.argv[0];
+ // The field above reports /pdbsourcepath as the current directory, so a
+ // linker living under the real one is reported the same way. pdbMakeAbsolute
+ // leaves an already absolute path alone, and a linker outside that directory
+ // is not a path /pdbsourcepath describes.
+ 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