Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/include/nolibc/crt.h
26289 views
1
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2
/*
3
* C Run Time support for NOLIBC
4
* Copyright (C) 2023 Zhangjin Wu <[email protected]>
5
*/
6
7
#ifndef _NOLIBC_CRT_H
8
#define _NOLIBC_CRT_H
9
10
#include "compiler.h"
11
12
char **environ __attribute__((weak));
13
const unsigned long *_auxv __attribute__((weak));
14
15
void _start(void);
16
static void __stack_chk_init(void);
17
static void exit(int);
18
19
extern void (*const __preinit_array_start[])(int, char **, char**) __attribute__((weak));
20
extern void (*const __preinit_array_end[])(int, char **, char**) __attribute__((weak));
21
22
extern void (*const __init_array_start[])(int, char **, char**) __attribute__((weak));
23
extern void (*const __init_array_end[])(int, char **, char**) __attribute__((weak));
24
25
extern void (*const __fini_array_start[])(void) __attribute__((weak));
26
extern void (*const __fini_array_end[])(void) __attribute__((weak));
27
28
void _start_c(long *sp);
29
__attribute__((weak,used))
30
#if __nolibc_has_feature(undefined_behavior_sanitizer)
31
__attribute__((no_sanitize("function")))
32
#endif
33
void _start_c(long *sp)
34
{
35
long argc;
36
char **argv;
37
char **envp;
38
int exitcode;
39
void (* const *ctor_func)(int, char **, char **);
40
void (* const *dtor_func)(void);
41
const unsigned long *auxv;
42
/* silence potential warning: conflicting types for 'main' */
43
int _nolibc_main(int, char **, char **) __asm__ ("main");
44
45
/* initialize stack protector */
46
__stack_chk_init();
47
48
/*
49
* sp : argc <-- argument count, required by main()
50
* argv: argv[0] <-- argument vector, required by main()
51
* argv[1]
52
* ...
53
* argv[argc-1]
54
* null
55
* environ: environ[0] <-- environment variables, required by main() and getenv()
56
* environ[1]
57
* ...
58
* null
59
* _auxv: _auxv[0] <-- auxiliary vector, required by getauxval()
60
* _auxv[1]
61
* ...
62
* null
63
*/
64
65
/* assign argc and argv */
66
argc = *sp;
67
argv = (void *)(sp + 1);
68
69
/* find environ */
70
environ = envp = argv + argc + 1;
71
72
/* find _auxv */
73
for (auxv = (void *)envp; *auxv++;)
74
;
75
_auxv = auxv;
76
77
for (ctor_func = __preinit_array_start; ctor_func < __preinit_array_end; ctor_func++)
78
(*ctor_func)(argc, argv, envp);
79
for (ctor_func = __init_array_start; ctor_func < __init_array_end; ctor_func++)
80
(*ctor_func)(argc, argv, envp);
81
82
/* go to application */
83
exitcode = _nolibc_main(argc, argv, envp);
84
85
for (dtor_func = __fini_array_end; dtor_func > __fini_array_start;)
86
(*--dtor_func)();
87
88
exit(exitcode);
89
}
90
91
#endif /* _NOLIBC_CRT_H */
92
93