rust-lang / rust-lang/rust

Build failure when using LTO with static linking

Open
#129,888 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-linkage A-LTO C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Clone the linked rational_test_app repository and run cargo build --release to reproduce the failure with static linking and LTO. Compare the successful dynamic-linking or no-LTO configurations; done means the release build succeeds with static linking and either lto = "thin" or lto = true.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
build-system, compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.