static int LinkCarbonFunctions( void ) { static int carbon_linked = 0; if ( !carbon_linked ) { if ( ( NSIsSymbolNameDefined( "_GetCurrentProcess" ) ) && ( NSIsSymbolNameDefined( "_CopyProcessName" ) ) ) { NSSymbol getProcSym = NULL, copyNameSym = NULL; getProcSym = NSLookupAndBindSymbolWithHint( "_GetCurrentProcess", "Carbon" ); copyNameSym = NSLookupAndBindSymbolWithHint( "_CopyProcessName", "Carbon" ); if ( ( getProcSym != NULL ) && ( copyNameSym != NULL ) ) { my_GetCurrentProcess = NSAddressOfSymbol( getProcSym ); my_CopyProcessName = NSAddressOfSymbol( copyNameSym ); // absolute paranoia if ( ( my_GetCurrentProcess != NULL ) && ( my_CopyProcessName != NULL ) ) { carbon_linked = 1; } } } } return ( carbon_linked ); }
static short OpenResourceMap( CFBundleRef bundleRef) { static int initialized = FALSE; static short (*openresourcemap)(CFBundleRef) = NULL; if (!initialized) { #if TCL_DYLD_USE_DLFCN #if MAC_OS_X_VERSION_MIN_REQUIRED < 1040 if (tclMacOSXDarwinRelease >= 8) #endif { openresourcemap = dlsym(RTLD_NEXT, "CFBundleOpenBundleResourceMap"); #ifdef TCL_DEBUG_LOAD if (!openresourcemap) { const char *errMsg = dlerror(); TclLoadDbgMsg("dlsym() failed: %s", errMsg); } #endif /* TCL_DEBUG_LOAD */ } if (!openresourcemap) #endif /* TCL_DYLD_USE_DLFCN */ { #if TCL_DYLD_USE_NSMODULE if (NSIsSymbolNameDefinedWithHint( "_CFBundleOpenBundleResourceMap", "CoreFoundation")) { NSSymbol nsSymbol = NSLookupAndBindSymbolWithHint( "_CFBundleOpenBundleResourceMap", "CoreFoundation"); if (nsSymbol) { openresourcemap = NSAddressOfSymbol(nsSymbol); } } #endif /* TCL_DYLD_USE_NSMODULE */ } initialized = TRUE; } if (openresourcemap) { return openresourcemap(bundleRef); } return -1; }
extern "C" void DebugAssert( OSType componentSignature, UInt32 options, const char * assertionString, const char * exceptionString, const char * errorString, const char * fileName, long lineNumber, void * value) { typedef void (*DAProc)(OSType, UInt32, const char *, const char *, const char *, const char *, long, void *); static DAProc realDAProc = NULL; if (realDAProc == NULL) realDAProc = (DAProc)NSAddressOfSymbol(NSLookupAndBindSymbolWithHint("_DebugAssert", "CoreServices")); if (realDAProc) (*realDAProc)(componentSignature, options, assertionString, exceptionString, errorString, fileName, lineNumber, value); else *(long *)0 = 0; // bus error }
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 Tcl_MacOSXOpenVersionedBundleResources( Tcl_Interp *interp, const char *bundleName, const char *bundleVersion, int hasResourceFile, int maxPathLen, char *libraryPath) { #ifdef HAVE_COREFOUNDATION CFBundleRef bundleRef, versionedBundleRef = NULL; CFStringRef bundleNameRef; CFURLRef libURL; libraryPath[0] = '\0'; bundleNameRef = CFStringCreateWithCString(NULL, bundleName, kCFStringEncodingUTF8); bundleRef = CFBundleGetBundleWithIdentifier(bundleNameRef); CFRelease(bundleNameRef); if (bundleVersion && bundleRef) { /* * Create bundle from bundleVersion subdirectory of 'Versions'. */ CFURLRef bundleURL = CFBundleCopyBundleURL(bundleRef); if (bundleURL) { CFStringRef bundleVersionRef = CFStringCreateWithCString(NULL, bundleVersion, kCFStringEncodingUTF8); if (bundleVersionRef) { CFStringRef bundleTailRef = CFURLCopyLastPathComponent( bundleURL); if (bundleTailRef) { if (CFStringCompare(bundleTailRef, bundleVersionRef, 0) == kCFCompareEqualTo) { versionedBundleRef = (CFBundleRef) CFRetain(bundleRef); } CFRelease(bundleTailRef); } if (!versionedBundleRef) { CFURLRef versURL = CFURLCreateCopyAppendingPathComponent( NULL, bundleURL, CFSTR("Versions"), TRUE); if (versURL) { CFURLRef versionedBundleURL = CFURLCreateCopyAppendingPathComponent( NULL, versURL, bundleVersionRef, TRUE); if (versionedBundleURL) { versionedBundleRef = CFBundleCreate(NULL, versionedBundleURL); CFRelease(versionedBundleURL); } CFRelease(versURL); } } CFRelease(bundleVersionRef); } CFRelease(bundleURL); } if (versionedBundleRef) { bundleRef = versionedBundleRef; } } if (bundleRef) { if (hasResourceFile) { /* * Dynamically acquire address for CFBundleOpenBundleResourceMap * symbol, since it is only present in full CoreFoundation on Mac * OS X and not in CFLite on pure Darwin. */ static int initialized = FALSE; static short (*openresourcemap)(CFBundleRef) = NULL; if (!initialized) { #if TCL_DYLD_USE_DLFCN #if MAC_OS_X_VERSION_MIN_REQUIRED < 1040 if (tclMacOSXDarwinRelease >= 8) #endif { const char *errMsg = nil; openresourcemap = dlsym(RTLD_NEXT, "CFBundleOpenBundleResourceMap"); if (!openresourcemap) { errMsg = dlerror(); TclLoadDbgMsg("dlsym() failed: %s", errMsg); } } if (!openresourcemap) #endif { #if TCL_DYLD_USE_NSMODULE NSSymbol nsSymbol = NULL; if (NSIsSymbolNameDefinedWithHint( "_CFBundleOpenBundleResourceMap", "CoreFoundation")) { nsSymbol = NSLookupAndBindSymbolWithHint( "_CFBundleOpenBundleResourceMap", "CoreFoundation"); if (nsSymbol) { openresourcemap = NSAddressOfSymbol(nsSymbol); } } #endif } initialized = TRUE; } if (openresourcemap) { short refNum; refNum = openresourcemap(bundleRef); } } libURL = CFBundleCopyResourceURL(bundleRef, CFSTR("Scripts"), NULL, NULL); if (libURL) { /* * FIXME: This is a quick fix, it is probably not right for * internationalization. */ CFURLGetFileSystemRepresentation(libURL, TRUE, (unsigned char*) libraryPath, maxPathLen); CFRelease(libURL); } if (versionedBundleRef) { CFRelease(versionedBundleRef); } } if (libraryPath[0]) { return TCL_OK; } else { return TCL_ERROR; } #else /* HAVE_COREFOUNDATION */ return TCL_ERROR; #endif /* HAVE_COREFOUNDATION */ }
static void *LinkSymbol(const char *symname, const char *fwhint) { return NSAddressOfSymbol(NSLookupAndBindSymbolWithHint(symname, fwhint)); }