link-riscv.sh raw

   1  #!/usr/bin/env bash
   2  #
   3  # Copyright (c) The Bitcoin Core developers
   4  # Distributed under the MIT software license, see the accompanying
   5  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
   6  
   7  export LC_ALL=C.UTF-8
   8  set -o errexit -o xtrace -o pipefail
   9  
  10  GCC=/opt/riscv-ilp32/bin/riscv32-unknown-elf-gcc
  11  GXX=/opt/riscv-ilp32/bin/riscv32-unknown-elf-g++
  12  CRTBEGIN=$("${GCC}" -print-file-name=crtbegin.o)
  13  LIBGCC=$("${GCC}" -print-libgcc-file-name)
  14  LIBSTDCXX=$("${GXX}" -print-file-name=libstdc++.a)
  15  LIBC=$("${GCC}" -print-file-name=libc.a)
  16  LIBM=$("${GCC}" -print-file-name=libm.a)
  17  
  18  echo -e "#include <script/script_error.h>\n int main() { return ScriptErrorString(ScriptError_t::SCRIPT_ERR_UNKNOWN_ERROR).size() > 0; }" > test.cpp
  19  
  20  "${GXX}" -I "${BASE_ROOT_DIR}"/src -g -std=c++20 -c test.cpp -o test.o
  21  
  22  # Make the binary executable on linux for testing purposes
  23  echo -e ".section .text
  24        .global _start
  25        .type _start, @function
  26  
  27        _start:
  28            .option push
  29            .option norelax
  30            la gp, __global_pointer$
  31            .option pop
  32  
  33            call main
  34  
  35            # Put Exit2 system call number into the a7 register
  36            li a7, 93
  37            ecall" > start.s
  38  
  39  "${GCC}" -c start.s -o start.o
  40  
  41  echo -e "#include <sys/stat.h>
  42        void _exit(int code) { while(1); }
  43        int _sbrk(int incr) { return 0; }
  44        int _write(int file, char *ptr, int len) { return 0; }
  45        int _close(int file) { return -1; }
  46        int _fstat(int file, struct stat *st) { st->st_mode = S_IFCHR; return 0; }
  47        int _isatty(int file) { return 1; }
  48        int _lseek(int file, int ptr, int dir) { return 0; }
  49        int _read(int file, char *ptr, int len) { return 0; }
  50        int _kill(int pid, int sig) { return -1; }
  51        int _getpid(void) { return -1; }" > syscalls.c
  52  
  53  "${GCC}" -g -c syscalls.c -o syscalls.o
  54  
  55  "${GXX}" -g -std=c++20 \
  56      -nostdlib \
  57      "${CRTBEGIN}" \
  58      test.o \
  59      start.o \
  60      syscalls.o \
  61      -Wl,--whole-archive \
  62      "${BASE_BUILD_DIR}"/lib/libbitcoin_consensus.a \
  63      "${BASE_BUILD_DIR}"/lib/libbitcoin_crypto.a \
  64      "${BASE_BUILD_DIR}"/src/secp256k1/lib/libsecp256k1.a \
  65      -Wl,--no-whole-archive \
  66      "${LIBSTDCXX}" \
  67      "${LIBC}" \
  68      "${LIBM}" \
  69      "${LIBGCC}" \
  70      -o test.elf
  71  
  72  file test.elf
  73  
  74