macOS - 上编译运行 risc-v (spike)

文章目录


一、说明

本文根据以下文章改编:


相关链接:


二、安装步骤

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

相关推荐
Hi2024021714 小时前
Vortex CUDA 生态适配:让 CUDA C、CUTLASS 与 Triton 在 RISC-V GPGPU 上运行
人工智能·risc-v·gpgpu
国科安芯1 天前
用 AS32X601 做一块关节电机驱动板:三相桥、电流采样、封波保护、FOC 代码一次讲透
risc-v·foc·bldc·关节电机·机器人关节·as32x601·三相逆变桥
传奇开心果编程1 天前
【SwiftUI入门练中学】第7课 动画与手势:让界面更灵动
学习·macos·ui·ios·swiftui·swift
硅基手札1 天前
【risc-v专栏 07】启动与 bare-metal 流程
risc-v
the3clipse1 天前
MacOS、Linux 与 Windows 视频编解码对比:AV1、HEVC、ProRes 与硬件加速优劣势
linux·windows·macos·hevc·av1·视频解码·硬件解码
硅基手札2 天前
【risc-v专栏 08】扩展生态与 -march/-mabi 匹配
risc-v
传奇开心果编程2 天前
【SwiftUI娓娓道来】第3课:让界面活起来——交互与动画指南
学习·macos·ui·ios·swiftui·swift
硅基手札2 天前
【risc-v专栏 06b】内存架构深挖:PMA / ePMP / Cache / CMO / MMU 细节 / RVWMO 形式化
架构·risc-v
朋友圈自动点赞工具2 天前
Gmail 委托邮箱在手机上关不掉
macos·智能手机·cocoa·硬件评测
小黄人软件2 天前
AndroidStudio老项目 macOS运行BlueTooth蓝牙串口助手(Android+Studio源码).rar
android·macos