/* ggDarwinDLOpen implements a "dlopen" wrapper */ gg_dlhand ggDarwinDLOpen(const char *filename, int flags) { gg_dlhand ret = NULL; struct gg_dlhand_darwin_t *darwin_ret = NULL; NSLinkEditErrorHandlers error_handlers; const char *pathname, *modulename; darwin_ret = (struct gg_dlhand_darwin_t *)malloc(sizeof(struct gg_dlhand_darwin_t)); if (darwin_ret == NULL) return ret; ret = (void *)darwin_ret; if (flags & GG_MODULE_GLOBAL) { darwin_ret->nsmodule_flags = NSLINKMODULE_OPTION_NONE; } else { darwin_ret->nsmodule_flags = NSLINKMODULE_OPTION_PRIVATE; } /* if */ splitstring(filename, &pathname, &modulename); dlerror_code = NSCreateObjectFileImageFromFile(pathname, &darwin_ret->objectFileImage); ggstrlcpy(dlerror_filename, filename, sizeof(dlerror_filename)); switch (dlerror_code) { case NSObjectFileImageSuccess: break; case NSObjectFileImageFailure: case NSObjectFileImageInappropriateFile: case NSObjectFileImageArch: case NSObjectFileImageFormat: case NSObjectFileImageAccess: goto err0; } /* switch */ /* Install our own error handlers */ error_handlers.undefined = ggDarwinErrorUndefined; error_handlers.multiple = ggDarwinErrorMultiple; error_handlers.linkEdit = ggDarwinErrorLinkEdit; #if 0 /* Let the default handlers do their work * as long as our own error handlers are stubs. */ NSInstallLinkEditErrorHandlers(&error_handlers); #endif /* try to load the module now */ darwin_ret->nsmodule = NSLinkModule(darwin_ret->objectFileImage, modulename, darwin_ret->nsmodule_flags); /* Either we return successful here or the error handlers * already aborted/exited the app before. */ return ret; err0: free(ret); return NULL; } /* ggDarwinDLOpen */
static void *dlopen(const char *path, int mode) { void *handle= 0; NSObjectFileImage ofi= 0; if (!dlInitialised) { dlinit(); dlInitialised= 1; } if (!path) return DL_APP_CONTEXT; switch (NSCreateObjectFileImageFromFile(path, &ofi)) { case NSObjectFileImageSuccess: handle= NSLinkModule(ofi, path, NSLINKMODULE_OPTION_RETURN_ON_ERROR); NSDestroyObjectFileImage(ofi); break; case NSObjectFileImageInappropriateFile: handle= (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR); break; default: handle= 0; break; } if (!handle) dlSetError("could not load shared object: %s", path); dprintf((stderr, "dlopen: %s => %d\n", path, (int)handle)); return handle; }
void * loadSharedObject (const char *path) { NSObjectFileImage image; switch (NSCreateObjectFileImageFromFile(path, &image)) { case NSObjectFileImageSuccess: { NSModule module = NSLinkModule(image, path, NSLINKMODULE_OPTION_RETURN_ON_ERROR); if (module) return module; logDyldError("link module"); logMessage(LOG_ERR, "shared object not linked: %s", path); break; } case NSObjectFileImageInappropriateFile: logMessage(LOG_ERR, "inappropriate object type: %s", path); break; case NSObjectFileImageArch: logMessage(LOG_ERR, "incorrect object architecture: %s", path); break; case NSObjectFileImageFormat: logMessage(LOG_ERR, "invalid object format: %s", path); break; case NSObjectFileImageAccess: logMessage(LOG_ERR, "inaccessible object: %s", path); break; case NSObjectFileImageFailure: default: logMessage(LOG_ERR, "shared object not loaded: %s", path); break; } return NULL; }
void *dlopen(char *filename, int unused) { NSObjectFileImage myImage; NSModule myModule; if (firstLoad) { /* If we are loading our first symbol (huzzah!) we should go ahead * and install link editor error handling! */ NSLinkEditErrorHandlers linkEditorErrorHandlers; linkEditorErrorHandlers.undefined = undefinedErrorHandler; linkEditorErrorHandlers.multiple = multipleErrorHandler; linkEditorErrorHandlers.linkEdit = linkEditErrorHandler; NSInstallLinkEditErrorHandlers(&linkEditorErrorHandlers); firstLoad = FALSE; } myDlError = NSCreateObjectFileImageFromFile(filename, &myImage); if (myDlError != NSObjectFileImageSuccess) return(NULL); myModule = NSLinkModule(myImage, filename, NSLINKMODULE_OPTION_PRIVATE); return((void *)myModule); }
static NSModule pr_LoadMachDyldModule(const char *name) { NSObjectFileImage ofi; NSModule h = NULL; if (NSCreateObjectFileImageFromFile(name, &ofi) == NSObjectFileImageSuccess) { h = NSLinkModule(ofi, name, NSLINKMODULE_OPTION_PRIVATE | NSLINKMODULE_OPTION_RETURN_ON_ERROR); if (h == NULL) { NSLinkEditErrors linkEditError; int errorNum; const char *fileName; const char *errorString; NSLinkEditError(&linkEditError, &errorNum, &fileName, &errorString); PR_LOG(_pr_linker_lm, PR_LOG_MIN, ("LoadMachDyldModule error %d:%d for file %s:\n%s", linkEditError, errorNum, fileName, errorString)); } if (NSDestroyObjectFileImage(ofi) == FALSE) { if (h) { (void)NSUnLinkModule(h, NSUNLINKMODULE_OPTION_NONE); h = NULL; } } } return h; }
void * caml_dlopen(char * libname, int for_execution) { NSObjectFileImage image; entry_t *bentry = caml_lookup_bundle(libname); NSObjectFileImageReturnCode retCode; void *result = NULL; if (bentry->count > 0) return bentry->handle; retCode = NSCreateObjectFileImageFromFile(libname, &image); switch (retCode) { case NSObjectFileImageSuccess: dlerror_string = NULL; result = (void*)NSLinkModule(image, libname, NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_RETURN_ON_ERROR); if (result != NULL) { bentry->count++; bentry->handle = result; } else NSDestroyObjectFileImage(image); break; 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 result; }
void *dlopen(const char *path, int WXUNUSED(mode) /* mode is ignored */) { NSObjectFileImage ofile; NSModule handle = NULL; int dyld_result = NSCreateObjectFileImageFromFile(path, &ofile); if ( dyld_result != NSObjectFileImageSuccess ) { handle = NULL; } else { handle = NSLinkModule ( ofile, path, NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_RETURN_ON_ERROR ); } if ( !handle ) TranslateError(path, dyld_result); return handle; }
/* dlopen */ void *darwin_dlopen(const char *path, int mode) { void *module = 0; NSObjectFileImage ofi = 0; NSObjectFileImageReturnCode ofirc; static int (*make_private_module_public) (NSModule module) = 0; unsigned int flags = NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE; sys::coreout << "darwin try bind module: " << path << sys::eofl; /* If we got no path, the app wants the global namespace, use -1 as the marker in this case */ if (!path) return (void *)-1; /* Create the object file image, works for things linked with the -bundle arg to ld */ ofirc = NSCreateObjectFileImageFromFile(path, &ofi); switch (ofirc) { case NSObjectFileImageSuccess: /* It was okay, so use NSLinkModule to link in the image */ if (!(mode & RTLD_LAZY)) flags += NSLINKMODULE_OPTION_BINDNOW; module = NSLinkModule(ofi, path,flags); sys::coreout << "darwin bind module: " << path << sys::eofl; /* Don't forget to destroy the object file image, unless you like leaks */ NSDestroyObjectFileImage(ofi); /* If the mode was global, then change the module, this avoids multiply defined symbol errors to first load private then make global. Silly, isn't it. */ if ((mode & RTLD_GLOBAL)) { if (!make_private_module_public) { _dyld_func_lookup("__dyld_NSMakePrivateModulePublic", (unsigned long *)&make_private_module_public); } make_private_module_public(module); } break; case NSObjectFileImageInappropriateFile: /* It may have been a dynamic library rather than a bundle, try to load it */ module = (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR); sys::coreout << " wrong filetype use addimage: " << (void*)module << sys::eofl; break; case NSObjectFileImageFailure: error(0,"Object file setup failure : \"%s\"", path); return 0; case NSObjectFileImageArch: error(0,"No object for this architecture : \"%s\"", path); return 0; case NSObjectFileImageFormat: error(0,"Bad object file format : \"%s\"", path); return 0; case NSObjectFileImageAccess: error(0,"Can't read object file : \"%s\"", path); return 0; } if (!module) error(0, "Can not open \"%s\"", path); return module; }
SharedLibraryRef dyldSharedLibraryLoader::loadSharedLibrary(const String& filename, const LoggerRef& logger) const { OW_LOG_DEBUG(logger, Format("Load request for %1 received.", filename)); NSObjectFileImage image = 0; NSObjectFileImageReturnCode dsoerr = NSCreateObjectFileImageFromFile(filename.c_str(), &image); const char* err_msg = NULL; NSModule libhandle = NULL; if (dsoerr == NSObjectFileImageSuccess) { libhandle = NSLinkModule(image, filename.c_str(), NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE); if (!libhandle) { NSLinkEditErrors errors; int errorNumber; const char *fileName; NSLinkEditError(&errors, &errorNumber, &fileName, &err_msg); } NSDestroyObjectFileImage(image); } else if ((dsoerr == NSObjectFileImageFormat || dsoerr == NSObjectFileImageInappropriateFile) && NSAddLibrary(filename.c_str()) == TRUE) { OW_LOG_ERROR(logger, Format("NSCreateObject: %1 failed with error \"%2\"", filename, dsoerr)); // libhandle = (NSModule)DYLD_LIBRARY_HANDLE; } else { err_msg = "cannot create object file image or add library"; OW_LOG_ERROR(logger, Format("NSCreateObject: %1 failed with error %2", filename, dsoerr)); } if (libhandle) { try { return SharedLibraryRef( new dyldSharedLibrary(libhandle, filename)); } catch (...) { NSUnLinkModule(libhandle, FALSE); throw; } } else { OW_LOG_ERROR(logger, Format("dyldSharedLibraryLoader::loadSharedLibrary:" " %1", err_msg)); return SharedLibraryRef( 0 ); } }
static void *wx_darwin_dlopen(const char *path, int WXUNUSED(mode) /* mode is ignored */) { NSObjectFileImage ofile; NSModule handle = NULL; unsigned dyld_result = NSCreateObjectFileImageFromFile(path, &ofile); if ( dyld_result != NSObjectFileImageSuccess ) { handle = NULL; static const char *const errorStrings[] = { "%d: Object Image Load Failure", "%d: Object Image Load Success", "%d: Not an recognisable object file", "%d: No valid architecture", "%d: Object image has an invalid format", "%d: Invalid access (permissions?)", "%d: Unknown error code from NSCreateObjectFileImageFromFile" }; const int index = dyld_result < WXSIZEOF(errorStrings) ? dyld_result : WXSIZEOF(errorStrings) - 1; // this call to sprintf() is safe as strings above are fixed at // compile-time and are shorter than WXSIZEOF(dl_last_error) sprintf(dl_last_error, errorStrings[index], dyld_result); } else { handle = NSLinkModule ( ofile, path, NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_RETURN_ON_ERROR ); if ( !handle ) { NSLinkEditErrors err; int code; const char *filename; const char *errmsg; NSLinkEditError(&err, &code, &filename, &errmsg); strncpy(dl_last_error, errmsg, WXSIZEOF(dl_last_error)-1); dl_last_error[WXSIZEOF(dl_last_error)-1] = '\0'; } } return handle; }
void * pg_dlopen(char *filename) { NSObjectFileImage image; cofiff_result = NSCreateObjectFileImageFromFile(filename, &image); if (cofiff_result != NSObjectFileImageSuccess) return NULL; return NSLinkModule(image, filename, NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_RETURN_ON_ERROR); }
void *vmddlopen( const char *path) { NSObjectFileImage image; NSObjectFileImageReturnCode retval; NSModule module; retval = NSCreateObjectFileImageFromFile(path, &image); if (retval != NSObjectFileImageSuccess) return NULL; module = NSLinkModule(image, path, NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_PRIVATE | NSLINKMODULE_OPTION_RETURN_ON_ERROR); return module; /* module will be NULL on error */ }
void *ILDynLibraryOpen(const char *name) { NSObjectFileImage file; NSObjectFileImageReturnCode result; NSModule module; void *image; const char *msg; /* Attempt to open the dylib file */ result = NSCreateObjectFileImageFromFile(name, &file); if(result == NSObjectFileImageInappropriateFile) { /* May be an image, and not a bundle */ image = (void *)NSAddImage(name, NSADDIMAGE_OPTION_RETURN_ON_ERROR); if(image) { return image; } } if(result != NSObjectFileImageSuccess) { switch(result) { case NSObjectFileImageFailure: msg = " (NSObjectFileImageFailure)"; break; case NSObjectFileImageInappropriateFile: msg = " (NSObjectFileImageInappropriateFile)"; break; case NSObjectFileImageArch: msg = " (NSObjectFileImageArch)"; break; case NSObjectFileImageFormat: msg = " (NSObjectFileImageFormat)"; break; case NSObjectFileImageAccess: msg = " (NSObjectFileImageAccess)"; break; default: msg = ""; break; } #ifdef IL_DYNLIB_DEBUG fprintf(stderr, "%s: could not load dynamic library%s\n", name, msg); #endif return 0; } /* Link the module dependencies */ module = NSLinkModule(file, name, NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_PRIVATE | NSLINKMODULE_OPTION_RETURN_ON_ERROR); return (void *)module; }
/* A function called through the vtable to open a module with this loader. Returns an opaque representation of the newly opened module for processing with this loader's other vtable functions. */ static lt_module vm_open (lt_user_data loader_data, const char *filename, lt_dladvise LT__UNUSED advise) { lt_module module = 0; NSObjectFileImage ofi = 0; if (!filename) { return (lt_module) -1; } switch (NSCreateObjectFileImageFromFile (filename, &ofi)) { case NSObjectFileImageSuccess: module = NSLinkModule (ofi, filename, NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE | NSLINKMODULE_OPTION_BINDNOW); NSDestroyObjectFileImage (ofi); if (module) { lt__module_export (module); } break; case NSObjectFileImageInappropriateFile: if (lt__image_symbol_p && lt__image_symbol) { module = (lt_module) lt__addimage(filename, NSADDIMAGE_OPTION_RETURN_ON_ERROR); } break; case NSObjectFileImageFailure: case NSObjectFileImageArch: case NSObjectFileImageFormat: case NSObjectFileImageAccess: /*NOWORK*/ break; } if (!module) { DYLD__SETERROR (CANNOT_OPEN); } return module; }
static void * mydlopen(char *path) { int dyld_result; NSObjectFileImage ofile; NSModule handle = NULL; dyld_result = NSCreateObjectFileImageFromFile(path, &ofile); if (dyld_result != NSObjectFileImageSuccess) { LOCAL_dl_errno = dyld_result; } else { /* NSLinkModule will cause the run to abort on any link error's */ /* not very friendly but the error recovery functionality is limited */ handle = NSLinkModule(ofile, path, TRUE); } return handle; }
void *dynamic_link_file( const char *path ) { NSObjectFileImageReturnCode irc; NSObjectFileImage mo; NSModule mp; irc = NSCreateObjectFileImageFromFile( path, &mo ); if (irc != NSObjectFileImageSuccess) { return NULL; } mp = NSLinkModule( mo, "slave.so", NSLINKMODULE_OPTION_BINDNOW ); return mp; }
static void * dyld_dlopen(const char * file) { NSObjectFileImage o=NULL; NSObjectFileImageReturnCode r; NSModule m=NULL; const unsigned int flags = NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE; dyld_error_set=0; r = NSCreateObjectFileImageFromFile(file,&o); if (NSObjectFileImageSuccess == r) { m=NSLinkModule(o,file,flags); NSDestroyObjectFileImage(o); if (!m) dyld_error_set=1; } return (void*)m; }
int main() { // NSCreateObjectFileImageFromMemory is only available on Mac OS X - not iPhone OS #if __MAC_OS_X_VERSION_MIN_REQUIRED NSObjectFileImage ofi; if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi) != NSObjectFileImageSuccess ) { FAIL("NSCreateObjectFileImageFromFile failed"); return 1; } NSModule mod = NSLinkModule(ofi, "test.bundle", NSLINKMODULE_OPTION_NONE); if ( mod == NULL ) { FAIL("NSLinkModule failed"); return 1; } NSSymbol sym = NSLookupSymbolInModule(mod, "_checkdata"); if ( sym == NULL ) { FAIL("NSLookupSymbolInModule failed"); return 1; } CheckFunc func = NSAddressOfSymbol(sym); if ( !func() ) { FAIL("NSAddressOfSymbol failed"); return 1; } if ( !NSUnLinkModule(mod, NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED) ) { FAIL("NSUnLinkModule failed"); return 1; } if ( !NSDestroyObjectFileImage(ofi) ) { FAIL("NSDestroyObjectFileImage failed"); return 1; } // call function again, even though bundle is unloaded func(); #endif PASS("bundle-basic"); return 0; }
DWORD OSXPluginInstance::Load(const char *fname) { if (fname==NULL || fname[0]==0) return FF_FAIL; Unload(); if (NSCreateObjectFileImageFromFile(fname, &m_ffImage)!=NSObjectFileImageSuccess) return FF_FAIL; NSModule m_ffModule = NSLinkModule( m_ffImage, fname, NSLINKMODULE_OPTION_NONE); if (m_ffModule==NULL) { Unload(); //to undo NSCreateObjectFileImageFromFile return FF_FAIL; } NSSymbol s = NSLookupSymbolInModule(m_ffModule, "_plugMain"); if (s==NULL) { Unload();//to undo NSLinkModule and NSCreateObjectFileImageFromFile return FF_FAIL; } FF_Main_FuncPtr pFreeFrameMain = (FF_Main_FuncPtr)NSAddressOfSymbol(s); if (pFreeFrameMain==NULL) { Unload(); //to undo same return FF_FAIL; } m_ffPluginMain = pFreeFrameMain; DWORD rval = InitPluginLibrary(); if (rval!=FF_SUCCESS) return rval; return FF_SUCCESS; }
static void* ll_load (lua_State *L, const char* path) { NSObjectFileImage img; NSObjectFileImageReturnCode ret; /* this would be a rare case, but prevents crashing if it happens */ if(!_dyld_present()) { lua_pushliteral(L, "dyld not present"); return nullptr; } ret = NSCreateObjectFileImageFromFile(path, &img); if (ret == NSObjectFileImageSuccess) { NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE | NSLINKMODULE_OPTION_RETURN_ON_ERROR); NSDestroyObjectFileImage(img); if (mod == nullptr) pusherror(L); return mod; } lua_pushstring(L, errorfromcode(ret)); return nullptr; }
static NSModule pr_LoadMachDyldModule(const char *name) { NSObjectFileImage ofi; NSModule h = NULL; if (NSCreateObjectFileImageFromFile(name, &ofi) == NSObjectFileImageSuccess) { h = NSLinkModule(ofi, name, NSLINKMODULE_OPTION_PRIVATE | NSLINKMODULE_OPTION_RETURN_ON_ERROR); /* * TODO: If NSLinkModule fails, use NSLinkEditError to retrieve * error information. */ if (NSDestroyObjectFileImage(ofi) == FALSE) { if (h) { (void)NSUnLinkModule(h, NSUNLINKMODULE_OPTION_NONE); h = NULL; } } } return h; }
int main() { // NSCreateObjectFileImageFromMemory is only available on Mac OS X - not iPhone OS #if __MAC_OS_X_VERSION_MIN_REQUIRED NSObjectFileImage ofi; if ( NSCreateObjectFileImageFromFile("test.bundle", &ofi) != NSObjectFileImageSuccess ) { FAIL("NSCreateObjectFileImageFromFile failed"); return 1; } // make sure not-yet-linked-ofi is not visible through _dyld_get_image_name int count = _dyld_image_count(); for(int i=0; i < count; ++i) { const char* name = _dyld_get_image_name(i); if ( strcmp(name, "test.bundle") == 0 ) { FAIL("unlinked test.bundle found via _dyld_get_image_name()"); return 1; } } NSModule mod = NSLinkModule(ofi, "test.bundle", NSLINKMODULE_OPTION_RETURN_ON_ERROR); if ( mod != NULL ) { FAIL("NSLinkModule succeeded but should have failed"); return 1; } // make sure link-failed-ofi is not visible through _dyld_get_image_name count = _dyld_image_count(); for(int i=0; i < count; ++i) { const char* name = _dyld_get_image_name(i); if ( strcmp(name, "test.bundle") == 0 ) { FAIL("failed linked test.bundle found via _dyld_get_image_name()"); return 1; } } #endif PASS("bundle-unlinkable"); return 0; }
/* darwin_dlopen */ static void *darwin_dlopen(const char *path, int mode) { void *module = 0; NSObjectFileImage ofi = 0; NSObjectFileImageReturnCode ofirc; /* If we got no path, the app wants the global namespace, use -1 as the marker in this case */ if (!path) return (void *)-1; /* Create the object file image, works for things linked with the -bundle arg to ld */ ofirc = NSCreateObjectFileImageFromFile(path, &ofi); switch (ofirc) { case NSObjectFileImageSuccess: /* It was okay, so use NSLinkModule to link in the image */ module = NSLinkModule(ofi, path, NSLINKMODULE_OPTION_RETURN_ON_ERROR | (mode & RTLD_GLOBAL) ? 0 : NSLINKMODULE_OPTION_PRIVATE | (mode & RTLD_LAZY) ? 0 : NSLINKMODULE_OPTION_BINDNOW); NSDestroyObjectFileImage(ofi); break; case NSObjectFileImageInappropriateFile: /* It may have been a dynamic library rather than a bundle, try to load it */ module = (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR); break; default: /* God knows what we got */ error(0, "Can not open \"%s\"", path); return 0; } if (!module) error(0, "Can not open \"%s\"", path); return module; }
static void *dlopen(const char *module_name, int how) { struct pike_dl_handle *handle = malloc(sizeof(struct pike_dl_handle)); NSObjectFileImageReturnCode code = 0; pike_dl_error = NULL; if (!handle) { pike_dl_error = "Out of memory."; return NULL; } handle->image = NULL; handle->module = NULL; /* FIXME: Should be fixed to detect if the module already is loaded. */ if ((code = NSCreateObjectFileImageFromFile(module_name, &handle->image)) != NSObjectFileImageSuccess) { #ifdef PIKE_DEBUG fprintf(stderr, "NSCreateObjectFileImageFromFile(\"%s\") failed with %d\n", module_name, code); #endif /* PIKE_DEBUG */ pike_dl_error = "NSCreateObjectFileImageFromFile() failed."; dlclose(handle); return NULL; } handle->module = NSLinkModule(handle->image, module_name, how | NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE); if (!handle->module) { dlclose(handle); return NULL; } return handle; }
static shl_t dl_open(AmplExports *ae, char *name, int *warned, int *pns) { FILE *f; char *d, *d0, *s; const char *cs; int ns; shl_t h; #ifdef Old_APPLE NS_pair p; #endif d = d0 = 0; for(s = name; *s; ++s) switch(*s) { case '.': d = s; break; case '/': #ifdef WIN32 case '\\': #endif d = 0; } ns = s - name; if (d && d - name > 3 && d[-3] == '_' && d[-2] == BitsAlt[0] && d[-1] == BitsAlt[1]) { d[-2] = Bits[0]; d[-1] = Bits[1]; d = 0; } tryagain: #ifdef Old_APPLE NSObjectFileImageReturnCode irc; irc = NSCreateObjectFileImageFromFile(name,&p.ofi); h = 0; if (irc == NSObjectFileImageSuccess) { p.m = NSLinkModule(p.ofi, name, NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_PRIVATE | NSLINKMODULE_OPTION_RETURN_ON_ERROR); if (!p.m) fprintf(stderr, "NSLinkModule(\"%s\") failed.\n", name); else { h = (NS_pair*)mymalloc(sizeof(NS_pair) + strlen(name) + 1); strcpy(p.name = (char*)(h+1), name); memcpy(h, &p, sizeof(NS_pair)); } } else if (irc != NSObjectFileImageAccess) fprintf(stderr, "return %d from NSCreateObjectFileImageFromFile(\"%s\")\n", irc, name); #else #ifdef WIN32 /*{*/ /* make sure name is for the right number of bits */ if (wrong_bits(ae, name)) h = 0; else #endif /*}*/ h = dlopen(name, RTLD_NOW); #endif if (!h) { if (d) { do s[3] = s[0]; while(--s >= d); d[0] = '_'; d[1] = Bits[0]; d[2] = Bits[1]; d0 = d; d = 0; ns += 3; goto tryagain; } if (!warned && (f = fopen(name,"rb"))) { fclose(f); if (reg_file(name)) { *warned = 1; if (d0) for(s = d0; (s[0] = s[3]); ++s); #ifdef NO_DLERROR fprintf(Stderr, "Cannot load library %s.\n", name); #else fprintf(Stderr, "Cannot load library %s", name); cs = dlerror(); fprintf(Stderr, cs ? ":\n%s\n" : ".\n", cs); #endif } } } *pns = ns; return h; }
MODULE_SCOPE int TclpDlopen( Tcl_Interp *interp, /* Used for error reporting. */ Tcl_Obj *pathPtr, /* Name of the file containing the desired * code (UTF-8). */ Tcl_LoadHandle *loadHandle, /* Filled with token for dynamically loaded * file which will be passed back to * (*unloadProcPtr)() to unload the file. */ Tcl_FSUnloadFileProc **unloadProcPtr) /* Filled with address of Tcl_FSUnloadFileProc * function which should be used for this * file. */ { Tcl_DyldLoadHandle *dyldLoadHandle; #if TCL_DYLD_USE_DLFCN void *dlHandle = NULL; #endif #if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY) const struct mach_header *dyldLibHeader = NULL; Tcl_DyldModuleHandle *modulePtr = NULL; #endif #if TCL_DYLD_USE_NSMODULE NSLinkEditErrors editError; int errorNumber; const char *errorName, *objFileImageErrMsg = NULL; #endif const char *errMsg = NULL; int result; Tcl_DString ds; char *fileName = NULL; const char *nativePath, *nativeFileName = NULL; /* * First try the full path the user gave us. This is particularly * important if the cwd is inside a vfs, and we are trying to load using a * relative path. */ nativePath = Tcl_FSGetNativePath(pathPtr); #if TCL_DYLD_USE_DLFCN #if MAC_OS_X_VERSION_MIN_REQUIRED < 1040 if (tclMacOSXDarwinRelease >= 8) #endif { dlHandle = dlopen(nativePath, RTLD_NOW | RTLD_LOCAL); if (!dlHandle) { /* * Let the OS loader examine the binary search path for whatever * string the user gave us which hopefully refers to a file on the * binary path. */ fileName = Tcl_GetString(pathPtr); nativeFileName = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds); dlHandle = dlopen(nativeFileName, RTLD_NOW | RTLD_LOCAL); } if (dlHandle) { TclLoadDbgMsg("dlopen() successful"); } else { errMsg = dlerror(); TclLoadDbgMsg("dlopen() failed: %s", errMsg); } } if (!dlHandle) #endif /* TCL_DYLD_USE_DLFCN */ { #if TCL_DYLD_USE_NSMODULE dyldLibHeader = NSAddImage(nativePath, NSADDIMAGE_OPTION_RETURN_ON_ERROR); if (dyldLibHeader) { TclLoadDbgMsg("NSAddImage() successful"); } else { NSLinkEditError(&editError, &errorNumber, &errorName, &errMsg); if (editError == NSLinkEditFileAccessError) { /* * The requested file was not found. Let the OS loader examine * the binary search path for whatever string the user gave us * which hopefully refers to a file on the binary path. */ if (!fileName) { fileName = Tcl_GetString(pathPtr); nativeFileName = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds); } dyldLibHeader = NSAddImage(nativeFileName, NSADDIMAGE_OPTION_WITH_SEARCHING | NSADDIMAGE_OPTION_RETURN_ON_ERROR); if (dyldLibHeader) { TclLoadDbgMsg("NSAddImage() successful"); } else { NSLinkEditError(&editError, &errorNumber, &errorName, &errMsg); TclLoadDbgMsg("NSAddImage() failed: %s", errMsg); } } else if ((editError == NSLinkEditFileFormatError && errorNumber == EBADMACHO) || editError == NSLinkEditOtherError){ NSObjectFileImageReturnCode err; NSObjectFileImage dyldObjFileImage; NSModule module; /* * The requested file was found but was not of type MH_DYLIB, * attempt to load it as a MH_BUNDLE. */ err = NSCreateObjectFileImageFromFile(nativePath, &dyldObjFileImage); if (err == NSObjectFileImageSuccess && dyldObjFileImage) { TclLoadDbgMsg("NSCreateObjectFileImageFromFile() " "successful"); module = NSLinkModule(dyldObjFileImage, nativePath, NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_RETURN_ON_ERROR); NSDestroyObjectFileImage(dyldObjFileImage); if (module) { modulePtr = (Tcl_DyldModuleHandle *) ckalloc(sizeof(Tcl_DyldModuleHandle)); modulePtr->module = module; modulePtr->nextPtr = NULL; TclLoadDbgMsg("NSLinkModule() successful"); } else { NSLinkEditError(&editError, &errorNumber, &errorName, &errMsg); TclLoadDbgMsg("NSLinkModule() failed: %s", errMsg); } } else { objFileImageErrMsg = DyldOFIErrorMsg(err); TclLoadDbgMsg("NSCreateObjectFileImageFromFile() failed: " "%s", objFileImageErrMsg); } } } #endif /* TCL_DYLD_USE_NSMODULE */ } if (0 #if TCL_DYLD_USE_DLFCN || dlHandle #endif #if TCL_DYLD_USE_NSMODULE || dyldLibHeader || modulePtr #endif ) { dyldLoadHandle = (Tcl_DyldLoadHandle *) ckalloc(sizeof(Tcl_DyldLoadHandle)); #if TCL_DYLD_USE_DLFCN dyldLoadHandle->dlHandle = dlHandle; #endif #if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY) dyldLoadHandle->dyldLibHeader = dyldLibHeader; dyldLoadHandle->modulePtr = modulePtr; #endif *loadHandle = (Tcl_LoadHandle) dyldLoadHandle; *unloadProcPtr = &TclpUnloadFile; result = TCL_OK; } else { Tcl_AppendResult(interp, errMsg, NULL); #if TCL_DYLD_USE_NSMODULE if (objFileImageErrMsg) { Tcl_AppendResult(interp, "\nNSCreateObjectFileImageFromFile() " "error: ", objFileImageErrMsg, NULL); } #endif result = TCL_ERROR; } if(fileName) { Tcl_DStringFree(&ds); } return result; }
bool CLibrary::Load(LPCTSTR lpLibFileName) { if(!Free()) return false; void *handler = 0; char name[MAX_PATHNAME_LEN+1]; #ifdef _UNICODE AString name2 = UnicodeStringToMultiByte(lpLibFileName); strcpy(name,nameWindowToUnix((const char *)name2)); #else strcpy(name,nameWindowToUnix(lpLibFileName)); #endif // replace ".dll" with ".so" size_t len = strlen(name); if ((len >=4) && (strcmp(name+len-4,".dll") == 0)) { strcpy(name+len-4,".so"); } #ifdef __ANDROID__ char tmp[MAX_PATHNAME_LEN+1]; if (strncmp(name, "./", 2) == 0) { strcpy(tmp, name+2); sprintf(name, "lib%s", tmp); } #endif TRACEN((printf("CLibrary::Load(this=%p,%ls) => %s\n",(void *)this,lpLibFileName,name))) #ifdef __APPLE_CC__ NSObjectFileImage image; NSObjectFileImageReturnCode nsret; nsret = NSCreateObjectFileImageFromFile (name, &image); if (nsret == NSObjectFileImageSuccess) { TRACEN((printf("NSCreateObjectFileImageFromFile(%s) : OK\n",name))) handler = (HMODULE)NSLinkModule(image,name,NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE | NSLINKMODULE_OPTION_BINDNOW); } else { TRACEN((printf("NSCreateObjectFileImageFromFile(%s) : ERROR\n",name))) } #elif ENV_BEOS // normalize path (remove things like "./", "..", etc..), otherwise it won't work BPath p(name, NULL, true); status_t err = B_OK; image_id image = load_add_on(p.Path()); TRACEN((printf("load_add_on(%s)=%d\n",p.Path(),(int)image))) if (image < 0) { err = (image_id)handler; handler = 0; } else { err = 0; handler = (HMODULE)image; } #else int options_dlopen = 0; #ifdef RTLD_LOCAL options_dlopen |= RTLD_LOCAL; #endif #ifdef RTLD_NOW options_dlopen |= RTLD_NOW; #endif #ifdef RTLD_GROUP #if ! (defined(hpux) || defined(__hpux)) options_dlopen |= RTLD_GROUP; // mainly for solaris but not for HPUX #endif #endif TRACEN((printf("CLibrary::Load - dlopen(%s,0x%d)\n",name,options_dlopen))) handler = dlopen(name,options_dlopen); #endif // __APPLE_CC__ TRACEN((printf("CLibrary::Load(%s) => %p\n",name,handler))) if (handler) { // Call DllMain() like in Windows : useless now // Propagate the value of global_use_utf16_conversion into the plugins int *tmp = (int *)local_GetProcAddress(handler,"global_use_utf16_conversion"); if (tmp) *tmp = global_use_utf16_conversion; #ifdef ENV_HAVE_LSTAT tmp = (int *)local_GetProcAddress(handler,"global_use_lstat"); if (tmp) *tmp = global_use_lstat; #endif // test construtors calls void (*fctTest)(void) = (void (*)(void))local_GetProcAddress(handler,"sync_TestConstructor"); if (fctTest) fctTest(); } else { #ifdef __APPLE_CC__ NSLinkEditErrors c; int num_err; const char *file,*err; NSLinkEditError(&c,&num_err,&file,&err); printf("Can't load '%ls' (%s)\n", lpLibFileName,err); #elif ENV_BEOS printf("Can't load '%ls' (%s)\n", lpLibFileName,strerror(err)); #else printf("Can't load '%ls' (%s)\n", lpLibFileName,dlerror()); #endif } _module = handler; TRACEN((printf("CLibrary::Load(this=%p,%ls) => _module=%p\n",(void *)this,lpLibFileName,_module))) return true; }
void * Instance_Module_Load(const char * libLocation, const char * name, void ** Load, void ** Unload) { char fileName[MAX_LOCATION]; char extension[MAX_EXTENSION]; void * library = null; #if defined(__unix__) || defined(__APPLE__) int attempts = 0; char * paths[] = { null, "/usr/lib/ec/lib", "/usr/lib32/ec/lib" }; #endif *Load = null; *Unload = null; #if defined(__WIN32__) strcpy(fileName, name); GetExtension(fileName, extension); if(!extension[0]) strcat(fileName, ".dll"); { uint16 _wfileName[MAX_LOCATION]; UTF8toUTF16Buffer(fileName, _wfileName, MAX_LOCATION); library = LoadLibraryW(_wfileName); } if(library) { #ifdef _WIN64 *Load = (void *)GetProcAddress(library, "__ecereDll_Load"); *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload"); #else *Load = (void *)GetProcAddress(library, "__ecereDll_Load@4"); *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload@4"); #endif if(!*Load) FreeLibrary(library); } #elif defined(__unix__) || defined(__APPLE__) if(libLocation || strchr(name, '/')) strcpy(fileName, libLocation ? libLocation : ""); else strcpy(fileName, "lib"); strcat(fileName, name); GetExtension(fileName, extension); if(!extension[0]) #if defined(__APPLE__) strcat(fileName, ".dylib"); #else strcat(fileName, ".so"); #endif #if !defined(__EMSCRIPTEN__) // dlerror(); library = dlopen(fileName, RTLD_LAZY); // if(!library) // printf("Error opening %s: %s", fileName, dlerror()); #endif while(!library && attempts < sizeof(paths)/sizeof(paths[0])) { if(paths[attempts]) strcpy(fileName, paths[attempts++]); else { attempts++; #ifdef DEB_HOST_MULTIARCH strcpy(fileName, DEB_HOST_MULTIARCH); strcat(fileName, "/ec/lib"); #else continue; #endif } strcat(fileName, name); GetExtension(fileName, extension); if(!extension[0]) #if defined(__APPLE__) strcat(fileName, ".dylib"); #else strcat(fileName, ".so"); #endif #if !defined(__EMSCRIPTEN__) library = dlopen(fileName, RTLD_LAZY); #endif } if(library) { *Load = dlsym(library, "__ecereDll_Load"); *Unload = dlsym(library, "__ecereDll_Unload"); #if !defined(__EMSCRIPTEN__) if(!*Load) dlclose(library); #endif } #elif defined(__APPLE__) if(libLocation || strchr(name, '/')) strcpy(fileName, libLocation ? libLocation : ""); else strcpy(fileName, "lib"); strcat(fileName, name); GetExtension(fileName, extension); if(!extension[0]) strcat(fileName, ".dylib"); { NSObjectFileImage *fileImage; NSObjectFileImageReturnCode returnCode = NSCreateObjectFileImageFromFile(fileName, &fileImage); if(returnCode == NSObjectFileImageSuccess) { printf("NSObjectFileImageSuccess!\n"); library = NSLinkModule(fileImage,fileName, NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE); // NSDestroyObjectFileImage(fileImage); if(library) { *Load = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Load")); *Unload = NSAddressOfSymbol(NSLookupSymbolInModule(library, "__ecereDll_Unload")); if(!*Load) { NSUnLinkModule(library, 0); } else printf("Found Load!\n"); } } else printf("No Success :(\n"); } #endif return library; }
dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, const char *pathname, FILE *fp) { dl_funcptr p = NULL; char funcname[258]; NSObjectFileImageReturnCode rc; NSObjectFileImage image; NSModule newModule; NSSymbol theSym; const char *errString; char errBuf[512]; PyOS_snprintf(funcname, sizeof(funcname), "_init%.200s", shortname); #ifdef USE_DYLD_GLOBAL_NAMESPACE if (NSIsSymbolNameDefined(funcname)) { theSym = NSLookupAndBindSymbol(funcname); p = (dl_funcptr)NSAddressOfSymbol(theSym); return p; } #endif rc = NSCreateObjectFileImageFromFile(pathname, &image); switch(rc) { default: case NSObjectFileImageFailure: case NSObjectFileImageFormat: /* for these a message is printed on stderr by dyld */ errString = "Can't create object file image"; break; case NSObjectFileImageSuccess: errString = NULL; break; case NSObjectFileImageInappropriateFile: errString = "Inappropriate file type for dynamic loading"; break; case NSObjectFileImageArch: errString = "Wrong CPU type in object file"; break; case NSObjectFileImageAccess: errString = "Can't read object file (no access)"; break; } if (errString == NULL) { newModule = NSLinkModule(image, pathname, LINKOPTIONS); if (newModule == NULL) { int errNo; const char *fileName, *moreErrorStr; NSLinkEditErrors c; NSLinkEditError( &c, &errNo, &fileName, &moreErrorStr ); PyOS_snprintf(errBuf, 512, "Failure linking new module: %s: %s", fileName, moreErrorStr); errString = errBuf; } } if (errString != NULL) { PyErr_SetString(PyExc_ImportError, errString); return NULL; } #ifdef USE_DYLD_GLOBAL_NAMESPACE if (!NSIsSymbolNameDefined(funcname)) { /* UnlinkModule() isn't implemented in current versions, but calling it does no harm */ NSUnLinkModule(newModule, FALSE); PyErr_Format(PyExc_ImportError, "Loaded module does not contain symbol %.200s", funcname); return NULL; } theSym = NSLookupAndBindSymbol(funcname); #else theSym = NSLookupSymbolInModule(newModule, funcname); if ( theSym == NULL ) { NSUnLinkModule(newModule, FALSE); PyErr_Format(PyExc_ImportError, "Loaded module does not contain symbol %.200s", funcname); return NULL; } #endif p = (dl_funcptr)NSAddressOfSymbol(theSym); return p; }
APR_DECLARE(apr_status_t) apr_dso_load(apr_dso_handle_t **res_handle, const char *path, apr_pool_t *pool) { #if defined(DSO_USE_SHL) shl_t os_handle = shl_load(path, BIND_IMMEDIATE, 0L); #elif defined(DSO_USE_DYLD) NSObjectFileImage image; NSModule os_handle = NULL; NSObjectFileImageReturnCode dsoerr; const char* err_msg = NULL; dsoerr = NSCreateObjectFileImageFromFile(path, &image); if (dsoerr == NSObjectFileImageSuccess) { #if defined(NSLINKMODULE_OPTION_RETURN_ON_ERROR) && defined(NSLINKMODULE_OPTION_NONE) os_handle = NSLinkModule(image, path, NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_NONE); /* If something went wrong, get the errors... */ if (!os_handle) { NSLinkEditErrors errors; int errorNumber; const char *fileName; NSLinkEditError(&errors, &errorNumber, &fileName, &err_msg); } #else os_handle = NSLinkModule(image, path, FALSE); #endif NSDestroyObjectFileImage(image); } else if ((dsoerr == NSObjectFileImageFormat || dsoerr == NSObjectFileImageInappropriateFile) && NSAddLibrary(path) == TRUE) { os_handle = (NSModule)DYLD_LIBRARY_HANDLE; } else { err_msg = "cannot create object file image or add library"; } #elif defined(DSO_USE_DLFCN) #if defined(OSF1) || defined(SEQUENT) || defined(SNI) ||\ (defined(__FreeBSD_version) && (__FreeBSD_version >= 220000)) void *os_handle = dlopen((char *)path, RTLD_NOW | RTLD_GLOBAL); #else int flags = RTLD_NOW | RTLD_GLOBAL; void *os_handle; #ifdef _AIX if (strchr(path + 1, '(') && path[strlen(path) - 1] == ')') { /* This special archive.a(dso.so) syntax is required for * the way libtool likes to build shared libraries on AIX. * dlopen() support for such a library requires that the * RTLD_MEMBER flag be enabled. */ flags |= RTLD_MEMBER; } #endif os_handle = dlopen(path, flags); #endif #endif /* DSO_USE_x */ *res_handle = apr_pcalloc(pool, sizeof(**res_handle)); if(os_handle == NULL) { #if defined(DSO_USE_SHL) (*res_handle)->errormsg = strerror(errno); return APR_EDSOOPEN; #elif defined(DSO_USE_DYLD) (*res_handle)->errormsg = (err_msg) ? err_msg : "link failed"; return APR_EDSOOPEN; #elif defined(DSO_USE_DLFCN) (*res_handle)->errormsg = dlerror(); return APR_EDSOOPEN; #endif } (*res_handle)->handle = (void*)os_handle; (*res_handle)->pool = pool; (*res_handle)->errormsg = NULL; apr_pool_cleanup_register(pool, *res_handle, dso_cleanup, apr_pool_cleanup_null); return APR_SUCCESS; }