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

LTO and no_builtins: undefined references to standard Rust symbols errors at link time #72140

Closed
Deluvi opened this issue May 12, 2020 · 7 comments · Fixed by #113923
Closed
Assignees
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Deluvi
Copy link

Deluvi commented May 12, 2020

It seems that depending on a Rust library that has the attribute #![no_builtins] prevents the linker to provide the needed symbols to the library.

I made a repository to demonstrate this issue with a minimal reproduction case: Deluvi/bug-no-builtins-lto-rust. The instructions to run the example are in the README.

Here is an example error line (you can find the whole cargo output in the "Error output" drop down below):

/usr/bin/ld: no_builtins_lib.2derfq4u-cgu.2:(.text._ZN5alloc7raw_vec19RawVec$LT$T$C$A$GT$7reserve17h791963cb9aa36cdbE+0x7f): undefined reference to `__rust_alloc'

If you want the compilation to work, you can remove the `#![no_builtins] line and the compilation will work just fine.

Meta

rustc --version --verbose:

rustc 1.43.1 (8d69840ab 2020-05-04)
binary: rustc
commit-hash: 8d69840ab92ea7f4d323420088dd8c9775f180cd
commit-date: 2020-05-04
host: x86_64-unknown-linux-gnu
release: 1.43.1
LLVM version: 9.0
Error output

cargo run --release -p no-builtins-bin
   Compiling no-builtins-lib v0.1.0 (/home/deluvi/tests/test-lto/no-builtins-lib)
   Compiling no-builtins-bin v0.1.0 (/home/deluvi/tests/test-lto/no-builtins-bin)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/deluvi/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/deluvi/tests/test-lto/target/release/deps/no_builtins_bin-d28e15586db11f8b.no_builtins_bin.ds5ffvdz-cgu.7.rcgu.o" "-o" "/home/deluvi/tests/test-lto/target/release/deps/no_builtins_bin-d28e15586db11f8b" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-L" "/home/deluvi/tests/test-lto/target/release/deps" "-L" "/home/deluvi/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/deluvi/tests/test-lto/target/release/deps/libno_builtins_lib-cef2adf5c7c12373.rlib" "-Wl,--start-group" "/tmp/rustcvFiUgJ/libbacktrace_sys-dc606003556dfe9c.rlib" "-Wl,--end-group" "/home/deluvi/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-2541f1e09df1c67d.rlib" "-Wl,-Bdynamic" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-lutil"
  = note: /usr/bin/ld: /home/deluvi/tests/test-lto/target/release/deps/libno_builtins_lib-cef2adf5c7c12373.rlib(no_builtins_lib-cef2adf5c7c12373.no_builtins_lib.2derfq4u-cgu.0.rcgu.o): in function `core::ptr::drop_in_place':
          no_builtins_lib.2derfq4u-cgu.0:(.text._ZN4core3ptr13drop_in_place17h107dbe4e36e3489dE+0x13): undefined reference to `__rust_dealloc'
          /usr/bin/ld: /home/deluvi/tests/test-lto/target/release/deps/libno_builtins_lib-cef2adf5c7c12373.rlib(no_builtins_lib-cef2adf5c7c12373.no_builtins_lib.2derfq4u-cgu.2.rcgu.o): in function `alloc::raw_vec::RawVec<T,A>::reserve':
          no_builtins_lib.2derfq4u-cgu.2:(.text._ZN5alloc7raw_vec19RawVec$LT$T$C$A$GT$7reserve17h791963cb9aa36cdbE+0x58): undefined reference to `__rust_realloc'
          /usr/bin/ld: no_builtins_lib.2derfq4u-cgu.2:(.text._ZN5alloc7raw_vec19RawVec$LT$T$C$A$GT$7reserve17h791963cb9aa36cdbE+0x7f): undefined reference to `__rust_alloc'
          /usr/bin/ld: no_builtins_lib.2derfq4u-cgu.2:(.text._ZN5alloc7raw_vec19RawVec$LT$T$C$A$GT$7reserve17h791963cb9aa36cdbE+0x8f): undefined reference to `__rust_dealloc'
          /usr/bin/ld: no_builtins_lib.2derfq4u-cgu.2:(.text._ZN5alloc7raw_vec19RawVec$LT$T$C$A$GT$7reserve17h791963cb9aa36cdbE+0x98): undefined reference to `core::alloc::Layout::dangling'
          /usr/bin/ld: no_builtins_lib.2derfq4u-cgu.2:(.text._ZN5alloc7raw_vec19RawVec$LT$T$C$A$GT$7reserve17h791963cb9aa36cdbE+0xa8): undefined reference to `core::alloc::Layout::dangling'
          /usr/bin/ld: no_builtins_lib.2derfq4u-cgu.2:(.text._ZN5alloc7raw_vec19RawVec$LT$T$C$A$GT$7reserve17h791963cb9aa36cdbE+0xc4): undefined reference to `alloc::raw_vec::capacity_overflow'
          /usr/bin/ld: no_builtins_lib.2derfq4u-cgu.2:(.text._ZN5alloc7raw_vec19RawVec$LT$T$C$A$GT$7reserve17h791963cb9aa36cdbE+0xd4): undefined reference to `alloc::alloc::handle_alloc_error'
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error

