I'm trying to cross-compile a Rust program for the tier 3 platform riscv64gc-unknown-linux-musl
, but am running into problems, I believe either with linking or with instructing Cargo properly.
I've tried just running:
cargo +nightly build -Z build-std --target riscv64gc-unknown-linux-musl
as indicated by this question's answers and this question's answer, leading to a large log of errors:
= note: /usr/bin/ld: cannot find crt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory
/usr/bin/ld: cannot find crtbegin.o: No such file or directory
/usr/bin/ld: /home/lindon/Projects/Rust/open_computers_lib/target/riscv64gc-unknown-linux-musl/debug/deps/open_computers_lib-0a95fe466728767f.1n96vpe9sdkbhvag.rcgu.o: Relocations in generic ELF (EM: 243)
/usr/bin/ld: /home/lindon/Projects/Rust/open_computers_lib/target/riscv64gc-unknown-linux-musl/debug/deps/open_computers_lib-0a95fe466728767f.1n96vpe9sdkbhvag.rcgu.o: Relocations in generic ELF (EM: 243)
...
(redacting a longer list of that last line being repeated with different file names)
I've also tried using a pre-built cross-compiling toolchain from here as well as trying to build the toolchain myself from here, and then using the following script:
export PATH="/opt/riscv-musl/riscv64-unknown-linux-musl/bin/:$PATH"
cargo +nightly build -Z build-std --target riscv64gc-unknown-linux-musl # same as before
Which gives me (same error log for both the pre-built and personally built toolchains):
Compiling compiler_builtins v0.1.82
Compiling core v0.0.0 (/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
Compiling libc v0.2.135
Compiling cc v1.0.73
Compiling memchr v2.5.0
Compiling std v0.0.0 (/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std)
error: linking with `cc` failed: exit status: 1
|
= note: "cc" "-m64" "/tmp/rustcL7qcAR/symbols.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.0.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.1.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.10.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.11.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.12.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.13.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.14.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.15.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.2.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.3.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.4.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.5.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.6.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.7.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.8.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.build_script_build.731b8ff0-cgu.9.rcgu.o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73.1qnaa8r4se7kx3js.rcgu.o" "-Wl,--as-needed" "-L" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/deps" "-L" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-f30f0c72643db558.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-f568c570fff954b1.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-e7b70edde5e55443.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-e89379089032cd0e.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-e7d90005a619f6ea.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-e8fa7b53a3149a70.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-db4d651637ed1365.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-255c18ca7ff0a7c6.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-1791beb5b36e409b.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-ab2b83cf39766489.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-21993ed5a33c1bb3.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-b113a1d1429de7b6.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-0aacf29316aaa95c.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-fcf994c37af81dc5.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-a5319eed00c3264d.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-c6c03e024a2f1e46.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-522518611024dce5.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-05898138a596088a.rlib" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-b7c79d85cf21a511.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/lindon/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/lindon/Projects/Rust/open_computers_lib/target/debug/build/std-c8bbb31701f1fb73/build_script_build-c8bbb31701f1fb73" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs"
= note: /opt/riscv-musl/riscv64-unknown-linux-musl/bin/ld: unrecognised emulation mode: elf_x86_64
Supported emulations: elf64lriscv elf64lriscv_lp64f elf64lriscv_lp64 elf32lriscv elf32lriscv_ilp32f elf32lriscv_ilp32 elf64briscv elf64briscv_lp64f elf64briscv_lp64 elf32briscv elf32briscv_ilp32f elf32briscv_ilp32
collect2: error: ld returned 1 exit status
error: could not compile `std` due to previous error
warning: build failed, waiting for other jobs to finish...
...
The presence of nightly-x86_64-unknown-linux-gnu
in the path for std
leads me to believe I could be instructing Cargo incorrectly, or it could just be because my host system is x86_64-unknown-linux-gnu
.
The program in question just has an empty main
function.
All testing was done after running performing a full system upgrade and running rustup update
, and I am using 2021 edition of Rust in the project.
I am running Arch Linux.
This answer assumes the following:
riscv linux musl
C cross-toolchain installed at /opt/riscv-musl/
.
/opt/riscv-musl/
, simply replace that path in these instructions with the location the toolchain is installed to.rustup toolchain install nightly
With a Rust project folder structure like so:
├── src/
├── .cargo/
│ └── config.toml
└── compile.sh
Inside config.toml
:
[target.riscv64gc-unknown-linux-musl]
rustflags = [
"-C", "target-feature=-crt-static"
]
linker = "riscv64-unknown-linux-musl-gcc"
ar = "riscv64-unknown-linux-musl-ar"
Inside compile.sh
:
export PATH="/opt/riscv-musl/bin/:$PATH"
cargo +nightly build -Z build-std --target riscv64gc-unknown-linux-musl
If riscv64-unknown-linux-musl-gcc
and riscv64-unknown-linux-musl-ar
are already on your $PATH
then the export
statement in compile.sh
is unnecessary.
Now if you want to compile to the riscv64gc-unknown-linux-musl
target, just run compile.sh
.
In order to get to the functional config.toml
I ended up with, I took the following:
[target.riscv64gc-unknown-linux-musl]
rustflags = [
"-L/home/blackdragon2447/x-tools/riscv64-unknown-linux-musl/riscv64-unknown-linux-musl/sysroot/lib",
"-L/home/blackdragon2447/x-tools/riscv64-unknown-linux-musl/lib/gcc/riscv64-unknown-linux-musl/11.2.0/",
"-L/home/blackdragon2447/x-tools/riscv64-unknown-linux-musl/riscv64-unknown-linux-musl/sysroot/usr/lib",
"-C", "link-args=-lc",
"-C", "prefer-dynamic",
"-C", "target-feature=-crt-static"
]
ar = "riscv64-unknown-linux-musl-ar"
linker = "riscv64-unknown-linux-musl-gcc"
from here and trimmed it down, so if the one in this answer doesn't work for some reason, try playing around with that one (don't forget to change the paths in it).