diff --git a/library/std/src/sys/env/unix.rs b/library/std/src/sys/env/unix.rs index 7a19f97b3ad50..744a4c526e0b0 100644 --- a/library/std/src/sys/env/unix.rs +++ b/library/std/src/sys/env/unix.rs @@ -49,7 +49,19 @@ pub unsafe fn environ() -> *mut *const *const c_char { static ENVIRON: LazyLock = LazyLock::new(|| { Environ(unsafe { - libc::dlsym(libc::RTLD_DEFAULT, c"environ".as_ptr()) as *mut *const *const c_char + let dynamic = + libc::dlsym(libc::RTLD_DEFAULT, c"environ".as_ptr()) as *mut *const *const c_char; + if !dynamic.is_null() { + dynamic + } else { + // Reading an `extern_weak` static yields the symbol's address + // (null if unresolved), not its contents. + unsafe extern "C" { + #[linkage = "extern_weak"] + static environ: *mut *const *const c_char; + } + environ + } }) }); ENVIRON.0