error: could not compile `no-builtins-bin`.

@Deluvi Deluvi added the C-bug Category: This is a bug. label May 12, 2020
@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. labels May 12, 2020
@pnkfelix
Copy link
Member

Inspired by discussion on PR #109821, I made a variant test case (largely taken from @DianQK's "stable" demo for that PR, to find out the historical behavior of Rust on that example. (The main differences are that 1. I changed the test to use extern crate, in order to be able to build on older versions of Rust, 2. I changed it to use lto = true rather than lto = fat, again for backward compatibility with older versions of Rust, and 3. I had the code start returning a non-() value from the linked crate, specially a &'static str, so that I could double-check that the dynamic behavior is plausible.)

Here is the main thing I've discovered so far: Rust 1.0 through 1.12 were able to compile and run the adapted example. It was back in Rust 1.13 that we started seeing this incompatbility between LTO and #![no_builtins], i.e. something along the lines of of:

% cargo  +1.13 build --release
   Compiling bar v0.1.0 (file:///home/ubuntu/Dev/Rust/Linking/issue_72140/rust-109821-reproducible/stable/bar)
   Compiling foo v0.1.0 (file:///home/ubuntu/Dev/Rust/Linking/issue_72140/rust-109821-reproducible/stable/foo)
   Compiling tool v0.1.0 (file:///home/ubuntu/Dev/Rust/Linking/issue_72140/rust-109821-reproducible/stable)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/ubuntu/.rustup/toolchains/1.13-x86_64-unknown-linux-gnu/lib/rustlib/x86_6\
4-unknown-linux-gnu/lib" "/home/ubuntu/Dev/Rust/Linking/issue_72140/rust-109821-reproducible/stable/target/release/tool.0.o" "-o" "/home/ubuntu/De\
v/Rust/Linking/issue_72140/rust-109821-reproducible/stable/target/release/tool" "-Wl,--gc-sections" "-pie" "-nodefaultlibs" "-L" "/home/ubuntu/Dev\
/Rust/Linking/issue_72140/rust-109821-reproducible/stable/target/release/deps" "-L" "/home/ubuntu/.rustup/toolchains/1.13-x86_64-unknown-linux-gnu\
/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "/tmp/rustc.C74lmNfGlWwA/libfoo.rlib" "/tmp/rustc.C74lmNfGlWwA/libstd-a4\
729905.rlib" "/tmp/rustc.C74lmNfGlWwA/liballoc_jemalloc-a4729905.rlib" "/tmp/rustc.C74lmNfGlWwA/libcompiler_builtins-a4729905.rlib" "-l" "dl" "-l"\
 "pthread" "-l" "gcc_s" "-l" "pthread" "-l" "c" "-l" "m" "-l" "rt" "-l" "util"
  = note: /usr/bin/ld: /tmp/rustc.C74lmNfGlWwA/libfoo.rlib(foo.0.o): in function `foo::msg':
foo.cgu-0.rs:(.text._ZN3foo3msg17hb7b968140369922eE+0x1): undefined reference to `bar::msg'
collect2: error: ld returned 1 exit status

So, this has been broken for a long time (i.e. since the end of 2016), but it hasn't "always" been broken.

@pnkfelix pnkfelix added the regression-from-stable-to-stable Performance or correctness regression from one stable version to another. label Jul 17, 2023
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jul 17, 2023
@DianQK
Copy link
Member

DianQK commented Jul 18, 2023

Maybe now you could look at #113716. I added the no-builtins attribute to the function. We can get #![no_builtins] to participate in LTO again.

@pnkfelix
Copy link
Member

Oh, was PR #35637 the reason that this broke between 1.12 and 1.13?

@DianQK
Copy link
Member

DianQK commented Jul 18, 2023

Oh, was PR #35637 the reason that this broke between 1.12 and 1.13?

Yes. The symbols required by #![no_builtins] are present in LTO, but they are not exported. #109821 is a possible solution.
However, now I have found a more suitable solution, which is to let #![no_builtins] participate in LTO again.

@pnkfelix
Copy link
Member

Yeah I think #113716 (plus reverting #35637) sounds far more reasonable than PR #109821.

(It still may not resolve some outstanding questions i have about what goes on with implicit dependencies on libcore, but that need not concern us here.)

@apiraino
Copy link
Contributor

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-medium

@rustbot rustbot added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jul 18, 2023
@DianQK
Copy link
Member

DianQK commented Aug 10, 2023

@rustbot claim

bors added a commit to rust-lang-ci/rust that referenced this issue Sep 14, 2023
Restore `#![no_builtins]` crates participation in LTO.

After rust-lang#113716, we can make `#![no_builtins]` crates participate in LTO again.

`#![no_builtins]` with LTO does not result in undefined references to the error. I believe this type of issue won't happen again. \(^▽^)/

I will test the following issues later to verify. The task format is `Fixes {issue} {nightly-2023-07-20 result} {PR rust-lang#113923 result}`.

- [x] Fixes rust-lang#72140. ❌ ✅
- [x] Fixes rust-lang#112245. ❌ ✅
- [x] Fixes rust-lang#110606. ❌ ✅
- [ ] Fixes rust-lang#105734.
- [ ] Fixes rust-lang#96486.
- [ ] Fixes rust-lang#108853.
- [x] Fixes rust-lang/compiler-builtins#347. ❌ ✅
- [ ] Fixes rust-lang#108893.
- [ ] Fixes rust-lang#78744. Fixes rust-lang#91158. Fixes rust-lang/cargo#10118.

 The `nightly-2023-07-20` version does not always reproduce problems due to changes in compiler-builtins, core, and user code. That's why this issue recurs and disappears.
Some issues were not tested due to the difficulty of reproducing them.

r? pnkfelix

cc `@bjorn3` `@japaric` `@alexcrichton` `@Amanieu`
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 12, 2023
…kfelix

Restore `#![no_builtins]` crates participation in LTO.

After rust-lang#113716, we can make `#![no_builtins]` crates participate in LTO again.

`#![no_builtins]` with LTO does not result in undefined references to the error. I believe this type of issue won't happen again. \(^▽^)/

I will test the following issues later to verify. The task format is `Fixes {issue} {nightly-2023-07-20 result} {PR rust-lang#113923 result}`.

- [x] Fixes rust-lang#72140. ❌ ✅
- [x] Fixes rust-lang#112245. ❌ ✅
- [x] Fixes rust-lang#110606. ❌ ✅
- [ ] Fixes rust-lang#105734.
- [ ] Fixes rust-lang#96486.
- [ ] Fixes rust-lang#108853.
- [x] Fixes rust-lang/compiler-builtins#347. ❌ ✅
- [ ] Fixes rust-lang#108893.
- [ ] Fixes rust-lang#78744. Fixes rust-lang#91158. Fixes rust-lang/cargo#10118.

 The `nightly-2023-07-20` version does not always reproduce problems due to changes in compiler-builtins, core, and user code. That's why this issue recurs and disappears.
Some issues were not tested due to the difficulty of reproducing them.

r? pnkfelix

cc `@bjorn3` `@japaric` `@alexcrichton` `@Amanieu`
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 17, 2023
…kfelix

Restore `#![no_builtins]` crates participation in LTO.

After rust-lang#113716, we can make `#![no_builtins]` crates participate in LTO again.

`#![no_builtins]` with LTO does not result in undefined references to the error. I believe this type of issue won't happen again. \(^▽^)/

I will test the following issues later to verify. The task format is `Fixes {issue} {nightly-2023-07-20 result} {PR rust-lang#113923 result}`.

- [x] Fixes rust-lang#72140. ❌ ✅
- [x] Fixes rust-lang#112245. ❌ ✅
- [x] Fixes rust-lang#110606. ❌ ✅
- [ ] Fixes rust-lang#105734.
- [ ] Fixes rust-lang#96486.
- [ ] Fixes rust-lang#108853.
- [x] Fixes rust-lang/compiler-builtins#347. ❌ ✅
- [ ] Fixes rust-lang#108893.
- [ ] Fixes rust-lang#78744. Fixes rust-lang#91158. Fixes rust-lang/cargo#10118.

 The `nightly-2023-07-20` version does not always reproduce problems due to changes in compiler-builtins, core, and user code. That's why this issue recurs and disappears.
Some issues were not tested due to the difficulty of reproducing them.

r? pnkfelix

cc `@bjorn3` `@japaric` `@alexcrichton` `@Amanieu`
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 26, 2023
…kfelix

Restore `#![no_builtins]` crates participation in LTO.

After rust-lang#113716, we can make `#![no_builtins]` crates participate in LTO again.

`#![no_builtins]` with LTO does not result in undefined references to the error. I believe this type of issue won't happen again.

Fixes rust-lang#72140.  Fixes rust-lang#112245. Fixes rust-lang#110606.  Fixes rust-lang#105734. Fixes rust-lang#96486. Fixes rust-lang#108853. Fixes rust-lang#108893. Fixes rust-lang#78744. Fixes rust-lang#91158. Fixes rust-lang/cargo#10118. Fixes rust-lang/compiler-builtins#347.

 The `nightly-2023-07-20` version does not always reproduce problems due to changes in compiler-builtins, core, and user code. That's why this issue recurs and disappears.
Some issues were not tested due to the difficulty of reproducing them.

r? pnkfelix

cc `@bjorn3` `@japaric` `@alexcrichton` `@Amanieu`
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 27, 2023
…kfelix

Restore `#![no_builtins]` crates participation in LTO.

After rust-lang#113716, we can make `#![no_builtins]` crates participate in LTO again.

`#![no_builtins]` with LTO does not result in undefined references to the error. I believe this type of issue won't happen again.

Fixes rust-lang#72140.  Fixes rust-lang#112245. Fixes rust-lang#110606.  Fixes rust-lang#105734. Fixes rust-lang#96486. Fixes rust-lang#108853. Fixes rust-lang#108893. Fixes rust-lang#78744. Fixes rust-lang#91158. Fixes rust-lang/cargo#10118. Fixes rust-lang/compiler-builtins#347.

 The `nightly-2023-07-20` version does not always reproduce problems due to changes in compiler-builtins, core, and user code. That's why this issue recurs and disappears.
Some issues were not tested due to the difficulty of reproducing them.

r? pnkfelix

cc `@bjorn3` `@japaric` `@alexcrichton` `@Amanieu`
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 28, 2023
…kfelix

Restore `#![no_builtins]` crates participation in LTO.

After rust-lang#113716, we can make `#![no_builtins]` crates participate in LTO again.

`#![no_builtins]` with LTO does not result in undefined references to the error. I believe this type of issue won't happen again.

Fixes rust-lang#72140.  Fixes rust-lang#112245. Fixes rust-lang#110606.  Fixes rust-lang#105734. Fixes rust-lang#96486. Fixes rust-lang#108853. Fixes rust-lang#108893. Fixes rust-lang#78744. Fixes rust-lang#91158. Fixes rust-lang/cargo#10118. Fixes rust-lang/compiler-builtins#347.

 The `nightly-2023-07-20` version does not always reproduce problems due to changes in compiler-builtins, core, and user code. That's why this issue recurs and disappears.
Some issues were not tested due to the difficulty of reproducing them.

r? pnkfelix

cc `@bjorn3` `@japaric` `@alexcrichton` `@Amanieu`
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 14, 2023
Restore `#![no_builtins]` crates participation in LTO.

After rust-lang#113716, we can make `#![no_builtins]` crates participate in LTO again.

`#![no_builtins]` with LTO does not result in undefined references to the error. I believe this type of issue won't happen again.

Fixes rust-lang#72140.  Fixes rust-lang#112245. Fixes rust-lang#110606.  Fixes rust-lang#105734. Fixes rust-lang#96486. Fixes rust-lang#108853. Fixes rust-lang#108893. Fixes rust-lang#78744. Fixes rust-lang#91158. Fixes rust-lang/cargo#10118. Fixes rust-lang/compiler-builtins#347.

 The `nightly-2023-07-20` version does not always reproduce problems due to changes in compiler-builtins, core, and user code. That's why this issue recurs and disappears.
Some issues were not tested due to the difficulty of reproducing them.

r? pnkfelix

cc `@bjorn3` `@japaric` `@alexcrichton` `@Amanieu`
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 16, 2023
…kfelix

Restore `#![no_builtins]` crates participation in LTO.

After rust-lang#113716, we can make `#![no_builtins]` crates participate in LTO again.

`#![no_builtins]` with LTO does not result in undefined references to the error. I believe this type of issue won't happen again.

Fixes rust-lang#72140.  Fixes rust-lang#112245. Fixes rust-lang#110606.  Fixes rust-lang#105734. Fixes rust-lang#96486. Fixes rust-lang#108853. Fixes rust-lang#108893. Fixes rust-lang#78744. Fixes rust-lang#91158. Fixes rust-lang/cargo#10118. Fixes rust-lang/compiler-builtins#347.

 The `nightly-2023-07-20` version does not always reproduce problems due to changes in compiler-builtins, core, and user code. That's why this issue recurs and disappears.
Some issues were not tested due to the difficulty of reproducing them.

r? pnkfelix

cc `@bjorn3` `@japaric` `@alexcrichton` `@Amanieu`
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 16, 2023
…kfelix

Restore `#![no_builtins]` crates participation in LTO.

After rust-lang#113716, we can make `#![no_builtins]` crates participate in LTO again.

`#![no_builtins]` with LTO does not result in undefined references to the error. I believe this type of issue won't happen again.

Fixes rust-lang#72140.  Fixes rust-lang#112245. Fixes rust-lang#110606.  Fixes rust-lang#105734. Fixes rust-lang#96486. Fixes rust-lang#108853. Fixes rust-lang#108893. Fixes rust-lang#78744. Fixes rust-lang#91158. Fixes rust-lang/cargo#10118. Fixes rust-lang/compiler-builtins#347.

 The `nightly-2023-07-20` version does not always reproduce problems due to changes in compiler-builtins, core, and user code. That's why this issue recurs and disappears.
Some issues were not tested due to the difficulty of reproducing them.

r? pnkfelix

cc `@bjorn3` `@japaric` `@alexcrichton` `@Amanieu`
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 30, 2023
…kfelix

Restore `#![no_builtins]` crates participation in LTO.

After rust-lang#113716, we can make `#![no_builtins]` crates participate in LTO again.

`#![no_builtins]` with LTO does not result in undefined references to the error. I believe this type of issue won't happen again.

Fixes rust-lang#72140.  Fixes rust-lang#112245. Fixes rust-lang#110606.  Fixes rust-lang#105734. Fixes rust-lang#96486. Fixes rust-lang#108853. Fixes rust-lang#108893. Fixes rust-lang#78744. Fixes rust-lang#91158. Fixes rust-lang/cargo#10118. Fixes rust-lang/compiler-builtins#347.

 The `nightly-2023-07-20` version does not always reproduce problems due to changes in compiler-builtins, core, and user code. That's why this issue recurs and disappears.
Some issues were not tested due to the difficulty of reproducing them.

r? pnkfelix

cc `@bjorn3` `@japaric` `@alexcrichton` `@Amanieu`
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 1, 2023
…kfelix

Restore `#![no_builtins]` crates participation in LTO.

After rust-lang#113716, we can make `#![no_builtins]` crates participate in LTO again.

`#![no_builtins]` with LTO does not result in undefined references to the error. I believe this type of issue won't happen again.

Fixes rust-lang#72140.  Fixes rust-lang#112245. Fixes rust-lang#110606.  Fixes rust-lang#105734. Fixes rust-lang#96486. Fixes rust-lang#108853. Fixes rust-lang#108893. Fixes rust-lang#78744. Fixes rust-lang#91158. Fixes rust-lang/cargo#10118. Fixes rust-lang/compiler-builtins#347.

 The `nightly-2023-07-20` version does not always reproduce problems due to changes in compiler-builtins, core, and user code. That's why this issue recurs and disappears.
Some issues were not tested due to the difficulty of reproducing them.

r? pnkfelix

cc `@bjorn3` `@japaric` `@alexcrichton` `@Amanieu`
@bors bors closed this as completed in 8c2b577 Dec 1, 2023
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. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
6 participants