0

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

Celuk
  • 561
  • 7
  • 17
  • I would say your c++ code or simulator is incorrect because of `segmentation fault` but you are saying that it works when you compiled as c code with `clang`. I still want to ask that did you try any other c++ code to compile and run? Also another question, why you used `-lgcc_s` flag while compiling with `clang++`? Anyways, maybe you can try to compile c++ code with `clang` instead of `clang++` as like `clang example.cpp -lstdc++` or `clang example.cpp -x c++` – Celuk Feb 03 '22 at 07:17
  • Yes, c code excuted succesfully but c++ didn't.We tried a simple cpp program (printing helloworld). Added gcc_s to avoid the following during compilation but got seg fault ld: error: undefined error: _unwind_resume referenced by ios_init.cc ios_init.o:(std::ios_base::sync_with_stdio(bool)) in archive /riscv-build/rv64/riscv64-unknown-elf//lib/libstdc++.a We also tried to use LLVM's own unwind library but the compilation failed with the following error: ld: error: relocation refers to a symbol in discarded section: .L0 defined in /riscv-build/rv64/riscv64-unknown-elf//lib/libstdc++.a – Dheeraj Feb 07 '22 at 10:54
  • When we tried compiling with clang instead of clang++ we go the same segmentation fault issue. – Dheeraj Feb 07 '22 at 10:55
  • In order to be sure that it is because `clang` compilation error, can you also try another simulator other than `qemu` like [spike](https://github.com/riscv-software-src/riscv-isa-sim). Also try with `riscv-unknown-elf-g++` compiler in `riscv-gnu-toolchain`. – Celuk Feb 07 '22 at 15:15
  • Thanks, we will try the following things. – Dheeraj Feb 08 '22 at 06:37
  • We had tried the suggestions given by you, but there is no improvement in the error. Kindly let us know if any other things can be tried – Dheeraj Mar 07 '22 at 11:41

0 Answers0