mes/lib/linux/riscv64-mes-m2/crt1.M1

75 lines
2.2 KiB
Plaintext

## Copyright (C) 2021 Andrius Štikonas
## Copyright (C) 2023 Janneke Nieuwenhuizen <janneke@gnu.org>
##
## This file is part of stage0.
##
## stage0 is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## stage0 is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with stage0. If not, see <http://www.gnu.org/licenses/>.
:_start
RD_FP RS1_SP MV ; Protect stack pointer
; Prepare argv
RD_A0 RS1_FP !8 ADDI ; ARGV_address = FP + 8
RD_SP RS1_SP !-8 ADDI
RS1_SP RS2_A0 SD ; Put argv on the stack
; Prepare envp
RD_A0 RS1_FP MV ; Address we need to load from
RD_A0 RS1_A0 LD ; Get ARGC
RD_A0 RS1_A0 !2 ADDI ; OFFSET = ARGC + 2
RD_A0 RS1_A0 RS2_X3 SLLI ; OFFSET = OFFSET * WORDSIZE
RD_A0 RS1_FP RS2_A0 ADD ; ENVP_address = RSP + OFFSET
RD_SP RS1_SP !-8 ADDI
RS1_SP RS2_A0 SD ; Put envp on the stack
# AAARG, this also does not work!?
# pop____%t0 ; envp
# push___%t0
# li_____%t0,$i32 &GLOBAL_environ
# li_____%t0,$i16_0000 @0
# XXX avoid Illegal instruction
# li_____%t1,$i32 &GLOBAL___stdin
# sw_____%t0,0(%t1)
# li_____%t0,$i16_0000 @1
# srai___%t0,16
# li_____%t1,$i32 &GLOBAL___stdout
# sw_____%t0,0(%t1)
# li_____%t0,$i16_0000 @2
# XXX avoid Segmentation fault
# srai___%t0,16
# li_____%t1,$i32 &GLOBAL___stderr
# sw_____%t0,0(%t1)
; Stack offset
RD_FP RS1_FP !8 ADDI
; Init libc
RD_RA $FUNCTION___init_io JAL
; Call main function
RD_RA $FUNCTION_main JAL
; Put return value on the stack so that _exit gets it
RD_SP RS1_SP !-16 ADDI
RS1_SP RS2_A0 SD
; Exit to kernel
RD_A0 RS1_SP LD
RD_A7 !93 ADDI ; Syscall for exit
ECALL ; Exit with code in a0