#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
+#include <stdint.h>
#include "libc.h"
-/* __overflow.c and atexit.c override these */
-static int (*const dummy)() = 0;
+static void dummy()
+{
+}
+
+/* atexit.c and __stdio_exit.c override these. the latter is linked
+ * as a consequence of linking either __toread.c or __towrite.c. */
weak_alias(dummy, __funcs_on_exit);
-weak_alias(dummy, __fflush_on_exit);
+weak_alias(dummy, __stdio_exit);
-void exit(int code)
-{
- static int lock;
+#ifndef SHARED
+weak_alias(dummy, _fini);
+extern void (*const __fini_array_start)() __attribute__((weak));
+extern void (*const __fini_array_end)() __attribute__((weak));
+#endif
- /* If more than one thread calls exit, hang until _Exit ends it all */
- LOCK(&lock);
+_Noreturn void exit(int code)
+{
+ __funcs_on_exit();
- /* Only do atexit & stdio flush if they were actually used */
- if (__funcs_on_exit) __funcs_on_exit();
- if (__fflush_on_exit) __fflush_on_exit((void *)0);
+#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
- /* Destructor s**t is kept separate from atexit to avoid bloat */
- if (libc.fini) libc.fini();
- if (libc.ldso_fini) libc.ldso_fini();
+ __stdio_exit();
_Exit(code);
for(;;);