]> git.proxmox.com Git - wasi-libc.git/blame - libc-bottom-half/crt/crt1.c
New upstream version 0.0~git20200731.215adc8
[wasi-libc.git] / libc-bottom-half / crt / crt1.c
CommitLineData
446cb3f1 1#include <wasi/api.h>
320054e8 2extern void __wasm_call_ctors(void);
afbf94c3 3extern int __original_main(void);
4e45d2b5 4extern void __wasm_call_dtors(void);
320054e8 5
a6d0b4c7 6__attribute__((export_name("_start")))
320054e8 7void _start(void) {
1f11b91e 8 // The linker synthesizes this to call constructors.
320054e8
DG
9 __wasm_call_ctors();
10
d9066a87
DG
11 // Call `__original_main` which will either be the application's zero-argument
12 // `__original_main` function or a libc routine which calls `__main_void`.
13 // TODO: Call `main` directly once we no longer have to support old compilers.
afbf94c3 14 int r = __original_main();
320054e8 15
1f11b91e 16 // Call atexit functions, destructors, stdio cleanup, etc.
4e45d2b5 17 __wasm_call_dtors();
320054e8 18
410c6607
DG
19 // If main exited successfully, just return, otherwise call
20 // `__wasi_proc_exit`.
320054e8 21 if (r != 0) {
410c6607 22 __wasi_proc_exit(r);
320054e8
DG
23 }
24}