Skip to content

Build failure when using LTO with static linking #129888

Open
@burtonageo

Description

@burtonageo

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LTOArea: Link-time optimization (LTO)A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions