| 1 | #include <unistd.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <sysexits.h> |
| 4 | #include <wasi/api.h> |
| 5 | #include <wasi/libc.h> |
| 6 | #include <wasi/libc-environ.h> |
| 7 | |
| 8 | // If the program does use `environ`, it'll get this version of |
| 9 | // `__wasilibc_environ`, which is initialized with a constructor function, so |
| 10 | // that it's initialized whenever user code might want to access it. |
| 11 | char **__wasilibc_environ; |
| 12 | weak_alias(__wasilibc_environ, _environ); |
| 13 | weak_alias(__wasilibc_environ, environ); |
| 14 | |
| 15 | // We define this function here in the same source file as |
| 16 | // `__wasilibc_environ`, so that this function is called in iff environment |
| 17 | // variable support is used. |
| 18 | // Concerning the 50 -- levels up to 100 are reserved for the implementation, |
| 19 | // so we an arbitrary number in the middle of the range to allow other |
| 20 | // reserved things to go before or after. |
| 21 | __attribute__((constructor(50))) |
| 22 | static void __wasilibc_initialize_environ_eagerly(void) { |
| 23 | __wasilibc_initialize_environ(); |
| 24 | } |
| 25 | |
| 26 | // See the comments in libc-environ.h. |
| 27 | void __wasilibc_maybe_reinitialize_environ_eagerly(void) { |
| 28 | // This translation unit is linked in if `environ` is used, meaning we need |
| 29 | // to eagerly reinitialize the environment variables. |
| 30 | __wasilibc_initialize_environ(); |
| 31 | } |