1/*
2 * This file is subject to the terms and conditions of the LGPL V2.1
3 * License.  See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2018 Kalray Inc.
7 */
8
9/* Startup code compliant to the ELF KVX ABI */
10
11#include <libc-symbols.h>
12#include <features.h>
13
14.type	    main,@function
15.type	    __uClibc_main,@function
16
17/*
18 * When we enter this piece of code, the program stack has been
19 * layed out by the kernel like this:
20 *        argc            argument counter (integer)
21 *        argv[0]         program name (pointer)
22 *        argv[1...N]     program args (pointers)
23 *        argv[argc-1]    end of args (integer)
24 *        NULL
25 *        env[0...N]      environment variables (pointers)
26 *        NULL
27 *
28 *  Moreover, when using dynamic loader, $r0 contains the rtld_fini
29 *  address
30 *
31 * And we need to call the following function:
32 *  __uClibc_main (int (*main) (int, char **, char **), int argc,
33 *		char **argv, void (*init) (void), void (*fini) (void),
34 *		void (*rtld_fini) (void), void *stack_end)
35 */
36.text
37.globl	_start
38.type	_start,@function
39.align 	8
40C_SYMBOL_NAME(_start):
41	/* Load argc from stack */
42	ld $r1 = 0[$sp]
43	/* Load argv addr from stack */
44	addd $r2 = $sp, 0x8
45#ifdef __PIC__
46	pcrel $r7 = @gotaddr()
47#endif
48	;;
49	/* $r0 contains rtld_fini when run by dynamic loader */
50	copyd $r5 = $r0
51	/* prepare __uClibc_main arg */
52#ifndef __PIC__
53	make $r3 = _init
54	make $r4 = _fini
55#endif
56	;;
57	/* Align stack to 32-byte boundary */
58	andd $sp = $sp, -32
59	make $r8 = 0
60	make $fp = 0
61	/* prepare __uClibc_main arg */
62#ifdef __PIC__
63	ld $r3 = @got(_init)[$r7]
64#endif
65	;;
66	/* Setup stack_end for __uClibc_main */
67	copyd $r6 = $sp
68	/* Clear compute status */
69	set $cs = $r8
70#ifdef __PIC__
71	ld $r4 = @got(_fini)[$r7]
72#endif
73	;;
74#ifdef __PIC__
75	ld $r0 = @got(main)[$r7]
76#else
77	make $r0 = main
78#endif
79	goto __uClibc_main
80	;;
81	/* We should never return ! */
82	errop
83	;;
84