mirror of
https://git.proxmox.com/git/wasi-libc
synced 2025-10-19 22:03:50 +00:00

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.
25 lines
785 B
C
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);
|
|
}
|
|
}
|