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

"The encapsulation symbol needs to be retained under --gc-sections properly" #49

Open
asomers opened this issue Jul 9, 2022 · 8 comments
Labels
help wanted Extra attention is needed

Comments

@asomers
Copy link

asomers commented Jul 9, 2022

The current master branch fails to build on FreeBSD 14.0-CURRENT amd64, even though it still works on FreeBSD stable/13.

$ cargo test
...
  = note: ld: error: undefined symbol: __start_linkme_SHENANIGANS
          >>> referenced by x4z07wsgcl2z4i4
          >>>               /usr/home/somers/src/rust/linkme/target/debug/deps/distributed_slice-425b465e5440679a.x4z07wsgcl2z4i4.rcgu.o:(distributed_slice::SHENANIGANS::hca25c6519cd4df0f)
          >>> the encapsulation symbol needs to be retained under --gc-sections properly; consider -z nostart-stop-gc (see https://lld.llvm.org/ELF/start-stop-gc)
          
          ld: error: undefined symbol: __stop_linkme_SHENANIGANS
          >>> referenced by x4z07wsgcl2z4i4
          >>>               /usr/home/somers/src/rust/linkme/target/debug/deps/distributed_slice-425b465e5440679a.x4z07wsgcl2z4i4.rcgu.o:(distributed_slice::SHENANIGANS::hca25c6519cd4df0f)
          
          ld: error: undefined symbol: __start_linkme_EMPTY
          >>> referenced by x4z07wsgcl2z4i4
          >>>               /usr/home/somers/src/rust/linkme/target/debug/deps/distributed_slice-425b465e5440679a.x4z07wsgcl2z4i4.rcgu.o:(distributed_slice::test_empty::EMPTY::h31c7f9e37773eb8f)
          >>> the encapsulation symbol needs to be retained under --gc-sections properly; consider -z nostart-stop-gc (see https://lld.llvm.org/ELF/start-stop-gc)
          
          ld: error: undefined symbol: __stop_linkme_EMPTY
          >>> referenced by x4z07wsgcl2z4i4
          >>>               /usr/home/somers/src/rust/linkme/target/debug/deps/distributed_slice-425b465e5440679a.x4z07wsgcl2z4i4.rcgu.o:(distributed_slice::test_empty::EMPTY::h31c7f9e37773eb8f)
          
          ld: error: undefined symbol: __start_linkme_NONCOPY
          >>> referenced by x4z07wsgcl2z4i4
          >>>               /usr/home/somers/src/rust/linkme/target/debug/deps/distributed_slice-425b465e5440679a.x4z07wsgcl2z4i4.rcgu.o:(distributed_slice::test_non_copy::NONCOPY::h920eb5b4a050a94d)
          >>> the encapsulation symbol needs to be retained under --gc-sections properly; consider -z nostart-stop-gc (see https://lld.llvm.org/ELF/start-stop-gc)
          
          ld: error: undefined symbol: __stop_linkme_NONCOPY
          >>> referenced by x4z07wsgcl2z4i4
          >>>               /usr/home/somers/src/rust/linkme/target/debug/deps/distributed_slice-425b465e5440679a.x4z07wsgcl2z4i4.rcgu.o:(distributed_slice::test_non_copy::NONCOPY::h920eb5b4a050a94d)
          cc: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: could not compile `linkme` due to previous error
error: could not compile `linkme` due to previous error
error: could not compile `linkme` due to previous error
@asomers
Copy link
Author

asomers commented Jul 9, 2022

LLVM's instructions on how to solve the problem in C are here, but I don't know the Rust compiler well enough to apply its advice:
https://lld.llvm.org/ELF/start-stop-gc

A workaround is to use RUSTFLAGS="-C link-dead-code".

@dtolnay
Copy link
Owner

dtolnay commented Oct 25, 2022

Mentioning @knan-md @allanjude in case either of you know of a way to fix this. The FreeBSD implementation is from #42.

@marienz
Copy link

marienz commented Nov 14, 2022

I see what looks like the same error on Linux (Fedora 36, using either Fedora's Rust or Rust 1.65.0 installed using rustup) if I link with clang/ld.lld (which I was doing by having

[target.'cfg(not(any(target_os = "windows", target_arch = "wasm32")))']
rustflags = ["-C", "linker=clang", "-C", "link-args=-fuse-ld=/usr/bin/ld.lld"]

in ~/.cargo/config.toml, which I'd copied from somewhere a while back to slightly speed up builds and forgot about...).

If I read https://lld.llvm.org/ELF/start-stop-gc right that's expected, ld.lld after 13.0.0 on any ELF platform probably triggers it (or GNU ld before October 2015, but using a linker that old that to link Rust is probably a less common combination).

(I don't know enough about linkers and how Rust interacts with them to help fix this, sorry...)

@dtolnay dtolnay added the help wanted Extra attention is needed label Nov 29, 2022
@dtolnay dtolnay changed the title Build failure on FreeBSD 14 "The encapsulation symbol needs to be retained under --gc-sections properly" Jan 19, 2023
@danakj
Copy link
Contributor

danakj commented Jul 27, 2023

Putting #[used] on the static variable seems like it should resolve this, but it does not help.

@danakj
Copy link
Contributor

danakj commented Jul 27, 2023

I get this same error targeting Chromeos and Android:
https://ci.chromium.org/ui/p/chrome/builders/try/linux-chromeos-compile-chrome/249317/overview
https://ci.chromium.org/ui/p/chromium/builders/try/android-12-x64-rel/469563/overview

For some reason it's not appearing for Linux and MacOS, though they are using lld.

@danakj
Copy link
Contributor

danakj commented Jul 27, 2023

A workaround is to use RUSTFLAGS="-C link-dead-code".

This does not help us in Chromium, it seems. Applying that to the crate that has the static variable does not remove these errors when we try to link that crate into a .so.

@danakj
Copy link
Contributor

danakj commented Jul 27, 2023

These errors are resolved for me by enabling the used_linker feature. I did not need to add any extra #[used] attr on to the static slice variable itself.

Example successful link here: https://logs.chromium.org/logs/chromium/buildbucket/cr-buildbucket/8774385254500438081/+/u/compile__with_patch_/stdout#L41799_0

MikaelUrankar added a commit to MikaelUrankar/signal-desktop that referenced this issue Aug 16, 2023
It works when it's built with RUSTFLAGS="-Clink-dead-code"

Broken with dtolnay/linkme@64e7c63
Reported here: dtolnay/linkme#49
@blackgnezdo
Copy link

blackgnezdo commented Dec 14, 2023

These errors are resolved for me by enabling the used_linker feature. I did not need to add any extra #[used] attr on to the static slice variable itself.

Example successful link here: https://logs.chromium.org/logs/chromium/buildbucket/cr-buildbucket/8774385254500438081/+/u/compile__with_patch_/stdout#L41799_0

This workaround also helped me with a very basic example I was building. It appears that lld is a common thread in the problematic configurations. It's also fairly unpleasant to have to pepper the code with #![feature(used_with_arg)] and depend on unstable features.

FWIW, I'm on Debian Linux, so it's not particularly immune to this issue either.

lcheylus added a commit to lcheylus/rust-linkme that referenced this issue Jun 5, 2024
- Add support for OpenBSD, same as for FreeBSD
- Tests OK with RUSTFLAGS="-Clink-arg=-fuse-ld=lld -Clink-arg=-z -Clink-arg=nostart-stop-gc"
See dtolnay#49 for explanations

Signed-off-by: Laurent Cheylus <foxy@free.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants