Skip to content

Commit 27e84b8

Browse files
authored
Rollup merge of #82037 - calavera:strip_debuginfo_osx, r=petrochenkov
Make symbols stripping work on MacOS X As reported in the [stabilization issue](#72110), 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]>
2 parents eab201d + df0fc6d commit 27e84b8

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::{DllImport, 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;
@@ -907,14 +907,6 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
907907
}
908908
}
909909

910-
fn escape_string(s: &[u8]) -> String {
911-
str::from_utf8(s).map(|s| s.to_owned()).unwrap_or_else(|_| {
912-
let mut x = "Non-UTF-8 output: ".to_string();
913-
x.extend(s.iter().flat_map(|&b| ascii::escape_default(b)).map(char::from));
914-
x
915-
})
916-
}
917-
918910
match prog {
919911
Ok(prog) => {
920912
if !prog.status.success() {
@@ -1056,6 +1048,47 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
10561048
// ... and otherwise we're processing a `*.dwp` packed dwarf file.
10571049
SplitDebuginfo::Packed => link_dwarf_object(sess, &out_filename),
10581050
}
1051+
1052+
if sess.target.is_like_osx {
1053+
if let Some(option) = osx_strip_opt(sess.opts.debugging_opts.strip) {
1054+
strip_symbols_in_osx(sess, &out_filename, option);
1055+
}
1056+
}
1057+
}
1058+
1059+
fn strip_symbols_in_osx<'a>(sess: &'a Session, out_filename: &Path, option: &str) {
1060+
let prog = Command::new("strip").arg(option).arg(out_filename).output();
1061+
match prog {
1062+
Ok(prog) => {
1063+
if !prog.status.success() {
1064+
let mut output = prog.stderr.clone();
1065+
output.extend_from_slice(&prog.stdout);
1066+
sess.struct_warn(&format!(
1067+
"stripping debug info with `strip` failed: {}",
1068+
prog.status
1069+
))
1070+
.note(&escape_string(&output))
1071+
.emit();
1072+
}
1073+
}
1074+
Err(e) => sess.fatal(&format!("unable to run `strip`: {}", e)),
1075+
}
1076+
}
1077+
1078+
fn osx_strip_opt<'a>(strip: Strip) -> Option<&'a str> {
1079+
match strip {
1080+
Strip::Debuginfo => Some("-S"),
1081+
Strip::Symbols => Some("-x"),
1082+
Strip::None => None,
1083+
}
1084+
}
1085+
1086+
fn escape_string(s: &[u8]) -> String {
1087+
str::from_utf8(s).map(|s| s.to_owned()).unwrap_or_else(|_| {
1088+
let mut x = "Non-UTF-8 output: ".to_string();
1089+
x.extend(s.iter().flat_map(|&b| ascii::escape_default(b)).map(char::from));
1090+
x
1091+
})
10591092
}
10601093

10611094
fn add_sanitizer_libraries(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
@@ -526,15 +526,18 @@ impl<'a> Linker for GccLinker<'a> {
526526
fn control_flow_guard(&mut self) {}
527527

528528
fn debuginfo(&mut self, strip: Strip) {
529+
// MacOS linker doesn't support stripping symbols directly anymore.
530+
if self.sess.target.is_like_osx {
531+
return;
532+
}
533+
529534
match strip {
530535
Strip::None => {}
531536
Strip::Debuginfo => {
532-
// MacOS linker does not support longhand argument --strip-debug
533-
self.linker_arg("-S");
537+
self.linker_arg("--strip-debug");
534538
}
535539
Strip::Symbols => {
536-
// MacOS linker does not support longhand argument --strip-all
537-
self.linker_arg("-s");
540+
self.linker_arg("--strip-all");
538541
}
539542
}
540543
}

0 commit comments

Comments
 (0)