xref: /linux/tools/include/nolibc/crt.h (revision 08ab9580726edcc12019d2097b56b88de1142ca7)
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 #ifndef NOLIBC_NO_RUNTIME
11 
12 #include "compiler.h"
13 
14 char **environ __attribute__((weak));
15 const unsigned long *_auxv __attribute__((weak));
16 
17 void _start(void);
18 static void __stack_chk_init(void);
19 static void exit(int);
20 static char *strrchr(const char *s, int c);
21 
22 extern void (*const __preinit_array_start[])(int, char **, char**) __attribute__((weak));
23 extern void (*const __preinit_array_end[])(int, char **, char**) __attribute__((weak));
24 
25 extern void (*const __init_array_start[])(int, char **, char**) __attribute__((weak));
26 extern void (*const __init_array_end[])(int, char **, char**) __attribute__((weak));
27 
28 extern void (*const __fini_array_start[])(void) __attribute__((weak));
29 extern void (*const __fini_array_end[])(void) __attribute__((weak));
30 
31 #ifndef NOLIBC_IGNORE_ERRNO
32 extern char *program_invocation_name __attribute__((weak));
33 extern char *program_invocation_short_name __attribute__((weak));
34 
35 static __inline__
36 char *__nolibc_program_invocation_short_name(char *long_name)
37 {
38 
39 	char *short_name;
40 
41 	short_name = strrchr(long_name, '/');
42 	if (!short_name || !short_name[0])
43 		return long_name;
44 
45 	return short_name + 1;
46 }
47 #endif /* NOLIBC_IGNORE_ERRNO */
48 
49 void _start_c(long *sp);
50 __attribute__((weak,used)) __nolibc_no_sanitize_undefined
51 void _start_c(long *sp)
52 {
53 	long argc;
54 	char **argv;
55 	char **envp;
56 	int exitcode;
57 	void (* const *ctor_func)(int, char **, char **);
58 	void (* const *dtor_func)(void);
59 	const unsigned long *auxv;
60 	/* silence potential warning: conflicting types for 'main' */
61 	int _nolibc_main(int, char **, char **) __asm__ ("main");
62 
63 	/* initialize stack protector */
64 	__stack_chk_init();
65 
66 	/*
67 	 * sp  :    argc          <-- argument count, required by main()
68 	 * argv:    argv[0]       <-- argument vector, required by main()
69 	 *          argv[1]
70 	 *          ...
71 	 *          argv[argc-1]
72 	 *          null
73 	 * environ: environ[0]    <-- environment variables, required by main() and getenv()
74 	 *          environ[1]
75 	 *          ...
76 	 *          null
77 	 * _auxv:   _auxv[0]      <-- auxiliary vector, required by getauxval()
78 	 *          _auxv[1]
79 	 *          ...
80 	 *          null
81 	 */
82 
83 	/* assign argc and argv */
84 	argc = *sp;
85 	argv = (void *)(sp + 1);
86 
87 	/* find environ */
88 	environ = envp = argv + argc + 1;
89 
90 	/* find _auxv */
91 	for (auxv = (void *)envp; *auxv++;)
92 		;
93 	_auxv = auxv;
94 
95 #ifndef NOLIBC_IGNORE_ERRNO
96 	if (argc > 0 && argv[0]) {
97 		program_invocation_name = argv[0];
98 		program_invocation_short_name = __nolibc_program_invocation_short_name(argv[0]);
99 	}
100 #endif /* NOLIBC_IGNORE_ERRNO */
101 
102 	for (ctor_func = __preinit_array_start; ctor_func < __preinit_array_end; ctor_func++)
103 		(*ctor_func)(argc, argv, envp);
104 	for (ctor_func = __init_array_start; ctor_func < __init_array_end; ctor_func++)
105 		(*ctor_func)(argc, argv, envp);
106 
107 	/* go to application */
108 	exitcode = _nolibc_main(argc, argv, envp);
109 
110 	for (dtor_func = __fini_array_end; dtor_func > __fini_array_start;)
111 		(*--dtor_func)();
112 
113 	exit(exitcode);
114 }
115 
116 #endif /* NOLIBC_NO_RUNTIME */
117 #endif /* _NOLIBC_CRT_H */
118