Path: blob/main/system/lib/standalone/__main_void.c
6174 views
/*1* Copyright 2019 The Emscripten Authors. All rights reserved.2* Emscripten is available under two separate licenses, the MIT license and the3* University of Illinois/NCSA Open Source License. Both these licenses can be4* found in the LICENSE file.5*/67#include <alloca.h>8#include <wasi/api.h>9#include <sysexits.h>1011// Much of this code comes from:12// https://github.com/CraneStation/wasi-libc/blob/master/libc-bottom-half/crt/crt1.c13// Converted malloc() calls to alloca() to avoid including malloc in all programs.1415int __main_argc_argv(int argc, char *argv[]);1617// If the application's `main` does not uses argc/argv, then it will be defined18// (by llvm) with an __main_void alias, and therefore this function will not19// be included, and `_start` will call the application's `__main_void` directly.20//21// If the application's `main` does use argc/argv, then _start will call this22// function which loads argv values and sends them to the23// application's `main` which gets mangled to `__main_argc_argv` by llvm.24__attribute__((weak))25int __main_void(void) {26/* Fill in the arguments from WASI syscalls. */27size_t argc;28char **argv = NULL;29size_t argv_buf_size;30__wasi_errno_t err;3132/* Get the sizes of the arrays we'll have to create to copy in the args. */33err = __wasi_args_sizes_get(&argc, &argv_buf_size);34if (err != __WASI_ERRNO_SUCCESS) {35__wasi_proc_exit(EX_OSERR);36}3738if (argc) {39/* Allocate memory for the array of pointers, adding null terminator. */40argv = alloca(sizeof(char *) * (argc + 1));41/* Allocate memory for storing the argument chars. */42uint8_t *argv_buf = alloca(sizeof(char) * argv_buf_size);43/* Make sure the last pointer in the array is NULL. */44argv[argc] = NULL;45/* Fill the argument chars, and the argv array with pointers into those chars. */46err = __wasi_args_get((uint8_t**)argv, argv_buf);47if (err != __WASI_ERRNO_SUCCESS) {48__wasi_proc_exit(EX_OSERR);49}50}5152return __main_argc_argv(argc, argv);53}545556