rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.6k stars 12.74k forks source link

strip = "symbols" doesn't work when cross-compiling from linux to macOS #114411

Open roblabla opened 1 year ago

roblabla commented 1 year ago

Currently, when compiling for macOS with -C strip=symbols, rustc will try to invoke strip on the output binary to remove its symbols. This, unfortunately, fails when cross-compiling from linux, as the strip binary on linux only understands ELF files and will choke on the mach-o. This results in the following warnings in the build logs, and an unstripped binary output:

warning: stripping debug info with `strip` failed: exit status: 1
  |
  = note: /usr/bin/strip: /home/roblabla/repro/target/x86_64-apple-darwin/release/deps/repro-19371802bd981e48: file format not recognized

Ideally, rustc should either:

  1. Use a cross-target stripper, such as llvm-strip (which is currently packaged in rustup as part of llvm-tools-preview, CC #85658)
  2. Allow the user to configure which strip utility it will use (which could then end up in .cargo/config) to use one provided by osxcross or llvm.

Reproduction steps:

cargo new testing
cd testing
curl -L https://github.com/roblabla/MacOSX-SDKs/releases/download/13.3/MacOSX13.3.sdk.tar.xz | tar xJ
echo '[profile.release]' >> Cargo.toml
echo 'strip = "symbols"' >> Cargo.toml
SDKROOT=$PWD/MacOSX13.3.sdk CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER=rust-lld cargo build --target=x86_64-apple-darwin --release

Meta

rustc --version --verbose:

rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: aarch64-apple-darwin
release: 1.71.0
LLVM version: 16.0.5
roblabla commented 1 year ago

A coworker hit a weird case where Linux' strip would actually break the Mach-O instead of bailing out, rendering it inoperable (dyld would choke trying to load it, claiming /usr/lib/libobjc.A.dylib could not be found - which is obviously suspicious, as that lib is there on all darwin installs).

We finally tracked it down to this after a lot of trial and error, but this created a lot of confusion.

madsmtm commented 3 days ago

@rustbot label A-cross O-macos