summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--byterun/unix.c62
-rwxr-xr-xconfigure7
2 files changed, 68 insertions, 1 deletions
diff --git a/byterun/unix.c b/byterun/unix.c
index f84e20357..65c960a3b 100644
--- a/byterun/unix.c
+++ b/byterun/unix.c
@@ -23,8 +23,12 @@
#include <fcntl.h>
#include "config.h"
#ifdef SUPPORT_DYNAMIC_LINKING
+#ifdef HAS_NSLINKMODULE
+#include <mach-o/dyld.h>
+#else
#include <dlfcn.h>
#endif
+#endif
#ifdef HAS_UNISTD
#include <unistd.h>
#endif
@@ -156,6 +160,63 @@ char * search_dll_in_path(struct ext_table * path, char * name)
}
#ifdef SUPPORT_DYNAMIC_LINKING
+#ifdef HAS_NSLINKMODULE
+/* Use MacOSX bundles */
+
+static char *dlerror_string = "No error";
+
+void * caml_dlopen(char * libname)
+{
+ NSObjectFileImage image;
+ NSObjectFileImageReturnCode retCode =
+ NSCreateObjectFileImageFromFile(libname, &image);
+ switch (retCode) {
+ case NSObjectFileImageSuccess:
+ dlerror_string = NULL;
+ return (void*)NSLinkModule(image, libname, NSLINKMODULE_OPTION_BINDNOW
+ | NSLINKMODULE_OPTION_RETURN_ON_ERROR);
+ case NSObjectFileImageAccess:
+ dlerror_string = "cannot access this bundle"; break;
+ case NSObjectFileImageArch:
+ dlerror_string = "this bundle has wrong CPU architecture"; break;
+ case NSObjectFileImageFormat:
+ case NSObjectFileImageInappropriateFile:
+ dlerror_string = "this file is not a proper bundle"; break;
+ default:
+ dlerror_string = "could not read object file"; break;
+ }
+ return NULL;
+}
+
+void caml_dlclose(void * handle)
+{
+ dlerror_string = NULL;
+ NSUnLinkModule((NSModule)handle, NSUNLINKMODULE_OPTION_NONE);
+}
+
+void * caml_dlsym(void * handle, char * name)
+{
+ NSSymbol sym;
+ char _name[1000] = "_";
+ strncat (_name, name, 998);
+ dlerror_string = NULL;
+ sym = NSLookupSymbolInModule((NSModule)handle, _name);
+ if (sym != NULL) return NSAddressOfSymbol(sym);
+ else return NULL;
+}
+
+char * caml_dlerror(void)
+{
+ NSLinkEditErrors c;
+ int errnum;
+ const char *fileName, *errorString;
+ if (dlerror_string != NULL) return dlerror_string;
+ NSLinkEditError(&c,&errnum,&fileName,&errorString);
+ return errorString;
+}
+
+#else
+/* Use normal dlopen */
#ifndef RTLD_GLOBAL
#define RTLD_GLOBAL 0
@@ -189,6 +250,7 @@ char * caml_dlerror(void)
return dlerror();
}
+#endif
#else
void * caml_dlopen(char * libname)
diff --git a/configure b/configure
index d4b9244fb..e565a8761 100755
--- a/configure
+++ b/configure
@@ -942,8 +942,13 @@ if sh ./hasgot -i locale.h && sh ./hasgot setlocale; then
echo "#define HAS_LOCALE" >> s.h
fi
-if sh ./hasgot $dllib -ldl dlopen; then
+if sh ./hasgot -i mach-o/dyld.h && sh ./hasgot NSLinkModule; then
+ echo "NSLinkModule() found. Using darwin dynamic loading."
+ echo "#define HAS_NSLINKMODULE" >> s.h
+elif sh ./hasgot $dllib dlopen; then
echo "dlopen() found."
+elif sh ./hasgot $dllib -ldl dlopen; then
+ echo "dlopen() found in -ldl."
dllib="$dllib -ldl"
fi