MENU

Riscv Toolchain 构建

January 11, 2024 • 阅读: 2098 • 笔记&折腾

安装环境:

准备: binutils-2.37 源码包, gcc-11.1.0 源码包, newlib-4.1.0 源码包

设置安装环境目录

export RISCV=/usr/xx/

编译 binutils,准备: binutils-2.37 源码包

mkdir binutils-build
cd binutils-build
../binutils-2.37/configure --target=riscv32-unknown-elf --prefix=$RISCV/usr --disable-werr
make -j4 && make install

编译 GCC, 准备:gcc-11.1.0 源码包

mkdir gcc-build
cd gcc-build
../gcc-11.1.0/configure --target=riscv32-unknown-elf --prefix=$RISCV/usr --with-abi=ilp32 --with-arch=rv32ic --enable-languages=c,c++

或者

../gcc-11.1.0/configure --prefix=$RISCV/usr --target=riscv32-unknown-elf --with-sysroot=$RISCV/usr/riscv32-unknown-elf --with-abi=ilp32 --with-arch=rv32imac --with-newlib --enable-languages=c,c++ --with-system-zlib --disable-shared --disable-threads --disable-tls --disable-libmudflap --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libgomp --disable-nls --enable-checking=yes --disable-multilib  CFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections" CXXFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections"

修改 Makefile 搜索 -O2 -g 修改成 -Os 去掉 -g

make all-gcc all-target-libgcc -j8
make install-gcc install-target-libgcc
(make all && make install)

架构选择:

# --with-abi=ilp32 --with-arch=rv32imc
# --with-abi=ilp32f --with-arch=rv32imfc
# --with-abi=ilp32d --with-arch=rv32imfdc

编译 newlib, 准备: newlib-4.1.0 源码包

mkdir newlib-build
cd newlib-build
../newlib-4.1.0/configure --target=riscv32-unknown-elf -disable-nls --enable-newlib-io-long-long --enable-newlib-io-long-double --enable-newlib-io-c99-formats --disable-libssp --prefix=$RISCV/usr

或者

../newlib-4.1.0/configure --target=riscv32-unknown-elf -disable-nls --enable-newlib-io-long-long --enable-newlib-io-long-double --enable-newlib-io-c99-formats --disable-libssp --prefix=$RISCV/usr CFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections"  CXXFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections" 
--enable-newlib-multithread
make -j4 && make install

可选
nano-build, nano 库编译, 一个轻量级的运行库,直接用 newlib-4.1.0 源码包编译

mkdir nano-build
cd nano-build
../newlib-4.1.0/configure --target=riscv32-unknown-elf --prefix=$RISCV/usr --disable-newlib-supplied-syscalls --enable-newlib-reent-check-verify --enable-newlib-reent-small --enable-newlib-retargetable-locking --disable-newlib-fvwrite-in-streamio --disable-newlib-fseek-optimization --disable-newlib-wide-orient --enable-newlib-nano-malloc --disable-newlib-unbuf-stream-opt --enable-lite-exit --enable-newlib-global-atexit --enable-newlib-nano-formatted-io --disable-nls CFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections"  CXXFLAGS_FOR_TARGET="-Os -ffunction-sections -fdata-sections" 
make -j4  

注意:不需要make install
拷贝当前目录下的libc.a libg.a libgloss.a libm.a 到对应的安装目录并改名为libc_nano.a libg_nano.a libgloss_nano.a libm_nano.a
编译用户程序时 可以使用 选项 -specs=nano.specs --specs=nosys.specs,生成无系统调用版本

编译 qemu, riscv 程序运行模拟器。准备: qemu-6.0.0 源码包

mkdir qemu-build
cd qemu-build
../qemu-6.0.0/configure --target-list=riscv32-softmmu,riscv32-linux-user --prefix=$RISCV/usr
make -j4 && make install

编译 gdb, 调试工具。准备: gdb-10.2 源码包

../gdb-10.2/configure --target=riscv32-unknown-elf  --prefix=$RISCV/usr
make -j4 && make install