Skip to content

Commit d9b8a78

Browse files
committed
codegen_ssa: use llvm-objcopy for macOS strip
1 parent 02f81c3 commit d9b8a78

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

compiler/rustc_codegen_ssa/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ codegen_ssa_L4Bender_exporting_symbols_unimplemented = exporting symbols not imp
22
33
codegen_ssa_add_native_library = failed to add native library {$library_path}: {$error}
44
5+
codegen_ssa_aix_strip_not_used = using host's `strip` binary to cross-compile to AIX which is not guaranteed to work
6+
57
codegen_ssa_apple_sdk_error_sdk_path = failed to get {$sdk_name} SDK path: {$error}
68
79
codegen_ssa_archive_build_failure = failed to build archive at `{$path}`: {$error}

compiler/rustc_codegen_ssa/src/back/link.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,8 @@ fn link_natively(
10871087
let strip = sess.opts.cg.strip;
10881088

10891089
if sess.target.is_like_osx {
1090-
let stripcmd = "/usr/bin/strip";
1090+
// `llvm-strip` is a symlink to `llvm-objcopy`, so use that (shipped with toolchain)
1091+
let stripcmd = "rust-objcopy";
10911092
match (strip, crate_type) {
10921093
(Strip::Debuginfo, _) => {
10931094
strip_symbols_with_external_utility(sess, stripcmd, out_filename, Some("-S"))
@@ -1103,11 +1104,14 @@ fn link_natively(
11031104
}
11041105
}
11051106

1106-
if sess.target.os == "illumos" {
1107+
if sess.target.is_like_solaris {
11071108
// Many illumos systems will have both the native 'strip' utility and
11081109
// the GNU one. Use the native version explicitly and do not rely on
11091110
// what's in the path.
1110-
let stripcmd = "/usr/bin/strip";
1111+
//
1112+
// If cross-compiling and there is not a native version, then use
1113+
// `llvm-strip` and hope.
1114+
let stripcmd = if !sess.host.is_like_solaris { "rust-objcopy" } else { "/usr/bin/strip" };
11111115
match strip {
11121116
// Always preserve the symbol table (-x).
11131117
Strip::Debuginfo => {
@@ -1120,6 +1124,10 @@ fn link_natively(
11201124
}
11211125

11221126
if sess.target.is_like_aix {
1127+
// `llvm-strip` doesn't work for AIX - their strip must be used.
1128+
if !sess.host.is_like_aix {
1129+
sess.dcx().emit_warn(errors::AixStripNotUsed);
1130+
}
11231131
let stripcmd = "/usr/bin/strip";
11241132
match strip {
11251133
Strip::Debuginfo => {
@@ -1147,6 +1155,13 @@ fn strip_symbols_with_external_utility(
11471155
if let Some(option) = option {
11481156
cmd.arg(option);
11491157
}
1158+
1159+
let mut new_path = sess.get_tools_search_paths(false);
1160+
if let Some(path) = env::var_os("PATH") {
1161+
new_path.extend(env::split_paths(&path));
1162+
}
1163+
cmd.env("PATH", env::join_paths(new_path).unwrap());
1164+
11501165
let prog = cmd.arg(out_filename).output();
11511166
match prog {
11521167
Ok(prog) => {

compiler/rustc_codegen_ssa/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1068,3 +1068,7 @@ pub(crate) struct ErrorCreatingImportLibrary<'a> {
10681068
pub lib_name: &'a str,
10691069
pub error: String,
10701070
}
1071+
1072+
#[derive(Diagnostic)]
1073+
#[diag(codegen_ssa_aix_strip_not_used)]
1074+
pub(crate) struct AixStripNotUsed;

0 commit comments

Comments
 (0)