Segmentation fault on RISCV Virtual Machine(QEMU+Linux)

I have cross compiled LLVM & Clang (version 10.0.0) to run on RISC-V virtual machine, although it is successfully done generating object file for RISC-V, while running those object file i am getting segmentation fault because of crt0.o file, how can we write custom riscv crt0.S file if any one knows please guide me in the right direction!

riscv@busybear$ clang -o sample sample.c -L/lib --target=riscv64 -mno-relax -L/
bin/RISCV1.0/NEW_BIN/lib -march=rv64imafdc

[ 323.732567] sample[100]: unhandled signal 11 code 0x1 at 0x0000000000000000 in sample[10000+1000]
[ 323.735826] CPU: 0 PID: 100 Comm: sample Not tainted 5.0.0 #1
[ 323.736533] sepc: 0000000000000000 ra : 0000000000011458 sp : 0000003fffb6cdb0
[ 323.737962] gp : 0000000000010800 tp : 000000155570f720 t0 : 0000000000000000
[ 323.738714] t1 : 0000000000000000 t2 : 0000000000013570 s0 : 00000000000f5620
[ 323.739443] s1 : 00000000000f55f8 a0 : 0000000000013788 a1 : 0000000000000000
[ 323.740192] a2 : 0000000000000049 a3 : 000000000000002f a4 : 000000000000002f
[ 323.741270] a5 : 000000155570f730 a6 : 7efefefefefefeff a7 : 00000000000000dd
[ 323.741991] s2 : 00000000000ecf25 s3 : 00000000000d3e78 s4 : 00000000000f5630
[ 323.742745] s5 : 0000000000000008 s6 : 000000155570f730 s7 : 00000000000f55f8
[ 323.743522] s8 : 0000000000000000 s9 : 00000000000d4828 s10: 0000003fff96ffa2
[ 323.744277] s11: 00000000000d4868 t3 : 0000000000000000 t4 : 0000000000000000
[ 323.745483] t5 : 0000000000000005 t6 : 0000003fff96eee0
[ 323.745875] sstatus: 8000000000006020 sbadaddr: 0000000000000000 scause: 000000000000000c
Segmentation fault

Here Clang is native RISC-V compiler! and busybear is the RISC-V Operating System!

crt0.S comes from glibc. If you are trying to write your own, then you are doing something wrong. And the file name is actually called crt1.o. If you are trying to use crt0.o instead of crt1.o you are doing something wrong. You haven’t given any info explaining how you got to this point, how you built the native clang compiler, etc. Busybear is a small buildroot system, not really intended to run compilers. On sw-dev you mentioned using libgloss, which is also wrong. I think you have made so many mistakes getting to this point that I can’t help you unless you explain everything you have done step by step, in sufficient detail so that others can reproduce what you have done, and are willing to start over from scratch. And even then I might not be able to help. Linux compilers are complicated. It takes a fair amount of knowledge about how linux compilers work to be able to successfully cross compile them.

You would be better off running a real distro like fedora or debian. On fedora, you can just dnf install clang. You don’t have to build it unless you want to. And if you do want to build it, you can use known working compilers (clang or gcc) to build it natively.

Also, on a real distro, you can use gdb to debug your code.

Hello jim wilson,

Initially i built riscv-gnu-toolchain on X86 ubuntu machine along with qemu emulator for RISC-V

here is the link :

After this i have RISC-V linux running,

My goal is to build a native llvm & clang which runs on RISC-V linux so now i have done cross-compiling LLVM & Clang from Official using riscv-gnu-toolchain

Now i got the risc-v target binaries like this:

RISCV2.0/bin$ file clang-10
clang-10: ELF 64-bit LSB executable, UCB RISC-V, version 1 (GNU/Linux), dynamically linked, interpreter /lib/, for GNU/Linux 4.15.0, not stripped

these binaries will only run on RISC-V linux so I copied these binaries into RISC-V linux and tried to compile a sample program:


int main()

{return 0;}

It is compiling successfully when i use clang glibc’s crt0.o (i have cross-compiled glibc to riscv-unknown-linux target)

but i am getting memory error as i mentioned at the start!

I have increased the rootfs size for linux using dd command so now i have 100GB space which is enough for installation of llvm& clang