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
23 /* When building as a 64-bit binary on AIX, define this to get the
24 * correct structure definitions. Also determines the field-name
25 * macros and gates some logic in readEntries(). -- Steven N. Hirsch
26 * <hirschs@btv.ibm.com> */
36 #include <sys/types.h>
44 # define AIX_SCNHDR SCNHDR_64
45 # define AIX_LDHDR LDHDR_64
46 # define AIX_LDSYM LDSYM_64
47 # define AIX_LDHDRSZ LDHDRSZ_64
49 # define AIX_SCNHDR SCNHDR
50 # define AIX_LDHDR LDHDR
51 # define AIX_LDSYM LDSYM
52 # define AIX_LDHDRSZ LDHDRSZ
55 /* When using Perl extensions written in C++ the longer versions
56 * of load() and unload() from libC and libC_r need to be used,
57 * otherwise statics in the extensions won't get initialized right.
58 * -- Stephanie Beals <bealzy@us.ibm.com> */
60 /* Older AIX C compilers cannot deal with C++ double-slash comments in
61 the ibmcxx and/or xlC includes. Since we only need a single file,
62 be more fine-grained about what's included <hirschs@btv.ibm.com> */
63 #ifdef USE_libC /* The define comes, when it comes, from hints/aix.pl. */
64 # define LOAD loadAndInit
65 # define UNLOAD terminateAndUnload
66 # if defined(USE_xlC_load_h)
67 # include "/usr/lpp/xlC/include/load.h"
68 # elif defined(USE_ibmcxx_load_h)
69 # include "/usr/ibmcxx/include/load.h"
73 # define UNLOAD unload
77 * AIX 4.3 does remove some useful definitions from ldfcn.h. Define
78 * these here to compensate for that lossage.
81 # define BEGINNING SEEK_SET
84 # define FSEEK(ldptr,o,p) fseek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr) +o):o,p)
87 # define FREAD(p,s,n,ldptr) fread(p,s,n,IOPTR(ldptr))
90 /* If using PerlIO, redefine these macros from <ldfcn.h> */
94 #define FSEEK(ldptr,o,p) PerlIO_seek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr)+o):o,p)
95 #define FREAD(p,s,n,ldptr) PerlIO_read(IOPTR(ldptr),p,s*n)
99 * We simulate dlopen() et al. through a call to load. Because AIX has
100 * no call to find an exported symbol we read the loader section of the
101 * loaded module and build a list of exported symbols and their virtual
106 char *name; /* the symbols's name */
107 void *addr; /* its relocated virtual address */
108 } Export, *ExportPtr;
111 * The void * handle returned from dlopen is actually a ModulePtr.
113 typedef struct Module {
115 char *name; /* module name for refcounting */
116 int refCnt; /* the number of references */
117 void *entry; /* entry point from load */
118 int nExports; /* the number of exports found */
119 ExportPtr exports; /* the array of exports */
120 } Module, *ModulePtr;
123 * We keep a list of all loaded modules to be able to reference count
124 * duplicate dlopen's.
126 static ModulePtr modList; /* XXX threaded */
129 * The last error from one of the dl* routines is kept in static
130 * variables here. Each error is returned only once to the caller.
132 static char errbuf[BUFSIZ]; /* XXX threaded */
133 static int errvalid; /* XXX threaded */
135 static void caterr(char *);
136 static int readExports(ModulePtr);
137 static void *findMain(void);
139 static char *strerror_failed = "(strerror failed)";
140 static char *strerror_r_failed = "(strerror_r failed)";
142 char *strerrorcat(char *str, int err) {
143 int strsiz = strlen(str);
148 char *buf = malloc(BUFSIZ);
152 if (strerror_r(err, buf, BUFSIZ) == 0)
155 msg = strerror_r_failed;
156 msgsiz = strlen(msg);
157 if (strsiz + msgsiz < BUFSIZ)
161 if ((msg = strerror(err)) == 0)
162 msg = strerror_failed;
163 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
164 if (strsiz + msgsiz < BUFSIZ) /* Do not move this after #endif. */
171 char *strerrorcpy(char *str, int err) {
176 char *buf = malloc(BUFSIZ);
180 if (strerror_r(err, buf, BUFSIZ) == 0)
183 msg = strerror_r_failed;
184 msgsiz = strlen(msg);
189 if ((msg = strerror(err)) == 0)
190 msg = strerror_failed;
191 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
192 if (msgsiz < BUFSIZ) /* Do not move this after #endif. */
200 void *dlopen(char *path, int mode)
203 register ModulePtr mp;
204 static void *mainModule; /* XXX threaded */
207 * Upon the first call register a terminate handler that will
208 * close all libraries.
210 if (mainModule == NULL) {
211 if ((mainModule = findMain()) == NULL)
215 * Scan the list of modules if have the module already loaded.
217 for (mp = modList; mp; mp = mp->next)
218 if (strcmp(mp->name, path) == 0) {
222 Newz(1000,mp,1,Module);
225 strcpy(errbuf, "Newz: ");
226 strerrorcat(errbuf, errno);
230 if ((mp->name = savepv(path)) == NULL) {
232 strcpy(errbuf, "savepv: ");
233 strerrorcat(errbuf, errno);
239 * load should be declared load(const char *...). Thus we
240 * cast the path to a normal char *. Ugly.
242 if ((mp->entry = (void *)LOAD((char *)path,
243 #ifdef L_LIBPATH_EXEC
248 int saverrno = errno;
253 strcpy(errbuf, "dlopen: ");
254 strcat(errbuf, path);
255 strcat(errbuf, ": ");
257 * If AIX says the file is not executable, the error
258 * can be further described by querying the loader about
261 if (saverrno == ENOEXEC) {
262 char *moreinfo[BUFSIZ/sizeof(char *)];
263 if (loadquery(L_GETMESSAGES, moreinfo, sizeof(moreinfo)) == -1)
264 strerrorcpy(errbuf, saverrno);
267 for (p = moreinfo; *p; p++)
271 strerrorcat(errbuf, saverrno);
278 * Assume anonymous exports come from the module this dlopen
279 * is linked into, that holds true as long as dlopen and all
280 * of the perl core are in the same shared object. Also bind
281 * against the main part, in the case a perl is not the main
282 * part, e.g mod_perl as DSO in Apache so perl modules can
283 * also reference Apache symbols.
285 if (loadbind(0, (void *)dlopen, mp->entry) == -1 ||
286 loadbind(0, mainModule, mp->entry)) {
287 int saverrno = errno;
291 strcpy(errbuf, "loadbind: ");
292 strerrorcat(errbuf, saverrno);
295 if (readExports(mp) == -1) {
303 * Attempt to decipher an AIX loader error message and append it
304 * to our static error message buffer.
306 static void caterr(char *s)
308 register char *p = s;
310 while (*p >= '0' && *p <= '9')
313 case L_ERROR_TOOMANY:
314 strcat(errbuf, "to many errors");
317 strcat(errbuf, "can't load library");
321 strcat(errbuf, "can't find symbol");
325 strcat(errbuf, "bad RLD");
329 strcat(errbuf, "bad exec format in");
333 strerrorcat(errbuf, atoi(++p));
341 void *dlsym(void *handle, const char *symbol)
343 register ModulePtr mp = (ModulePtr)handle;
344 register ExportPtr ep;
348 * Could speed up search, but I assume that one assigns
349 * the result to function pointers anyways.
351 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
352 if (strcmp(ep->name, symbol) == 0)
355 strcpy(errbuf, "dlsym: undefined symbol ");
356 strcat(errbuf, symbol);
369 int dlclose(void *handle)
371 register ModulePtr mp = (ModulePtr)handle;
373 register ModulePtr mp1;
375 if (--mp->refCnt > 0)
377 result = UNLOAD(mp->entry);
380 strerrorcpy(errbuf, errno);
383 register ExportPtr ep;
385 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
388 safefree(mp->exports);
393 for (mp1 = modList; mp1; mp1 = mp1->next)
394 if (mp1->next == mp) {
395 mp1->next = mp->next;
404 /* Added by Wayne Scott
405 * This is needed because the ldopen system call calls
406 * calloc to allocated a block of date. The ldclose call calls free.
407 * Without this we get this system calloc and perl's free, resulting
408 * in a "Bad free" message. This way we always use perl's malloc.
410 void *calloc(size_t ne, size_t sz)
414 out = (void *) safemalloc(ne*sz);
420 * Build the export table from the XCOFF .loader section.
422 static int readExports(ModulePtr mp)
433 if ((ldp = ldopen(mp->name, ldp)) == NULL) {
437 if (errno != ENOENT) {
439 strcpy(errbuf, "readExports: ");
440 strerrorcat(errbuf, errno);
444 * The module might be loaded due to the LIBPATH
445 * environment variable. Search for the loaded
446 * module using L_GETINFO.
448 if ((buf = safemalloc(size)) == NULL) {
450 strcpy(errbuf, "readExports: ");
451 strerrorcat(errbuf, errno);
454 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
457 if ((buf = safemalloc(size)) == NULL) {
459 strcpy(errbuf, "readExports: ");
460 strerrorcat(errbuf, errno);
466 strcpy(errbuf, "readExports: ");
467 strerrorcat(errbuf, errno);
472 * Traverse the list of loaded modules. The entry point
473 * returned by LOAD() does actually point to the data
476 lp = (struct ld_info *)buf;
478 if (lp->ldinfo_dataorg == mp->entry) {
479 ldp = ldopen(lp->ldinfo_filename, ldp);
482 if (lp->ldinfo_next == 0)
485 lp = (struct ld_info *)((char *)lp + lp->ldinfo_next);
490 strcpy(errbuf, "readExports: ");
491 strerrorcat(errbuf, errno);
495 #ifdef USE_64_BIT_ALL
496 if (TYPE(ldp) != U803XTOCMAGIC) {
498 if (TYPE(ldp) != U802TOCMAGIC) {
501 strcpy(errbuf, "readExports: bad magic");
502 while(ldclose(ldp) == FAILURE)
506 if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
508 strcpy(errbuf, "readExports: cannot read loader section header");
509 while(ldclose(ldp) == FAILURE)
514 * We read the complete loader section in one chunk, this makes
515 * finding long symbol names residing in the string table easier.
517 if ((ldbuf = (char *)safemalloc(sh.s_size)) == NULL) {
519 strcpy(errbuf, "readExports: ");
520 strerrorcat(errbuf, errno);
521 while(ldclose(ldp) == FAILURE)
525 if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
527 strcpy(errbuf, "readExports: cannot seek to loader section");
529 while(ldclose(ldp) == FAILURE)
533 /* This first case is a hack, since it assumes that the 3rd parameter to
534 FREAD is 1. See the redefinition of FREAD above to see how this works. */
536 if (FREAD(ldbuf, sh.s_size, 1, ldp) != sh.s_size) {
538 if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
541 strcpy(errbuf, "readExports: cannot read loader section");
543 while(ldclose(ldp) == FAILURE)
547 lhp = (AIX_LDHDR *)ldbuf;
548 ls = (AIX_LDSYM *)(ldbuf+AIX_LDHDRSZ);
550 * Count the number of exports to include in our export table.
552 for (i = lhp->l_nsyms; i; i--, ls++) {
553 if (!LDR_EXPORT(*ls))
557 Newz(1001, mp->exports, mp->nExports, Export);
558 if (mp->exports == NULL) {
560 strcpy(errbuf, "readExports: ");
561 strerrorcat(errbuf, errno);
563 while(ldclose(ldp) == FAILURE)
568 * Fill in the export table. All entries are relative to
569 * the entry point we got from load.
572 ls = (AIX_LDSYM *)(ldbuf+AIX_LDHDRSZ);
573 for (i = lhp->l_nsyms; i; i--, ls++) {
575 if (!LDR_EXPORT(*ls))
577 #ifndef USE_64_BIT_ALL
578 if (ls->l_zeroes == 0)
580 symname = ls->l_offset+lhp->l_stoff+ldbuf;
581 #ifndef USE_64_BIT_ALL
583 symname = ls->l_name;
585 ep->name = savepv(symname);
586 ep->addr = (void *)((unsigned long)mp->entry + ls->l_value);
590 while(ldclose(ldp) == FAILURE)
596 * Find the main modules entry point. This is used as export pointer
597 * for loadbind() to be able to resolve references to the main part.
599 static void * findMain(void)
607 if ((buf = safemalloc(size)) == NULL) {
609 strcpy(errbuf, "findMain: ");
610 strerrorcat(errbuf, errno);
613 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
616 if ((buf = safemalloc(size)) == NULL) {
618 strcpy(errbuf, "findMain: ");
619 strerrorcat(errbuf, errno);
625 strcpy(errbuf, "findMain: ");
626 strerrorcat(errbuf, errno);
631 * The first entry is the main module. The entry point
632 * returned by load() does actually point to the data
635 lp = (struct ld_info *)buf;
636 ret = lp->ldinfo_dataorg;
643 * Platform: SunOS/Solaris, possibly others which use dlopen.
644 * Author: Paul Marquess (Paul.Marquess@btinternet.com)
645 * Created: 10th July 1994
648 * 15th July 1994 - Added code to explicitly save any error messages.
649 * 3rd August 1994 - Upgraded to v3 spec.
650 * 9th August 1994 - Changed to use IV
651 * 10th August 1994 - Tim Bunce: Added RTLD_LAZY, switchable debugging,
652 * basic FreeBSD support, removed ClearError
662 #include "dlutils.c" /* SaveError() etc */
666 dl_private_init(pTHX)
668 (void)dl_generic_private_init(aTHX);
671 MODULE = DynaLoader PACKAGE = DynaLoader
674 (void)dl_private_init(aTHX);
678 dl_load_file(filename, flags=0)
682 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags));
684 Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
685 RETVAL = dlopen(filename, 1) ;
686 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%x\n", RETVAL));
687 ST(0) = sv_newmortal() ;
689 SaveError(aTHX_ "%s",dlerror()) ;
691 sv_setiv( ST(0), PTR2IV(RETVAL) );
694 dl_unload_file(libref)
697 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_unload_file(%lx):\n", libref));
698 RETVAL = (dlclose(libref) == 0 ? 1 : 0);
700 SaveError(aTHX_ "%s", dlerror()) ;
701 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " retval = %d\n", RETVAL));
706 dl_find_symbol(libhandle, symbolname)
710 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_find_symbol(handle=%x, symbol=%s)\n",
711 libhandle, symbolname));
712 RETVAL = dlsym(libhandle, symbolname);
713 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " symbolref = %x\n", RETVAL));
714 ST(0) = sv_newmortal() ;
716 SaveError(aTHX_ "%s",dlerror()) ;
718 sv_setiv( ST(0), PTR2IV(RETVAL));
727 # These functions should not need changing on any platform:
730 dl_install_xsub(perl_name, symref, filename="$Package")
735 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n",
737 ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
738 (void(*)(pTHX_ CV *))symref,