From: Daniel A. S. <st...@ic...> - 2002-02-15 13:18:46
|
At 21:39 +1100 on 15/2/02, Daniel A. Steffen wrote: >probably we should rewrite tclLoadDyld.c using the 10.1 >TwoLevelNamespaces API anyway. I took a stab at this, c.f. below. (based on tclLoadDyld.c from the tip of core-8-3-1-branch) works well, doesn't fail on nonexistent files, searches DYLD_LIBRARY_PATH etc. Howver, it probably requires 10.1, <mach-o/dyld.h> is unclear on this (Jim?), so it might not be appropriate for core-8-3-1-branch ? If I don't hear any objections, I'll check a CONSTified version into the HEAD though. Cheers, Daniel -------------------------- paste into tclLoadDyld.c ------------------------= -- #include "tclInt.h" #include "tclPort.h" #include <mach-o/dyld.h> /* *---------------------------------------------------------------------- * * TclpLoadFile -- * * Dynamically loads a binary code file into memory and returns * the addresses of two procedures within that file, if they * are defined. * * Results: * A standard Tcl completion code. If an error occurs, an error * message is left in the interpreter's result. *proc1Ptr and *proc2Pt= r * are filled in with the addresses of the symbols given by * *sym1 and *sym2, or NULL if those symbols can't be found. * * Side effects: * New code suddenly appears in memory. * *---------------------------------------------------------------------- */ int TclpLoadFile(interp, fileName, sym1, sym2, proc1Ptr, proc2Ptr, clientDataPtr= ) Tcl_Interp *interp; /* Used for error reporting. */ char *fileName; /* Name of the file containing the desired * code. */ char *sym1, *sym2; /* Names of two procedures to look up in * the file's symbol table. */ Tcl_PackageInitProc **proc1Ptr, **proc2Ptr; /* Where to return the addresses corresponding * to sym1 and sym2. */ ClientData *clientDataPtr; /* Filled with token for dynamically loaded * file which will be passed back to * TclpUnloadFile() to unload the file. */ { NSSymbol symbol; const struct mach_header *dyld_lib; Tcl_DString newName, ds; char *native; native =3D Tcl_UtfToExternalDString(NULL, fileName, -1, &ds); dyld_lib =3D NSAddImage(native, NSADDIMAGE_OPTION_WITH_SEARCHING | NSADDIMAGE_OPTION_RETURN_ON_ERROR); Tcl_DStringFree(&ds); =A0=A0 if (!dyld_lib) { NSLinkEditErrors editError; char *name, *msg; NSLinkEditError(&editError, &errno, &name, &msg); Tcl_AppendResult(interp, msg, (char *) NULL); return TCL_ERROR; } /* * dyld adds an underscore to the beginning of symbol names. */ native =3D Tcl_UtfToExternalDString(NULL, sym1, -1, &ds); Tcl_DStringInit(&newName); Tcl_DStringAppend(&newName, "_", 1); native =3D Tcl_DStringAppend(&newName, native, -1); symbol =3D NSLookupSymbolInImage(dyld_lib, native, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); if(symbol) { *proc1Ptr =3D NSAddressOfSymbol(symbol); *clientDataPtr =3D NSModuleForSymbol(symbol); } else { *proc1Ptr=3DNULL; *clientDataPtr=3DNULL; } Tcl_DStringFree(&newName); Tcl_DStringFree(&ds); native =3D Tcl_UtfToExternalDString(NULL, sym2, -1, &ds); Tcl_DStringInit(&newName); Tcl_DStringAppend(&newName, "_", 1); native =3D Tcl_DStringAppend(&newName, native, -1); symbol =3D NSLookupSymbolInImage(dyld_lib, native, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); if(symbol) { *proc2Ptr =3D NSAddressOfSymbol(symbol); } else { *proc2Ptr=3DNULL; } Tcl_DStringFree(&newName); Tcl_DStringFree(&ds); =A0=A0 return TCL_OK; } ------------------------ end paste into tclLoadDyld.c ----------------------= --- -- ** Daniel A. Steffen ** "And now to something completely ** Department of Mathematics ** different" Monty Python ** Macquarie University ** <mailto:st...@ma...> ** NSW 2109 Australia ** <http://www.maths.mq.edu.au/~steffen/> |