wasi-libc/libc-bottom-half/crt/crt1.c
Dan Gohman 446cb3f1aa
Wasi snapshot preview1 (#140)
* Make __wasi_linkcount_t a uint64_t (#134)

Refs: https://github.com/WebAssembly/WASI/pull/127

* Generate the WASI interface from witx.

This replaces the hand-maintained <wasi/core.h> header with a
<wasi/api.h> generated from witx.

Most of the churn here is caused by upstream WASI renamings; hopefully
in the future ABI updates will be less noisy.
2019-11-21 20:06:00 -08:00

46 lines
1.4 KiB
C

#include <stdlib.h>
#include <sysexits.h>
#include <wasi/api.h>
#include <wasi/libc.h>
#include <wasi/libc-internal.h>
__wasi_errno_t __wasilibc_populate_environ(void) __attribute__((weak));
extern void __wasm_call_ctors(void);
extern int __original_main(void);
extern void __prepare_for_exit(void);
void _Exit(int) __attribute__((noreturn));
__wasi_errno_t __wasilibc_populate_libpreopen(void) __attribute__((weak));
void _start(void) {
// Record the preopened resources, if needed.
if (&__wasilibc_populate_libpreopen != NULL &&
__wasilibc_populate_libpreopen() != __WASI_ERRNO_SUCCESS)
{
_Exit(EX_OSERR);
}
// Fill in the environment from WASI syscalls, if needed.
if (&__wasilibc_populate_environ != NULL &&
__wasilibc_populate_environ() != __WASI_ERRNO_SUCCESS)
{
_Exit(EX_OSERR);
}
// The linker synthesizes this to call constructors.
__wasm_call_ctors();
// Call `__original_main` which will either be the application's
// zero-argument `main` function (renamed by the compiler) or a libc
// routine which populates `argv` and `argc` and calls the application's
// two-argument `main`.
int r = __original_main();
// Call atexit functions, destructors, stdio cleanup, etc.
__prepare_for_exit();
// If main exited successfully, just return, otherwise call _Exit.
if (r != 0) {
_Exit(r);
}
}