ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.7k stars 2.54k forks source link

compiler_rt duplicate symbol when statically linking #5320

Open shiimizu opened 4 years ago

shiimizu commented 4 years ago

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

(zigg is just shell script that invokes zig cc -target aarch64-linux-musl since 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
nektro commented 3 years ago

What crate were you trying to cross compile?

nacardin commented 3 years ago

This looks similar to what I see when cross compiling a rust crate from x86_64-linux-gnu to x86_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-musl instead of x86_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.

  = note: warning: unsupported linker arg: --as-needed
          warning: unsupported linker arg: -z noexecstack
          warning: unsupported linker arg: -zrelro
          warning: unsupported linker arg: -znow
          warning: unsupported linker arg: -Bstatic
          warning: unsupported linker arg: -Bdynamic
nektro commented 3 years ago

x86_64-unknown-linux-musl isn't a valid Zig target afaik. x86_64-linux-musl works because Zig expects a triple. so that's not the issue at hand

photex commented 3 years ago

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:

ld.lld: error: duplicate symbol: __progname_full
          >>> defined at libc.c
          >>>            libc.lo:(__progname_full) in archive /usr/local/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-0a03e83fc44965da.rlib
          >>> defined at libc.c
          >>>            /root/.cache/zig/o/5a075940dff85385c3b0a9a10452e3d1/libc.o:(.bss.__progname_full+0x0) in archive /root/.cache/zig/o/4551a41bf623d7f1aaf1ca8eda457e85/libc.a
photex commented 3 years ago

I just looked at the actual command rustc generates for the link step, and it does specify -nostartfiles so 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 commented 3 years ago

I just looked at the actual command rustc generates for the link step, and it does specify -nostartfiles so 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.

I don't think it's as simple as just a missing flag - AFAIK LLD by default behaves as if -nostartfiles was 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 commented 3 years ago

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:

rustup target add aarch64-unknown-linux-musl
cargo init tmp
cd tmp
mkdir .cargo && touch .cargo/config.toml

.cargo/config.toml:

[target.x86_64-unknown-linux-musl]
linker = "/home/mizu/src/bin/zigcc"

[target.aarch64-unknown-linux-musl]
linker = "/home/mizu/src/bin/zigcc"

zigcc script:

#!/bin/sh
zig cc -target aarch64-linux-musl $@

Trying to build:

mizu@arch ~/s/tmp (master)> cargo build -v -j8 --target --release --target aarch64-unknown-linux-musl
   Compiling tmp v0.1.0 (/home/mizu/src/tmp)
     Running `rustc --crate-name tmp --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no -C metadata=2a52e9ef211a284c -C extra-filename=-2a52e9ef211a284c --out-dir /home/mizu/src/tmp/target/aarch64-unknown-linux-musl/release/deps --target aarch64-unknown-linux-musl -C linker=/home/mizu/src/bin/zigcc -L dependency=/home/mizu/src/tmp/target/aarch64-unknown-linux-musl/release/deps -L dependency=/home/mizu/src/tmp/target/release/deps`
error: linking with `/home/mizu/src/bin/zigcc` failed: exit status: 1
  |
  = note: "/home/mizu/src/bin/zigcc" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crt1.o" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crti.o" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtbegin.o" "/home/mizu/src/tmp/target/aarch64-unknown-linux-musl/release/deps/tmp-2a52e9ef211a284c.tmp.6928ceeb-cgu.0.rcgu.o" "/home/mizu/src/tmp/target/aarch64-unknown-linux-musl/release/deps/tmp-2a52e9ef211a284c.tmp.6928ceeb-cgu.1.rcgu.o" "/home/mizu/src/tmp/target/aarch64-unknown-linux-musl/release/deps/tmp-2a52e9ef211a284c.tmp.6928ceeb-cgu.2.rcgu.o" "/home/mizu/src/tmp/target/aarch64-unknown-linux-musl/release/deps/tmp-2a52e9ef211a284c.tmp.6928ceeb-cgu.3.rcgu.o" "/home/mizu/src/tmp/target/aarch64-unknown-linux-musl/release/deps/tmp-2a52e9ef211a284c.4ucp8ulk07m9gm2r.rcgu.o" "-Wl,--as-needed" "-L" "/home/mizu/src/tmp/target/aarch64-unknown-linux-musl/release/deps" "-L" "/home/mizu/src/tmp/target/release/deps" "-L" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libstd-bdbf6aa170ce7026.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libpanic_unwind-5be1c53da60b5881.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libminiz_oxide-1959fbdf206646e4.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libadler-a47f5830822f9c02.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libobject-cd98f396222b4d7e.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libaddr2line-d38f6341d62392ad.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libgimli-a61cab30ff0219ae.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libstd_detect-9f17ab861ecf115c.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/librustc_demangle-352911c204e74fa0.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libhashbrown-9aa532a01c969bdd.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/librustc_std_workspace_alloc-26d9b724dec55e94.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libunwind-d8b041d6a66897d1.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libcfg_if-06496de8cf6164c5.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/liblibc-a4a4ce071f33e013.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/liballoc-ef91896e3c9fecd3.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/librustc_std_workspace_core-c0638f1a74e9729c.rlib" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libcore-9451ddfb08bf3e9a.rlib" "-Wl,--end-group" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-deb7323b0a537dc8.rlib" "-Wl,-Bdynamic" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-nostartfiles" "-L" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib" "-L" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained" "-o" "/home/mizu/src/tmp/target/aarch64-unknown-linux-musl/release/deps/tmp-2a52e9ef211a284c" "-Wl,--gc-sections" "-static" "-no-pie" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtend.o" "/home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crtn.o"
  = note: warning: unsupported linker arg: --as-needed
          warning: unsupported linker arg: -Bstatic
          warning: unsupported linker arg: -Bdynamic
          warning: unsupported linker arg: -znoexecstack
          warning: unsupported linker arg: -zrelro
          warning: unsupported linker arg: -znow
          ld.lld: error: duplicate symbol: _start
          >>> defined at crt1.c
          >>>            /home/mizu/.cache/zig/o/f6cd4be2bc5abdf530e2d00f59810213/crt1.o:(.text+0x0)
          >>> defined at crt1.c
          >>>            /home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crt1.o:(.text+0x0)

          ld.lld: error: duplicate symbol: _start_c
          >>> defined at crt1.c
          >>>            /home/mizu/.cache/zig/o/f6cd4be2bc5abdf530e2d00f59810213/crt1.o:(_start_c)
          >>> defined at crt1.c
          >>>            /home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crt1.o:(.text._start_c+0x0)

          ld.lld: error: duplicate symbol: _init
          >>> defined at /home/mizu/.cache/zig/o/15297845b4426c92f203a9bd9c2fabb7/crti.o:(.init+0x0)
          >>> defined at /home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crti.o:(.init+0x0)

          ld.lld: error: duplicate symbol: _fini
          >>> defined at /home/mizu/.cache/zig/o/15297845b4426c92f203a9bd9c2fabb7/crti.o:(.fini+0x0)
          >>> defined at /home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/self-contained/crti.o:(.fini+0x0)

          ld.lld: error: duplicate symbol: __init_tp
          >>> defined at __init_tls.c
          >>>            __init_tls.lo:(__init_tp) in archive /home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/liblibc-a4a4ce071f33e013.rlib
          >>> defined at __init_tls.c
          >>>            /home/mizu/.cache/zig/o/b2b8d5340238cc9049a2acf66d725b20/__init_tls.o:(.text.__init_tp+0x0) in archive /home/mizu/.cache/zig/o/ba2fec6018ac82e4bb0fec9898caac2f/libc.a

          ld.lld: error: duplicate symbol: __copy_tls
          >>> defined at __init_tls.c
          >>>            __init_tls.lo:(__copy_tls) in archive /home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/liblibc-a4a4ce071f33e013.rlib
          >>> defined at __init_tls.c
          >>>            /home/mizu/.cache/zig/o/b2b8d5340238cc9049a2acf66d725b20/__init_tls.o:(.text.__copy_tls+0x0) in archive /home/mizu/.cache/zig/o/ba2fec6018ac82e4bb0fec9898caac2f/libc.a

          ld.lld: error: duplicate symbol: __progname
          >>> defined at libc.c
          >>>            libc.lo:(__progname) in archive /home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/liblibc-a4a4ce071f33e013.rlib
          >>> defined at libc.c
          >>>            /home/mizu/.cache/zig/o/10d9697181e072b2c49780417398bd92/libc.o:(.bss.__progname+0x0) in archive /home/mizu/.cache/zig/o/ba2fec6018ac82e4bb0fec9898caac2f/libc.a

          ld.lld: error: duplicate symbol: __progname_full
          >>> defined at libc.c
          >>>            libc.lo:(__progname_full) in archive /home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/liblibc-a4a4ce071f33e013.rlib
          >>> defined at libc.c
          >>>            /home/mizu/.cache/zig/o/10d9697181e072b2c49780417398bd92/libc.o:(.bss.__progname_full+0x0) in archive /home/mizu/.cache/zig/o/ba2fec6018ac82e4bb0fec9898caac2f/libc.a

          ld.lld: error: duplicate symbol: __get_handler_set
          >>> defined at sigaction.c
          >>>            sigaction.lo:(__get_handler_set) in archive /home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/liblibc-a4a4ce071f33e013.rlib
          >>> defined at sigaction.c
          >>>            /home/mizu/.cache/zig/o/49cee9df8f412a767e3a9c38d8455fb6/sigaction.o:(.text.__get_handler_set+0x0) in archive /home/mizu/.cache/zig/o/ba2fec6018ac82e4bb0fec9898caac2f/libc.a

          ld.lld: error: duplicate symbol: __libc_sigaction
          >>> defined at sigaction.c
          >>>            sigaction.lo:(__libc_sigaction) in archive /home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/liblibc-a4a4ce071f33e013.rlib
          >>> defined at sigaction.c
          >>>            /home/mizu/.cache/zig/o/49cee9df8f412a767e3a9c38d8455fb6/sigaction.o:(.text.__libc_sigaction+0x0) in archive /home/mizu/.cache/zig/o/ba2fec6018ac82e4bb0fec9898caac2f/libc.a

          ld.lld: error: duplicate symbol: __sigaction
          >>> defined at sigaction.c
          >>>            sigaction.lo:(__sigaction) in archive /home/mizu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-musl/lib/liblibc-a4a4ce071f33e013.rlib
          >>> defined at sigaction.c
          >>>            /home/mizu/.cache/zig/o/49cee9df8f412a767e3a9c38d8455fb6/sigaction.o:(.text.__sigaction+0x0) in archive /home/mizu/.cache/zig/o/ba2fec6018ac82e4bb0fec9898caac2f/libc.a

error: could not compile `tmp` due to previous error

Caused by:
  process didn't exit successfully: `rustc --crate-name tmp --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no -C metadata=2a52e9ef211a284c -C extra-filename=-2a52e9ef211a284c --out-dir /home/mizu/src/tmp/target/aarch64-unknown-linux-musl/release/deps --target aarch64-unknown-linux-musl -C linker=/home/mizu/src/bin/zigcc -L dependency=/home/mizu/src/tmp/target/aarch64-unknown-linux-musl/release/deps -L dependency=/home/mizu/src/tmp/target/release/deps` (exit status: 1)

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 at

Building for real this time:

# < copy the build command from where it starts "/home/mizu/src/bin/zigcc" "/home/mizu/.rustup/ ....etc..  >
# < paste to build.sh >
sd '("[^"]*(liblibc|crt1\.o|crti\.o)[^"]*")' ' ' build.sh   # naive attempt to remove unwanted libs (double check with the error messages!)
sh build.sh
I'm using `fish` shell and [sd](https://github.com/chmln/sd) instead of `sed` It'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 at `target/aarch64-unknown-linux-musl/release/deps/` This also works for `x86_64-linux-musl` target.
syrusakbary commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

Any suggestion how to fix or workaround this issue?

You could expose the respective lld options in the build system and linker code.

Cloudef commented 2 years ago

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 commented 1 year ago

For anyone with issue, I use this zigcc script to remove duplicated files.

new_array=()

for value in "$@"
do
    [[ $value != *self-contained/*crt* ]] && new_array+=($value)
done

zig cc -target x86_64-linux-musl "${new_array[@]}"

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