riscv gcc 工具链编译

编译riscv gcc

下载完毕后,就要开始编译。首先在riscv-gnu-toolchain根目录下,创建build目录。用于编译riscv gcc。

riscv gcc可以编译成以下几个版本

  • riscv32-unknown-elf-gcc
  • riscv64-unknown-elf-gcc
  • riscv32-unknown-linux-gnu-gcc
  • riscv64-unknown-linux-gnu-gcc
  • riscv64-multilib-elf-gcc
  • riscv64-liunx-multilib-gcc

以下编译,是基于 riscv架构支持 imc三种指令集,来进行编译的。如果想要支持其他的指令集,只需要修改--with-arch选项。

使用以下这个命令,将所有仓库,都clone下来。

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

以下是各个仓库的github地址:

riscv-gcc

https://github.com/riscv/riscv-gcc

riscv-glibc

https://github.com/riscv/riscv-glibc

riscv-newlib

https://github.com/riscv/riscv-newlib

riscv-dejagnu

https://github.com/riscv/riscv-dejagnu

riscv-gdb

https://github.com/riscv/riscv-binutils-gdb.git

riscv-binutils

https://github.com/riscv/riscv-binutils-gdb.git

riscv-qemu

https://github.com/riscv/riscv-qemu.git

ABI

Supported ABIs are ilp32 (32-bit soft-float), ilp32d (32-bit hard-float), ilp32f (32-bit with single-precision in registers and double in memory, niche use only), lp64 lp64f lp64d (same but with 64-bit long and pointers).

 编译注意:

1、glibc 和 gcc 不要去单独编译

2、ABI

 码云

