void* NSGLGetProcAddress (const GLubyte *name) { NSSymbol symbol; char* symbolName; /* prepend a '_' for the Unix C symbol mangling convention */ symbolName = MALLOC(strlen((const char*)name) + 2); strcpy(symbolName+1, (const char*)name); symbolName[0] = '_'; symbol = NULL; if (NSIsSymbolNameDefined(symbolName)) symbol = NSLookupAndBindSymbol(symbolName); free(symbolName); return symbol ? NSAddressOfSymbol(symbol) : NULL; }
int main () { fputs ("hi\n", stdout); fputs ("there\n", stdout); puts ("test"); /* this call will always go to libSystem, even though it is defined in mylib.dylib */ if (NSIsSymbolNameDefined ("_foo")) { int (*addr)(void) = NSAddressOfSymbol (NSLookupAndBindSymbol ("_foo")); if (addr) { addr (); } } }
void* GLExtHandler::SPK_NSGLGetProcAddress(const char* name) { NSSymbol symbol; char *symbolName; symbolName = (char*)malloc (strlen (name) + 2); strcpy(symbolName + 1, name); symbolName[0] = '_'; symbol = NULL; if (NSIsSymbolNameDefined (symbolName)) symbol = NSLookupAndBindSymbol (symbolName); free (symbolName); return symbol ? NSAddressOfSymbol (symbol) : NULL; }
static void * mydlsym(char *symbol) { void *addr; char funcname[256]; #if HAVE_SNPRINTF snprintf(funcname, sizeof(funcname), "_%.200s", symbol); #else sprintf(funcname, "_%.200s", symbol); #endif if (NSIsSymbolNameDefined(funcname)) addr = NSAddressOfSymbol(NSLookupAndBindSymbol(funcname)); else addr = NULL; return addr; }
//---------------------------------------------------------------------------- void* GTGetFunctionPointer (const char* function) { NSSymbol symbol; char* symbolName; // Prepend a '_' for the Unix C symbol mangling convention. symbolName = (char*)malloc(strlen((const char*)function) + 2); strcpy(symbolName + 1,(const char*)function); symbolName[0] = '_'; symbol = 0; if (NSIsSymbolNameDefined(symbolName)) { symbol = NSLookupAndBindSymbol(symbolName); } free(symbolName); return symbol ? NSAddressOfSymbol(symbol) : 0; }
PGFunction pg_dlsym(void *handle, char *funcname) { NSSymbol symbol; char *symname = (char *) malloc(strlen(funcname) + 2); sprintf(symname, "_%s", funcname); if (NSIsSymbolNameDefined(symname)) { symbol = NSLookupAndBindSymbol(symname); free(symname); return (PGFunction) NSAddressOfSymbol(symbol); } else { free(symname); return NULL; } }
/* Safe to assume our mh is good. */ static NSSymbol lt__linkedlib_symbol (const char *symname, const mach_header *mh) { NSSymbol symbol = 0; if (lt__image_symbol && NSIsSymbolNameDefined (symname)) { unsigned long offset = sizeof(mach_header); struct load_command *lc; int j; for (j = 0; j < mh->ncmds; j++) { lc = (struct load_command*) (((unsigned long) mh) + offset); if ((LC_LOAD_DYLIB == lc->cmd) || (LC_LOAD_WEAK_DYLIB == lc->cmd)) { unsigned long base = ((dylib_command *) lc)->dylib.name.offset; char *name = (char *) (base + (unsigned long) lc); const mach_header *mh1 = lt__match_loadedlib (name); if (!mh1) { /* Maybe NSAddImage can find it */ mh1 = lt__addimage (name, NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED | NSADDIMAGE_OPTION_WITH_SEARCHING | NSADDIMAGE_OPTION_RETURN_ON_ERROR); } if (mh1) { symbol = lt__image_symbol (mh1, symname, LT__SYMLOOKUP_OPTS); if (symbol) break; } } offset += lc->cmdsize; } } return symbol; }
/* 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* LookupSymbol(const mach_header* aLib, const char* aSymbolName) { // Try to use |NSLookupSymbolInImage| since it is faster than searching // the global symbol table. If we couldn't get a mach_header pointer // for the XPCOM dylib, then use |NSLookupAndBindSymbol| to search the // global symbol table (this shouldn't normally happen, unless the user // has called XPCOMGlueStartup(".") and relies on libxpcom.dylib // already being loaded). NSSymbol sym = nsnull; if (aLib) { sym = NSLookupSymbolInImage(aLib, aSymbolName, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } else { if (NSIsSymbolNameDefined(aSymbolName)) sym = NSLookupAndBindSymbol(aSymbolName); } if (!sym) return nsnull; return NSAddressOfSymbol(sym); }
void* dlsym(void* handle, char* symbol) { if (handle == &dl_self) { if (NSIsSymbolNameDefined(symbol)) { NSSymbol retsym; retsym = NSLookupAndBindSymbol(symbol); return NSAddressOfSymbol(retsym); } else { last_error = DLSYM_ERROR; return NULL; } } else { if (NSIsSymbolNameDefinedInImage(handle, symbol)) { NSSymbol retsym; retsym = NSLookupSymbolInImage(handle, symbol, 0); return NSAddressOfSymbol(retsym); } else { last_error = DLSYM_ERROR; return NULL; } } }
static int SetupForObjcLookup( void ) { if ( !LinkedObjcRuntime ) { // see if libobjc is loaded if ( NSIsSymbolNameDefined( "_objc_getClass" ) ) { _dyld_lookup_and_bind_with_hint( "_objc_getClass", "libobjc", (void **) &my_objc_getClass, NULL ); _dyld_lookup_and_bind_with_hint( "_class_getInstanceMethod", "libobjc", (void **) &my_class_getInstanceMethod, NULL ); _dyld_lookup_and_bind_with_hint( "_class_getClassMethod", "libobjc", (void **) &my_class_getClassMethod, NULL ); _dyld_lookup_and_bind_with_hint( "_sel_getUid", "libobjc", (void **) &my_sel_getUid, NULL ); if ( my_objc_getClass != NULL ) LinkedObjcRuntime = 1; } } return ( LinkedObjcRuntime ); }
Togl_FuncPtr Togl_GetProcAddr(const char *funcname) { #if defined(TOGL_OSMESA) return (Togl_FuncPtr) OSMesaGetProcAddress(funcname); #elif defined(TOGL_WGL) return (Togl_FuncPtr) wglGetProcAddress(funcname); #elif defined(__APPLE__) char buf[256]; snprintf(buf, sizeof buf - 1, "_%s", funcname); buf[sizeof buf - 1] = '\0'; if (NSIsSymbolNameDefined(buf)) { NSSymbol nssym; nssym = NSLookupAndBindSymbol(buf); if (nssym) return (Togl_FuncPtr) NSAddressOfSymbol(nssym); } return NULL; #else # if defined(TOGL_X11) && defined(GLX_VERSION_1_4) /* Strictly speaking, we can only call glXGetProcAddress if glXQueryVersion * says we're using version 1.4 or later. */ return (Togl_FuncPtr) glXGetProcAddress(funcname); # else /* Linux, IRIX, OSF/1, ? */ static void *dlHandle = NULL; if (dlHandle == NULL) dlHandle = dlopen(NULL, RTLD_LAZY); /* Strictly speaking, the following cast of a data pointer to a function * pointer is not legal in ISO C, but we don't have any choice. */ return (Togl_FuncPtr) dlsym(dlHandle, funcname); # endif #endif }
/* * dyld adds libraries by first adding the directly dependant libraries in link order, and * then adding the dependencies for those libraries, so we should do the same... but we don't * bother adding the extra dependencies, if the symbols are neither in the loaded image nor * any of it's direct dependencies, then it probably isn't there. */ NSSymbol *search_linked_libs(const struct mach_header * mh, const char *symbol) { unsigned int n; struct load_command *lc = 0; struct mach_header *wh; NSSymbol *nssym = 0; if (dyld_NSAddImage && dyld_NSIsSymbolNameDefinedInImage && dyld_NSLookupSymbolInImage) { lc = (struct load_command *)((char *)mh + sizeof(struct mach_header)); for (n = 0; n < mh->ncmds; n++, lc = (struct load_command *)((char *)lc + lc->cmdsize)) { if ((LC_LOAD_DYLIB == lc->cmd) || (LC_LOAD_WEAK_DYLIB == lc->cmd)) { if ((wh = (struct mach_header *) my_find_image((char *)(((struct dylib_command *)lc)->dylib.name.offset + (char *)lc)))) { if (dyld_NSIsSymbolNameDefinedInImage(wh, symbol)) { nssym = dyld_NSLookupSymbolInImage(wh, symbol, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); break; } } } } if ((!nssym) && NSIsSymbolNameDefined(symbol)) { /* I've never seen this debug message...*/ debug("Symbol \"%s\" is defined but was not found", symbol); } } return nssym; }
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; }
static void *dlsymIntern(struct dlstatus *dls, const char *symbol, int canSetError) { NSSymbol *nssym = 0; #ifdef __GCC__ void *caller = __builtin_return_address(1); /* Be *very* careful about inlining */ #else void *caller = NULL; #endif const struct mach_header *caller_mh = 0; const char* savedErrorStr = NULL; resetdlerror(); #ifndef RTLD_SELF #define RTLD_SELF ((void *) -3) #endif if (NULL == dls) dls = RTLD_SELF; if ((RTLD_NEXT == dls) || (RTLD_SELF == dls)) { if (dyld_NSIsSymbolNameDefinedInImage && dyld_NSLookupSymbolInImage) { caller_mh = image_for_address(caller); if (RTLD_SELF == dls) { /* FIXME: We should be using the NSModule api, if SELF is an MH_BUNDLE * But it appears to work anyway, and looking at the code in dyld_libfuncs.c * this is acceptable. */ if (dyld_NSIsSymbolNameDefinedInImage(caller_mh, symbol)) { nssym = dyld_NSLookupSymbolInImage(caller_mh, symbol, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } } if (!nssym) { if (RTLD_SELF == dls) savedErrorStr = dyld_error_str(); nssym = search_linked_libs(caller_mh, symbol); } } else { if (canSetError) error("RTLD_SELF and RTLD_NEXT are not supported"); return NULL; } } if (!nssym) { if (RTLD_DEFAULT == dls) { dls = &mainStatus; } if (!isValidStatus(dls)) return NULL; if (dls->module != MAGIC_DYLIB_MOD) { nssym = NSLookupSymbolInModule(dls->module, symbol); if (!nssym && NSIsSymbolNameDefined(symbol)) { debug("Searching dependencies"); savedErrorStr = dyld_error_str(); nssym = search_linked_libs(get_mach_header_from_NSModule(dls->module), symbol); } } else if (dls->lib && dyld_NSIsSymbolNameDefinedInImage && dyld_NSLookupSymbolInImage) { if (dyld_NSIsSymbolNameDefinedInImage(dls->lib, symbol)) { nssym = dyld_NSLookupSymbolInImage(dls->lib, symbol, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } else if (NSIsSymbolNameDefined(symbol)) { debug("Searching dependencies"); savedErrorStr = dyld_error_str(); nssym = search_linked_libs(dls->lib, symbol); } } else if (dls->module == MAGIC_DYLIB_MOD) { /* Global context, use NSLookupAndBindSymbol */ if (NSIsSymbolNameDefined(symbol)) { /* There doesn't seem to be a return on error option for this call??? this is potentially broken, if binding fails, it will improperly exit the application. */ nssym = NSLookupAndBindSymbol(symbol); } else { if (savedErrorStr) free((char*)savedErrorStr); savedErrorStr = malloc(256); snprintf((char*)savedErrorStr, 256, "Symbol \"%s\" not in global context",symbol); } } } /* Error reporting */ if (!nssym) { if (!savedErrorStr || !strlen(savedErrorStr)) { if (savedErrorStr) free((char*)savedErrorStr); savedErrorStr = malloc(256); snprintf((char*)savedErrorStr, 256,"Symbol \"%s\" not found",symbol); } if (canSetError) { error(savedErrorStr); } else { debug(savedErrorStr); } if (savedErrorStr) free((char*)savedErrorStr); return NULL; } return NSAddressOfSymbol(nssym); }
void * cc_dl_sym(cc_libhandle handle, const char * symbolname) { void * ptr = NULL; #ifdef HAVE_DL_LIB if ((handle == NULL) || (handle->nativehnd == NULL)) return NULL; ptr = dlsym((void *)handle->nativehnd, symbolname); if (cc_dl_debugging()) { const char * e = dlerror(); if (e) { cc_debugerror_post("cc_dl_sym", "dlsym(\"%s\", \"%s\") failed with: '%s'", handle->libname.getString(), symbolname, e); } } #elif defined (HAVE_DYLD_RUNTIME_BINDING) /* Note: The dlopen() version returns NULL here if handle or handle->nativehnd are NULL, but we do not need a handle for symbol lookup on Mac OS X - if we have one, it makes the lookup faster, but that's all, so we can get away with having no valid handle. */ NSSymbol symbol = NULL; char * mangledname; NSLinkEditErrors c; int e; const char * file; const char * errstr; if (cc_dl_debugging()) { cc_debugerror_postinfo("cc_dl_sym", "Looking up symbol %s", symbolname); } mangledname = malloc(strlen(symbolname) + 2); strcpy(mangledname + 1, symbolname); mangledname[0] = '_'; if (handle && handle->nativehnd) { if (NSIsSymbolNameDefinedInImage(handle->nativehnd, mangledname)) symbol = NSLookupSymbolInImage(handle->nativehnd, mangledname, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } /* If we did not specifically load the library ourselves (handle->nativehnd being NULL), or if the symbol could not be found in the library, let's try if we can find it in any of the loaded libs. */ if (!symbol && NSIsSymbolNameDefined(mangledname)) { symbol = NSLookupAndBindSymbol(mangledname); } if (cc_dl_debugging()) { if (symbol == NULL) { NSLinkEditError(&c, &e, &file, &errstr); cc_debugerror_post("cc_dl_sym", "symbol %s not found: %s", symbolname, errstr); } } free (mangledname); ptr = symbol ? NSAddressOfSymbol(symbol) : NULL; #elif defined (HAVE_WINDLL_RUNTIME_BINDING) if ((handle == NULL) || (handle->nativehnd == NULL)) return NULL; ptr = dl_internal::cstyle_cast<void *>(GetProcAddress((HINSTANCE) handle->nativehnd, symbolname)); if (cc_dl_debugging() && (ptr == NULL)) { cc_string funcstr; cc_string_construct(&funcstr); cc_string_sprintf(&funcstr, "GetProcAddress(\"%s\", \"%s\")", handle->libname.getString(), symbolname); cc_win32_print_error("cc_dl_sym", cc_string_get_text(&funcstr), GetLastError()); cc_string_clean(&funcstr); } #elif defined (HAVE_DLD_LIB) { int retval = shl_findsym((shl_t *)(&handle->nativehnd), symbolname, TYPE_UNDEFINED, &ptr); if (cc_dl_debugging() && (retval == -1)) { const char * e = strerror(errno); cc_debugerror_post("cc_dl_sym", "shl_findsym(\"%s\", \"%s\", ...) failed with: '%s'", handle->libname.getString(), symbolname, e); } } #endif return ptr; }
wxString GetApplicationPath() { static bool found = false; static wxString path; if (!found) { /* Windows */ #ifdef __WXMSW__ wxChar buf[512] = wxT(""); GetModuleFileName(NULL, buf, 511); path = wxString(buf, wxConvUTF8); /* UNIX & MAC*/ #else wxString argv0; # ifdef __WXMAC__ if (NSIsSymbolNameDefined("__NSGetExecutablePath")) { char buf[512]; size_t bufLen = 512; buf[0] = 0; ((NSGetExecutablePathProcPtr) NSAddressOfSymbol(NSLookupAndBindSymbol("__NSGetExecutablePath")))(buf, &bufLen); wxString strBuf = wxString(); size_t actualBuflen = strlen(buf); if (actualBuflen > 0) { // FIXME: we *assume* that the NS stuff returns utf-8 encoded strings path = wxString(buf, wxConvUTF8); found=true; return path; } } # endif argv0 = wxTheApp->argv[0]; /* check absolute path */ if (wxIsAbsolutePath(argv0)) { path = argv0; } else { /* check relative path */ wxString fname = wxGetCwd() + wxFILE_SEP_PATH + argv0; if (wxFileExists(fname)) { path = fname; } else { /* find on PATH */ wxPathList pathlist; pathlist.AddEnvList(wxT("PATH")); path = pathlist.FindAbsoluteValidPath(argv0); } } wxFileName filename(path); filename.Normalize(); path = filename.GetFullPath(); #endif found = true; } return path; }
static void * dlsymIntern(struct dlstatus * dls, const char *symbol,int canSetError) { NSSymbol * nssym = 0; /* If it is a module - use NSLookupSymbolInModule */ if (dls->module != MAGIC_DYLIB_MOD) { nssym = NSLookupSymbolInModule(dls->module, symbol); if (!nssym && canSetError) error("unable to find symbol \"%s\"", symbol); } else { if (dls->lib != NULL) { /* dylib, use NSIsSymbolNameDefinedInImage */ if (NSIsSymbolNameDefinedInImage(dls->lib,symbol)) { nssym = NSLookupSymbolInImage(dls->lib, symbol, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR ); } } else { /* Global context, use NSLookupAndBindSymbol */ if (NSIsSymbolNameDefined(symbol)) { /* There doesn't seem to be a return on error option for this call??? this is potentially broken, if binding fails, it will improperly exit the application. If anyone comes up with an example of this happening I will install error handlers before making this call, and remove them afterwards. The error handlers themselves can cause problems as they are global to the process, and errors that have nothing to do with dlcompat could cause the error handlers to get called */ nssym = NSLookupAndBindSymbol(symbol); } } if (!nssym) { NSLinkEditErrors ler; int lerno; const char* errstr; const char* file; NSLinkEditError(&ler,&lerno,&file,&errstr); if (errstr && strlen(errstr)) { if (canSetError) error(errstr); debug("%s",errstr); } else if (canSetError) { error("unable to find symbol \"%s\"", symbol); } } } if (!nssym) return NULL; return NSAddressOfSymbol(nssym); }