Skip to content

Commit 70246e6

Browse files
Noratriebpietroalbini
authored andcommitted
Pass objcopy args for stripping on OSX
When `-Cstrip` was changed to use the bundled rust-objcopy instead of /usr/bin/strip on OSX, strip-like arguments were preserved. But strip and objcopy are, while being the same binary, different, they have different defaults depending on which binary they are. Notably, strip strips everything by default, and objcopy doesn't strip anything by default. Additionally, `-S` actually means `--strip-all`, so debuginfo stripped everything and symbols didn't strip anything. We now correctly pass `--strip-debug` and `--strip-all`.
1 parent 953a5ca commit 70246e6

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1117,14 +1117,14 @@ fn link_natively(
11171117
let stripcmd = "rust-objcopy";
11181118
match (strip, crate_type) {
11191119
(Strip::Debuginfo, _) => {
1120-
strip_symbols_with_external_utility(sess, stripcmd, out_filename, Some("-S"))
1120+
strip_symbols_with_external_utility(sess, stripcmd, out_filename, Some("--strip-debug"))
11211121
}
11221122
// Per the manpage, `-x` is the maximum safe strip level for dynamic libraries. (#93988)
11231123
(Strip::Symbols, CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro) => {
11241124
strip_symbols_with_external_utility(sess, stripcmd, out_filename, Some("-x"))
11251125
}
11261126
(Strip::Symbols, _) => {
1127-
strip_symbols_with_external_utility(sess, stripcmd, out_filename, None)
1127+
strip_symbols_with_external_utility(sess, stripcmd, out_filename, Some("--strip-all"))
11281128
}
11291129
(Strip::None, _) => {}
11301130
}

tests/run-make/strip/hello.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
hey_i_get_compiled();
3+
}
4+
5+
#[inline(never)]
6+
fn hey_i_get_compiled() {
7+
println!("Hi! Do or do not strip me, your choice.");
8+
}

tests/run-make/strip/rmake.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//@ ignore-windows Windows does not actually strip
2+
3+
// Test that -Cstrip correctly strips/preserves debuginfo and symbols.
4+
5+
use run_make_support::{bin_name, is_darwin, llvm_dwarfdump, llvm_nm, rustc};
6+
7+
fn main() {
8+
// We use DW_ (the start of any DWARF name) to check that some debuginfo is present.
9+
let dwarf_indicator = "DW_";
10+
11+
let test_symbol = "hey_i_get_compiled";
12+
let binary = &bin_name("hello");
13+
14+
// Avoid checking debuginfo on darwin, because it is not actually affected by strip.
15+
// Darwin *never* puts debuginfo in the main binary (-Csplit-debuginfo=off just removes it),
16+
// so we never actually have any debuginfo in there, so we can't check that it's present.
17+
let do_debuginfo_check = !is_darwin();
18+
19+
// Additionally, use -Cdebuginfo=2 to make the test independent of the amount of debuginfo
20+
// for std.
21+
22+
// -Cstrip=none should preserve symbols and debuginfo.
23+
rustc().arg("hello.rs").arg("-Cdebuginfo=2").arg("-Cstrip=none").run();
24+
llvm_nm().input(binary).run().assert_stdout_contains(test_symbol);
25+
if do_debuginfo_check {
26+
llvm_dwarfdump().input(binary).run().assert_stdout_contains(dwarf_indicator);
27+
}
28+
29+
// -Cstrip=debuginfo should preserve symbols and strip debuginfo.
30+
rustc().arg("hello.rs").arg("-Cdebuginfo=2").arg("-Cstrip=debuginfo").run();
31+
llvm_nm().input(binary).run().assert_stdout_contains(test_symbol);
32+
if do_debuginfo_check {
33+
llvm_dwarfdump().input(binary).run().assert_stdout_not_contains(dwarf_indicator);
34+
}
35+
36+
// -Cstrip=symbols should strip symbols and strip debuginfo.
37+
rustc().arg("hello.rs").arg("-Cdebuginfo=2").arg("-Cstrip=symbols").run();
38+
llvm_nm().input(binary).run().assert_stderr_not_contains(test_symbol);
39+
if do_debuginfo_check {
40+
llvm_dwarfdump().input(binary).run().assert_stdout_not_contains(dwarf_indicator);
41+
}
42+
}

0 commit comments

Comments
 (0)