• 源码编译risc-v虚拟机和编译器 riscv-gnu-toolchain 和 riscv-tools 在ubuntu 22.04


    1. 编译 riscv-gnu-toolchain

    1.1 预备环境

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

    1.2 下载源代码
    https://github.com/riscv-collab/riscv-gnu-toolchain

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

    1.3 编译elf 工具

    1. cd riscv-gnu-toolchain/
    2. for riscv64-unknown-elf-gcc:
    3. ./configure --prefix=/opt/riscv
    4. sudo make -j

    1.4 编译 linux 工具


    for riscv64-unknown-linux-gnu-gcc:

    1. ./configure --prefix=/opt/riscv --enable-multilib
    2. sudo make linux -j
    3. export PATH=/opt/riscv/bin:$PATH

    2. 编译 riscv-tools

    for Spike, the ISA simulator:

    2.1 预备环境

    1. $ 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
    2. $ sudo apt-get install device-tree-compiler

    2.2 下载代码
    https://github.com/riscv-software-src/riscv-tools

    git clone --recursive https://github.com/riscv-software-src/riscv-tools.git 

    2.3 编译并处理编译错误

    1. cd riscv-tools \
    2. && git submodule update --init --recursive \
    3. && export RISCV=/opt/riscv \
    4. && sudo ./build.sh

    -1. 设置了PATH
    export PATH=/opt/riscv/bin:$PATH

    0. 指定安装目录
    第一行加入RISCV

    1. vim build.sh
    2. RISCV=/opt/riscv

    1. 重复定义变量
    /home/hipper/ex/RISC-V_source_codes/riscv-tools/riscv-openocd/build/../src/jtag/drivers/bitbang.h:60: multiple definition of `bitbang_swd';
     

    //LL:: 60 line 注释掉 .h 文件中的这个变量定义声明

    2. 忘记包含头文件
    ../fesvr/dtm.cc:488:16: error: ‘runtime_error’ is not a member of ‘std’
     

    1. vim riscv-isa-sim/fesvr/dtm.cc
    2. #include <stdexcept>

    3. 忘记包含头文件
    ../riscv/devices.h:45:18: error: ‘runtime_error’ is not a member of ‘std’
     

    1. vim riscv-isa-sim/riscv/devices.h
    2. #include <stdexcept>

    4. 指定架构 rv64gc_zifencei

    ../machine/flush_icache.c:4: Error: unrecognized opcode `fence.i', extension `zifencei' required

    1. vim ./riscv-tools/build.sh
    2. CC= CXX= build_project riscv-pk --prefix=$RISCV --host=riscv64-unknown-elf --with-arch=rv64gc_zifencei


    5.
     multiple definition of `tohost'; /tmp/ccMXpCzH.o:(.sbss+0x10): first defined here

     extern 


     
    6.
    multiple definition of `fromhost'; /tmp/ccMXpCzH.o:(.sbss+0x8): first defined here

     extern 


     3. 示例


     3.1 源码 example:

    用如下编译器指示语句包一下

    #pragma GCC push_options
    #pragma GCC optimize ("no-tree-loop-distribute-patterns")

    ... ...

    #pragma GCC pop_options

    1. #include
    2. #pragma GCC push_options
    3. #pragma GCC optimize ("no-tree-loop-distribute-patterns")
    4. int main(void)
    5. { //printf("Hello RISC-V World!\n");
    6. int a = 2;
    7. int b = 3;
    8. int c = 2;
    9. c = a+b;
    10. printf("c=%d\n", c);
    11. return 0;
    12. }
    13. #pragma GCC pop_options

    3.2 编译示例
     

    1. $ riscv64-unknown-elf-gcc ./hello_world.c -o hello_world
    2. $ spike pk ./hello_world

    3.3 运行
     

    1. $ spike pk ./hello_world
    2. <stdin>:20.39-24.9: Warning (interrupt_provider): /cpus/cpu@0/interrupt-controller: Missing #address-cells in interrupt provider
    3. bbl loader
    4. c=5
    5. (base) hipper@hipper-G21:~/ex/RISC-V_source_codes/ex_tmp_riscv_tools$

    大家都这么来建立测试c代码文件:


     

    $echo -e '#include <stdio.h>\n int main(void) { printf("Hello RISC-V World!\\n"); return 0; }' > hello_world.c

    参考文件:

    ./riscv-tools/regression.sh

    4. 显摆显摆成果

    一些git diff的输出:

    1. (base) hipper@hipper-G21:~/ex/RISC-V_source_codes/riscv-tools$ git diff
    2. diff --git a/build.sh b/build.sh
    3. index 2ebe825..1ba0fe7 100755
    4. --- a/build.sh
    5. +++ b/build.sh
    6. @@ -3,6 +3,7 @@
    7. # Script to build RISC-V ISA simulator, proxy kernel, and GNU toolchain.
    8. # Tools will be installed to $RISCV.
    9. +RISCV=/opt/riscv
    10. . build.common
    11. echo "Starting RISC-V Toolchain build process"
    12. @@ -19,7 +20,7 @@ check_version autoconf 2.64 "OpenOCD build"
    13. build_project riscv-openocd --prefix=$RISCV --enable-remote-bitbang --enable-jtag_vpi --disable-werror
    14. build_project riscv-isa-sim --prefix=$RISCV
    15. -CC= CXX= build_project riscv-pk --prefix=$RISCV --host=riscv64-unknown-elf
    16. +CC= CXX= build_project riscv-pk --prefix=$RISCV --host=riscv64-unknown-elf --with-arch=rv64gc_zifencei
    17. build_project riscv-tests --prefix=$RISCV/riscv64-unknown-elf
    18. echo -e "\\nRISC-V Toolchain installation completed!"
    19. diff --git a/riscv-isa-sim b/riscv-isa-sim
    20. --- a/riscv-isa-sim
    21. +++ b/riscv-isa-sim
    22. @@ -1 +1 @@
    23. -Subproject commit 2710fe575e7e6a4e2418224f8d254d5ca31f6c0e
    24. +Subproject commit 2710fe575e7e6a4e2418224f8d254d5ca31f6c0e-dirty
    25. diff --git a/riscv-openocd b/riscv-openocd
    26. --- a/riscv-openocd
    27. +++ b/riscv-openocd
    28. @@ -1 +1 @@
    29. -Subproject commit 35eed36ffdd082f5abfc16d4cc93511f6e225284
    30. +Subproject commit 35eed36ffdd082f5abfc16d4cc93511f6e225284-dirty
    31. diff --git a/riscv-tests b/riscv-tests
    32. --- a/riscv-tests
    33. +++ b/riscv-tests
    34. @@ -1 +1 @@
    35. -Subproject commit 79064081503b53fdb44094e32ff54a3ab20a9bf2
    36. +Subproject commit 79064081503b53fdb44094e32ff54a3ab20a9bf2-dirty
    37. (base) hipper@hipper-G21:~/ex/RISC-V_source_codes/riscv-tools$


    ____________________________________________________________________________
     

    1. (base) hipper@hipper-G21:~/ex/RISC-V_source_codes/riscv-tools$ cd riscv-openocd/
    2. (base) hipper@hipper-G21:~/ex/RISC-V_source_codes/riscv-tools/riscv-openocd$ git diff
    3. diff --git a/src/jtag/drivers/bitbang.h b/src/jtag/drivers/bitbang.h
    4. index 577717ebd..32eb69f53 100644
    5. --- a/src/jtag/drivers/bitbang.h
    6. +++ b/src/jtag/drivers/bitbang.h
    7. @@ -57,7 +57,7 @@ struct bitbang_interface {
    8. void (*swdio_drive)(bool on);
    9. };
    10. -const struct swd_driver bitbang_swd;
    11. +//LL:: const struct swd_driver bitbang_swd;
    12. extern bool swd_mode;
    13. (base) hipper@hipper-G21:~/ex/RISC-V_source_codes/riscv-tools/riscv-openocd$


    ____________________________________________________________________________

    1. (base) hipper@hipper-G21:~/ex/RISC-V_source_codes/riscv-tools$ cd riscv-isa-sim/
    2. (base) hipper@hipper-G21:~/ex/RISC-V_source_codes/riscv-tools/riscv-isa-sim$ git diff
    3. diff --git a/fesvr/dtm.cc b/fesvr/dtm.cc
    4. index 5409321a..7c8f83c8 100644
    5. --- a/fesvr/dtm.cc
    6. +++ b/fesvr/dtm.cc
    7. @@ -6,7 +6,7 @@
    8. #include <string.h>
    9. #include <assert.h>
    10. #include <pthread.h>
    11. -
    12. +#include <stdexcept>
    13. #define RV_X(x, s, n) \
    14. (((x) >> (s)) & ((1 << (n)) - 1))
    15. #define ENCODE_ITYPE_IMM(x) \
    16. diff --git a/riscv/devices.h b/riscv/devices.h
    17. index 4e4d27ff..aee50892 100644
    18. --- a/riscv/devices.h
    19. +++ b/riscv/devices.h
    20. @@ -6,7 +6,7 @@
    21. #include <string>
    22. #include <map>
    23. #include <vector>
    24. -
    25. +#include <stdexcept>
    26. class processor_t;
    27. class abstract_device_t {
    28. (base) hipper@hipper-G21:~/ex/RISC-V_source_codes/riscv-tools/riscv-isa-sim$


    ____________________________________________________________________________

    参考:

    最后解决example 代码包裹问题,借鉴了这位仁兄的记录,发现遇到的问题也如出一辙:

    RSIC-V_h~k~f的博客-CSDN博客

  • 相关阅读:
    【Bond与你白话IaC之Terraform for Docker篇】 攻城狮如何向女友解释IaC呢?
    用Auth Analyzer插件批量测试接口越权,安全测试快人一步!
    【OpenCV】Chapter4.灰度变换与直方图
    SSM+Vue+Element-UI实现网上跳蚤市场
    《Python 自动化办公应用大全》书籍推荐(包邮送书五本)
    JavaScript理论篇1之基础理论
    我给大家免费公开五份阿里Java架构师学习手册助力金九银十
    关于组织开展2022年广东省技术先进型服务企业认定工作的通知
    user32dIl出错,丢失条目:lockworkstation是怎么回事
    信息安全基础笔记
  • 原文地址:https://blog.csdn.net/eloudy/article/details/132858306