rust-lang / rust

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

Build failure when using LTO with static linking #129888

Open burtonageo opened 3 months ago

burtonageo commented 3 months ago

I have been experimenting with building crates as cdylibs/staticlibs out of tree as a workaround for emulating cargo artefacts on stable rust. I have created a repository with an example of this here. To minimally reproduce the error, run:

git clone --depth 1 https://github.com/burtonageo/rational_test_app
cd rational_test_app
cargo build --release

When building with static linking and LTO, I get the following error:

error: failed to get bitcode from object file for LTO (could not find requested section)

I have configured the release profile to use lto. When building with dynamic linking and LTO, or static linking without LTO, the build succeeds. This can be reproduced with either lto = "thin" or lto = true. To reproduce the error, run cargo build --release. To disable static linking, use the --no-default-features flag.

Meta

rustc --version --verbose:

cargo 1.80.1 (376290515 2024-07-16)
release: 1.80.1
commit-hash: 37629051518c3df9ac2c1744589362a02ecafa99
commit-date: 2024-07-16
host: aarch64-apple-darwin
libgit2: 1.7.2 (sys:0.18.3 vendored)
libcurl: 8.7.1 (sys:0.4.72+curl-8.6.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Mac OS 14.6.1 [64-bit]

This issue is also seen on nightly:

rustc +nightly --version --verbose:

cargo 1.82.0-nightly (8f40fc59f 2024-08-21)
release: 1.82.0-nightly
commit-hash: 8f40fc59fb0c8df91c97405785197f3c630304ea
commit-date: 2024-08-21
host: aarch64-apple-darwin
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.7.1 (sys:0.4.74+curl-8.9.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Mac OS 14.6.1 [64-bit]

madsmtm commented 2 months ago

This does not seem to be specific to macOS, I got the same result using, for example, --target=x86_64-unknown-redox.

@rustbot label -O-macos

zamazan4ik commented 2 days ago

Met the same problem for ppc64le target in Maturin: https://github.com/PyO3/maturin/discussions/2337