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

linking with cc failed when using lto = fat #72268

Closed
hellow554 opened this issue May 16, 2020 · 6 comments
Closed

linking with cc failed when using lto = fat #72268

hellow554 opened this issue May 16, 2020 · 6 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@hellow554
Copy link
Contributor

hellow554 commented May 16, 2020

This bug can be reproduced only by using two crates, nothing else worked so far

A
├── B
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
├── Cargo.toml
└── src
    └── main.rs

A::Cargo.toml

[package]
name = "A"
version = "0.1.0"

[profile.dev]
lto = "fat"

[dependencies]
B = { path = "./B" }

A::main.rs is empty (or just a plain fn main() {}, doesn't matter).

B::Cargo.toml

[package]
name = "B"
version = "0.1.0"

[lib]
crate_type = ["rlib", "cdylib"]

B::src/lib.rs is empty as well (or any public function, doesn't matter as well)

When compiling this with cargo build, it errors:

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-Wl,-plugin-opt=O0" "-Wl,-plugin-opt=mcpu=x86-64" "-L" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/tmp/tmp.DW07BccpbK/A/target/debug/deps/B.42q3kclfyl99cg7w.rcgu.o" "-o" "/tmp/tmp.DW07BccpbK/A/target/debug/deps/libB.so" "-Wl,--version-script=/tmp/rustcS2Vx8P/list" "/tmp/tmp.DW07BccpbK/A/target/debug/deps/B.4c2aglkwgm7gduii.rcgu.o" "-Wl,--gc-sections" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/tmp/tmp.DW07BccpbK/A/target/debug/deps" "-L" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-0126adaa107032a6.rlib" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-57961a3692c39401.rlib" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-9fa9e50746f44a4c.rlib" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-251012178f8196c3.rlib" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libbacktrace-cafd612ee1b4bf88.rlib" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libbacktrace_sys-140b53dc81f0d53c.rlib" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-cbaa12f3d93cc54c.rlib" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-a5bcc2d112e5a391.rlib" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-5ada18586d2652e2.rlib" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-ac41f78688451395.rlib" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-b1966f66603f91b6.rlib" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-428d15b56101bdc7.rlib" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-941f7f6bc622e313.rlib" "-Wl,--end-group" "/home/marcel/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-e915d31ab7edbbd4.rlib" "-Wl,-Bdynamic" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-ldl" "-lutil" "-shared"
  = note: /tmp/tmp.DW07BccpbK/A/target/debug/deps/B.42q3kclfyl99cg7w.rcgu.o: file not recognized: file format not recognized
          collect2: error: ld returned 1 exit status

Meta

rustc --version --verbose:

rustc 1.45.0-nightly (a74d1862d 2020-05-14)
@hellow554 hellow554 added the C-bug Category: This is a bug. label May 16, 2020
@hellow554
Copy link
Contributor Author

hellow554 commented May 16, 2020

searched nightlies: from nightly-2020-01-01 to nightly-2020-05-14
regressed nightly: nightly-2020-05-03
searched commits: from 7f65393 to f05a524
regressed commit: dae90c1

cc #71716 @alexcrichton

@jonas-schievink jonas-schievink added A-linkage Area: linking into static, shared libraries and binaries T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. labels May 16, 2020
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label May 16, 2020
@ehuss
Copy link
Contributor

ehuss commented May 16, 2020

@alexcrichton It looks like when a dependency has mixed crate types (["cdylib", "rlib"]), cargo is passing -Clinker-plugin-lto, which causes the cdylib to be built incorrectly. Should Cargo avoid passing the flag in that scenerio, or should rustc be smart enough to ignore it when generating the cdylib?

Also, note, that this hits #60059 on macos where linking won't even start.

@alexcrichton
Copy link
Member

Nah yeah this is a bug in Cargo's passing of -Clinker-plugin-lto, I'll look to fix this.

@alexcrichton
Copy link
Member

This should be fixed in rust-lang/cargo#8254.

It's worth noting that with an rlib/cdylib crate type output LTO is turned off (due to #51009), so the LTO setting doesn't actually do anything other than cause more work during compilation otherwise.

alexcrichton added a commit to alexcrichton/cargo that referenced this issue May 18, 2020
In the case that LTO is happening but we're also generating a
cdylib/rlib simultatneously that means that the final artifact will use
the rlib but the cdylib still needs to be produced. To get this to work
the cdylib's dependency tree needs to be compiled with embedded bitcode.
The cdylib itself will be linked with the linker because we can't LTO an
rlib+cdylib compilation, but the final executable will need to load
bitcode from all its deps.

This is meant to address rust-lang/rust#72268
bors added a commit to rust-lang/cargo that referenced this issue May 18, 2020
Handle LTO with an rlib/cdylib crate type

In the case that LTO is happening but we're also generating a
cdylib/rlib simultatneously that means that the final artifact will use
the rlib but the cdylib still needs to be produced. To get this to work
the cdylib's dependency tree needs to be compiled with embedded bitcode.
The cdylib itself will be linked with the linker because we can't LTO an
rlib+cdylib compilation, but the final executable will need to load
bitcode from all its deps.

This is meant to address rust-lang/rust#72268
@spastorino
Copy link
Member

@alexcrichton we should close this one now, right?.

@alexcrichton
Copy link
Member

Indeed!

@jyn514 jyn514 removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Feb 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants