| 1 |
/* |
| 2 |
* tclLoadDyld.c -- |
| 3 |
* |
| 4 |
* This procedure provides a version of the TclLoadFile that works with |
| 5 |
* Apple's dyld dynamic loading. |
| 6 |
* Original version of his file (superseded long ago) provided by |
| 7 |
* Wilfredo Sanchez ([email protected]). |
| 8 |
* |
| 9 |
* Copyright (c) 1995 Apple Computer, Inc. |
| 10 |
* Copyright (c) 2001-2007 Daniel A. Steffen <[email protected]> |
| 11 |
* |
| 12 |
* See the file "license.terms" for information on usage and redistribution of |
| 13 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "tclInt.h" |
| 17 |
|
| 18 |
#ifndef MODULE_SCOPE |
| 19 |
# define MODULE_SCOPE extern |
| 20 |
#endif |
| 21 |
|
| 22 |
/* |
| 23 |
* Use preferred dlfcn API on 10.4 and later |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifndef TCL_DYLD_USE_DLFCN |
| 27 |
# ifdef NO_DLFCN_H |
| 28 |
# define TCL_DYLD_USE_DLFCN 0 |
| 29 |
# else |
| 30 |
# define TCL_DYLD_USE_DLFCN 1 |
| 31 |
# endif |
| 32 |
#endif |
| 33 |
|
| 34 |
/* |
| 35 |
* Use deprecated NSModule API only to support 10.3 and earlier: |
| 36 |
*/ |
| 37 |
|
| 38 |
#ifndef TCL_DYLD_USE_NSMODULE |
| 39 |
# define TCL_DYLD_USE_NSMODULE 0 |
| 40 |
#endif |
| 41 |
|
| 42 |
/* |
| 43 |
* Use includes for the API we're using. |
| 44 |
*/ |
| 45 |
|
| 46 |
#if TCL_DYLD_USE_DLFCN |
| 47 |
# include <dlfcn.h> |
| 48 |
#endif /* TCL_DYLD_USE_DLFCN */ |
| 49 |
|
| 50 |
#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY) |
| 51 |
#if defined (__clang__) || ((__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) |
| 52 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 53 |
#endif |
| 54 |
#include <mach-o/dyld.h> |
| 55 |
#include <mach-o/fat.h> |
| 56 |
#include <mach-o/swap.h> |
| 57 |
#include <mach-o/arch.h> |
| 58 |
#include <libkern/OSByteOrder.h> |
| 59 |
#include <mach/mach.h> |
| 60 |
|
| 61 |
typedef struct Tcl_DyldModuleHandle { |
| 62 |
struct Tcl_DyldModuleHandle *nextPtr; |
| 63 |
NSModule module; |
| 64 |
} Tcl_DyldModuleHandle; |
| 65 |
#endif /* TCL_DYLD_USE_NSMODULE || TCL_LOAD_FROM_MEMORY */ |
| 66 |
|
| 67 |
typedef struct { |
| 68 |
void *dlHandle; |
| 69 |
#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY) |
| 70 |
const struct mach_header *dyldLibHeader; |
| 71 |
Tcl_DyldModuleHandle *modulePtr; |
| 72 |
#endif |
| 73 |
} Tcl_DyldLoadHandle; |
| 74 |
|
| 75 |
#if TCL_DYLD_USE_DLFCN || defined(TCL_LOAD_FROM_MEMORY) |
| 76 |
MODULE_SCOPE long tclMacOSXDarwinRelease; |
| 77 |
#endif |
| 78 |
|
| 79 |
/* |
| 80 |
* Static functions defined in this file. |
| 81 |
*/ |
| 82 |
|
| 83 |
static void * FindSymbol(Tcl_Interp *interp, |
| 84 |
Tcl_LoadHandle loadHandle, const char *symbol); |
| 85 |
static void UnloadFile(Tcl_LoadHandle handle); |
| 86 |
|
| 87 |
/* |
| 88 |
*---------------------------------------------------------------------- |
| 89 |
* |
| 90 |
* DyldOFIErrorMsg -- |
| 91 |
* |
| 92 |
* Converts a numerical NSObjectFileImage error into an error message |
| 93 |
* string. |
| 94 |
* |
| 95 |
* Results: |
| 96 |
* Error message string. |
| 97 |
* |
| 98 |
* Side effects: |
| 99 |
* None. |
| 100 |
* |
| 101 |
*---------------------------------------------------------------------- |
| 102 |
*/ |
| 103 |
|
| 104 |
#if TCL_DYLD_USE_NSMODULE |
| 105 |
static const char * |
| 106 |
DyldOFIErrorMsg( |
| 107 |
int err) |
| 108 |
{ |
| 109 |
switch(err) { |
| 110 |
case NSObjectFileImageSuccess: |
| 111 |
return NULL; |
| 112 |
case NSObjectFileImageFailure: |
| 113 |
return "object file setup failure"; |
| 114 |
case NSObjectFileImageInappropriateFile: |
| 115 |
return "not a Mach-O MH_BUNDLE file"; |
| 116 |
case NSObjectFileImageArch: |
| 117 |
return "no object for this architecture"; |
| 118 |
case NSObjectFileImageFormat: |
| 119 |
return "bad object file format"; |
| 120 |
case NSObjectFileImageAccess: |
| 121 |
return "can't read object file"; |
| 122 |
default: |
| 123 |
return "unknown error"; |
| 124 |
} |
| 125 |
} |
| 126 |
#endif /* TCL_DYLD_USE_NSMODULE */ |
| 127 |
|
| 128 |
/* |
| 129 |
*---------------------------------------------------------------------- |
| 130 |
* |
| 131 |
* TclpDlopen -- |
| 132 |
* |
| 133 |
* Dynamically loads a binary code file into memory and returns a handle |
| 134 |
* to the new code. |
| 135 |
* |
| 136 |
* Results: |
| 137 |
* A standard Tcl completion code. If an error occurs, an error message |
| 138 |
* is left in the interpreter's result. |
| 139 |
* |
| 140 |
* Side effects: |
| 141 |
* New code suddenly appears in memory. |
| 142 |
* |
| 143 |
*---------------------------------------------------------------------- |
| 144 |
*/ |
| 145 |
|
| 146 |
MODULE_SCOPE int |
| 147 |
TclpDlopen( |
| 148 |
Tcl_Interp *interp, /* Used for error reporting. */ |
| 149 |
Tcl_Obj *pathPtr, /* Name of the file containing the desired |
| 150 |
* code (UTF-8). */ |
| 151 |
Tcl_LoadHandle *loadHandle, /* Filled with token for dynamically loaded |
| 152 |
* file which will be passed back to |
| 153 |
* (*unloadProcPtr)() to unload the file. */ |
| 154 |
Tcl_FSUnloadFileProc **unloadProcPtr, |
| 155 |
/* Filled with address of Tcl_FSUnloadFileProc |
| 156 |
* function which should be used for this |
| 157 |
* file. */ |
| 158 |
int flags) |
| 159 |
{ |
| 160 |
Tcl_DyldLoadHandle *dyldLoadHandle; |
| 161 |
Tcl_LoadHandle newHandle; |
| 162 |
void *dlHandle = NULL; |
| 163 |
#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY) |
| 164 |
const struct mach_header *dyldLibHeader = NULL; |
| 165 |
Tcl_DyldModuleHandle *modulePtr = NULL; |
| 166 |
#endif |
| 167 |
#if TCL_DYLD_USE_NSMODULE |
| 168 |
NSLinkEditErrors editError; |
| 169 |
int errorNumber; |
| 170 |
const char *errorName, *objFileImageErrMsg = NULL; |
| 171 |
#endif /* TCL_DYLD_USE_NSMODULE */ |
| 172 |
const char *errMsg = NULL; |
| 173 |
int result; |
| 174 |
Tcl_DString ds; |
| 175 |
const char *nativePath, *nativeFileName = NULL; |
| 176 |
#if TCL_DYLD_USE_DLFCN |
| 177 |
int dlopenflags = 0; |
| 178 |
#endif /* TCL_DYLD_USE_DLFCN */ |
| 179 |
|
| 180 |
/* |
| 181 |
* First try the full path the user gave us. This is particularly |
| 182 |
* important if the cwd is inside a vfs, and we are trying to load using a |
| 183 |
* relative path. |
| 184 |
*/ |
| 185 |
|
| 186 |
nativePath = (const char *)Tcl_FSGetNativePath(pathPtr); |
| 187 |
nativeFileName = Tcl_UtfToExternalDString(NULL, Tcl_GetString(pathPtr), |
| 188 |
-1, &ds); |
| 189 |
|
| 190 |
#if TCL_DYLD_USE_DLFCN |
| 191 |
/* |
| 192 |
* Use (RTLD_NOW|RTLD_LOCAL) as default, see [Bug #3216070] |
| 193 |
*/ |
| 194 |
|
| 195 |
if (flags & TCL_LOAD_GLOBAL) { |
| 196 |
dlopenflags |= RTLD_GLOBAL; |
| 197 |
} else { |
| 198 |
dlopenflags |= RTLD_LOCAL; |
| 199 |
} |
| 200 |
if (flags & TCL_LOAD_LAZY) { |
| 201 |
dlopenflags |= RTLD_LAZY; |
| 202 |
} else { |
| 203 |
dlopenflags |= RTLD_NOW; |
| 204 |
} |
| 205 |
dlHandle = dlopen(nativePath, dlopenflags); |
| 206 |
if (!dlHandle) { |
| 207 |
/* |
| 208 |
* Let the OS loader examine the binary search path for whatever string |
| 209 |
* the user gave us which hopefully refers to a file on the binary |
| 210 |
* path. |
| 211 |
*/ |
| 212 |
|
| 213 |
dlHandle = dlopen(nativeFileName, dlopenflags); |
| 214 |
if (!dlHandle) { |
| 215 |
errMsg = dlerror(); |
| 216 |
} |
| 217 |
} |
| 218 |
#endif /* TCL_DYLD_USE_DLFCN */ |
| 219 |
|
| 220 |
if (!dlHandle) { |
| 221 |
#if TCL_DYLD_USE_NSMODULE |
| 222 |
dyldLibHeader = NSAddImage(nativePath, |
| 223 |
NSADDIMAGE_OPTION_RETURN_ON_ERROR); |
| 224 |
if (!dyldLibHeader) { |
| 225 |
NSLinkEditError(&editError, &errorNumber, &errorName, &errMsg); |
| 226 |
if (editError == NSLinkEditFileAccessError) { |
| 227 |
/* |
| 228 |
* The requested file was not found. Let the OS loader examine |
| 229 |
* the binary search path for whatever string the user gave us |
| 230 |
* which hopefully refers to a file on the binary path. |
| 231 |
*/ |
| 232 |
|
| 233 |
dyldLibHeader = NSAddImage(nativeFileName, |
| 234 |
NSADDIMAGE_OPTION_WITH_SEARCHING | |
| 235 |
NSADDIMAGE_OPTION_RETURN_ON_ERROR); |
| 236 |
if (!dyldLibHeader) { |
| 237 |
NSLinkEditError(&editError, &errorNumber, &errorName, |
| 238 |
&errMsg); |
| 239 |
} |
| 240 |
} else if ((editError == NSLinkEditFileFormatError |
| 241 |
&& errorNumber == EBADMACHO) |
| 242 |
|| editError == NSLinkEditOtherError){ |
| 243 |
NSObjectFileImageReturnCode err; |
| 244 |
NSObjectFileImage dyldObjFileImage; |
| 245 |
NSModule module; |
| 246 |
|
| 247 |
/* |
| 248 |
* The requested file was found but was not of type MH_DYLIB, |
| 249 |
* attempt to load it as a MH_BUNDLE. |
| 250 |
*/ |
| 251 |
|
| 252 |
err = NSCreateObjectFileImageFromFile(nativePath, |
| 253 |
&dyldObjFileImage); |
| 254 |
if (err == NSObjectFileImageSuccess && dyldObjFileImage) { |
| 255 |
int nsflags = NSLINKMODULE_OPTION_RETURN_ON_ERROR; |
| 256 |
if (!(flags & 1)) { |
| 257 |
nsflags |= NSLINKMODULE_OPTION_PRIVATE; |
| 258 |
} |
| 259 |
if (!(flags & 2)) { |
| 260 |
nsflags |= NSLINKMODULE_OPTION_BINDNOW; |
| 261 |
} |
| 262 |
module = NSLinkModule(dyldObjFileImage, nativePath, nsflags); |
| 263 |
NSDestroyObjectFileImage(dyldObjFileImage); |
| 264 |
if (module) { |
| 265 |
modulePtr = (Tcl_DyldModuleHandle *)ckalloc(sizeof(Tcl_DyldModuleHandle)); |
| 266 |
modulePtr->module = module; |
| 267 |
modulePtr->nextPtr = NULL; |
| 268 |
} else { |
| 269 |
NSLinkEditError(&editError, &errorNumber, &errorName, |
| 270 |
&errMsg); |
| 271 |
} |
| 272 |
} else { |
| 273 |
objFileImageErrMsg = DyldOFIErrorMsg(err); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
#endif /* TCL_DYLD_USE_NSMODULE */ |
| 278 |
} |
| 279 |
|
| 280 |
if (dlHandle |
| 281 |
#if TCL_DYLD_USE_NSMODULE |
| 282 |
|| dyldLibHeader || modulePtr |
| 283 |
#endif /* TCL_DYLD_USE_NSMODULE */ |
| 284 |
) { |
| 285 |
dyldLoadHandle = (Tcl_DyldLoadHandle *)ckalloc(sizeof(Tcl_DyldLoadHandle)); |
| 286 |
dyldLoadHandle->dlHandle = dlHandle; |
| 287 |
#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY) |
| 288 |
dyldLoadHandle->dyldLibHeader = dyldLibHeader; |
| 289 |
dyldLoadHandle->modulePtr = modulePtr; |
| 290 |
#endif /* TCL_DYLD_USE_NSMODULE || TCL_LOAD_FROM_MEMORY */ |
| 291 |
newHandle = (Tcl_LoadHandle)ckalloc(sizeof(*newHandle)); |
| 292 |
newHandle->clientData = dyldLoadHandle; |
| 293 |
newHandle->findSymbolProcPtr = &FindSymbol; |
| 294 |
newHandle->unloadFileProcPtr = &UnloadFile; |
| 295 |
*unloadProcPtr = &UnloadFile; |
| 296 |
*loadHandle = newHandle; |
| 297 |
result = TCL_OK; |
| 298 |
} else { |
| 299 |
Tcl_Obj *errObj; |
| 300 |
|
| 301 |
TclNewObj(errObj); |
| 302 |
if (errMsg != NULL) { |
| 303 |
Tcl_AppendToObj(errObj, errMsg, -1); |
| 304 |
} |
| 305 |
#if TCL_DYLD_USE_NSMODULE |
| 306 |
if (objFileImageErrMsg) { |
| 307 |
Tcl_AppendPrintfToObj(errObj, |
| 308 |
"\nNSCreateObjectFileImageFromFile() error: %s", |
| 309 |
objFileImageErrMsg); |
| 310 |
} |
| 311 |
#endif /* TCL_DYLD_USE_NSMODULE */ |
| 312 |
Tcl_SetObjResult(interp, errObj); |
| 313 |
result = TCL_ERROR; |
| 314 |
} |
| 315 |
|
| 316 |
Tcl_DStringFree(&ds); |
| 317 |
return result; |
| 318 |
} |
| 319 |
|
| 320 |
/* |
| 321 |
*---------------------------------------------------------------------- |
| 322 |
* |
| 323 |
* FindSymbol -- |
| 324 |
* |
| 325 |
* Looks up a symbol, by name, through a handle associated with a |
| 326 |
* previously loaded piece of code (shared library). |
| 327 |
* |
| 328 |
* Results: |
| 329 |
* Returns a pointer to the function associated with 'symbol' if it is |
| 330 |
* found. Otherwise returns NULL and may leave an error message in the |
| 331 |
* interp's result. |
| 332 |
* |
| 333 |
*---------------------------------------------------------------------- |
| 334 |
*/ |
| 335 |
|
| 336 |
static void * |
| 337 |
FindSymbol( |
| 338 |
Tcl_Interp *interp, /* For error reporting. */ |
| 339 |
Tcl_LoadHandle loadHandle, /* Handle from TclpDlopen. */ |
| 340 |
const char *symbol) /* Symbol name to look up. */ |
| 341 |
{ |
| 342 |
Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *)loadHandle->clientData; |
| 343 |
Tcl_PackageInitProc *proc = NULL; |
| 344 |
const char *errMsg = NULL; |
| 345 |
Tcl_DString ds; |
| 346 |
const char *native; |
| 347 |
|
| 348 |
native = Tcl_UtfToExternalDString(NULL, symbol, -1, &ds); |
| 349 |
if (dyldLoadHandle->dlHandle) { |
| 350 |
#if TCL_DYLD_USE_DLFCN |
| 351 |
proc = (Tcl_PackageInitProc *)dlsym(dyldLoadHandle->dlHandle, native); |
| 352 |
if (!proc) { |
| 353 |
errMsg = dlerror(); |
| 354 |
} |
| 355 |
#endif /* TCL_DYLD_USE_DLFCN */ |
| 356 |
} else { |
| 357 |
#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY) |
| 358 |
NSSymbol nsSymbol = NULL; |
| 359 |
Tcl_DString newName; |
| 360 |
|
| 361 |
/* |
| 362 |
* dyld adds an underscore to the beginning of symbol names. |
| 363 |
*/ |
| 364 |
|
| 365 |
Tcl_DStringInit(&newName); |
| 366 |
TclDStringAppendLiteral(&newName, "_"); |
| 367 |
native = Tcl_DStringAppend(&newName, native, -1); |
| 368 |
if (dyldLoadHandle->dyldLibHeader) { |
| 369 |
nsSymbol = NSLookupSymbolInImage(dyldLoadHandle->dyldLibHeader, |
| 370 |
native, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW | |
| 371 |
NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); |
| 372 |
if (nsSymbol) { |
| 373 |
/* |
| 374 |
* Until dyld supports unloading of MY_DYLIB binaries, the |
| 375 |
* following is not needed. |
| 376 |
*/ |
| 377 |
|
| 378 |
#ifdef DYLD_SUPPORTS_DYLIB_UNLOADING |
| 379 |
NSModule module = NSModuleForSymbol(nsSymbol); |
| 380 |
Tcl_DyldModuleHandle *modulePtr = dyldLoadHandle->modulePtr; |
| 381 |
|
| 382 |
while (modulePtr != NULL) { |
| 383 |
if (module == modulePtr->module) { |
| 384 |
break; |
| 385 |
} |
| 386 |
modulePtr = modulePtr->nextPtr; |
| 387 |
} |
| 388 |
if (modulePtr == NULL) { |
| 389 |
modulePtr = (Tcl_DyldModuleHandle *)ckalloc(sizeof(Tcl_DyldModuleHandle)); |
| 390 |
modulePtr->module = module; |
| 391 |
modulePtr->nextPtr = dyldLoadHandle->modulePtr; |
| 392 |
dyldLoadHandle->modulePtr = modulePtr; |
| 393 |
} |
| 394 |
#endif /* DYLD_SUPPORTS_DYLIB_UNLOADING */ |
| 395 |
} else { |
| 396 |
NSLinkEditErrors editError; |
| 397 |
int errorNumber; |
| 398 |
const char *errorName; |
| 399 |
|
| 400 |
NSLinkEditError(&editError, &errorNumber, &errorName, &errMsg); |
| 401 |
} |
| 402 |
} else if (dyldLoadHandle->modulePtr) { |
| 403 |
nsSymbol = NSLookupSymbolInModule( |
| 404 |
dyldLoadHandle->modulePtr->module, native); |
| 405 |
} |
| 406 |
if (nsSymbol) { |
| 407 |
proc = (Tcl_PackageInitProc *)NSAddressOfSymbol(nsSymbol); |
| 408 |
} |
| 409 |
Tcl_DStringFree(&newName); |
| 410 |
#endif /* TCL_DYLD_USE_NSMODULE */ |
| 411 |
} |
| 412 |
Tcl_DStringFree(&ds); |
| 413 |
if (errMsg && (interp != NULL)) { |
| 414 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 415 |
"cannot find symbol \"%s\": %s", symbol, errMsg)); |
| 416 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "LOAD_SYMBOL", symbol, |
| 417 |
(char *)NULL); |
| 418 |
} |
| 419 |
return (void *)proc; |
| 420 |
} |
| 421 |
|
| 422 |
/* |
| 423 |
*---------------------------------------------------------------------- |
| 424 |
* |
| 425 |
* UnloadFile -- |
| 426 |
* |
| 427 |
* Unloads a dynamically loaded binary code file from memory. Code |
| 428 |
* pointers in the formerly loaded file are no longer valid after calling |
| 429 |
* this function. |
| 430 |
* |
| 431 |
* Results: |
| 432 |
* None. |
| 433 |
* |
| 434 |
* Side effects: |
| 435 |
* Code dissapears from memory. Note that dyld currently only supports |
| 436 |
* unloading of binaries of type MH_BUNDLE loaded with NSLinkModule() in |
| 437 |
* TclpDlopen() above. |
| 438 |
* |
| 439 |
*---------------------------------------------------------------------- |
| 440 |
*/ |
| 441 |
|
| 442 |
static void |
| 443 |
UnloadFile( |
| 444 |
Tcl_LoadHandle loadHandle) /* loadHandle returned by a previous call to |
| 445 |
* TclpDlopen(). The loadHandle is a token |
| 446 |
* that represents the loaded file. */ |
| 447 |
{ |
| 448 |
Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *)loadHandle->clientData; |
| 449 |
|
| 450 |
if (dyldLoadHandle->dlHandle) { |
| 451 |
#if TCL_DYLD_USE_DLFCN |
| 452 |
(void) dlclose(dyldLoadHandle->dlHandle); |
| 453 |
#endif /* TCL_DYLD_USE_DLFCN */ |
| 454 |
} else { |
| 455 |
#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY) |
| 456 |
Tcl_DyldModuleHandle *modulePtr = dyldLoadHandle->modulePtr; |
| 457 |
|
| 458 |
while (modulePtr != NULL) { |
| 459 |
void *ptr = modulePtr; |
| 460 |
|
| 461 |
(void) NSUnLinkModule(modulePtr->module, |
| 462 |
NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); |
| 463 |
modulePtr = modulePtr->nextPtr; |
| 464 |
ckfree(ptr); |
| 465 |
} |
| 466 |
#endif /* TCL_DYLD_USE_NSMODULE */ |
| 467 |
} |
| 468 |
ckfree(dyldLoadHandle); |
| 469 |
ckfree(loadHandle); |
| 470 |
} |
| 471 |
|
| 472 |
/* |
| 473 |
*---------------------------------------------------------------------- |
| 474 |
* |
| 475 |
* TclGuessPackageName -- |
| 476 |
* |
| 477 |
* If the "load" command is invoked without providing a package name, |
| 478 |
* this procedure is invoked to try to figure it out. |
| 479 |
* |
| 480 |
* Results: |
| 481 |
* Always returns 0 to indicate that we couldn't figure out a package |
| 482 |
* name; generic code will then try to guess the package from the file |
| 483 |
* name. A return value of 1 would have meant that we figured out the |
| 484 |
* package name and put it in bufPtr. |
| 485 |
* |
| 486 |
* Side effects: |
| 487 |
* None. |
| 488 |
* |
| 489 |
*---------------------------------------------------------------------- |
| 490 |
*/ |
| 491 |
|
| 492 |
int |
| 493 |
TclGuessPackageName( |
| 494 |
const char *fileName, /* Name of file containing package (already |
| 495 |
* translated to local form if needed). */ |
| 496 |
Tcl_DString *bufPtr) /* Initialized empty dstring. Append package |
| 497 |
* name to this if possible. */ |
| 498 |
{ |
| 499 |
return 0; |
| 500 |
} |
| 501 |
|
| 502 |
/* |
| 503 |
*---------------------------------------------------------------------- |
| 504 |
* |
| 505 |
* TclpLoadMemoryGetBuffer -- |
| 506 |
* |
| 507 |
* Allocate a buffer that can be used with TclpLoadMemory() below. |
| 508 |
* |
| 509 |
* Results: |
| 510 |
* Pointer to allocated buffer or NULL if an error occurs. |
| 511 |
* |
| 512 |
* Side effects: |
| 513 |
* Buffer is allocated. |
| 514 |
* |
| 515 |
*---------------------------------------------------------------------- |
| 516 |
*/ |
| 517 |
|
| 518 |
#ifdef TCL_LOAD_FROM_MEMORY |
| 519 |
MODULE_SCOPE void * |
| 520 |
TclpLoadMemoryGetBuffer( |
| 521 |
size_t size) /* Size of desired buffer. */ |
| 522 |
{ |
| 523 |
void *buffer = NULL; |
| 524 |
|
| 525 |
/* |
| 526 |
* NSCreateObjectFileImageFromMemory is available but always fails |
| 527 |
* prior to Darwin 7. |
| 528 |
*/ |
| 529 |
if (tclMacOSXDarwinRelease >= 7) { |
| 530 |
/* |
| 531 |
* We must allocate the buffer using vm_allocate, because |
| 532 |
* NSCreateObjectFileImageFromMemory will dispose of it using |
| 533 |
* vm_deallocate. |
| 534 |
*/ |
| 535 |
|
| 536 |
if (vm_allocate(mach_task_self(), (vm_address_t *) &buffer, size, 1)) { |
| 537 |
buffer = NULL; |
| 538 |
} |
| 539 |
} |
| 540 |
return buffer; |
| 541 |
} |
| 542 |
#endif /* TCL_LOAD_FROM_MEMORY */ |
| 543 |
|
| 544 |
/* |
| 545 |
*---------------------------------------------------------------------- |
| 546 |
* |
| 547 |
* TclpLoadMemory -- |
| 548 |
* |
| 549 |
* Dynamically loads binary code file from memory and returns a handle to |
| 550 |
* the new code. |
| 551 |
* |
| 552 |
* Results: |
| 553 |
* A standard Tcl completion code. If an error occurs, an error message |
| 554 |
* is left in the interpreter's result. |
| 555 |
* |
| 556 |
* Side effects: |
| 557 |
* New code is loaded from memory. |
| 558 |
* |
| 559 |
*---------------------------------------------------------------------- |
| 560 |
*/ |
| 561 |
|
| 562 |
#ifdef TCL_LOAD_FROM_MEMORY |
| 563 |
MODULE_SCOPE int |
| 564 |
TclpLoadMemory( |
| 565 |
void *buffer, /* Buffer containing the desired code |
| 566 |
* (allocated with TclpLoadMemoryGetBuffer). */ |
| 567 |
size_t size, /* Allocation size of buffer. */ |
| 568 |
int codeSize, /* Size of code data read into buffer or -1 if |
| 569 |
* an error occurred and the buffer should |
| 570 |
* just be freed. */ |
| 571 |
const char *path, |
| 572 |
Tcl_LoadHandle *loadHandle, /* Filled with token for dynamically loaded |
| 573 |
* file which will be passed back to |
| 574 |
* (*unloadProcPtr)() to unload the file. */ |
| 575 |
Tcl_FSUnloadFileProc **unloadProcPtr, |
| 576 |
/* Filled with address of Tcl_FSUnloadFileProc |
| 577 |
* function which should be used for this |
| 578 |
* file. */ |
| 579 |
int flags) |
| 580 |
{ |
| 581 |
Tcl_LoadHandle newHandle; |
| 582 |
Tcl_DyldLoadHandle *dyldLoadHandle; |
| 583 |
NSObjectFileImage dyldObjFileImage = NULL; |
| 584 |
Tcl_DyldModuleHandle *modulePtr; |
| 585 |
NSModule module; |
| 586 |
int nsflags = NSLINKMODULE_OPTION_RETURN_ON_ERROR; |
| 587 |
|
| 588 |
/* |
| 589 |
* Try to create an object file image that we can load from. |
| 590 |
*/ |
| 591 |
|
| 592 |
if (codeSize >= 0) { |
| 593 |
NSObjectFileImageReturnCode err = NSObjectFileImageSuccess; |
| 594 |
const struct fat_header *fh = (const struct fat_header *)buffer; |
| 595 |
uint32_t ms = 0; |
| 596 |
#ifndef __LP64__ |
| 597 |
const struct mach_header *mh = NULL; |
| 598 |
# define mh_size sizeof(struct mach_header) |
| 599 |
# define mh_magic MH_MAGIC |
| 600 |
# define arch_abi 0 |
| 601 |
#else |
| 602 |
const struct mach_header_64 *mh = NULL; |
| 603 |
# define mh_size sizeof(struct mach_header_64) |
| 604 |
# define mh_magic MH_MAGIC_64 |
| 605 |
# define arch_abi CPU_ARCH_ABI64 |
| 606 |
#endif /* __LP64__ */ |
| 607 |
|
| 608 |
if ((size_t)codeSize >= sizeof(struct fat_header) |
| 609 |
&& fh->magic == OSSwapHostToBigInt32(FAT_MAGIC)) { |
| 610 |
uint32_t fh_nfat_arch = OSSwapBigToHostInt32(fh->nfat_arch); |
| 611 |
|
| 612 |
/* |
| 613 |
* Fat binary, try to find mach_header for our architecture |
| 614 |
*/ |
| 615 |
|
| 616 |
if ((size_t)codeSize >= sizeof(struct fat_header) + |
| 617 |
fh_nfat_arch * sizeof(struct fat_arch)) { |
| 618 |
void *fatarchs = (char *)buffer + sizeof(struct fat_header); |
| 619 |
const NXArchInfo *arch = NXGetLocalArchInfo(); |
| 620 |
struct fat_arch *fa; |
| 621 |
|
| 622 |
if (fh->magic != FAT_MAGIC) { |
| 623 |
swap_fat_arch((struct fat_arch *)fatarchs, fh_nfat_arch, arch->byteorder); |
| 624 |
} |
| 625 |
fa = NXFindBestFatArch(arch->cputype | arch_abi, |
| 626 |
arch->cpusubtype, (struct fat_arch *)fatarchs, fh_nfat_arch); |
| 627 |
if (fa) { |
| 628 |
mh = (const struct mach_header_64 *)((char *) buffer + fa->offset); |
| 629 |
ms = fa->size; |
| 630 |
} else { |
| 631 |
err = NSObjectFileImageInappropriateFile; |
| 632 |
} |
| 633 |
if (fh->magic != FAT_MAGIC) { |
| 634 |
swap_fat_arch((struct fat_arch *)fatarchs, fh_nfat_arch, arch->byteorder); |
| 635 |
} |
| 636 |
} else { |
| 637 |
err = NSObjectFileImageInappropriateFile; |
| 638 |
} |
| 639 |
} else { |
| 640 |
/* |
| 641 |
* Thin binary |
| 642 |
*/ |
| 643 |
|
| 644 |
mh = (const struct mach_header_64 *)buffer; |
| 645 |
ms = codeSize; |
| 646 |
} |
| 647 |
if (ms && !(ms >= mh_size && mh->magic == mh_magic && |
| 648 |
mh->filetype == MH_BUNDLE)) { |
| 649 |
err = NSObjectFileImageInappropriateFile; |
| 650 |
} |
| 651 |
if (err == NSObjectFileImageSuccess) { |
| 652 |
err = NSCreateObjectFileImageFromMemory(buffer, codeSize, |
| 653 |
&dyldObjFileImage); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
/* |
| 658 |
* If it went wrong (or we were asked to just deallocate), get rid of the |
| 659 |
* memory block. |
| 660 |
*/ |
| 661 |
|
| 662 |
if (dyldObjFileImage == NULL) { |
| 663 |
vm_deallocate(mach_task_self(), (vm_address_t) buffer, size); |
| 664 |
return TCL_ERROR; |
| 665 |
} |
| 666 |
|
| 667 |
/* |
| 668 |
* Extract the module we want from the image of the object file. |
| 669 |
*/ |
| 670 |
|
| 671 |
if (!(flags & 1)) { |
| 672 |
nsflags |= NSLINKMODULE_OPTION_PRIVATE; |
| 673 |
} |
| 674 |
if (!(flags & 2)) { |
| 675 |
nsflags |= NSLINKMODULE_OPTION_BINDNOW; |
| 676 |
} |
| 677 |
module = NSLinkModule(dyldObjFileImage, "[Memory Based Bundle]", nsflags); |
| 678 |
NSDestroyObjectFileImage(dyldObjFileImage); |
| 679 |
if (!module) { |
| 680 |
NSLinkEditErrors editError; |
| 681 |
int errorNumber; |
| 682 |
const char *errorName, *errMsg; |
| 683 |
|
| 684 |
NSLinkEditError(&editError, &errorNumber, &errorName, &errMsg); |
| 685 |
return TCL_ERROR; |
| 686 |
} |
| 687 |
|
| 688 |
/* |
| 689 |
* Stash the module reference within the load handle we create and return. |
| 690 |
*/ |
| 691 |
|
| 692 |
modulePtr = (Tcl_DyldModuleHandle *)ckalloc(sizeof(Tcl_DyldModuleHandle)); |
| 693 |
modulePtr->module = module; |
| 694 |
modulePtr->nextPtr = NULL; |
| 695 |
dyldLoadHandle = (Tcl_DyldLoadHandle *)ckalloc(sizeof(Tcl_DyldLoadHandle)); |
| 696 |
dyldLoadHandle->dlHandle = NULL; |
| 697 |
dyldLoadHandle->dyldLibHeader = NULL; |
| 698 |
dyldLoadHandle->modulePtr = modulePtr; |
| 699 |
newHandle = (Tcl_LoadHandle)ckalloc(sizeof(*newHandle)); |
| 700 |
newHandle->clientData = dyldLoadHandle; |
| 701 |
newHandle->findSymbolProcPtr = &FindSymbol; |
| 702 |
newHandle->unloadFileProcPtr = &UnloadFile; |
| 703 |
*loadHandle = newHandle; |
| 704 |
*unloadProcPtr = &UnloadFile; |
| 705 |
return TCL_OK; |
| 706 |
} |
| 707 |
#endif /* TCL_LOAD_FROM_MEMORY */ |
| 708 |
|
| 709 |
/* |
| 710 |
* Local Variables: |
| 711 |
* mode: c |
| 712 |
* c-basic-offset: 4 |
| 713 |
* fill-column: 79 |
| 714 |
* End: |
| 715 |
*/ |