RISC-V GNU 工具鏈:安裝與使用


1. 安裝Wmware和unbuntu,我安裝的是Wmware workstation pro 12.1.1 build-3770994, unbuntu 是18.04.2 amd版本, ubuntu-18.04.2-desktop-amd64.iso

2. 安裝好unbuntu后,在/home/{username}目錄里面創建目錄riscv, 我的用戶名是kaguo,所以創建的目錄就是/home/kaguo/riscv

3.  cd /home/kaguo/riscv , 進入riscv目錄后,執行以下命令,安裝一些編譯riscv工具鏈需要的庫。

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

詳見:https://github.com/riscv/riscv-gnu-toolchain

建議把ubuntu的源鏡像改為阿里雲的,我開始用默認的會遇到 libtool庫不能更新的問題。請參考

https://yq.aliyun.com/articles/639051

可以用下面命令先安裝vim和gvim

sudo apt-get install vim
sudo apt-get install vim-gtk

4. 在riscv目錄中執行以下命令,

     sudo apt install git //首先安裝git

     git clone https://github.com/riscv/riscv-tools.git


接着你可以用下面的命令

$ git submodule update --init --recursive

將會下載下面的幾個模塊

  • Spike, the ISA simulator
  • riscv-tests, a battery of ISA-level tests
  • riscv-opcodes, the enumeration of all RISC-V opcodes executable by the simulator
  • riscv-pk, which contains bbl, a boot loader for Linux and similar OS kernels, and pk, a proxy kernel that services system calls for a target-machine application by forwarding them to the host machine

但是如果網絡環境不好,可以使用下面命令一個模塊一個模塊單獨下載:

git clone --recursive https://github.com/riscv/riscv-openocd.git
git clone --recursive https://github.com/riscv/riscv-isa-sim.git
git clone --recursive https://github.com/riscv/riscv-opcodes.git
git clone --recursive https://github.com/riscv/riscv-pk.git
git clone --recursive https://github.com/riscv/riscv-tests.git

5.下載riscv gun toolchain,

在目錄/home/kaguo/riscv,執行

git clone --recursive https://github.com/riscv/riscv-gnu-toolchain

或者

git clone https://github.com/riscv/riscv-gnu-toolchain
cd riscv-gnu-toolchain
git submodule update --init --recursive

將會下載riscv gun工具鏈,網絡環境不好的化,可以按下面的方法,一個模塊一個模塊單獨下載:

git clone https://github.com/riscv/riscv-gnu-toolchain
cd riscv-gnu-toolchain
git clone --recursive https://github.com/riscv/riscv-qemu.git
git clone --recursive https://github.com/riscv/riscv-newlib.git
git clone --recursive https://github.com/riscv/riscv-binutils-gdb.git
git clone --recursive https://github.com/riscv/riscv-dejagnu.git
git clone --recursive https://github.com/riscv/riscv-glibc.git
git clone --recursive https://github.com/riscv/riscv-gcc.git

因為這樣下載的目錄和前一種方式不同,主要是qemu和gdb,binutils三個模塊,可以進行一些copy工作,使得qemu, riscv-binutils,riscv-gdb三個目錄有數據,否則編譯會出錯。

cd riscv-qemu
cp -a * ../qemu
cd riscv-binutils-gdb
cp -a * ../riscv-gdb
cp -a * ../riscv-binutils

6.先編譯riscv gun toolchain,再編譯riscv tools,否則pk模塊會編譯出錯

cd /home/kaguo/riscv/riscv-tools/riscv-tools/riscv-gnu-toolchain,

vim ~/.bashrc

在最后增加兩行,

export  RISCV="/home/kaguo/riscv/riscv-tools/riscv-gnu-toolchain"
export PATH=$PATH:$RISCV/bin
然后source ~/.bashrc
現在用下面的命令開始編譯riscv gun tool chain
./configure --prefix=$RISCV
make
https://github.com/riscv/riscv-gnu-toolchain
這樣會編譯出64位的gcc riscv工具鏈,如果想同時產生32位工具鏈,可以用下面的命令

 
 
 
         
./configure --prefix=$RISCV --with-arch=rv32gc --with-abi=ilp32d
make


如果出現ifconfig不能找問題,安裝net-tools

 sudo apt-get install net-tools

 

7.  回到上一級目錄,cd /home/kaguo/riscv/riscv-tools/riscv-tools/
執行下面命令編譯riscv tools
 
 
 
         
./build.sh
編譯完后,大部分可以執行文件都會安裝到$RISCV/bin目錄,單似乎pk的默認沒有安裝。
如果遇到下面錯誤,可以刪除openocd 目錄,用git clone --recursive https://github.com/riscv/riscv-opcodes.git 重新下載。
Configuring project riscv-openocd
configure: error: cannot find install-sh, install.sh, or shtool in ".." "../.." "../../.."
進入 cd /home/kaguo/riscv/riscv-tools/riscv-tools/riscv-pk/build
 
 
 
         
cp pk dummy_payload config.status bbl_payload bbl /home/kaguo/riscv/riscv-tools/riscv-tools/riscv-gnu-toolchain/bin
把pk的可執行文件copy到$RISCV/bin目錄。
8. 最后一步,測試運行。
編寫hello.c文件
 
 
 
         
#include <stdio.h>
int main(void)
{
        printf("Hello World!\n");
        return 0;
}

編譯hello.c,生產riscv的可執行文件hello
 
 
 
         
riscv64-unknown-elf-gcc  -o  hello hello.c
這時候的 hello,並不能用./hello執行,因為它是riscv asi的機器碼,而我們的計算機是x86平台,我們可以spike模擬器來執行該文件。
 
 
 
         
spike pk hello
 結果為:
 
 
 
         
kaguo@ubuntu:~/riscv/riscv-tools/riscv-tools$ spike pk hello
bbl loader
Hello World!
注意:因為spike默認要分配2G的內存,所以我們要給虛擬機分配2G以上的內存空間,否則會出現下面的錯誤,我為ubuntu分配的內存是4G。
kaguo@ubuntu:~/riscv/riscv-tools/riscv-tools/riscv-gnu-toolchain$ spike
terminate called after throwing an instance of 'std::runtime_error'
what(): couldn't allocate 2147483648 bytes of target memory
Aborted (core dumped)

 spike -m128 pk hello
或者我們可以用m參數,每次制定為spike分配的內存數量。






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM