diff options
Diffstat (limited to 'unix/tclLoadDyld.c')
-rw-r--r-- | unix/tclLoadDyld.c | 94 |
1 files changed, 57 insertions, 37 deletions
diff --git a/unix/tclLoadDyld.c b/unix/tclLoadDyld.c index 08d651c..4fa1954 100644 --- a/unix/tclLoadDyld.c +++ b/unix/tclLoadDyld.c @@ -84,13 +84,23 @@ MODULE_SCOPE long tclMacOSXDarwinRelease; #endif #ifdef TCL_DEBUG_LOAD -#define TclLoadDbgMsg(m, ...) do { \ - fprintf(stderr, "%s:%d: %s(): " m ".\n", \ - strrchr(__FILE__, '/')+1, __LINE__, __func__, ##__VA_ARGS__); \ - } while (0) +#define TclLoadDbgMsg(m, ...) \ + do { \ + fprintf(stderr, "%s:%d: %s(): " m ".\n", \ + strrchr(__FILE__, '/')+1, __LINE__, __func__, \ + ##__VA_ARGS__); \ + } while (0) #else #define TclLoadDbgMsg(m, ...) #endif + +/* + * Static functions defined in this file. + */ + +static void * FindSymbol(Tcl_Interp *interp, + Tcl_LoadHandle loadHandle, const char *symbol); +static void UnloadFile(Tcl_LoadHandle handle); #if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY) /* @@ -110,7 +120,7 @@ MODULE_SCOPE long tclMacOSXDarwinRelease; *---------------------------------------------------------------------- */ -static CONST char* +static const char * DyldOFIErrorMsg( int err) { @@ -165,6 +175,7 @@ TclpDlopen( * file. */ { Tcl_DyldLoadHandle *dyldLoadHandle; + Tcl_LoadHandle newHandle; #if TCL_DYLD_USE_DLFCN void *dlHandle = NULL; #endif @@ -269,8 +280,7 @@ TclpDlopen( | NSLINKMODULE_OPTION_RETURN_ON_ERROR); NSDestroyObjectFileImage(dyldObjFileImage); if (module) { - modulePtr = (Tcl_DyldModuleHandle *) - ckalloc(sizeof(Tcl_DyldModuleHandle)); + modulePtr = ckalloc(sizeof(Tcl_DyldModuleHandle)); modulePtr->module = module; modulePtr->nextPtr = NULL; TclLoadDbgMsg("NSLinkModule() successful"); @@ -296,8 +306,7 @@ TclpDlopen( || dyldLibHeader || modulePtr #endif ) { - dyldLoadHandle = (Tcl_DyldLoadHandle *) - ckalloc(sizeof(Tcl_DyldLoadHandle)); + dyldLoadHandle = ckalloc(sizeof(Tcl_DyldLoadHandle)); #if TCL_DYLD_USE_DLFCN dyldLoadHandle->dlHandle = dlHandle; #endif @@ -305,8 +314,12 @@ TclpDlopen( dyldLoadHandle->dyldLibHeader = dyldLibHeader; dyldLoadHandle->modulePtr = modulePtr; #endif - *loadHandle = (Tcl_LoadHandle) dyldLoadHandle; - *unloadProcPtr = &TclpUnloadFile; + newHandle = ckalloc(sizeof(*newHandle)); + newHandle->clientData = dyldLoadHandle; + newHandle->findSymbolProcPtr = &FindSymbol; + newHandle->unloadFileProcPtr = &UnloadFile; + *unloadProcPtr = &UnloadFile; + *loadHandle = newHandle; result = TCL_OK; } else { Tcl_AppendResult(interp, errMsg, NULL); @@ -327,7 +340,7 @@ TclpDlopen( /* *---------------------------------------------------------------------- * - * TclpFindSymbol -- + * FindSymbol -- * * Looks up a symbol, by name, through a handle associated with a * previously loaded piece of code (shared library). @@ -340,13 +353,13 @@ TclpDlopen( *---------------------------------------------------------------------- */ -MODULE_SCOPE Tcl_PackageInitProc * -TclpFindSymbol( +static void * +FindSymbol( Tcl_Interp *interp, /* For error reporting. */ Tcl_LoadHandle loadHandle, /* Handle from TclpDlopen. */ - CONST char *symbol) /* Symbol name to look up. */ + const char *symbol) /* Symbol name to look up. */ { - Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle; + Tcl_DyldLoadHandle *dyldLoadHandle = loadHandle->clientData; Tcl_PackageInitProc *proc = NULL; const char *errMsg = NULL; Tcl_DString ds; @@ -398,8 +411,7 @@ TclpFindSymbol( modulePtr = modulePtr->nextPtr; } if (modulePtr == NULL) { - modulePtr = (Tcl_DyldModuleHandle *) - ckalloc(sizeof(Tcl_DyldModuleHandle)); + modulePtr = ckalloc(sizeof(Tcl_DyldModuleHandle)); modulePtr->module = module; modulePtr->nextPtr = dyldLoadHandle->modulePtr; dyldLoadHandle->modulePtr = modulePtr; @@ -434,8 +446,11 @@ TclpFindSymbol( #endif /* TCL_DYLD_USE_NSMODULE */ } Tcl_DStringFree(&ds); - if (errMsg) { - Tcl_AppendResult(interp, errMsg, NULL); + if (errMsg && (interp != NULL)) { + Tcl_AppendResult(interp, "cannot find symbol \"", symbol, "\": ", + errMsg, NULL); + Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "LOAD_SYMBOL", symbol, + NULL); } return proc; } @@ -443,7 +458,7 @@ TclpFindSymbol( /* *---------------------------------------------------------------------- * - * TclpUnloadFile -- + * UnloadFile -- * * Unloads a dynamically loaded binary code file from memory. Code * pointers in the formerly loaded file are no longer valid after calling @@ -460,13 +475,13 @@ TclpFindSymbol( *---------------------------------------------------------------------- */ -MODULE_SCOPE void -TclpUnloadFile( +static void +UnloadFile( Tcl_LoadHandle loadHandle) /* loadHandle returned by a previous call to * TclpDlopen(). The loadHandle is a token * that represents the loaded file. */ { - Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle; + Tcl_DyldLoadHandle *dyldLoadHandle = loadHandle->clientData; #if TCL_DYLD_USE_DLFCN if (dyldLoadHandle->dlHandle) { @@ -501,7 +516,8 @@ TclpUnloadFile( } #endif /* TCL_DYLD_USE_NSMODULE */ } - ckfree((char*) dyldLoadHandle); + ckfree(dyldLoadHandle); + ckfree(loadHandle); } /* @@ -526,7 +542,7 @@ TclpUnloadFile( int TclGuessPackageName( - CONST char *fileName, /* Name of file containing package (already + const char *fileName, /* Name of file containing package (already * translated to local form if needed). */ Tcl_DString *bufPtr) /* Initialized empty dstring. Append package * name to this if possible. */ @@ -611,6 +627,7 @@ TclpLoadMemory( * function which should be used for this * file. */ { + Tcl_LoadHandle newHandle; Tcl_DyldLoadHandle *dyldLoadHandle; NSObjectFileImage dyldObjFileImage = NULL; Tcl_DyldModuleHandle *modulePtr; @@ -627,14 +644,14 @@ TclpLoadMemory( uint32_t ms = 0; #ifndef __LP64__ const struct mach_header *mh = NULL; - #define mh_size sizeof(struct mach_header) - #define mh_magic MH_MAGIC - #define arch_abi 0 +# define mh_size sizeof(struct mach_header) +# define mh_magic MH_MAGIC +# define arch_abi 0 #else const struct mach_header_64 *mh = NULL; - #define mh_size sizeof(struct mach_header_64) - #define mh_magic MH_MAGIC_64 - #define arch_abi CPU_ARCH_ABI64 +# define mh_size sizeof(struct mach_header_64) +# define mh_magic MH_MAGIC_64 +# define arch_abi CPU_ARCH_ABI64 #endif if ((size_t) codeSize >= sizeof(struct fat_header) @@ -745,18 +762,21 @@ TclpLoadMemory( * Stash the module reference within the load handle we create and return. */ - modulePtr = (Tcl_DyldModuleHandle *) ckalloc(sizeof(Tcl_DyldModuleHandle)); + modulePtr = ckalloc(sizeof(Tcl_DyldModuleHandle)); modulePtr->module = module; modulePtr->nextPtr = NULL; - dyldLoadHandle = (Tcl_DyldLoadHandle *) - ckalloc(sizeof(Tcl_DyldLoadHandle)); + dyldLoadHandle = ckalloc(sizeof(Tcl_DyldLoadHandle)); #if TCL_DYLD_USE_DLFCN dyldLoadHandle->dlHandle = NULL; #endif dyldLoadHandle->dyldLibHeader = NULL; dyldLoadHandle->modulePtr = modulePtr; - *loadHandle = (Tcl_LoadHandle) dyldLoadHandle; - *unloadProcPtr = &TclpUnloadFile; + newHandle = ckalloc(sizeof(*newHandle)); + newHandle->clientData = dyldLoadHandle; + newHandle->findSymbolProcPtr = &FindSymbol; + newHandle->unloadFileProcPtr = &UnloadFile; + *loadHandle = newHandle; + *unloadProcPtr = &UnloadFile; return TCL_OK; } #endif /* TCL_LOAD_FROM_MEMORY */ |