Skip to content

Commit 3f1985a

Browse files
committed
Auto merge of rust-lang#135034 - Noratrieb:strip-correctly, r=<try>
Pass objcopy args for stripping on OSX When `-Cstrip` was changed in rust-lang#131405 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`. fixes rust-lang#135028 try-job: aarch64-apple try-job: dist-aarch64-apple
2 parents 6ca6659 + 75a5355 commit 3f1985a

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+7-18
Original file line numberDiff line numberDiff line change
@@ -1104,14 +1104,14 @@ fn link_natively(
11041104
let stripcmd = "rust-objcopy";
11051105
match (strip, crate_type) {
11061106
(Strip::Debuginfo, _) => {
1107-
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &["-S"])
1107+
strip_with_external_utility(sess, stripcmd, out_filename, &["--strip-debug"])
11081108
}
11091109
// Per the manpage, `-x` is the maximum safe strip level for dynamic libraries. (#93988)
11101110
(Strip::Symbols, CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro) => {
1111-
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &["-x"])
1111+
strip_with_external_utility(sess, stripcmd, out_filename, &["-x"])
11121112
}
11131113
(Strip::Symbols, _) => {
1114-
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &[])
1114+
strip_with_external_utility(sess, stripcmd, out_filename, &["--strip-all"])
11151115
}
11161116
(Strip::None, _) => {}
11171117
}
@@ -1127,9 +1127,7 @@ fn link_natively(
11271127
let stripcmd = if !sess.host.is_like_solaris { "rust-objcopy" } else { "/usr/bin/strip" };
11281128
match strip {
11291129
// Always preserve the symbol table (-x).
1130-
Strip::Debuginfo => {
1131-
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &["-x"])
1132-
}
1130+
Strip::Debuginfo => strip_with_external_utility(sess, stripcmd, out_filename, &["-x"]),
11331131
// Strip::Symbols is handled via the --strip-all linker option.
11341132
Strip::Symbols => {}
11351133
Strip::None => {}
@@ -1145,15 +1143,11 @@ fn link_natively(
11451143
match strip {
11461144
Strip::Debuginfo => {
11471145
// FIXME: AIX's strip utility only offers option to strip line number information.
1148-
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &[
1149-
"-X32_64", "-l",
1150-
])
1146+
strip_with_external_utility(sess, stripcmd, out_filename, &["-X32_64", "-l"])
11511147
}
11521148
Strip::Symbols => {
11531149
// Must be noted this option might remove symbol __aix_rust_metadata and thus removes .info section which contains metadata.
1154-
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &[
1155-
"-X32_64", "-r",
1156-
])
1150+
strip_with_external_utility(sess, stripcmd, out_filename, &["-X32_64", "-r"])
11571151
}
11581152
Strip::None => {}
11591153
}
@@ -1166,12 +1160,7 @@ fn link_natively(
11661160
}
11671161
}
11681162

1169-
fn strip_symbols_with_external_utility(
1170-
sess: &Session,
1171-
util: &str,
1172-
out_filename: &Path,
1173-
options: &[&str],
1174-
) {
1163+
fn strip_with_external_utility(sess: &Session, util: &str, out_filename: &Path, options: &[&str]) {
11751164
let mut cmd = Command::new(util);
11761165
cmd.args(options);
11771166

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

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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, llvm_dwarfdump, llvm_nm, rustc};
6+
7+
fn main() {
8+
// We use DW_AT (the start of a DWARF attribute name) to check that some debuginfo is present.
9+
// Additionally, use -Cdebuginfo=2 to make the test independent of the amount of debuginfo
10+
// for std.
11+
12+
// -Cstrip=none should preserve symbols and debuginfo.
13+
rustc().arg("hello.rs").arg("-Cdebuginfo=2").arg("-Cstrip=none").run();
14+
llvm_nm().input(bin_name("hello")).run().assert_stdout_contains("hey_i_get_compiled");
15+
llvm_dwarfdump().input(bin_name("hello")).run().assert_stdout_contains("DW_AT");
16+
17+
// -Cstrip=debuginfo should preserve symbols and strip debuginfo.
18+
rustc().arg("hello.rs").arg("-Cdebuginfo=2").arg("-Cstrip=debuginfo").run();
19+
llvm_nm().input(bin_name("hello")).run().assert_stdout_contains("hey_i_get_compiled");
20+
llvm_dwarfdump().input(bin_name("hello")).run().assert_stdout_not_contains("DW_AT");
21+
22+
// -Cstrip=symbols should strip symbols and strip debuginfo.
23+
rustc().arg("hello.rs").arg("-Cdebuginfo=2").arg("-Cstrip=symbols").run();
24+
llvm_nm().input(bin_name("hello")).run().assert_stderr_contains("no symbols");
25+
llvm_dwarfdump().input(bin_name("hello")).run().assert_stdout_not_contains("DW_AT");
26+
}

0 commit comments

Comments
 (0)