Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
14char **environ __attribute__((weak));
15const unsigned long *_auxv __attribute__((weak));
16
17void _start(void);
18static void __stack_chk_init(void);
19static void exit(int);
20
21extern void (*const __preinit_array_start[])(int, char **, char**) __attribute__((weak));
22extern void (*const __preinit_array_end[])(int, char **, char**) __attribute__((weak));
23
24extern void (*const __init_array_start[])(int, char **, char**) __attribute__((weak));
25extern void (*const __init_array_end[])(int, char **, char**) __attribute__((weak));
26
27extern void (*const __fini_array_start[])(void) __attribute__((weak));
28extern void (*const __fini_array_end[])(void) __attribute__((weak));
29
30void _start_c(long *sp);
31__attribute__((weak,used))
32#if __nolibc_has_feature(undefined_behavior_sanitizer)
33 __attribute__((no_sanitize("function")))
34#endif
35void _start_c(long *sp)
36{
37 long argc;
38 char **argv;
39 char **envp;
40 int exitcode;
41 void (* const *ctor_func)(int, char **, char **);
42 void (* const *dtor_func)(void);
43 const unsigned long *auxv;
44 /* silence potential warning: conflicting types for 'main' */
45 int _nolibc_main(int, char **, char **) __asm__ ("main");
46
47 /* initialize stack protector */
48 __stack_chk_init();
49
50 /*
51 * sp : argc <-- argument count, required by main()
52 * argv: argv[0] <-- argument vector, required by main()
53 * argv[1]
54 * ...
55 * argv[argc-1]
56 * null
57 * environ: environ[0] <-- environment variables, required by main() and getenv()
58 * environ[1]
59 * ...
60 * null
61 * _auxv: _auxv[0] <-- auxiliary vector, required by getauxval()
62 * _auxv[1]
63 * ...
64 * null
65 */
66
67 /* assign argc and argv */
68 argc = *sp;
69 argv = (void *)(sp + 1);
70
71 /* find environ */
72 environ = envp = argv + argc + 1;
73
74 /* find _auxv */
75 for (auxv = (void *)envp; *auxv++;)
76 ;
77 _auxv = auxv;
78
79 for (ctor_func = __preinit_array_start; ctor_func < __preinit_array_end; ctor_func++)
80 (*ctor_func)(argc, argv, envp);
81 for (ctor_func = __init_array_start; ctor_func < __init_array_end; ctor_func++)
82 (*ctor_func)(argc, argv, envp);
83
84 /* go to application */
85 exitcode = _nolibc_main(argc, argv, envp);
86
87 for (dtor_func = __fini_array_end; dtor_func > __fini_array_start;)
88 (*--dtor_func)();
89
90 exit(exitcode);
91}
92
93#endif /* NOLIBC_NO_RUNTIME */
94#endif /* _NOLIBC_CRT_H */