当前位置: 首页 > 工具软件 > zlib-ng > 使用案例 >

Crosstool-NG 编译 riscv64-unknown-elf-gcc

蓬运诚
2023-12-01

How to build toolchain

env setup

主机环境: ubuntu-20.04.3-desktop-amd64.iso
安装包 : sudo apt-get install net-tools openssh-server git vim make gcc gawk  bison flex  texinfo automake  libtool-bin cvs libncurses5-dev ninja-build libglib2.0-dev libpixman-1-dev help2man python3-dev

Clone the Crosstool-NG and build.

$ git clone https://github.com/crosstool-ng/crosstool-ng
$ git checkout crosstool-ng-1.24.0
	// 注意 : 1.24.0 中 RV32I 包括 6条csr指令 和  6条csr指令 和 1条 fence指令
	// 注意 : 1.25.0 中 RV32I 包括 6条csr指令 // 不包括  6条csr指令 和 1条 fence指令
	// 请查看 https://blog.csdn.net/u011011827/article/details/124197544
	// 请查看 https://blog.csdn.net/u011011827/article/details/121371305 中的 "RV32I指令集"
	 // 所以 1.24.0 编译器 中的 -march=rv32ima 等价于 1.25.0 编译器 中的 -march=rv32ima_zicsr_zifencei 
	
$ ./bootstrap
$ ./configure --enable-local
$ make

$ ./ct-ng menuconfig

Change the following configs:

CT_EXPERIMENTAL=y
CT_ARCH_RISCV=y
CT_ARCH_64=y
CT_ARCH_ARCH=rv32ima
CT_ARCH_ABI=ilp32
CT_MULTILIB=y
CT_DEBUG_GDB=y
CT_PREFIX_DIR=/opt/riscv # 安装目录
	https://github.com/riscv-collab/riscv-gnu-toolchain/issues/457
	https://github.com/riscv-collab/riscv-gnu-toolchain/issues/457
	注意: 两个链接说的是 rv64-cc 编译 库的时候 ,没有用 -mcmodel=medany ,
	默认为 your libraries were compiled with the default -mcmodel=medlow , which won't work with a load address of 0x80000000 on a 64-bit target.
	解决方案:加上 -mcmodel=medany 就可以了 . 即 CT_LIBC_NEWLIB_TARGET_CFLAGS="-mcmodel=medany"

Build the GNU toolchain for RISC-V.

$ ./ct-ng build // 这个过程需要下载软件包 // 这个过程总耗时大概 2小时

A toolchain is installed at ~/x-tools/riscv64-unknown-elf directory.

How to build

Add path of toolchain that is described above section.

$ export PATH=~/x-tools/riscv64-unknown-elf:$PATH
~/x-tools/riscv64-unknown-elf/bin $ ./riscv64-unknown-elf-gcc  --version 
riscv64-unknown-elf-gcc (crosstool-NG UNKNOWN) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ riscv64-unknown-elf-gcc --print-multi-lib 
.;
rv32i/ilp32;@march=rv32i@mabi=ilp32
rv32im/ilp32;@march=rv32im@mabi=ilp32
rv32iac/ilp32;@march=rv32iac@mabi=ilp32
rv32imac/ilp32;@march=rv32imac@mabi=ilp32
rv32imafc/ilp32f;@march=rv32imafc@mabi=ilp32f
rv64imac/lp64;@march=rv64imac@mabi=lp64
rv64imafdc/lp64d;@march=rv64imafdc@mabi=lp64d

$ riscv64-unknown-elf-gcc -Q --help=target
The following options are target specific:
  -mabi=                                ilp32
  -march=                               rv32ima
  -mbranch-cost=N                       3
  -mcmodel=                             [default]
  -mdiv                                 [enabled]
  -mexplicit-relocs                     [disabled]
  -mfdiv                                [disabled]
  -mplt                                 [enabled]
  -mpreferred-stack-boundary=           0
  -mrelax                               [enabled]
  -msave-restore                        [disabled]
  -msmall-data-limit=N                  0
  -mstrict-align                        [enabled]
  -mtune=PROCESSOR            

  Supported ABIs (for use with the -mabi= option):
    ilp32 ilp32d ilp32f lp64 lp64d lp64f

  Known code models (for use with the -mcmodel= option):
    medany medlow

使用中的问题

编译会生成gdb
但是该gdb 不能打印 sstatus 和其他一些列 csr
具体请查看
https://blog.csdn.net/u011011827/article/details/124197544

其他

// all
./configure --prefix=$PREFIX --enable-multilib --with-cmodel=medany
//some
./configure --prefix=$PREFIX --with-arch=rv32gc  --with-abi=ilp32d --disable-gdb

// default
https://five-embeddev.com/toolchain/2019/06/26/gcc-targets/
we pass --with-abi= to gcc to set the default ABI for compiler
  • ./ct-ng build 过程日志
