]> git.proxmox.com Git - wasi-libc.git/commitdiff
Also add a way to read `environ` without triggering eager init.
authorDan Gohman <dev@sunfishcode.online>
Thu, 18 Mar 2021 16:48:01 +0000 (09:48 -0700)
committerDan Gohman <dev@sunfishcode.online>
Tue, 25 May 2021 22:25:05 +0000 (15:25 -0700)
Add a `__wasilibc_get_environ` function which returns the value of
`environ` but without performing eager init.

expected/wasm32-wasi/defined-symbols.txt
libc-bottom-half/headers/public/wasi/libc-environ.h
libc-bottom-half/sources/__wasilibc_environ.c [new file with mode: 0644]

index c97b3268b708ab1aafea684c88cfcbe835e669a0..b8d2ba813746fe50a0a089c2b72ce2bb14feb123 100644 (file)
@@ -313,6 +313,7 @@ __wasilibc_fd_renumber
 __wasilibc_find_abspath
 __wasilibc_find_relpath
 __wasilibc_find_relpath_alloc
+__wasilibc_get_environ
 __wasilibc_initialize_environ
 __wasilibc_link
 __wasilibc_link_newat
index ffc962e7b5daadafa62c4af3b31d71d29a31b3ad..f84ba8e606492fc679d8bb8d51b917007889aa3f 100644 (file)
@@ -24,6 +24,11 @@ void __wasilibc_deinitialize_environ(void);
 /// referenced in the program.
 void __wasilibc_maybe_reinitialize_environ_eagerly(void);
 
+/// Return the value of the `environ` variable. Using `environ` directly
+/// requires eager initialization of the environment variables. Using this
+/// function instead of `environ` allows initialization to happen lazily.
+char **__wasilibc_get_environ(void);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libc-bottom-half/sources/__wasilibc_environ.c b/libc-bottom-half/sources/__wasilibc_environ.c
new file mode 100644 (file)
index 0000000..53d0a55
--- /dev/null
@@ -0,0 +1,14 @@
+#include <wasi/libc-environ.h>
+
+extern char **__wasilibc_environ;
+
+// See the comments in libc-environ.h.
+char **__wasilibc_get_environ(void) {
+    // Perform lazy initialization if needed.
+    __wasilibc_ensure_environ();
+
+    // Return `environ`. Use the `__wasilibc_`-prefixed name so that we don't
+    // pull in the `environ` symbol directly, which would lead to eager
+    // initialization being done instead.
+    return __wasilibc_environ;
+}