]> git.proxmox.com Git - wasi-libc.git/blob - basics/crt/crt1.c
a0f5a7349be7d72bdfd0690db6e01e1a646f6bc0
[wasi-libc.git] / basics / crt / crt1.c
1 extern void __wasm_call_ctors(void);
2 extern int __original_main(void);
3 extern void __prepare_for_exit(void);
4 void _Exit(int) __attribute__((noreturn));
5
6 void _start(void) {
7 // The linker synthesizes this to call constructors.
8 __wasm_call_ctors();
9
10 // Call `__original_main` which will either be the application's
11 // zero-argument `main` function (renamed by the compiler) or a libc
12 // routine which populates `argv` and `argc` and calls the application's
13 // two-argument `main`.
14 int r = __original_main();
15
16 // Call atexit functions, destructors, stdio cleanup, etc.
17 __prepare_for_exit();
18
19 // If main exited successfully, just return, otherwise call _Exit.
20 if (r != 0) {
21 _Exit(r);
22 }
23 }