[Bug libgcj/38812] gcj-built executables don't run after strip (libgcj erroneously references _environ)

fxcoudert at gcc dot gnu dot org gcc-bugzilla@gcc.gnu.org
Mon May 17 12:54:00 GMT 2010



------- Comment #5 from fxcoudert at gcc dot gnu dot org  2010-05-17 12:53 -------
On Mac OS X (all versions), the correct fix is to use _NSGetEnviron() instead
of extern variable environ, and to include <crt_externs.h>. Example of use:

#include <stdlib.h>
#include <string.h>
#include <crt_externs.h>

int main (void)
{
  // Instead of: extern char **environ;
#define environ (*_NSGetEnviron())

  char **env = malloc (3 * sizeof(char *));
  env[0] = strdup ("VAR1=this is variable one");
  env[1] = strdup ("PATH=/dev/null");
  env[2] = NULL;
  environ = env;

  system ("/usr/bin/env");
  return 0;
}


(see http://www.gnu.org/software/gnulib/manual/html_node/environ.html)



So, a possible patch (protecting target-specific code with #idef's; I don't
know if that's the style libjava maintainers would like best) is:

Index: java/lang/natPosixProcess.cc
===================================================================
--- java/lang/natPosixProcess.cc        (revision 159481)
+++ java/lang/natPosixProcess.cc        (working copy)
@@ -54,7 +54,12 @@
 using gnu::java::nio::channels::FileChannelImpl;
 using namespace java::lang;

+#ifdef __APPLE__
+#include <crt_externs.h>
+#define environ (*_NSGetEnviron())
+#else
 extern char **environ;
+#endif

 static char *
 new_string (jstring string)


-- 

fxcoudert at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fxcoudert at gcc dot gnu dot
                   |                            |org, tromey at redhat dot
                   |                            |com, aph at redhat dot com
   Last reconfirmed|2009-01-13 01:03:10         |2010-05-17 12:53:59
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38812



More information about the Java-prs mailing list