Skip to content

Commit df0fc6d

Browse files
committed
Make symbols stripping work on MacOS X
As reported in the stabilization issue, MacOS' linker doesn't support the `-s` and `-S` flags to strip symbols anymore. However, the os ships a separated tool to perform these operations. This change allows the compiler to use that tool after a target has been compiled to strip symbols. For rationale, see: #72110 (comment) For option selection, see: https://www.unix.com/man-page/osx/1/strip/ Signed-off-by: David Calavera <[email protected]>
1 parent 9d9c2c9 commit df0fc6d

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+42-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_fs_util::fix_windows_verbatim_for_gcc;
55
use rustc_hir::def_id::CrateNum;
66
use rustc_middle::middle::cstore::{EncodedMetadata, LibSource};
77
use rustc_middle::middle::dependency_format::Linkage;
8-
use rustc_session::config::{self, CFGuard, CrateType, DebugInfo};
8+
use rustc_session::config::{self, CFGuard, CrateType, DebugInfo, Strip};
99
use rustc_session::config::{OutputFilenames, OutputType, PrintRequest};
1010
use rustc_session::output::{check_file_is_writeable, invalid_output_for_target, out_filename};
1111
use rustc_session::search_paths::PathKind;
@@ -717,14 +717,6 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
717717
}
718718
}
719719

720-
fn escape_string(s: &[u8]) -> String {
721-
str::from_utf8(s).map(|s| s.to_owned()).unwrap_or_else(|_| {
722-
let mut x = "Non-UTF-8 output: ".to_string();
723-
x.extend(s.iter().flat_map(|&b| ascii::escape_default(b)).map(char::from));
724-
x
725-
})
726-
}
727-
728720
match prog {
729721
Ok(prog) => {
730722
if !prog.status.success() {
@@ -866,6 +858,47 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
866858
// ... and otherwise we're processing a `*.dwp` packed dwarf file.
867859
SplitDebuginfo::Packed => link_dwarf_object(sess, &out_filename),
868860
}
861+
862+
if sess.target.is_like_osx {
863+
if let Some(option) = osx_strip_opt(sess.opts.debugging_opts.strip) {
864+
strip_symbols_in_osx(sess, &out_filename, option);
865+
}
866+
}
867+
}
868+
869+
fn strip_symbols_in_osx<'a>(sess: &'a Session, out_filename: &Path, option: &str) {
870+
let prog = Command::new("strip").arg(option).arg(out_filename).output();
871+
match prog {
872+
Ok(prog) => {
873+
if !prog.status.success() {
874+
let mut output = prog.stderr.clone();
875+
output.extend_from_slice(&prog.stdout);
876+
sess.struct_warn(&format!(
877+
"stripping debug info with `strip` failed: {}",
878+
prog.status
879+
))
880+
.note(&escape_string(&output))
881+
.emit();
882+
}
883+
}
884+
Err(e) => sess.fatal(&format!("unable to run `strip`: {}", e)),
885+
}
886+
}
887+
888+
fn osx_strip_opt<'a>(strip: Strip) -> Option<&'a str> {
889+
match strip {
890+
Strip::Debuginfo => Some("-S"),
891+
Strip::Symbols => Some("-x"),
892+
Strip::None => None,
893+
}
894+
}
895+
896+
fn escape_string(s: &[u8]) -> String {
897+
str::from_utf8(s).map(|s| s.to_owned()).unwrap_or_else(|_| {
898+
let mut x = "Non-UTF-8 output: ".to_string();
899+
x.extend(s.iter().flat_map(|&b| ascii::escape_default(b)).map(char::from));
900+
x
901+
})
869902
}
870903

871904
fn link_sanitizers(sess: &Session, crate_type: CrateType, linker: &mut dyn Linker) {

compiler/rustc_codegen_ssa/src/back/linker.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -482,15 +482,18 @@ impl<'a> Linker for GccLinker<'a> {
482482
fn control_flow_guard(&mut self) {}
483483

484484
fn debuginfo(&mut self, strip: Strip) {
485+
// MacOS linker doesn't support stripping symbols directly anymore.
486+
if self.sess.target.is_like_osx {
487+
return;
488+
}
489+
485490
match strip {
486491
Strip::None => {}
487492
Strip::Debuginfo => {
488-
// MacOS linker does not support longhand argument --strip-debug
489-
self.linker_arg("-S");
493+
self.linker_arg("--strip-debug");
490494
}
491495
Strip::Symbols => {
492-
// MacOS linker does not support longhand argument --strip-all
493-
self.linker_arg("-s");
496+
self.linker_arg("--strip-all");
494497
}
495498
}
496499
}

0 commit comments

Comments
 (0)