3 * Written: 8/31/94 by Wayne Scott (wscott@ichips.intel.com)
5 * All I did was take Jens-Uwe Mager's libdl emulation library for
6 * AIX and merged it with the dl_dlopen.xs file to create a dynamic library
7 * package that works for AIX.
9 * I did change all malloc's, free's, strdup's, calloc's to use the perl
10 * equilvant. I also removed some stuff we will not need. Call fini()
11 * on statup... It can probably be trimmed more.
15 * @(#)dlfcn.c 1.5 revision of 93/02/14 20:14:17
16 * This is an unpublished work copyright (c) 1992 Helios Software GmbH
17 * 3000 Hannover 1, Germany
27 #include <sys/types.h>
33 * AIX 4.3 does remove some useful definitions from ldfcn.h. Define
34 * these here to compensate for that lossage.
37 # define BEGINNING SEEK_SET
40 # define FSEEK(ldptr,o,p) fseek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr) +o):o,p)
43 # define FREAD(p,s,n,ldptr) fread(p,s,n,IOPTR(ldptr))
46 /* If using PerlIO, redefine these macros from <ldfcn.h> */
48 #define FSEEK(ldptr,o,p) PerlIO_seek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr)+o):o,p)
49 #define FREAD(p,s,n,ldptr) PerlIO_read(IOPTR(ldptr),p,s*n)
53 * We simulate dlopen() et al. through a call to load. Because AIX has
54 * no call to find an exported symbol we read the loader section of the
55 * loaded module and build a list of exported symbols and their virtual
60 char *name; /* the symbols's name */
61 void *addr; /* its relocated virtual address */
65 * The void * handle returned from dlopen is actually a ModulePtr.
67 typedef struct Module {
69 char *name; /* module name for refcounting */
70 int refCnt; /* the number of references */
71 void *entry; /* entry point from load */
72 int nExports; /* the number of exports found */
73 ExportPtr exports; /* the array of exports */
77 * We keep a list of all loaded modules to be able to call the fini
78 * handlers at atexit() time.
80 static ModulePtr modList;
83 * The last error from one of the dl* routines is kept in static
84 * variables here. Each error is returned only once to the caller.
86 static char errbuf[BUFSIZ];
89 static void caterr(char *);
90 static int readExports(ModulePtr);
91 static void terminate(void);
92 static void *findMain(void);
94 static char *strerror_failed = "(strerror failed)";
95 static char *strerror_r_failed = "(strerror_r failed)";
97 char *strerrorcat(char *str, int err) {
98 int strsiz = strlen(str);
103 char *buf = malloc(BUFSIZ);
107 if (strerror_r(err, buf, sizeof(buf)) == 0)
110 msg = strerror_r_failed;
111 msgsiz = strlen(msg);
112 if (strsiz + msgsiz < BUFSIZ)
116 if ((msg = strerror(err)) == 0)
117 msg = strerror_failed;
118 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
119 if (strsiz + msgsiz < BUFSIZ) /* Do not move this after #endif. */
126 char *strerrorcpy(char *str, int err) {
131 char *buf = malloc(BUFSIZ);
135 if (strerror_r(err, buf, sizeof(buf)) == 0)
138 msg = strerror_r_failed;
139 msgsiz = strlen(msg);
144 if ((msg = strerror(err)) == 0)
145 msg = strerror_failed;
146 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
147 if (msgsiz < BUFSIZ) /* Do not move this after #endif. */
155 void *dlopen(char *path, int mode)
157 register ModulePtr mp;
158 static void *mainModule;
161 * Upon the first call register a terminate handler that will
162 * close all libraries. Also get a reference to the main module
163 * for use with loadbind.
166 if ((mainModule = findMain()) == NULL)
171 * Scan the list of modules if have the module already loaded.
173 for (mp = modList; mp; mp = mp->next)
174 if (strcmp(mp->name, path) == 0) {
178 Newz(1000,mp,1,Module);
181 strcpy(errbuf, "Newz: ");
182 strerrorcat(errbuf, errno);
186 if ((mp->name = savepv(path)) == NULL) {
188 strcpy(errbuf, "savepv: ");
189 strerrorcat(errbuf, errno);
194 * load should be declared load(const char *...). Thus we
195 * cast the path to a normal char *. Ugly.
197 if ((mp->entry = (void *)load((char *)path, L_NOAUTODEFER, NULL)) == NULL) {
201 strcpy(errbuf, "dlopen: ");
202 strcat(errbuf, path);
203 strcat(errbuf, ": ");
205 * If AIX says the file is not executable, the error
206 * can be further described by querying the loader about
209 if (errno == ENOEXEC) {
210 char *tmp[BUFSIZ/sizeof(char *)];
211 if (loadquery(L_GETMESSAGES, tmp, sizeof(tmp)) == -1)
212 strerrorcpy(errbuf, errno);
215 for (p = tmp; *p; p++)
219 strerrorcat(errbuf, errno);
225 if (loadbind(0, mainModule, mp->entry) == -1) {
228 strcpy(errbuf, "loadbind: ");
229 strerrorcat(errbuf, errno);
232 if (readExports(mp) == -1) {
240 * Attempt to decipher an AIX loader error message and append it
241 * to our static error message buffer.
243 static void caterr(char *s)
245 register char *p = s;
247 while (*p >= '0' && *p <= '9')
250 case L_ERROR_TOOMANY:
251 strcat(errbuf, "to many errors");
254 strcat(errbuf, "can't load library");
258 strcat(errbuf, "can't find symbol");
262 strcat(errbuf, "bad RLD");
266 strcat(errbuf, "bad exec format in");
270 strerrorcat(errbuf, atoi(++p));
278 void *dlsym(void *handle, const char *symbol)
280 register ModulePtr mp = (ModulePtr)handle;
281 register ExportPtr ep;
285 * Could speed up search, but I assume that one assigns
286 * the result to function pointers anyways.
288 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
289 if (strcmp(ep->name, symbol) == 0)
292 strcpy(errbuf, "dlsym: undefined symbol ");
293 strcat(errbuf, symbol);
306 int dlclose(void *handle)
308 register ModulePtr mp = (ModulePtr)handle;
310 register ModulePtr mp1;
312 if (--mp->refCnt > 0)
314 result = unload(mp->entry);
317 strerrorcpy(errbuf, errno);
320 register ExportPtr ep;
322 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
325 safefree(mp->exports);
330 for (mp1 = modList; mp1; mp1 = mp1->next)
331 if (mp1->next == mp) {
332 mp1->next = mp->next;
341 static void terminate(void)
347 /* Added by Wayne Scott
348 * This is needed because the ldopen system call calls
349 * calloc to allocated a block of date. The ldclose call calls free.
350 * Without this we get this system calloc and perl's free, resulting
351 * in a "Bad free" message. This way we always use perl's malloc.
353 void *calloc(size_t ne, size_t sz)
357 out = (void *) safemalloc(ne*sz);
363 * Build the export table from the XCOFF .loader section.
365 static int readExports(ModulePtr mp)
375 if ((ldp = ldopen(mp->name, ldp)) == NULL) {
379 if (errno != ENOENT) {
381 strcpy(errbuf, "readExports: ");
382 strerrorcat(errbuf, errno);
386 * The module might be loaded due to the LIBPATH
387 * environment variable. Search for the loaded
388 * module using L_GETINFO.
390 if ((buf = safemalloc(size)) == NULL) {
392 strcpy(errbuf, "readExports: ");
393 strerrorcat(errbuf, errno);
396 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
399 if ((buf = safemalloc(size)) == NULL) {
401 strcpy(errbuf, "readExports: ");
402 strerrorcat(errbuf, errno);
408 strcpy(errbuf, "readExports: ");
409 strerrorcat(errbuf, errno);
414 * Traverse the list of loaded modules. The entry point
415 * returned by load() does actually point to the data
418 lp = (struct ld_info *)buf;
420 if (lp->ldinfo_dataorg == mp->entry) {
421 ldp = ldopen(lp->ldinfo_filename, ldp);
424 if (lp->ldinfo_next == 0)
427 lp = (struct ld_info *)((char *)lp + lp->ldinfo_next);
432 strcpy(errbuf, "readExports: ");
433 strerrorcat(errbuf, errno);
437 if (TYPE(ldp) != U802TOCMAGIC) {
439 strcpy(errbuf, "readExports: bad magic");
440 while(ldclose(ldp) == FAILURE)
444 if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
446 strcpy(errbuf, "readExports: cannot read loader section header");
447 while(ldclose(ldp) == FAILURE)
452 * We read the complete loader section in one chunk, this makes
453 * finding long symbol names residing in the string table easier.
455 if ((ldbuf = (char *)safemalloc(sh.s_size)) == NULL) {
457 strcpy(errbuf, "readExports: ");
458 strerrorcat(errbuf, errno);
459 while(ldclose(ldp) == FAILURE)
463 if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
465 strcpy(errbuf, "readExports: cannot seek to loader section");
467 while(ldclose(ldp) == FAILURE)
471 /* This first case is a hack, since it assumes that the 3rd parameter to
472 FREAD is 1. See the redefinition of FREAD above to see how this works. */
474 if (FREAD(ldbuf, sh.s_size, 1, ldp) != sh.s_size) {
476 if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
479 strcpy(errbuf, "readExports: cannot read loader section");
481 while(ldclose(ldp) == FAILURE)
485 lhp = (LDHDR *)ldbuf;
486 ls = (LDSYM *)(ldbuf+LDHDRSZ);
488 * Count the number of exports to include in our export table.
490 for (i = lhp->l_nsyms; i; i--, ls++) {
491 if (!LDR_EXPORT(*ls))
495 Newz(1001, mp->exports, mp->nExports, Export);
496 if (mp->exports == NULL) {
498 strcpy(errbuf, "readExports: ");
499 strerrorcat(errbuf, errno);
501 while(ldclose(ldp) == FAILURE)
506 * Fill in the export table. All entries are relative to
507 * the entry point we got from load.
510 ls = (LDSYM *)(ldbuf+LDHDRSZ);
511 for (i = lhp->l_nsyms; i; i--, ls++) {
513 if (!LDR_EXPORT(*ls))
515 if (ls->l_zeroes == 0)
516 symname = ls->l_offset+lhp->l_stoff+ldbuf;
518 symname = ls->l_name;
519 ep->name = savepv(symname);
520 ep->addr = (void *)((unsigned long)mp->entry + ls->l_value);
524 while(ldclose(ldp) == FAILURE)
530 * Find the main modules entry point. This is used as export pointer
531 * for loadbind() to be able to resolve references to the main part.
533 static void * findMain(void)
541 if ((buf = safemalloc(size)) == NULL) {
543 strcpy(errbuf, "findMain: ");
544 strerrorcat(errbuf, errno);
547 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
550 if ((buf = safemalloc(size)) == NULL) {
552 strcpy(errbuf, "findMain: ");
553 strerrorcat(errbuf, errno);
559 strcpy(errbuf, "findMain: ");
560 strerrorcat(errbuf, errno);
565 * The first entry is the main module. The entry point
566 * returned by load() does actually point to the data
569 lp = (struct ld_info *)buf;
570 ret = lp->ldinfo_dataorg;
577 * Platform: SunOS/Solaris, possibly others which use dlopen.
578 * Author: Paul Marquess (pmarquess@bfsec.bt.co.uk)
579 * Created: 10th July 1994
582 * 15th July 1994 - Added code to explicitly save any error messages.
583 * 3rd August 1994 - Upgraded to v3 spec.
584 * 9th August 1994 - Changed to use IV
585 * 10th August 1994 - Tim Bunce: Added RTLD_LAZY, switchable debugging,
586 * basic FreeBSD support, removed ClearError
596 #include "dlutils.c" /* SaveError() etc */
602 (void)dl_generic_private_init();
605 MODULE = DynaLoader PACKAGE = DynaLoader
608 (void)dl_private_init();
612 dl_load_file(filename, flags=0)
616 DLDEBUG(1,PerlIO_printf(PerlIO_stderr(), "dl_load_file(%s,%x):\n", filename,flags));
618 warn("Can't make loaded symbols global on this platform while loading %s",filename);
619 RETVAL = dlopen(filename, 1) ;
620 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), " libref=%x\n", RETVAL));
621 ST(0) = sv_newmortal() ;
623 SaveError("%s",dlerror()) ;
625 sv_setiv( ST(0), (IV)RETVAL);
629 dl_find_symbol(libhandle, symbolname)
633 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_find_symbol(handle=%x, symbol=%s)\n",
634 libhandle, symbolname));
635 RETVAL = dlsym(libhandle, symbolname);
636 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), " symbolref = %x\n", RETVAL));
637 ST(0) = sv_newmortal() ;
639 SaveError("%s",dlerror()) ;
641 sv_setiv( ST(0), (IV)RETVAL);
650 # These functions should not need changing on any platform:
653 dl_install_xsub(perl_name, symref, filename="$Package")
658 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_install_xsub(name=%s, symref=%x)\n",
660 ST(0)=sv_2mortal(newRV((SV*)newXS(perl_name, (void(*)())symref, filename)));