Skip to content
陳鍾誠 edited this page Dec 25, 2018 · 22 revisions

工具鏈

經我測試,riscv-tools 最好的開發環境是 Mac,然後才是 Linux,而用 windows 則完全不行,別浪費時間了!

推薦使用 Mac 開發。

iMac 安裝的例子

參考: https://github.com/riscv/homebrew-riscv

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew tap riscv/riscv
$ brew install riscv-tools
$ brew test riscv-tools

ubuntu 的安裝例子

$ sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev libusb-1.0-0-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev device-tree-compiler pkg-config libexpat-dev
...
$ git clone https://github.com/riscv/riscv-tools.git
$ cd riscv-tools
$ git submodule update --init --recursive
$ pwd
/root/riscv/riscv-tools
$ export RISCV=/root/riscv/toolchain
$ ./build.sh

windows 安裝的例子:

這個行不通! (git submodule update --init --recursive 永遠執行不完,安裝了 15 萬個檔 1.07GB 還結束不了) 猜測是因為 windows 沒有 hardlink, 因此將資料重複放在許多資料夾下,於是才會有這麼多檔案。

gcc

安裝完之後就可以使用了! 讓我們先從一個 C 語言的 hello 程式開始!

$ cd code
$ cd 01-hello/
$ ls
hello.c
$ riscv64-unknown-elf-gcc -O2 -o hello hello.c
$ spike pk hello
bbl loader
Hello RISC-V

但是每次都要打 riscv64-unknown-elf-gcc 實在太長,於是我用 ln 指令設了 riscv64gcc 這個比較短的名稱以方便使用!

$ ln -s /usr/local/bin/riscv64-unknown-elf-gcc /usr/local/bin/riscv64gcc
$ riscv64gcc
riscv64gcc: fatal error: no input files
compilation terminated.

這樣之後就會比較輕鬆了!

錯誤

當我用雲端 linode 的 $5 美元主機安裝 riscv 時,用 gcc 編譯沒問題,但用 spike 執行時就有問題,錯誤如下:

root@localhost:~/test/riscv/code/01-hello# spike pk hello.o
terminate called after throwing an instance of 'std::runtime_error'
  what():  couldn't allocate 2147483648 bytes of target memory
Aborted (core dumped)

結果是我記憶體不夠,於是只好加參數 -m 限制記憶體,以下是一個範例!

root@localhost:~/test/riscv/code/01-hello# spike -m128 pk hello.o
Hello RISC-V

組譯與連結

使用 GNU 工具鏈的作法,我們可以這樣作:

$ riscv64-unknown-linux-gnu-as add.s -o add
$ riscv64-unknown-linux-gnu-gcc -c main.c -o main.o
$ riscv64-unknown-linux-gnu-ld --sysroot=/home/riscv/sysroot -dynamic-linker /lib/ld-linux-riscv64-lp64d.so.1 add.o main.o  -o main /home/riscv/sysroot/usr/lib/crt1.o -lc

指定指令集

riscv32-unknown-elf-gcc -Wall -O0 -march=rv32i hello.c -o hello
riscv32-unknown-elf-objdump -d hello > hello.dump

參考文獻

Clone this wiki locally