rework langinfo code for ABI compat and for use by time code
[musl] / src / env / __libc_start_main.c
index 70af77b..2a8698b 100644 (file)
@@ -1,24 +1,52 @@
+#include <elf.h>
 #include "libc.h"
 
-/* Any use of __environ/environ will override this symbol. */
-char **__dummy_environ = (void *)-1;
-weak_alias(__dummy_environ, ___environ);
+void __init_tls(size_t *);
+void __init_security(size_t *);
+void __init_ldso_ctors(void);
 
-int __libc_start_main(
-       int (*main)(int, char **, char **), int argc, char **argv,
-       int (*init)(int, char **, char **), void (*fini)(void),
-       void (*ldso_fini)(void))
+#ifndef SHARED
+static void dummy() {}
+weak_alias(dummy, _init);
+extern void (*const __init_array_start)() __attribute__((weak));
+extern void (*const __init_array_end)() __attribute__((weak));
+#endif
+
+#define AUX_CNT 38
+
+extern size_t __hwcap, __sysinfo;
+extern char *__progname, *__progname_full;
+
+void __init_libc(char **envp, char *pn)
 {
-       /* Save the environment if it may be used by libc/application */
-       char **envp = argv+argc+1;
-       if (___environ != (void *)-1) ___environ = envp;
+       size_t i, *auxv, aux[AUX_CNT] = { 0 };
+       __environ = envp;
+       for (i=0; envp[i]; i++);
+       libc.auxv = auxv = (void *)(envp+i+1);
+       for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT) aux[auxv[i]] = auxv[i+1];
+       __hwcap = aux[AT_HWCAP];
+       __sysinfo = aux[AT_SYSINFO];
+
+       if (pn) {
+               __progname = __progname_full = pn;
+               for (i=0; pn[i]; i++) if (pn[i]=='/') __progname = pn+i+1;
+       }
 
-       /* Avoid writing 0 and triggering unnecessary COW */
-       if (ldso_fini) libc.ldso_fini = ldso_fini;
-       if (fini) libc.fini = fini;
+       __init_tls(aux);
+       __init_security(aux);
+}
+
+int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv)
+{
+       char **envp = argv+argc+1;
 
-       /* Execute constructors (static) linked into the application */
-       if (init) init(argc, argv, envp);
+#ifndef SHARED
+       __init_libc(envp, argv[0]);
+       _init();
+       uintptr_t a = (uintptr_t)&__init_array_start;
+       for (; a<(uintptr_t)&__init_array_end; a+=sizeof(void(*)()))
+               (*(void (**)())a)();
+#endif
 
        /* Pass control to to application */
        exit(main(argc, argv, envp));