os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/unix/tclLoadAix.c
First public contribution.
4 * This file implements the dlopen and dlsym APIs under the
5 * AIX operating system, to enable the Tcl "load" command to
6 * work. This code was provided by Jens-Uwe Mager.
8 * This file is subject to the following copyright notice, which is
9 * different from the notice used elsewhere in Tcl. The file has
10 * been modified to incorporate the file dlfcn.h in-line.
12 * Copyright (c) 1992,1993,1995,1996, Jens-Uwe Mager, Helios Software GmbH
13 * Not derived from licensed software.
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.
20 * RCS: @(#) $Id: tclLoadAix.c,v 1.3 1999/04/16 00:48:04 stanton Exp $
22 * Note: this file has been altered from the original in a few
23 * ways in order to work properly with Tcl.
27 * @(#)dlfcn.c 1.7 revision of 95/08/14 19:08:38
28 * This is an unpublished work copyright (c) 1992 HELIOS Software GmbH
29 * 30159 Hannover, Germany
36 #include <sys/types.h>
40 #include "../compat/dlfcn.h"
43 * We simulate dlopen() et al. through a call to load. Because AIX has
44 * no call to find an exported symbol we read the loader section of the
45 * loaded module and build a list of exported symbols and their virtual
50 char *name; /* the symbols's name */
51 void *addr; /* its relocated virtual address */
55 * xlC uses the following structure to list its constructors and
56 * destructors. This is gleaned from the output of munch.
59 void (*init)(void); /* call static constructors */
60 void (*term)(void); /* call static destructors */
64 * The void * handle returned from dlopen is actually a ModulePtr.
66 typedef struct Module {
68 char *name; /* module name for refcounting */
69 int refCnt; /* the number of references */
70 void *entry; /* entry point from load */
71 struct dl_info *info; /* optional init/terminate functions */
72 CdtorPtr cdtors; /* optional C++ constructors */
73 int nExports; /* the number of exports found */
74 ExportPtr exports; /* the array of exports */
78 * We keep a list of all loaded modules to be able to call the fini
79 * handlers and destructors at atexit() time.
81 static ModulePtr modList;
84 * The last error from one of the dl* routines is kept in static
85 * variables here. Each error is returned only once to the caller.
87 static char errbuf[BUFSIZ];
90 static void caterr(char *);
91 static int readExports(ModulePtr);
92 static void terminate(void);
93 static void *findMain(void);
95 VOID *dlopen(const char *path, int mode)
97 register ModulePtr mp;
98 static void *mainModule;
101 * Upon the first call register a terminate handler that will
102 * close all libraries. Also get a reference to the main module
103 * for use with loadbind.
106 if ((mainModule = findMain()) == NULL)
111 * Scan the list of modules if we have the module already loaded.
113 for (mp = modList; mp; mp = mp->next)
114 if (strcmp(mp->name, path) == 0) {
118 if ((mp = (ModulePtr)calloc(1, sizeof(*mp))) == NULL) {
120 strcpy(errbuf, "calloc: ");
121 strcat(errbuf, strerror(errno));
122 return (VOID *) NULL;
124 mp->name = malloc((unsigned) (strlen(path) + 1));
125 strcpy(mp->name, path);
127 * load should be declared load(const char *...). Thus we
128 * cast the path to a normal char *. Ugly.
130 if ((mp->entry = (void *)load((char *)path, L_NOAUTODEFER, NULL)) == NULL) {
134 strcpy(errbuf, "dlopen: ");
135 strcat(errbuf, path);
136 strcat(errbuf, ": ");
138 * If AIX says the file is not executable, the error
139 * can be further described by querying the loader about
142 if (errno == ENOEXEC) {
143 char *tmp[BUFSIZ/sizeof(char *)];
144 if (loadquery(L_GETMESSAGES, tmp, sizeof(tmp)) == -1)
145 strcpy(errbuf, strerror(errno));
148 for (p = tmp; *p; p++)
152 strcat(errbuf, strerror(errno));
153 return (VOID *) NULL;
158 if (loadbind(0, mainModule, mp->entry) == -1) {
161 strcpy(errbuf, "loadbind: ");
162 strcat(errbuf, strerror(errno));
163 return (VOID *) NULL;
166 * If the user wants global binding, loadbind against all other
169 if (mode & RTLD_GLOBAL) {
170 register ModulePtr mp1;
171 for (mp1 = mp->next; mp1; mp1 = mp1->next)
172 if (loadbind(0, mp1->entry, mp->entry) == -1) {
175 strcpy(errbuf, "loadbind: ");
176 strcat(errbuf, strerror(errno));
177 return (VOID *) NULL;
180 if (readExports(mp) == -1) {
182 return (VOID *) NULL;
185 * If there is a dl_info structure, call the init function.
187 if (mp->info = (struct dl_info *)dlsym(mp, "dl_info")) {
193 * If the shared object was compiled using xlC we will need
194 * to call static constructors (and later on dlclose destructors).
196 if (mp->cdtors = (CdtorPtr)dlsym(mp, "__cdtors")) {
197 while (mp->cdtors->init) {
198 (*mp->cdtors->init)();
207 * Attempt to decipher an AIX loader error message and append it
208 * to our static error message buffer.
210 static void caterr(char *s)
212 register char *p = s;
214 while (*p >= '0' && *p <= '9')
216 switch(atoi(s)) { /* INTL: "C", UTF safe. */
217 case L_ERROR_TOOMANY:
218 strcat(errbuf, "to many errors");
221 strcat(errbuf, "can't load library");
225 strcat(errbuf, "can't find symbol");
229 strcat(errbuf, "bad RLD");
233 strcat(errbuf, "bad exec format in");
237 strcat(errbuf, strerror(atoi(++p))); /* INTL: "C", UTF safe. */
245 VOID *dlsym(void *handle, const char *symbol)
247 register ModulePtr mp = (ModulePtr)handle;
248 register ExportPtr ep;
252 * Could speed up the search, but I assume that one assigns
253 * the result to function pointers anyways.
255 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
256 if (strcmp(ep->name, symbol) == 0)
259 strcpy(errbuf, "dlsym: undefined symbol ");
260 strcat(errbuf, symbol);
273 int dlclose(void *handle)
275 register ModulePtr mp = (ModulePtr)handle;
277 register ModulePtr mp1;
279 if (--mp->refCnt > 0)
281 if (mp->info && mp->info->fini)
284 while (mp->cdtors->term) {
285 (*mp->cdtors->term)();
288 result = unload(mp->entry);
291 strcpy(errbuf, strerror(errno));
294 register ExportPtr ep;
296 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
304 for (mp1 = modList; mp1; mp1 = mp1->next)
305 if (mp1->next == mp) {
306 mp1->next = mp->next;
315 static void terminate(void)
322 * Build the export table from the XCOFF .loader section.
324 static int readExports(ModulePtr mp)
334 if ((ldp = ldopen(mp->name, ldp)) == NULL) {
338 if (errno != ENOENT) {
340 strcpy(errbuf, "readExports: ");
341 strcat(errbuf, strerror(errno));
345 * The module might be loaded due to the LIBPATH
346 * environment variable. Search for the loaded
347 * module using L_GETINFO.
349 if ((buf = malloc(size)) == NULL) {
351 strcpy(errbuf, "readExports: ");
352 strcat(errbuf, strerror(errno));
355 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
358 if ((buf = malloc(size)) == NULL) {
360 strcpy(errbuf, "readExports: ");
361 strcat(errbuf, strerror(errno));
367 strcpy(errbuf, "readExports: ");
368 strcat(errbuf, strerror(errno));
373 * Traverse the list of loaded modules. The entry point
374 * returned by load() does actually point to the data
377 lp = (struct ld_info *)buf;
379 if (lp->ldinfo_dataorg == mp->entry) {
380 ldp = ldopen(lp->ldinfo_filename, ldp);
383 if (lp->ldinfo_next == 0)
386 lp = (struct ld_info *)((char *)lp + lp->ldinfo_next);
391 strcpy(errbuf, "readExports: ");
392 strcat(errbuf, strerror(errno));
396 if (TYPE(ldp) != U802TOCMAGIC) {
398 strcpy(errbuf, "readExports: bad magic");
399 while(ldclose(ldp) == FAILURE)
404 * Get the padding for the data section. This is needed for
405 * AIX 4.1 compilers. This is used when building the final
406 * function pointer to the exported symbol.
408 if (ldnshread(ldp, _DATA, &shdata) != SUCCESS) {
410 strcpy(errbuf, "readExports: cannot read data section header");
411 while(ldclose(ldp) == FAILURE)
415 if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
417 strcpy(errbuf, "readExports: cannot read loader section header");
418 while(ldclose(ldp) == FAILURE)
423 * We read the complete loader section in one chunk, this makes
424 * finding long symbol names residing in the string table easier.
426 if ((ldbuf = (char *)malloc(sh.s_size)) == NULL) {
428 strcpy(errbuf, "readExports: ");
429 strcat(errbuf, strerror(errno));
430 while(ldclose(ldp) == FAILURE)
434 if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
436 strcpy(errbuf, "readExports: cannot seek to loader section");
438 while(ldclose(ldp) == FAILURE)
442 if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
444 strcpy(errbuf, "readExports: cannot read loader section");
446 while(ldclose(ldp) == FAILURE)
450 lhp = (LDHDR *)ldbuf;
451 ls = (LDSYM *)(ldbuf+LDHDRSZ);
453 * Count the number of exports to include in our export table.
455 for (i = lhp->l_nsyms; i; i--, ls++) {
456 if (!LDR_EXPORT(*ls))
460 if ((mp->exports = (ExportPtr)calloc(mp->nExports, sizeof(*mp->exports))) == NULL) {
462 strcpy(errbuf, "readExports: ");
463 strcat(errbuf, strerror(errno));
465 while(ldclose(ldp) == FAILURE)
470 * Fill in the export table. All entries are relative to
471 * the entry point we got from load.
474 ls = (LDSYM *)(ldbuf+LDHDRSZ);
475 for (i = lhp->l_nsyms; i; i--, ls++) {
477 char tmpsym[SYMNMLEN+1];
478 if (!LDR_EXPORT(*ls))
480 if (ls->l_zeroes == 0)
481 symname = ls->l_offset+lhp->l_stoff+ldbuf;
484 * The l_name member is not zero terminated, we
485 * must copy the first SYMNMLEN chars and make
486 * sure we have a zero byte at the end.
488 strncpy(tmpsym, ls->l_name, SYMNMLEN);
489 tmpsym[SYMNMLEN] = '\0';
492 ep->name = malloc((unsigned) (strlen(symname) + 1));
493 strcpy(ep->name, symname);
494 ep->addr = (void *)((unsigned long)mp->entry +
495 ls->l_value - shdata.s_vaddr);
499 while(ldclose(ldp) == FAILURE)
505 * Find the main modules entry point. This is used as export pointer
506 * for loadbind() to be able to resolve references to the main part.
508 static void * findMain(void)
516 if ((buf = malloc(size)) == NULL) {
518 strcpy(errbuf, "findMain: ");
519 strcat(errbuf, strerror(errno));
522 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
525 if ((buf = malloc(size)) == NULL) {
527 strcpy(errbuf, "findMain: ");
528 strcat(errbuf, strerror(errno));
534 strcpy(errbuf, "findMain: ");
535 strcat(errbuf, strerror(errno));
540 * The first entry is the main module. The entry point
541 * returned by load() does actually point to the data
544 lp = (struct ld_info *)buf;
545 ret = lp->ldinfo_dataorg;