How to build Rust programs under custom RISC-V target?

Hello! I recently tried to build a Rust program with RISC-V V vector extension, but main branch Rust does not have such a target (it only provides riscv64gc, but not V). In this case I try to use -Z build-std and a json based Rust target.

I wrote file riscv64gcv-unknown-linux-gnu.json as target:

{
  "arch": "riscv64",
  "code-model": "medium",
  "cpu": "generic-rv64",
  "data-layout": "e-m:e-p:64:64-i64:64-i128:128-n64-S128",
  "emit-debug-gdb-scripts": false,
  "env": "gnu",
  "executables": true,
  "features": "+m,+a,+f,+d,+c,+v",
  "linker-flavor": "ld.lld",
  "llvm-target": "riscv64",
  "max-atomic-width": 64,
  "os": "linux",
  "target-family": "unix",
  "panic-strategy": "abort",
  "target-c-int-width": "32",
  "target-endian": "little",
  "target-pointer-width": "64",
  "vendor": "unknown",
  "pre-link-args": {
    "gcc": [
        "-mabi=lp64d",
        "-Wl,--as-needed"
    ]
  },
  "has-rpath": true,
  "llvm-abiname": "lp64d",
  "dynamic-linking": true
}

I expect this project will build and in generated ELF file we can discover use of vector instrucions. However it reported error:

PS D:\IntrinRiscv\rust-rvv-intrinsics> cargo build --example vadd --target riscv64gcv-unknown-linux-gnu.json -Z build-std=std,libc,panic_abort
   Compiling compiler_builtins v0.1.70
   Compiling core v0.0.0 (D:\Applications\Rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core)
   Compiling libc v0.2.116
   Compiling cc v1.0.69
   Compiling memchr v2.4.1
   Compiling std v0.0.0 (D:\Applications\Rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std)
   Compiling rust-rvv-intrinsics v0.1.0 (D:\IntrinRiscv\rust-rvv-intrinsics)
   Compiling unwind v0.0.0 (D:\Applications\Rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\unwind)
   Compiling rustc-std-workspace-core v1.99.0 (D:\Applications\Rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\rustc-std-workspace-core)
   Compiling alloc v0.0.0 (D:\Applications\Rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\alloc)
   Compiling cfg-if v0.1.10
   Compiling adler v0.2.3
   Compiling rustc-demangle v0.1.21
   Compiling rustc-std-workspace-alloc v1.99.0 (D:\Applications\Rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\rustc-std-workspace-alloc)
   Compiling panic_abort v0.0.0 (D:\Applications\Rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\panic_abort)
   Compiling panic_unwind v0.0.0 (D:\Applications\Rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\panic_unwind)
   Compiling gimli v0.25.0
   Compiling hashbrown v0.12.0
   Compiling miniz_oxide v0.4.0
   Compiling object v0.26.2
   Compiling std_detect v0.1.5 (D:\Applications\Rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\stdarch\crates\std_detect)
   Compiling addr2line v0.16.0
   Compiling proc_macro v0.0.0 (D:\Applications\Rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\proc_macro)
error: linking with `lld` failed: exit code: 1
  |
  = note: "lld" "-flavor" "gnu" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\examples\\vadd-80725a840e4e6225.1evm6rhz32otobdg.rcgu.o" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\examples\\vadd-80725a840e4e6225.1tenkcyi623svufp.rcgu.o" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\examples\\vadd-80725a840e4e6225.20ozjuv3165vmrew.rcgu.o" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\examples\\vadd-80725a840e4e6225.42638p37927iclw1.rcgu.o" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\examples\\vadd-80725a840e4e6225.4f8iq8ccu6nae273.rcgu.o" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\examples\\vadd-80725a840e4e6225.4g89sdx7z0i8c2e.rcgu.o" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\examples\\vadd-80725a840e4e6225.4imavrblyp2yba0m.rcgu.o" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\examples\\vadd-80725a840e4e6225.5admkiou5lxz1qzz.rcgu.o" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\examples\\vadd-80725a840e4e6225.u4kq7k2spigw45k.rcgu.o" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\examples\\vadd-80725a840e4e6225.vuwrp8eos96o62e.rcgu.o" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\examples\\vadd-80725a840e4e6225.1vgsed6awf0prtnb.rcgu.o" "--as-needed" "-L" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps" "-L" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\debug\\deps" "-L" "D:\\Applications\\Rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\riscv64gcv-unknown-linux-gnu\\lib" "-Bstatic" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\librust_rvv_intrinsics-a0fbe7cccbc2d7ad.rlib" "--start-group" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libstd-e7c37d7857ae5613.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libpanic_abort-e2ee9a865d61b172.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libobject-06e7d31a625f993f.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libmemchr-c15b2d4c40112290.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libaddr2line-87be915ef7c54dd7.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libgimli-b4ddb86d33d2532a.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\librustc_demangle-1fd0b845f2feb518.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libstd_detect-8de508e8d46c15c7.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libhashbrown-1b3d9892f3460f13.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libminiz_oxide-f77756071922a243.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libadler-4b3882c10ba2a26b.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\librustc_std_workspace_alloc-70e356eeea693ad6.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libunwind-5759e5fe3be1ff09.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libcfg_if-e0d37e4f1b298b24.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\liblibc-eeab3a960a5fd818.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\liballoc-01d98cfb77a6748d.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\librustc_std_workspace_core-095bc7e859d28295.rlib" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libcore-6e079a56b471fa60.rlib" "--end-group" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\deps\\libcompiler_builtins-98f3e4f9b98c425f.rlib" "-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "--eh-frame-hdr" "-znoexecstack" "-L" "D:\\Applications\\Rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\riscv64gcv-unknown-linux-gnu\\lib" "-o" "D:\\IntrinRiscv\\rust-rvv-intrinsics\\target\\riscv64gcv-unknown-linux-gnu\\debug\\examples\\vadd-80725a840e4e6225" "--gc-sections" "-Texamples/bare.ld"
  = note: lld: error: unable to find library -lgcc_s
          lld: error: unable to find library -lutil
          lld: error: unable to find library -lrt
          lld: error: unable to find library -lpthread
          lld: error: unable to find library -lm
          lld: error: unable to find library -ldl
          lld: error: unable to find library -lc

What should I deal with this error? For reference, I uploaded my project into repository GitHub - luojia65/rust-rvv-intrinsics: test repository. Thanks!

To link programs for a target with std, you need the libc for said system to be installed. Also for linux targets you can't directly use lld or any other linker. You have to use gcc or clang. They wrap around the real linker and tell it where to find system libraries and all kinds of other flags to successfully link. The only targets I know of where you can directly invoke the linker are windows and baremetal targets. Cross-compiling from windows to linux is not the easiest thing to do. You may have better luck with wsl2.

1 Like

Happy cake day! I'd try with your advice.