mkdir /opt/riscv
export  RISCV=/opt/riscv
[root@centos7 ~]# git clone --recursive  https://gitee.com/mirrors/riscv-gnu-toolchain.git
Cloning into 'riscv-gnu-toolchain'...
remote: Enumerating objects: 51, done.
remote: Counting objects: 100% (51/51), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 8695 (delta 15), reused 0 (delta 0), pack-reused 8644
Receiving objects: 100% (8695/8695), 5.03 MiB | 0 bytes/s, done.
Resolving deltas: 100% (4719/4719), done.
Submodule 'qemu' (https://git.qemu.org/git/qemu.git) registered for path 'qemu'
Submodule 'riscv-binutils' (https://github.com/riscv-collab/riscv-binutils-gdb.git) registered for path 'riscv-binutils'
Submodule 'riscv-dejagnu' (https://github.com/riscv-collab/riscv-dejagnu.git) registered for path 'riscv-dejagnu'
Submodule 'riscv-gcc' (https://github.com/riscv-collab/riscv-gcc.git) registered for path 'riscv-gcc'
Submodule 'riscv-gdb' (https://github.com/riscv-collab/riscv-binutils-gdb.git) registered for path 'riscv-gdb'
Submodule 'riscv-glibc' (git://sourceware.org/git/glibc.git) registered for path 'riscv-glibc'
Submodule 'riscv-newlib' (git://sourceware.org/git/newlib-cygwin.git) registered for path 'riscv-newlib'
Cloning into 'qemu'...

 设置代理

git config --global url."https://github.com.cnpmjs.org/".insteadOf "https://github.com/"
[root@centos7 ~]# cd riscv-gnu-toolchain/
[root@centos7 riscv-gnu-toolchain]# mkdir build; cd build
[root@centos7 build]# ../configure --prefix=/opt/riscv --enable-multilib
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for grep that handles long lines and -e... /usr/bin/grep
checking for fgrep... /usr/bin/grep -F
checking for grep that handles long lines and -e... (cached) /usr/bin/grep
checking for bash... /bin/sh
checking for __gmpz_init in -lgmp... yes
checking for mpfr_init in -lmpfr... yes
checking for mpc_init2 in -lmpc... yes
checking for curl... /usr/bin/curl
checking for wget... /usr/bin/wget
checking for ftp... no
configure: creating ./config.status
config.status: creating Makefile
config.status: creating scripts/wrapper/awk/awk
config.status: creating scripts/wrapper/sed/sed
[root@centos7 build]# make -j128
[root@centos7 build]# ls
build-binutils-linux  build-gdb-linux  config.log  config.status  Makefile  scripts  stamps
[root@centos7 build]# ls ../src
ls: cannot access ../src: No such file or directory
[root@centos7 build]# 
[root@centos7 build]#   ls /opt/riscv/
bin  include  lib  libexec  riscv64-unknown-elf  share
You have new mail in /var/spool/mail/root
[root@centos7 build]#   ls /opt/riscv/bin/
riscv64-unknown-elf-addr2line  riscv64-unknown-elf-c++filt  riscv64-unknown-elf-gcc         riscv64-unknown-elf-gcc-ranlib  riscv64-unknown-elf-gprof     riscv64-unknown-elf-nm       riscv64-unknown-elf-readelf
riscv64-unknown-elf-ar         riscv64-unknown-elf-cpp      riscv64-unknown-elf-gcc-11.1.0  riscv64-unknown-elf-gcov        riscv64-unknown-elf-ld        riscv64-unknown-elf-objcopy  riscv64-unknown-elf-size
riscv64-unknown-elf-as         riscv64-unknown-elf-elfedit  riscv64-unknown-elf-gcc-ar      riscv64-unknown-elf-gcov-dump   riscv64-unknown-elf-ld.bfd    riscv64-unknown-elf-objdump  riscv64-unknown-elf-strings
riscv64-unknown-elf-c++        riscv64-unknown-elf-g++      riscv64-unknown-elf-gcc-nm      riscv64-unknown-elf-gcov-tool   riscv64-unknown-elf-lto-dump  riscv64-unknown-elf-ranlib   riscv64-unknown-elf-strip
[root@centos7 build]# 

 

在build目录下,有如下的文件夹和文件生成。

其中有newlib和newlib-nano,就表示c运行库,就是用的newlib以及newlib-nano

 

unknown-linux-gnu-gcc

make clean

[root@centos7 build]# make clean
rm -rf build-* stamps install-newlib-nano
[root@centos7 build]# 
[root@centos7 build]# mkdir /opt/riscv-gnu
You have new mail in /var/spool/mail/root
[root@centos7 build]# ../configure --prefix=/opt/riscv-gnu
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for grep that handles long lines and -e... /usr/bin/grep
checking for fgrep... /usr/bin/grep -F
checking for grep that handles long lines and -e... (cached) /usr/bin/grep
checking for bash... /bin/sh
checking for __gmpz_init in -lgmp... yes
checking for mpfr_init in -lmpfr... yes
checking for mpc_init2 in -lmpc... yes
checking for curl... /usr/bin/curl
checking for wget... /usr/bin/wget
checking for ftp... no
configure: creating ./config.status
config.status: creating Makefile
config.status: creating scripts/wrapper/awk/awk
config.status: creating scripts/wrapper/sed/sed
[root@centos7 build]# make linux -j128

make编译得到是我们最常用的unknown-elf-gcc等工具链,而make linux则是unknown-linux-gnu-gcc

[root@centos7 build]# ls /opt/riscv-gnu/bin/
riscv64-unknown-linux-gnu-addr2line  riscv64-unknown-linux-gnu-ld       riscv64-unknown-linux-gnu-readelf
riscv64-unknown-linux-gnu-ar         riscv64-unknown-linux-gnu-ld.bfd   riscv64-unknown-linux-gnu-size
riscv64-unknown-linux-gnu-as         riscv64-unknown-linux-gnu-nm       riscv64-unknown-linux-gnu-strings
riscv64-unknown-linux-gnu-c++filt    riscv64-unknown-linux-gnu-objcopy  riscv64-unknown-linux-gnu-strip
riscv64-unknown-linux-gnu-elfedit    riscv64-unknown-linux-gnu-objdump
riscv64-unknown-linux-gnu-gprof      riscv64-unknown-linux-gnu-ranlib

gcc生成失败

Resolving deltas: 100% (550451/550451), done.
Submodule path 'riscv-glibc': checked out '9826b03b747b841f5fc6de2054bf1ef3f5c4bdf3'

[root@centos7 build]# make clean
rm -rf build-* stamps install-newlib-nano
[root@centos7 build]# ls
config.log config.status Makefile scripts
[root@centos7 build]#

 

删除build

[root@centos7 build]# make distclean
rm -rf build-* stamps install-newlib-nano
rm -rf src
[root@centos7 build]# make clean
rm -rf build-* stamps install-newlib-nano
[root@centos7 build]# ls
config.log  config.status  Makefile  scripts
[root@centos7 build]# cd ..
[root@centos7 riscv-gnu-toolchain-gnu]# rm build/ -rf
[root@centos7 riscv-gnu-toolchain-gnu]# mkdir build; cd build
[root@centos7 build]# ../configure --prefix=/opt/riscv64-linux --with-arch=rv64imafdc --enable-linux
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for grep that handles long lines and -e... /usr/bin/grep
checking for fgrep... /usr/bin/grep -F
checking for grep that handles long lines and -e... (cached) /usr/bin/grep
checking for bash... /bin/sh
checking for __gmpz_init in -lgmp... yes
checking for mpfr_init in -lmpfr... yes
checking for mpc_init2 in -lmpc... yes
checking for curl... /usr/bin/curl
checking for wget... /usr/bin/wget
checking for ftp... no
configure: creating ./config.status
config.status: creating Makefile
config.status: creating scripts/wrapper/awk/awk
config.status: creating scripts/wrapper/sed/sed

 riscv-gcc

[root@centos7 riscv-gcc]#  ../configure --prefix=/opt/riscv64-linux  
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for grep that handles long lines and -e... /usr/bin/grep
checking for fgrep... /usr/bin/grep -F
checking for grep that handles long lines and -e... (cached) /usr/bin/grep
checking for bash... /bin/sh
checking for __gmpz_init in -lgmp... yes
checking for mpfr_init in -lmpfr... yes
checking for mpc_init2 in -lmpc... yes
checking for curl... /usr/bin/curl
checking for wget... /usr/bin/wget
checking for ftp... no
configure: creating ./config.status
config.status: creating Makefile
config.status: creating scripts/wrapper/awk/awk
config.status: creating scripts/wrapper/sed/sed
[root@centos7 riscv-gcc]# make linux -j128
Makefile:26: *** missing separator.  Stop.
[root@centos7 riscv-gcc]# vi Makefile +26

单独编译gcc

删除riscv-gcc

重新拉取下

$ git clone https://gitee.com/mirrors/riscv-gnu-toolchain

进入源码目录:

$ cd riscv-gnu-toolchain

注意上面 clone 的主仓库并不包含子仓库的内容,所以需要继续更新子仓库。注意这里首先排除了 qemu 这个子仓库,一来因为 qemu 完整下载太大;二来 qemu 对 toolchain 的编译本身来说其实并不需要。

$ git rm qemu
$ git submodule update --init --recursive

耐心等待子仓库下载完成。

复制代码
[root@centos7 riscv_build]# git clone https://gitee.com/mirrors/riscv-gnu-toolchain
Cloning into 'riscv-gnu-toolchain'...
remote: Enumerating objects: 51, done.
remote: Counting objects: 100% (51/51), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 8695 (delta 15), reused 0 (delta 0), pack-reused 8644
Receiving objects: 100% (8695/8695), 5.03 MiB | 8.94 MiB/s, done.
Resolving deltas: 100% (4719/4719), done.
[root@centos7 riscv_build]# cd riscv-gnu-toolchain
[root@centos7 riscv-gnu-toolchain]# git rm qemu
rm 'qemu'
[root@centos7 riscv-gnu-toolchain]# git submodule update --init --recursive
Submodule 'riscv-binutils' (https://github.com/riscv-collab/riscv-binutils-gdb.git) registered for path 'riscv-binutils'
Submodule 'riscv-dejagnu' (https://github.com/riscv-collab/riscv-dejagnu.git) registered for path 'riscv-dejagnu'
Submodule 'riscv-gcc' (https://github.com/riscv-collab/riscv-gcc.git) registered for path 'riscv-gcc'
Submodule 'riscv-gdb' (https://github.com/riscv-collab/riscv-binutils-gdb.git) registered for path 'riscv-gdb'
Submodule 'riscv-glibc' (git://sourceware.org/git/glibc.git) registered for path 'riscv-glibc'
Submodule 'riscv-newlib' (git://sourceware.org/git/newlib-cygwin.git) registered for path 'riscv-newlib'
Cloning into 'riscv-binutils'...
复制代码

/root/riscv_build/riscv-gnu-toolchain/riscv-gcc/build

 1090  mkdir build
 1091  cd build/
 1092  ../configure --prefix=/opt/riscv64-linux
 1093  make  -j128
[root@centos7 build]# ls
aarch64-unknown-linux-gnu        lto-plugin                        stage1-fixincludes
build-aarch64-unknown-linux-gnu  Makefile                          stage1-gcc
compare                          prev-aarch64-unknown-linux-gnu    stage1-intl
config.log                       prev-fixincludes                  stage1-libbacktrace
config.status                    prev-gcc                          stage1-libcody
c++tools                         prev-intl                         stage1-libcpp
fixincludes                      prev-libbacktrace                 stage1-libdecnumber
gcc                              prev-libcody                      stage1-libiberty
intl                             prev-libcpp                       stage1-lto-plugin
libbacktrace                     prev-libdecnumber                 stage1-zlib
libcc1                           prev-libiberty                    stage_current
libcody                          prev-lto-plugin                   stage_final
libcpp                           prev-zlib                         stage_last
libdecnumber                     serdep.tmp                        zlib
libiberty                        stage1-aarch64-unknown-linux-gnu
[root@centos7 build]# pwd
/root/riscv_build/riscv-gnu-toolchain/riscv-gcc/build
[root@centos7 build]# ls /opt/riscv64-linux/bin/
aarch64-unknown-linux-gnu-c++         gcc-ar                               riscv64-unknown-linux-gnu-elfedit
aarch64-unknown-linux-gnu-g++         gcc-nm                               riscv64-unknown-linux-gnu-gprof
aarch64-unknown-linux-gnu-gcc         gcc-ranlib                           riscv64-unknown-linux-gnu-ld
aarch64-unknown-linux-gnu-gcc-11.1.0  gcov                                 riscv64-unknown-linux-gnu-ld.bfd
aarch64-unknown-linux-gnu-gcc-ar      gcov-dump                            riscv64-unknown-linux-gnu-nm
aarch64-unknown-linux-gnu-gcc-nm      gcov-tool                            riscv64-unknown-linux-gnu-objcopy
aarch64-unknown-linux-gnu-gcc-ranlib  gfortran                             riscv64-unknown-linux-gnu-objdump
aarch64-unknown-linux-gnu-gfortran    lto-dump                             riscv64-unknown-linux-gnu-ranlib
c++                                   riscv64-unknown-linux-gnu-addr2line  riscv64-unknown-linux-gnu-readelf
cpp                                   riscv64-unknown-linux-gnu-ar         riscv64-unknown-linux-gnu-size
g++                                   riscv64-unknown-linux-gnu-as         riscv64-unknown-linux-gnu-strings
gcc                                   riscv64-unknown-linux-gnu-c++filt    riscv64-unknown-linux-gnu-strip

是aarch64

[root@centos7 build]# make distclean
make[1]: Entering directory `/root/riscv_build/riscv-gnu-toolchain/riscv-gcc/build'
rm -f stage_current
make[1]: Leaving directory `/root/riscv_build/riscv-gnu-toolchain/riscv-gcc/build'
rm -rf stage1-* 
rm -rf stage2-* 

gcc --target=riscv64-unknown   --with-arch

[root@centos7 build]# ../configure --prefix=/opt/riscv64-linux --target=riscv64-unknown-linux-gnu
checking build system type... aarch64-unknown-linux-gnu
checking host system type... aarch64-unknown-linux-gnu
checking target system type... riscv64-unknown-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether ln works... yes
checking whether ln -s works... yes
checking for a sed that does not truncate output... /usr/bin/sed
checking for gawk... gawk
checking for libatomic support... yes
checking for libitm support... no
checking for libsanitizer support... yes
checking for libvtv support... no
checking for libhsail-rt support... no
checking for libphobos support... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking whether g++ accepts -static-libstdc++ -static-libgcc... yes
checking for gnatbind... gnatbind
checking for gnatmake... gnatmake
checking whether compiler driver understands Ada... yes
checking how to compare bootstrapped objects... cmp --ignore-initial=16 $$f1 $$f2
checking whether g++ supports C++11 features by default... no
checking whether g++ supports C++11 features with -std=gnu++11... yes
checking for objdir... .libs
checking for the correct version of gmp.h... yes
checking for the correct version of mpfr.h... buggy but acceptable
checking for the correct version of mpc.h... yes
checking for the correct version of the gmp/mpfr/mpc libraries... yes
checking for isl 0.15 or later... no
required isl version is 0.15 or later
The following languages will be built: c,c++,fortran,lto,objc
*** This configuration is not supported in the following subdirectories:
     target-libitm target-libvtv gnattools gotools target-libada target-libhsail-rt target-libphobos target-zlib target-libgo target-libffi target-liboffloadmic
    (Any other directories should still work fine.)
checking for default BUILD_CONFIG... 
checking for --enable-vtable-verify... no
*** removing c++tools/Makefile to force reconfigure
checking for bison... bison -y
checking for bison... bison
checking for gm4... no
checking for gnum4... no
checking for m4... m4
checking for flex... flex
checking for flex... flex
checking for makeinfo... makeinfo
checking for expect... no
checking for runtest... no
checking for ar... ar
checking for as... as
checking for dlltool... no
checking for ld... ld
checking for lipo... no
checking for nm... nm
checking for ranlib... ranlib
checking for strip... strip
checking for windres... no
checking for windmc... no
checking for objcopy... objcopy
checking for objdump... objdump
checking for otool... no
checking for readelf... readelf
checking for riscv64-unknown-linux-gnu-cc... no
checking for riscv64-unknown-linux-gnu-gcc... no
checking for riscv64-unknown-linux-gnu-c++... no
checking for riscv64-unknown-linux-gnu-g++... no
checking for riscv64-unknown-linux-gnu-cxx... no
checking for riscv64-unknown-linux-gnu-gxx... no
checking for riscv64-unknown-linux-gnu-gcc... no
checking for riscv64-unknown-linux-gnu-gfortran... no
checking for riscv64-unknown-linux-gnu-gccgo... no
checking for riscv64-unknown-linux-gnu-gdc... no
checking for ar... /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin/ar
checking for as... /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin/as
checking for dlltool... no
checking for riscv64-unknown-linux-gnu-dlltool... no
checking for ld... /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin/ld
checking for lipo... no
checking for riscv64-unknown-linux-gnu-lipo... no
checking for nm... /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin/nm
checking for objcopy... /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin/objcopy
checking for objdump... /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin/objdump
checking for otool... no
checking for riscv64-unknown-linux-gnu-otool... no
checking for ranlib... /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin/ranlib
checking for readelf... /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin/readelf
checking for strip... /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin/strip
checking for windres... no
checking for riscv64-unknown-linux-gnu-windres... no
checking for windmc... no
checking for riscv64-unknown-linux-gnu-windmc... no
checking where to find the target ar... pre-installed in /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin
checking where to find the target as... pre-installed in /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin
checking where to find the target cc... just compiled
checking where to find the target c++... just compiled
checking where to find the target c++ for libstdc++... just compiled
checking where to find the target dlltool... pre-installed
checking where to find the target gcc... just compiled
checking where to find the target gfortran... just compiled
checking where to find the target gccgo... pre-installed
checking where to find the target gdc... pre-installed
checking where to find the target ld... pre-installed in /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin
checking where to find the target lipo... pre-installed
checking where to find the target nm... pre-installed in /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin
checking where to find the target objcopy... pre-installed in /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin
checking where to find the target objdump... pre-installed in /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin
checking where to find the target otool... pre-installed
checking where to find the target ranlib... pre-installed in /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin
checking where to find the target readelf... pre-installed in /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin
checking where to find the target strip... pre-installed in /opt/riscv64-linux/riscv64-unknown-linux-gnu/bin
checking where to find the target windres... pre-installed
checking where to find the target windmc... pre-installed
checking whether to enable maintainer-specific portions of Makefiles... no
configure: creating ./config.status
config.status: creating Makefile
compilation terminated.
compilation terminated.
make[4]: *** [Makefile:925: _gcov_execv.o] Error 1
In file included from ../../../../../libgcc/gthr.h:148,
                 from ../../../../../libgcc/libgcov-interface.c:27:
./gthr-default.h:35:10: fatal error: pthread.h: No such file or directory
   35 | #include <pthread.h>
      |          ^~~~~~~~~~~
compilation terminated.
In file included from ../../../../../libgcc/gthr.h:148,
                 from ../../../../../libgcc/libgcov-interface.c:27:
./gthr-default.h:35:10: fatal error: pthread.h: No such file or directory
   35 | #include <pthread.h>
      |          ^~~~~~~~~~~
make[4]: *** [Makefile:925: _gcov_execvp.o] Error 1
compilation terminated.
make[4]: *** [Makefile:925: _gcov_execve.o] Error 1
make[4]: *** [Makefile:925: _gcov_reset.o] Error 1
make[4]: *** [Makefile:925: _gcov_lock_unlock.o] Error 1
In file included from ../../../../../libgcc/gthr.h:148,
                 from ../../../../../libgcc/unwind-sjlj.c:31:
./gthr-default.h:35:10: fatal error: pthread.h: No such file or directory
   35 | #include <pthread.h>
      |          ^~~~~~~~~~~
compilation terminated.
In file included from ../../../../../libgcc/gthr.h:148,
                 from ../../../../../libgcc/emutls.c:31:
./gthr-default.h:35:10: fatal error: pthread.h: No such file or directory
   35 | #include <pthread.h>
      |          ^~~~~~~~~~~
compilation terminated.
make[4]: *** [../../../../../libgcc/shared-object.mk:14: unwind-sjlj.o] Error 1
make[4]: *** [../../../../../libgcc/shared-object.mk:14: emutls.o] Error 1
In file included from ../../../../../libgcc/gthr.h:148,
                 from ../../../../../libgcc/unwind-dw2.c:37:
./gthr-default.h:35:10: fatal error: pthread.h: No such file or directory
   35 | #include <pthread.h>
      |          ^~~~~~~~~~~
compilation terminated.
In file included from ../../../../../libgcc/gthr.h:148,
                 from ../../../../../libgcc/unwind-dw2-fde-dip.c:47:
./gthr-default.h:35:10: fatal error: pthread.h: No such file or directory
   35 | #include <pthread.h>
      |          ^~~~~~~~~~~
compilation terminated.
make[4]: *** [../../../../../libgcc/shared-object.mk:14: unwind-dw2.o] Error 1
make[4]: *** [../../../../../libgcc/shared-object.mk:14: unwind-dw2-fde-dip.o] Error 1
make[4]: Leaving directory '/root/riscv_build/riscv-gnu-toolchain/riscv-gcc/build/riscv64-unknown-linux-gnu/lib32/ilp32/libgcc'
make[3]: *** [Makefile:1211: multi-do] Error 1
make[3]: Leaving directory '/root/riscv_build/riscv-gnu-toolchain/riscv-gcc/build/riscv64-unknown-linux-gnu/libgcc'
make[2]: *** [Makefile:127: all-multi] Error 2
make[2]: Leaving directory '/root/riscv_build/riscv-gnu-toolchain/riscv-gcc/build/riscv64-unknown-linux-gnu/libgcc'
make[1]: *** [Makefile:13854: all-target-libgcc] Error 2
make[1]: Leaving directory '/root/riscv_build/riscv-gnu-toolchain/riscv-gcc/build'
make: *** [Makefile:961: all] Error 2
[root@centos7 build]# find ./ -name pthread.h
[root@centos7 build]# cd ..

参考How to Build a GCC Cross-Compiler

不能单独编译gcc

../configure --prefix=/opt/riscv64-linux --with-arch=rv64imafdc --with-abi=lp64 --enable-linux

make linux $(nproc)

最终编译glibc还是报错,gcc编译成功

checking for library containing waddstr... -lncursesw
checking for library containing tgetent... none required
checking size of unsigned long long... 8
checking size of unsigned long... 8
checking size of unsigned __int128... 16
checking for library containing dlopen... none required
checking whether to use expat... yes
checking for libexpat... no
configure: error: expat is missing or unusable
make[2]: *** [Makefile:10036: configure-gdb] Error 1
make[2]: Leaving directory '/root/riscv_build/riscv-gnu-toolchain/build/build-gdb-linux'
make[1]: *** [Makefile:854: all] Error 2
make[1]: Leaving directory '/root/riscv_build/riscv-gnu-toolchain/build/build-gdb-linux'
make: *** [Makefile:281: stamps/build-gdb-linux] Error 2
You have new mail in /var/spool/mail/root
[root@centos7 build]# ls /opt/riscv64/bin/
riscv64-unknown-linux-gnu-addr2line  riscv64-unknown-linux-gnu-ld       riscv64-unknown-linux-gnu-readelf
riscv64-unknown-linux-gnu-ar         riscv64-unknown-linux-gnu-ld.bfd   riscv64-unknown-linux-gnu-size
riscv64-unknown-linux-gnu-as         riscv64-unknown-linux-gnu-nm       riscv64-unknown-linux-gnu-strings
riscv64-unknown-linux-gnu-c++filt    riscv64-unknown-linux-gnu-objcopy  riscv64-unknown-linux-gnu-strip
riscv64-unknown-linux-gnu-elfedit    riscv64-unknown-linux-gnu-objdump
riscv64-unknown-linux-gnu-gprof      riscv64-unknown-linux-gnu-ranlib
[root@centos7 build]# ls /opt/riscv64-linux/bin/
riscv64-unknown-linux-gnu-addr2line   riscv64-unknown-linux-gnu-gcc-ar      riscv64-unknown-linux-gnu-lto-dump
riscv64-unknown-linux-gnu-ar          riscv64-unknown-linux-gnu-gcc-nm      riscv64-unknown-linux-gnu-nm
riscv64-unknown-linux-gnu-as          riscv64-unknown-linux-gnu-gcc-ranlib  riscv64-unknown-linux-gnu-objcopy
riscv64-unknown-linux-gnu-c++         riscv64-unknown-linux-gnu-gcov        riscv64-unknown-linux-gnu-objdump
riscv64-unknown-linux-gnu-c++filt     riscv64-unknown-linux-gnu-gcov-dump   riscv64-unknown-linux-gnu-ranlib
riscv64-unknown-linux-gnu-cpp         riscv64-unknown-linux-gnu-gcov-tool   riscv64-unknown-linux-gnu-readelf
riscv64-unknown-linux-gnu-elfedit     riscv64-unknown-linux-gnu-gfortran    riscv64-unknown-linux-gnu-size
riscv64-unknown-linux-gnu-g++         riscv64-unknown-linux-gnu-gprof       riscv64-unknown-linux-gnu-strings
riscv64-unknown-linux-gnu-gcc         riscv64-unknown-linux-gnu-ld          riscv64-unknown-linux-gnu-strip
riscv64-unknown-linux-gnu-gcc-11.1.0  riscv64-unknown-linux-gnu-ld.bfd
[root@centos7 build]# ls /opt/riscv64-linux/
bin  include  lib  lib64  libexec  riscv64-unknown-linux-gnu  share  sysroot
[root@centos7 build]# ls /opt/riscv64-linux/sysroot/
etc  lib  sbin  usr  var
[root@centos7 build]# 
[root@centos7 build]# /opt/riscv64-linux/bin/riscv64-unknown-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=/opt/riscv64-linux/bin/riscv64-unknown-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/opt/riscv64-linux/libexec/gcc/riscv64-unknown-linux-gnu/11.1.0/lto-wrapper
Target: riscv64-unknown-linux-gnu
Configured with: /root/riscv_build/riscv-gnu-toolchain/build/../riscv-gcc/configure --target=riscv64-unknown-linux-gnu --prefix=/opt/riscv64-linux --with-sysroot=/opt/riscv64-linux/sysroot --with-system-zlib --enable-shared --enable-tls --enable-languages=c,c++,fortran --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libsanitizer --disable-nls --disable-bootstrap --src=../../riscv-gcc --disable-multilib --with-abi=lp64 --with-arch=rv64imafdc --with-tune=rocket 'CFLAGS_FOR_TARGET=-O2   -mcmodel=medlow' 'CXXFLAGS_FOR_TARGET=-O2   -mcmodel=medlow'
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.1.0 (GCC) 
[root@centos7 build]# 
[root@centos7 build]# pwd
/root/riscv_build/riscv-gnu-toolchain/build
[root@centos7 build]# 
[root@centos7 build]# ls
build-binutils-linux    build-gdb-linux                    config.log     scripts
build-gcc-linux-stage1  build-glibc-linux-headers          config.status  stamps
build-gcc-linux-stage2  build-glibc-linux-rv64imafdc-lp64  Makefile
[root@centos7 build]# vi Makefile 
[root@centos7 riscv_debug]# riscv64-unknown-linux-gnu-gcc  -print-sysroot
/opt/riscv64-linux/sysroot
[root@centos7 riscv_debug]# riscv64-unknown-linux-gnu-gcc   -dumpmachine
riscv64-unknown-linux-gnu
[root@centos7 riscv_debug]# 
[root@centos7 build]# file /opt/riscv64-linux/bin/riscv64-unknown-linux-gnu-gcc 
/opt/riscv64-linux/bin/riscv64-unknown-linux-gnu-gcc: ELF 64-bit LSB executable, ARM aarch64, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 3.7.0, BuildID[sha1]=d92cee985ed96437624011d203e5f3db8a70e9fd, not stripped
[root@centos7 build]# 

riscv-glibc(可以不编译,直接git rm)

[root@centos7 riscv-gnu-toolchain]# cd riscv-glibc/
[root@centos7 riscv-glibc]# mkdir build; cd build
[root@centos7 build]# ../configure --prefix=/opt/riscv64-linux
checking build system type... aarch64-unknown-linux-gnu
checking host system type... aarch64-unknown-linux-gnu
checking for gcc... gcc
checking for python3... python3
checking version of python3... 3.6.8, ok
configure: error: 
*** These critical programs are missing or too old: make compiler
*** Check the INSTALL file for required versions.
[root@centos7 build]# make  -j128
make: *** No targets specified and no makefile found.  Stop.
[root@centos7 build]# ls
config.log
[root@centos7 build]# make --version
GNU Make 3.82
Built for aarch64-redhat-linux-gnu
Copyright (C) 2010  Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

下载make  http://ftp.gnu.org/gnu/make/

tar -zxvf make-4.3.tar.gz #解压
cd make-4.3 #进入目录
./configure #
make && make install #编译#安装
ln -s -f /usr/local/bin/make /usr/bin/make #必须运行该语句否则还是旧版本
make -version

[root@centos7 make-4.3]# ls /usr/local/bin/make 
/usr/local/bin/make
[root@centos7 make-4.3]# /usr/local/bin/make --version
GNU Make 4.3
Built for aarch64-unknown-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[root@centos7 make-4.3]# 
[root@centos7 make-4.3]# ls /usr/bin/make
/usr/bin/make
[root@centos7 make-4.3]# mv /usr/bin/make /usr/bin/make.bak
[root@centos7 make-4.3]# ln -s -f /usr/local/bin/make /usr/bin/make
[root@centos7 make-4.3]# make --version
GNU Make 4.3
Built for aarch64-unknown-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[root@centos7 make-4.3]# 

升级make后还报这个错误,可以忽略

checking for python3... python3
checking version of python3... 3.6.8, ok
configure: error: 
*** These critical programs are missing or too old: compiler
*** Check the INSTALL file for required versions.
../configure --prefix=/opt/riscv64-linux --with-arch=rv64imafdc --with-abi=lp64 --enable-linux

make linux $(nproc)

最终编译glibc还是报错

checking for library containing waddstr... -lncursesw
checking for library containing tgetent... none required
checking size of unsigned long long... 8
checking size of unsigned long... 8
checking size of unsigned __int128... 16
checking for library containing dlopen... none required
checking whether to use expat... yes
checking for libexpat... no
configure: error: expat is missing or unusable
make[2]: *** [Makefile:10036: configure-gdb] Error 1
make[2]: Leaving directory '/root/riscv_build/riscv-gnu-toolchain/build/build-gdb-linux'
make[1]: *** [Makefile:854: all] Error 2
make[1]: Leaving directory '/root/riscv_build/riscv-gnu-toolchain/build/build-gdb-linux'
make: *** [Makefile:281: stamps/build-gdb-linux] Error 2
You have new mail in /var/spool/mail/root
[root@centos7 build]# ls /opt/riscv64/bin/
riscv64-unknown-linux-gnu-addr2line  riscv64-unknown-linux-gnu-ld       riscv64-unknown-linux-gnu-readelf
riscv64-unknown-linux-gnu-ar         riscv64-unknown-linux-gnu-ld.bfd   riscv64-unknown-linux-gnu-size
riscv64-unknown-linux-gnu-as         riscv64-unknown-linux-gnu-nm       riscv64-unknown-linux-gnu-strings
riscv64-unknown-linux-gnu-c++filt    riscv64-unknown-linux-gnu-objcopy  riscv64-unknown-linux-gnu-strip
riscv64-unknown-linux-gnu-elfedit    riscv64-unknown-linux-gnu-objdump
riscv64-unknown-linux-gnu-gprof      riscv64-unknown-linux-gnu-ranlib
[root@centos7 build]# ls /opt/riscv64-linux/bin/
riscv64-unknown-linux-gnu-addr2line   riscv64-unknown-linux-gnu-gcc-ar      riscv64-unknown-linux-gnu-lto-dump
riscv64-unknown-linux-gnu-ar          riscv64-unknown-linux-gnu-gcc-nm      riscv64-unknown-linux-gnu-nm
riscv64-unknown-linux-gnu-as          riscv64-unknown-linux-gnu-gcc-ranlib  riscv64-unknown-linux-gnu-objcopy
riscv64-unknown-linux-gnu-c++         riscv64-unknown-linux-gnu-gcov        riscv64-unknown-linux-gnu-objdump
riscv64-unknown-linux-gnu-c++filt     riscv64-unknown-linux-gnu-gcov-dump   riscv64-unknown-linux-gnu-ranlib
riscv64-unknown-linux-gnu-cpp         riscv64-unknown-linux-gnu-gcov-tool   riscv64-unknown-linux-gnu-readelf
riscv64-unknown-linux-gnu-elfedit     riscv64-unknown-linux-gnu-gfortran    riscv64-unknown-linux-gnu-size
riscv64-unknown-linux-gnu-g++         riscv64-unknown-linux-gnu-gprof       riscv64-unknown-linux-gnu-strings
riscv64-unknown-linux-gnu-gcc         riscv64-unknown-linux-gnu-ld          riscv64-unknown-linux-gnu-strip
riscv64-unknown-linux-gnu-gcc-11.1.0  riscv64-unknown-linux-gnu-ld.bfd
[root@centos7 build]# ls /opt/riscv64-linux/
bin  include  lib  lib64  libexec  riscv64-unknown-linux-gnu  share  sysroot
[root@centos7 build]# ls /opt/riscv64-linux/sysroot/
etc  lib  sbin  usr  var
[root@centos7 build]# 

sysroot

sysroot/usr/lib

checking for library containing waddstr... -lncursesw
checking for library containing tgetent... none required
checking size of unsigned long long... 8
checking size of unsigned long... 8
checking size of unsigned __int128... 16
checking for library containing dlopen... none required
checking whether to use expat... yes
checking for libexpat... no
configure: error: expat is missing or unusable
make[2]: *** [Makefile:10036: configure-gdb] Error 1
make[2]: Leaving directory '/root/riscv_build/riscv-gnu-toolchain/build/build-gdb-linux'
make[1]: *** [Makefile:854: all] Error 2
make[1]: Leaving directory '/root/riscv_build/riscv-gnu-toolchain/build/build-gdb-linux'
make: *** [Makefile:281: stamps/build-gdb-linux] Error 2
You have new mail in /var/spool/mail/root
[root@centos7 build]# ls /opt/riscv64/bin/
riscv64-unknown-linux-gnu-addr2line  riscv64-unknown-linux-gnu-ld       riscv64-unknown-linux-gnu-readelf
riscv64-unknown-linux-gnu-ar         riscv64-unknown-linux-gnu-ld.bfd   riscv64-unknown-linux-gnu-size
riscv64-unknown-linux-gnu-as         riscv64-unknown-linux-gnu-nm       riscv64-unknown-linux-gnu-strings
riscv64-unknown-linux-gnu-c++filt    riscv64-unknown-linux-gnu-objcopy  riscv64-unknown-linux-gnu-strip
riscv64-unknown-linux-gnu-elfedit    riscv64-unknown-linux-gnu-objdump
riscv64-unknown-linux-gnu-gprof      riscv64-unknown-linux-gnu-ranlib
[root@centos7 build]# ls /opt/riscv64-linux/bin/
riscv64-unknown-linux-gnu-addr2line   riscv64-unknown-linux-gnu-gcc-ar      riscv64-unknown-linux-gnu-lto-dump
riscv64-unknown-linux-gnu-ar          riscv64-unknown-linux-gnu-gcc-nm      riscv64-unknown-linux-gnu-nm
riscv64-unknown-linux-gnu-as          riscv64-unknown-linux-gnu-gcc-ranlib  riscv64-unknown-linux-gnu-objcopy
riscv64-unknown-linux-gnu-c++         riscv64-unknown-linux-gnu-gcov        riscv64-unknown-linux-gnu-objdump
riscv64-unknown-linux-gnu-c++filt     riscv64-unknown-linux-gnu-gcov-dump   riscv64-unknown-linux-gnu-ranlib
riscv64-unknown-linux-gnu-cpp         riscv64-unknown-linux-gnu-gcov-tool   riscv64-unknown-linux-gnu-readelf
riscv64-unknown-linux-gnu-elfedit     riscv64-unknown-linux-gnu-gfortran    riscv64-unknown-linux-gnu-size
riscv64-unknown-linux-gnu-g++         riscv64-unknown-linux-gnu-gprof       riscv64-unknown-linux-gnu-strings
riscv64-unknown-linux-gnu-gcc         riscv64-unknown-linux-gnu-ld          riscv64-unknown-linux-gnu-strip
riscv64-unknown-linux-gnu-gcc-11.1.0  riscv64-unknown-linux-gnu-ld.bfd
[root@centos7 build]# ls /opt/riscv64-linux/
bin  include  lib  lib64  libexec  riscv64-unknown-linux-gnu  share  sysroot
[root@centos7 build]# ls /opt/riscv64-linux/sysroot/
etc  lib  sbin  usr  var
[root@centos7 build]# 

 Makefile

Makefile


riscv64-linux-gnu-

Ubuntu (tested on 19.04):

$ apt-get install git build-essential gdb-multiarch
$ apt-get install qemu-system-misc gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu

 交叉编译

[root@centos7 linux-5.14]# make ARCH=riscv CROSS_COMPILE=riscv64-unknown-elf-gcc menuconfig
  UPD     scripts/kconfig/mconf-cfg
  HOSTCC  scripts/kconfig/mconf.o
  HOSTCC  scripts/kconfig/lxdialog/checklist.o
  HOSTCC  scripts/kconfig/lxdialog/inputbox.o
  HOSTCC  scripts/kconfig/lxdialog/menubox.o
  HOSTCC  scripts/kconfig/lxdialog/textbox.o
  HOSTCC  scripts/kconfig/lxdialog/util.o
  HOSTCC  scripts/kconfig/lxdialog/yesno.o
  HOSTLD  scripts/kconfig/mconf
scripts/Kconfig.include:39: compiler 'riscv64-unknown-elf-gccgcc' not found
make[2]: *** [menuconfig] Error 1
make[1]: *** [menuconfig] Error 2
make: *** [__sub-make] Error 2
[root@centos7 linux-5.14]# make ARCH=riscv CROSS_COMPILE=riscv64-unknown-elf- menuconfig
 .config - Linux/riscv 5.14.0 Kernel Configuration
 

 

[root@centos7 linux-5.14]# grep "GCC_PLUGIN" .config
CONFIG_HAVE_GCC_PLUGINS=y
CONFIG_GCC_PLUGINS=y
# CONFIG_GCC_PLUGIN_CYC_COMPLEXITY is not set
# CONFIG_GCC_PLUGIN_LATENT_ENTROPY is not set
# CONFIG_GCC_PLUGIN_RANDSTRUCT is not set
# CONFIG_GCC_PLUGIN_STRUCTLEAK_USER is not set
# CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF is not set
# CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL is not set
[root@centos7 linux-5.14]# grep "GCC_PLUGIN" .config.bak
CONFIG_HAVE_GCC_PLUGINS=y
[root@centos7 linux-5.14]# rm .config
rm: remove regular file ‘.config’? u
[root@centos7 linux-5.14]# cp .config.bak  .config
cp: overwrite ‘.config’? y
[root@centos7 linux-5.14]# 
 

安装 riscv64-unknown-elf-gdb

  1. 安装依赖(针对 Linux,macOS 可以遇到错误再去搜索)

    • python 并非必须
    • 在 Ubuntu 20.04 等系统中,python 和 python-dev 需要替换成 python2 和 python2-dev
      sudo apt-get install libncurses5-dev python python-dev texinfo libreadline-dev
      
  2. 前往清华镜像下载最新的 GDB 源代码

  3. 解压源代码,并定位到目录

  4. 执行以下命令

    • --prefix 是安装的路径,按照以上指令会安装到 /usr/local/bin/ 下
    • --with-python 是 python2 的地址,它和 --enable-tui 都是为了支持后续安装一个可视化插件,并非必须
../configure --prefix=/opt/riscv/ --with-python=/usr/bin/python --target=riscv64-unknown-elf --enable-tui=yes

 

[root@centos7 gdb-11.1]# mkdir build
[root@centos7 gdb-11.1]# cd build/
[root@centos7 build]# ../configure --prefix=/opt/riscv/ --with-python=/usr/bin/python --target=riscv64-unknown-elf --enable-tui=yes
# Linux
make -j$(nproc)
  files='./gdb.go gdb/experimental.go gdb/iterator.go gdb/printing.go gdb/support.go gdb/types.go' ; \
  cd ./guile ; \
  for go in $files ; do \
    source="`echo $go | sed 's/\.go$/.scm/'`" ; \
    echo  compile  -Warity-mismatch -Wformat -Wunused-toplevel -L . -o "$go" "$source" ; \
     compile  -Warity-mismatch -Wformat -Wunused-toplevel -L . -o "$go" "$source" || exit 1 ; \
  done ; \
fi
mkdir ./system-gdbinit
touch stamp-guile
files='elinos.py wrs-linux.py' ; \
for file in $files ; do \
  f=../../../gdb/data-directory/../system-gdbinit/$file ; \
  if test -f $f ; then \
    /usr/bin/install -c -m 644 $f ./system-gdbinit ; \
  fi ; \
done
touch stamp-system-gdbinit
mkdir -p -- ./python/gdb
touch stamp-syscalls
mkdir -p -- ./python/gdb/command
mkdir -p -- ./python/gdb/function
mkdir -p -- ./python/gdb/printer
touch stamp-python
make[4]: Leaving directory `/root/riscv_debug/gdb-11.1/build/gdb/data-directory'
make[3]: Leaving directory `/root/riscv_debug/gdb-11.1/build/gdb'
make[2]: Leaving directory `/root/riscv_debug/gdb-11.1/build/gdb'
make[1]: Leaving directory `/root/riscv_debug/gdb-11.1/build'

[root@centos7 build]# make install

[root@centos7 riscv]# find ./ -name  riscv64-unknown-elf-gdb
./bin/riscv64-unknown-elf-gdb
[root@centos7 riscv]# 
[root@centos7 linux-5.14]# riscv64-unknown-elf-gdb vmlinux.o
GNU gdb (GDB) 11.1
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=aarch64-unknown-linux-gnu --target=riscv64-unknown-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from vmlinux.o...
(gdb) b rand_initialize
Breakpoint 1 at 0x20dac8: file drivers/char/random.c, line 1779.
(gdb) r
Don't know how to run.  Try "help target".
(gdb) 

qemu

[root@centos7 qemu-4.2.0]# ./configure --target-list=riscv64-softmmu --prefix=/opt/riscv/
make -j $(nproc)
sudo make install
[root@centos7 linux-5.14]# qemu-system-riscv64 -version
QEMU emulator version 4.2.0
Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers
[root@centos7 linux-5.14]# 
[root@centos7 buildroot]# ls output/images/
fw_dynamic.bin  fw_dynamic.elf  fw_jump.bin  fw_jump.elf  rootfs.bak  rootfs.ext2  rootfs.tar  start-qemu.sh
[root@centos7 buildroot]# 
[root@centos7 buildroot]# cat output/images/start-qemu.sh 
#!/bin/sh
(
BINARIES_DIR="${0%/*}/"
cd ${BINARIES_DIR}

if [ "${1}" = "serial-only" ]; then
    EXTRA_ARGS='-nographic'
else
    EXTRA_ARGS=''
fi

export PATH="/root/buildroot-2021.08.1/output/host/bin:${PATH}"
exec qemu-system-riscv64 -M virt -bios fw_jump.elf -kernel Image -append "rootwait root=/dev/vda ro" -drive file=rootfs.ext2,format=raw,id=hd0 -device virtio-blk-device,drive=hd0 -netdev user,id=net0 -device virtio-net-device,netdev=net0 -nographic  ${EXTRA_ARGS}
)
[root@centos7 ~]# ls ~/riscv_debug/linux-5.14/arch/riscv/boot/Image
/root/riscv_debug/linux-5.14/arch/riscv/boot/Image
[root@centos7 ~]# ls ~/riscv_debug/linux-5.14/arch/riscv/boot/
dts  Image  Image.gz  install.sh  loader.lds.S  loader.S  Makefile
[root@centos7 ~]# 
"start-qemu.sh" 6L, 291C written
[root@centos7 images]# bash start-qemu.sh

OpenSBI v0.9
   ____                    _____ ____ _____
  / __ \                  / ____|  _ \_   _|
 | |  | |_ __   ___ _ __ | (___ | |_) || |
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
 | |__| | |_) |  __/ | | |____) | |_) || |_
  \____/| .__/ \___|_| |_|_____/|____/_____|
        | |
        |_|

Platform Name             : riscv-virtio,qemu
Platform Features         : timer,mfdeleg
Platform HART Count       : 1
Firmware Base             : 0x80000000
Firmware Size             : 124 KB
Runtime SBI Version       : 0.2

Domain0 Name              : root
Domain0 Boot HART         : 0
Domain0 HARTs             : 0*
Domain0 Region00          : 0x0000000080000000-0x000000008001ffff ()
Domain0 Region01          : 0x0000000000000000-0xffffffffffffffff (R,W,X)
Domain0 Next Address      : 0x0000000080200000
Domain0 Next Arg1         : 0x0000000082200000
Domain0 Next Mode         : S-mode
Domain0 SysReset          : yes

Boot HART ID              : 0
Boot HART Domain          : root
Boot HART ISA             : rv64imafdcsu
Boot HART Features        : scounteren,mcounteren
Boot HART PMP Count       : 16
Boot HART PMP Granularity : 4
Boot HART PMP Address Bits: 54
Boot HART MHPM Count      : 0
Boot HART MHPM Count      : 0
Boot HART MIDELEG         : 0x0000000000000222
Boot HART MEDELEG         : 0x000000000000b109
[    0.000000] Linux version 5.14.0 (root@2d75d24508a3) (riscv64-linux-gnu-gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0, GNU ld (GNU Binutils for Ubuntu) 2.30) #23 Wed Oct 20 02:33:31 UTC 2021
[    0.000000] OF: fdt: Ignoring memory range 0x80000000 - 0x80200000
[    0.000000] Machine model: riscv-virtio,qemu
[    0.000000] earlycon: sbi0 at I/O port 0x0 (options '')
[    0.000000] printk: bootconsole [sbi0] enabled
[    0.000000] Zone ranges:
[    0.000000]   DMA32    [mem 0x0000000080200000-0x0000000087ffffff]
[    0.000000]   Normal   empty
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0: [mem 0x0000000080200000-0x0000000087ffffff]
[    0.000000] Initmem setup node 0 [mem 0x0000000080200000-0x0000000087ffffff]
[    0.000000] SBI specification v0.2 detected
[    0.000000] SBI implementation ID=0x1 Version=0x9
[    0.000000] SBI TIME extension detected
[    0.000000] SBI IPI extension detected
[    0.000000] SBI RFENCE extension detected
[    0.000000] riscv: ISA extensions acdfimsu
[    0.000000] riscv: ELF capabilities acdfim
[    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 31815
[    0.000000] Kernel command line: earlycon=sbi
[    0.000000] Dentry cache hash table entries: 16384 (order: 5, 131072 bytes, linear)
[    0.000000] Inode-cache hash table entries: 8192 (order: 4, 65536 bytes, linear)
[    0.000000] Sorting __ex_table...
[    0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off

根据epc 定位linux kernel panic 位置

https://blog.csdn.net/han_dawei/article/details/41846055

root@centos7 linux-5.14]# riscv64-unknown-elf-objdump -dr   vmlinux.o   >  linux-dr
[root@centos7 linux-5.14]# cat System.map  | grep -i  rand_in
ffffffff802148e0 T rand_initialize
[root@centos7 linux-5.14]# vi linux-dr 

Get ready to debug

Now we have all the componenents needed to begin debugging Haiku’s bootloader.

Boot u-boot + Haiku in qemu-system-riscv64

qemu-system-riscv64 -bios /tmp/riscv64-build/u-boot.bin \
  -M virt -m 4G \
  -drive file=haiku-mmc.image,format=raw,id=hd0 -device virtio-blk-device,drive=hd0 \
  -s -S
  • -bios ...
    • The bios is the u-boot bios you compiled
  • -M virt
    • A standardized “qemu riscv64 machine”
  • -m 4G
    • 4 GiB of memory
  • -drive
    • Haiku’s disk image (contains our EFI bootloader, and Haiku’s filesystem)
  • -s
    • Listen for GDB connections on 127.0.0.1:1234
  • -S
    • Halt boot until GDB says to begin running

At this point, qemu will “wait” for your command to start running.

qemu-system-riscv64 -nographic -machine virt -bios fw_jump.elf -kernel /root/riscv_debug/linux-5.14/arch/riscv/boot/Image -append root=/dev/vda ro console=ttyS0 -drive file=rootfs.ext2,format=raw,id=hd0 -device virtio-blk-device,drive=hd0 -s -S

qemu-system-riscv64 -nographic -machine virt -bios fw_jump.elf -kernel /root/riscv_debug/linux-5.14/arch/riscv/boot/Image -append root=/dev/vda ro console=ttyS0 -drive file=rootfs.ext2,format=raw,id=hd0 -device virtio-blk-device,drive=hd0 -S -gdb tcp::1234

qemu-system-riscv64 -nographic -machine virt -bios fw_jump.elf -kernel /root/riscv_debug/linux-5.14/arch/riscv/boot/Image -append root=/dev/vda ro console=ttyS0 -drive file=rootfs.ext2,format=raw,id=hd0 -device virtio-blk-device,drive=hd0 -s -S
[root@centos7 images]# bash start-qemu.sh
qemu-system-riscv64: GLib: g_mapped_file_unref: assertion 'file != NULL' failed
qemu-system-riscv64: could not load kernel '/root/riscv_debug/linux-5.14/vmlinux.o'

Attach GDB

Now, we attach to qemu from GDB, and start debugging!

TIP: The hex addresses below could change over time. If you don’t get debugging symbols (aka source-code view of what’s happening) you’ll need to “figure the new addresses out”. I’ve included some basic information on how I got them.

  1. Start gdb for your architecture
    1. riscv64-elf-gdb /tmp/riscv64-build/u-boot on ArchLinux
  2. Connect to qemu
    1. target extended-remote 127.0.0.1:1234
  3. Tell gdb where u-boot symbols should be loaded
    1. add-symbol-file /tmp/riscv64-build/u-boot 0xfff66000
    2. This hex value came from booting u-boot, and typing bdinfo for the “reloc addr”
  4. Tell gdb where our EFI bootloader symbols should be loaded
    1. add-symbol-file .../generated.riscv64/objects/haiku/riscv64/debug_1/system/boot/efi/boot_loader_efi 0xfe6b0000
    2. This hex value came from compiling u-boot with full tracing, and looking where u-boot puts our EFI bootloader in memory.
    3. EFI: Entry efi_load_image, ..., EFI: efi_add_memory_map_pg: 0xfe6b0000 0x60 1 yes

These important commands can be placed into a text file, and passed to gdb on every startup via:

riscv64-elf-gdb -x (file) /tmp/riscv64-build/u-boot

[root@centos7 linux-5.14]# riscv64-unknown-elf-gdb vmlinux.o
GNU gdb (GDB) 11.1
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=aarch64-unknown-linux-gnu --target=riscv64-unknown-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from vmlinux.o...
(gdb)  target remote :1234
Remote debugging using :1234
warning: Can not parse XML target description; XML support was disabled at compile time
0x000000008000bcd8 in ?? ()
(gdb) b rand_initialize
Breakpoint 1 at 0x20dac8: file drivers/char/random.c, line 1779.
(gdb) c
Continuing.



^C
Program received signal SIGINT, Interrupt.
0x000000008000bcd8 in ?? ()
(gdb) quit
A debugging session is active.

        Inferior 1 [process 1] will be detached.

Quit anyway? (y or n) y
Detaching from program: /root/riscv_debug/linux-5.14/vmlinux.o, process 1
Ending remote debugging.
[Inferior 1 (process 1) detached

Start debugging!

You’ll generally get “one shot” at debugging. If you hit a hard lockup, you’ll likely need to exit gdb, and qemu, and repeat the two sections above.

I’ve heard there are some “extended-remote” commands allowing you to restart from within gdb, however I haven’t figured them out yet :-)

Breakpoints

You can set a breakpoint to “stop” execution at a fixed function. Since we have u-boot’s symbols AND Haiku’s bootloader symbols you can pass any function from the two.

  • break _relocate – Stop when GDB sees Haiku’s EFI relocate function getting called.

Watchpoints

You can set a watchpoint to “stop” when a value is read, written, or matches an expression.

  • watch foo – Stop when the program writes to a variable called “foo”
  • rwatch foo – Stop when the program reads from a variable called “foo”

Cleanup

Sometimes you’ll want to adjust breakpoints/watchpoints

  • info watchpoints – Show all watchpoints
  • info breakpoints – Show all breakpoints
  • delete – Delete all watchpoints/breakpoints

Begin Debugging

Now that you have defined some “stuff to do”, you can start execution.

  • continue / c
    • Start qemu running until GDB “stops” based on conditions above, or user presses <ctl> + <c> in GDB
  • <ctl> + <c>
    • Pause qemu, return control to GDB
  • step / s
    • Continue until control reaches a different source line, then stop it and return control to GDB

More details on how to use GDB controls can be found in their online docs

file xx.elf : 导入要调试的elf

add-symbol-file xx.elf : 将elf的符号导入

add-symbol-file xx.elf 0x80200000 : 将elf的符号导入,并指定地址

target remote :1234 : 连接远程gdb server

stepi : 单步执行,每次执行一个指令,会跟踪进入函数

stepi 4 : 单步执行,走4步

tbreak *0x80200000: 在内存0x80200000地址添加一个临时断点

x 0x80200000 : 查看0x80200000地址的值

info registers a3 : 查看寄存器a3的值

print xx : 查看xx变量的值
(gdb) add-symbol-file  vmlinux.o
add symbol table from file "vmlinux.o"
(y or n) y
Reading symbols from vmlinux.o...
(gdb) target remote :1234
Remote debugging using :1234
warning: Can not parse XML target description; XML support was disabled at compile time
handle_exception () at arch/riscv/kernel/entry.S:251
warning: Source file is more recent than executable.
251             REG_L s0, TASK_TI_FLAGS(tp) /* current_thread_info->flags */
(gdb) b init_std_data
Breakpoint 1 at 0x20daf0: file ./include/linux/timekeeping.h, line 79.

 riscv64-unknown-elf-gdb vmlinux

[root@centos7 linux-5.14]# riscv64-unknown-elf-gdb vmlinux
GNU gdb (GDB) 11.1
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=aarch64-unknown-linux-gnu --target=riscv64-unknown-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from vmlinux...
(gdb) hbreak  init_std_data
No hardware breakpoint support in the target.
(gdb) add-symbol-file  vmlinux
add symbol table from file "vmlinux"
(y or n) y
[root@centos7 linux-5.14]# riscv64-unknown-elf-gdb vmlinux
GNU gdb (GDB) 11.1
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=aarch64-unknown-linux-gnu --target=riscv64-unknown-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from vmlinux...
(gdb) target remote :1234
Remote debugging using :1234
warning: Can not parse XML target description; XML support was disabled at compile time
0x0000000000001000 in ?? ()
(gdb) add-symbol-file  vmlinux
add symbol table from file "vmlinux"
(y or n) y
Reading symbols from vmlinux...
(gdb)  hbreak start_kernel
Hardware assisted breakpoint 1 at 0xffffffff8080066a: file init/main.c, line 917.
(gdb)  hbreak init_std_data
Hardware assisted breakpoint 2 at 0xffffffff8081a3fa: file ./include/linux/timekeeping.h, line 79.
(gdb) c
Continuing.

Breakpoint 1, start_kernel () at init/main.c:917
917     {
(gdb) c
Continuing.

Breakpoint 2, init_std_data (r=0xffffffff8129c1b8 <input_pool>) at drivers/char/random.c:1752
1752            ktime_t now = ktime_get_real();
(gdb) bt
#0  init_std_data (r=0xffffffff8129c1b8 <input_pool>) at drivers/char/random.c:1752
#1  rand_initialize () at drivers/char/random.c:1780
#2  0xffffffff80800b2e in start_kernel () at init/main.c:1035
#3  0xffffffff8000116a in _start_kernel () at arch/riscv/kernel/head.S:327
Backtrace stopped: frame did not save the PC
(gdb) list
1747     * as that can only decrease the entropy in the pool.
1748     */
1749    static void __init init_std_data(struct entropy_store *r)
1750    {
1751            int i;
1752            ktime_t now = ktime_get_real();
1753            unsigned long rv;
1754
1755            mix_pool_bytes(r, &now, sizeof(now));
1756            for (i = r->poolinfo->poolbytes; i > 0; i -= sizeof(rv)) {
(gdb) 
Backtrace stopped: frame did not save the PC
(gdb) list
1747     * as that can only decrease the entropy in the pool.
1748     */
1749    static void __init init_std_data(struct entropy_store *r)
1750    {
1751            int i;
1752            ktime_t now = ktime_get_real();
1753            unsigned long rv;
1754
1755            mix_pool_bytes(r, &now, sizeof(now));
1756            for (i = r->poolinfo->poolbytes; i > 0; i -= sizeof(rv)) {
(gdb) c
Continuing.

 重新编译内核

make ARCH=riscv CROSS_COMPILE=riscv64-unknown-elf- defconfig

Kernel hacking —> Compile-time checks and compiler options —> [ ] Compile the kernel with debug info
 

make ARCH=riscv CROSS_COMPILE=riscv64-unknown-elf- j128

 buildrootfs 起来

[    1.250697] usbcore: registered new interface driver uas
[    1.252240] usbcore: registered new interface driver usb-storage
[    1.254922] mousedev: PS/2 mouse device common for all mice
[    1.258399] sdhci: Secure Digital Host Controller Interface driver
[    1.258900] sdhci: Copyright(c) Pierre Ossman
[    1.259904] sdhci-pltfm: SDHCI platform and OF driver helper
[    1.261902] usbcore: registered new interface driver usbhid
[    1.262394] usbhid: USB HID core driver
[    1.266727] NET: Registered PF_INET6 protocol family
[    1.291337] Segment Routing with IPv6
[    1.293531] sit: IPv6, IPv4 and MPLS over IPv4 tunneling driver
[    1.303138] NET: Registered PF_PACKET protocol family
[    1.308585] 9pnet: Installing 9P2000 support
[    1.310025] Key type dns_resolver registered
[    1.316497] debug_vm_pgtable: [debug_vm_pgtable         ]: Validating architecture page table helpers
[    1.363218] EXT4-fs (vda): mounting ext2 file system using the ext4 subsystem
[    1.387067] EXT4-fs (vda): mounted filesystem without journal. Opts: (null). Quota mode: disabled.
[    1.388731] VFS: Mounted root (ext2 filesystem) readonly on device 254:0.
[    1.395819] devtmpfs: mounted
[    1.587835] Freeing unused kernel image (initmem) memory: 2152K
[    1.589171] Run /sbin/init as init process
[    1.806038] EXT4-fs (vda): re-mounted. Opts: (null). Quota mode: disabled.
Starting syslogd: OK
Starting klogd: OK
Running sysctl: OK
Saving random seed: [    3.244781] random: dd: uninitialized urandom read (512 bytes read)
OK
Starting network: Waiting for interface eth0 to appear............... timeout!
run-parts: /etc/network/if-pre-up.d/wait_iface: exit status 1
FAIL

Welcome to Buildroot
buildroot login: 

采用编译前的内核

[root@centos7 linux-5.14]# riscv64-unknown-elf-gdb vmlinux
GNU gdb (GDB) 11.1
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=aarch64-unknown-linux-gnu --target=riscv64-unknown-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from vmlinux...
(gdb) target remote :1234
Remote debugging using :1234
warning: Can not parse XML target description; XML support was disabled at compile time
0x0000000000001000 in ?? ()
(gdb) add-symbol-file vmlinux
add symbol table from file "vmlinux"
(y or n) y
Reading symbols from vmlinux...
(gdb) b init_std_data
Breakpoint 1 at 0xffffffff80214908: file ./include/linux/timekeeping.h, line 79.
(gdb) c
Continuing.

Breakpoint 1, init_std_data (r=<optimized out>) at drivers/char/random.c:1752
warning: Source file is more recent than executable.
1752 ktime_t now = ktime_get_real();
(gdb) bt
#0 init_std_data (r=<optimized out>) at drivers/char/random.c:1752
#1 rand_initialize () at drivers/char/random.c:1780
#2 0xffffffff80200f14 in start_kernel () at init/main.c:1035
#3 0xffffffff8000016c in _start_kernel () at arch/riscv/kernel/head.S:327
Backtrace stopped: frame did not save the PC
(gdb) c
Continuing.

卡到这里不动了

A[    0.000000] random: get_random_bytes called from start_kernel+0x4c4/0x6d0 with crng_init=0
[    0.000000] riscv_timer_init_dt: Registering clocksource cpuid [0] hartid [0]
[    0.000000] clocksource: riscv_clocksource: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns
[    0.000209] sched_clock: 64 bits at 10MHz, resolution 100ns, wraps every 4398046511100ns
BGHI[    0.013085] Console: colour dummy device 80x25
[    0.014272] printk: console [tty0] enabled
[    0.015779] printk: bootconsole [sbi0] disabled
J

 vmlinux-gdb.py

[root@centos7 linux-5.14]# find ./ -name vmlinux-gdb.py
./scripts/gdb/vmlinux-gdb.py

Add the script file to GDB’s auto load path so that you can later use the lx-* helper commands. Some descriptions about the commands can be found here 2.

echo "add-auto-load-safe-path path/to/linux-v.x.y/vmlinux-gdb.py" >> ~/.gdbinit
[root@centos7 linux-5.14]# echo "add-auto-load-safe-path  /root/riscv_debug/linux-5.14/scripts/gdb/vmlinux-gdb.py" >> ~/.gdbinit
[root@centos7 linux-5.14]# 
 
[root@centos7 linux-5.14]# riscv64-unknown-elf-gdb vmlinux
GNU gdb (GDB) 11.1
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=aarch64-unknown-linux-gnu --target=riscv64-unknown-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from vmlinux...
(gdb) target remote :1234
Remote debugging using :1234
warning: Can not parse XML target description; XML support was disabled at compile time
0x0000000000001000 in ?? ()
(gdb) add-symbol-file  vmlinux
add symbol table from file "vmlinux"
(y or n) y
Reading symbols from vmlinux...
(gdb) hb init_std_data
Hardware assisted breakpoint 1 at 0xffffffff80214908: file ./include/linux/timekeeping.h, line 79.
(gdb) hbreak start_kernel
Hardware assisted breakpoint 2 at 0xffffffff80200a80: file init/main.c, line 917.
(gdb) c
Continuing.

Breakpoint 2, start_kernel () at init/main.c:917
warning: Source file is more recent than executable.
917     {
(gdb) bt
#0  start_kernel () at init/main.c:917
#1  0xffffffff8000016c in _start_kernel () at arch/riscv/kernel/head.S:327
Backtrace stopped: frame did not save the PC
(gdb) list
912             pr_notice("Unknown command line parameters:%s\n", unknown_options);
913             memblock_free(__pa(unknown_options), len);
914     }
915
916     asmlinkage __visible void __init __no_sanitize_address start_kernel(void)
917     {
918             char *command_line;
919             char *after_dashes;
920
921             set_task_stack_end_magic(&init_task);
(gdb) 

Examples of using the Linux-provided gdb helpers

  • Load module (and main kernel) symbols:

    (gdb) lx-symbols
    loading vmlinux
    scanning for modules in /home/user/linux/build
    loading @0xffffffffa0020000: /home/user/linux/build/net/netfilter/xt_tcpudp.ko
    loading @0xffffffffa0016000: /home/user/linux/build/net/netfilter/xt_pkttype.ko
    loading @0xffffffffa0002000: /home/user/linux/build/net/netfilter/xt_limit.ko
    loading @0xffffffffa00ca000: /home/user/linux/build/net/packet/af_packet.ko
    loading @0xffffffffa003c000: /home/user/linux/build/fs/fuse/fuse.ko
    ...
    loading @0xffffffffa0000000: /home/user/linux/build/drivers/ata/ata_generic.ko
    
  • Set a breakpoint on some not yet loaded module function, e.g.:

    (gdb) b btrfs_init_sysfs
    Function "btrfs_init_sysfs" not defined.
    Make breakpoint pending on future shared library load? (y or [n]) y
    Breakpoint 1 (btrfs_init_sysfs) pending.
    
  • Continue the target:

    (gdb) c
    
  • Load the module on the target and watch the symbols being loaded as well as the breakpoint hit:

    loading @0xffffffffa0034000: /home/user/linux/build/lib/libcrc32c.ko
    loading @0xffffffffa0050000: /home/user/linux/build/lib/lzo/lzo_compress.ko
    loading @0xffffffffa006e000: /home/user/linux/build/lib/zlib_deflate/zlib_deflate.ko
    loading @0xffffffffa01b1000: /home/user/linux/build/fs/btrfs/btrfs.ko
    
    Breakpoint 1, btrfs_init_sysfs () at /home/user/linux/fs/btrfs/sysfs.c:36
    36              btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
    
  • Dump the log buffer of the target kernel:

    (gdb) lx-dmesg
    [     0.000000] Initializing cgroup subsys cpuset
    [     0.000000] Initializing cgroup subsys cpu
    [     0.000000] Linux version 3.8.0-rc4-dbg+ (...
    [     0.000000] Command line: root=/dev/sda2 resume=/dev/sda1 vga=0x314
    [     0.000000] e820: BIOS-provided physical RAM map:
    [     0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
    [     0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
    ....
    
  • Examine fields of the current task struct:

    (gdb) p $lx_current().pid
    $1 = 4998
    (gdb) p $lx_current().comm
    $2 = "modprobe\000\000\000\000\000\000\000"
    
  • Make use of the per-cpu function for the current or a specified CPU:

    (gdb) p $lx_per_cpu("runqueues").nr_running
    $3 = 1
    (gdb) p $lx_per_cpu("runqueues", 2).nr_running
    $4 = 0
    
  • Dig into hrtimers using the container_of helper:

    (gdb) set $next = $lx_per_cpu("hrtimer_bases").clock_base[0].active.next
    (gdb) p *$container_of($next, "struct hrtimer", "node")
    $5 = {
      node = {
        node = {
          __rb_parent_color = 18446612133355256072,
          rb_right = 0x0 <irq_stack_union>,
          rb_left = 0x0 <irq_stack_union>
        },
        expires = {
          tv64 = 1835268000000
        }
      },
      _softexpires = {
        tv64 = 1835268000000
      },
      function = 0xffffffff81078232 <tick_sched_timer>,
      base = 0xffff88003fd0d6f0,
      state = 1,
      start_pid = 0,
      start_site = 0xffffffff81055c1f <hrtimer_start_range_ns+20>,
      start_comm = "swapper/2\000\000\000\000\000\000"
    }
    

List of commands and functions

The number of commands and convenience functions may evolve over the time, this is just a snapshot of the initial version:

(gdb) apropos lx
function lx_current -- Return current task
function lx_module -- Find module by name and return the module variable
function lx_per_cpu -- Return per-cpu variable
function lx_task_by_pid -- Find Linux task by PID and return the task_struct variable
function lx_thread_info -- Calculate Linux thread_info from task variable
lx-dmesg -- Print Linux kernel log buffer
lx-lsmod -- List currently loaded modules
lx-symbols -- (Re-)load symbols of Linux kernel and currently loaded modules

Detailed help can be obtained via "help <command-name>" for commands and "help function <function-name>" for convenience functions.

 

 github

mkdir /opt/riscv
export  RISCV=/opt/riscv

[root@centos7 ~]# git clone --recursive https://github.com/riscv/riscv-gnu-toolchain.git Cloning into 'riscv-gnu-toolchain'... remote: Enumerating objects: 7901, done. remote: Counting objects: 100% (363/363), done. remote: Compressing objects: 100% (325/325), done. remote: Total 7901 (delta 49), reused 307 (delta 35), pack-reused 7538 Receiving objects: 100% (7901/7901), 5.48 MiB | 3.93 MiB/s, done. Resolving deltas: 100% (3769/3769), done. Submodule 'qemu' (https://git.qemu.org/git/qemu.git) registered for path 'qemu' Submodule 'riscv-binutils' (https://github.com/riscv-collab/riscv-binutils-gdb.git) registered for path 'riscv-binutils' Submodule 'riscv-dejagnu' (https://github.com/riscv-collab/riscv-dejagnu.git) registered for path 'riscv-dejagnu' Submodule 'riscv-gcc' (https://github.com/riscv-collab/riscv-gcc.git) registered for path 'riscv-gcc' Submodule 'riscv-gdb' (https://github.com/riscv-collab/riscv-binutils-gdb.git) registered for path 'riscv-gdb' Submodule 'riscv-glibc' (git://sourceware.org/git/glibc.git) registered for path 'riscv-glibc' Submodule 'riscv-newlib' (git://sourceware.org/git/newlib-cygwin.git) registered for path 'riscv-newlib' Cloning into 'qemu'... cd riscv-gnu-toolchainerror: RPC failed; result=22, HTTP code = 422 fatal: The remote end hung up unexpectedly Clone of 'https://git.qemu.org/git/qemu.git' into submodule path 'qemu' failed [root@centos7 ~]# cd riscv-gnu-toolchain [root@centos7 riscv-gnu-toolchain]# mkdir build; cd build [root@centos7 build]# ../configure --prefix=$RISCV --enable-multilib checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking for grep that handles long lines and -e... /usr/bin/grep checking for fgrep... /usr/bin/grep -F checking for grep that handles long lines and -e... (cached) /usr/bin/grep checking for bash... /bin/sh checking for __gmpz_init in -lgmp... yes checking for mpfr_init in -lmpfr... yes checking for mpc_init2 in -lmpc... yes checking for curl... /usr/bin/curl checking for wget... /usr/bin/wget checking for ftp... no configure: creating ./config.status config.status: creating Makefile config.status: creating scripts/wrapper/awk/awk config.status: creating scripts/wrapper/sed/sed [root@centos7 build]# make -j96 cd /root/riscv-gnu-toolchain/build/.. && \ flock `git rev-parse --git-dir`/config git submodule init /root/riscv-gnu-toolchain/build/../riscv-gcc/ && \ flock `git rev-parse --git-dir`/config git submodule update /root/riscv-gnu-toolchain/build/../riscv-gcc/ cd /root/riscv-gnu-toolchain/build/.. && \

Debugging Linux Kernel with QEMU + GDB

设备树-RISC-V from scratch 2Permalink

Running 64- and 32-bit RISC-V Linux on QEMU

使用 GDB 对 rCore 进行 debug*

linux设备驱动:从如何定位oops的代码行谈驱动调试方法

RISC-V GNU 工具链:安装与使用

riscv - boot - head

RISC-V BYTES: CROSS-PLATFORM DEBUGGING WITH QEMU AND GDB

risc-v gcc 工具链编译

利用码云镜像快速拉取riscv-gnu-toolchain工具链

ABI接口

原文地址:https://www.cnblogs.com/dream397/p/15427944.html