Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary build fails if two crates exported same function with #[no_mangle] and LTO enabled #98666

Open
ValeryAntopol opened this issue Jun 29, 2022 · 3 comments
Labels
C-bug Category: This is a bug. needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged.

Comments

@ValeryAntopol
Copy link

When linking crates which both export function with the same name with #[no_mangle], LTO fails to process it and emits a message that cannot be understood without looking into rustc sources. For simplicity, i put a minimal reproducible example into a repo https://github.com/ValeryAntopol/rust-lto-bug. The helper crate builds to a library with default settings, rust-lto-bug builds to a binary with default settings + LTO enabled, links with the helper crate.

I tried this code:

helper/lib.rs:

#[no_mangle]
pub fn foo() {}

main.rs:

#[allow(unused)]
use helper;

#[no_mangle]
pub fn foo() {}

fn main() {}

I expected to see this happen:
One of the following:

  • successful compilation where LTO somehow handles it (compilation without LTO works successfully, why should one with LTO enabled fail?)
  • compilation/linking error saying that it failed because of a function exported twice

I also expect that the behavior will be the same for both enabled and disabled LTO, but i understand that it could be too difficult to achieve.

Instead, this happened:

cargo build, LTO enabled:

   Compiling helper v0.1.0 (/Users/valeryantopol/work/rust-lto-bug/helper)
   Compiling rust-lto-bug v0.1.0 (/Users/valeryantopol/work/rust-lto-bug)
warning: Linking globals named 'foo': symbol multiply defined!

error: failed to load bitcode of module "sqkmfdx3qawf5f0": 

warning: `rust-lto-bug` (bin "rust-lto-bug") generated 1 warning
error: could not compile `rust-lto-bug` due to previous error; 1 warning emitted

cargo build --release, LTO enabled:

   Compiling helper v0.1.0 (/Users/valeryantopol/work/rust-lto-bug/helper)
   Compiling rust-lto-bug v0.1.0 (/Users/valeryantopol/work/rust-lto-bug)
warning: Linking globals named 'foo': symbol multiply defined!

error: failed to load bitcode of module "rust_lto_bug.db9ad83a-cgu.1": 

warning: `rust-lto-bug` (bin "rust-lto-bug") generated 1 warning
error: could not compile `rust-lto-bug` due to previous error; 1 warning emitted

cargo build, LTO disabled:

   Compiling helper v0.1.0 (/Users/valeryantopol/work/rust-lto-bug/helper)
   Compiling rust-lto-bug v0.1.0 (/Users/valeryantopol/work/rust-lto-bug)
    Finished dev [unoptimized + debuginfo] target(s) in 0.70s

Meta

Versions checked:

rustc 1.61.0 (fe5b13d68 2022-05-18)
rustc 1.64.0-nightly (830880640 2022-06-28)
rustc 1.63.0-beta.2 (6c1f14289 2022-06-28)

On all of them behavior is the same, only the module hash changes. But it is the same on different compilations with the same version.

RUST_BACKTACE=1 adds nothing to the output.

@ValeryAntopol ValeryAntopol added the C-bug Category: This is a bug. label Jun 29, 2022
@PPakalns
Copy link

Issue still actual with rustc 1.69.0 (84c898d 2023-04-16)
with lto="fat"

With lto = "thin", at least in my case, issue was resolved.

@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@ToufeeqP
Copy link

Yes, with lto = "thin", it works in my case as well

@DianQK
Copy link
Member

DianQK commented Dec 27, 2023

I'm sure it's not a bug. This is the behavior of Linker when FatLTO is not used. Linker will not add symbols from static libraries if the corresponding symbols exist in the already loaded object file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged.
Projects
None yet
Development

No branches or pull requests

5 participants