$ ./ct-ng build
[INFO ]  Performing some trivial sanity checks
[WARN ]  Number of open files 1024 may not be sufficient to build the toolchain; increasing to 2048
[INFO ]  Build started 20220706.140504
[INFO ]  Building environment variables
[WARN ]  Directory '/home/pop/src' does not exist.
[WARN ]  Will not save downloaded tarballs to local storage.
[EXTRA]  Preparing working directories
[EXTRA]  Installing user-supplied crosstool-NG configuration
[EXTRA]  =================================================================
[EXTRA]  Dumping internal crosstool-NG configuration
[EXTRA]    Building a toolchain for:
[EXTRA]      build  = x86_64-pc-linux-gnu
[EXTRA]      host   = x86_64-pc-linux-gnu
[EXTRA]      target = riscv64-unknown-elf
[EXTRA]  Dumping internal crosstool-NG configuration: done in 0.15s (at 00:05)
[INFO ]  =================================================================
[INFO ]  Retrieving needed toolchain components' tarballs
[INFO ]  Retrieving needed toolchain components' tarballs: done in 0.75s (at 00:06)
[INFO ]  =================================================================
[INFO ]  Extracting and patching toolchain components
[INFO ]  Extracting and patching toolchain components: done in 0.70s (at 00:07)
[INFO ]  =================================================================
[INFO ]  Installing ncurses for build
[EXTRA]    Configuring ncurses
[EXTRA]    Building ncurses
[EXTRA]    Installing ncurses
[INFO ]  Installing ncurses for build: done in 31.82s (at 00:39)
[INFO ]  =================================================================
[INFO ]  Installing zlib for host
[EXTRA]    Configuring zlib
[EXTRA]    Building zlib
[EXTRA]    Installing zlib
[INFO ]  Installing zlib for host: done in 1.92s (at 00:41)
[INFO ]  =================================================================
[INFO ]  Installing GMP for host
[EXTRA]    Configuring GMP
[EXTRA]    Building GMP
[EXTRA]    Installing GMP
[INFO ]  Installing GMP for host: done in 42.93s (at 01:24)
[INFO ]  =================================================================
[INFO ]  Installing MPFR for host
[EXTRA]    Configuring MPFR
[EXTRA]    Building MPFR
[EXTRA]    Installing MPFR
[INFO ]  Installing MPFR for host: done in 25.71s (at 01:49)
[INFO ]  =================================================================
[INFO ]  Installing ISL for host
[EXTRA]    Configuring ISL
[EXTRA]    Building ISL
[EXTRA]    Installing ISL
[INFO ]  Installing ISL for host: done in 32.03s (at 02:21)
[INFO ]  =================================================================
[INFO ]  Installing MPC for host
[EXTRA]    Configuring MPC
[EXTRA]    Building MPC
[EXTRA]    Installing MPC
[INFO ]  Installing MPC for host: done in 7.66s (at 02:29)
[INFO ]  =================================================================
[INFO ]  Installing expat for host
[EXTRA]    Configuring expat
[EXTRA]    Building expat
[EXTRA]    Installing expat
[INFO ]  Installing expat for host: done in 8.99s (at 02:38)
[INFO ]  =================================================================
[INFO ]  Installing ncurses for host
[EXTRA]    Configuring ncurses
[EXTRA]    Building ncurses
[EXTRA]    Installing ncurses
[INFO ]  Installing ncurses for host: done in 25.38s (at 03:03)
[INFO ]  =================================================================
[INFO ]  Installing binutils for host
[EXTRA]    Configuring binutils
[EXTRA]    Building binutils
[EXTRA]    Installing binutils
[INFO ]  Installing binutils for host: done in 71.68s (at 04:15)
[INFO ]  =================================================================
[INFO ]  Installing C library headers
[INFO ]  Installing C library headers: done in 0.11s (at 04:15)
[INFO ]  =================================================================
[INFO ]  Installing core C gcc compiler
[EXTRA]    Configuring core C gcc compiler
[EXTRA]    Building gcc
[EXTRA]    Installing gcc
[EXTRA]    Housekeeping for core gcc compiler
[EXTRA]       '' --> lib (gcc)   lib (os)
[INFO ]  Installing core C gcc compiler: done in 733.36s (at 16:29)
[INFO ]  =================================================================
[INFO ]  Installing C library
[EXTRA]    Configuring C library
[EXTRA]    Building C library
[EXTRA]    Installing C library
[INFO ]  Installing C library: done in 49.64s (at 17:18)
[INFO ]  =================================================================
[INFO ]  Installing final gcc compiler
[EXTRA]    Configuring final gcc compiler
[EXTRA]    Building final gcc compiler
[EXTRA]    Installing final gcc compiler
[EXTRA]    Housekeeping for core gcc compiler
[EXTRA]       '' --> lib (gcc)   lib (os)
[INFO ]  Installing final gcc compiler: done in 676.29s (at 28:35)
[INFO ]  =================================================================
[INFO ]  Installing cross-gdb
[EXTRA]    Configuring cross gdb
[EXTRA]    Building cross gdb
[EXTRA]    Installing cross gdb
[EXTRA]    Installing '.gdbinit' template
[INFO ]  Installing cross-gdb: done in 343.00s (at 34:18)
[INFO ]  =================================================================
[INFO ]  Finalizing the toolchain's directory
[INFO ]    Stripping all toolchain executables
[EXTRA]    Creating toolchain aliases
[EXTRA]    Removing installed documentation
[EXTRA]    Collect license information from: /opt/source/crosstool-ng/.build/riscv64-unknown-elf/src
[EXTRA]    Put the license information to: /opt/toolchain/riscv/riscv64-unknown-elf/crosstool-ng-rv64/share/licenses
[INFO ]  Finalizing the toolchain's directory: done in 24.77s (at 34:44)
[INFO ]  Build completed at 20220706.143947
[INFO ]  (elapsed: 34:42.97)
[INFO ]  Finishing installation (may take a few seconds)...
 类似资料: