sl@0: /* sl@0: * tclMacLoad.c -- sl@0: * sl@0: * This procedure provides a version of the TclLoadFile for use sl@0: * on the Macintosh. This procedure will only work with systems sl@0: * that use the Code Fragment Manager. sl@0: * sl@0: * Copyright (c) 1995-1997 Sun Microsystems, Inc. sl@0: * sl@0: * See the file "license.terms" for information on usage and redistribution sl@0: * of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: * sl@0: * RCS: @(#) $Id: tclMacLoad.c,v 1.16 2002/10/09 11:54:26 das Exp $ sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /* sl@0: * Seems that the 3.0.1 Universal headers leave this define out. So we sl@0: * define it here... sl@0: */ sl@0: sl@0: #ifndef fragNoErr sl@0: #define fragNoErr noErr sl@0: #endif sl@0: sl@0: #include "tclPort.h" sl@0: #include "tclInt.h" sl@0: #include "tclMacInt.h" sl@0: sl@0: #if GENERATINGPOWERPC sl@0: #define OUR_ARCH_TYPE kPowerPCCFragArch sl@0: #else sl@0: #define OUR_ARCH_TYPE kMotorola68KCFragArch sl@0: #endif sl@0: sl@0: /* sl@0: * The following data structure defines the structure of a code fragment sl@0: * resource. We can cast the resource to be of this type to access sl@0: * any fields we need to see. sl@0: */ sl@0: struct CfrgHeader { sl@0: long res1; sl@0: long res2; sl@0: long version; sl@0: long res3; sl@0: long res4; sl@0: long filler1; sl@0: long filler2; sl@0: long itemCount; sl@0: char arrayStart; /* Array of externalItems begins here. */ sl@0: }; sl@0: typedef struct CfrgHeader CfrgHeader, *CfrgHeaderPtr, **CfrgHeaderPtrHand; sl@0: sl@0: /* sl@0: * The below structure defines a cfrag item within the cfrag resource. sl@0: */ sl@0: struct CfrgItem { sl@0: OSType archType; sl@0: long updateLevel; sl@0: long currVersion; sl@0: long oldDefVersion; sl@0: long appStackSize; sl@0: short appSubFolder; sl@0: char usage; sl@0: char location; sl@0: long codeOffset; sl@0: long codeLength; sl@0: long res1; sl@0: long res2; sl@0: short itemSize; sl@0: Str255 name; /* This is actually variable sized. */ sl@0: }; sl@0: typedef struct CfrgItem CfrgItem; sl@0: sl@0: /* sl@0: * On MacOS, old shared libraries which contain many code fragments sl@0: * cannot, it seems, be loaded in one go. We need to look provide sl@0: * the name of a code fragment while we load. Since with the sl@0: * separation of the 'load' and 'findsymbol' be do not necessarily sl@0: * know a symbol name at load time, we have to store some further sl@0: * information in a structure like this so we can ensure we load sl@0: * properly in 'findsymbol' if the first attempts didn't work. sl@0: */ sl@0: typedef struct TclMacLoadInfo { sl@0: int loaded; sl@0: CFragConnectionID connID; sl@0: FSSpec fileSpec; sl@0: } TclMacLoadInfo; sl@0: sl@0: static int TryToLoad(Tcl_Interp *interp, TclMacLoadInfo *loadInfo, Tcl_Obj *pathPtr, sl@0: CONST char *sym /* native */); sl@0: sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclpDlopen -- sl@0: * sl@0: * This procedure is called to carry out dynamic loading of binary sl@0: * code for the Macintosh. This implementation is based on the sl@0: * Code Fragment Manager & will not work on other systems. sl@0: * sl@0: * Results: sl@0: * The result is TCL_ERROR, and an error message is left in sl@0: * the interp's result. sl@0: * sl@0: * Side effects: sl@0: * New binary code is loaded. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: int sl@0: TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr) sl@0: Tcl_Interp *interp; /* Used for error reporting. */ sl@0: Tcl_Obj *pathPtr; /* Name of the file containing the desired sl@0: * code (UTF-8). */ sl@0: Tcl_LoadHandle *loadHandle; /* Filled with token for dynamically loaded sl@0: * file which will be passed back to sl@0: * (*unloadProcPtr)() to unload the file. */ sl@0: Tcl_FSUnloadFileProc **unloadProcPtr; sl@0: /* Filled with address of Tcl_FSUnloadFileProc sl@0: * function which should be used for sl@0: * this file. */ sl@0: { sl@0: OSErr err; sl@0: FSSpec fileSpec; sl@0: CONST char *native; sl@0: TclMacLoadInfo *loadInfo; sl@0: sl@0: native = Tcl_FSGetNativePath(pathPtr); sl@0: err = FSpLocationFromPath(strlen(native), native, &fileSpec); sl@0: sl@0: if (err != noErr) { sl@0: Tcl_SetResult(interp, "could not locate shared library", TCL_STATIC); sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: loadInfo = (TclMacLoadInfo *) ckalloc(sizeof(TclMacLoadInfo)); sl@0: loadInfo->loaded = 0; sl@0: loadInfo->fileSpec = fileSpec; sl@0: loadInfo->connID = NULL; sl@0: sl@0: if (TryToLoad(interp, loadInfo, pathPtr, NULL) != TCL_OK) { sl@0: ckfree((char*) loadInfo); sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: *loadHandle = (Tcl_LoadHandle)loadInfo; sl@0: *unloadProcPtr = &TclpUnloadFile; sl@0: return TCL_OK; sl@0: } sl@0: sl@0: /* sl@0: * See the comments about 'struct TclMacLoadInfo' above. This sl@0: * function ensures the appropriate library or symbol is sl@0: * loaded. sl@0: */ sl@0: static int sl@0: TryToLoad(Tcl_Interp *interp, TclMacLoadInfo *loadInfo, Tcl_Obj *pathPtr, sl@0: CONST char *sym /* native */) sl@0: { sl@0: OSErr err; sl@0: CFragConnectionID connID; sl@0: Ptr dummy; sl@0: short fragFileRef, saveFileRef; sl@0: Handle fragResource; sl@0: UInt32 offset = 0; sl@0: UInt32 length = kCFragGoesToEOF; sl@0: Str255 errName; sl@0: StringPtr fragName=NULL; sl@0: sl@0: if (loadInfo->loaded == 1) { sl@0: return TCL_OK; sl@0: } sl@0: sl@0: /* sl@0: * See if this fragment has a 'cfrg' resource. It will tell us where sl@0: * to look for the fragment in the file. If it doesn't exist we will sl@0: * assume we have a ppc frag using the whole data fork. If it does sl@0: * exist we find the frag that matches the one we are looking for and sl@0: * get the offset and size from the resource. sl@0: */ sl@0: sl@0: saveFileRef = CurResFile(); sl@0: SetResLoad(false); sl@0: fragFileRef = FSpOpenResFile(&loadInfo->fileSpec, fsRdPerm); sl@0: SetResLoad(true); sl@0: if (fragFileRef != -1) { sl@0: if (sym != NULL) { sl@0: UseResFile(fragFileRef); sl@0: fragResource = Get1Resource(kCFragResourceType, kCFragResourceID); sl@0: HLock(fragResource); sl@0: if (ResError() == noErr) { sl@0: CfrgItem* srcItem; sl@0: long itemCount, index; sl@0: Ptr itemStart; sl@0: sl@0: itemCount = (*(CfrgHeaderPtrHand)fragResource)->itemCount; sl@0: itemStart = &(*(CfrgHeaderPtrHand)fragResource)->arrayStart; sl@0: for (index = 0; index < itemCount; sl@0: index++, itemStart += srcItem->itemSize) { sl@0: srcItem = (CfrgItem*)itemStart; sl@0: if (srcItem->archType != OUR_ARCH_TYPE) continue; sl@0: if (!strncasecmp(sym, (char *) srcItem->name + 1, sl@0: strlen(sym))) { sl@0: offset = srcItem->codeOffset; sl@0: length = srcItem->codeLength; sl@0: fragName=srcItem->name; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: /* sl@0: * Close the resource file. If the extension wants to reopen the sl@0: * resource fork it should use the tclMacLibrary.c file during it's sl@0: * construction. sl@0: */ sl@0: HUnlock(fragResource); sl@0: ReleaseResource(fragResource); sl@0: CloseResFile(fragFileRef); sl@0: UseResFile(saveFileRef); sl@0: if (sym == NULL) { sl@0: /* We just return */ sl@0: return TCL_OK; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Now we can attempt to load the fragment using the offset & length sl@0: * obtained from the resource. We don't worry about the main entry point sl@0: * as we are going to search for specific entry points passed to us. sl@0: */ sl@0: sl@0: err = GetDiskFragment(&loadInfo->fileSpec, offset, length, fragName, sl@0: kLoadCFrag, &connID, &dummy, errName); sl@0: sl@0: if (err != fragNoErr) { sl@0: p2cstr(errName); sl@0: if(pathPtr) { sl@0: Tcl_AppendResult(interp, "couldn't load file \"", sl@0: Tcl_GetString(pathPtr), sl@0: "\": ", errName, (char *) NULL); sl@0: } else if(sym) { sl@0: Tcl_AppendResult(interp, "couldn't load library \"", sl@0: sym, sl@0: "\": ", errName, (char *) NULL); sl@0: } sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: loadInfo->connID = connID; sl@0: loadInfo->loaded = 1; sl@0: sl@0: return TCL_OK; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclpFindSymbol -- sl@0: * sl@0: * Looks up a symbol, by name, through a handle associated with sl@0: * a previously loaded piece of code (shared library). sl@0: * sl@0: * Results: sl@0: * Returns a pointer to the function associated with 'symbol' if sl@0: * it is found. Otherwise returns NULL and may leave an error sl@0: * message in the interp's result. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: Tcl_PackageInitProc* sl@0: TclpFindSymbol(interp, loadHandle, symbol) sl@0: Tcl_Interp *interp; sl@0: Tcl_LoadHandle loadHandle; sl@0: CONST char *symbol; sl@0: { sl@0: Tcl_DString ds; sl@0: Tcl_PackageInitProc *proc=NULL; sl@0: TclMacLoadInfo *loadInfo = (TclMacLoadInfo *)loadHandle; sl@0: Str255 symbolName; sl@0: CFragSymbolClass symClass; sl@0: OSErr err; sl@0: sl@0: if (loadInfo->loaded == 0) { sl@0: int res; sl@0: /* sl@0: * First thing we must do is infer the package name from the sl@0: * sym variable. We do this by removing the '_Init'. sl@0: */ sl@0: Tcl_UtfToExternalDString(NULL, symbol, -1, &ds); sl@0: Tcl_DStringSetLength(&ds, Tcl_DStringLength(&ds) - 5); sl@0: res = TryToLoad(interp, loadInfo, NULL, Tcl_DStringValue(&ds)); sl@0: Tcl_DStringFree(&ds); sl@0: if (res != TCL_OK) { sl@0: return NULL; sl@0: } sl@0: } sl@0: sl@0: Tcl_UtfToExternalDString(NULL, symbol, -1, &ds); sl@0: strcpy((char *) symbolName + 1, Tcl_DStringValue(&ds)); sl@0: symbolName[0] = (unsigned) Tcl_DStringLength(&ds); sl@0: err = FindSymbol(loadInfo->connID, symbolName, (Ptr *) &proc, &symClass); sl@0: Tcl_DStringFree(&ds); sl@0: if (err != fragNoErr || symClass == kDataCFragSymbol) { sl@0: Tcl_SetResult(interp, sl@0: "could not find Initialization routine in library", sl@0: TCL_STATIC); sl@0: return NULL; sl@0: } sl@0: return proc; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclpUnloadFile -- sl@0: * sl@0: * Unloads a dynamically loaded binary code file from memory. sl@0: * Code pointers in the formerly loaded file are no longer valid sl@0: * after calling this function. sl@0: * sl@0: * Results: sl@0: * None. sl@0: * sl@0: * Side effects: sl@0: * Does nothing. Can anything be done? sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: void sl@0: TclpUnloadFile(loadHandle) sl@0: Tcl_LoadHandle loadHandle; /* loadHandle returned by a previous call sl@0: * to TclpDlopen(). The loadHandle is sl@0: * a token that represents the loaded sl@0: * file. */ sl@0: { sl@0: TclMacLoadInfo *loadInfo = (TclMacLoadInfo *)loadHandle; sl@0: if (loadInfo->loaded) { sl@0: CloseConnection((CFragConnectionID*) &(loadInfo->connID)); sl@0: } sl@0: ckfree((char*)loadInfo); sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclGuessPackageName -- sl@0: * sl@0: * If the "load" command is invoked without providing a package sl@0: * name, this procedure is invoked to try to figure it out. sl@0: * sl@0: * Results: sl@0: * Always returns 0 to indicate that we couldn't figure out a sl@0: * package name; generic code will then try to guess the package sl@0: * from the file name. A return value of 1 would have meant that sl@0: * we figured out the package name and put it in bufPtr. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: int sl@0: TclGuessPackageName( sl@0: CONST char *fileName, /* Name of file containing package (already sl@0: * translated to local form if needed). */ sl@0: Tcl_DString *bufPtr) /* Initialized empty dstring. Append sl@0: * package name to this if possible. */ sl@0: { sl@0: return 0; sl@0: }