master
1#ifdef _REENTRANT
2#include <stdatomic.h>
3extern void __wasi_init_tp(void);
4#endif
5#ifdef __wasilibc_use_wasip2
6#include <wasi/wasip2.h>
7#else
8#include <wasi/api.h>
9#endif
10extern void __wasm_call_ctors(void);
11extern int __main_void(void);
12extern void __wasm_call_dtors(void);
13
14__attribute__((export_name("_start")))
15void _start(void) {
16 // Commands should only be called once per instance. This simple check
17 // ensures that the `_start` function isn't started more than once.
18 //
19 // We use `volatile` here to prevent the store to `started` from being
20 // sunk past any subsequent code, and to prevent any compiler from
21 // optimizing based on the knowledge that `_start` is the program
22 // entrypoint.
23#ifdef _REENTRANT
24 static volatile _Atomic int started = 0;
25 int expected = 0;
26 if (!atomic_compare_exchange_strong(&started, &expected, 1)) {
27 __builtin_trap();
28 }
29#else
30 static volatile int started = 0;
31 if (started != 0) {
32 __builtin_trap();
33 }
34 started = 1;
35#endif
36
37#ifdef _REENTRANT
38 __wasi_init_tp();
39#endif
40
41 // The linker synthesizes this to call constructors.
42 __wasm_call_ctors();
43
44 // Call `__main_void` which will either be the application's zero-argument
45 // `__main_void` function or a libc routine which obtains the command-line
46 // arguments and calls `__main_argv_argc`.
47 int r = __main_void();
48
49 // Call atexit functions, destructors, stdio cleanup, etc.
50 __wasm_call_dtors();
51
52 // If main exited successfully, just return, otherwise call
53 // `__wasi_proc_exit`.
54#ifdef __wasilibc_use_wasip2
55 if (r != 0) {
56 exit_result_void_void_t status = { .is_err = true };
57 exit_exit(&status);
58 }
59#else
60 if (r != 0) {
61 __wasi_proc_exit(r);
62 }
63#endif
64}