| 1 | #include <stdlib.h> |
| 2 | #include <stdint.h> |
| 3 | #include "libc.h" |
| 4 | |
| 5 | static void dummy() |
| 6 | { |
| 7 | } |
| 8 | |
| 9 | /* atexit.c and __stdio_exit.c override these. the latter is linked |
| 10 | * as a consequence of linking either __toread.c or __towrite.c. */ |
| 11 | weak_alias(dummy, __funcs_on_exit); |
| 12 | weak_alias(dummy, __stdio_exit); |
| 13 | weak_alias(dummy, _fini); |
| 14 | |
| 15 | extern weak hidden void (*const __fini_array_start)(void), (*const __fini_array_end)(void); |
| 16 | |
| 17 | static void libc_exit_fini(void) |
| 18 | { |
| 19 | 	uintptr_t a = (uintptr_t)&__fini_array_end; |
| 20 | 	for (; a>(uintptr_t)&__fini_array_start; a-=sizeof(void(*)())) |
| 21 | 		(*(void (**)())(a-sizeof(void(*)())))(); |
| 22 | 	_fini(); |
| 23 | } |
| 24 | |
| 25 | weak_alias(libc_exit_fini, __libc_exit_fini); |
| 26 | |
| 27 | _Noreturn void exit(int code) |
| 28 | { |
| 29 | 	__funcs_on_exit(); |
| 30 | 	__libc_exit_fini(); |
| 31 | 	__stdio_exit(); |
| 32 | 	_Exit(code); |
| 33 | } |