sl@0: /* sl@0: * tclLoadDyld.c -- sl@0: * sl@0: * This procedure provides a version of the TclLoadFile that works with sl@0: * Apple's dyld dynamic loading. sl@0: * Original version of his file (now superseded long ago) provided by sl@0: * Wilfredo Sanchez (wsanchez@apple.com). sl@0: * sl@0: * Copyright (c) 1995 Apple Computer, Inc. sl@0: * Copyright (c) 2001-2007 Daniel A. Steffen sl@0: * sl@0: * See the file "license.terms" for information on usage and redistribution of sl@0: * this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: * sl@0: * RCS: @(#) $Id: tclLoadDyld.c,v 1.14.2.9 2007/04/29 02:20:16 das Exp $ sl@0: */ sl@0: sl@0: #include "tclInt.h" sl@0: #include "tclPort.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #undef panic sl@0: #include sl@0: sl@0: #ifndef MODULE_SCOPE sl@0: #define MODULE_SCOPE extern sl@0: #endif sl@0: sl@0: typedef struct Tcl_DyldModuleHandle { sl@0: struct Tcl_DyldModuleHandle *nextPtr; sl@0: NSModule module; sl@0: } Tcl_DyldModuleHandle; sl@0: sl@0: typedef struct Tcl_DyldLoadHandle { sl@0: CONST struct mach_header *dyldLibHeader; sl@0: Tcl_DyldModuleHandle *modulePtr; sl@0: } Tcl_DyldLoadHandle; sl@0: sl@0: #ifdef TCL_LOAD_FROM_MEMORY sl@0: MODULE_SCOPE long tclMacOSXDarwinRelease; sl@0: #endif sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * DyldOFIErrorMsg -- sl@0: * sl@0: * Converts a numerical NSObjectFileImage error into an error message sl@0: * string. sl@0: * sl@0: * Results: sl@0: * Error message string. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: static CONST char* sl@0: DyldOFIErrorMsg( sl@0: int err) sl@0: { sl@0: switch(err) { sl@0: case NSObjectFileImageSuccess: sl@0: return NULL; sl@0: case NSObjectFileImageFailure: sl@0: return "object file setup failure"; sl@0: case NSObjectFileImageInappropriateFile: sl@0: return "not a Mach-O MH_BUNDLE file"; sl@0: case NSObjectFileImageArch: sl@0: return "no object for this architecture"; sl@0: case NSObjectFileImageFormat: sl@0: return "bad object file format"; sl@0: case NSObjectFileImageAccess: sl@0: return "can't read object file"; sl@0: default: sl@0: return "unknown error"; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclpDlopen -- sl@0: * sl@0: * Dynamically loads a binary code file into memory and returns a handle sl@0: * to the new code. sl@0: * sl@0: * Results: sl@0: * A standard Tcl completion code. If an error occurs, an error message sl@0: * is left in the interpreter's result. sl@0: * sl@0: * Side effects: sl@0: * New code suddenly appears in memory. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: MODULE_SCOPE int sl@0: TclpDlopen( 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 this sl@0: * file. */ sl@0: { sl@0: Tcl_DyldLoadHandle *dyldLoadHandle; sl@0: CONST struct mach_header *dyldLibHeader; sl@0: NSObjectFileImage dyldObjFileImage = NULL; sl@0: Tcl_DyldModuleHandle *modulePtr = NULL; sl@0: CONST char *native; sl@0: sl@0: /* sl@0: * First try the full path the user gave us. This is particularly sl@0: * important if the cwd is inside a vfs, and we are trying to load using a sl@0: * relative path. sl@0: */ sl@0: sl@0: native = Tcl_FSGetNativePath(pathPtr); sl@0: dyldLibHeader = NSAddImage(native, NSADDIMAGE_OPTION_RETURN_ON_ERROR); sl@0: sl@0: if (!dyldLibHeader) { sl@0: NSLinkEditErrors editError; sl@0: int errorNumber; sl@0: CONST char *name, *msg, *objFileImageErrMsg = NULL; sl@0: sl@0: NSLinkEditError(&editError, &errorNumber, &name, &msg); sl@0: sl@0: if (editError == NSLinkEditFileAccessError) { sl@0: /* sl@0: * The requested file was not found. Let the OS loader examine the sl@0: * binary search path for whatever string the user gave us which sl@0: * hopefully refers to a file on the binary path. sl@0: */ sl@0: sl@0: Tcl_DString ds; sl@0: char *fileName = Tcl_GetString(pathPtr); sl@0: CONST char *native = sl@0: Tcl_UtfToExternalDString(NULL, fileName, -1, &ds); sl@0: sl@0: dyldLibHeader = NSAddImage(native, NSADDIMAGE_OPTION_WITH_SEARCHING sl@0: | NSADDIMAGE_OPTION_RETURN_ON_ERROR); sl@0: Tcl_DStringFree(&ds); sl@0: if (!dyldLibHeader) { sl@0: NSLinkEditError(&editError, &errorNumber, &name, &msg); sl@0: } sl@0: } else if ((editError == NSLinkEditFileFormatError sl@0: && errorNumber == EBADMACHO) sl@0: || editError == NSLinkEditOtherError){ sl@0: /* sl@0: * The requested file was found but was not of type MH_DYLIB, sl@0: * attempt to load it as a MH_BUNDLE. sl@0: */ sl@0: sl@0: NSObjectFileImageReturnCode err = sl@0: NSCreateObjectFileImageFromFile(native, &dyldObjFileImage); sl@0: objFileImageErrMsg = DyldOFIErrorMsg(err); sl@0: } sl@0: sl@0: if (!dyldLibHeader && !dyldObjFileImage) { sl@0: Tcl_AppendResult(interp, msg, NULL); sl@0: if (msg && *msg) { sl@0: Tcl_AppendResult(interp, "\n", NULL); sl@0: } sl@0: if (objFileImageErrMsg) { sl@0: Tcl_AppendResult(interp, sl@0: "NSCreateObjectFileImageFromFile() error: ", sl@0: objFileImageErrMsg, NULL); sl@0: } sl@0: return TCL_ERROR; sl@0: } sl@0: } sl@0: sl@0: if (dyldObjFileImage) { sl@0: NSModule module; sl@0: sl@0: module = NSLinkModule(dyldObjFileImage, native, sl@0: NSLINKMODULE_OPTION_BINDNOW sl@0: | NSLINKMODULE_OPTION_RETURN_ON_ERROR); sl@0: NSDestroyObjectFileImage(dyldObjFileImage); sl@0: sl@0: if (!module) { sl@0: NSLinkEditErrors editError; sl@0: int errorNumber; sl@0: CONST char *name, *msg; sl@0: sl@0: NSLinkEditError(&editError, &errorNumber, &name, &msg); sl@0: Tcl_AppendResult(interp, msg, NULL); sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: modulePtr = (Tcl_DyldModuleHandle *) sl@0: ckalloc(sizeof(Tcl_DyldModuleHandle)); sl@0: modulePtr->module = module; sl@0: modulePtr->nextPtr = NULL; sl@0: } sl@0: sl@0: dyldLoadHandle = (Tcl_DyldLoadHandle *) sl@0: ckalloc(sizeof(Tcl_DyldLoadHandle)); sl@0: dyldLoadHandle->dyldLibHeader = dyldLibHeader; sl@0: dyldLoadHandle->modulePtr = modulePtr; sl@0: *loadHandle = (Tcl_LoadHandle) dyldLoadHandle; sl@0: *unloadProcPtr = &TclpUnloadFile; 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 a sl@0: * previously loaded piece of code (shared library). sl@0: * sl@0: * Results: sl@0: * Returns a pointer to the function associated with 'symbol' if it is sl@0: * found. Otherwise returns NULL and may leave an error message in the sl@0: * interp's result. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: MODULE_SCOPE Tcl_PackageInitProc * sl@0: TclpFindSymbol( sl@0: Tcl_Interp *interp, /* For error reporting. */ sl@0: Tcl_LoadHandle loadHandle, /* Handle from TclpDlopen. */ sl@0: CONST char *symbol) /* Symbol name to look up. */ sl@0: { sl@0: NSSymbol nsSymbol; sl@0: CONST char *native; sl@0: Tcl_DString newName, ds; sl@0: Tcl_PackageInitProc *proc = NULL; sl@0: Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle; sl@0: sl@0: /* sl@0: * dyld adds an underscore to the beginning of symbol names. sl@0: */ sl@0: sl@0: native = Tcl_UtfToExternalDString(NULL, symbol, -1, &ds); sl@0: Tcl_DStringInit(&newName); sl@0: Tcl_DStringAppend(&newName, "_", 1); sl@0: native = Tcl_DStringAppend(&newName, native, -1); sl@0: sl@0: if (dyldLoadHandle->dyldLibHeader) { sl@0: nsSymbol = NSLookupSymbolInImage(dyldLoadHandle->dyldLibHeader, native, sl@0: NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW | sl@0: NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); sl@0: if (nsSymbol) { sl@0: /* sl@0: * Until dyld supports unloading of MY_DYLIB binaries, the sl@0: * following is not needed. sl@0: */ sl@0: sl@0: #ifdef DYLD_SUPPORTS_DYLIB_UNLOADING sl@0: NSModule module = NSModuleForSymbol(nsSymbol); sl@0: Tcl_DyldModuleHandle *modulePtr = dyldLoadHandle->modulePtr; sl@0: sl@0: while (modulePtr != NULL) { sl@0: if (module == modulePtr->module) { sl@0: break; sl@0: } sl@0: modulePtr = modulePtr->nextPtr; sl@0: } sl@0: if (modulePtr == NULL) { sl@0: modulePtr = (Tcl_DyldModuleHandle *) sl@0: ckalloc(sizeof(Tcl_DyldModuleHandle)); sl@0: modulePtr->module = module; sl@0: modulePtr->nextPtr = dyldLoadHandle->modulePtr; sl@0: dyldLoadHandle->modulePtr = modulePtr; sl@0: } sl@0: #endif /* DYLD_SUPPORTS_DYLIB_UNLOADING */ sl@0: sl@0: } else { sl@0: NSLinkEditErrors editError; sl@0: int errorNumber; sl@0: CONST char *name, *msg; sl@0: sl@0: NSLinkEditError(&editError, &errorNumber, &name, &msg); sl@0: Tcl_AppendResult(interp, msg, NULL); sl@0: } sl@0: } else { sl@0: nsSymbol = NSLookupSymbolInModule(dyldLoadHandle->modulePtr->module, sl@0: native); sl@0: } sl@0: if (nsSymbol) { sl@0: proc = NSAddressOfSymbol(nsSymbol); sl@0: } sl@0: Tcl_DStringFree(&newName); sl@0: Tcl_DStringFree(&ds); 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. Code sl@0: * pointers in the formerly loaded file are no longer valid after calling sl@0: * this function. sl@0: * sl@0: * Results: sl@0: * None. sl@0: * sl@0: * Side effects: sl@0: * Code dissapears from memory. Note that dyld currently only supports sl@0: * unloading of binaries of type MH_BUNDLE loaded with NSLinkModule() in sl@0: * TclpDlopen() above. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: MODULE_SCOPE void sl@0: TclpUnloadFile( sl@0: Tcl_LoadHandle loadHandle) /* loadHandle returned by a previous call to sl@0: * TclpDlopen(). The loadHandle is a token sl@0: * that represents the loaded file. */ sl@0: { sl@0: Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle; sl@0: Tcl_DyldModuleHandle *modulePtr = dyldLoadHandle->modulePtr; sl@0: sl@0: while (modulePtr != NULL) { sl@0: void *ptr; sl@0: sl@0: NSUnLinkModule(modulePtr->module, sl@0: NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); sl@0: ptr = modulePtr; sl@0: modulePtr = modulePtr->nextPtr; sl@0: ckfree(ptr); sl@0: } sl@0: ckfree((char*) dyldLoadHandle); 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 name, sl@0: * 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 package sl@0: * name; generic code will then try to guess the package from the file sl@0: * name. A return value of 1 would have meant that we figured out the sl@0: * 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 package sl@0: * name to this if possible. */ sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: #ifdef TCL_LOAD_FROM_MEMORY sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclpLoadMemoryGetBuffer -- sl@0: * sl@0: * Allocate a buffer that can be used with TclpLoadMemory() below. sl@0: * sl@0: * Results: sl@0: * Pointer to allocated buffer or NULL if an error occurs. sl@0: * sl@0: * Side effects: sl@0: * Buffer is allocated. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: MODULE_SCOPE void * sl@0: TclpLoadMemoryGetBuffer( sl@0: Tcl_Interp *interp, /* Used for error reporting. */ sl@0: int size) /* Size of desired buffer. */ sl@0: { sl@0: void *buffer = NULL; sl@0: sl@0: /* sl@0: * NSCreateObjectFileImageFromMemory is available but always fails sl@0: * prior to Darwin 7. sl@0: */ sl@0: if (tclMacOSXDarwinRelease >= 7) { sl@0: /* sl@0: * We must allocate the buffer using vm_allocate, because sl@0: * NSCreateObjectFileImageFromMemory will dispose of it using sl@0: * vm_deallocate. sl@0: */ sl@0: sl@0: if (vm_allocate(mach_task_self(), (vm_address_t *) &buffer, size, 1)) { sl@0: buffer = NULL; sl@0: } sl@0: } sl@0: return buffer; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclpLoadMemory -- sl@0: * sl@0: * Dynamically loads binary code file from memory and returns a handle to sl@0: * the new code. sl@0: * sl@0: * Results: sl@0: * A standard Tcl completion code. If an error occurs, an error message sl@0: * is left in the interpreter's result. sl@0: * sl@0: * Side effects: sl@0: * New code is loaded from memory. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: MODULE_SCOPE int sl@0: TclpLoadMemory( sl@0: Tcl_Interp *interp, /* Used for error reporting. */ sl@0: void *buffer, /* Buffer containing the desired code sl@0: * (allocated with TclpLoadMemoryGetBuffer). */ sl@0: int size, /* Allocation size of buffer. */ sl@0: int codeSize, /* Size of code data read into buffer or -1 if sl@0: * an error occurred and the buffer should sl@0: * just be freed. */ 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 this sl@0: * file. */ sl@0: { sl@0: Tcl_DyldLoadHandle *dyldLoadHandle; sl@0: NSObjectFileImage dyldObjFileImage = NULL; sl@0: Tcl_DyldModuleHandle *modulePtr; sl@0: NSModule module; sl@0: CONST char *objFileImageErrMsg = NULL; sl@0: sl@0: /* sl@0: * Try to create an object file image that we can load from. sl@0: */ sl@0: sl@0: if (codeSize >= 0) { sl@0: NSObjectFileImageReturnCode err = NSObjectFileImageSuccess; sl@0: CONST struct fat_header *fh = buffer; sl@0: uint32_t ms = 0; sl@0: #ifndef __LP64__ sl@0: CONST struct mach_header *mh = NULL; sl@0: #define mh_magic OSSwapHostToBigInt32(MH_MAGIC) sl@0: #define mh_size sizeof(struct mach_header) sl@0: #else sl@0: CONST struct mach_header_64 *mh = NULL; sl@0: #define mh_magic OSSwapHostToBigInt32(MH_MAGIC_64) sl@0: #define mh_size sizeof(struct mach_header_64) sl@0: #endif sl@0: sl@0: if ((size_t) codeSize >= sizeof(struct fat_header) sl@0: && fh->magic == OSSwapHostToBigInt32(FAT_MAGIC)) { sl@0: /* sl@0: * Fat binary, try to find mach_header for our architecture sl@0: */ sl@0: uint32_t fh_nfat_arch = OSSwapBigToHostInt32(fh->nfat_arch); sl@0: sl@0: if ((size_t) codeSize >= sizeof(struct fat_header) + sl@0: fh_nfat_arch * sizeof(struct fat_arch)) { sl@0: void *fatarchs = (char*)buffer + sizeof(struct fat_header); sl@0: CONST NXArchInfo *arch = NXGetLocalArchInfo(); sl@0: struct fat_arch *fa; sl@0: sl@0: if (fh->magic != FAT_MAGIC) { sl@0: swap_fat_arch(fatarchs, fh_nfat_arch, arch->byteorder); sl@0: } sl@0: fa = NXFindBestFatArch(arch->cputype, arch->cpusubtype, sl@0: fatarchs, fh_nfat_arch); sl@0: if (fa) { sl@0: mh = (void*)((char*)buffer + fa->offset); sl@0: ms = fa->size; sl@0: } else { sl@0: err = NSObjectFileImageInappropriateFile; sl@0: } sl@0: if (fh->magic != FAT_MAGIC) { sl@0: swap_fat_arch(fatarchs, fh_nfat_arch, arch->byteorder); sl@0: } sl@0: } else { sl@0: err = NSObjectFileImageInappropriateFile; sl@0: } sl@0: } else { sl@0: /* sl@0: * Thin binary sl@0: */ sl@0: mh = buffer; sl@0: ms = codeSize; sl@0: } sl@0: if (ms && !(ms >= mh_size && mh->magic == mh_magic && sl@0: mh->filetype == OSSwapHostToBigInt32(MH_BUNDLE))) { sl@0: err = NSObjectFileImageInappropriateFile; sl@0: } sl@0: if (err == NSObjectFileImageSuccess) { sl@0: err = NSCreateObjectFileImageFromMemory(buffer, codeSize, sl@0: &dyldObjFileImage); sl@0: } sl@0: objFileImageErrMsg = DyldOFIErrorMsg(err); sl@0: } sl@0: sl@0: /* sl@0: * If it went wrong (or we were asked to just deallocate), get rid of the sl@0: * memory block and create an error message. sl@0: */ sl@0: sl@0: if (dyldObjFileImage == NULL) { sl@0: vm_deallocate(mach_task_self(), (vm_address_t) buffer, size); sl@0: if (objFileImageErrMsg != NULL) { sl@0: Tcl_AppendResult(interp, sl@0: "NSCreateObjectFileImageFromMemory() error: ", sl@0: objFileImageErrMsg, NULL); sl@0: } sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: /* sl@0: * Extract the module we want from the image of the object file. sl@0: */ sl@0: sl@0: module = NSLinkModule(dyldObjFileImage, "[Memory Based Bundle]", sl@0: NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_RETURN_ON_ERROR); sl@0: NSDestroyObjectFileImage(dyldObjFileImage); sl@0: sl@0: if (!module) { sl@0: NSLinkEditErrors editError; sl@0: int errorNumber; sl@0: CONST char *name, *msg; sl@0: sl@0: NSLinkEditError(&editError, &errorNumber, &name, &msg); sl@0: Tcl_AppendResult(interp, msg, NULL); sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: /* sl@0: * Stash the module reference within the load handle we create and return. sl@0: */ sl@0: sl@0: modulePtr = (Tcl_DyldModuleHandle *) ckalloc(sizeof(Tcl_DyldModuleHandle)); sl@0: modulePtr->module = module; sl@0: modulePtr->nextPtr = NULL; sl@0: sl@0: dyldLoadHandle = (Tcl_DyldLoadHandle *) sl@0: ckalloc(sizeof(Tcl_DyldLoadHandle)); sl@0: dyldLoadHandle->dyldLibHeader = NULL; sl@0: dyldLoadHandle->modulePtr = modulePtr; sl@0: *loadHandle = (Tcl_LoadHandle) dyldLoadHandle; sl@0: *unloadProcPtr = &TclpUnloadFile; sl@0: return TCL_OK; sl@0: } sl@0: #endif sl@0: sl@0: /* sl@0: * Local Variables: sl@0: * mode: c sl@0: * c-basic-offset: 4 sl@0: * fill-column: 78 sl@0: * End: sl@0: */