1
Fork 0

Auto merge of #135034 - Noratrieb:strip-correctly, r=jieyouxu

Pass objcopy args for stripping on OSX

When `-Cstrip` was changed in #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 #135028

try-job: aarch64-apple
try-job: dist-aarch64-apple
This commit is contained in:
bors 2025-01-03 17:20:39 +00:00
commit 3f43b1a636
3 changed files with 57 additions and 18 deletions

View file

@ -1104,14 +1104,14 @@ fn link_natively(
let stripcmd = "rust-objcopy"; let stripcmd = "rust-objcopy";
match (strip, crate_type) { match (strip, crate_type) {
(Strip::Debuginfo, _) => { (Strip::Debuginfo, _) => {
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &["-S"]) strip_with_external_utility(sess, stripcmd, out_filename, &["--strip-debug"])
} }
// Per the manpage, `-x` is the maximum safe strip level for dynamic libraries. (#93988) // Per the manpage, `-x` is the maximum safe strip level for dynamic libraries. (#93988)
(Strip::Symbols, CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro) => { (Strip::Symbols, CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro) => {
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &["-x"]) strip_with_external_utility(sess, stripcmd, out_filename, &["-x"])
} }
(Strip::Symbols, _) => { (Strip::Symbols, _) => {
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &[]) strip_with_external_utility(sess, stripcmd, out_filename, &["--strip-all"])
} }
(Strip::None, _) => {} (Strip::None, _) => {}
} }
@ -1127,9 +1127,7 @@ fn link_natively(
let stripcmd = if !sess.host.is_like_solaris { "rust-objcopy" } else { "/usr/bin/strip" }; let stripcmd = if !sess.host.is_like_solaris { "rust-objcopy" } else { "/usr/bin/strip" };
match strip { match strip {
// Always preserve the symbol table (-x). // Always preserve the symbol table (-x).
Strip::Debuginfo => { Strip::Debuginfo => strip_with_external_utility(sess, stripcmd, out_filename, &["-x"]),
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &["-x"])
}
// Strip::Symbols is handled via the --strip-all linker option. // Strip::Symbols is handled via the --strip-all linker option.
Strip::Symbols => {} Strip::Symbols => {}
Strip::None => {} Strip::None => {}
@ -1145,15 +1143,11 @@ fn link_natively(
match strip { match strip {
Strip::Debuginfo => { Strip::Debuginfo => {
// FIXME: AIX's strip utility only offers option to strip line number information. // FIXME: AIX's strip utility only offers option to strip line number information.
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &[ strip_with_external_utility(sess, stripcmd, out_filename, &["-X32_64", "-l"])
"-X32_64", "-l",
])
} }
Strip::Symbols => { Strip::Symbols => {
// Must be noted this option might remove symbol __aix_rust_metadata and thus removes .info section which contains metadata. // Must be noted this option might remove symbol __aix_rust_metadata and thus removes .info section which contains metadata.
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &[ strip_with_external_utility(sess, stripcmd, out_filename, &["-X32_64", "-r"])
"-X32_64", "-r",
])
} }
Strip::None => {} Strip::None => {}
} }
@ -1166,12 +1160,7 @@ fn link_natively(
} }
} }
fn strip_symbols_with_external_utility( fn strip_with_external_utility(sess: &Session, util: &str, out_filename: &Path, options: &[&str]) {
sess: &Session,
util: &str,
out_filename: &Path,
options: &[&str],
) {
let mut cmd = Command::new(util); let mut cmd = Command::new(util);
cmd.args(options); cmd.args(options);

View file

@ -0,0 +1,8 @@
fn main() {
hey_i_get_compiled();
}
#[inline(never)]
fn hey_i_get_compiled() {
println!("Hi! Do or do not strip me, your choice.");
}

View file

@ -0,0 +1,42 @@
//@ ignore-windows Windows does not actually strip
// Test that -Cstrip correctly strips/preserves debuginfo and symbols.
use run_make_support::{bin_name, is_darwin, llvm_dwarfdump, llvm_nm, rustc};
fn main() {
// We use DW_ (the start of any DWARF name) to check that some debuginfo is present.
let dwarf_indicator = "DW_";
let test_symbol = "hey_i_get_compiled";
let binary = &bin_name("hello");
// Avoid checking debuginfo on darwin, because it is not actually affected by strip.
// Darwin *never* puts debuginfo in the main binary (-Csplit-debuginfo=off just removes it),
// so we never actually have any debuginfo in there, so we can't check that it's present.
let do_debuginfo_check = !is_darwin();
// Additionally, use -Cdebuginfo=2 to make the test independent of the amount of debuginfo
// for std.
// -Cstrip=none should preserve symbols and debuginfo.
rustc().arg("hello.rs").arg("-Cdebuginfo=2").arg("-Cstrip=none").run();
llvm_nm().input(binary).run().assert_stdout_contains(test_symbol);
if do_debuginfo_check {
llvm_dwarfdump().input(binary).run().assert_stdout_contains(dwarf_indicator);
}
// -Cstrip=debuginfo should preserve symbols and strip debuginfo.
rustc().arg("hello.rs").arg("-Cdebuginfo=2").arg("-Cstrip=debuginfo").run();
llvm_nm().input(binary).run().assert_stdout_contains(test_symbol);
if do_debuginfo_check {
llvm_dwarfdump().input(binary).run().assert_stdout_not_contains(dwarf_indicator);
}
// -Cstrip=symbols should strip symbols and strip debuginfo.
rustc().arg("hello.rs").arg("-Cdebuginfo=2").arg("-Cstrip=symbols").run();
llvm_nm().input(binary).run().assert_stderr_not_contains(test_symbol);
if do_debuginfo_check {
llvm_dwarfdump().input(binary).run().assert_stdout_not_contains(dwarf_indicator);
}
}