| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "tclInt.h" |
| 21 |
#include <fcntl.h> |
| 22 |
#ifdef HAVE_EXEC_AOUT_H |
| 23 |
# include <sys/exec_aout.h> |
| 24 |
#endif |
| 25 |
#ifdef HAVE_UNISTD_H |
| 26 |
# include <unistd.h> |
| 27 |
#else |
| 28 |
# include "../compat/unistd.h" |
| 29 |
#endif |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#ifdef USE_SYS_EXEC_H |
| 37 |
#include <sys/exec.h> |
| 38 |
#endif |
| 39 |
#ifdef USE_A_OUT_H |
| 40 |
#include <a.out.h> |
| 41 |
#endif |
| 42 |
#ifdef USE_SYS_EXEC_AOUT_H |
| 43 |
#include <sys/exec_aout.h> |
| 44 |
#define a_magic a_midmag |
| 45 |
#endif |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#ifndef TCL_LOADSHIM |
| 52 |
#define TCL_LOADSHIM 0x4000L |
| 53 |
#endif |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#ifndef TCL_LOADALIGN |
| 61 |
#define TCL_LOADALIGN 0x4000L |
| 62 |
#endif |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#ifndef TCL_LOADMAX |
| 70 |
#define TCL_LOADMAX 2000000L |
| 71 |
#endif |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
extern char * brk _ANSI_ARGS_((char *)); |
| 78 |
extern char * sbrk _ANSI_ARGS_((size_t)); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static char * SymbolTableFile = NULL; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
typedef Tcl_PackageInitProc * (* DictFn) _ANSI_ARGS_ ((CONST char * symbol)); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
static int FindLibraries _ANSI_ARGS_((Tcl_Interp * interp, Tcl_Obj * pathPtr, |
| 99 |
Tcl_DString * buf)); |
| 100 |
static void UnlinkSymbolTable _ANSI_ARGS_((void)); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
int |
| 141 |
TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr) |
| 142 |
Tcl_Interp *interp; |
| 143 |
Tcl_Obj *pathPtr; |
| 144 |
|
| 145 |
Tcl_LoadHandle *loadHandle; |
| 146 |
|
| 147 |
|
| 148 |
Tcl_FSUnloadFileProc **unloadProcPtr; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
{ |
| 153 |
char * inputSymbolTable; |
| 154 |
|
| 155 |
Tcl_DString linkCommandBuf; |
| 156 |
|
| 157 |
char * linkCommand; |
| 158 |
char relocatedFileName [L_tmpnam]; |
| 159 |
|
| 160 |
|
| 161 |
int relocatedFd; |
| 162 |
|
| 163 |
struct exec relocatedHead; |
| 164 |
unsigned long relocatedSize; |
| 165 |
char * startAddress; |
| 166 |
int status; |
| 167 |
char * p; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
if (SymbolTableFile != NULL) { |
| 172 |
inputSymbolTable = SymbolTableFile; |
| 173 |
} else if (tclExecutableName == NULL) { |
| 174 |
Tcl_SetResult (interp, "can't find the tclsh executable", TCL_STATIC); |
| 175 |
return TCL_ERROR; |
| 176 |
} else { |
| 177 |
inputSymbolTable = tclExecutableName; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
tmpnam (relocatedFileName); |
| 183 |
Tcl_DStringInit (&linkCommandBuf); |
| 184 |
Tcl_DStringAppend (&linkCommandBuf, "exec ld -o ", -1); |
| 185 |
Tcl_DStringAppend (&linkCommandBuf, relocatedFileName, -1); |
| 186 |
#if defined(__mips) || defined(mips) |
| 187 |
Tcl_DStringAppend (&linkCommandBuf, " -G 0 ", -1); |
| 188 |
#endif |
| 189 |
Tcl_DStringAppend (&linkCommandBuf, " -u TclLoadDictionary_", -1); |
| 190 |
TclGuessPackageName(Tcl_GetString(pathPtr), &linkCommandBuf); |
| 191 |
Tcl_DStringAppend (&linkCommandBuf, " -A ", -1); |
| 192 |
Tcl_DStringAppend (&linkCommandBuf, inputSymbolTable, -1); |
| 193 |
Tcl_DStringAppend (&linkCommandBuf, " -N -T XXXXXXXX ", -1); |
| 194 |
Tcl_DStringAppend (&linkCommandBuf, Tcl_GetString(pathPtr), -1); |
| 195 |
Tcl_DStringAppend (&linkCommandBuf, " ", -1); |
| 196 |
|
| 197 |
if (FindLibraries (interp, pathPtr, &linkCommandBuf) != TCL_OK) { |
| 198 |
Tcl_DStringFree (&linkCommandBuf); |
| 199 |
return TCL_ERROR; |
| 200 |
} |
| 201 |
|
| 202 |
linkCommand = Tcl_DStringValue (&linkCommandBuf); |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
startAddress = (char *) (((unsigned long) sbrk (0) |
| 207 |
+ TCL_LOADSHIM + TCL_LOADALIGN - 1) |
| 208 |
& (- TCL_LOADALIGN)); |
| 209 |
p = strstr (linkCommand, "-T") + 3; |
| 210 |
sprintf (p, "%08lx", (long) startAddress); |
| 211 |
p [8] = ' '; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
status = Tcl_Eval (interp, linkCommand); |
| 216 |
Tcl_DStringFree (&linkCommandBuf); |
| 217 |
if (status != 0) { |
| 218 |
return TCL_ERROR; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
relocatedFd = open (relocatedFileName, O_RDONLY); |
| 224 |
if (relocatedFd < 0) { |
| 225 |
goto ioError; |
| 226 |
} |
| 227 |
status= read (relocatedFd, (char *) & relocatedHead, sizeof relocatedHead); |
| 228 |
if (status < sizeof relocatedHead) { |
| 229 |
goto ioError; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
if (relocatedHead.a_magic != OMAGIC) { |
| 235 |
Tcl_AppendResult (interp, "bad magic number in intermediate file \"", |
| 236 |
relocatedFileName, "\"", (char *) NULL); |
| 237 |
goto failure; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
if ((unsigned long) sbrk (0) > (unsigned long) startAddress) { |
| 243 |
Tcl_SetResult (interp, "can't load, memory allocation is inconsistent.", |
| 244 |
TCL_STATIC); |
| 245 |
goto failure; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
relocatedSize = relocatedHead.a_text + relocatedHead.a_data |
| 251 |
+ relocatedHead.a_bss; |
| 252 |
if (relocatedSize > TCL_LOADMAX) { |
| 253 |
Tcl_SetResult (interp, "module too big to load", TCL_STATIC); |
| 254 |
goto failure; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
(void) brk (startAddress + relocatedSize); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
#if defined(__mips) || defined(mips) |
| 271 |
status = lseek (relocatedFd, |
| 272 |
(off_t) N_TXTOFF (relocatedHead.ex_f, relocatedHead.ex_o), |
| 273 |
SEEK_SET); |
| 274 |
#else |
| 275 |
status = lseek (relocatedFd, (off_t) N_TXTOFF (relocatedHead), SEEK_SET); |
| 276 |
#endif |
| 277 |
if (status < 0) { |
| 278 |
goto ioError; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
relocatedSize = relocatedHead.a_text + relocatedHead.a_data; |
| 284 |
if (read (relocatedFd, startAddress, relocatedSize) < relocatedSize) { |
| 285 |
brk (startAddress); |
| 286 |
ioError: |
| 287 |
Tcl_AppendResult (interp, "error on intermediate file \"", |
| 288 |
relocatedFileName, "\": ", Tcl_PosixError (interp), |
| 289 |
(char *) NULL); |
| 290 |
failure: |
| 291 |
(void) unlink (relocatedFileName); |
| 292 |
return TCL_ERROR; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
(void) close (relocatedFd); |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
if (SymbolTableFile != NULL) { |
| 303 |
UnlinkSymbolTable (); |
| 304 |
} else { |
| 305 |
atexit (UnlinkSymbolTable); |
| 306 |
} |
| 307 |
SymbolTableFile = ckalloc (strlen (relocatedFileName) + 1); |
| 308 |
strcpy (SymbolTableFile, relocatedFileName); |
| 309 |
|
| 310 |
*loadHandle = startAddress; |
| 311 |
return TCL_OK; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
Tcl_PackageInitProc* |
| 330 |
TclpFindSymbol(interp, loadHandle, symbol) |
| 331 |
Tcl_Interp *interp; |
| 332 |
Tcl_LoadHandle loadHandle; |
| 333 |
CONST char *symbol; |
| 334 |
{ |
| 335 |
|
| 336 |
DictFn dictionary = (DictFn) loadHandle; |
| 337 |
return (Tcl_PackageInitProc*) dictionary(sym1); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
static int |
| 357 |
FindLibraries (interp, pathPtr, buf) |
| 358 |
Tcl_Interp * interp; |
| 359 |
Tcl_Obj * pathPtr; |
| 360 |
Tcl_DString * buf; |
| 361 |
{ |
| 362 |
FILE * f; |
| 363 |
int c = 0; |
| 364 |
char * p; |
| 365 |
CONST char *native; |
| 366 |
|
| 367 |
char *fileName = Tcl_GetString(pathPtr); |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
native = Tcl_FSGetNativePath(pathPtr); |
| 372 |
f = fopen(native, "rb"); |
| 373 |
|
| 374 |
if (f == NULL) { |
| 375 |
Tcl_AppendResult (interp, "couldn't open \"", fileName, "\": ", |
| 376 |
Tcl_PosixError (interp), (char *) NULL); |
| 377 |
return TCL_ERROR; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
p = "@LIBS: "; |
| 383 |
while (*p != '\0' && (c = getc (f)) != EOF) { |
| 384 |
if (c == *p) { |
| 385 |
++p; |
| 386 |
} |
| 387 |
else { |
| 388 |
p = "@LIBS: "; |
| 389 |
if (c == *p) { |
| 390 |
++p; |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
if (c == EOF) { |
| 398 |
Tcl_AppendResult (interp, "File \"", fileName, |
| 399 |
"\" is not a Tcl load module.", (char *) NULL); |
| 400 |
(void) fclose (f); |
| 401 |
return TCL_ERROR; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
while ((c = getc (f)) != '\0' && c != EOF) { |
| 407 |
char cc = c; |
| 408 |
Tcl_DStringAppend (buf, &cc, 1); |
| 409 |
} |
| 410 |
(void) fclose (f); |
| 411 |
|
| 412 |
if (c == EOF) { |
| 413 |
Tcl_AppendResult (interp, "Library directory in \"", fileName, |
| 414 |
"\" ends prematurely.", (char *) NULL); |
| 415 |
return TCL_ERROR; |
| 416 |
} |
| 417 |
|
| 418 |
return TCL_OK; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
static void |
| 440 |
UnlinkSymbolTable () |
| 441 |
{ |
| 442 |
(void) unlink (SymbolTableFile); |
| 443 |
ckfree (SymbolTableFile); |
| 444 |
SymbolTableFile = NULL; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
void |
| 466 |
TclpUnloadFile(loadHandle) |
| 467 |
Tcl_LoadHandle loadHandle; |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
{ |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
int |
| 495 |
TclGuessPackageName(fileName, bufPtr) |
| 496 |
CONST char *fileName; |
| 497 |
|
| 498 |
Tcl_DString *bufPtr; |
| 499 |
|
| 500 |
{ |
| 501 |
CONST char *p, *q; |
| 502 |
char *r; |
| 503 |
|
| 504 |
if ((q = strrchr(fileName,'/'))) { |
| 505 |
q++; |
| 506 |
} else { |
| 507 |
q = fileName; |
| 508 |
} |
| 509 |
if (!strncmp(q,"lib",3)) { |
| 510 |
q+=3; |
| 511 |
} |
| 512 |
p = q; |
| 513 |
while ((*p) && (*p != '.') && ((*p<'0') || (*p>'9'))) { |
| 514 |
p++; |
| 515 |
} |
| 516 |
if ((p>q+2) && !strncmp(p-2,"_G0.",4)) { |
| 517 |
p-=2; |
| 518 |
} |
| 519 |
if (p<q) { |
| 520 |
return 0; |
| 521 |
} |
| 522 |
|
| 523 |
Tcl_DStringAppend(bufPtr,q, p-q); |
| 524 |
|
| 525 |
r = Tcl_DStringValue(bufPtr); |
| 526 |
r += strlen(r) - (p-q); |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
Tcl_UtfToTitle(r); |
| 533 |
Tcl_DStringSetLength(bufPtr, strlen(Tcl_DStringValue(bufPtr))); |
| 534 |
|
| 535 |
return 1; |
| 536 |
} |