| 1 |
/* |
| 2 |
* tclLoadAix.c -- |
| 3 |
* |
| 4 |
* This file implements the dlopen and dlsym APIs under the AIX operating |
| 5 |
* system, to enable the Tcl "load" command to work. This code was |
| 6 |
* provided by Jens-Uwe Mager. |
| 7 |
* |
| 8 |
* This file is subject to the following copyright notice, which is |
| 9 |
* different from the notice used elsewhere in Tcl. The file has been |
| 10 |
* modified to incorporate the file dlfcn.h in-line. |
| 11 |
* |
| 12 |
* Copyright (c) 1992,1993,1995,1996, Jens-Uwe Mager, Helios Software GmbH |
| 13 |
* Not derived from licensed software. |
| 14 |
* |
| 15 |
* Permission is granted to freely use, copy, modify, and redistribute |
| 16 |
* this software, provided that the author is not construed to be liable |
| 17 |
* for any results of using the software, alterations are clearly marked |
| 18 |
* as such, and this notice is not modified. |
| 19 |
* |
| 20 |
* Note: this file has been altered from the original in a few ways in order |
| 21 |
* to work properly with Tcl. |
| 22 |
*/ |
| 23 |
|
| 24 |
/* |
| 25 |
* @(#)dlfcn.c 1.7 revision of 95/08/14 19:08:38 |
| 26 |
* This is an unpublished work copyright (c) 1992 HELIOS Software GmbH |
| 27 |
* 30159 Hannover, Germany |
| 28 |
*/ |
| 29 |
|
| 30 |
#include <stdio.h> |
| 31 |
#include <errno.h> |
| 32 |
#include <string.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <sys/types.h> |
| 35 |
#include <sys/ldr.h> |
| 36 |
#include <a.out.h> |
| 37 |
#include <ldfcn.h> |
| 38 |
#include "../compat/dlfcn.h" |
| 39 |
|
| 40 |
/* |
| 41 |
* We simulate dlopen() et al. through a call to load. Because AIX has no call |
| 42 |
* to find an exported symbol we read the loader section of the loaded module |
| 43 |
* and build a list of exported symbols and their virtual address. |
| 44 |
*/ |
| 45 |
|
| 46 |
typedef struct { |
| 47 |
char *name; /* The symbols's name. */ |
| 48 |
void *addr; /* Its relocated virtual address. */ |
| 49 |
} Export, *ExportPtr; |
| 50 |
|
| 51 |
/* |
| 52 |
* xlC uses the following structure to list its constructors and destructors. |
| 53 |
* This is gleaned from the output of munch. |
| 54 |
*/ |
| 55 |
|
| 56 |
typedef struct { |
| 57 |
void (*init)(void); /* call static constructors */ |
| 58 |
void (*term)(void); /* call static destructors */ |
| 59 |
} Cdtor, *CdtorPtr; |
| 60 |
|
| 61 |
/* |
| 62 |
* The void * handle returned from dlopen is actually a ModulePtr. |
| 63 |
*/ |
| 64 |
|
| 65 |
typedef struct Module { |
| 66 |
struct Module *next; |
| 67 |
char *name; /* module name for refcounting */ |
| 68 |
int refCnt; /* the number of references */ |
| 69 |
void *entry; /* entry point from load */ |
| 70 |
struct dl_info *info; /* optional init/terminate functions */ |
| 71 |
CdtorPtr cdtors; /* optional C++ constructors */ |
| 72 |
int nExports; /* the number of exports found */ |
| 73 |
ExportPtr exports; /* the array of exports */ |
| 74 |
} Module, *ModulePtr; |
| 75 |
|
| 76 |
/* |
| 77 |
* We keep a list of all loaded modules to be able to call the fini handlers |
| 78 |
* and destructors at atexit() time. |
| 79 |
*/ |
| 80 |
|
| 81 |
static ModulePtr modList; |
| 82 |
|
| 83 |
/* |
| 84 |
* The last error from one of the dl* routines is kept in static variables |
| 85 |
* here. Each error is returned only once to the caller. |
| 86 |
*/ |
| 87 |
|
| 88 |
static char errbuf[BUFSIZ]; |
| 89 |
static int errvalid; |
| 90 |
|
| 91 |
static void caterr(char *); |
| 92 |
static int readExports(ModulePtr); |
| 93 |
static void terminate(void); |
| 94 |
static void *findMain(void); |
| 95 |
|
| 96 |
void * |
| 97 |
dlopen( |
| 98 |
const char *path, |
| 99 |
int mode) |
| 100 |
{ |
| 101 |
ModulePtr mp; |
| 102 |
static void *mainModule; |
| 103 |
|
| 104 |
/* |
| 105 |
* Upon the first call register a terminate handler that will close all |
| 106 |
* libraries. Also get a reference to the main module for use with |
| 107 |
* loadbind. |
| 108 |
*/ |
| 109 |
|
| 110 |
if (!mainModule) { |
| 111 |
mainModule = findMain(); |
| 112 |
if (mainModule == NULL) { |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
atexit(terminate); |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
* Scan the list of modules if we have the module already loaded. |
| 120 |
*/ |
| 121 |
|
| 122 |
for (mp = modList; mp; mp = mp->next) { |
| 123 |
if (strcmp(mp->name, path) == 0) { |
| 124 |
mp->refCnt++; |
| 125 |
return (void *) mp; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
mp = (ModulePtr) calloc(1, sizeof(*mp)); |
| 130 |
if (mp == NULL) { |
| 131 |
errvalid++; |
| 132 |
strcpy(errbuf, "calloc: "); |
| 133 |
strcat(errbuf, strerror(errno)); |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
|
| 137 |
mp->name = malloc(strlen(path) + 1); |
| 138 |
strcpy(mp->name, path); |
| 139 |
|
| 140 |
/* |
| 141 |
* load should be declared load(const char *...). Thus we cast the path to |
| 142 |
* a normal char *. Ugly. |
| 143 |
*/ |
| 144 |
|
| 145 |
mp->entry = (void *) load((char *)path, L_NOAUTODEFER, NULL); |
| 146 |
if (mp->entry == NULL) { |
| 147 |
free(mp->name); |
| 148 |
free(mp); |
| 149 |
errvalid++; |
| 150 |
strcpy(errbuf, "dlopen: "); |
| 151 |
strcat(errbuf, path); |
| 152 |
strcat(errbuf, ": "); |
| 153 |
|
| 154 |
/* |
| 155 |
* If AIX says the file is not executable, the error can be further |
| 156 |
* described by querying the loader about the last error. |
| 157 |
*/ |
| 158 |
|
| 159 |
if (errno == ENOEXEC) { |
| 160 |
char *tmp[BUFSIZ/sizeof(char *)], **p; |
| 161 |
|
| 162 |
if (loadquery(L_GETMESSAGES, tmp, sizeof(tmp)) == -1) { |
| 163 |
strcpy(errbuf, strerror(errno)); |
| 164 |
} else { |
| 165 |
for (p=tmp ; *p ; p++) { |
| 166 |
caterr(*p); |
| 167 |
} |
| 168 |
} |
| 169 |
} else { |
| 170 |
strcat(errbuf, strerror(errno)); |
| 171 |
} |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
|
| 175 |
mp->refCnt = 1; |
| 176 |
mp->next = modList; |
| 177 |
modList = mp; |
| 178 |
|
| 179 |
if (loadbind(0, mainModule, mp->entry) == -1) { |
| 180 |
loadbindFailure: |
| 181 |
dlclose(mp); |
| 182 |
errvalid++; |
| 183 |
strcpy(errbuf, "loadbind: "); |
| 184 |
strcat(errbuf, strerror(errno)); |
| 185 |
return NULL; |
| 186 |
} |
| 187 |
|
| 188 |
/* |
| 189 |
* If the user wants global binding, loadbind against all other loaded |
| 190 |
* modules. |
| 191 |
*/ |
| 192 |
|
| 193 |
if (mode & RTLD_GLOBAL) { |
| 194 |
ModulePtr mp1; |
| 195 |
|
| 196 |
for (mp1 = mp->next; mp1; mp1 = mp1->next) { |
| 197 |
if (loadbind(0, mp1->entry, mp->entry) == -1) { |
| 198 |
goto loadbindFailure; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if (readExports(mp) == -1) { |
| 204 |
dlclose(mp); |
| 205 |
return NULL; |
| 206 |
} |
| 207 |
|
| 208 |
/* |
| 209 |
* If there is a dl_info structure, call the init function. |
| 210 |
*/ |
| 211 |
|
| 212 |
if (mp->info = (struct dl_info *)dlsym(mp, "dl_info")) { |
| 213 |
if (mp->info->init) { |
| 214 |
mp->info->init(); |
| 215 |
} |
| 216 |
} else { |
| 217 |
errvalid = 0; |
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
* If the shared object was compiled using xlC we will need to call static |
| 222 |
* constructors (and later on dlclose destructors). |
| 223 |
*/ |
| 224 |
|
| 225 |
if (mp->cdtors = (CdtorPtr) dlsym(mp, "__cdtors")) { |
| 226 |
while (mp->cdtors->init) { |
| 227 |
mp->cdtors->init(); |
| 228 |
mp->cdtors++; |
| 229 |
} |
| 230 |
} else { |
| 231 |
errvalid = 0; |
| 232 |
} |
| 233 |
|
| 234 |
return (void *) mp; |
| 235 |
} |
| 236 |
|
| 237 |
/* |
| 238 |
* Attempt to decipher an AIX loader error message and append it to our static |
| 239 |
* error message buffer. |
| 240 |
*/ |
| 241 |
|
| 242 |
static void |
| 243 |
caterr( |
| 244 |
char *s) |
| 245 |
{ |
| 246 |
char *p = s; |
| 247 |
|
| 248 |
while (*p >= '0' && *p <= '9') { |
| 249 |
p++; |
| 250 |
} |
| 251 |
switch (atoi(s)) { /* INTL: "C", UTF safe. */ |
| 252 |
case L_ERROR_TOOMANY: |
| 253 |
strcat(errbuf, "to many errors"); |
| 254 |
break; |
| 255 |
case L_ERROR_NOLIB: |
| 256 |
strcat(errbuf, "can't load library"); |
| 257 |
strcat(errbuf, p); |
| 258 |
break; |
| 259 |
case L_ERROR_UNDEF: |
| 260 |
strcat(errbuf, "can't find symbol"); |
| 261 |
strcat(errbuf, p); |
| 262 |
break; |
| 263 |
case L_ERROR_RLDBAD: |
| 264 |
strcat(errbuf, "bad RLD"); |
| 265 |
strcat(errbuf, p); |
| 266 |
break; |
| 267 |
case L_ERROR_FORMAT: |
| 268 |
strcat(errbuf, "bad exec format in"); |
| 269 |
strcat(errbuf, p); |
| 270 |
break; |
| 271 |
case L_ERROR_ERRNO: |
| 272 |
strcat(errbuf, strerror(atoi(++p))); /* INTL: "C", UTF safe. */ |
| 273 |
break; |
| 274 |
default: |
| 275 |
strcat(errbuf, s); |
| 276 |
break; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
void * |
| 281 |
dlsym( |
| 282 |
void *handle, |
| 283 |
const char *symbol) |
| 284 |
{ |
| 285 |
ModulePtr mp = (ModulePtr)handle; |
| 286 |
ExportPtr ep; |
| 287 |
int i; |
| 288 |
|
| 289 |
/* |
| 290 |
* Could speed up the search, but I assume that one assigns the result to |
| 291 |
* function pointers anyways. |
| 292 |
*/ |
| 293 |
|
| 294 |
for (ep = mp->exports, i = mp->nExports; i; i--, ep++) { |
| 295 |
if (strcmp(ep->name, symbol) == 0) { |
| 296 |
return ep->addr; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
errvalid++; |
| 301 |
strcpy(errbuf, "dlsym: undefined symbol "); |
| 302 |
strcat(errbuf, symbol); |
| 303 |
return NULL; |
| 304 |
} |
| 305 |
|
| 306 |
char * |
| 307 |
dlerror(void) |
| 308 |
{ |
| 309 |
if (errvalid) { |
| 310 |
errvalid = 0; |
| 311 |
return errbuf; |
| 312 |
} |
| 313 |
return NULL; |
| 314 |
} |
| 315 |
|
| 316 |
int |
| 317 |
dlclose( |
| 318 |
void *handle) |
| 319 |
{ |
| 320 |
ModulePtr mp = (ModulePtr)handle; |
| 321 |
int result; |
| 322 |
ModulePtr mp1; |
| 323 |
|
| 324 |
if (--mp->refCnt > 0) { |
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
if (mp->info && mp->info->fini) { |
| 329 |
mp->info->fini(); |
| 330 |
} |
| 331 |
|
| 332 |
if (mp->cdtors) { |
| 333 |
while (mp->cdtors->term) { |
| 334 |
mp->cdtors->term(); |
| 335 |
mp->cdtors++; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
result = unload(mp->entry); |
| 340 |
if (result == -1) { |
| 341 |
errvalid++; |
| 342 |
strcpy(errbuf, strerror(errno)); |
| 343 |
} |
| 344 |
|
| 345 |
if (mp->exports) { |
| 346 |
ExportPtr ep; |
| 347 |
int i; |
| 348 |
for (ep = mp->exports, i = mp->nExports; i; i--, ep++) { |
| 349 |
if (ep->name) { |
| 350 |
free(ep->name); |
| 351 |
} |
| 352 |
} |
| 353 |
free(mp->exports); |
| 354 |
} |
| 355 |
|
| 356 |
if (mp == modList) { |
| 357 |
modList = mp->next; |
| 358 |
} else { |
| 359 |
for (mp1 = modList; mp1; mp1 = mp1->next) { |
| 360 |
if (mp1->next == mp) { |
| 361 |
mp1->next = mp->next; |
| 362 |
break; |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
free(mp->name); |
| 368 |
free(mp); |
| 369 |
return result; |
| 370 |
} |
| 371 |
|
| 372 |
static void |
| 373 |
terminate(void) |
| 374 |
{ |
| 375 |
while (modList) { |
| 376 |
dlclose(modList); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
/* |
| 381 |
* Build the export table from the XCOFF .loader section. |
| 382 |
*/ |
| 383 |
|
| 384 |
static int |
| 385 |
readExports( |
| 386 |
ModulePtr mp) |
| 387 |
{ |
| 388 |
LDFILE *ldp = NULL; |
| 389 |
SCNHDR sh, shdata; |
| 390 |
LDHDR *lhp; |
| 391 |
char *ldbuf; |
| 392 |
LDSYM *ls; |
| 393 |
int i; |
| 394 |
ExportPtr ep; |
| 395 |
const char *errMsg; |
| 396 |
|
| 397 |
#define Error(msg) do{errMsg=(msg);goto error;}while(0) |
| 398 |
#define SysErr() Error(strerror(errno)) |
| 399 |
|
| 400 |
ldp = ldopen(mp->name, ldp); |
| 401 |
if (ldp == NULL) { |
| 402 |
struct ld_info *lp; |
| 403 |
char *buf; |
| 404 |
int size = 0; |
| 405 |
|
| 406 |
if (errno != ENOENT) { |
| 407 |
SysErr(); |
| 408 |
} |
| 409 |
|
| 410 |
/* |
| 411 |
* The module might be loaded due to the LIBPATH environment variable. |
| 412 |
* Search for the loaded module using L_GETINFO. |
| 413 |
*/ |
| 414 |
|
| 415 |
while (1) { |
| 416 |
size += 4 * 1024; |
| 417 |
buf = malloc(size); |
| 418 |
if (buf == NULL) { |
| 419 |
SysErr(); |
| 420 |
} |
| 421 |
|
| 422 |
i = loadquery(L_GETINFO, buf, size); |
| 423 |
|
| 424 |
if (i != -1) { |
| 425 |
break; |
| 426 |
} |
| 427 |
free(buf); |
| 428 |
if (errno != ENOMEM) { |
| 429 |
SysErr(); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
/* |
| 434 |
* Traverse the list of loaded modules. The entry point returned by |
| 435 |
* load() does actually point to the data segment origin. |
| 436 |
*/ |
| 437 |
|
| 438 |
lp = (struct ld_info *) buf; |
| 439 |
while (lp) { |
| 440 |
if (lp->ldinfo_dataorg == mp->entry) { |
| 441 |
ldp = ldopen(lp->ldinfo_filename, ldp); |
| 442 |
break; |
| 443 |
} |
| 444 |
if (lp->ldinfo_next == 0) { |
| 445 |
lp = NULL; |
| 446 |
} else { |
| 447 |
lp = (struct ld_info *)((char *)lp + lp->ldinfo_next); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
free(buf); |
| 452 |
|
| 453 |
if (!ldp) { |
| 454 |
SysErr(); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
if (TYPE(ldp) != U802TOCMAGIC) { |
| 459 |
Error("bad magic"); |
| 460 |
} |
| 461 |
|
| 462 |
/* |
| 463 |
* Get the padding for the data section. This is needed for AIX 4.1 |
| 464 |
* compilers. This is used when building the final function pointer to the |
| 465 |
* exported symbol. |
| 466 |
*/ |
| 467 |
|
| 468 |
if (ldnshread(ldp, _DATA, &shdata) != SUCCESS) { |
| 469 |
Error("cannot read data section header"); |
| 470 |
} |
| 471 |
|
| 472 |
if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) { |
| 473 |
Error("cannot read loader section header"); |
| 474 |
} |
| 475 |
|
| 476 |
/* |
| 477 |
* We read the complete loader section in one chunk, this makes finding |
| 478 |
* long symbol names residing in the string table easier. |
| 479 |
*/ |
| 480 |
|
| 481 |
ldbuf = (char *) malloc(sh.s_size); |
| 482 |
if (ldbuf == NULL) { |
| 483 |
SysErr(); |
| 484 |
} |
| 485 |
|
| 486 |
if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) { |
| 487 |
free(ldbuf); |
| 488 |
Error("cannot seek to loader section"); |
| 489 |
} |
| 490 |
|
| 491 |
if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) { |
| 492 |
free(ldbuf); |
| 493 |
Error("cannot read loader section"); |
| 494 |
} |
| 495 |
|
| 496 |
lhp = (LDHDR *) ldbuf; |
| 497 |
ls = (LDSYM *)(ldbuf + LDHDRSZ); |
| 498 |
|
| 499 |
/* |
| 500 |
* Count the number of exports to include in our export table. |
| 501 |
*/ |
| 502 |
|
| 503 |
for (i = lhp->l_nsyms; i; i--, ls++) { |
| 504 |
if (!LDR_EXPORT(*ls)) { |
| 505 |
continue; |
| 506 |
} |
| 507 |
mp->nExports++; |
| 508 |
} |
| 509 |
|
| 510 |
mp->exports = (ExportPtr) calloc(mp->nExports, sizeof(*mp->exports)); |
| 511 |
if (mp->exports == NULL) { |
| 512 |
free(ldbuf); |
| 513 |
SysErr(); |
| 514 |
} |
| 515 |
|
| 516 |
/* |
| 517 |
* Fill in the export table. All entries are relative to the entry point |
| 518 |
* we got from load. |
| 519 |
*/ |
| 520 |
|
| 521 |
ep = mp->exports; |
| 522 |
ls = (LDSYM *)(ldbuf + LDHDRSZ); |
| 523 |
for (i=lhp->l_nsyms ; i!=0 ; i--,ls++) { |
| 524 |
char *symname; |
| 525 |
char tmpsym[SYMNMLEN+1]; |
| 526 |
|
| 527 |
if (!LDR_EXPORT(*ls)) { |
| 528 |
continue; |
| 529 |
} |
| 530 |
|
| 531 |
if (ls->l_zeroes == 0) { |
| 532 |
symname = ls->l_offset + lhp->l_stoff + ldbuf; |
| 533 |
} else { |
| 534 |
/* |
| 535 |
* The l_name member is not zero terminated, we must copy the |
| 536 |
* first SYMNMLEN chars and make sure we have a zero byte at the |
| 537 |
* end. |
| 538 |
*/ |
| 539 |
|
| 540 |
strncpy(tmpsym, ls->l_name, SYMNMLEN); |
| 541 |
tmpsym[SYMNMLEN] = '\0'; |
| 542 |
symname = tmpsym; |
| 543 |
} |
| 544 |
ep->name = malloc(strlen(symname) + 1); |
| 545 |
strcpy(ep->name, symname); |
| 546 |
ep->addr = (void *)((unsigned long) |
| 547 |
mp->entry + ls->l_value - shdata.s_vaddr); |
| 548 |
ep++; |
| 549 |
} |
| 550 |
free(ldbuf); |
| 551 |
while (ldclose(ldp) == FAILURE) { |
| 552 |
/* Empty body */ |
| 553 |
} |
| 554 |
return 0; |
| 555 |
|
| 556 |
/* |
| 557 |
* This is a factoring out of the error-handling code to make the rest of |
| 558 |
* the function much simpler to read. |
| 559 |
*/ |
| 560 |
|
| 561 |
error: |
| 562 |
errvalid++; |
| 563 |
strcpy(errbuf, "readExports: "); |
| 564 |
strcat(errbuf, errMsg); |
| 565 |
|
| 566 |
if (ldp != NULL) { |
| 567 |
while (ldclose(ldp) == FAILURE) { |
| 568 |
/* Empty body */ |
| 569 |
} |
| 570 |
} |
| 571 |
return -1; |
| 572 |
} |
| 573 |
|
| 574 |
/* |
| 575 |
* Find the main modules entry point. This is used as export pointer for |
| 576 |
* loadbind() to be able to resolve references to the main part. |
| 577 |
*/ |
| 578 |
|
| 579 |
static void * |
| 580 |
findMain(void) |
| 581 |
{ |
| 582 |
struct ld_info *lp; |
| 583 |
char *buf; |
| 584 |
int size = 4*1024; |
| 585 |
int i; |
| 586 |
void *ret; |
| 587 |
|
| 588 |
buf = malloc(size); |
| 589 |
if (buf == NULL) { |
| 590 |
goto error; |
| 591 |
} |
| 592 |
|
| 593 |
while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) { |
| 594 |
free(buf); |
| 595 |
size += 4*1024; |
| 596 |
buf = malloc(size); |
| 597 |
if (buf == NULL) { |
| 598 |
goto error; |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
if (i == -1) { |
| 603 |
free(buf); |
| 604 |
goto error; |
| 605 |
} |
| 606 |
|
| 607 |
/* |
| 608 |
* The first entry is the main module. The entry point returned by load() |
| 609 |
* does actually point to the data segment origin. |
| 610 |
*/ |
| 611 |
|
| 612 |
lp = (struct ld_info *) buf; |
| 613 |
ret = lp->ldinfo_dataorg; |
| 614 |
free(buf); |
| 615 |
return ret; |
| 616 |
|
| 617 |
error: |
| 618 |
errvalid++; |
| 619 |
strcpy(errbuf, "findMain: "); |
| 620 |
strcat(errbuf, strerror(errno)); |
| 621 |
return NULL; |
| 622 |
} |
| 623 |
|
| 624 |
/* |
| 625 |
* Local Variables: |
| 626 |
* mode: c |
| 627 |
* c-basic-offset: 4 |
| 628 |
* fill-column: 78 |
| 629 |
* End: |
| 630 |
*/ |