[llvm] [flang][runtime] Use dlsym to access char** environ on FreeBSD (PR #158477)

Gleb Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 14 05:24:38 PDT 2025


https://github.com/arrowd created https://github.com/llvm/llvm-project/pull/158477

None

>From 9832e87581eff8552c51cc5306a15be68251d2b5 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 | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/flang-rt/lib/runtime/environment.cpp b/flang-rt/lib/runtime/environment.cpp
index 97ac56236e799..f74fabce877d6 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,8 @@ void ExecutionEnvironment::Configure(int ac, const char *av[],
 
 #ifdef _WIN32
   envp = _environ;
+#elif defined(__FreeBSD__)
+  envp = *reinterpret_cast<char***>(dlsym(RTLD_DEFAULT, "environ"));
 #else
   envp = environ;
 #endif



More information about the llvm-commits mailing list