Skip to content

Commit e82f46a

Browse files
committed
Ignore -C strip on MSVC
1 parent 0fd5712 commit e82f46a

File tree

4 files changed

+103
-57
lines changed

4 files changed

+103
-57
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+35-42
Original file line numberDiff line numberDiff line change
@@ -922,52 +922,45 @@ impl<'a> Linker for MsvcLinker<'a> {
922922
}
923923
}
924924

925-
fn debuginfo(&mut self, strip: Strip, natvis_debugger_visualizers: &[PathBuf]) {
926-
match strip {
927-
Strip::None => {
928-
// This will cause the Microsoft linker to generate a PDB file
929-
// from the CodeView line tables in the object files.
930-
self.cmd.arg("/DEBUG");
931-
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-
941-
// This will cause the Microsoft linker to embed .natvis info into the PDB file
942-
let natvis_dir_path = self.sess.sysroot.join("lib\\rustlib\\etc");
943-
if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) {
944-
for entry in natvis_dir {
945-
match entry {
946-
Ok(entry) => {
947-
let path = entry.path();
948-
if path.extension() == Some("natvis".as_ref()) {
949-
let mut arg = OsString::from("/NATVIS:");
950-
arg.push(path);
951-
self.cmd.arg(arg);
952-
}
953-
}
954-
Err(error) => {
955-
self.sess.dcx().emit_warn(errors::NoNatvisDirectory { error });
956-
}
925+
fn debuginfo(&mut self, _strip: Strip, natvis_debugger_visualizers: &[PathBuf]) {
926+
// This will cause the Microsoft linker to generate a PDB file
927+
// from the CodeView line tables in the object files.
928+
self.cmd.arg("/DEBUG");
929+
930+
// Default to emitting only the file name of the PDB file into
931+
// the binary instead of the full path. Emitting the full path
932+
// may leak private information (such as user names).
933+
// See https://github.com/rust-lang/rust/issues/87825.
934+
//
935+
// This default behavior can be overridden by explicitly passing
936+
// `-Clink-arg=/PDBALTPATH:...` to rustc.
937+
self.cmd.arg("/PDBALTPATH:%_PDB%");
938+
939+
// This will cause the Microsoft linker to embed .natvis info into the PDB file
940+
let natvis_dir_path = self.sess.sysroot.join("lib\\rustlib\\etc");
941+
if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) {
942+
for entry in natvis_dir {
943+
match entry {
944+
Ok(entry) => {
945+
let path = entry.path();
946+
if path.extension() == Some("natvis".as_ref()) {
947+
let mut arg = OsString::from("/NATVIS:");
948+
arg.push(path);
949+
self.cmd.arg(arg);
957950
}
958951
}
959-
}
960-
961-
// This will cause the Microsoft linker to embed .natvis info for all crates into the PDB file
962-
for path in natvis_debugger_visualizers {
963-
let mut arg = OsString::from("/NATVIS:");
964-
arg.push(path);
965-
self.cmd.arg(arg);
952+
Err(error) => {
953+
self.sess.dcx().emit_warn(errors::NoNatvisDirectory { error });
954+
}
966955
}
967956
}
968-
Strip::Debuginfo | Strip::Symbols => {
969-
self.cmd.arg("/DEBUG:NONE");
970-
}
957+
}
958+
959+
// This will cause the Microsoft linker to embed .natvis info for all crates into the PDB file
960+
for path in natvis_debugger_visualizers {
961+
let mut arg = OsString::from("/NATVIS:");
962+
arg.push(path);
963+
self.cmd.arg(arg);
971964
}
972965
}
973966

src/doc/rustc/src/codegen-options/index.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -548,22 +548,23 @@ data from binaries during linking.
548548

549549
Supported values for this option are:
550550

551-
- `none` - debuginfo and symbols (if they exist) are copied to the produced
552-
binary or separate files depending on the target (e.g. `.pdb` files in case
553-
of MSVC).
551+
- `none` - debuginfo and symbols are not modified.
554552
- `debuginfo` - debuginfo sections and debuginfo symbols from the symbol table
555-
section are stripped at link time and are not copied to the produced binary
556-
or separate files. This should leave backtraces mostly-intact but may make
557-
using a debugger like gdb or lldb ineffectual.
558-
- `symbols` - same as `debuginfo`, but the rest of the symbol table section is stripped as well,
559-
depending on platform support. On platforms which depend on this symbol table for backtraces,
560-
profiling, and similar, this can affect them so negatively as to make the trace incomprehensible.
561-
Programs which may be combined with others, such as CLI pipelines and developer tooling,
562-
or even anything which wants crash-reporting, should usually avoid `-Cstrip=symbols`.
563-
564-
Note that, at any level, removing debuginfo only necessarily impacts "friendly" introspection.
565-
`-Cstrip` cannot be relied on as a meaningful security or obfuscation measure, as disassemblers
566-
and decompilers can extract considerable information even in the absence of symbols.
553+
section are stripped at link time and are not copied to the produced binary.
554+
This should leave backtraces mostly-intact but may make using a debugger like
555+
gdb or lldb ineffectual. Prior to 1.79, this unintentionally disabled the
556+
generation of `*.pdb` files on MSVC, resulting in the absence of symbols.
557+
- `symbols` - same as `debuginfo`, but the rest of the symbol table section is
558+
stripped as well, depending on platform support. On platforms which depend on
559+
this symbol table for backtraces, profiling, and similar, this can affect
560+
them so negatively as to make the trace incomprehensible. Programs which may
561+
be combined with others, such as CLI pipelines and developer tooling, or even
562+
anything which wants crash-reporting, should usually avoid `-Cstrip=symbols`.
563+
564+
Note that, at any level, removing debuginfo only necessarily impacts "friendly"
565+
introspection. `-Cstrip` cannot be relied on as a meaningful security or
566+
obfuscation measure, as disassemblers and decompilers can extract considerable
567+
information even in the absence of symbols.
567568

568569
## symbol-mangling-version
569570

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ compile-flags: -C strip=debuginfo
2+
//@ only-msvc
3+
//@ run-pass
4+
5+
use std::path::Path;
6+
7+
pub fn is_related_pdb<P: AsRef<Path>>(path: &P, exe: &P) -> bool {
8+
let (exe, path) = (exe.as_ref(), path.as_ref());
9+
10+
path.extension()
11+
.map(|x| x.to_ascii_lowercase())
12+
.is_some_and(|x| x == "pdb")
13+
&& path.file_stem() == exe.file_stem()
14+
}
15+
16+
pub fn main() {
17+
let curr_exe = std::env::current_exe().unwrap();
18+
let curr_dir = curr_exe.parent().unwrap();
19+
20+
let entries = std::fs::read_dir(curr_dir).unwrap();
21+
22+
assert!(entries
23+
.map_while(|x| x.ok())
24+
.find(|x| is_related_pdb(&x.path(), &curr_exe))
25+
.is_some());
26+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ compile-flags: -C strip=symbols
2+
//@ only-msvc
3+
//@ run-pass
4+
5+
use std::path::Path;
6+
7+
pub fn is_related_pdb<P: AsRef<Path>>(path: &P, exe: &P) -> bool {
8+
let (exe, path) = (exe.as_ref(), path.as_ref());
9+
10+
path.extension()
11+
.map(|x| x.to_ascii_lowercase())
12+
.is_some_and(|x| x == "pdb")
13+
&& path.file_stem() == exe.file_stem()
14+
}
15+
16+
pub fn main() {
17+
let curr_exe = std::env::current_exe().unwrap();
18+
let curr_dir = curr_exe.parent().unwrap();
19+
20+
let entries = std::fs::read_dir(curr_dir).unwrap();
21+
22+
assert!(entries
23+
.map_while(|x| x.ok())
24+
.find(|x| is_related_pdb(&x.path(), &curr_exe))
25+
.is_some());
26+
}

0 commit comments

Comments
 (0)