文章目录
一、说明
本文根据以下文章改编:
相关链接:
- riscv-gnu-toolchain
GNU toolchain for RISC-V, including GCC
https://github.com/riscv/riscv-gnu-toolchain - brew - Custom-GCC-and-cross-compilers
https://docs.brew.sh/Custom-GCC-and-cross-compilers - riscv-isa-sim
Spike, a RISC-V ISA Simulator
https://github.com/riscv-software-src/riscv-isa-sim - riscv-pk
RISC-V Proxy Kernel
https://github.com/riscv-software-src/riscv-pk
二、安装步骤
1、使用 brew 安装相关依赖
shell
brew install dtc gawk gnu-sed gmp mpfr libmpc isl zlib expat texinfo flock
brew tap riscv/riscv
brew install riscv-tools
brew install riscv-isa-sim
brew install qemu
brew 安装的文件包一般位于:/usr/local/Cellar
下
2、下载预编译好的 RISC-V 工具链
riscv64-unknown-elf-gcc-8.3.0-2020.04.0-x86_64-apple-darwin.tar.gz
https://static.dev.sifive.com/dev-tools/riscv64-unknown-elf-gcc-8.3.0-2020.04.0-x86_64-apple-darwin.tar.gz
解压后,将 bin 目录添加到环境变量
可以得到如下的可执行文件等:
riscv64-unknown-elf-addr2line
- riscv64-unknown-elf-gcc-ar
- riscv64-unknown-elf-ld.bfd
- riscv64-unknown-elf-ar
- riscv64-unknown-elf-gcc-nm
- riscv64-unknown-elf-nm
- riscv64-unknown-elf-as
- riscv64-unknown-elf-gcc-ranlib
- riscv64-unknown-elf-objcopy
- riscv64-unknown-elf-c++
- riscv64-unknown-elf-gcov
- riscv64-unknown-elf-objdump
- riscv64-unknown-elf-c++filt
- riscv64-unknown-elf-gcov-dump
- riscv64-unknown-elf-ranlib
- riscv64-unknown-elf-cpp
- riscv64-unknown-elf-gcov-tool
- riscv64-unknown-elf-readelf
- riscv64-unknown-elf-elfedit
- riscv64-unknown-elf-gdb
- riscv64-unknown-elf-size
- riscv64-unknown-elf-g++
- riscv64-unknown-elf-gdb-add-index
- riscv64-unknown-elf-strings
- riscv64-unknown-elf-gcc
- riscv64-unknown-elf-gprof
- riscv64-unknown-elf-strip
- riscv64-unknown-elf-gcc-8.3.0
- riscv64-unknown-elf-ld
3、下载 spike 预编译的二进制程序包
spike-pk-prebuilt-x86_64-apple-darwin.tar.gz
https://cloud.tsinghua.edu.cn/f/6246e90c407b4a508816/
解压后,制作 pk 软连接
shell
ln xxx/software/spike-pk-prebuilt-x86_64-apple-darwin/pk /usr/local/bin/pk
否则执行程序可能报错:
libc++abi: terminating due to uncaught exception of type std::runtime_error: could not open /usr/local/bin/pk (did you misspell it? If VCS, did you forget +permissive/+permissive-off?)
三、测试
实现如下功能:
-
生成 RISC-V 汇编
-
使用 gcc 把 RISC-V 汇编变成 RISC-V 可执行文件
-
用 spike 运行 RISC-V 可执行文件。
你的编译器 gcc qemu/spike
MiniDecaf 源文件 ------------> RISC-V 汇编 -----> 可执行文件 --------> 输出
1、编写文件 input.c
c
int main(){return 233;}
2、编译到 input.s
shell
riscv64-unknown-elf-gcc -march=rv32im -mabi=ilp32 -O3 -S input.c
得到 cat input.s
,你可以查看它的内容
shell
cat input.s
得到:
shell
.file "input.c"
.option nopic
.attribute arch, "rv32i2p1_m2p0"
.attribute unaligned_access, 0
.attribute stack_align, 16
.text
.section .text.startup,"ax",@progbits
.align 2
.globl main
.type main, @function
main:
li a0,122
ret
.size main, .-main
.ident "GCC: (g2ee5e430018-dirty) 12.2.0"
3、编译成可执行文件
shell
riscv64-unknown-elf-gcc -march=rv32im -mabi=ilp32 input.s
生成 a.out
文件
你也可以跳过 -s
直接生成可执行文件
shell
riscv64-unknown-elf-gcc -march=rv32im -mabi=ilp32 -O3 test.c
查看 a.out
文件属性
shell
file a.out
得到:
a.out: ELF 32-bit LSB executable, UCB RISC-V, soft-float ABI, version 1 (SYSV), statically linked, not stripped
4、执行 a.out
shell
spike --isa=RV32G /usr/local/bin/pk a.out
打印出
shell
bbl loader
继续执行
打印 $?
,这是 spike 的返回码,也就是我们 .c 文件 main 方法返回的值
shell
echo $?
伊织 2023-08-21