| 1 |
/* |
| 2 |
* tclLoadShl.c -- |
| 3 |
* |
| 4 |
* This procedure provides a version of the TclLoadFile that works |
| 5 |
* with the "shl_load" and "shl_findsym" library procedures for |
| 6 |
* dynamic loading (e.g. for HP machines). |
| 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: tclLoadShl.c,v 1.13 2002/10/10 12:25:53 vincentdarley Exp $ |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <dl.h> |
| 17 |
|
| 18 |
/* |
| 19 |
* On some HP machines, dl.h defines EXTERN; remove that definition. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifdef EXTERN |
| 23 |
# undef EXTERN |
| 24 |
#endif |
| 25 |
|
| 26 |
#include "tclInt.h" |
| 27 |
|
| 28 |
/* |
| 29 |
*---------------------------------------------------------------------- |
| 30 |
* |
| 31 |
* TclpDlopen -- |
| 32 |
* |
| 33 |
* Dynamically loads a binary code file into memory and returns |
| 34 |
* a handle to the new code. |
| 35 |
* |
| 36 |
* Results: |
| 37 |
* A standard Tcl completion code. If an error occurs, an error |
| 38 |
* message is left in the interp's result. |
| 39 |
* |
| 40 |
* Side effects: |
| 41 |
* New code suddenly appears in memory. |
| 42 |
* |
| 43 |
*---------------------------------------------------------------------- |
| 44 |
*/ |
| 45 |
|
| 46 |
int |
| 47 |
TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr) |
| 48 |
Tcl_Interp *interp; /* Used for error reporting. */ |
| 49 |
Tcl_Obj *pathPtr; /* Name of the file containing the desired |
| 50 |
* code (UTF-8). */ |
| 51 |
Tcl_LoadHandle *loadHandle; /* Filled with token for dynamically loaded |
| 52 |
* file which will be passed back to |
| 53 |
* (*unloadProcPtr)() to unload the file. */ |
| 54 |
Tcl_FSUnloadFileProc **unloadProcPtr; |
| 55 |
/* Filled with address of Tcl_FSUnloadFileProc |
| 56 |
* function which should be used for |
| 57 |
* this file. */ |
| 58 |
{ |
| 59 |
shl_t handle; |
| 60 |
CONST char *native; |
| 61 |
char *fileName = Tcl_GetString(pathPtr); |
| 62 |
|
| 63 |
/* |
| 64 |
* The flags below used to be BIND_IMMEDIATE; they were changed at |
| 65 |
* the suggestion of Wolfgang Kechel ([email protected]): "This |
| 66 |
* enables verbosity for missing symbols when loading a shared lib |
| 67 |
* and allows to load libtk8.0.sl into tclsh8.0 without problems. |
| 68 |
* In general, this delays resolving symbols until they are actually |
| 69 |
* needed. Shared libs do no longer need all libraries linked in |
| 70 |
* when they are build." |
| 71 |
*/ |
| 72 |
|
| 73 |
|
| 74 |
/* |
| 75 |
* First try the full path the user gave us. This is particularly |
| 76 |
* important if the cwd is inside a vfs, and we are trying to load |
| 77 |
* using a relative path. |
| 78 |
*/ |
| 79 |
native = Tcl_FSGetNativePath(pathPtr); |
| 80 |
handle = shl_load(native, |
| 81 |
BIND_DEFERRED|BIND_VERBOSE|DYNAMIC_PATH, 0L); |
| 82 |
|
| 83 |
if (handle == NULL) { |
| 84 |
/* |
| 85 |
* Let the OS loader examine the binary search path for |
| 86 |
* whatever string the user gave us which hopefully refers |
| 87 |
* to a file on the binary path |
| 88 |
*/ |
| 89 |
Tcl_DString ds; |
| 90 |
native = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds); |
| 91 |
handle = shl_load(native, |
| 92 |
BIND_DEFERRED|BIND_VERBOSE|DYNAMIC_PATH, 0L); |
| 93 |
Tcl_DStringFree(&ds); |
| 94 |
} |
| 95 |
|
| 96 |
if (handle == NULL) { |
| 97 |
Tcl_AppendResult(interp, "couldn't load file \"", fileName, |
| 98 |
"\": ", Tcl_PosixError(interp), (char *) NULL); |
| 99 |
return TCL_ERROR; |
| 100 |
} |
| 101 |
*loadHandle = (Tcl_LoadHandle) handle; |
| 102 |
*unloadProcPtr = &TclpUnloadFile; |
| 103 |
return TCL_OK; |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
*---------------------------------------------------------------------- |
| 108 |
* |
| 109 |
* TclpFindSymbol -- |
| 110 |
* |
| 111 |
* Looks up a symbol, by name, through a handle associated with |
| 112 |
* a previously loaded piece of code (shared library). |
| 113 |
* |
| 114 |
* Results: |
| 115 |
* Returns a pointer to the function associated with 'symbol' if |
| 116 |
* it is found. Otherwise returns NULL and may leave an error |
| 117 |
* message in the interp's result. |
| 118 |
* |
| 119 |
*---------------------------------------------------------------------- |
| 120 |
*/ |
| 121 |
Tcl_PackageInitProc* |
| 122 |
TclpFindSymbol(interp, loadHandle, symbol) |
| 123 |
Tcl_Interp *interp; |
| 124 |
Tcl_LoadHandle loadHandle; |
| 125 |
CONST char *symbol; |
| 126 |
{ |
| 127 |
Tcl_DString newName; |
| 128 |
Tcl_PackageInitProc *proc=NULL; |
| 129 |
shl_t handle = (shl_t)loadHandle; |
| 130 |
/* |
| 131 |
* Some versions of the HP system software still use "_" at the |
| 132 |
* beginning of exported symbols while others don't; try both |
| 133 |
* forms of each name. |
| 134 |
*/ |
| 135 |
|
| 136 |
if (shl_findsym(&handle, symbol, (short) TYPE_PROCEDURE, (void *) &proc) |
| 137 |
!= 0) { |
| 138 |
Tcl_DStringInit(&newName); |
| 139 |
Tcl_DStringAppend(&newName, "_", 1); |
| 140 |
Tcl_DStringAppend(&newName, symbol, -1); |
| 141 |
if (shl_findsym(&handle, Tcl_DStringValue(&newName), |
| 142 |
(short) TYPE_PROCEDURE, (void *) &proc) != 0) { |
| 143 |
proc = NULL; |
| 144 |
} |
| 145 |
Tcl_DStringFree(&newName); |
| 146 |
} |
| 147 |
return proc; |
| 148 |
} |
| 149 |
|
| 150 |
/* |
| 151 |
*---------------------------------------------------------------------- |
| 152 |
* |
| 153 |
* TclpUnloadFile -- |
| 154 |
* |
| 155 |
* Unloads a dynamically loaded binary code file from memory. |
| 156 |
* Code pointers in the formerly loaded file are no longer valid |
| 157 |
* after calling this function. |
| 158 |
* |
| 159 |
* Results: |
| 160 |
* None. |
| 161 |
* |
| 162 |
* Side effects: |
| 163 |
* Code removed from memory. |
| 164 |
* |
| 165 |
*---------------------------------------------------------------------- |
| 166 |
*/ |
| 167 |
|
| 168 |
void |
| 169 |
TclpUnloadFile(loadHandle) |
| 170 |
Tcl_LoadHandle loadHandle; /* loadHandle returned by a previous call |
| 171 |
* to TclpDlopen(). The loadHandle is |
| 172 |
* a token that represents the loaded |
| 173 |
* file. */ |
| 174 |
{ |
| 175 |
shl_t handle; |
| 176 |
|
| 177 |
handle = (shl_t) loadHandle; |
| 178 |
shl_unload(handle); |
| 179 |
} |
| 180 |
|
| 181 |
/* |
| 182 |
*---------------------------------------------------------------------- |
| 183 |
* |
| 184 |
* TclGuessPackageName -- |
| 185 |
* |
| 186 |
* If the "load" command is invoked without providing a package |
| 187 |
* name, this procedure is invoked to try to figure it out. |
| 188 |
* |
| 189 |
* Results: |
| 190 |
* Always returns 0 to indicate that we couldn't figure out a |
| 191 |
* package name; generic code will then try to guess the package |
| 192 |
* from the file name. A return value of 1 would have meant that |
| 193 |
* we figured out the package name and put it in bufPtr. |
| 194 |
* |
| 195 |
* Side effects: |
| 196 |
* None. |
| 197 |
* |
| 198 |
*---------------------------------------------------------------------- |
| 199 |
*/ |
| 200 |
|
| 201 |
int |
| 202 |
TclGuessPackageName(fileName, bufPtr) |
| 203 |
CONST char *fileName; /* Name of file containing package (already |
| 204 |
* translated to local form if needed). */ |
| 205 |
Tcl_DString *bufPtr; /* Initialized empty dstring. Append |
| 206 |
* package name to this if possible. */ |
| 207 |
{ |
| 208 |
return 0; |
| 209 |
} |