| 1 |
/* |
| 2 |
* tclLoadOSF.c -- |
| 3 |
* |
| 4 |
* This function provides a version of the TclLoadFile that works under |
| 5 |
* OSF/1 1.0/1.1/1.2 and related systems, utilizing the old OSF/1 |
| 6 |
* /sbin/loader and /usr/include/loader.h. OSF/1 versions from 1.3 and on |
| 7 |
* use ELF, rtld, and dlopen()[/usr/include/ldfcn.h]. |
| 8 |
* |
| 9 |
* This is useful for: |
| 10 |
* OSF/1 1.0, 1.1, 1.2 (from OSF) |
| 11 |
* includes: MK4 and AD1 (from OSF RI) |
| 12 |
* OSF/1 1.3 (from OSF) using ROSE |
| 13 |
* HP OSF/1 1.0 ("Acorn") using COFF |
| 14 |
* |
| 15 |
* This is likely to be useful for: |
| 16 |
* Paragon OSF/1 (from Intel) |
| 17 |
* HI-OSF/1 (from Hitachi) |
| 18 |
* |
| 19 |
* This is NOT to be used on: |
| 20 |
* Digitial Alpha OSF/1 systems |
| 21 |
* OSF/1 1.3 or later (from OSF) using ELF |
| 22 |
* includes: MK6, MK7, AD2, AD3 (from OSF RI) |
| 23 |
* |
| 24 |
* This approach to things was utter @&^#; thankfully, OSF/1 eventually |
| 25 |
* supported dlopen(). |
| 26 |
* |
| 27 |
* John Robert LoVerso <[email protected]> |
| 28 |
* |
| 29 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 30 |
* |
| 31 |
* See the file "license.terms" for information on usage and redistribution of |
| 32 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 33 |
*/ |
| 34 |
|
| 35 |
#include "tclInt.h" |
| 36 |
#include <sys/types.h> |
| 37 |
#include <loader.h> |
| 38 |
|
| 39 |
/* |
| 40 |
* Static functions defined within this file. |
| 41 |
*/ |
| 42 |
|
| 43 |
static void * FindSymbol(Tcl_Interp *interp, |
| 44 |
Tcl_LoadHandle loadHandle, const char* symbol); |
| 45 |
static void UnloadFile(Tcl_LoadHandle handle); |
| 46 |
|
| 47 |
/* |
| 48 |
*---------------------------------------------------------------------- |
| 49 |
* |
| 50 |
* TclpDlopen -- |
| 51 |
* |
| 52 |
* Dynamically loads a binary code file into memory and returns a handle |
| 53 |
* to the new code. |
| 54 |
* |
| 55 |
* Results: |
| 56 |
* A standard Tcl completion code. If an error occurs, an error message |
| 57 |
* is left in the interp's result. |
| 58 |
* |
| 59 |
* Side effects: |
| 60 |
* New code suddenly appears in memory. |
| 61 |
* |
| 62 |
*---------------------------------------------------------------------- |
| 63 |
*/ |
| 64 |
|
| 65 |
int |
| 66 |
TclpDlopen( |
| 67 |
Tcl_Interp *interp, /* Used for error reporting. */ |
| 68 |
Tcl_Obj *pathPtr, /* Name of the file containing the desired |
| 69 |
* code (UTF-8). */ |
| 70 |
Tcl_LoadHandle *loadHandle, /* Filled with token for dynamically loaded |
| 71 |
* file which will be passed back to |
| 72 |
* (*unloadProcPtr)() to unload the file. */ |
| 73 |
Tcl_FSUnloadFileProc **unloadProcPtr, |
| 74 |
/* Filled with address of Tcl_FSUnloadFileProc |
| 75 |
* function which should be used for this |
| 76 |
* file. */ |
| 77 |
int flags) |
| 78 |
{ |
| 79 |
Tcl_LoadHandle newHandle; |
| 80 |
ldr_module_t lm; |
| 81 |
char *pkg; |
| 82 |
char *fileName = Tcl_GetString(pathPtr); |
| 83 |
const char *native; |
| 84 |
|
| 85 |
/* |
| 86 |
* First try the full path the user gave us. This is particularly |
| 87 |
* important if the cwd is inside a vfs, and we are trying to load using a |
| 88 |
* relative path. |
| 89 |
*/ |
| 90 |
|
| 91 |
native = Tcl_FSGetNativePath(pathPtr); |
| 92 |
lm = (Tcl_PackageInitProc *) load(native, LDR_NOFLAGS); |
| 93 |
|
| 94 |
if (lm == LDR_NULL_MODULE) { |
| 95 |
/* |
| 96 |
* Let the OS loader examine the binary search path for whatever |
| 97 |
* string the user gave us which hopefully refers to a file on the |
| 98 |
* binary path |
| 99 |
*/ |
| 100 |
|
| 101 |
Tcl_DString ds; |
| 102 |
|
| 103 |
native = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds); |
| 104 |
lm = (Tcl_PackageInitProc *) load(native, LDR_NOFLAGS); |
| 105 |
Tcl_DStringFree(&ds); |
| 106 |
} |
| 107 |
|
| 108 |
if (lm == LDR_NULL_MODULE) { |
| 109 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 110 |
"couldn't load file \"%s\": %s", |
| 111 |
fileName, Tcl_PosixError(interp))); |
| 112 |
return TCL_ERROR; |
| 113 |
} |
| 114 |
|
| 115 |
*clientDataPtr = NULL; |
| 116 |
|
| 117 |
/* |
| 118 |
* My convention is to use a [OSF loader] package name the same as shlib, |
| 119 |
* since the idiots never implemented ldr_lookup() and it is otherwise |
| 120 |
* impossible to get a package name given a module. |
| 121 |
* |
| 122 |
* I build loadable modules with a makefile rule like |
| 123 |
* ld ... -export $@: -o $@ $(OBJS) |
| 124 |
*/ |
| 125 |
|
| 126 |
if ((pkg = strrchr(fileName, '/')) == NULL) { |
| 127 |
pkg = fileName; |
| 128 |
} else { |
| 129 |
pkg++; |
| 130 |
} |
| 131 |
newHandle = ckalloc(sizeof(*newHandle)); |
| 132 |
newHandle->clientData = pkg; |
| 133 |
newHandle->findSymbolProcPtr = &FindSymbol; |
| 134 |
newHandle->unloadFileProcPtr = &UnloadFile; |
| 135 |
*loadHandle = newHandle; |
| 136 |
*unloadProcPtr = &UnloadFile; |
| 137 |
return TCL_OK; |
| 138 |
} |
| 139 |
|
| 140 |
/* |
| 141 |
*---------------------------------------------------------------------- |
| 142 |
* |
| 143 |
* FindSymbol -- |
| 144 |
* |
| 145 |
* Looks up a symbol, by name, through a handle associated with a |
| 146 |
* previously loaded piece of code (shared library). |
| 147 |
* |
| 148 |
* Results: |
| 149 |
* Returns a pointer to the function associated with 'symbol' if it is |
| 150 |
* found. Otherwise returns NULL and may leave an error message in the |
| 151 |
* interp's result. |
| 152 |
* |
| 153 |
*---------------------------------------------------------------------- |
| 154 |
*/ |
| 155 |
|
| 156 |
static void * |
| 157 |
FindSymbol( |
| 158 |
Tcl_Interp *interp, |
| 159 |
Tcl_LoadHandle loadHandle, |
| 160 |
const char *symbol) |
| 161 |
{ |
| 162 |
void *retval = ldr_lookup_package((char *) loadHandle, symbol); |
| 163 |
|
| 164 |
if (retval == NULL && interp != NULL) { |
| 165 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 166 |
"cannot find symbol \"%s\"", symbol)); |
| 167 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "LOAD_SYMBOL", symbol, (char *)NULL); |
| 168 |
} |
| 169 |
return retval; |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
*---------------------------------------------------------------------- |
| 174 |
* |
| 175 |
* UnloadFile -- |
| 176 |
* |
| 177 |
* Unloads a dynamically loaded binary code file from memory. Code |
| 178 |
* pointers in the formerly loaded file are no longer valid after calling |
| 179 |
* this function. |
| 180 |
* |
| 181 |
* Results: |
| 182 |
* None. |
| 183 |
* |
| 184 |
* Side effects: |
| 185 |
* Does nothing. Can anything be done? |
| 186 |
* |
| 187 |
*---------------------------------------------------------------------- |
| 188 |
*/ |
| 189 |
|
| 190 |
static void |
| 191 |
UnloadFile( |
| 192 |
Tcl_LoadHandle loadHandle) /* loadHandle returned by a previous call to |
| 193 |
* TclpDlopen(). The loadHandle is a token |
| 194 |
* that represents the loaded file. */ |
| 195 |
{ |
| 196 |
ckfree(loadHandle); |
| 197 |
} |
| 198 |
|
| 199 |
/* |
| 200 |
*---------------------------------------------------------------------- |
| 201 |
* |
| 202 |
* TclGuessPackageName -- |
| 203 |
* |
| 204 |
* If the "load" command is invoked without providing a package name, |
| 205 |
* this function is invoked to try to figure it out. |
| 206 |
* |
| 207 |
* Results: |
| 208 |
* Always returns 0 to indicate that we couldn't figure out a package |
| 209 |
* name; generic code will then try to guess the package from the file |
| 210 |
* name. A return value of 1 would have meant that we figured out the |
| 211 |
* package name and put it in bufPtr. |
| 212 |
* |
| 213 |
* Side effects: |
| 214 |
* None. |
| 215 |
* |
| 216 |
*---------------------------------------------------------------------- |
| 217 |
*/ |
| 218 |
|
| 219 |
int |
| 220 |
TclGuessPackageName( |
| 221 |
const char *fileName, /* Name of file containing package (already |
| 222 |
* translated to local form if needed). */ |
| 223 |
Tcl_DString *bufPtr) /* Initialized empty dstring. Append package |
| 224 |
* name to this if possible. */ |
| 225 |
{ |
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
/* |
| 230 |
* Local Variables: |
| 231 |
* mode: c |
| 232 |
* c-basic-offset: 4 |
| 233 |
* fill-column: 78 |
| 234 |
* End: |
| 235 |
*/ |