How to resolve Segmentation Fault in RISC-V Program

150 views
Skip to first unread message

Vindhya Ejanthkar

unread,
Mar 4, 2024, 8:01:37 AM3/4/24
to RISC-V SW Dev
// factorial.c
#include <stdio.h>
unsigned long long factorial(unsigned int n) {
    if (n == 0)
        return 1;
    else
        return n * factorial(n - 1);
}
int main() {
    unsigned long long result = factorial(10);
    printf("Factorial of 10 = %llu\n", result);
    return 0;
}

  1. Compiled the program using RISC-V GCC Compiler with optimization flags (-O2) and profiling flag (-pg).
  2. Executed the compiled program using the Spike RISC-V simulator with pk.
  3. Encountered a segfault error during execution.

    Attaching my commands and output :
    vindhya@latitude-7390:~$ nano factorial.c
    vindhya@latitude-7390:~$ /home/latitude-7390/rvv64/bin/riscv64-unknown-elf-gcc -O2 -pg factorial.c -o factorial
    vindhya@latitude-7390:~$ /home/vindhya/riscv-vector/riscv-isa-sim/build/spike /home/latitude-7390/rvv64/bin/riscv64-unknown-elf/bin/pk ./factorial
    z  0000000000000000 ra 00000000000101d4 sp 0000003fff7ffff0 gp 000000000001b018
    tp 0000000000000000 t0 0000000000010818 t1 000000000000000f t2 0000000000000000
    s0 0000000000000000 s1 0000000000000000 a0 00000000000101d4 a1 0000003ffffffb58
    a2 0000000000000000 a3 0000000000000010 a4 000000000001a680 a5 0000000000000000
    a6 000000000000001f a7 0000000000000000 s2 0000000000000000 s3 0000000000000000
    s4 0000000000000000 s5 0000000000000000 s6 0000000000000000 s7 0000000000000000
    s8 0000000000000000 s9 0000000000000000 sA 0000000000000000 sB 0000000000000000
    t3 0000000000000000 t4 0000000000000000 t5 0000000000000000 t6 0000000000000000
    pc 00000000000101cc va/inst 0000003fff7ffff8 sr 8000000200006020
    User store segfault @ 0x0000003fff7ffff8

    Let me know what more information should i provide and how to resolve this.

Tommy Murphy

unread,
Mar 4, 2024, 8:26:37 AM3/4/24
to Vindhya Ejanthkar, RISC-V SW Dev
You're compiling your program with a bare-metal Newlib toolchain (e.g. riscv64-unknown-elf-gcc) but running Spike with pk which is normally (always?) intended for programs compiled with the Linux toolchain (e.g. riscv64-unknown-linux-gnu-gcc) creating a Linux user mode executable.

You should search for information about running bare-metal programs on Spike. There should be some useful info out there for your situation. Or else try compiling your program with a RISC-V Linux toolchain.

Bruce Hoult

unread,
Mar 4, 2024, 6:13:30 PM3/4/24
to Tommy Murphy, Vindhya Ejanthkar, RISC-V SW Dev
No, that's not correct. Newlib is simply a slimmer version of libc,
but still by default makes Linux syscalls for things such as printf.
It is absolutely normal to use riscv64-unknown-elf-gcc to make
programs to run in Spike because the most important attribute is it
links the program statically, so everything needed is included in the
one binary.

I use riscv64-unknown-elf-gcc all the time to make programs that I
want to run on a variety of RISC-V Linux boards with different OS
distros and different glibc versions.

On Tue, Mar 5, 2024 at 2:26 AM Tommy Murphy <tommy_...@hotmail.com> wrote:
>
> You're compiling your program with a bare-metal Newlib toolchain (e.g. riscv64-unknown-elf-gcc) but running Spike with pk which is normally (always?) intended for programs compiled with the Linux toolchain (e.g. riscv64-unknown-linux-gnu-gcc) creating a Linux user mode executable.
>
> You should search for information about running bare-metal programs on Spike. There should be some useful info out there for your situation. Or else try compiling your program with a RISC-V Linux toolchain.
>
> --
> You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+un...@groups.riscv.org.
> To view this discussion on the web visit https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/LO2P123MB7050F3AE610DEE51BD6386D2F9232%40LO2P123MB7050.GBRP123.PROD.OUTLOOK.COM.

Vindhya Ejanthkar

unread,
Mar 4, 2024, 10:06:23 PM3/4/24
to RISC-V SW Dev, Bruce Hoult, Vindhya Ejanthkar, RISC-V SW Dev, tommy_...@hotmail.com
Any idea how to resolve this ?

Tommy Murphy

unread,
Mar 5, 2024, 2:54:18 AM3/5/24
to Vindhya Ejanthkar, RISC-V SW Dev, Bruce Hoult, Vindhya Ejanthkar, RISC-V SW Dev
No, that's not correct. 

Apologies if anything that I posted was inadvertently misleading. Thanks for correcting it, Bruce.

> Any idea how to resolve this ?

Reply all
Reply to author
Forward
Message has been deleted
0 new messages