4 * This procedure provides a version of the TclLoadFile that
5 * works with Apple's dyld dynamic loading. This file
6 * provided by Wilfredo Sanchez (wsanchez@apple.com).
7 * This works on Mac OS X.
9 * Copyright (c) 1995 Apple Computer, Inc.
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
19 #include <mach-o/dyld.h>
21 typedef struct Tcl_DyldModuleHandle {
22 struct Tcl_DyldModuleHandle *nextModuleHandle;
24 } Tcl_DyldModuleHandle;
26 typedef struct Tcl_DyldLoadHandle {
27 const struct mach_header *dyld_lib;
28 Tcl_DyldModuleHandle *firstModuleHandle;
32 *----------------------------------------------------------------------
36 * Dynamically loads a binary code file into memory and returns
37 * a handle to the new code.
40 * A standard Tcl completion code. If an error occurs, an error
41 * message is left in the interpreter's result.
44 * New code suddenly appears in memory.
46 *----------------------------------------------------------------------
50 TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr)
51 Tcl_Interp *interp; /* Used for error reporting. */
52 Tcl_Obj *pathPtr; /* Name of the file containing the desired
54 Tcl_LoadHandle *loadHandle; /* Filled with token for dynamically loaded
55 * file which will be passed back to
56 * (*unloadProcPtr)() to unload the file. */
57 Tcl_FSUnloadFileProc **unloadProcPtr;
58 /* Filled with address of Tcl_FSUnloadFileProc
59 * function which should be used for
62 Tcl_DyldLoadHandle *dyldLoadHandle;
63 const struct mach_header *dyld_lib;
67 * First try the full path the user gave us. This is particularly
68 * important if the cwd is inside a vfs, and we are trying to load
69 * using a relative path.
71 native = Tcl_FSGetNativePath(pathPtr);
72 dyld_lib = NSAddImage(native,
73 NSADDIMAGE_OPTION_WITH_SEARCHING |
74 NSADDIMAGE_OPTION_RETURN_ON_ERROR);
78 * Let the OS loader examine the binary search path for
79 * whatever string the user gave us which hopefully refers
80 * to a file on the binary path
83 char *fileName = Tcl_GetString(pathPtr);
84 native = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds);
85 dyld_lib = NSAddImage(native,
86 NSADDIMAGE_OPTION_WITH_SEARCHING |
87 NSADDIMAGE_OPTION_RETURN_ON_ERROR);
92 NSLinkEditErrors editError;
94 NSLinkEditError(&editError, &errno, &name, &msg);
95 Tcl_AppendResult(interp, msg, (char *) NULL);
99 dyldLoadHandle = (Tcl_DyldLoadHandle *) ckalloc(sizeof(Tcl_DyldLoadHandle));
100 if (!dyldLoadHandle) return TCL_ERROR;
101 dyldLoadHandle->dyld_lib = dyld_lib;
102 dyldLoadHandle->firstModuleHandle = NULL;
103 *loadHandle = (Tcl_LoadHandle) dyldLoadHandle;
104 *unloadProcPtr = &TclpUnloadFile;
109 *----------------------------------------------------------------------
113 * Looks up a symbol, by name, through a handle associated with
114 * a previously loaded piece of code (shared library).
117 * Returns a pointer to the function associated with 'symbol' if
118 * it is found. Otherwise returns NULL and may leave an error
119 * message in the interp's result.
121 *----------------------------------------------------------------------
124 TclpFindSymbol(interp, loadHandle, symbol)
126 Tcl_LoadHandle loadHandle;
131 Tcl_DString newName, ds;
132 Tcl_PackageInitProc* proc = NULL;
133 Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle;
135 * dyld adds an underscore to the beginning of symbol names.
138 native = Tcl_UtfToExternalDString(NULL, symbol, -1, &ds);
139 Tcl_DStringInit(&newName);
140 Tcl_DStringAppend(&newName, "_", 1);
141 native = Tcl_DStringAppend(&newName, native, -1);
142 nsSymbol = NSLookupSymbolInImage(dyldLoadHandle->dyld_lib, native,
143 NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW |
144 NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
146 Tcl_DyldModuleHandle *dyldModuleHandle;
147 proc = NSAddressOfSymbol(nsSymbol);
148 dyldModuleHandle = (Tcl_DyldModuleHandle *) ckalloc(sizeof(Tcl_DyldModuleHandle));
149 if (dyldModuleHandle) {
150 dyldModuleHandle->module = NSModuleForSymbol(nsSymbol);
151 dyldModuleHandle->nextModuleHandle = dyldLoadHandle->firstModuleHandle;
152 dyldLoadHandle->firstModuleHandle = dyldModuleHandle;
155 Tcl_DStringFree(&newName);
156 Tcl_DStringFree(&ds);
162 *----------------------------------------------------------------------
166 * Unloads a dynamically loaded binary code file from memory.
167 * Code pointers in the formerly loaded file are no longer valid
168 * after calling this function.
174 * Code dissapears from memory.
175 * Note that this is a no-op on older (OpenStep) versions of dyld.
177 *----------------------------------------------------------------------
181 TclpUnloadFile(loadHandle)
182 Tcl_LoadHandle loadHandle; /* loadHandle returned by a previous call
183 * to TclpDlopen(). The loadHandle is
184 * a token that represents the loaded
187 Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle;
188 Tcl_DyldModuleHandle *dyldModuleHandle = dyldLoadHandle->firstModuleHandle;
191 while (dyldModuleHandle) {
192 NSUnLinkModule(dyldModuleHandle->module, NSUNLINKMODULE_OPTION_NONE);
193 ptr = dyldModuleHandle;
194 dyldModuleHandle = dyldModuleHandle->nextModuleHandle;
197 ckfree(dyldLoadHandle);
201 *----------------------------------------------------------------------
203 * TclGuessPackageName --
205 * If the "load" command is invoked without providing a package
206 * name, this procedure is invoked to try to figure it out.
209 * Always returns 0 to indicate that we couldn't figure out a
210 * package name; generic code will then try to guess the package
211 * from the file name. A return value of 1 would have meant that
212 * we figured out the package name and put it in bufPtr.
217 *----------------------------------------------------------------------
221 TclGuessPackageName(fileName, bufPtr)
222 CONST char *fileName; /* Name of file containing package (already
223 * translated to local form if needed). */
224 Tcl_DString *bufPtr; /* Initialized empty dstring. Append
225 * package name to this if possible. */