[llvm] [flang][runtime] Use dlsym to access char** environ on FreeBSD (PR #158477)
Gleb Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 29 08:18:24 PDT 2025
https://github.com/arrowd updated https://github.com/llvm/llvm-project/pull/158477
>From f17c23baf0b8c11e84a3530ec92848a103b4a8cc Mon Sep 17 00:00:00 2001
From: Gleb Popov <6yearold at gmail.com>
Date: Sun, 14 Sep 2025 15:20:35 +0300
Subject: [PATCH] [flang][runtime] Use dlsym to access char** environ on
FreeBSD
---
flang-rt/lib/runtime/environment.cpp | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/flang-rt/lib/runtime/environment.cpp b/flang-rt/lib/runtime/environment.cpp
index 97ac56236e799..22af817c9767a 100644
--- a/flang-rt/lib/runtime/environment.cpp
+++ b/flang-rt/lib/runtime/environment.cpp
@@ -17,6 +17,10 @@
#ifdef _WIN32
extern char **_environ;
+#elif defined(__FreeBSD__)
+// FreeBSD has environ in crt rather than libc. Using "extern char** environ"
+// in the code of a shared library makes it fail to link with -Wl,--no-undefined
+// See https://reviews.freebsd.org/D30842#840642
#else
extern char **environ;
#endif
@@ -104,6 +108,10 @@ void ExecutionEnvironment::Configure(int ac, const char *av[],
#ifdef _WIN32
envp = _environ;
+#elif defined(__FreeBSD__)
+ auto envpp = reinterpret_cast<char ***>(dlsym(RTLD_DEFAULT, "environ"));
+ if (envpp)
+ envp = *envpp;
#else
envp = environ;
#endif
More information about the llvm-commits
mailing list