sl@0: /* sl@0: * tclLoadAix.c -- sl@0: * sl@0: * This file implements the dlopen and dlsym APIs under the sl@0: * AIX operating system, to enable the Tcl "load" command to sl@0: * work. This code was provided by Jens-Uwe Mager. sl@0: * sl@0: * This file is subject to the following copyright notice, which is sl@0: * different from the notice used elsewhere in Tcl. The file has sl@0: * been modified to incorporate the file dlfcn.h in-line. sl@0: * sl@0: * Copyright (c) 1992,1993,1995,1996, Jens-Uwe Mager, Helios Software GmbH sl@0: * Not derived from licensed software. sl@0: sl@0: * Permission is granted to freely use, copy, modify, and redistribute sl@0: * this software, provided that the author is not construed to be liable sl@0: * for any results of using the software, alterations are clearly marked sl@0: * as such, and this notice is not modified. sl@0: * sl@0: * RCS: @(#) $Id: tclLoadAix.c,v 1.3 1999/04/16 00:48:04 stanton Exp $ sl@0: * sl@0: * Note: this file has been altered from the original in a few sl@0: * ways in order to work properly with Tcl. sl@0: */ sl@0: sl@0: /* sl@0: * @(#)dlfcn.c 1.7 revision of 95/08/14 19:08:38 sl@0: * This is an unpublished work copyright (c) 1992 HELIOS Software GmbH sl@0: * 30159 Hannover, Germany sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "../compat/dlfcn.h" sl@0: sl@0: /* sl@0: * We simulate dlopen() et al. through a call to load. Because AIX has sl@0: * no call to find an exported symbol we read the loader section of the sl@0: * loaded module and build a list of exported symbols and their virtual sl@0: * address. sl@0: */ sl@0: sl@0: typedef struct { sl@0: char *name; /* the symbols's name */ sl@0: void *addr; /* its relocated virtual address */ sl@0: } Export, *ExportPtr; sl@0: sl@0: /* sl@0: * xlC uses the following structure to list its constructors and sl@0: * destructors. This is gleaned from the output of munch. sl@0: */ sl@0: typedef struct { sl@0: void (*init)(void); /* call static constructors */ sl@0: void (*term)(void); /* call static destructors */ sl@0: } Cdtor, *CdtorPtr; sl@0: sl@0: /* sl@0: * The void * handle returned from dlopen is actually a ModulePtr. sl@0: */ sl@0: typedef struct Module { sl@0: struct Module *next; sl@0: char *name; /* module name for refcounting */ sl@0: int refCnt; /* the number of references */ sl@0: void *entry; /* entry point from load */ sl@0: struct dl_info *info; /* optional init/terminate functions */ sl@0: CdtorPtr cdtors; /* optional C++ constructors */ sl@0: int nExports; /* the number of exports found */ sl@0: ExportPtr exports; /* the array of exports */ sl@0: } Module, *ModulePtr; sl@0: sl@0: /* sl@0: * We keep a list of all loaded modules to be able to call the fini sl@0: * handlers and destructors at atexit() time. sl@0: */ sl@0: static ModulePtr modList; sl@0: sl@0: /* sl@0: * The last error from one of the dl* routines is kept in static sl@0: * variables here. Each error is returned only once to the caller. sl@0: */ sl@0: static char errbuf[BUFSIZ]; sl@0: static int errvalid; sl@0: sl@0: static void caterr(char *); sl@0: static int readExports(ModulePtr); sl@0: static void terminate(void); sl@0: static void *findMain(void); sl@0: sl@0: VOID *dlopen(const char *path, int mode) sl@0: { sl@0: register ModulePtr mp; sl@0: static void *mainModule; sl@0: sl@0: /* sl@0: * Upon the first call register a terminate handler that will sl@0: * close all libraries. Also get a reference to the main module sl@0: * for use with loadbind. sl@0: */ sl@0: if (!mainModule) { sl@0: if ((mainModule = findMain()) == NULL) sl@0: return NULL; sl@0: atexit(terminate); sl@0: } sl@0: /* sl@0: * Scan the list of modules if we have the module already loaded. sl@0: */ sl@0: for (mp = modList; mp; mp = mp->next) sl@0: if (strcmp(mp->name, path) == 0) { sl@0: mp->refCnt++; sl@0: return (VOID *) mp; sl@0: } sl@0: if ((mp = (ModulePtr)calloc(1, sizeof(*mp))) == NULL) { sl@0: errvalid++; sl@0: strcpy(errbuf, "calloc: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: return (VOID *) NULL; sl@0: } sl@0: mp->name = malloc((unsigned) (strlen(path) + 1)); sl@0: strcpy(mp->name, path); sl@0: /* sl@0: * load should be declared load(const char *...). Thus we sl@0: * cast the path to a normal char *. Ugly. sl@0: */ sl@0: if ((mp->entry = (void *)load((char *)path, L_NOAUTODEFER, NULL)) == NULL) { sl@0: free(mp->name); sl@0: free(mp); sl@0: errvalid++; sl@0: strcpy(errbuf, "dlopen: "); sl@0: strcat(errbuf, path); sl@0: strcat(errbuf, ": "); sl@0: /* sl@0: * If AIX says the file is not executable, the error sl@0: * can be further described by querying the loader about sl@0: * the last error. sl@0: */ sl@0: if (errno == ENOEXEC) { sl@0: char *tmp[BUFSIZ/sizeof(char *)]; sl@0: if (loadquery(L_GETMESSAGES, tmp, sizeof(tmp)) == -1) sl@0: strcpy(errbuf, strerror(errno)); sl@0: else { sl@0: char **p; sl@0: for (p = tmp; *p; p++) sl@0: caterr(*p); sl@0: } sl@0: } else sl@0: strcat(errbuf, strerror(errno)); sl@0: return (VOID *) NULL; sl@0: } sl@0: mp->refCnt = 1; sl@0: mp->next = modList; sl@0: modList = mp; sl@0: if (loadbind(0, mainModule, mp->entry) == -1) { sl@0: dlclose(mp); sl@0: errvalid++; sl@0: strcpy(errbuf, "loadbind: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: return (VOID *) NULL; sl@0: } sl@0: /* sl@0: * If the user wants global binding, loadbind against all other sl@0: * loaded modules. sl@0: */ sl@0: if (mode & RTLD_GLOBAL) { sl@0: register ModulePtr mp1; sl@0: for (mp1 = mp->next; mp1; mp1 = mp1->next) sl@0: if (loadbind(0, mp1->entry, mp->entry) == -1) { sl@0: dlclose(mp); sl@0: errvalid++; sl@0: strcpy(errbuf, "loadbind: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: return (VOID *) NULL; sl@0: } sl@0: } sl@0: if (readExports(mp) == -1) { sl@0: dlclose(mp); sl@0: return (VOID *) NULL; sl@0: } sl@0: /* sl@0: * If there is a dl_info structure, call the init function. sl@0: */ sl@0: if (mp->info = (struct dl_info *)dlsym(mp, "dl_info")) { sl@0: if (mp->info->init) sl@0: (*mp->info->init)(); sl@0: } else sl@0: errvalid = 0; sl@0: /* sl@0: * If the shared object was compiled using xlC we will need sl@0: * to call static constructors (and later on dlclose destructors). sl@0: */ sl@0: if (mp->cdtors = (CdtorPtr)dlsym(mp, "__cdtors")) { sl@0: while (mp->cdtors->init) { sl@0: (*mp->cdtors->init)(); sl@0: mp->cdtors++; sl@0: } sl@0: } else sl@0: errvalid = 0; sl@0: return (VOID *) mp; sl@0: } sl@0: sl@0: /* sl@0: * Attempt to decipher an AIX loader error message and append it sl@0: * to our static error message buffer. sl@0: */ sl@0: static void caterr(char *s) sl@0: { sl@0: register char *p = s; sl@0: sl@0: while (*p >= '0' && *p <= '9') sl@0: p++; sl@0: switch(atoi(s)) { /* INTL: "C", UTF safe. */ sl@0: case L_ERROR_TOOMANY: sl@0: strcat(errbuf, "to many errors"); sl@0: break; sl@0: case L_ERROR_NOLIB: sl@0: strcat(errbuf, "can't load library"); sl@0: strcat(errbuf, p); sl@0: break; sl@0: case L_ERROR_UNDEF: sl@0: strcat(errbuf, "can't find symbol"); sl@0: strcat(errbuf, p); sl@0: break; sl@0: case L_ERROR_RLDBAD: sl@0: strcat(errbuf, "bad RLD"); sl@0: strcat(errbuf, p); sl@0: break; sl@0: case L_ERROR_FORMAT: sl@0: strcat(errbuf, "bad exec format in"); sl@0: strcat(errbuf, p); sl@0: break; sl@0: case L_ERROR_ERRNO: sl@0: strcat(errbuf, strerror(atoi(++p))); /* INTL: "C", UTF safe. */ sl@0: break; sl@0: default: sl@0: strcat(errbuf, s); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: VOID *dlsym(void *handle, const char *symbol) sl@0: { sl@0: register ModulePtr mp = (ModulePtr)handle; sl@0: register ExportPtr ep; sl@0: register int i; sl@0: sl@0: /* sl@0: * Could speed up the search, but I assume that one assigns sl@0: * the result to function pointers anyways. sl@0: */ sl@0: for (ep = mp->exports, i = mp->nExports; i; i--, ep++) sl@0: if (strcmp(ep->name, symbol) == 0) sl@0: return ep->addr; sl@0: errvalid++; sl@0: strcpy(errbuf, "dlsym: undefined symbol "); sl@0: strcat(errbuf, symbol); sl@0: return NULL; sl@0: } sl@0: sl@0: char *dlerror(void) sl@0: { sl@0: if (errvalid) { sl@0: errvalid = 0; sl@0: return errbuf; sl@0: } sl@0: return NULL; sl@0: } sl@0: sl@0: int dlclose(void *handle) sl@0: { sl@0: register ModulePtr mp = (ModulePtr)handle; sl@0: int result; sl@0: register ModulePtr mp1; sl@0: sl@0: if (--mp->refCnt > 0) sl@0: return 0; sl@0: if (mp->info && mp->info->fini) sl@0: (*mp->info->fini)(); sl@0: if (mp->cdtors) sl@0: while (mp->cdtors->term) { sl@0: (*mp->cdtors->term)(); sl@0: mp->cdtors++; sl@0: } sl@0: result = unload(mp->entry); sl@0: if (result == -1) { sl@0: errvalid++; sl@0: strcpy(errbuf, strerror(errno)); sl@0: } sl@0: if (mp->exports) { sl@0: register ExportPtr ep; sl@0: register int i; sl@0: for (ep = mp->exports, i = mp->nExports; i; i--, ep++) sl@0: if (ep->name) sl@0: free(ep->name); sl@0: free(mp->exports); sl@0: } sl@0: if (mp == modList) sl@0: modList = mp->next; sl@0: else { sl@0: for (mp1 = modList; mp1; mp1 = mp1->next) sl@0: if (mp1->next == mp) { sl@0: mp1->next = mp->next; sl@0: break; sl@0: } sl@0: } sl@0: free(mp->name); sl@0: free(mp); sl@0: return result; sl@0: } sl@0: sl@0: static void terminate(void) sl@0: { sl@0: while (modList) sl@0: dlclose(modList); sl@0: } sl@0: sl@0: /* sl@0: * Build the export table from the XCOFF .loader section. sl@0: */ sl@0: static int readExports(ModulePtr mp) sl@0: { sl@0: LDFILE *ldp = NULL; sl@0: SCNHDR sh, shdata; sl@0: LDHDR *lhp; sl@0: char *ldbuf; sl@0: LDSYM *ls; sl@0: int i; sl@0: ExportPtr ep; sl@0: sl@0: if ((ldp = ldopen(mp->name, ldp)) == NULL) { sl@0: struct ld_info *lp; sl@0: char *buf; sl@0: int size = 4*1024; sl@0: if (errno != ENOENT) { sl@0: errvalid++; sl@0: strcpy(errbuf, "readExports: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: return -1; sl@0: } sl@0: /* sl@0: * The module might be loaded due to the LIBPATH sl@0: * environment variable. Search for the loaded sl@0: * module using L_GETINFO. sl@0: */ sl@0: if ((buf = malloc(size)) == NULL) { sl@0: errvalid++; sl@0: strcpy(errbuf, "readExports: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: return -1; sl@0: } sl@0: while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) { sl@0: free(buf); sl@0: size += 4*1024; sl@0: if ((buf = malloc(size)) == NULL) { sl@0: errvalid++; sl@0: strcpy(errbuf, "readExports: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: return -1; sl@0: } sl@0: } sl@0: if (i == -1) { sl@0: errvalid++; sl@0: strcpy(errbuf, "readExports: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: free(buf); sl@0: return -1; sl@0: } sl@0: /* sl@0: * Traverse the list of loaded modules. The entry point sl@0: * returned by load() does actually point to the data sl@0: * segment origin. sl@0: */ sl@0: lp = (struct ld_info *)buf; sl@0: while (lp) { sl@0: if (lp->ldinfo_dataorg == mp->entry) { sl@0: ldp = ldopen(lp->ldinfo_filename, ldp); sl@0: break; sl@0: } sl@0: if (lp->ldinfo_next == 0) sl@0: lp = NULL; sl@0: else sl@0: lp = (struct ld_info *)((char *)lp + lp->ldinfo_next); sl@0: } sl@0: free(buf); sl@0: if (!ldp) { sl@0: errvalid++; sl@0: strcpy(errbuf, "readExports: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: return -1; sl@0: } sl@0: } sl@0: if (TYPE(ldp) != U802TOCMAGIC) { sl@0: errvalid++; sl@0: strcpy(errbuf, "readExports: bad magic"); sl@0: while(ldclose(ldp) == FAILURE) sl@0: ; sl@0: return -1; sl@0: } sl@0: /* sl@0: * Get the padding for the data section. This is needed for sl@0: * AIX 4.1 compilers. This is used when building the final sl@0: * function pointer to the exported symbol. sl@0: */ sl@0: if (ldnshread(ldp, _DATA, &shdata) != SUCCESS) { sl@0: errvalid++; sl@0: strcpy(errbuf, "readExports: cannot read data section header"); sl@0: while(ldclose(ldp) == FAILURE) sl@0: ; sl@0: return -1; sl@0: } sl@0: if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) { sl@0: errvalid++; sl@0: strcpy(errbuf, "readExports: cannot read loader section header"); sl@0: while(ldclose(ldp) == FAILURE) sl@0: ; sl@0: return -1; sl@0: } sl@0: /* sl@0: * We read the complete loader section in one chunk, this makes sl@0: * finding long symbol names residing in the string table easier. sl@0: */ sl@0: if ((ldbuf = (char *)malloc(sh.s_size)) == NULL) { sl@0: errvalid++; sl@0: strcpy(errbuf, "readExports: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: while(ldclose(ldp) == FAILURE) sl@0: ; sl@0: return -1; sl@0: } sl@0: if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) { sl@0: errvalid++; sl@0: strcpy(errbuf, "readExports: cannot seek to loader section"); sl@0: free(ldbuf); sl@0: while(ldclose(ldp) == FAILURE) sl@0: ; sl@0: return -1; sl@0: } sl@0: if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) { sl@0: errvalid++; sl@0: strcpy(errbuf, "readExports: cannot read loader section"); sl@0: free(ldbuf); sl@0: while(ldclose(ldp) == FAILURE) sl@0: ; sl@0: return -1; sl@0: } sl@0: lhp = (LDHDR *)ldbuf; sl@0: ls = (LDSYM *)(ldbuf+LDHDRSZ); sl@0: /* sl@0: * Count the number of exports to include in our export table. sl@0: */ sl@0: for (i = lhp->l_nsyms; i; i--, ls++) { sl@0: if (!LDR_EXPORT(*ls)) sl@0: continue; sl@0: mp->nExports++; sl@0: } sl@0: if ((mp->exports = (ExportPtr)calloc(mp->nExports, sizeof(*mp->exports))) == NULL) { sl@0: errvalid++; sl@0: strcpy(errbuf, "readExports: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: free(ldbuf); sl@0: while(ldclose(ldp) == FAILURE) sl@0: ; sl@0: return -1; sl@0: } sl@0: /* sl@0: * Fill in the export table. All entries are relative to sl@0: * the entry point we got from load. sl@0: */ sl@0: ep = mp->exports; sl@0: ls = (LDSYM *)(ldbuf+LDHDRSZ); sl@0: for (i = lhp->l_nsyms; i; i--, ls++) { sl@0: char *symname; sl@0: char tmpsym[SYMNMLEN+1]; sl@0: if (!LDR_EXPORT(*ls)) sl@0: continue; sl@0: if (ls->l_zeroes == 0) sl@0: symname = ls->l_offset+lhp->l_stoff+ldbuf; sl@0: else { sl@0: /* sl@0: * The l_name member is not zero terminated, we sl@0: * must copy the first SYMNMLEN chars and make sl@0: * sure we have a zero byte at the end. sl@0: */ sl@0: strncpy(tmpsym, ls->l_name, SYMNMLEN); sl@0: tmpsym[SYMNMLEN] = '\0'; sl@0: symname = tmpsym; sl@0: } sl@0: ep->name = malloc((unsigned) (strlen(symname) + 1)); sl@0: strcpy(ep->name, symname); sl@0: ep->addr = (void *)((unsigned long)mp->entry + sl@0: ls->l_value - shdata.s_vaddr); sl@0: ep++; sl@0: } sl@0: free(ldbuf); sl@0: while(ldclose(ldp) == FAILURE) sl@0: ; sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: * Find the main modules entry point. This is used as export pointer sl@0: * for loadbind() to be able to resolve references to the main part. sl@0: */ sl@0: static void * findMain(void) sl@0: { sl@0: struct ld_info *lp; sl@0: char *buf; sl@0: int size = 4*1024; sl@0: int i; sl@0: void *ret; sl@0: sl@0: if ((buf = malloc(size)) == NULL) { sl@0: errvalid++; sl@0: strcpy(errbuf, "findMain: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: return NULL; sl@0: } sl@0: while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) { sl@0: free(buf); sl@0: size += 4*1024; sl@0: if ((buf = malloc(size)) == NULL) { sl@0: errvalid++; sl@0: strcpy(errbuf, "findMain: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: return NULL; sl@0: } sl@0: } sl@0: if (i == -1) { sl@0: errvalid++; sl@0: strcpy(errbuf, "findMain: "); sl@0: strcat(errbuf, strerror(errno)); sl@0: free(buf); sl@0: return NULL; sl@0: } sl@0: /* sl@0: * The first entry is the main module. The entry point sl@0: * returned by load() does actually point to the data sl@0: * segment origin. sl@0: */ sl@0: lp = (struct ld_info *)buf; sl@0: ret = lp->ldinfo_dataorg; sl@0: free(buf); sl@0: return ret; sl@0: } sl@0: