wasi-libc/libc-bottom-half/crt/crt1.c
Dan Gohman 4e45d2b5ad
Rename __prepare_for_exit to __wasm_call_dtors. (#201)
This emphasizes the relationship with `__wasm_call_ctors`. Note however
that while `__wasm_call_ctors` is synthesized by the linker,
`__wasm_call_dtors` is still defined by libc.

Static constructors are registered statically, but static destructors
need to be registered dynamically so that they only run if their
corresponding static constructors have run, and so that they're
ordered with respect to interleaved `atexit` calls.
2020-06-08 15:21:18 -07:00

25 lines
785 B
C

#include <wasi/api.h>
extern void __wasm_call_ctors(void);
extern int __original_main(void);
extern void __wasm_call_dtors(void);
__attribute__((export_name("_start")))
void _start(void) {
// The linker synthesizes this to call constructors.
__wasm_call_ctors();
// Call `__original_main` which will either be the application's zero-argument
// `__original_main` function or a libc routine which calls `__main_void`.
// TODO: Call `main` directly once we no longer have to support old compilers.
int r = __original_main();
// Call atexit functions, destructors, stdio cleanup, etc.
__wasm_call_dtors();
// If main exited successfully, just return, otherwise call
// `__wasi_proc_exit`.
if (r != 0) {
__wasi_proc_exit(r);
}
}