about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2015-11-11 22:08:23 -0500
committerRich Felker <dalias@aerifal.cx>2015-11-11 22:08:23 -0500
commitad1cd43a86645ba2d4f7c8747240452a349d6bc1 (patch)
treedb609558e4f10ed6554517d30b47bababec06c76
parent4aaf879eb08a22c501b43d8604e67614f7dbc55f (diff)
downloadmusl-ad1cd43a86645ba2d4f7c8747240452a349d6bc1.tar.gz
musl-ad1cd43a86645ba2d4f7c8747240452a349d6bc1.tar.xz
musl-ad1cd43a86645ba2d4f7c8747240452a349d6bc1.zip
unify static and dynamic libc init/fini code paths
use weak definitions that the dynamic linker can override instead of
preprocessor conditionals on SHARED so that the same libc start and
exit code can be used for both static and dynamic linking.
-rw-r--r--src/env/__libc_start_main.c26
-rw-r--r--src/exit/exit.c20
-rw-r--r--src/ldso/dynlink.c4
3 files changed, 22 insertions, 28 deletions
diff --git a/src/env/__libc_start_main.c b/src/env/__libc_start_main.c
index d1f6a5e1..7ffada0b 100644
--- a/src/env/__libc_start_main.c
+++ b/src/env/__libc_start_main.c
@@ -8,21 +8,15 @@
 
 void __init_tls(size_t *);
 
-#ifndef SHARED
-static void dummy() {}
-weak_alias(dummy, _init);
+extern void _init() __attribute__((weak));
 extern void (*const __init_array_start)() __attribute__((weak));
 extern void (*const __init_array_end)() __attribute__((weak));
-#endif
 
 static void dummy1(void *p) {}
 weak_alias(dummy1, __init_ssp);
 
 #define AUX_CNT 38
 
-#ifndef SHARED
-static
-#endif
 void __init_libc(char **envp, char *pn)
 {
 	size_t i, *auxv, aux[AUX_CNT] = { 0 };
@@ -57,20 +51,22 @@ void __init_libc(char **envp, char *pn)
 	libc.secure = 1;
 }
 
+static void libc_start_init(void)
+{
+	if (_init) _init();
+	uintptr_t a = (uintptr_t)&__init_array_start;
+	for (; a<(uintptr_t)&__init_array_end; a+=sizeof(void(*)()))
+		(*(void (**)())a)();
+}
+
+weak_alias(libc_start_init, __libc_start_init);
+
 int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv)
 {
 	char **envp = argv+argc+1;
 
-#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)();
-#else
-	void __libc_start_init(void);
 	__libc_start_init();
-#endif
 
 	/* Pass control to the application */
 	exit(main(argc, argv, envp));
diff --git a/src/exit/exit.c b/src/exit/exit.c
index 163d8f1b..b391f338 100644
--- a/src/exit/exit.c
+++ b/src/exit/exit.c
@@ -11,24 +11,24 @@ static void dummy()
 weak_alias(dummy, __funcs_on_exit);
 weak_alias(dummy, __stdio_exit);
 
-#ifndef SHARED
-weak_alias(dummy, _fini);
+extern void _fini() __attribute__((weak));
 extern void (*const __fini_array_start)() __attribute__((weak));
 extern void (*const __fini_array_end)() __attribute__((weak));
-#endif
 
-_Noreturn void exit(int code)
+static void libc_exit_fini(void)
 {
-	__funcs_on_exit();
-
-#ifndef SHARED
 	uintptr_t a = (uintptr_t)&__fini_array_end;
 	for (; a>(uintptr_t)&__fini_array_start; a-=sizeof(void(*)()))
 		(*(void (**)())(a-sizeof(void(*)())))();
-	_fini();
-#endif
+	if (_fini) _fini();
+}
 
-	__stdio_exit();
+weak_alias(libc_exit_fini, __libc_exit_fini);
 
+_Noreturn void exit(int code)
+{
+	__funcs_on_exit();
+	__libc_exit_fini();
+	__stdio_exit();
 	_Exit(code);
 }
diff --git a/src/ldso/dynlink.c b/src/ldso/dynlink.c
index 5fbe2bb5..ac755d94 100644
--- a/src/ldso/dynlink.c
+++ b/src/ldso/dynlink.c
@@ -1175,7 +1175,7 @@ static void kernel_mapped_dso(struct dso *p)
 	p->kernel_mapped = 1;
 }
 
-static void do_fini()
+void __libc_exit_fini()
 {
 	struct dso *p;
 	size_t dyn[DYN_CNT];
@@ -1659,8 +1659,6 @@ _Noreturn void __dls3(size_t *sp)
 	debug.state = 0;
 	_dl_debug_state();
 
-	__init_libc(envp, argv[0]);
-	atexit(do_fini);
 	errno = 0;
 
 	CRTJMP((void *)aux[AT_ENTRY], argv-1);