用于堆栈指针初始化的 RISC-V 启动代码

Ane*_*ran 0 riscv

如何用 C 语言编写一个简单的启动代码来初始化 RISC-V 处理器的堆栈指针?(必须通过 risc-v 编译器进行编译)

谢谢

小智 5

设置堆栈的最简单的启动代码看起来像

_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
Run Code Online (Sandbox Code Playgroud)

您可能还需要执行一些操作,例如调用并__libc_init_array注册、清除和(a0 和 a1 到 main)、重新定位数据部分以及将 BSS 归零,具体取决于您想要支持的 C ABI 数量。__libc_fini_arrayatexitargcargv

SiFive 的 Freedom E SDK 包含我们完整的启动代码:https://raw.githubusercontent.com/si Five /freedom-e-sdk/master/bsp/env/start.S 以及我们某些平台的相关链接器脚本。您还可以在 newlib ( https://github.com/riscv/riscv-newlib/blob/riscv-newlib-2.5.0/libgloss/riscv/crt0.S ) 和 glibc ( https://github.com/riscv-newlib-2.5.0/libgloss/riscv/crt0.S )中看到更复杂的内容。 com/riscv/riscv-glibc/blob/riscv-glibc-2.26/sysdeps/riscv/start.S),尽管它们都假设堆栈已由执行环境初始化。