main (int argc, char **argv) { char *libname = NULL; if (LIBNAME) libname = LIBNAME; else if (argc == 2 && argv[1] != NULL) libname = argv[1]; if (libname) { if (! NSAddLibrary (libname)) { fprintf (stderr, "Unable to load `%s' library.\n", libname); exit (1); } if (NSIsSymbolNameDefined ("_foo")) { int (*addr)(void) = NSAddressOfSymbol (NSLookupAndBindSymbol ("_foo")); printf ("foo is resolved to address %lx\n", (unsigned long) addr); if (addr) addr (); } } else { fprintf (stderr, "No library specified.\n"); } if (SECONDLIBNAME) libname = SECONDLIBNAME; else if (argc == 2 && argv[1] != NULL) libname = argv[1]; if (libname) { if (! NSAddLibrary (libname)) { fprintf (stderr, "Unable to load `%s' library.\n", libname); exit (1); } if (NSIsSymbolNameDefined ("_blubby")) { int (*addr)(int) = NSAddressOfSymbol (NSLookupAndBindSymbol ("_blubby")); printf ("blubby is resolved to address %lx\n", (unsigned long) addr); if (addr) { addr (5); addr (6); } } } else { fprintf (stderr, "No library specified.\n"); } }
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 ); } }
/* Attempt to link a library from a specified filename */ dso_handle dso_link(const char *path) { /* We need to load the library publicly as NSModuleFileImage is not yet implemented (at least for non MH_BUNDLE libraries */ if (NSAddLibrary(path) != TRUE) return (NULL); /* We need to return a non-null value, even if it has no meaning. One day this whole crap will be fixed */ return ((void *)!NULL); }
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; }