RustでRISC-Vバイナリ生成 2018/10版

近いうちにstableで使えるようになるはずです(8月にnightlyに入ったので、12週間後、来週あたり?にstableになるはずです)。 ですが、今必要なので、今確認できた方法を書きます。

下の環境で確認しています。

Ubuntu16.04@VirtualBox rustc 1.31.0-nightly (77af31408 2018-10-11)

参考にしているissueは下記です。

github.com

$ cargo new --lib riscv-hello
$ cd riscv-hello
$ rustup override add nightly
$ rustup target add riscv32imac-unknown-none-elf
$ cat src/lib.rs
#![no_std]

#[no_mangle]
pub fn test(x: i32, y: i32) -> i32 {
    x + y
}
$ cargo build --target riscv32imac-unknown-none-elf --release
$ rustup component add llvm-tools-preview
$ ~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-objdump -arch-name=riscv32 -d target/riscv32imac-unknown-none-elf/release/libriscv_hello.rlib
target/riscv32imac-unknown-none-elf/release/libriscv_hello.rlib(riscv_hello-15541eb62c865df0.riscv_hello.ahnzheao-cgu.0.rcgu.o):        file format ELF32-riscv

Disassembly of section .text.test:
test:
       0:       2e 95   add     a0, a0, a1
       2:       82 80   ret

最後のobjdumpはcargo経由でできるはずなのですが、cargo経由ですとなぜかx86_64のobjdumpが出力されます…。 もう少し色々試して、続報をお届けできるようにしたいですね。