static void *dlsym(void *handle, char *function) { NSSymbol symbol = NSLookupSymbolInModule(((struct pike_dl_handle *)handle)->module, function); return symbol?NSAddressOfSymbol(symbol):NULL; }
/* used by dlsym to find the symbol */ void *dlsymIntern(void *handle, const char *symbol) { NSSymbol nssym = NULL; if (handle == (void *)-1) { /* Global context */ if (NSIsSymbolNameDefined(symbol)) nssym = NSLookupAndBindSymbol(symbol); } else { if (is_mach_header(handle)) { /* library */ if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol)) nssym = NSLookupSymbolInImage((struct mach_header *)handle, symbol, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } else /* bundle */ nssym = NSLookupSymbolInModule((NSModule)handle, symbol); } if (!nssym) { error(0, "Symbol \"%s\" Not found", symbol); return NULL; } return NSAddressOfSymbol(nssym); }
void *dlsym(void *myModule, char *mySymbolName) { NSSymbol mySymbol; mySymbol = NSLookupSymbolInModule((NSModule)myModule, mySymbolName); return NSAddressOfSymbol(mySymbol); }
static void *GetSymbol(void *handle, const char *symbol) { NSSymbol sym; /* We have to use a different lookup approach for images and modules */ if((((struct mach_header *)handle)->magic == MH_MAGIC) || (((struct mach_header *)handle)->magic == MH_CIGAM)) { if(NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol)) { sym = NSLookupSymbolInImage((struct mach_header *)handle, symbol, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } else { sym = 0; } } else { sym = NSLookupSymbolInModule((NSModule)handle, symbol); } /* Did we find the symbol? */ if(sym == 0) { return 0; } /* Convert the symbol into the address that we require */ return (void *)NSAddressOfSymbol(sym); }
/* ggDarwinDLSym implements a "dlsym" wrapper */ void *ggDarwinDLSym(gg_dlhand handle, const char *symbol) { void *nsaddr = NULL; NSSymbol nssymbol = 0; struct gg_dlhand_darwin_t *darwin_module; darwin_module = (struct gg_dlhand_darwin_t *)handle; switch (darwin_module->nsmodule_flags) { case NSLINKMODULE_OPTION_NONE: nssymbol = NSLookupAndBindSymbol(symbol); break; case NSLINKMODULE_OPTION_PRIVATE: nssymbol = NSLookupSymbolInModule(darwin_module->nsmodule, symbol); break; } /* switch */ nsaddr = NSAddressOfSymbol(nssymbol); /* no error handling needed here. The error handlers * are called, when an error occurs. */ return nsaddr; } /* ggDarwinDLSym */
void* getmachosym(NSModule library, const char* symname) { NSSymbol sym = NSLookupSymbolInModule(library, symname); if(sym) return NSAddressOfSymbol(sym); return 0; }
static lua_CFunction ll_sym (lua_State *L, void* lib, const char* sym) { NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym); if (nss == nullptr) { lua_pushfstring(L, "symbol " LUA_QS " not found", sym); return nullptr; } return (lua_CFunction)NSAddressOfSymbol(nss); }
static void * dyld_dlsym(void * hand, const char * name) { NSSymbol sym=NSLookupSymbolInModule((NSModule)hand, name); void * addr = NULL; dyld_error_set=0; if (sym) addr=(void*)NSAddressOfSymbol(sym); if (!addr) dyld_error_set=1; return addr; }
static void* find_sym_addr(NS_pair *p, const char *name) { NSSymbol nss; if (nss = NSLookupSymbolInModule(p->m, name)) return NSAddressOfSymbol(nss); return 0; }
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; }
void *dlsym(void *handle, const char *symbol) { int sym_len = str_len(symbol); void *value = NULL; char *malloc_sym = NULL; NSSymbol *nssym = 0; malloc_sym = malloc(sym_len + 2); if (malloc_sym) { sprintf(malloc_sym, "_%s", symbol); /* If the handle is -1, if is the app global context */ if (handle == (void *)-1) { /* Global context, use NSLookupAndBindSymbol */ if (NSIsSymbolNameDefined(malloc_sym)) { nssym = NSLookupAndBindSymbol(malloc_sym); } } /* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image for libraries, and NSLookupSymbolInModule for bundles */ else { /* Check for both possible magic numbers depending on x86/ppc byte order */ if ((((struct mach_header *)handle)->magic == MH_MAGIC) || (((struct mach_header *)handle)->magic == MH_CIGAM)) { if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, malloc_sym)) { nssym = NSLookupSymbolInImage((struct mach_header *)handle, malloc_sym, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } } else { nssym = NSLookupSymbolInModule(handle, malloc_sym); } } if (!nssym) { error(0, "Symbol \"%s\" Not found", symbol); } value = NSAddressOfSymbol(nssym); free(malloc_sym); } else { error(-1, "Unable to allocate memory"); } return value; }
static void *dlsym(void *handle, const char *symbol) { char _symbol[256]; NSSymbol *nsSymbol= 0; snprintf(_symbol, sizeof(_symbol), "_%s", symbol); dprintf((stderr, "dlsym: looking for %s (%s) in %d\n", symbol, _symbol, (int)handle)); if (!handle) { dprintf((stderr, "dlsym: setting app context for this handle\n")); handle= DL_APP_CONTEXT; } if (DL_APP_CONTEXT == handle) { dprintf((stderr, "dlsym: looking in app context\n")); if (NSIsSymbolNameDefined(_symbol)) nsSymbol= NSLookupAndBindSymbol(_symbol); } else { if (( (MH_MAGIC == ((struct mach_header *)handle)->magic)) /* ppc */ || (MH_CIGAM == ((struct mach_header *)handle)->magic)) /* 386 */ { if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, _symbol)) { nsSymbol= NSLookupSymbolInImage ((struct mach_header *)handle, _symbol, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND /*| NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR*/); dprintf((stderr, "dlsym: bundle (image) lookup returned %p\n", nsSymbol)); } else dprintf((stderr, "dlsym: bundle (image) symbol not defined\n")); } else { nsSymbol= NSLookupSymbolInModule(handle, _symbol); dprintf((stderr, "dlsym: dylib (module) lookup returned %p\n", nsSymbol)); } } if (!nsSymbol) { dlSetError("symbol not found: %s", _symbol); return 0; } return NSAddressOfSymbol(nsSymbol); }
static void *wx_darwin_dlsym(void *handle, const char *symbol) { // as on many other systems, C symbols have prepended underscores under // Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not // aware of this wxCharBuffer buf(strlen(symbol) + 1); char *p = buf.data(); p[0] = '_'; strcpy(p + 1, symbol); NSSymbol nsSymbol = NSLookupSymbolInModule((NSModule)handle, p ); return nsSymbol ? NSAddressOfSymbol(nsSymbol) : NULL; }
void *vmddlsym( void *handle, const char *symname ) { char *realsymname; NSModule module; NSSymbol sym; /* Hack around the leading underscore in the symbol name */ realsymname = (char *)malloc(strlen(symname)+2); strcpy(realsymname, "_"); strcat(realsymname, symname); module = (NSModule)handle; sym = NSLookupSymbolInModule(module, realsymname); free(realsymname); if (sym) return (void *)(NSAddressOfSymbol(sym)); return NULL; }
int findSharedSymbol (void *object, const char *symbol, void *pointerAddress) { NSModule module = object; char name[strlen(symbol) + 2]; snprintf(name, sizeof(name), "_%s", symbol); { NSSymbol sym = NSLookupSymbolInModule(module, name); if (sym) { void **address = pointerAddress; *address = NSAddressOfSymbol(sym); return 1; } } return 0; }
/* A function called through the vtable to get the address of a symbol loaded from a particular module. */ static void * vm_sym (lt_user_data loader_data, lt_module module, const char *name) { NSSymbol *nssym = 0; const mach_header *mh = (const mach_header *) module; char saveError[256] = "Symbol not found"; if (module == (lt_module) -1) { void *address, *unused; _dyld_lookup_and_bind (name, (unsigned long*) &address, &unused); return address; } if (mh->magic == LT__MAGIC) { if (lt__image_symbol_p && lt__image_symbol) { if (lt__image_symbol_p (mh, name)) { nssym = lt__image_symbol (mh, name, LT__SYMLOOKUP_OPTS); } } } else { nssym = NSLookupSymbolInModule (module, name); } if (!nssym) { strncpy (saveError, dylderror (LT__STRERROR (SYMBOL_NOT_FOUND)), 255); saveError[255] = 0; if (!mh) { mh = (mach_header *)lt__nsmodule_get_header (module); } nssym = lt__linkedlib_symbol (name, mh); } if (!nssym) { LT__SETERRORSTR (saveError); } return nssym ? NSAddressOfSymbol (nssym) : 0; }
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; }
/** * Looks up a symbol from a dynamically loaded library * * This function queries a loaded library for a symbol specified in a * string, and returns a pointer to it. We don't check for dlerror() or * similar functions, since we want a non-NULL symbol anyway. * * @param handle handle to the module * @param psz_function function name * @return NULL on error, or the address of the symbol */ static void *module_Lookup( module_handle_t handle, const char *psz_function ) { #if defined(HAVE_DL_DYLD) char psz_call[strlen( psz_function ) + 2]; psz_call[0] = '_'; memcpy( psz_call + 1, psz_function, sizeof( psz_call ) - 1 ); NSSymbol sym = NSLookupSymbolInModule( handle, psz_call ); return NSAddressOfSymbol( sym ); #elif defined(HAVE_DL_BEOS) void * p_symbol; if( B_OK == get_image_symbol( handle, psz_function, B_SYMBOL_TYPE_TEXT, &p_symbol ) ) { return p_symbol; } else { return NULL; } #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE) wchar_t wide[strlen( psz_function ) + 1]; size_t i = 0; do wide[i] = psz_function[i]; /* UTF-16 <- ASCII */ while( psz_function[i++] ); return (void *)GetProcAddress( handle, wide ); #elif defined(HAVE_DL_WINDOWS) && defined(WIN32) return (void *)GetProcAddress( handle, (char *)psz_function ); #elif defined(HAVE_DL_DLOPEN) return dlsym( handle, psz_function ); #elif defined(HAVE_DL_SHL_LOAD) void *p_sym; shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym ); return p_sym; #endif }
void *resolve_link_symbol( void *info, const char *name ) { char *temp; NSModule mp = (NSModule)info; void *addr; NSSymbol sym; temp = alloca( strlen( name ) + 2 ); sprintf( temp, "_%s", name ); sym = NSLookupSymbolInModule( mp, temp ); if (!sym) { return NULL; } addr = NSAddressOfSymbol( sym ); return addr; }
/* dlsymIntern is used by dlsym to find the symbol */ void *dlsymIntern(void *handle, const char *symbol) { NSSymbol *nssym = 0; /* If the handle is -1, if is the app global context */ if (handle == (void *)-1) { /* Global context, use NSLookupAndBindSymbol */ if (NSIsSymbolNameDefined(symbol)) { nssym = NSLookupAndBindSymbol(symbol); } } /* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image for libraries, and NSLookupSymbolInModule for bundles */ else { /* Check for both possible magic numbers depending on x86/ppc byte order */ if ((((struct mach_header *)handle)->magic == MH_MAGIC) || (((struct mach_header *)handle)->magic == MH_CIGAM)) { if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol)) { nssym = NSLookupSymbolInImage((struct mach_header *)handle, symbol, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } } else { nssym = NSLookupSymbolInModule(handle, symbol); } } if (!nssym) { error(0, "Symbol \"%s\" Not found", symbol); return NULL; } return NSAddressOfSymbol(nssym); }
static void * _module_getsymbol( module_handle_t handle, const char * psz_function ) { #if defined(HAVE_DL_DYLD) NSSymbol sym = NSLookupSymbolInModule( handle, psz_function ); return NSAddressOfSymbol( sym ); #elif defined(HAVE_DL_BEOS) void * p_symbol; if( B_OK == get_image_symbol( handle, psz_function, B_SYMBOL_TYPE_TEXT, &p_symbol ) ) { return p_symbol; } else { return NULL; } #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE) wchar_t psz_real[256]; MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 ); return (void *)GetProcAddress( handle, psz_real ); #elif defined(HAVE_DL_WINDOWS) && defined(WIN32) return (void *)GetProcAddress( handle, (char *)psz_function ); #elif defined(HAVE_DL_DLOPEN) return dlsym( handle, psz_function ); #elif defined(HAVE_DL_SHL_LOAD) void *p_sym; shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym ); return p_sym; #endif }
static void *_CFBundleDYLDGetSymbolByNameWithSearch(CFBundleRef bundle, CFStringRef symbolName, Boolean globalSearch) { void *result = NULL; char buff[1026]; NSSymbol symbol = NULL; buff[0] = '_'; if (CFStringGetCString(symbolName, &(buff[1]), 1024, kCFStringEncodingUTF8)) { if (bundle->_moduleCookie) { symbol = NSLookupSymbolInModule((NSModule)(bundle->_moduleCookie), buff); } else if (bundle->_imageCookie) { symbol = NSLookupSymbolInImage(bundle->_imageCookie, buff, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND|NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } if (!symbol && !bundle->_moduleCookie && (!bundle->_imageCookie || globalSearch)) { char hintBuff[1026]; CFStringRef executableName = _CFBundleCopyExecutableName(bundle, NULL, NULL); hintBuff[0] = '\0'; if (executableName) { if (!CFStringGetCString(executableName, hintBuff, 1024, kCFStringEncodingUTF8)) hintBuff[0] = '\0'; CFRelease(executableName); } // Nowdays, NSIsSymbolNameDefinedWithHint() and NSLookupAndBindSymbolWithHint() // are identical, except the first just returns a bool, so checking with the // Is function first just causes a redundant lookup. // This returns NULL on failure. symbol = NSLookupAndBindSymbolWithHint(buff, hintBuff); } if (symbol) result = NSAddressOfSymbol(symbol); #if defined(DEBUG) if (!result) CFLog(__kCFLogBundle, CFSTR("dyld cannot find symbol %@ in %@"), symbolName, bundle); #endif /* DEBUG */ #if LOG_BUNDLE_LOAD printf("bundle %p handle %p module %p image %p dyld returns symbol %p for %s\n", bundle, bundle->_handleCookie, bundle->_moduleCookie, bundle->_imageCookie, result, buff + 1); #endif /* LOG_BUNDLE_LOAD */ } return result; }
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 * 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; }
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; }
APR_DECLARE(apr_status_t) apr_dso_sym(apr_dso_handle_sym_t *ressym, apr_dso_handle_t *handle, const char *symname) { #if defined(DSO_USE_SHL) void *symaddr = NULL; int status; errno = 0; status = shl_findsym((shl_t *)&handle->handle, symname, TYPE_PROCEDURE, &symaddr); if (status == -1 && errno == 0) /* try TYPE_DATA instead */ status = shl_findsym((shl_t *)&handle->handle, symname, TYPE_DATA, &symaddr); if (status == -1) return APR_ESYMNOTFOUND; *ressym = symaddr; return APR_SUCCESS; #elif defined(DSO_USE_DYLD) void *retval = NULL; NSSymbol symbol; char *symname2 = (char*)malloc(sizeof(char)*(strlen(symname)+2)); sprintf(symname2, "_%s", symname); #ifdef NSLINKMODULE_OPTION_PRIVATE if (handle->handle == DYLD_LIBRARY_HANDLE) { symbol = NSLookupAndBindSymbol(symname2); } else { symbol = NSLookupSymbolInModule((NSModule)handle->handle, symname2); } #else symbol = NSLookupAndBindSymbol(symname2); #endif free(symname2); if (symbol == NULL) { handle->errormsg = "undefined symbol"; return APR_ESYMNOTFOUND; } retval = NSAddressOfSymbol(symbol); if (retval == NULL) { handle->errormsg = "cannot resolve symbol"; return APR_ESYMNOTFOUND; } *ressym = retval; return APR_SUCCESS; #elif defined(DSO_USE_DLFCN) #if defined(DLSYM_NEEDS_UNDERSCORE) void *retval; char *symbol = (char*)malloc(sizeof(char)*(strlen(symname)+2)); sprintf(symbol, "_%s", symname); retval = dlsym(handle->handle, symbol); free(symbol); #elif defined(SEQUENT) || defined(SNI) void *retval = dlsym(handle->handle, (char *)symname); #else void *retval = dlsym(handle->handle, symname); #endif /* DLSYM_NEEDS_UNDERSCORE */ if (retval == NULL) { handle->errormsg = dlerror(); return APR_ESYMNOTFOUND; } *ressym = retval; return APR_SUCCESS; #endif /* DSO_USE_x */ }
MODULE_SCOPE Tcl_PackageInitProc * TclpFindSymbol( Tcl_Interp *interp, /* For error reporting. */ Tcl_LoadHandle loadHandle, /* Handle from TclpDlopen. */ CONST char *symbol) /* Symbol name to look up. */ { Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle; Tcl_PackageInitProc *proc = NULL; const char *errMsg = NULL; Tcl_DString ds; const char *native; native = Tcl_UtfToExternalDString(NULL, symbol, -1, &ds); #if TCL_DYLD_USE_DLFCN if (dyldLoadHandle->dlHandle) { proc = dlsym(dyldLoadHandle->dlHandle, native); if (proc) { TclLoadDbgMsg("dlsym() successful"); } else { errMsg = dlerror(); TclLoadDbgMsg("dlsym() failed: %s", errMsg); } } else #endif /* TCL_DYLD_USE_DLFCN */ { #if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY) NSSymbol nsSymbol = NULL; Tcl_DString newName; /* * dyld adds an underscore to the beginning of symbol names. */ Tcl_DStringInit(&newName); Tcl_DStringAppend(&newName, "_", 1); native = Tcl_DStringAppend(&newName, native, -1); if (dyldLoadHandle->dyldLibHeader) { nsSymbol = NSLookupSymbolInImage(dyldLoadHandle->dyldLibHeader, native, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); if (nsSymbol) { TclLoadDbgMsg("NSLookupSymbolInImage() successful"); #ifdef DYLD_SUPPORTS_DYLIB_UNLOADING /* * Until dyld supports unloading of MY_DYLIB binaries, the * following is not needed. */ NSModule module = NSModuleForSymbol(nsSymbol); Tcl_DyldModuleHandle *modulePtr = dyldLoadHandle->modulePtr; while (modulePtr != NULL) { if (module == modulePtr->module) { break; } modulePtr = modulePtr->nextPtr; } if (modulePtr == NULL) { modulePtr = (Tcl_DyldModuleHandle *) ckalloc(sizeof(Tcl_DyldModuleHandle)); modulePtr->module = module; modulePtr->nextPtr = dyldLoadHandle->modulePtr; dyldLoadHandle->modulePtr = modulePtr; } #endif /* DYLD_SUPPORTS_DYLIB_UNLOADING */ } else { NSLinkEditErrors editError; int errorNumber; const char *errorName; NSLinkEditError(&editError, &errorNumber, &errorName, &errMsg); TclLoadDbgMsg("NSLookupSymbolInImage() failed: %s", errMsg); } } else if (dyldLoadHandle->modulePtr) { nsSymbol = NSLookupSymbolInModule( dyldLoadHandle->modulePtr->module, native); if (nsSymbol) { TclLoadDbgMsg("NSLookupSymbolInModule() successful"); } else { TclLoadDbgMsg("NSLookupSymbolInModule() failed"); } } if (nsSymbol) { proc = NSAddressOfSymbol(nsSymbol); if (proc) { TclLoadDbgMsg("NSAddressOfSymbol() successful"); } else { TclLoadDbgMsg("NSAddressOfSymbol() failed"); } } Tcl_DStringFree(&newName); #endif /* TCL_DYLD_USE_NSMODULE */ } Tcl_DStringFree(&ds); if (errMsg) { Tcl_AppendResult(interp, errMsg, NULL); } return proc; }
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; }