RISC-V GNU编译环境搭建与运行实践

RISC-V GNU编译环境搭建与运行实践

  • riscv-gnu-toolchain交叉编译工具链
    • riscv-gnu-toolchain下载
    • riscv-gnu-toolchain编译
  • RISC-V运行环境
    • Spike + PK
    • qemu

riscv-gnu-toolchain交叉编译工具链

riscv-gnu-toolchain是riscv的gcc交叉编译工具链
github地址为:https://github.com/riscv/riscv-gnu-toolchain
gcc交叉编译工具链环境分为下载和编译两步,但是由于外网的原因,许多人在第一步中就被劝退,体验不是很好。下面分两步来介绍:

riscv-gnu-toolchain下载

riscv-gnu-toolchain工具链由以下几个子模块组成:
这些子模块包括:

  • riscv-newlib:https://github.com/riscv/riscv-newlib
  • riscv-binutils:https://github.com/riscv/riscv-binutils-gdb
  • riscv-gdb:https://github.com/riscv/riscv-binutils-gdb (riscv-gdb和riscv-binutils为同一个仓库下的不同分支)
  • riscv-dejagnu:https://github.com/riscv/riscv-dejagnu
  • riscv-glibc:https://github.com/riscv/riscv-glibc
  • riscv-gcc:https://github.com/riscv/riscv-gcc
  • riscv-qemu :riscv架构下的qemu(指令模拟器)

其中的 riscv-qemu 属于运行的环境,不算是工具链的组成部分,可以先不管。
如果有不错的外网环境的话,可以直接下载:

git clone  [email protected]:riscv/riscv-gnu-toolchain.git
cd riscv-gnu-toolchain
# 下载子模块
git submodule update --init --recursive

但我觉得就算是外网环境,下的也挺慢的。工具链的下载问题可以通过国内的码云(Gitee)镜像来解决,细节可以参考我的另一篇博客:利用码云镜像快速拉取riscv-gnu-toolchain工具链

riscv-gnu-toolchain编译

先下载一下依赖库:

sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev

再设置环境变量路径
比如在/etc/profile文件里,增加

export RISCV=".your.path./riscv-gnu-toolchain"
export PATH=$PATH:$RISCV/bin

使环境变量生效:
(注意使用此方法环境变量的生效期只为当前终端,关闭当前终端或另开一个终端都需要重新source一遍。但是机器重启后该环境变量则不需要每次使其设定生效。)

source /etc/profile

最后编译riscv-gnu-toolchain
riscv-gnu-toolchain 工具链分elf-gcc,linux-gnu-gcc两个版本,以及他们对应的32位和64位版本。两个的主要区别在于:

  • riscv32-unknown-elf-gcc,riscv64-unknown-elf-gcc:使用的riscv-newlib库(面向嵌入式的C库),而且只支持静态链接,不支持动态链接。
  • riscv32-unknown-linux-gnu-gcc,riscv64-unknown-linux-gnu-gcc:使用的是glibc标准库,支持动态链接。

个人认为从名字上便可以区分两者的使用场景,如果是编译简单,较小的elf程序,使用elf-gcc版本即可,如果编译比较大的程序或者需要动态库(比如编译linux,或opencv库等),推荐使用linux-gnu-gcc版本。

# 需要进入root权限:
sudo su

# 最后在source一下,使$RISCV环境变量生效
source /etc/profile

# 创建一个build文件夹安装,更干净一些
mkdir build
cd build

# 默认生成64位的编译工具:riscv64-unknown-elf-***
../configure --prefix=$RISCV
make -j4
# 安装至设定的$RISCV路径
make install 

# 生成32位的编译工具:riscv32-unknown-elf-***
../configure --prefix=$RISCV --with-arch=rv32gc --with-abi=ilp32d
make -j4
make install

# 编译linux-gnu-gcc版本(默认64位):riscv64-unknown-linux-gnu-***
../configure --prefix=$RISCV
make linux -j4
make install

编译成功后可以使用对应的gcc工具验证

riscv64-unknown-elf-gcc helloworld.c -o helloworld

如果你成功编译安装,但是系统提示找不到riscv64-unknown-elf-gcc,可能是因为重新开了新的终端。可以使用前source一下环境变量,使得$RISCV环境变量生效。或者重启之后,一劳永逸。

source /etc/profile
riscv64-unknown-elf-gcc helloworld.c -o helloworld

但是生成的二进制文件不能直接运行,因为主机架构是X86。
在这里插入图片描述

RISC-V运行环境

得到了RISC-V的二进制elf文件后,需要依赖指令模拟器才能在X86架构的主机上运行RISC-V的elf文件,比如Spike, qemu, gem5等。下面介绍两种常用的运行环境(Spike+PK 和 qemu),可自行选择。

Spike + PK

Spike github地址:https://github.com/riscv/riscv-isa-sim
PK github地址:https://github.com/riscv/riscv-pk
Spike是专门的RISC-V指令模拟器,支持RISC-V各种模块的指令(如A,F,D,V等),而且也有debug模式。
RISC-V Proxy Kernel(riscv-pk)是RISC-V的一个轻量级应用程序执行环境,可以托管静态链接的RISC-V ELF二进制文件。
可以把spike理解为模拟硬件,pk理解为操作系统,两个配合来加载运行RISC-V ELF二进制文件。

下载与编译:
spike

apt-get install device-tree-compiler
git clone [email protected]:riscv/riscv-isa-sim.git
cd riscv-isa-sim
mkdir build
cd build
../configure --prefix=$RISCV
make
[sudo] make install

pk

git clone [email protected]:riscv/riscv-pk.git
cd riscv-pk
mkdir build
cd build
../configure --prefix=$RISCV --host=riscv64-unknown-elf
make
make install

测试:

spike pk helloworld

在这里插入图片描述
需要注意的是,PK是不能够加载动态链接的RISCV ELF文件的(正好对应elf-gcc版本只支持静态链接编译)。而linux-gnu-gcc版本的riscv工具链默认是动态链接,直接使用spike + pk无法正常执行。
在这里插入图片描述
调整方式只需要手动指定静态链接即可(加上-static参数):
在这里插入图片描述

qemu

qemu是一个通用的、开源的机器仿真器和虚拟机。qemu支持各种架构的指令模拟,如arm,aarch64,以及RISC-V。具体的架构选择需要在编译的时候通过config文件指定。
qemu官网:https://www.qemu.org
下载与编译:

git clone https://git.qemu.org/git/qemu.git
cd qemu
git submodule init
git submodule update --recursive

或者直接下载zip包

wget https://download.qemu.org/qemu-5.2.0.tar.xz
tar xvJf qemu-5.2.0.tar.xz

riscv的qemu有两种:

  • riscv-64-linux-user为用户模式,可以运行基于riscv指令集编译的程序文件(target-list=riscv64-linux-user)
  • riscv64-softmmu为镜像模拟器,可以运行基于riscv指令集编译的linux镜像(target-list=riscv64-softmmu)

这里运行简单ELF程序我们直接用用户模式:

cd qemu
./configure --target-list=riscv64-linux-user
make -j4

测试:

./riscv64-linux-user/qemu-riscv64 helloworld

在这里插入图片描述
与Spike+PK只支持静态链接程序不同,qemu-riscv是支持静态链接和动态链接的程序的。
在这里插入图片描述

你可能感兴趣的:(RISCV,risc-v,qemu,交叉编译,github,gnu)