Repository navigation
compiler_rt duplicate symbol when statically linking #5320
Copy link
Copy link
Closed
Milestone
Description
Hi.
I'm trying to cross compile a crate in Rust using Zig (0.6.0+e79d7e0de) as a C compiler but I keep getting the error below.
I'm on WSL Ubuntu 18.04 trying to cross compile to aarch64-linux-musl.
Any thoughts?
error: linking with `zigg` failed: exit code: 1
|
= note: "zigg" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-nostdlib" "/home/mizu/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/crt1.o" "/home/mizu/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/crti.o" "-L" "/home/mizu/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib" "/mnt/d/Users/mizu/project/target/aarch64-unknown-linux-musl/release/deps/project-46dfb886f8739374.project.3leaiexz-cgu.0.rcgu.o" "-o" "/mnt/d/Users/mizu/project/target/aarch64-unknown-linux-musl/release/deps/project-46dfb886f8739374" "-Wl,--gc-sections" "-no-pie" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-L" "/mnt/d/Users/mizu/project/target/aarch64-unknown-linux-musl/release/deps" "-L" "/mnt/d/Users/mizu/project/target/release/deps" "-L" "/mnt/d/Users/mizu/project/target/aarch64-unknown-linux-musl/release/build/backtrace-sys-8ccc9707fc1f8b25/out" "-L" "/mnt/d/Users/mizu/project/target/aarch64-unknown-linux-musl/release/build/libz-sys-c6b81db4d3172bde/out/build" "-L" "/home/mizu/source/openssl/aarch64-linux-musl/lib" "-L" "/mnt/d/Users/mizu/project/target/aarch64-unknown-linux-musl/release/build/ring-31232a853800cfaa/out" "-L" "/home/mizu/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib" "-Wl,-Bstatic" "/tmp/rustcERnQRF/libopenssl_sys-0d899a1ca06d34db.rlib" "/tmp/rustcERnQRF/libbacktrace_sys-c46504543db0e47d.rlib" "/tmp/rustcERnQRF/liblibz_sys-4d45f07d5a71592d.rlib" "/tmp/rustcERnQRF/libring-fb104b3759fbb556.rlib" "-Wl,--start-group" "/tmp/rustcERnQRF/libbacktrace_sys-46271f041dd5b308.rlib" "/tmp/rustcERnQRF/libunwind-476b15d30ed96b76.rlib" "/tmp/rustcERnQRF/liblibc-cf2ebd1a477362df.rlib" "-Wl,--end-group" "/home/mizu/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-7a1d3fa1614309d1.rlib" "-static" "-s" "-Wl,-Bdynamic" "/home/mizu/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/crtn.o"
= note: warning: unsupported linker arg: --as-needed
warning: unsupported linker arg: -z noexecstack
warning: unsupported linker arg: --eh-frame-hdr
warning: unsupported linker arg: -zrelro
warning: unsupported linker arg: -znow
warning: unsupported linker arg: -Bstatic
warning: unsupported linker arg: --start-group
warning: unsupported linker arg: --end-group
warning: unsupported linker arg: -Bdynamic
lld: error: duplicate symbol: __cmptf2
>>> defined at comparetf2.c:45 (/checkout/src/llvm-project/compiler-rt/lib/builtins/comparetf2.c:45)
>>> comparetf2.o:(__letf2) in archive /home/mizu/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-7a1d3fa1614309d1.rlib
>>> defined at compiler_rt
>>> /home/mizu/.cache/zig/stage1/o/4YQazyYIi0oVIjBMqsZOnjaQFSQt0iHstjiK6fd6YYnCHJOgr7q533xflYlL1UlQ/compiler_rt.o:(.text.__letf2+0x0) in archive /home/mizu/.cache/zig/stage1/o/4YQazyYIi0oVIjBMqsZOnjaQFSQt0iHstjiK6fd6YYnCHJOgr7q533xflYlL1UlQ/libcompiler_rt.a
lld: error: duplicate symbol: __eqtf2
>>> defined at comparetf2.c:45 (/checkout/src/llvm-project/compiler-rt/lib/builtins/comparetf2.c:45)
>>> comparetf2.o:(__letf2) in archive /home/mizu/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-7a1d3fa1614309d1.rlib
>>> defined at compiler_rt
>>> /home/mizu/.cache/zig/stage1/o/4YQazyYIi0oVIjBMqsZOnjaQFSQt0iHstjiK6fd6YYnCHJOgr7q533xflYlL1UlQ/compiler_rt.o:(.text.__letf2+0x0) in archive /home/mizu/.cache/zig/stage1/o/4YQazyYIi0oVIjBMqsZOnjaQFSQt0iHstjiK6fd6YYnCHJOgr7q533xflYlL1UlQ/libcompiler_rt.a
lld: error: duplicate symbol: __gttf2
>>> defined at comparetf2.c:98 (/checkout/src/llvm-project/compiler-rt/lib/builtins/comparetf2.c:98)
>>> comparetf2.o:(__getf2) in archive /home/mizu/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-7a1d3fa1614309d1.rlib
>>> defined at compiler_rt
>>> /home/mizu/.cache/zig/stage1/o/4YQazyYIi0oVIjBMqsZOnjaQFSQt0iHstjiK6fd6YYnCHJOgr7q533xflYlL1UlQ/compiler_rt.o:(.text.__getf2+0x0) in archive /home/mizu/.cache/zig/stage1/o/4YQazyYIi0oVIjBMqsZOnjaQFSQt0iHstjiK6fd6YYnCHJOgr7q533xflYlL1UlQ/libcompiler_rt.a
lld: error: duplicate symbol: __lttf2
>>> defined at comparetf2.c:45 (/checkout/src/llvm-project/compiler-rt/lib/builtins/comparetf2.c:45)
>>> comparetf2.o:(__letf2) in archive /home/mizu/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-7a1d3fa1614309d1.rlib
>>> defined at compiler_rt
>>> /home/mizu/.cache/zig/stage1/o/4YQazyYIi0oVIjBMqsZOnjaQFSQt0iHstjiK6fd6YYnCHJOgr7q533xflYlL1UlQ/compiler_rt.o:(.text.__letf2+0x0) in archive /home/mizu/.cache/zig/stage1/o/4YQazyYIi0oVIjBMqsZOnjaQFSQt0iHstjiK6fd6YYnCHJOgr7q533xflYlL1UlQ/libcompiler_rt.a
lld: error: duplicate symbol: __netf2
>>> defined at comparetf2.c:45 (/checkout/src/llvm-project/compiler-rt/lib/builtins/comparetf2.c:45)
>>> comparetf2.o:(__letf2) in archive /home/mizu/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-7a1d3fa1614309d1.rlib
>>> defined at compiler_rt
>>> /home/mizu/.cache/zig/stage1/o/4YQazyYIi0oVIjBMqsZOnjaQFSQt0iHstjiK6fd6YYnCHJOgr7q533xflYlL1UlQ/compiler_rt.o:(.text.__letf2+0x0) in archive /home/mizu/.cache/zig/stage1/o/4YQazyYIi0oVIjBMqsZOnjaQFSQt0iHstjiK6fd6YYnCHJOgr7q533xflYlL1UlQ/libcompiler_rt.a
(
ziggis just shell script that invokeszig cc -target aarch64-linux-muslsince Rust appends the linker arguments rather than prepend)
$ rustup show
nightly-x86_64-unknown-linux-gnu (default)
rustc 1.45.0-nightly (bad3bf622 2020-05-09)
clang/llvm 10
gcc 10
cmake 3.10.2
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
nektro commentedon May 22, 2021
What crate were you trying to cross compile?
nacardin commentedon Jun 7, 2021
This looks similar to what I see when cross compiling a rust crate from
x86_64-linux-gnutox86_64-unknown-linux-musl. I created an example repo https://github.com/nacardin/zig-rust-musl-test.In the zig wrapper, if I use
x86_64-unknown-linux-muslinstead ofx86_64-linux-musl, then I see the same error as above.Looks like it is calling
ld.lld, but maybe with the wrong flavor? Since these args aren't accepted.nektro commentedon Jun 7, 2021
x86_64-unknown-linux-muslisn't a valid Zig target afaik.x86_64-linux-muslworks because Zig expects a triple. so that's not the issue at handphotex commentedon Jun 19, 2021
I've also just encountered this. The rust musl target must be including musl libc now. But I'm not sure how to control that.
I just created a hello world and set zig cc as the linker (but I'm using the correct triple). The rust-std crate and zig both appear to be providing key functions from musl and so the result is duplicate symbols:
Here is one example:
photex commentedon Jun 21, 2021
I just looked at the actual command rustc generates for the link step, and it does specify
-nostartfilesso it seem that zig cc is missing this somehow.If I'm not too far off the mark here, I'd be happy to take a shot at addressing this.
kubkon commentedon Jul 26, 2021
I don't think it's as simple as just a missing flag - AFAIK LLD by default behaves as if
-nostartfileswas specified (link) plus it's a GNU option anyhow. The underlying problem here is that both Rust and Zig link statically against a copy of musl-libc, both exporting the same set of symbols leading to an inevitable symbol duplication error by the linker.My gut tells me that the best lasting solution would be to have Zig's shipped musl-libc export symbols with weak linkage. However, that would imply modifying the musl-libc implementation in C which might not be the best idea given that the plan is to implement our libc in Zig anyhow (then we are free to select linkage scope as we please btw). Whatever the decision moving forward with this is however, I don't think this is a contributor friendly issue.
shiimizu commentedon Oct 6, 2021
This doesn't seem to be an issue with Zig itself. See below for the 'solution'.
Here's an easy way to reproduce the issue:
.cargo/config.toml:zigccscript:Trying to build:
Thanks to the insights given by @kubkon , I solved the issue by manually removing Rust's libc from the linker args. If you read carefully it tells you the location Rust's libc (amongst other things) around where it says
>>> defined atBuilding for real this time:
I'm using
fishshell and sd instead ofsedIt's more of a workaround since I have to manually copy the build invocation command from the terminal after my entire program fails to build (because that's when Rust spits out the linker args. Rust doesn't seem to allow you to play around with them).
It builds fine now and the executable (
tmp-2a52e9ef211a284c) is located attarget/aarch64-unknown-linux-musl/release/deps/This also works for
x86_64-linux-musltarget.syrusakbary commentedon Dec 9, 2021
I'm trying to cross compile Wasmer (Rust) c-api into musl and I'm getting the same error when using the generated Wasmer musl lib... any tip on how to make it work in an automated fashion @kubkon ?
Cloudef commentedon Oct 24, 2022
Currently experiencing the same issue. Zig does not seem to support -fallow-multiple-definition, and -fno-compiler-rt does not seem to do anything. Any suggestion how to fix or workaround this issue?
matu3ba commentedon Oct 25, 2022
You could expose the respective lld options in the build system and linker code.
Cloudef commentedon Oct 30, 2022
Doing this for now, but I'll want to research how to properly fix these issues in zig later:
https://gist.github.com/Cloudef/acb74ff9e36ab41709479240596ab501#file-zig-stdenv-nix-L165-L167
jiacai2050 commentedon Feb 11, 2023
For anyone with issue, I use this
zigccscript to remove duplicated files.Tested with zig 0.11.0-dev.1501+885d69689, and rust 1.67.1
Reference: https://github.com/rust-cross/cargo-zigbuild/pull/12/files
alexrp commentedon Apr 11, 2025
Is this still a problem?
andrewrk commentedon Feb 11, 2026
discussion continues at https://codeberg.org/ziglang/zig/issues/31182