]> git.proxmox.com Git - wasi-libc.git/blame - libc-bottom-half/sources/environ.c
Use MUSL's `weak*` feature in bottom half (#306)
[wasi-libc.git] / libc-bottom-half / sources / environ.c
CommitLineData
9efc2f42
DG
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.
11char **__wasilibc_environ;
87c2aa04
AB
12weak_alias(__wasilibc_environ, _environ);
13weak_alias(__wasilibc_environ, environ);
9efc2f42
DG
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)))
22static void __wasilibc_initialize_environ_eagerly(void) {
23 __wasilibc_initialize_environ();
24}
82fc2c4f
DG
25
26// See the comments in libc-environ.h.
27void __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}