1/* Startup code compliant to the ELF LoongArch ABI.
2   Copyright (C) 2022 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4
5
6   The GNU C Library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Lesser General Public
8   License as published by the Free Software Foundation; either
9   version 2.1 of the License, or (at your option) any later version.
10
11   The GNU C Library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with the GNU C Library.  If not, see
18   <https://www.gnu.org/licenses/>.  */
19
20#define __ASSEMBLY__ 1
21#include <entry.h>
22#include <sys/asm.h>
23
24/* The entry point's job is to call __libc_start_main.  Per the ABI,
25   a0 contains the address of a function to be passed to atexit.
26   __libc_start_main wants this in a5.  */
27
28/*
29int
30__libc_start_main (int (*main) (int, char **, char **),
31		   int argc,
32		   char **argv,
33		   __typeof (main) init,
34		   void (*fini) (void),
35		   void (*rtld_fini) (void),
36		   void *stack_end);
37 */
38
39ENTRY (ENTRY_POINT)
40
41/* Terminate call stack by noting ra is undefined.  Use a dummy
42   .cfi_label to force starting the FDE.  */
43	.cfi_label .Ldummy
44	cfi_undefined (1)
45	or		a5, a0, zero /* rtld_fini */
46
47/* We must get symbol main through GOT table, since main may not be local.
48   For instance: googletest defines main in dynamic library.  */
49	la.got		a0, t0, main
50	REG_L		a1, sp, 0
51	ADDI		a2, sp, SZREG
52
53	/* Adjust $sp for 16-aligned */
54	BSTRINS		sp, zero, 3, 0
55
56	move		a3, zero /* used to be init */
57	move		a4, zero /* used to be fini */
58	or		a6, sp, zero /* stack_end */
59
60	la.got		ra, t0, __libc_start_main
61	jirl		ra, ra, 0
62
63	la.got		ra, t0, abort
64	jirl		ra, ra, 0
65END (ENTRY_POINT)
66