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