|
1 | | -#include <wasi/api.h> |
2 | | -#include <wasi/libc.h> |
3 | | -#include <stdlib.h> |
4 | | -#include <sysexits.h> |
| 1 | +// Old compilers define `__original_main`. If that doesn't exist, we |
| 2 | +// get called here. New compilers define `__main_void`. If that doesn't |
| 3 | +// exist, we'll try something else. |
| 4 | +// TODO: Remove this layer when we no longer have to support old compilers. |
| 5 | +int __main_void(void); |
5 | 6 |
|
6 | | -// The user's `main` function, expecting arguments. |
7 | | -int main(int argc, char *argv[]); |
8 | | - |
9 | | -// If the user's `main` function expects arguments, the compiler won't emit |
10 | | -// an `__original_main` function so this version will get linked in, which |
11 | | -// initializes the argument data and calls `main`. |
12 | 7 | __attribute__((weak)) |
13 | 8 | int __original_main(void) { |
14 | | - __wasi_errno_t err; |
15 | | - |
16 | | - // Get the sizes of the arrays we'll have to create to copy in the args. |
17 | | - size_t argv_buf_size; |
18 | | - size_t argc; |
19 | | - err = __wasi_args_sizes_get(&argc, &argv_buf_size); |
20 | | - if (err != __WASI_ERRNO_SUCCESS) { |
21 | | - _Exit(EX_OSERR); |
22 | | - } |
23 | | - |
24 | | - // Add 1 for the NULL pointer to mark the end, and check for overflow. |
25 | | - size_t num_ptrs = argc + 1; |
26 | | - if (num_ptrs == 0) { |
27 | | - _Exit(EX_SOFTWARE); |
28 | | - } |
29 | | - |
30 | | - // Allocate memory for storing the argument chars. |
31 | | - char *argv_buf = malloc(argv_buf_size); |
32 | | - if (argv_buf == NULL) { |
33 | | - _Exit(EX_SOFTWARE); |
34 | | - } |
35 | | - |
36 | | - // Allocate memory for the array of pointers. This uses `calloc` both to |
37 | | - // handle overflow and to initialize the NULL pointer at the end. |
38 | | - char **argv = calloc(num_ptrs, sizeof(char *)); |
39 | | - if (argv == NULL) { |
40 | | - free(argv_buf); |
41 | | - _Exit(EX_SOFTWARE); |
42 | | - } |
43 | | - |
44 | | - // Fill the argument chars, and the argv array with pointers into those chars. |
45 | | - // TODO: Remove the casts on `argv_ptrs` and `argv_buf` once the witx is updated with char8 support. |
46 | | - err = __wasi_args_get((uint8_t **)argv, (uint8_t *)argv_buf); |
47 | | - if (err != __WASI_ERRNO_SUCCESS) { |
48 | | - free(argv_buf); |
49 | | - free(argv); |
50 | | - _Exit(EX_OSERR); |
51 | | - } |
52 | | - |
53 | | - // Call main with the arguments! |
54 | | - return main(argc, argv); |
| 9 | + return __main_void(); |
55 | 10 | } |
0 commit comments