How do I remove the dependency on libunwind when cross-compiling Rust programs with the panic_abort option?

Viewed 40

I'm specifying the -Cpanic=abort and -Zbuild-std=panic_abort when compiling. Why does the linker still say it needs libunwind to compile a program?

I'm experimenting with various ways to cross-compile Rust programs as small as possible (using the min-sized-rust repo as a reference). Right now I'm trying to compile the powerpc64-unknown-linux-musl target and I'm stuck on trying to remove a dependency on libunwind.

Here's my setup:

# 1. Install the Rust std source code
rustup component add rust-src --toolchain nightly
# 2. Setup a simple rust repo
cargo init testing
cd testing
# 3. Download a musl toolchain
wget https://musl.cc/powerpc64-linux-musl-cross.tgz
tar xzf powerpc64-linux-musl-cross.tgz
# 4. Try to compile the project (options on the command line instead of in files for
# maximum obviousness).
# RUSTFLAGS:
#  -Cpanic=abort - abort immediately on panic
#  -Clink-self-contained=no - don't use rustc's builtin libraries and objects (this
#    is needed because powerpc64-unknown-linux-musl is a tier 3 target)
#  -Clink-arg=--sysroot and -Clink-arg=/path/to/sysroot - pass the option to the linker
#    to specify the sysroot of cross-compilation toolchain
# Cargo options:
#  --config target.<triple>.linker - specify the linker to use
#  -Zbuild-std=std,panic_abort - build the standard library from source. Specify
#    panic_abort to make the abort on panic work
RUSTFLAGS="-Cpanic=abort -Clink-self-contained=no -Clink-arg=--sysroot -Clink-arg=powerpc64-linux-musl-cross/powerpc64-linux-musl/" \
cargo +nightly build \
    --config "target.powerpc64-unknown-linux-musl.linker=\"powerpc64-linux-musl-cross/bin/powerpc64-linux-musl-gcc\"" \
    --target powerpc64-unknown-linux-musl -Zbuild-std=panic_abort,std --release

This fails with the following error:

error: linking with `/home/user/Projects/testing/powerpc64-linux-musl-cross/bin/powerpc64-linux-musl-gcc` failed: exit status: 1

<output snipped>

  = note: /home/user/Projects/testing/powerpc64-linux-musl-cross/bin/../lib/gcc/powerpc64-linux-musl/11.2.1/../../../../powerpc64-linux-musl/bin/ld: cannot find -lunwind
0 Answers
Related