rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.85k stars 12.66k forks source link

Cross-compile a cdylib failed on target mipsel-unknown-linux-musl #116620

Open zouyalong-coder opened 1 year ago

zouyalong-coder commented 1 year ago

Hello, I've written a simple Rust library and compiled it for several targets. I found that it compiles successfully on x86_64-unknown-linux-gnu and x86_64-apple-darwin, both as dynamic and static libraries. However, when targeting mipsel-unknown-linux-musl, only the static library compiles successfully. When attempting to compile a dynamic library, I encountered different issues on macOS and Ubuntu.

On macOS, the error is:

ld: unknown option: --version-script=/var/folders/xh/250r_b1d10q0kklv9g7rwq4c0000gn/T/rustctZ046f/list
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

On ubuntu, the error is:

  = note: /usr/bin/ld: /home/zouyalong/mercku/mercku_os/rust/libmap/target/mipsel-unknown-linux-musl/release/deps/map.map.e6b571e945e58e5d-cgu.0.rcgu.o: Relocations in generic ELF (EM: 8)
          /usr/bin/ld: /home/zouyalong/mercku/mercku_os/rust/libmap/target/mipsel-unknown-linux-musl/release/deps/map.map.e6b571e945e58e5d-cgu.0.rcgu.o: Relocations in generic ELF (EM: 8)
          /usr/bin/ld: /home/zouyalong/mercku/mercku_os/rust/libmap/target/mipsel-unknown-linux-musl/release/deps/map.map.e6b571e945e58e5d-cgu.0.rcgu.o: Relocations in generic ELF (EM: 8)
          /usr/bin/ld: /home/zouyalong/mercku/mercku_os/rust/libmap/target/mipsel-unknown-linux-musl/release/deps/map.map.e6b571e945e58e5d-cgu.0.rcgu.o: Relocations in generic ELF (EM: 8)
          /usr/bin/ld: /home/zouyalong/mercku/mercku_os/rust/libmap/target/mipsel-unknown-linux-musl/release/deps/map.map.e6b571e945e58e5d-cgu.0.rcgu.o: Relocations in generic ELF (EM: 8)
          /usr/bin/ld: /home/zouyalong/mercku/mercku_os/rust/libmap/target/mipsel-unknown-linux-musl/release/deps/map.map.e6b571e945e58e5d-cgu.0.rcgu.o: Relocations in generic ELF (EM: 8)
          /usr/bin/ld: /home/zouyalong/mercku/mercku_os/rust/libmap/target/mipsel-unknown-linux-musl/release/deps/map.map.e6b571e945e58e5d-cgu.0.rcgu.o: Relocations in generic ELF (EM: 8)
          /home/zouyalong/mercku/mercku_os/rust/libmap/target/mipsel-unknown-linux-musl/release/deps/map.map.e6b571e945e58e5d-cgu.0.rcgu.o: error adding symbols: File in wrong format
          collect2: error: ld returned 1 exit status

I expected to see this happen: shared library can be built

Meta

macos: rustc --version --verbose:

rustc 1.72.0-nightly (fe7454bf4 2023-06-19)
binary: rustc
commit-hash: fe7454bf439c93cbe9ac8a8f7fcfacd5a40244c2
commit-date: 2023-06-19
host: x86_64-apple-darwin
release: 1.72.0-nightly
LLVM version: 16.0.5

ubuntu rustc --version --verbose:

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2
Backtrace

``` ```

saethlin commented 1 year ago

error adding symbols: File in wrong format

This linker error is often caused by attempting to link together artifacts compiled for different architectures. From your description of what works and what doesn't, it sounds like your dynamic build for mipsel is trying to link in a mixture of mipsel and x86_64 objects.

bjorn3 commented 1 year ago

If you cross-compile you need a linker capable of linking binaries for the target. In your case you seem to use the default host compiler which on macOS is incapable of linking ELF binaries and on x86_64 Ubuntu only capable of linking x86_64 binaries. In general you should get a cross-compilation C toolchain capable of producing binaries for the target and then point rustc to the gcc or clang inside it as linker. For musl specifically you may however be able to away with -Clink-arg=-fuse-ld=lld if you have the lld linker installed as rustc comes bundled with a precompiled musl libc and knows how to point the linker to it.