Reputation: 391
how to write a simple startup code in C which initialize the stack pointer for RISC-V processor? (which have to compile thorugh risc-v compiler)
thanks
Upvotes: 0
Views: 7814
Reputation: 1068
The simplest start code that sets up a stack looks something like
_start:
/* Clear the link register, so debuggers know to terminate their
* backtrace. */
cfi_undefined (ra)
/* Setup the global pointer, which the ABI assumes points to the
* __global_pointer$ symbol. */
.option push
.option norelax
la gp, __global_pointer$
.option pop
/* Setup the stack pointer. This symbol will only be looked at
* here, so you can pick any name you want. */
la sp, __stack_pointer$
/* Call main, leaving the arguments undefined. */
call main
/* Spin after main() returns. */
1:
j 1b
you might also want to do something like calling __libc_init_array
and registering __libc_fini_array
with atexit
, clearing argc
and argv
(a0 and a1 to main), relocating the data section, and zeroing the BSS, depending on exactly how much of the C ABI you want to support.
SiFive's Freedom E SDK contains our full startup code: https://raw.githubusercontent.com/sifive/freedom-e-sdk/master/bsp/env/start.S , as well as the relevant linker scripts for some of our platforms. You can also see more complicated in newlib (https://github.com/riscv/riscv-newlib/blob/riscv-newlib-2.5.0/libgloss/riscv/crt0.S) and glibc (https://github.com/riscv/riscv-glibc/blob/riscv-glibc-2.26/sysdeps/riscv/start.S), though those both assume that the stack has already been initialized by the execution environment.
Upvotes: 5
Reputation: 219
There is SCR1 - an open-source RISC-V compatible core (https://github.com/syntacore/scr1) with SDK (https://github.com/syntacore/scr1-sdk, please, clone with submodules, recursively).
You can find sample applications in the SDK.
The startup code is located in the bootloader (https://github.com/syntacore/sc-bl).
See label _crt_start
in src/startup.S:
Upvotes: 2