|
2 | 2 | #include <sysexits.h> |
3 | 3 | #include <wasi/core.h> |
4 | 4 | #include <wasi/libc.h> |
| 5 | +#include <wasi/libc-internal.h> |
5 | 6 |
|
6 | | -extern char **__environ; |
| 7 | +__wasi_errno_t __wasilibc_populate_environ(void) __attribute__((weak)); |
7 | 8 | extern void __wasm_call_ctors(void); |
8 | 9 | extern int main(int, char *[]); |
9 | 10 | extern void __prepare_for_exit(void); |
@@ -37,32 +38,6 @@ static __wasi_errno_t populate_args(size_t *argc, char ***argv) { |
37 | 38 | return __wasi_args_get(*argv, argv_buf); |
38 | 39 | } |
39 | 40 |
|
40 | | -static __wasi_errno_t populate_environ(void) { |
41 | | - __wasi_errno_t err; |
42 | | - |
43 | | - /* Get the sizes of the arrays we'll have to create to copy in the environment. */ |
44 | | - size_t environ_count; |
45 | | - size_t environ_buf_size; |
46 | | - err = __wasi_environ_sizes_get(&environ_count, &environ_buf_size); |
47 | | - if (err != __WASI_ESUCCESS) { |
48 | | - return err; |
49 | | - } |
50 | | - |
51 | | - /* Allocate memory for the array of pointers, adding null terminator. */ |
52 | | - __environ = malloc(sizeof(char *) * (environ_count + 1)); |
53 | | - /* Allocate memory for storing the environment chars. */ |
54 | | - char *environ_buf = malloc(sizeof(char) * environ_buf_size); |
55 | | - if (__environ == NULL || environ_buf == NULL) { |
56 | | - return __WASI_ENOMEM; |
57 | | - } |
58 | | - |
59 | | - /* Make sure the last pointer in the array is NULL. */ |
60 | | - __environ[environ_count] = NULL; |
61 | | - |
62 | | - /* Fill the environment chars, and the __environ array with pointers into those chars. */ |
63 | | - return __wasi_environ_get(__environ, environ_buf); |
64 | | -} |
65 | | - |
66 | 41 | static __wasi_errno_t populate_libpreopen(void) { |
67 | 42 | __wasilibc_init_preopen(); |
68 | 43 |
|
@@ -110,9 +85,11 @@ void _start(void) { |
110 | 85 | _Exit(EX_OSERR); |
111 | 86 | } |
112 | 87 |
|
113 | | - /* Fill in the environment from WASI syscalls. */ |
114 | | - if (populate_environ() != __WASI_ESUCCESS) { |
115 | | - _Exit(EX_OSERR); |
| 88 | + /* Fill in the environment from WASI syscalls, if needed. */ |
| 89 | + if (&__wasilibc_populate_environ != NULL) { |
| 90 | + if (__wasilibc_populate_environ() != __WASI_ESUCCESS) { |
| 91 | + _Exit(EX_OSERR); |
| 92 | + } |
116 | 93 | } |
117 | 94 |
|
118 | 95 | /* Fill in the arguments from WASI syscalls. */ |
|
0 commit comments