| 1 |
/* |
| 2 |
* tclWinLoad.c -- |
| 3 |
* |
| 4 |
* This function provides a version of the TclLoadFile that works with |
| 5 |
* the Windows "LoadLibrary" and "GetProcAddress" API for dynamic |
| 6 |
* loading. |
| 7 |
* |
| 8 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 9 |
* |
| 10 |
* See the file "license.terms" for information on usage and redistribution of |
| 11 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "tclWinInt.h" |
| 15 |
|
| 16 |
/* |
| 17 |
* Native name of the directory in the native filesystem where DLLs used in |
| 18 |
* this process are copied prior to loading, and mutex used to protect its |
| 19 |
* allocation. |
| 20 |
*/ |
| 21 |
|
| 22 |
static WCHAR *dllDirectoryName = NULL; |
| 23 |
static Tcl_Mutex dllDirectoryNameMutex; |
| 24 |
|
| 25 |
/* |
| 26 |
* Static functions defined within this file. |
| 27 |
*/ |
| 28 |
|
| 29 |
static void * FindSymbol(Tcl_Interp *interp, |
| 30 |
Tcl_LoadHandle loadHandle, const char *symbol); |
| 31 |
static int InitDLLDirectoryName(void); |
| 32 |
static void UnloadFile(Tcl_LoadHandle loadHandle); |
| 33 |
|
| 34 |
/* |
| 35 |
*---------------------------------------------------------------------- |
| 36 |
* |
| 37 |
* TclpDlopen -- |
| 38 |
* |
| 39 |
* Dynamically loads a binary code file into memory and returns a handle |
| 40 |
* to the new code. |
| 41 |
* |
| 42 |
* Results: |
| 43 |
* A standard Tcl completion code. If an error occurs, an error message |
| 44 |
* is left in the interp's result. |
| 45 |
* |
| 46 |
* Side effects: |
| 47 |
* New code suddenly appears in memory. |
| 48 |
* |
| 49 |
*---------------------------------------------------------------------- |
| 50 |
*/ |
| 51 |
|
| 52 |
int |
| 53 |
TclpDlopen( |
| 54 |
Tcl_Interp *interp, /* Used for error reporting. */ |
| 55 |
Tcl_Obj *pathPtr, /* Name of the file containing the desired |
| 56 |
* code (UTF-8). */ |
| 57 |
Tcl_LoadHandle *loadHandle, /* Filled with token for dynamically loaded |
| 58 |
* file which will be passed back to |
| 59 |
* (*unloadProcPtr)() to unload the file. */ |
| 60 |
Tcl_FSUnloadFileProc **unloadProcPtr, |
| 61 |
/* Filled with address of Tcl_FSUnloadFileProc |
| 62 |
* function which should be used for this |
| 63 |
* file. */ |
| 64 |
int flags) |
| 65 |
{ |
| 66 |
HINSTANCE hInstance = NULL; |
| 67 |
const WCHAR *nativeName; |
| 68 |
Tcl_LoadHandle handlePtr; |
| 69 |
DWORD firstError; |
| 70 |
(void)flags; |
| 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 using a |
| 75 |
* relative path. |
| 76 |
*/ |
| 77 |
|
| 78 |
nativeName = (const WCHAR *)Tcl_FSGetNativePath(pathPtr); |
| 79 |
if (nativeName != NULL) { |
| 80 |
hInstance = LoadLibraryExW(nativeName, NULL, |
| 81 |
LOAD_WITH_ALTERED_SEARCH_PATH); |
| 82 |
} |
| 83 |
if (hInstance == NULL) { |
| 84 |
/* |
| 85 |
* Let the OS loader examine the binary search path for whatever |
| 86 |
* string the user gave us which hopefully refers to a file on the |
| 87 |
* binary path. |
| 88 |
*/ |
| 89 |
|
| 90 |
Tcl_DString ds; |
| 91 |
|
| 92 |
/* |
| 93 |
* Remember the first error on load attempt to be used if the |
| 94 |
* second load attempt below also fails. |
| 95 |
*/ |
| 96 |
firstError = (nativeName == NULL) ? |
| 97 |
ERROR_MOD_NOT_FOUND : GetLastError(); |
| 98 |
|
| 99 |
nativeName = (WCHAR *)Tcl_WinUtfToTChar(Tcl_GetString(pathPtr), -1, &ds); |
| 100 |
hInstance = LoadLibraryExW(nativeName, NULL, |
| 101 |
LOAD_WITH_ALTERED_SEARCH_PATH); |
| 102 |
Tcl_DStringFree(&ds); |
| 103 |
} |
| 104 |
|
| 105 |
if (hInstance == NULL) { |
| 106 |
DWORD lastError; |
| 107 |
Tcl_Obj *errMsg; |
| 108 |
|
| 109 |
/* |
| 110 |
* We choose to only use the error from the second call if the first |
| 111 |
* call failed due to the file not being found. Else stick to the |
| 112 |
* first error for reporting purposes. |
| 113 |
*/ |
| 114 |
if (firstError == ERROR_MOD_NOT_FOUND || |
| 115 |
firstError == ERROR_DLL_NOT_FOUND) { |
| 116 |
lastError = GetLastError(); |
| 117 |
} else { |
| 118 |
lastError = firstError; |
| 119 |
} |
| 120 |
|
| 121 |
errMsg = Tcl_ObjPrintf("couldn't load library \"%s\": ", |
| 122 |
TclGetString(pathPtr)); |
| 123 |
|
| 124 |
/* |
| 125 |
* Check for possible DLL errors. This doesn't work quite right, |
| 126 |
* because Windows seems to only return ERROR_MOD_NOT_FOUND for just |
| 127 |
* about any problem, but it's better than nothing. It'd be even |
| 128 |
* better if there was a way to get what DLLs |
| 129 |
*/ |
| 130 |
|
| 131 |
if (interp) { |
| 132 |
switch (lastError) { |
| 133 |
case ERROR_MOD_NOT_FOUND: |
| 134 |
Tcl_SetErrorCode(interp, "WIN_LOAD", "MOD_NOT_FOUND", (char *)NULL); |
| 135 |
goto notFoundMsg; |
| 136 |
case ERROR_DLL_NOT_FOUND: |
| 137 |
Tcl_SetErrorCode(interp, "WIN_LOAD", "DLL_NOT_FOUND", (char *)NULL); |
| 138 |
notFoundMsg: |
| 139 |
Tcl_AppendToObj(errMsg, "this library or a dependent library" |
| 140 |
" could not be found in library path", -1); |
| 141 |
break; |
| 142 |
case ERROR_PROC_NOT_FOUND: |
| 143 |
Tcl_SetErrorCode(interp, "WIN_LOAD", "PROC_NOT_FOUND", (char *)NULL); |
| 144 |
Tcl_AppendToObj(errMsg, "A function specified in the import" |
| 145 |
" table could not be resolved by the system. Windows" |
| 146 |
" is not telling which one, I'm sorry.", -1); |
| 147 |
break; |
| 148 |
case ERROR_INVALID_DLL: |
| 149 |
Tcl_SetErrorCode(interp, "WIN_LOAD", "INVALID_DLL", (char *)NULL); |
| 150 |
Tcl_AppendToObj(errMsg, "this library or a dependent library" |
| 151 |
" is damaged", -1); |
| 152 |
break; |
| 153 |
case ERROR_DLL_INIT_FAILED: |
| 154 |
Tcl_SetErrorCode(interp, "WIN_LOAD", "DLL_INIT_FAILED", (char *)NULL); |
| 155 |
Tcl_AppendToObj(errMsg, "the library initialization" |
| 156 |
" routine failed", -1); |
| 157 |
break; |
| 158 |
case ERROR_BAD_EXE_FORMAT: |
| 159 |
Tcl_SetErrorCode(interp, "WIN_LOAD", "BAD_EXE_FORMAT", (char *)NULL); |
| 160 |
Tcl_AppendToObj(errMsg, "Bad exe format. Possibly a 32/64-bit mismatch.", -1); |
| 161 |
break; |
| 162 |
default: |
| 163 |
TclWinConvertError(lastError); |
| 164 |
Tcl_AppendToObj(errMsg, Tcl_PosixError(interp), -1); |
| 165 |
} |
| 166 |
Tcl_SetObjResult(interp, errMsg); |
| 167 |
} |
| 168 |
return TCL_ERROR; |
| 169 |
} |
| 170 |
|
| 171 |
/* |
| 172 |
* Succeded; package everything up for Tcl. |
| 173 |
*/ |
| 174 |
|
| 175 |
handlePtr = (Tcl_LoadHandle)ckalloc(sizeof(struct Tcl_LoadHandle_)); |
| 176 |
handlePtr->clientData = hInstance; |
| 177 |
handlePtr->findSymbolProcPtr = &FindSymbol; |
| 178 |
handlePtr->unloadFileProcPtr = &UnloadFile; |
| 179 |
*loadHandle = handlePtr; |
| 180 |
*unloadProcPtr = &UnloadFile; |
| 181 |
return TCL_OK; |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
*---------------------------------------------------------------------- |
| 186 |
* |
| 187 |
* FindSymbol -- |
| 188 |
* |
| 189 |
* Looks up a symbol, by name, through a handle associated with a |
| 190 |
* previously loaded piece of code (shared library). |
| 191 |
* |
| 192 |
* Results: |
| 193 |
* Returns a pointer to the function associated with 'symbol' if it is |
| 194 |
* found. Otherwise returns NULL and may leave an error message in the |
| 195 |
* interp's result. |
| 196 |
* |
| 197 |
*---------------------------------------------------------------------- |
| 198 |
*/ |
| 199 |
|
| 200 |
static void * |
| 201 |
FindSymbol( |
| 202 |
Tcl_Interp *interp, |
| 203 |
Tcl_LoadHandle loadHandle, |
| 204 |
const char *symbol) |
| 205 |
{ |
| 206 |
HINSTANCE hInstance = (HINSTANCE) loadHandle->clientData; |
| 207 |
void *proc = NULL; |
| 208 |
|
| 209 |
/* |
| 210 |
* For each symbol, check for both Symbol and _Symbol, since Borland |
| 211 |
* generates C symbols with a leading '_' by default. |
| 212 |
*/ |
| 213 |
|
| 214 |
proc = (void *)GetProcAddress(hInstance, symbol); |
| 215 |
if (proc == NULL) { |
| 216 |
Tcl_DString ds; |
| 217 |
const char *sym2; |
| 218 |
|
| 219 |
Tcl_DStringInit(&ds); |
| 220 |
TclDStringAppendLiteral(&ds, "_"); |
| 221 |
sym2 = Tcl_DStringAppend(&ds, symbol, -1); |
| 222 |
proc = (void *)GetProcAddress(hInstance, sym2); |
| 223 |
Tcl_DStringFree(&ds); |
| 224 |
} |
| 225 |
if (proc == NULL && interp != NULL) { |
| 226 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 227 |
"cannot find symbol \"%s\"", symbol)); |
| 228 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "LOAD_SYMBOL", symbol, (char *)NULL); |
| 229 |
} |
| 230 |
return proc; |
| 231 |
} |
| 232 |
|
| 233 |
/* |
| 234 |
*---------------------------------------------------------------------- |
| 235 |
* |
| 236 |
* UnloadFile -- |
| 237 |
* |
| 238 |
* Unloads a dynamically loaded binary code file from memory. Code |
| 239 |
* pointers in the formerly loaded file are no longer valid after calling |
| 240 |
* this function. |
| 241 |
* |
| 242 |
* Results: |
| 243 |
* None. |
| 244 |
* |
| 245 |
* Side effects: |
| 246 |
* Code removed from memory. |
| 247 |
* |
| 248 |
*---------------------------------------------------------------------- |
| 249 |
*/ |
| 250 |
|
| 251 |
static void |
| 252 |
UnloadFile( |
| 253 |
Tcl_LoadHandle loadHandle) /* loadHandle returned by a previous call to |
| 254 |
* TclpDlopen(). The loadHandle is a token |
| 255 |
* that represents the loaded file. */ |
| 256 |
{ |
| 257 |
HINSTANCE hInstance = (HINSTANCE) loadHandle->clientData; |
| 258 |
|
| 259 |
FreeLibrary(hInstance); |
| 260 |
ckfree(loadHandle); |
| 261 |
} |
| 262 |
|
| 263 |
/* |
| 264 |
*---------------------------------------------------------------------- |
| 265 |
* |
| 266 |
* TclGuessPackageName -- |
| 267 |
* |
| 268 |
* If the "load" command is invoked without providing a package name, |
| 269 |
* this function is invoked to try to figure it out. |
| 270 |
* |
| 271 |
* Results: |
| 272 |
* Always returns 0 to indicate that we couldn't figure out a package |
| 273 |
* name; generic code will then try to guess the package from the file |
| 274 |
* name. A return value of 1 would have meant that we figured out the |
| 275 |
* package name and put it in bufPtr. |
| 276 |
* |
| 277 |
* Side effects: |
| 278 |
* None. |
| 279 |
* |
| 280 |
*---------------------------------------------------------------------- |
| 281 |
*/ |
| 282 |
|
| 283 |
int |
| 284 |
TclGuessPackageName( |
| 285 |
const char *fileName, /* Name of file containing package (already |
| 286 |
* translated to local form if needed). */ |
| 287 |
Tcl_DString *bufPtr) /* Initialized empty dstring. Append package |
| 288 |
* name to this if possible. */ |
| 289 |
{ |
| 290 |
(void)fileName; |
| 291 |
(void)bufPtr; |
| 292 |
|
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
/* |
| 297 |
*---------------------------------------------------------------------- |
| 298 |
* |
| 299 |
* TclpTempFileNameForLibrary -- |
| 300 |
* |
| 301 |
* Constructs a temporary file name for loading a shared object (DLL). |
| 302 |
* |
| 303 |
* Results: |
| 304 |
* Returns the constructed file name. |
| 305 |
* |
| 306 |
* On Windows, a DLL is identified by the final component of its path name. |
| 307 |
* Cross linking among DLL's (and hence, preloading) will not work unless this |
| 308 |
* name is preserved when copying a DLL from a VFS to a temp file for |
| 309 |
* preloading. For this reason, all DLLs in a given process are copied to a |
| 310 |
* temp directory, and their names are preserved. |
| 311 |
* |
| 312 |
*---------------------------------------------------------------------- |
| 313 |
*/ |
| 314 |
|
| 315 |
Tcl_Obj * |
| 316 |
TclpTempFileNameForLibrary( |
| 317 |
Tcl_Interp *interp, /* Tcl interpreter. */ |
| 318 |
Tcl_Obj *path) /* Path name of the DLL in the VFS. */ |
| 319 |
{ |
| 320 |
Tcl_Obj *fileName; /* Name of the temp file. */ |
| 321 |
Tcl_Obj *tail; /* Tail of the source path. */ |
| 322 |
|
| 323 |
Tcl_MutexLock(&dllDirectoryNameMutex); |
| 324 |
if (dllDirectoryName == NULL) { |
| 325 |
if (InitDLLDirectoryName() == TCL_ERROR) { |
| 326 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 327 |
"couldn't create temporary directory: %s", |
| 328 |
Tcl_PosixError(interp))); |
| 329 |
Tcl_MutexUnlock(&dllDirectoryNameMutex); |
| 330 |
return NULL; |
| 331 |
} |
| 332 |
} |
| 333 |
Tcl_MutexUnlock(&dllDirectoryNameMutex); |
| 334 |
|
| 335 |
/* |
| 336 |
* Now we know where to put temporary DLLs, construct the name. |
| 337 |
*/ |
| 338 |
|
| 339 |
fileName = TclpNativeToNormalized(dllDirectoryName); |
| 340 |
tail = TclPathPart(interp, path, TCL_PATH_TAIL); |
| 341 |
if (tail == NULL) { |
| 342 |
Tcl_DecrRefCount(fileName); |
| 343 |
return NULL; |
| 344 |
} |
| 345 |
Tcl_AppendToObj(fileName, "/", 1); |
| 346 |
Tcl_AppendObjToObj(fileName, tail); |
| 347 |
return fileName; |
| 348 |
} |
| 349 |
|
| 350 |
/* |
| 351 |
*---------------------------------------------------------------------- |
| 352 |
* |
| 353 |
* InitDLLDirectoryName -- |
| 354 |
* |
| 355 |
* Helper for TclpTempFileNameForLibrary; builds a temporary directory |
| 356 |
* that is specific to the current process. Should only be called once |
| 357 |
* per process start. Caller must hold dllDirectoryNameMutex. |
| 358 |
* |
| 359 |
* Results: |
| 360 |
* Tcl result code. |
| 361 |
* |
| 362 |
* Side-effects: |
| 363 |
* Creates temp directory. |
| 364 |
* Allocates memory pointed to by dllDirectoryName. |
| 365 |
* |
| 366 |
*---------------------------------------------------------------------- |
| 367 |
* [Candidate for process global?] |
| 368 |
*/ |
| 369 |
|
| 370 |
static int |
| 371 |
InitDLLDirectoryName(void) |
| 372 |
{ |
| 373 |
size_t nameLen; /* Length of the temp folder name. */ |
| 374 |
WCHAR name[MAX_PATH]; /* Path name of the temp folder. */ |
| 375 |
DWORD id; /* The process id. */ |
| 376 |
DWORD lastError; /* Last error to happen in Win API. */ |
| 377 |
int i; |
| 378 |
|
| 379 |
/* |
| 380 |
* Determine the name of the directory to use, and create it. (Keep |
| 381 |
* trying with new names until an attempt to create the directory |
| 382 |
* succeeds) |
| 383 |
*/ |
| 384 |
|
| 385 |
nameLen = GetTempPathW(MAX_PATH, name); |
| 386 |
if (nameLen >= MAX_PATH-12) { |
| 387 |
Tcl_SetErrno(ENAMETOOLONG); |
| 388 |
return TCL_ERROR; |
| 389 |
} |
| 390 |
|
| 391 |
wcscpy(name+nameLen, L"TCLXXXXXXXX"); |
| 392 |
nameLen += 11; |
| 393 |
|
| 394 |
id = GetCurrentProcessId(); |
| 395 |
lastError = ERROR_ALREADY_EXISTS; |
| 396 |
|
| 397 |
for (i=0 ; i<256 ; i++) { |
| 398 |
wsprintfW(name+nameLen-8, L"%08x", id); |
| 399 |
if (CreateDirectoryW(name, NULL)) { |
| 400 |
/* |
| 401 |
* Issue: we don't schedule this directory for deletion by anyone. |
| 402 |
* Can we ask the OS to do this for us? There appears to be |
| 403 |
* potential for using CreateFile (with the flag |
| 404 |
* FILE_FLAG_BACKUP_SEMANTICS) and RemoveDirectory to do this... |
| 405 |
*/ |
| 406 |
|
| 407 |
goto copyToGlobalBuffer; |
| 408 |
} |
| 409 |
lastError = GetLastError(); |
| 410 |
if (lastError != ERROR_ALREADY_EXISTS) { |
| 411 |
break; |
| 412 |
} |
| 413 |
id *= 16777619; |
| 414 |
} |
| 415 |
|
| 416 |
TclWinConvertError(lastError); |
| 417 |
return TCL_ERROR; |
| 418 |
|
| 419 |
/* |
| 420 |
* Store our computed value in the global. |
| 421 |
*/ |
| 422 |
|
| 423 |
copyToGlobalBuffer: |
| 424 |
dllDirectoryName = (WCHAR *)ckalloc((nameLen+1) * sizeof(WCHAR)); |
| 425 |
wcscpy(dllDirectoryName, name); |
| 426 |
return TCL_OK; |
| 427 |
} |
| 428 |
|
| 429 |
/* |
| 430 |
* These functions are fallbacks if we somehow determine that the platform can |
| 431 |
* do loading from memory but the user wishes to disable it. They just report |
| 432 |
* (gracefully) that they fail. |
| 433 |
*/ |
| 434 |
|
| 435 |
#ifdef TCL_LOAD_FROM_MEMORY |
| 436 |
|
| 437 |
MODULE_SCOPE void * |
| 438 |
TclpLoadMemoryGetBuffer( |
| 439 |
size_t size) /* Dummy: unused by this implementation */ |
| 440 |
{ |
| 441 |
return NULL; |
| 442 |
} |
| 443 |
|
| 444 |
MODULE_SCOPE int |
| 445 |
TclpLoadMemory( |
| 446 |
void *buffer, /* Dummy: unused by this implementation */ |
| 447 |
size_t size, /* Dummy: unused by this implementation */ |
| 448 |
int codeSize, /* Dummy: unused by this implementation */ |
| 449 |
const char *path, /* Dummy: unused by this implementation */ |
| 450 |
Tcl_LoadHandle *loadHandle, /* Dummy: unused by this implementation */ |
| 451 |
Tcl_FSUnloadFileProc **unloadProcPtr, |
| 452 |
/* Dummy: unused by this implementation */ |
| 453 |
int flags) |
| 454 |
/* Dummy: unused by this implementation */ |
| 455 |
{ |
| 456 |
return TCL_ERROR; |
| 457 |
} |
| 458 |
|
| 459 |
#endif /* TCL_LOAD_FROM_MEMORY */ |
| 460 |
/* |
| 461 |
* Local Variables: |
| 462 |
* mode: c |
| 463 |
* c-basic-offset: 4 |
| 464 |
* fill-column: 78 |
| 465 |
* End: |
| 466 |
*/ |