From 25bec21f20208a9369656a337cf5325e7b3a5a8d Mon Sep 17 00:00:00 2001 From: taitep Date: Sun, 4 Jan 2026 18:51:23 +0100 Subject: Initial commit --- src/crt0.S | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/crt0.S (limited to 'src/crt0.S') diff --git a/src/crt0.S b/src/crt0.S new file mode 100644 index 0000000..97228c1 --- /dev/null +++ b/src/crt0.S @@ -0,0 +1,70 @@ +# vim: filetype=asm: +/* Copyright (c) 2017 SiFive Inc. All rights reserved. + + This copyrighted material is made available to anyone wishing to use, + modify, copy, or redistribute it subject to the terms and conditions + of the FreeBSD License. This program is distributed in the hope that + it will be useful, but WITHOUT ANY WARRANTY expressed or implied, + including the implied warranties of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. A copy of this license is available at + http://www.opensource.org/licenses. + + Modified in 2026 by taitep to run bare metal on the TRVE emulator, +*/ + +#include "newlib.h" + +#========================================================================= +# crt0.S : Entry point for RISC-V user programs +#========================================================================= + + .text + .global _start, _exit, _heap_end + .type _start, @function +_start: + # Initialize the stack pointer + la sp, _stack_top + + # Initialize global pointer +.option push +.option norelax +1:auipc gp, %pcrel_hi(__global_pointer$) + addi gp, gp, %pcrel_lo(1b) +.option pop + + # Clear the bss segment + la a0, _bss_start + la a2, _bss_end + sub a2, a2, a0 + li a1, 0 + call memset + + # Clear the sbss segment + la a0, _sbss_start + la a2, _sbss_end + sub a2, a2, a0 + li a1, 0 + call memset + +#ifdef _LITE_EXIT + # Make reference to atexit weak to avoid unconditionally pulling in + # support code. Refer to comments in __atexit.c for more details. + .weak atexit + la a0, atexit + beqz a0, .Lweak_atexit + .weak __libc_fini_array +#endif + + la a0, __libc_fini_array # Register global termination functions + call atexit # to be called upon exit +#ifdef _LITE_EXIT +.Lweak_atexit: +#endif + call __libc_init_array # Run global initialization functions + + call main + call exit +_exit: j _exit + .size _start, .-_start +_heap_end: + .dword _heap_start -- cgit v1.2.3