xref: /linux/tools/include/nolibc/crt.h (revision 981368e1440b76f68b1ac8f5fb14e739f80ecc4e)
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 void _start_c(long *sp)
17 {
18 	long argc;
19 	char **argv;
20 	char **envp;
21 	const unsigned long *auxv;
22 	/* silence potential warning: conflicting types for 'main' */
23 	int _nolibc_main(int, char **, char **) __asm__ ("main");
24 
25 	/* initialize stack protector */
26 	__stack_chk_init();
27 
28 	/*
29 	 * sp  :    argc          <-- argument count, required by main()
30 	 * argv:    argv[0]       <-- argument vector, required by main()
31 	 *          argv[1]
32 	 *          ...
33 	 *          argv[argc-1]
34 	 *          null
35 	 * environ: environ[0]    <-- environment variables, required by main() and getenv()
36 	 *          environ[1]
37 	 *          ...
38 	 *          null
39 	 * _auxv:   _auxv[0]      <-- auxiliary vector, required by getauxval()
40 	 *          _auxv[1]
41 	 *          ...
42 	 *          null
43 	 */
44 
45 	/* assign argc and argv */
46 	argc = *sp;
47 	argv = (void *)(sp + 1);
48 
49 	/* find environ */
50 	environ = envp = argv + argc + 1;
51 
52 	/* find _auxv */
53 	for (auxv = (void *)envp; *auxv++;)
54 		;
55 	_auxv = auxv;
56 
57 	/* go to application */
58 	exit(_nolibc_main(argc, argv, envp));
59 }
60 
61 #endif /* _NOLIBC_CRT_H */
62