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; /* XXX threaded */
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]; /* XXX threaded */
87 static int errvalid; /* XXX threaded */
89 static void caterr(char *);
90 static int readExports(ModulePtr);
91 static void terminate(void);
93 static char *strerror_failed = "(strerror failed)";
94 static char *strerror_r_failed = "(strerror_r failed)";
96 char *strerrorcat(char *str, int err) {
97 int strsiz = strlen(str);
102 char *buf = malloc(BUFSIZ);
106 if (strerror_r(err, buf, BUFSIZ) == 0)
109 msg = strerror_r_failed;
110 msgsiz = strlen(msg);
111 if (strsiz + msgsiz < BUFSIZ)
115 if ((msg = strerror(err)) == 0)
116 msg = strerror_failed;
117 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
118 if (strsiz + msgsiz < BUFSIZ) /* Do not move this after #endif. */
125 char *strerrorcpy(char *str, int err) {
130 char *buf = malloc(BUFSIZ);
134 if (strerror_r(err, buf, BUFSIZ) == 0)
137 msg = strerror_r_failed;
138 msgsiz = strlen(msg);
143 if ((msg = strerror(err)) == 0)
144 msg = strerror_failed;
145 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
146 if (msgsiz < BUFSIZ) /* Do not move this after #endif. */
154 void *dlopen(char *path, int mode)
157 register ModulePtr mp;
158 static int inited; /* XXX threaded */
161 * Upon the first call register a terminate handler that will
162 * close all libraries.
169 * Scan the list of modules if have the module already loaded.
171 for (mp = modList; mp; mp = mp->next)
172 if (strcmp(mp->name, path) == 0) {
176 Newz(1000,mp,1,Module);
179 strcpy(errbuf, "Newz: ");
180 strerrorcat(errbuf, errno);
184 if ((mp->name = savepv(path)) == NULL) {
186 strcpy(errbuf, "savepv: ");
187 strerrorcat(errbuf, errno);
193 * load should be declared load(const char *...). Thus we
194 * cast the path to a normal char *. Ugly.
196 if ((mp->entry = (void *)load((char *)path,
197 #ifdef L_LIBPATH_EXEC
202 int saverrno = errno;
207 strcpy(errbuf, "dlopen: ");
208 strcat(errbuf, path);
209 strcat(errbuf, ": ");
211 * If AIX says the file is not executable, the error
212 * can be further described by querying the loader about
215 if (saverrno == ENOEXEC) {
216 char *moreinfo[BUFSIZ/sizeof(char *)];
217 if (loadquery(L_GETMESSAGES, moreinfo, sizeof(moreinfo)) == -1)
218 strerrorcpy(errbuf, saverrno);
221 for (p = moreinfo; *p; p++)
225 strerrorcat(errbuf, saverrno);
232 * Assume anonymous exports come from the module this dlopen
233 * is linked into, that holds true as long as dlopen and all
234 * of the perl core are in the same shared object.
236 if (loadbind(0, (void *)dlopen, mp->entry) == -1) {
237 int saverrno = errno;
241 strcpy(errbuf, "loadbind: ");
242 strerrorcat(errbuf, saverrno);
245 if (readExports(mp) == -1) {
253 * Attempt to decipher an AIX loader error message and append it
254 * to our static error message buffer.
256 static void caterr(char *s)
258 register char *p = s;
260 while (*p >= '0' && *p <= '9')
263 case L_ERROR_TOOMANY:
264 strcat(errbuf, "to many errors");
267 strcat(errbuf, "can't load library");
271 strcat(errbuf, "can't find symbol");
275 strcat(errbuf, "bad RLD");
279 strcat(errbuf, "bad exec format in");
283 strerrorcat(errbuf, atoi(++p));
291 void *dlsym(void *handle, const char *symbol)
293 register ModulePtr mp = (ModulePtr)handle;
294 register ExportPtr ep;
298 * Could speed up search, but I assume that one assigns
299 * the result to function pointers anyways.
301 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
302 if (strcmp(ep->name, symbol) == 0)
305 strcpy(errbuf, "dlsym: undefined symbol ");
306 strcat(errbuf, symbol);
319 int dlclose(void *handle)
321 register ModulePtr mp = (ModulePtr)handle;
323 register ModulePtr mp1;
325 if (--mp->refCnt > 0)
327 result = unload(mp->entry);
330 strerrorcpy(errbuf, errno);
333 register ExportPtr ep;
335 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
338 safefree(mp->exports);
343 for (mp1 = modList; mp1; mp1 = mp1->next)
344 if (mp1->next == mp) {
345 mp1->next = mp->next;
354 static void terminate(void)
360 /* Added by Wayne Scott
361 * This is needed because the ldopen system call calls
362 * calloc to allocated a block of date. The ldclose call calls free.
363 * Without this we get this system calloc and perl's free, resulting
364 * in a "Bad free" message. This way we always use perl's malloc.
366 void *calloc(size_t ne, size_t sz)
370 out = (void *) safemalloc(ne*sz);
376 * Build the export table from the XCOFF .loader section.
378 static int readExports(ModulePtr mp)
389 if ((ldp = ldopen(mp->name, ldp)) == NULL) {
393 if (errno != ENOENT) {
395 strcpy(errbuf, "readExports: ");
396 strerrorcat(errbuf, errno);
400 * The module might be loaded due to the LIBPATH
401 * environment variable. Search for the loaded
402 * module using L_GETINFO.
404 if ((buf = safemalloc(size)) == NULL) {
406 strcpy(errbuf, "readExports: ");
407 strerrorcat(errbuf, errno);
410 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
413 if ((buf = safemalloc(size)) == NULL) {
415 strcpy(errbuf, "readExports: ");
416 strerrorcat(errbuf, errno);
422 strcpy(errbuf, "readExports: ");
423 strerrorcat(errbuf, errno);
428 * Traverse the list of loaded modules. The entry point
429 * returned by load() does actually point to the data
432 lp = (struct ld_info *)buf;
434 if (lp->ldinfo_dataorg == mp->entry) {
435 ldp = ldopen(lp->ldinfo_filename, ldp);
438 if (lp->ldinfo_next == 0)
441 lp = (struct ld_info *)((char *)lp + lp->ldinfo_next);
446 strcpy(errbuf, "readExports: ");
447 strerrorcat(errbuf, errno);
451 if (TYPE(ldp) != U802TOCMAGIC) {
453 strcpy(errbuf, "readExports: bad magic");
454 while(ldclose(ldp) == FAILURE)
458 if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
460 strcpy(errbuf, "readExports: cannot read loader section header");
461 while(ldclose(ldp) == FAILURE)
466 * We read the complete loader section in one chunk, this makes
467 * finding long symbol names residing in the string table easier.
469 if ((ldbuf = (char *)safemalloc(sh.s_size)) == NULL) {
471 strcpy(errbuf, "readExports: ");
472 strerrorcat(errbuf, errno);
473 while(ldclose(ldp) == FAILURE)
477 if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
479 strcpy(errbuf, "readExports: cannot seek to loader section");
481 while(ldclose(ldp) == FAILURE)
485 /* This first case is a hack, since it assumes that the 3rd parameter to
486 FREAD is 1. See the redefinition of FREAD above to see how this works. */
488 if (FREAD(ldbuf, sh.s_size, 1, ldp) != sh.s_size) {
490 if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
493 strcpy(errbuf, "readExports: cannot read loader section");
495 while(ldclose(ldp) == FAILURE)
499 lhp = (LDHDR *)ldbuf;
500 ls = (LDSYM *)(ldbuf+LDHDRSZ);
502 * Count the number of exports to include in our export table.
504 for (i = lhp->l_nsyms; i; i--, ls++) {
505 if (!LDR_EXPORT(*ls))
509 Newz(1001, mp->exports, mp->nExports, Export);
510 if (mp->exports == NULL) {
512 strcpy(errbuf, "readExports: ");
513 strerrorcat(errbuf, errno);
515 while(ldclose(ldp) == FAILURE)
520 * Fill in the export table. All entries are relative to
521 * the entry point we got from load.
524 ls = (LDSYM *)(ldbuf+LDHDRSZ);
525 for (i = lhp->l_nsyms; i; i--, ls++) {
527 if (!LDR_EXPORT(*ls))
529 if (ls->l_zeroes == 0)
530 symname = ls->l_offset+lhp->l_stoff+ldbuf;
532 symname = ls->l_name;
533 ep->name = savepv(symname);
534 ep->addr = (void *)((unsigned long)mp->entry + ls->l_value);
538 while(ldclose(ldp) == FAILURE)
545 * Platform: SunOS/Solaris, possibly others which use dlopen.
546 * Author: Paul Marquess (pmarquess@bfsec.bt.co.uk)
547 * Created: 10th July 1994
550 * 15th July 1994 - Added code to explicitly save any error messages.
551 * 3rd August 1994 - Upgraded to v3 spec.
552 * 9th August 1994 - Changed to use IV
553 * 10th August 1994 - Tim Bunce: Added RTLD_LAZY, switchable debugging,
554 * basic FreeBSD support, removed ClearError
564 #include "dlutils.c" /* SaveError() etc */
568 dl_private_init(pTHX)
570 (void)dl_generic_private_init(aTHX);
573 MODULE = DynaLoader PACKAGE = DynaLoader
576 (void)dl_private_init(aTHX);
580 dl_load_file(filename, flags=0)
584 DLDEBUG(1,PerlIO_printf(PerlIO_stderr(), "dl_load_file(%s,%x):\n", filename,flags));
586 Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
587 RETVAL = dlopen(filename, 1) ;
588 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), " libref=%x\n", RETVAL));
589 ST(0) = sv_newmortal() ;
591 SaveError(aTHX_ "%s",dlerror()) ;
593 sv_setiv( ST(0), (IV)RETVAL);
597 dl_find_symbol(libhandle, symbolname)
601 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_find_symbol(handle=%x, symbol=%s)\n",
602 libhandle, symbolname));
603 RETVAL = dlsym(libhandle, symbolname);
604 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), " symbolref = %x\n", RETVAL));
605 ST(0) = sv_newmortal() ;
607 SaveError(aTHX_ "%s",dlerror()) ;
609 sv_setiv( ST(0), (IV)RETVAL);
618 # These functions should not need changing on any platform:
621 dl_install_xsub(perl_name, symref, filename="$Package")
626 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_install_xsub(name=%s, symref=%x)\n",
628 ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
629 (void(*)(pTHX_ CV *))symref,