From d63e6b41fcc05059bd51f559229712e4a3998d15 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Tue, 26 Sep 2023 13:04:13 -0400 Subject: [PATCH] Don't `foreign import` `environ` `musl` defines the `environ` symbol as a weak alias, which on AArch64 requires particular treatment by the compiler. As GHC doesn't have access to the prototype of the symbol, it doesn't know that this treatment is necessary and consequently we emit assembly that the linker will choke on. See GHC #24011. --- System/Posix/Env/Internal.hsc | 9 ++++++--- cbits/HsUnix.c | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/System/Posix/Env/Internal.hsc b/System/Posix/Env/Internal.hsc index d8732a6a..1932b857 100644 --- a/System/Posix/Env/Internal.hsc +++ b/System/Posix/Env/Internal.hsc @@ -22,8 +22,11 @@ getCEnviron = nsGetEnviron >>= peek foreign import ccall unsafe "_NSGetEnviron" nsGetEnviron :: IO (Ptr (Ptr CString)) #else -getCEnviron = peek c_environ_p +getCEnviron = _getCEnviron -foreign import ccall unsafe "&environ" - c_environ_p :: Ptr (Ptr CString) +-- N.B. we cannot import `environ` directly in Haskell as it may be a weak symbol +-- which requires special treatment by the compiler, which GHC is not equipped to +-- provide. See GHC #24011. +foreign import ccall unsafe "__hsunix_get_environ" + _getCEnviron :: IO (Ptr CString) #endif diff --git a/cbits/HsUnix.c b/cbits/HsUnix.c index f2908d06..60d259f3 100644 --- a/cbits/HsUnix.c +++ b/cbits/HsUnix.c @@ -8,6 +8,8 @@ #include "HsUnix.h" +char **__hsunix_get_environ (void) {return environ;} + #ifdef HAVE_RTLDNEXT void *__hsunix_rtldNext (void) {return RTLD_NEXT;} #endif