Debian Codegraph @

pTk/mTk/tclUnix/tclLoadDyld.c

raw symbols SCIP
1 /*
2 * tclLoadDyld.c --
3 *
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 ([email protected]).
7 * This works on Mac OS X.
8 *
9 * Copyright (c) 1995 Apple Computer, Inc.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tclLoadDyld.c,v 1.14 2002/10/29 00:04:08 das Exp $
15 */
16
17 #include "tclInt.h"
18 #include "tclPort.h"
19 #include <mach-o/dyld.h>
20
21 typedef struct Tcl_DyldModuleHandle {
22 struct Tcl_DyldModuleHandle *nextModuleHandle;
23 NSModule module;
24 } Tcl_DyldModuleHandle;
25
26 typedef struct Tcl_DyldLoadHandle {
27 const struct mach_header *dyld_lib;
28 Tcl_DyldModuleHandle *firstModuleHandle;
29 } Tcl_DyldLoadHandle;
30
31 /*
32 *----------------------------------------------------------------------
33 *
34 * TclpDlopen --
35 *
36 * Dynamically loads a binary code file into memory and returns
37 * a handle to the new code.
38 *
39 * Results:
40 * A standard Tcl completion code. If an error occurs, an error
41 * message is left in the interpreter's result.
42 *
43 * Side effects:
44 * New code suddenly appears in memory.
45 *
46 *----------------------------------------------------------------------
47 */
48
49 int
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
53 * code (UTF-8). */
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
60 * this file. */
61 {
62 Tcl_DyldLoadHandle *dyldLoadHandle;
63 const struct mach_header *dyld_lib;
64 CONST char *native;
65
66 /*
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.
70 */
71 native = Tcl_FSGetNativePath(pathPtr);
72 dyld_lib = NSAddImage(native,
73 NSADDIMAGE_OPTION_WITH_SEARCHING |
74 NSADDIMAGE_OPTION_RETURN_ON_ERROR);
75
76 if (!dyld_lib) {
77 /*
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
81 */
82 Tcl_DString ds;
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);
88 Tcl_DStringFree(&ds);
89 }
90
91 if (!dyld_lib) {
92 NSLinkEditErrors editError;
93 char *name, *msg;
94 NSLinkEditError(&editError, &errno, &name, &msg);
95 Tcl_AppendResult(interp, msg, (char *) NULL);
96 return TCL_ERROR;
97 }
98
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;
105 return TCL_OK;
106 }
107
108 /*
109 *----------------------------------------------------------------------
110 *
111 * TclpFindSymbol --
112 *
113 * Looks up a symbol, by name, through a handle associated with
114 * a previously loaded piece of code (shared library).
115 *
116 * Results:
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.
120 *
121 *----------------------------------------------------------------------
122 */
123 Tcl_PackageInitProc*
124 TclpFindSymbol(interp, loadHandle, symbol)
125 Tcl_Interp *interp;
126 Tcl_LoadHandle loadHandle;
127 CONST char *symbol;
128 {
129 NSSymbol nsSymbol;
130 CONST char *native;
131 Tcl_DString newName, ds;
132 Tcl_PackageInitProc* proc = NULL;
133 Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle;
134 /*
135 * dyld adds an underscore to the beginning of symbol names.
136 */
137
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);
145 if(nsSymbol) {
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;
153 }
154 } else {
155 NSLinkEditErrors editError;
156 char *name, *msg;
157 NSLinkEditError(&editError, &errno, &name, &msg);
158 Tcl_AppendResult(interp, msg, (char *) NULL);
159 }
160 Tcl_DStringFree(&newName);
161 Tcl_DStringFree(&ds);
162
163 return proc;
164 }
165
166 /*
167 *----------------------------------------------------------------------
168 *
169 * TclpUnloadFile --
170 *
171 * Unloads a dynamically loaded binary code file from memory.
172 * Code pointers in the formerly loaded file are no longer valid
173 * after calling this function.
174 *
175 * Results:
176 * None.
177 *
178 * Side effects:
179 * Code dissapears from memory.
180 * Note that this is a no-op on older (OpenStep) versions of dyld.
181 *
182 *----------------------------------------------------------------------
183 */
184
185 void
186 TclpUnloadFile(loadHandle)
187 Tcl_LoadHandle loadHandle; /* loadHandle returned by a previous call
188 * to TclpDlopen(). The loadHandle is
189 * a token that represents the loaded
190 * file. */
191 {
192 Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle;
193 Tcl_DyldModuleHandle *dyldModuleHandle = dyldLoadHandle->firstModuleHandle;
194 void *ptr;
195
196 while (dyldModuleHandle) {
197 NSUnLinkModule(dyldModuleHandle->module, NSUNLINKMODULE_OPTION_NONE);
198 ptr = dyldModuleHandle;
199 dyldModuleHandle = dyldModuleHandle->nextModuleHandle;
200 ckfree(ptr);
201 }
202 ckfree(dyldLoadHandle);
203 }
204
205 /*
206 *----------------------------------------------------------------------
207 *
208 * TclGuessPackageName --
209 *
210 * If the "load" command is invoked without providing a package
211 * name, this procedure is invoked to try to figure it out.
212 *
213 * Results:
214 * Always returns 0 to indicate that we couldn't figure out a
215 * package name; generic code will then try to guess the package
216 * from the file name. A return value of 1 would have meant that
217 * we figured out the package name and put it in bufPtr.
218 *
219 * Side effects:
220 * None.
221 *
222 *----------------------------------------------------------------------
223 */
224
225 int
226 TclGuessPackageName(fileName, bufPtr)
227 CONST char *fileName; /* Name of file containing package (already
228 * translated to local form if needed). */
229 Tcl_DString *bufPtr; /* Initialized empty dstring. Append
230 * package name to this if possible. */
231 {
232 return 0;
233 }