xref: /openbmc/linux/tools/include/nolibc/crt.h (revision 7f1005dd)
1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3  * C Run Time support for NOLIBC
4  * Copyright (C) 2023 Zhangjin Wu <falcon@tinylab.org>
5  */
6 
7 #ifndef _NOLIBC_CRT_H
8 #define _NOLIBC_CRT_H
9 
10 char **environ __attribute__((weak));
11 const unsigned long *_auxv __attribute__((weak));
12 
13 static void __stack_chk_init(void);
14 static void exit(int);
15 
16 __attribute__((weak))
17 void _start_c(long *sp)
18 {
19 	long argc;
20 	char **argv;
21 	char **envp;
22 	const unsigned long *auxv;
23 	/* silence potential warning: conflicting types for 'main' */
24 	int _nolibc_main(int, char **, char **) __asm__ ("main");
25 
26 	/* initialize stack protector */
27 	__stack_chk_init();
28 
29 	/*
30 	 * sp  :    argc          <-- argument count, required by main()
31 	 * argv:    argv[0]       <-- argument vector, required by main()
32 	 *          argv[1]
33 	 *          ...
34 	 *          argv[argc-1]
35 	 *          null
36 	 * environ: environ[0]    <-- environment variables, required by main() and getenv()
37 	 *          environ[1]
38 	 *          ...
39 	 *          null
40 	 * _auxv:   _auxv[0]      <-- auxiliary vector, required by getauxval()
41 	 *          _auxv[1]
42 	 *          ...
43 	 *          null
44 	 */
45 
46 	/* assign argc and argv */
47 	argc = *sp;
48 	argv = (void *)(sp + 1);
49 
50 	/* find environ */
51 	environ = envp = argv + argc + 1;
52 
53 	/* find _auxv */
54 	for (auxv = (void *)envp; *auxv++;)
55 		;
56 	_auxv = auxv;
57 
58 	/* go to application */
59 	exit(_nolibc_main(argc, argv, envp));
60 }
61 
62 #endif /* _NOLIBC_CRT_H */
63