summaryrefslogtreecommitdiff
path: root/lib/libc/internal/init/init.c
blob: 85d4d8f02f7cc5f35866093e25e1b406524d2295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <alloca.h>
#include <libc.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/cdefs.h>

#define BUFSIZ 4096

extern void __libc_init_vdso(void) __attribute__((weak));
extern void __libc_init_tls(void) __attribute__((weak));
extern void __libc_init_io(void) __attribute__((weak));

extern int main(int, char **, char **);

struct libc __libc = { 0 };
char **environ;
char *__progname;

__used void __libc_init(uintptr_t *rsp)
{
	char **argv;
	int argc;

	argc = (int)(*rsp);
	argv = (char **)(++rsp);

	rsp += argc;

	environ = (char **)(++rsp);
	__progname = argv[0];

	while (*rsp)
		rsp++;

	__libc.auxv = (size_t *)++rsp;

	if (__libc_init_io) {
		__libc_init_io();

		__libc.stdout.buf = alloca(BUFSIZ);
		__libc.stdout.buf_size = BUFSIZ;
		__libc.stdout.buf_len = 0;
		__libc.stdout.buf_pos = 0;

		__libc.stdin.buf = alloca(BUFSIZ);
		__libc.stdin.buf_size = BUFSIZ;
		__libc.stdin.buf_len = 0;
		__libc.stdin.buf_pos = 0;
	}

	if (__libc_init_tls)
		__libc_init_tls();

	if (__libc_init_vdso)
		__libc_init_vdso();

	exit(main(argc, argv, environ));
}