Skip to content

Pass objcopy args for stripping on OSX #135034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,14 +1104,14 @@ fn link_natively(
let stripcmd = "rust-objcopy";
match (strip, crate_type) {
(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)
(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_with_external_utility(sess, stripcmd, out_filename, &[])
strip_with_external_utility(sess, stripcmd, out_filename, &["--strip-all"])
}
(Strip::None, _) => {}
}
Expand All @@ -1127,9 +1127,7 @@ fn link_natively(
let stripcmd = if !sess.host.is_like_solaris { "rust-objcopy" } else { "/usr/bin/strip" };
match strip {
// Always preserve the symbol table (-x).
Strip::Debuginfo => {
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &["-x"])
}
Strip::Debuginfo => strip_with_external_utility(sess, stripcmd, out_filename, &["-x"]),
// Strip::Symbols is handled via the --strip-all linker option.
Strip::Symbols => {}
Strip::None => {}
Expand All @@ -1145,15 +1143,11 @@ fn link_natively(
match strip {
Strip::Debuginfo => {
// FIXME: AIX's strip utility only offers option to strip line number information.
strip_symbols_with_external_utility(sess, stripcmd, out_filename, &[
"-X32_64", "-l",
])
strip_with_external_utility(sess, stripcmd, out_filename, &["-X32_64", "-l"])
}
Strip::Symbols => {
// 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, &[
"-X32_64", "-r",
])
strip_with_external_utility(sess, stripcmd, out_filename, &["-X32_64", "-r"])
}
Strip::None => {}
}
Expand All @@ -1166,12 +1160,7 @@ fn link_natively(
}
}

fn strip_symbols_with_external_utility(
sess: &Session,
util: &str,
out_filename: &Path,
options: &[&str],
) {
fn strip_with_external_utility(sess: &Session, util: &str, out_filename: &Path, options: &[&str]) {
let mut cmd = Command::new(util);
cmd.args(options);

Expand Down
8 changes: 8 additions & 0 deletions tests/run-make/strip/hello.rs
Original file line number Diff line number Diff line change
@@ -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.");
}
42 changes: 42 additions & 0 deletions tests/run-make/strip/rmake.rs
Original file line number Diff line number Diff line change
@@ -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();
Comment on lines +14 to +17
Copy link
Member

@jieyouxu jieyouxu Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: the split-debuginfo behavior is observed in the split-debuginfo run-make test:

ifeq ($(UNAME),Darwin)
# If disabled, don't run `dsymutil`.
off:
rm -rf $(TMPDIR)/*.dSYM
$(RUSTC) foo.rs -g -C split-debuginfo=off
[ ! -d $(TMPDIR)/foo.dSYM ]
# Packed by default, but only if debuginfo is requested
packed:
rm -rf $(TMPDIR)/*.dSYM
$(RUSTC) foo.rs
[ ! -d $(TMPDIR)/foo.dSYM ]
rm -rf $(TMPDIR)/*.dSYM
$(RUSTC) foo.rs -g
[ -d $(TMPDIR)/foo.dSYM ]
rm -rf $(TMPDIR)/*.dSYM
$(RUSTC) foo.rs -g -C split-debuginfo=packed
[ -d $(TMPDIR)/foo.dSYM ]
rm -rf $(TMPDIR)/*.dSYM
# Object files are preserved with unpacked and `dsymutil` isn't run
unpacked:
$(RUSTC) foo.rs -g -C split-debuginfo=unpacked
ls $(TMPDIR)/*.o
[ ! -d $(TMPDIR)/foo.dSYM ]


// 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);
}
}
Loading