Skip to content

Commit

Permalink
Don't foreign import environ
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
bgamari authored and hasufell committed Sep 29, 2023
1 parent 25b0f11 commit d63e6b4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 6 additions & 3 deletions System/Posix/Env/Internal.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions cbits/HsUnix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d63e6b4

Please sign in to comment.