Skip to content

Commit c5b5713

Browse files
committed
Auto merge of #121297 - michaelwoerister:set-pdb-alt-path, r=wesleywiser
link.exe: Don't embed full path to PDB file in binary. This PR makes `rustc` unconditionally pass `/PDBALTPATH:%_PDB%` to MSVC-style linkers, causing the linker to only embed the filename of the PDB in the binary instead of the full path. This will help implement the [trim-paths RFC](#111540) for `*-msvc` targets. Passing `/PDBALTPATH:%_PDB%` to the linker is already done by many projects that need reproducible builds and [debugger's should still be able to find the PDB](https://learn.microsoft.com/cpp/build/reference/pdbpath) if it is in the same directory as the binary. r? `@ghost` Fixes #87825
2 parents accc516 + 0a094ba commit c5b5713

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+9
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,15 @@ impl<'a> Linker for MsvcLinker<'a> {
929929
// from the CodeView line tables in the object files.
930930
self.cmd.arg("/DEBUG");
931931

932+
// Default to emitting only the file name of the PDB file into
933+
// the binary instead of the full path. Emitting the full path
934+
// may leak private information (such as user names).
935+
// See https://github.com/rust-lang/rust/issues/87825.
936+
//
937+
// This default behavior can be overridden by explicitly passing
938+
// `-Clink-arg=/PDBALTPATH:...` to rustc.
939+
self.cmd.arg("/PDBALTPATH:%_PDB%");
940+
932941
// This will cause the Microsoft linker to embed .natvis info into the PDB file
933942
let natvis_dir_path = self.sess.sysroot.join("lib\\rustlib\\etc");
934943
if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) {

tests/run-make/pdb-alt-path/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
include ../tools.mk
2+
3+
# only-windows-msvc
4+
5+
all:
6+
# Test that we don't have the full path to the PDB file in the binary
7+
$(RUSTC) main.rs -g --crate-name my_crate_name --crate-type bin -Cforce-frame-pointers
8+
$(CGREP) "my_crate_name.pdb" < $(TMPDIR)/my_crate_name.exe
9+
$(CGREP) -v "\\my_crate_name.pdb" < $(TMPDIR)/my_crate_name.exe
10+
11+
# Test that backtraces still can find debuginfo by checking that they contain symbol names and
12+
# source locations.
13+
$(TMPDIR)/my_crate_name.exe &> $(TMPDIR)/backtrace.txt
14+
$(CGREP) "my_crate_name::fn_in_backtrace" < $(TMPDIR)/backtrace.txt
15+
$(CGREP) "main.rs:15" < $(TMPDIR)/backtrace.txt
16+
17+
# Test that explicitly passed `-Clink-arg=/PDBALTPATH:...` is respected
18+
$(RUSTC) main.rs -g --crate-name my_crate_name --crate-type bin -Clink-arg=/PDBALTPATH:abcdefg.pdb -Cforce-frame-pointers
19+
$(CGREP) "abcdefg.pdb" < $(TMPDIR)/my_crate_name.exe
20+
$(CGREP) -v "my_crate_name.pdb" < $(TMPDIR)/my_crate_name.exe

tests/run-make/pdb-alt-path/main.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// The various #[inline(never)] annotations and std::hint::black_box calls are
2+
// an attempt to make unwinding as non-flaky as possible on i686-pc-windows-msvc.
3+
4+
#[inline(never)]
5+
fn generate_backtrace(x: &u32) {
6+
std::hint::black_box(x);
7+
let bt = std::backtrace::Backtrace::force_capture();
8+
println!("{}", bt);
9+
std::hint::black_box(x);
10+
}
11+
12+
#[inline(never)]
13+
fn fn_in_backtrace(x: &u32) {
14+
std::hint::black_box(x);
15+
generate_backtrace(x);
16+
std::hint::black_box(x);
17+
}
18+
19+
fn main() {
20+
let x = &41;
21+
std::hint::black_box(x);
22+
fn_in_backtrace(x);
23+
std::hint::black_box(x);
24+
}

0 commit comments

Comments
 (0)