]> git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/crt/crt1.c
b3906dc5f36ced774262b3952ab75d2ab9a0cb9f
[wasi-libc.git] / libc-bottom-half / crt / crt1.c
1 #include <wasi/api.h>
2 extern void __wasm_call_ctors(void);
3 extern int __main_void(void);
4 extern void __wasm_call_dtors(void);
5
6 __attribute__((export_name("_start")))
7 void _start(void) {
8 // The linker synthesizes this to call constructors.
9 __wasm_call_ctors();
10
11 // Call `__main_void` which will either be the application's zero-argument
12 // `__main_void` function or a libc routine which obtains the command-line
13 // arguments and calls `__main_argv_argc`.
14 int r = __main_void();
15
16 // Call atexit functions, destructors, stdio cleanup, etc.
17 __wasm_call_dtors();
18
19 // If main exited successfully, just return, otherwise call
20 // `__wasi_proc_exit`.
21 if (r != 0) {
22 __wasi_proc_exit(r);
23 }
24 }