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

Cannot link to --crate-type staticlib as -whole-archive #58277

Closed
krdln opened this issue Feb 7, 2019 · 6 comments
Closed

Cannot link to --crate-type staticlib as -whole-archive #58277

krdln opened this issue Feb 7, 2019 · 6 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries

Comments

@krdln
Copy link
Contributor

krdln commented Feb 7, 2019

Background: I was trying to integrate libfoo.a (compiled as crate-type = "staticlib") as a static depencency of libbar.so library (written in C++). Turned out that bazel was passing -whole-archive libfoo.a to the linker, which caused problems:

Steps to reproduce

This is a slightly different scenario, but produces the same error:

touch foo.rs
rustc --crate-type staticlib foo.rs
ld -r -whole-archive libfoo.a -o foo.o

Fails with (both with ld and ld.gold):

libfoo.a(clzsi2.o): In function `__clzsi2':
clzsi2.c:(.text.__clzsi2+0x0): multiple definition of `__clzsi2'
libfoo.a(compiler_builtins-05f9079cfd1746c1.compiler_builtins.5xigqg70-cgu.0.rcgu.o):/cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.5/src/int/mod.rs:332: first defined here

Failing version

rustc 1.34.0-nightly (4b1e39b7b 2019-02-05)
GNU ld (GNU Binutils for Ubuntu) 2.30
GNU gold (GNU Binutils for Ubuntu 2.30) 1.15
Ubuntu 18.04

Last working versions

This scenario works without errors on stable (1.32) and beta (1.33.0-beta.6).


I don't actually understand linkers enough to even say if that's a bug or expected behaviour, but at least for me it's a regression, because a scenario that was working on stable has stopped to work on nightly.

@nagisa
Copy link
Member

nagisa commented Feb 7, 2019

This is probably expected. The two different archives contain two definitions of the same compiler-provided function and both compilers are right in including that.

@jonas-schievink jonas-schievink added the A-linkage Area: linking into static, shared libraries and binaries label Feb 8, 2019
@cdetrio
Copy link

cdetrio commented Feb 11, 2019

I ran into the same issue when using nightly (downgrading to stable fixed it).

@MackieLoeffel
Copy link

I am running into the same problem and my current workaround is to remove clzsi2.o from the static library before linking it into a shared library:

touch foo.rs
rustc +nightly --crate-type staticlib foo.rs
ar d libfoo.a clzsi2.o # without this line I get the linker error from above
ld -r -whole-archive libfoo.a -o foo.o

@nagisa I don't understand what you mean with two different compilers. What other compiler than rustc do you mean? The problem seems to me that rustc puts two versions of __clzsi2 into libfoo.a.

kleisauke added a commit to libvips/build-win64-mxe that referenced this issue Apr 12, 2019
Building librsvg with Rust 1.34.0 fails due to rust-lang/rust#58277.
seirl added a commit to prologin/stechec2 that referenced this issue May 11, 2019
@kleisauke
Copy link
Contributor

It look like this is fixed with rust-lang/compiler-builtins@752e35a which is available within Rust 1.36.0 (#60841).

Note that the above mentioned workaround only needs to be applied to Rust 1.35.0 and 1.34.0, otherwise it will cause linker errors. See for example:
https://gitlab.gnome.org/GNOME/librsvg/issues/485#note_549519

gnomesysadmins pushed a commit to GNOME/librsvg that referenced this issue Jul 16, 2019
e310e36 solved static linking and the MinGW-w64 builds but included a
workaround for Rust 1.34 and 1.35 that didn't work for Rust 1.36.

The main problem with Rust 1.34 and 1.35 is that it included its own version
of the `__clzsi2` intrinsic instead of the C version. This caused duplicate
symbols during linking and required a workaround by removing the duplicated
symbol, see:
rust-lang/rust#58277

Fortunately, this is solved within rustc 1.36, see:
rust-lang/compiler-builtins@752e35a

The workaround, however, did not ensure that `__clzsi2` intrinsic is only
removed when it is duplicated (it could also remove the optimized C version).

This commit ensures that the `__clzsi2` intrinsic is present only once. Note
that the entire workaround can be removed when the minimum Rust version is
bumped to 1.36.

Closes: https://gitlab.gnome.org/GNOME/librsvg/issues/485
@petrochenkov
Copy link
Contributor

I have submitted an RFC about supporting whole archive - rust-lang/rfcs#2951.

@petrochenkov
Copy link
Contributor

Closing this issue, the whole-archive modifier was implemented about 9 months ago, and #93901 now intends to make it available on stable.

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
Projects
None yet
Development

No branches or pull requests

7 participants