Debian Codegraph @

libc-top-half/musl/src/exit/exit.c

raw
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 #ifdef __wasilibc_unmodified_upstream // fini
14 weak_alias(dummy, _fini);
15
16 extern weak hidden void (*const __fini_array_start)(void), (*const __fini_array_end)(void);
17
18 static void libc_exit_fini(void)
19 {
20 uintptr_t a = (uintptr_t)&__fini_array_end;
21 for (; a>(uintptr_t)&__fini_array_start; a-=sizeof(void(*)()))
22 (*(void (**)())(a-sizeof(void(*)())))();
23 _fini();
24 }
25
26 weak_alias(libc_exit_fini, __libc_exit_fini);
27 #endif
28
29 #ifdef __wasilibc_unmodified_upstream // WASI libc uses a custom exit
30 _Noreturn void exit(int code)
31 {
32 __funcs_on_exit();
33 __libc_exit_fini();
34 __stdio_exit();
35 _Exit(code);
36 }
37 #else
38 // Split out the cleanup functions so that we can call them without calling
39 // _Exit if we don't need to. This allows _start to just return if main
40 // returns 0.
41 void __wasm_call_dtors(void)
42 {
43 __funcs_on_exit();
44 __stdio_exit();
45 }
46
47 _Noreturn void exit(int code)
48 {
49 __wasm_call_dtors();
50 _Exit(code);
51 }
52 #endif