Debian Codegraph @

pTk/mTk/tclUnix/tclLoadDl.c

raw symbols SCIP
1 /*
2 * tclLoadDl.c --
3 *
4 * This procedure provides a version of the TclLoadFile that
5 * works with the "dlopen" and "dlsym" library procedures for
6 * dynamic loading.
7 *
8 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
9 *
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 *
13 * RCS: @(#) $Id: tclLoadDl.c,v 1.13 2002/10/10 12:25:53 vincentdarley Exp $
14 */
15
16 #include "tclInt.h"
17 #ifdef NO_DLFCN_H
18 # include "../compat/dlfcn.h"
19 #else
20 # include <dlfcn.h>
21 #endif
22
23 /*
24 * In some systems, like SunOS 4.1.3, the RTLD_NOW flag isn't defined
25 * and this argument to dlopen must always be 1. The RTLD_GLOBAL
26 * flag is needed on some systems (e.g. SCO and UnixWare) but doesn't
27 * exist on others; if it doesn't exist, set it to 0 so it has no effect.
28 */
29
30 #ifndef RTLD_NOW
31 # define RTLD_NOW 1
32 #endif
33
34 #ifndef RTLD_GLOBAL
35 # define RTLD_GLOBAL 0
36 #endif
37
38 /*
39 *---------------------------------------------------------------------------
40 *
41 * TclpDlopen --
42 *
43 * Dynamically loads a binary code file into memory and returns
44 * a handle to the new code.
45 *
46 * Results:
47 * A standard Tcl completion code. If an error occurs, an error
48 * message is left in the interp's result.
49 *
50 * Side effects:
51 * New code suddenly appears in memory.
52 *
53 *---------------------------------------------------------------------------
54 */
55
56 int
57 TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr)
58 Tcl_Interp *interp; /* Used for error reporting. */
59 Tcl_Obj *pathPtr; /* Name of the file containing the desired
60 * code (UTF-8). */
61 Tcl_LoadHandle *loadHandle; /* Filled with token for dynamically loaded
62 * file which will be passed back to
63 * (*unloadProcPtr)() to unload the file. */
64 Tcl_FSUnloadFileProc **unloadProcPtr;
65 /* Filled with address of Tcl_FSUnloadFileProc
66 * function which should be used for
67 * this file. */
68 {
69 VOID *handle;
70 CONST char *native;
71
72 /*
73 * First try the full path the user gave us. This is particularly
74 * important if the cwd is inside a vfs, and we are trying to load
75 * using a relative path.
76 */
77 native = Tcl_FSGetNativePath(pathPtr);
78 handle = dlopen(native, RTLD_NOW | RTLD_GLOBAL);
79 if (handle == NULL) {
80 /*
81 * Let the OS loader examine the binary search path for
82 * whatever string the user gave us which hopefully refers
83 * to a file on the binary path
84 */
85 Tcl_DString ds;
86 char *fileName = Tcl_GetString(pathPtr);
87 native = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds);
88 handle = dlopen(native, RTLD_NOW | RTLD_GLOBAL);
89 Tcl_DStringFree(&ds);
90 }
91
92 if (handle == NULL) {
93 Tcl_AppendResult(interp, "couldn't load file \"",
94 Tcl_GetString(pathPtr),
95 "\": ", dlerror(), (char *) NULL);
96 return TCL_ERROR;
97 }
98
99 *unloadProcPtr = &TclpUnloadFile;
100 *loadHandle = (Tcl_LoadHandle)handle;
101 return TCL_OK;
102 }
103
104 /*
105 *----------------------------------------------------------------------
106 *
107 * TclpFindSymbol --
108 *
109 * Looks up a symbol, by name, through a handle associated with
110 * a previously loaded piece of code (shared library).
111 *
112 * Results:
113 * Returns a pointer to the function associated with 'symbol' if
114 * it is found. Otherwise returns NULL and may leave an error
115 * message in the interp's result.
116 *
117 *----------------------------------------------------------------------
118 */
119 Tcl_PackageInitProc*
120 TclpFindSymbol(interp, loadHandle, symbol)
121 Tcl_Interp *interp;
122 Tcl_LoadHandle loadHandle;
123 CONST char *symbol;
124 {
125 CONST char *native;
126 Tcl_DString newName, ds;
127 VOID *handle = (VOID*)loadHandle;
128 Tcl_PackageInitProc *proc;
129 /*
130 * Some platforms still add an underscore to the beginning of symbol
131 * names. If we can't find a name without an underscore, try again
132 * with the underscore.
133 */
134
135 native = Tcl_UtfToExternalDString(NULL, symbol, -1, &ds);
136 proc = (Tcl_PackageInitProc *) dlsym(handle, /* INTL: Native. */
137 native);
138 if (proc == NULL) {
139 Tcl_DStringInit(&newName);
140 Tcl_DStringAppend(&newName, "_", 1);
141 native = Tcl_DStringAppend(&newName, native, -1);
142 proc = (Tcl_PackageInitProc *) dlsym(handle, /* INTL: Native. */
143 native);
144 Tcl_DStringFree(&newName);
145 }
146 Tcl_DStringFree(&ds);
147
148 return proc;
149 }
150
151 /*
152 *----------------------------------------------------------------------
153 *
154 * TclpUnloadFile --
155 *
156 * Unloads a dynamically loaded binary code file from memory.
157 * Code pointers in the formerly loaded file are no longer valid
158 * after calling this function.
159 *
160 * Results:
161 * None.
162 *
163 * Side effects:
164 * Code removed from memory.
165 *
166 *----------------------------------------------------------------------
167 */
168
169 void
170 TclpUnloadFile(loadHandle)
171 Tcl_LoadHandle loadHandle; /* loadHandle returned by a previous call
172 * to TclpDlopen(). The loadHandle is
173 * a token that represents the loaded
174 * file. */
175 {
176 VOID *handle;
177
178 handle = (VOID *) loadHandle;
179 dlclose(handle);
180 }
181
182 /*
183 *----------------------------------------------------------------------
184 *
185 * TclGuessPackageName --
186 *
187 * If the "load" command is invoked without providing a package
188 * name, this procedure is invoked to try to figure it out.
189 *
190 * Results:
191 * Always returns 0 to indicate that we couldn't figure out a
192 * package name; generic code will then try to guess the package
193 * from the file name. A return value of 1 would have meant that
194 * we figured out the package name and put it in bufPtr.
195 *
196 * Side effects:
197 * None.
198 *
199 *----------------------------------------------------------------------
200 */
201
202 int
203 TclGuessPackageName(fileName, bufPtr)
204 CONST char *fileName; /* Name of file containing package (already
205 * translated to local form if needed). */
206 Tcl_DString *bufPtr; /* Initialized empty dstring. Append
207 * package name to this if possible. */
208 {
209 return 0;
210 }