|
| 1 | +#include <stdlib.h> |
| 2 | +#include <sysexits.h> |
| 3 | +#include <wasi/core.h> |
| 4 | + |
| 5 | +extern char **environ; |
1 | 6 | extern void __wasm_call_ctors(void); |
2 | 7 | extern int main(int, char *[]); |
3 | 8 | extern void __prepare_for_exit(void); |
4 | 9 | void _Exit(int) __attribute__((noreturn)); |
5 | 10 |
|
| 11 | +static __wasi_errno_t populate_args(size_t *argc, char **argv) { |
| 12 | + __wasi_errno_t err; |
| 13 | + |
| 14 | + /* Get the sizes of the arrays we'll have to create to copy in the args. */ |
| 15 | + size_t argv_size; |
| 16 | + err = __wasi_arg_sizes_get(argc, &argv_size); |
| 17 | + if (err != __WASI_ESUCCESS) { |
| 18 | + return err; |
| 19 | + } |
| 20 | + |
| 21 | + /* Allocate memory for the array of pointers. */ |
| 22 | + argv = malloc(sizeof(char *) * *argc); |
| 23 | + /* Allocate memory for storing the argument chars. */ |
| 24 | + char *argv_buf = malloc(sizeof(char) * argv_size); |
| 25 | + if (argv == NULL || argv_buf == NULL) { |
| 26 | + return __WASI_ENOMEM; |
| 27 | + } |
| 28 | + |
| 29 | + /* Fill the argument chars, and the argv array with pointers into those chars. */ |
| 30 | + return __wasi_argv_get(argv, argv_buf); |
| 31 | +} |
| 32 | + |
| 33 | +static __wasi_errno_t populate_environ() { |
| 34 | + __wasi_errno_t err; |
| 35 | + |
| 36 | + /* Get the sizes of the arrays we'll have to create to copy in the environment. */ |
| 37 | + size_t environ_count; |
| 38 | + size_t environ_size; |
| 39 | + err = __wasi_environ_sizes_get(&environ_count, &environ_size); |
| 40 | + if (err != __WASI_ESUCCESS) { |
| 41 | + return err; |
| 42 | + } |
| 43 | + |
| 44 | + /* Allocate memory for the array of pointers. */ |
| 45 | + environ = malloc(sizeof(char *) * environ_count); |
| 46 | + /* Allocate memory for storing the environment chars. */ |
| 47 | + char *environ_buf = malloc(sizeof(char) * environ_size); |
| 48 | + if (environ == NULL || environ_buf == NULL) { |
| 49 | + return __WASI_ENOMEM; |
| 50 | + } |
| 51 | + |
| 52 | + /* Fill the environment chars, and the __environ array with pointers into those chars. */ |
| 53 | + return __wasi_environ_get(environ, environ_buf); |
| 54 | +} |
| 55 | + |
6 | 56 | void _start(void) { |
7 | 57 | /* The linker synthesizes this to call constructors. */ |
8 | 58 | __wasm_call_ctors(); |
9 | 59 |
|
10 | | - /* For now, just call main with no arguments. */ |
11 | | - int r = main(0, 0); |
| 60 | + /* Fill in the environment from WASI syscalls. */ |
| 61 | + if (populate_environ() != __WASI_ESUCCESS) { |
| 62 | + _Exit(EX_OSERR); |
| 63 | + } |
| 64 | + |
| 65 | + /* Fill in the arguments from WASI syscalls. */ |
| 66 | + size_t argc; |
| 67 | + char **argv; |
| 68 | + if (populate_args(&argc, argv) != __WASI_ESUCCESS) { |
| 69 | + _Exit(EX_OSERR); |
| 70 | + } |
| 71 | + |
| 72 | + /* Call main with the arguments. */ |
| 73 | + int r = main(argc, argv); |
12 | 74 |
|
13 | 75 | /* Call atexit functions, destructors, stdio cleanup, etc. */ |
14 | 76 | __prepare_for_exit(); |
|
0 commit comments