We are trying to cross compile llvm using gnu-riscv-toolchain so that we can generate llvm compilers (clang & clang++) which act like native compilers in riscv target. Our host machine is an x86 server running ubuntu 20.04 and target is a QEMU simulator running linux OS configured to simulate riscv architecture. Here is the command we used to cross compile llvm:
cmake -DCMAKE_BUILD_TYPE="Release" \
-DCMAKE_C_COMPILER=/opt/rv64/bin/riscv64-unknown-linux-gnu-gcc \
-DCMAKE_CXX_COMPILER=/opt/rv64/bin/riscv64-unknown-linux-gnu-g++ \``
-DLLVM_ENABLE_PROJECTS=clang;lld;compiler-rt;libcxx;libcxxabi \
-DLLVM_DEFAULT_TARGET_TRIPLE=riscv64-unknown-linux-gnu \
-DLLVM_TARGET_ARCH=riscv64 \
-DLLVM_TARGETS_TO_BUILD=RISCV ../
Based on the discussion here (Using Clang to compile for RISC-V), we were able to successfully compile and execute a c program in our simulator for riscv target with the following command.
/riscv-build/bin/clang --sysroot=/rv64/riscv64-unknown-elf --gcc-toolchain=/rv64 --target=riscv64 -march=rv64gc hello.c -o hello -mno-relax -v
We then compiled a c++ program using clang++ succesfully in same target with the following command.
/riscv-build/bin/clang++ --sysroot=/riscv-build/rv64/riscv64-unknown-linux-elf/ --gcc-toolchain=/riscv-build/rv64 --target=riscv64 -march=rv64gc hello.cpp -o hello -v -mno-relax -lgcc_s
But we got segmentation fault (hello[100]: unhandled signal 11 code 0x1 at 0x0000000000000000 in hello[10000+1000]
), when we executed the binary in the same simulator. We are unable to figure out the reason for the segmentation fault. Since we got c program working, not sure if the c++ compilation command is incorrect.
Below is the -v output of clang++ :
clang version 12.0.1 (https://github.com/llvm/llvm-project.git fed41342a82f5a3a9201819a82bf7a48313e296b)
Target: riscv64--
Thread model: posix
InstalledDir: /riscv-build/bin
Found candidate GCC installation: /riscv-build/rv64/lib/gcc/riscv64-unknown-elf/11.1.0
Found candidate GCC installation: /riscv-build/rv64/lib/gcc/riscv64-unknown-linux-gnu/11.1.0
Selected GCC installation: /riscv-build/rv64/lib/gcc/riscv64-unknown-linux-gnu/11.1.0
"/riscv-build/bin/clang-12" -cc1 -triple riscv64-- -emit-obj -mrelax-all --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hello.cpp -mrelocation-model static -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -nostdsysteminc -target-feature +m -target-feature +a -target-feature +f -target-feature +d -target-feature +c -target-feature -relax -target-feature -save-restore -target-abi lp64d -msmall-data-limit 8 -fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir /riscv-build/lib/clang/12.0.1 -I /riscv-build/rv64/riscv64-unknown-elf/include/c++/11.1.0/riscv64-unknown-elf/ -isysroot /riscv-build/rv64/riscv64-unknown-elf/ -internal-isystem /riscv-build/rv64/riscv64-unknown-elf//include/c++/11.1.0 -internal-isystem /riscv-build/rv64/riscv64-unknown-elf//include/c++/11.1.0/riscv64-unknown-linux-gnu -internal-isystem /riscv-build/rv64/riscv64-unknown-elf//include/c++/11.1.0/backward -internal-isystem /riscv-build/rv64/riscv64-unknown-elf/include -fdeprecated-macro -fdebug-compilation-dir /riscv-build/bin -ferror-limit 19 -fno-signed-char -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -faddrsig -o /tmp/hello-1b4de5.o -x c++ hello.cpp
clang -cc1 version 12.0.1 based upon LLVM 12.0.1 default target riscv64-unknown-linux-gnu
ignoring nonexistent directory "/riscv-build/rv64/riscv64-unknown-elf//include/c++/11.1.0/riscv64-unknown-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
/riscv-build/rv64/riscv64-unknown-elf/include/c++/11.1.0/riscv64-unknown-elf
/riscv-build/rv64/riscv64-unknown-elf//include/c++/11.1.0
/riscv-build/rv64/riscv64-unknown-elf//include/c++/11.1.0/backward
/riscv-build/rv64/riscv64-unknown-elf/include
/riscv-build/lib/clang/12.0.1/include
End of search list.
[ 269.514744] random: clang-12: uninitialized urandom read (4 bytes read)
"/riscv-build/bin/ld" --sysroot=/riscv-build/rv64/riscv64-unknown-elf/ -m elf64lriscv /riscv-build/rv64/riscv64-unknown-elf//lib/crt0.o /riscv-build/rv64/lib/gcc/riscv64-unknown-linux-gnu/11.1.0/crtbegin.o -L/riscv-build/rv64/riscv64-unknown-linux-gnu/lib -L/riscv-build/rv64/lib/gcc/riscv64-unknown-linux-gnu/11.1.0 -L/riscv-build/rv64/riscv64-unknown-elf//lib /tmp/hello-1b4de5.o -lgcc_s -lstdc++ --start-group -lc -lgloss --end-group -lgcc /riscv-build/rv64/lib/gcc/riscv64-unknown-linux-gnu/11.1.0/crtend.o -o hello
Any help is appreciated