New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing '_environ' symbol used in libunix.a #5693
Comments
Comment author: @xavierleroy Which version of XCode are you using? As I said on caml-list, the "environ" variable is standard according to POSIX and Single Unix Specification, so something went terribly wrong here. Damien, Xavier C.: can you reproduce on your Macs? |
Comment author: @xavierleroy More info posted by reporter on caml-list: My setting is: Mac OS X 10.7.4 I don't develop in Xcode, but use makefiles instead. Homebrew was used to install ocaml. The error only occurs when linking to a dynamic lib, not when I link against an executable (main loop in c). |
Comment author: thelema Per Benedikt Meurer on caml-list: Of course... as stated in environ(7): "Shared libraries and bundles don't have direct access to environ, which It has always been this way on OS X, so Keyan's patch is correct. |
Comment author: @damiendoligez That command line looks suspect to me:
In any case, I cannot reproduce the bug as reported. When I do "ocamlopt -o foo.so unix.cmxa -ccopt -shared", I get errors, not only on _environ, but also on _caml_atom_table, _caml_code_area_start, and _caml_code_area_end. I have uploaded an example that's almost working, but when I run a.out I get this error: Xavier, can you tell us what's I'm missing? |
Comment author: @damiendoligez OK, what I was missing is the surprising semantics of COMMON data in C object files and its variations across C linkers... We'll probably have a better fix in a future version of OCaml, but for now you need to reference a function from "startup.c", to prevent the Mac OS X linker from dropping the file when building your shared library. See the new version of my example code (I've also fixed bar.c to avoid a segfault). |
Comment author: @damiendoligez Note for the future: the best solution we found so far is to add an initializer to every global variable of the runtime system, to force them into the data segment. |
Comment author: @damiendoligez I have uploaded a more complete example that demonstrates passing arguments, returning values, and catching exceptions (dynlink-example3). |
As of now, Do we still have a macOS problem here and should we do something about it? @damiendoligez |
I don't think the problem is limited to IIRC, the problem is that an uninitialized global variable in C code is declared as COMMON in the object file, and the macos linker (unlike the linux linker) feels free to drop the object file when it is referenced only by this variable. So our best bet so far is to have initializers for all of the runtime's global variables to avoid this COMMON nonsense. Given the rarity of reports on this problem, this is not a priority. |
FWIW, I've never had a problem building on MacOS. |
This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc. |
This issue has not be observed in a long time. It's not by lack of OCaml users who run macOS! So, I'm takign the liberty to close. |
Original bug ID: 5693
Reporter: thelema
Assigned to: @damiendoligez
Status: assigned (set by @damiendoligez on 2017-03-15T10:23:50Z)
Resolution: open
Priority: low
Severity: minor
Platform: x86_64
OS: OS X
OS Version: 10.7.4
Version: 3.12.1
Category: otherlibs
Bug description
When building a mixed OCaml/C library that includes unix.cmxa, the osx linker gives an error about an undefined symbol "_environ".
IRC Log: http://tunes.org/~nef/logs/ocaml/12.07.20 - starts at 07:02:49
Bug reporter cannot reproduce.
Steps to reproduce
When compiling a library as follows:
ocamlopt -o libocaml_rl_pi_rnn.so -I /usr/local/lib/ocaml/
-I /usr/local/lib/ocaml/site-lib/extlib -I
/usr/local/lib/ocaml/site-lib/batteries unix.cmxa nums.cmxa
batteries.cmxa rnnRL.cmxa ocaml_rl_pi_bi.ml -ccopt
ocaml_interface.c -ccopt -I/usr/local/lib/ocaml/ -ccopt
-shared
The following error happens.
Undefined symbols for architecture x86_64:
"_environ", referenced from:
_unix_execvpe in libunix.a(execvp.o)
_unix_environment in libunix.a(envir.o)
Additional information
10:17 < thelema> http://trac.macports.org/browser/trunk/dports/lang/hugs98/files/patch-packages-base-include-HsBase.h.diff?rev=81676
...
10:36 < [Joshuah]> its a bug in the unix code. i added the lines posted in the
link you gave me to unix.c and another .c file in
otherlibs/unix
10:36 < [Joshuah]> it now compiles and runs
The patch is:
-extern char** environ;
+#ifdef APPLE
+#include <crt_externs.h>
+#define environ (*_NSGetEnviron())
+#else
+#endif
File attachments
The text was updated successfully, but these errors were encountered: