Skip to content

Unknown symbol `_environ' on MacOS X

Shared libraries and bundles on MacOS X Leopard don't have direct access to environ (i.e. extern char **environ), which is only available to the loader ld when a complete program is being linked. If direct access to environ is needed, the _NSGetEnviron() routine, defined in <crt_externs.h> can be used to retrieve the address of environ at runtime (man environ(7) see last paragraph in section PROGRAMMING).

Two Libraries are affected by this - namely base and unix. Both of these Libraries implement (duplicate code) access to environ:

* ToDo: write a feature test that doesn't assume 'environ' to
*    be in scope at link-time. */
extern char** environ;
INLINE char **__hscore_environ() { return environ; }
extern char **environ;

Unfortunately this does not work on MaxOS X Leopard with XCode 3.0. My temporary workaround was to comment out the current definition of environ in the header files and add the following instead:

/* ToDo: write a feature test that doesn't assume 'environ' to
 *    be in scope at link-time. */
//extern char **environ;
//INLINE char **__hscore_environ() { return environ; }
#include <crt_externs.h>
INLINE char **__hscore_environ(void) { return (*_NSGetEnviron()); }
//extern char **environ;
#include <crt_externs.h>
INLINE char **__hsunix_environ (void) { return (*_NSGetEnviron()); }
--getEnvironmentPrim :: IO [String]
--getEnvironmentPrim = do
--  c_environ <- peek c_environ_p
--  arr <- peekArray0 nullPtr c_environ
--  mapM peekCString arr
--
--foreign import ccall unsafe "&environ"
--    c_environ_p :: Ptr (Ptr CString)
getEnvironmentPrim :: IO [String]
getEnvironmentPrim = do
  c_environ <- c_environ_p
  arr <- peekArray0 nullPtr c_environ
  mapM peekCString arr

foreign import ccall unsafe "__hsunix_environ"
  c_environ_p :: IO (Ptr CString)

Unfortunately I do not know GHC well enough to propose a clean patch. With these 'hacks' ghc compiled from source on MacOS X Leopard with XCode 3.0 works well. It would also probably be good to get rid of the code duplication in the base and unix libraries.

Shall I open another Ticket for libraries/unix?

References:

Trac metadata
Trac field Value
Version 6.9
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/base
Test case
Differential revisions
BlockedBy
Related
Blocking
CC igor@bytelabs.org
Operating system
Architecture
Edited by mafo
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information