rework langinfo code for ABI compat and for use by time code
[musl] / src / env / __libc_start_main.c
1 #include <elf.h>
2 #include "libc.h"
3
4 void __init_tls(size_t *);
5 void __init_security(size_t *);
6 void __init_ldso_ctors(void);
7
8 #ifndef SHARED
9 static void dummy() {}
10 weak_alias(dummy, _init);
11 extern void (*const __init_array_start)() __attribute__((weak));
12 extern void (*const __init_array_end)() __attribute__((weak));
13 #endif
14
15 #define AUX_CNT 38
16
17 extern size_t __hwcap, __sysinfo;
18 extern char *__progname, *__progname_full;
19
20 void __init_libc(char **envp, char *pn)
21 {
22         size_t i, *auxv, aux[AUX_CNT] = { 0 };
23         __environ = envp;
24         for (i=0; envp[i]; i++);
25         libc.auxv = auxv = (void *)(envp+i+1);
26         for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT) aux[auxv[i]] = auxv[i+1];
27         __hwcap = aux[AT_HWCAP];
28         __sysinfo = aux[AT_SYSINFO];
29
30         if (pn) {
31                 __progname = __progname_full = pn;
32                 for (i=0; pn[i]; i++) if (pn[i]=='/') __progname = pn+i+1;
33         }
34
35         __init_tls(aux);
36         __init_security(aux);
37 }
38
39 int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv)
40 {
41         char **envp = argv+argc+1;
42
43 #ifndef SHARED
44         __init_libc(envp, argv[0]);
45         _init();
46         uintptr_t a = (uintptr_t)&__init_array_start;
47         for (; a<(uintptr_t)&__init_array_end; a+=sizeof(void(*)()))
48                 (*(void (**)())a)();
49 #endif
50
51         /* Pass control to to application */
52         exit(main(argc, argv, envp));
53         return 0;
54 }