From 71409b5604f45f5afadb03be82d257bb112965cb Mon Sep 17 00:00:00 2001 From: Jacques Garrigue Date: Thu, 9 Jan 2003 08:42:13 +0000 Subject: support vanilla MacOSX dynamic loading git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@5378 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02 --- byterun/unix.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'byterun/unix.c') 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 #include "config.h" #ifdef SUPPORT_DYNAMIC_LINKING +#ifdef HAS_NSLINKMODULE +#include +#else #include #endif +#endif #ifdef HAS_UNISTD #include #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) -- cgit v1.2.3-70-g09d2