static void ll_unloadlib (void *lib) { NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); }
static int mydlclose(void *handle) { NSUnLinkModule(handle, NSUNLINKMODULE_OPTION_NONE); return TRUE; }
int dlclose(void *myModule) { NSUnLinkModule(myModule, FALSE); return(0); }
static int wx_darwin_dlclose(void *handle) { NSUnLinkModule((NSModule)handle, NSUNLINKMODULE_OPTION_NONE); return 0; }
int main() { // NSObjectFileImage APIs are only available on Mac OS X - not iPhone OS #if __MAC_OS_X_VERSION_MIN_REQUIRED int fd = open("test.bundle", O_RDONLY, 0); if ( fd == -1 ) { FAIL("open() failed"); return 1; } struct stat stat_buf; if ( fstat(fd, &stat_buf) == -1) { FAIL("fstat() failed"); return 0; } void* loadAddress = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); if ( loadAddress == ((void*)(-1)) ) { FAIL("mmap() failed"); return 0; } close(fd); NSObjectFileImage ofi; if ( NSCreateObjectFileImageFromMemory(loadAddress, stat_buf.st_size, &ofi) != NSObjectFileImageSuccess ) { FAIL("NSCreateObjectFileImageFromMemory failed"); return 0; } NSModule mod = NSLinkModule(ofi, "he_he", NSLINKMODULE_OPTION_NONE); if ( mod == NULL ) { FAIL("NSLinkModule failed"); return 0; } // look for he_he string in list of images loaded struct dyld_all_image_infos* infos = getImageInfosFromKernel(); if ( infos->infoArrayCount < 2 ) { FAIL("bundle-memory-load-all-images: dyld_all_image_infos.infoArrayCount is < 2"); return 0; } bool found = false; for( int i=0; i < infos->infoArrayCount; ++i) { //fprintf(stderr, "infos->infoArray[%d].imageLoadAddress=%p %s\n", i, infos->infoArray[i].imageLoadAddress, infos->infoArray[i].imageFilePath); if ( infos->infoArray[i].imageFilePath == NULL ) { FAIL("bundle-memory-load-all-images: NULL image path found"); exit(0); } if ( strcmp(infos->infoArray[i].imageFilePath, "he_he") == 0 ) found = true; } if ( !found ) { FAIL("bundle-memory-load-all-images: loaded memory bundle 'he_he' nout found"); return 0; } if ( !NSUnLinkModule(mod, NSUNLINKMODULE_OPTION_NONE) ) { FAIL("NSUnLinkModule failed"); return 0; } if ( !NSDestroyObjectFileImage(ofi) ) { FAIL("NSDestroyObjectFileImage failed"); return 0; } // Should check that loadAddress is unmmaped now (by call to NSDestroyObjectFileImage) #endif PASS("bundle-memory-load-all-images"); return 0; }
int dlclose(void *handle) { NSUnLinkModule((NSModule)handle, NSUNLINKMODULE_OPTION_NONE); return 0; }
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; }
static void checkBundle(const char* path, bool unlinkBeforeDestroy) { int fd = open(path, O_RDONLY, 0); if ( fd == -1 ) { printf("[FAIL] open(%s) failed", path); exit(0); } struct stat stat_buf; if ( fstat(fd, &stat_buf) == -1) { printf("[FAIL] fstat() failed\n"); exit(0); } void* loadAddress = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); if ( loadAddress == ((void*)(-1)) ) { printf("[FAIL] mmap() failed\n"); exit(0); } close(fd); NSObjectFileImage ofi; if ( NSCreateObjectFileImageFromMemory(loadAddress, stat_buf.st_size, &ofi) != NSObjectFileImageSuccess ) { printf("[FAIL] NSCreateObjectFileImageFromMemory failed\n"); exit(0); } NSModule mod = NSLinkModule(ofi, path, NSLINKMODULE_OPTION_NONE); if ( mod == NULL ) { printf("[FAIL] NSLinkModule failed\n"); exit(0); } if ( !unlinkBeforeDestroy ) { // API lets you destroy ofi and NSModule lives on if ( !NSDestroyObjectFileImage(ofi) ) { printf("[FAIL] NSDestroyObjectFileImage failed\n"); exit(0); } } NSSymbol sym = NSLookupSymbolInModule(mod, "_fooInBundle"); if ( sym == NULL ) { printf("[FAIL] NSLookupSymbolInModule failed\n"); exit(0); } void* func = NSAddressOfSymbol(sym); if ( func == NULL ) { printf("[FAIL] NSAddressOfSymbol failed\n"); exit(0); } Dl_info info; if ( dladdr(func, &info) == 0 ) { printf("[FAIL] dladdr(&p, xx) failed\n"); exit(0); } //printf("_fooInBundle found in %s\n", info.dli_fname); if ( !NSUnLinkModule(mod, NSUNLINKMODULE_OPTION_NONE) ) { printf("[FAIL] NSUnLinkModule failed\n"); exit(0); } if ( dladdr(func, &info) != 0 ) { printf("[FAIL] dladdr(&p, xx) found but should not have\n"); exit(0); } if ( unlinkBeforeDestroy ) { if ( !NSDestroyObjectFileImage(ofi) ) { printf("[FAIL] NSDestroyObjectFileImage failed\n"); exit(0); } } }
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; }
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 0; } NSModule mod = NSLinkModule(ofi, "test.bundle", NSLINKMODULE_OPTION_NONE); if ( mod == NULL ) { FAIL("NSLinkModule failed"); return 0; } NSSymbol sym = NSLookupSymbolInModule(mod, "_setValue"); if ( sym == NULL ) { FAIL("NSLookupSymbolInModule failed"); return 0; } setter func = NSAddressOfSymbol(sym); (*func)(1); //fprintf(stderr, "address of foo() = %p in bundle first load %p\n", func, mod); NSModule mod2 = NSLinkModule(ofi, "test2.bundle", NSLINKMODULE_OPTION_NONE); if ( mod2 == NULL ) { NSLinkEditErrors c; int errorNumber; const char* fileName; const char* errorString; NSLinkEditError(&c, &errorNumber, &fileName, &errorString); FAIL("2nd NSLinkModule failed: %s", errorString); return 0; } if ( mod == mod2 ) { FAIL("2nd NSLinkModule return same function address as first"); return 0; } NSSymbol sym2getter = NSLookupSymbolInModule(mod2, "_getValue"); if ( sym2getter == NULL ) { FAIL("2nd NSLookupSymbolInModule failed"); return 0; } getter func2getter = NSAddressOfSymbol(sym2getter); if ( (*func2getter)() != 0 ) { FAIL("_getValue() on second link returned non-zero"); return 0; } NSSymbol sym2 = NSLookupSymbolInModule(mod2, "_setValue"); if ( sym2 == NULL ) { FAIL("2nd NSLookupSymbolInModule failed"); return 0; } setter func2 = NSAddressOfSymbol(sym2); (*func2)(2); //fprintf(stderr, "address of foo() = %p in bundle second load %p\n", func2, mod2); if ( func == func2 ) { FAIL("2nd NSAddressOfSymbol return same function address as 1st"); return 0; } NSModule mod3 = NSLinkModule(ofi, "test3.bundle", NSLINKMODULE_OPTION_NONE); if ( mod3 == NULL ) { FAIL("3rd NSLinkModule failed"); return 0; } if ( mod3 == mod ) { FAIL("3rd NSLinkModule return same function address as 1st"); return 0; } if ( mod3 == mod2 ) { FAIL("3rd NSLinkModule return same function address as 2nd"); return 0; } NSSymbol sym3 = NSLookupSymbolInModule(mod3, "_setValue"); if ( sym3 == NULL ) { FAIL("3rd NSLookupSymbolInModule failed"); return 0; } setter func3 = NSAddressOfSymbol(sym3); (*func3)(3); //fprintf(stderr, "address of foo() = %p in bundle third load %p\n", func3, mod3); if ( func3 == func ) { FAIL("3rd NSAddressOfSymbol return same function address as 1st"); return 0; } if ( func3 == func2 ) { FAIL("3rd NSAddressOfSymbol return same function address as 2nd"); return 0; } if ( !NSUnLinkModule(mod, NSUNLINKMODULE_OPTION_NONE) ) { FAIL("NSUnLinkModule failed"); return 0; } if ( !NSUnLinkModule(mod3, NSUNLINKMODULE_OPTION_NONE) ) { FAIL("3rd NSUnLinkModule failed"); return 0; } if ( !NSUnLinkModule(mod2, NSUNLINKMODULE_OPTION_NONE) ) { FAIL("2nd NSUnLinkModule failed"); return 0; } // now link again after unlinking everything NSModule mod4 = NSLinkModule(ofi, "test4.bundle", NSLINKMODULE_OPTION_NONE); if ( mod4 == NULL ) { FAIL("4th NSLinkModule failed"); return 0; } // check that this is really a new copy by verifying the getValue() returns zero NSSymbol sym4getter = NSLookupSymbolInModule(mod4, "_getValue"); if ( sym4getter == NULL ) { FAIL("4th NSLookupSymbolInModule failed"); return 0; } getter func4getter = NSAddressOfSymbol(sym4getter); if ( (*func4getter)() != 0 ) { FAIL("_getValue() on fourth link returned non-zero"); return 0; } if ( !NSUnLinkModule(mod4, NSUNLINKMODULE_OPTION_NONE) ) { FAIL("4th NSUnLinkModule failed"); return 0; } if ( !NSDestroyObjectFileImage(ofi) ) { FAIL("NSDestroyObjectFileImage failed"); return 0; } #endif PASS("bundle-multi-link"); return 0; }
void unloadSharedObject (void *object) { NSModule module = object; NSUnLinkModule(module, NSUNLINKMODULE_OPTION_NONE); }
void * Instance_Module_Load(char * name, void ** Load, void ** Unload) { char fileName[MAX_LOCATION]; char extension[MAX_EXTENSION]; void * library = null; *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) { *Load = (void *)GetProcAddress(library, "__ecereDll_Load@4"); *Unload = (void *)GetProcAddress(library, "__ecereDll_Unload@4"); if(!*Load) FreeLibrary(library); } #elif defined(__unix__) || defined(__APPLE__) strcpy(fileName, "lib"); strcat(fileName, name); GetExtension(fileName, extension); if(!extension[0]) #if defined(__APPLE__) strcat(fileName, ".dylib"); #else strcat(fileName, ".so"); #endif library = dlopen(fileName, RTLD_LAZY); if(library) { *Load = dlsym(library, "__ecereDll_Load"); *Unload = dlsym(library, "__ecereDll_Unload"); if(!*Load) dlclose(library); } #elif defined(__APPLE__) 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; }
void pg_dlclose(void *handle) { NSUnLinkModule(handle, FALSE); }
int vmddlclose( void *handle ) { NSModule module = (NSModule *)handle; NSUnLinkModule(module, NSUNLINKMODULE_OPTION_NONE); return 0; }