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.
14 #define PERLIO_NOT_STDIO 0
17 * On AIX 4.3 and above the emulation layer is not needed any more, and
18 * indeed if perl uses its emulation and perl is linked into apache
19 * which is supposed to use the native dlopen conflicts arise.
20 * Jens-Uwe Mager jum@helios.de
22 #ifdef USE_NATIVE_DLOPEN
29 #include "dlutils.c" /* SaveError() etc */
34 * @(#)dlfcn.c 1.5 revision of 93/02/14 20:14:17
35 * This is an unpublished work copyright (c) 1992 Helios Software GmbH
36 * 3000 Hannover 1, Germany
42 /* When building as a 64-bit binary on AIX, define this to get the
43 * correct structure definitions. Also determines the field-name
44 * macros and gates some logic in readEntries(). -- Steven N. Hirsch
45 * <hirschs@btv.ibm.com> */
55 #include <sys/types.h>
63 # define AIX_SCNHDR SCNHDR_64
64 # define AIX_LDHDR LDHDR_64
65 # define AIX_LDSYM LDSYM_64
66 # define AIX_LDHDRSZ LDHDRSZ_64
68 # define AIX_SCNHDR SCNHDR
69 # define AIX_LDHDR LDHDR
70 # define AIX_LDSYM LDSYM
71 # define AIX_LDHDRSZ LDHDRSZ
74 /* When using Perl extensions written in C++ the longer versions
75 * of load() and unload() from libC and libC_r need to be used,
76 * otherwise statics in the extensions won't get initialized right.
77 * -- Stephanie Beals <bealzy@us.ibm.com> */
79 /* Older AIX C compilers cannot deal with C++ double-slash comments in
80 the ibmcxx and/or xlC includes. Since we only need a single file,
81 be more fine-grained about what's included <hirschs@btv.ibm.com> */
83 #ifdef USE_libC /* The define comes, when it comes, from hints/aix.pl. */
84 # define LOAD loadAndInit
85 # define UNLOAD terminateAndUnload
86 # if defined(USE_vacpp_load_h)
87 # include "/usr/vacpp/include/load.h"
88 # elif defined(USE_ibmcxx_load_h)
89 # include "/usr/ibmcxx/include/load.h"
90 # elif defined(USE_xlC_load_h)
91 # include "/usr/lpp/xlC/include/load.h"
92 # elif defined(USE_load_h)
93 # include "/usr/include/load.h"
97 # define UNLOAD unload
101 * AIX 4.3 does remove some useful definitions from ldfcn.h. Define
102 * these here to compensate for that lossage.
105 # define BEGINNING SEEK_SET
108 # define FSEEK(ldptr,o,p) fseek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr) +o):o,p)
111 # define FREAD(p,s,n,ldptr) fread(p,s,n,IOPTR(ldptr))
118 # define RTLD_GLOBAL 0
122 * We simulate dlopen() et al. through a call to load. Because AIX has
123 * no call to find an exported symbol we read the loader section of the
124 * loaded module and build a list of exported symbols and their virtual
129 char *name; /* the symbols's name */
130 void *addr; /* its relocated virtual address */
131 } Export, *ExportPtr;
134 * The void * handle returned from dlopen is actually a ModulePtr.
136 typedef struct Module {
138 char *name; /* module name for refcounting */
139 int refCnt; /* the number of references */
140 void *entry; /* entry point from load */
141 int nExports; /* the number of exports found */
142 ExportPtr exports; /* the array of exports */
143 } Module, *ModulePtr;
147 * We keep a list of all loaded modules to be able to reference count
148 * duplicate dlopen's.
153 * The last error from one of the dl* routines is kept in static
154 * variables here. Each error is returned only once to the caller.
156 char x_errbuf[BUFSIZ];
159 } my_cxtx_t; /* this *must* be named my_cxtx_t */
161 #define DL_CXT_EXTRA /* ask for dl_cxtx to be defined in dlutils.c */
162 #include "dlutils.c" /* SaveError() etc */
164 #define dl_modList (dl_cxtx.x_modList)
165 #define dl_errbuf (dl_cxtx.x_errbuf)
166 #define dl_errvalid (dl_cxtx.x_errvalid)
167 #define dl_mainModule (dl_cxtx.x_mainModule)
169 static void caterr(char *);
170 static int readExports(ModulePtr);
171 static void *findMain(void);
173 /* these statics are ok because they're constants */
174 static char *strerror_failed = "(strerror failed)";
175 static char *strerror_r_failed = "(strerror_r failed)";
177 char *strerrorcat(char *str, int err) {
178 int strsiz = strlen(str);
182 #ifdef USE_5005THREADS
183 char *buf = malloc(BUFSIZ);
187 if (strerror_r(err, buf, BUFSIZ) == 0)
190 msg = strerror_r_failed;
191 msgsiz = strlen(msg);
192 if (strsiz + msgsiz < BUFSIZ)
198 if ((msg = strerror(err)) == 0)
199 msg = strerror_failed;
200 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
201 if (strsiz + msgsiz < BUFSIZ) /* Do not move this after #endif. */
208 char *strerrorcpy(char *str, int err) {
212 #ifdef USE_5005THREADS
213 char *buf = malloc(BUFSIZ);
217 if (strerror_r(err, buf, BUFSIZ) == 0)
220 msg = strerror_r_failed;
221 msgsiz = strlen(msg);
228 if ((msg = strerror(err)) == 0)
229 msg = strerror_failed;
230 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
231 if (msgsiz < BUFSIZ) /* Do not move this after #endif. */
239 void *dlopen(char *path, int mode)
243 register ModulePtr mp;
246 * Upon the first call register a terminate handler that will
247 * close all libraries.
249 if (dl_mainModule == NULL) {
250 if ((dl_mainModule = findMain()) == NULL)
254 * Scan the list of modules if have the module already loaded.
256 for (mp = dl_modList; mp; mp = mp->next)
257 if (strcmp(mp->name, path) == 0) {
261 Newz(1000,mp,1,Module);
264 strcpy(dl_errbuf, "Newz: ");
265 strerrorcat(dl_errbuf, errno);
269 if ((mp->name = savepv(path)) == NULL) {
271 strcpy(dl_errbuf, "savepv: ");
272 strerrorcat(dl_errbuf, errno);
278 * load should be declared load(const char *...). Thus we
279 * cast the path to a normal char *. Ugly.
281 if ((mp->entry = (void *)LOAD((char *)path,
282 #ifdef L_LIBPATH_EXEC
287 int saverrno = errno;
292 strcpy(dl_errbuf, "dlopen: ");
293 strcat(dl_errbuf, path);
294 strcat(dl_errbuf, ": ");
296 * If AIX says the file is not executable, the error
297 * can be further described by querying the loader about
300 if (saverrno == ENOEXEC) {
301 char *moreinfo[BUFSIZ/sizeof(char *)];
302 if (loadquery(L_GETMESSAGES, moreinfo, sizeof(moreinfo)) == -1)
303 strerrorcpy(dl_errbuf, saverrno);
306 for (p = moreinfo; *p; p++)
310 strerrorcat(dl_errbuf, saverrno);
314 mp->next = dl_modList;
317 * Assume anonymous exports come from the module this dlopen
318 * is linked into, that holds true as long as dlopen and all
319 * of the perl core are in the same shared object. Also bind
320 * against the main part, in the case a perl is not the main
321 * part, e.g mod_perl as DSO in Apache so perl modules can
322 * also reference Apache symbols.
324 if (loadbind(0, (void *)dlopen, mp->entry) == -1 ||
325 loadbind(0, dl_mainModule, mp->entry)) {
326 int saverrno = errno;
330 strcpy(dl_errbuf, "loadbind: ");
331 strerrorcat(dl_errbuf, saverrno);
334 if (readExports(mp) == -1) {
342 * Attempt to decipher an AIX loader error message and append it
343 * to our static error message buffer.
345 static void caterr(char *s)
349 register char *p = s;
351 while (*p >= '0' && *p <= '9')
354 case L_ERROR_TOOMANY:
355 strcat(dl_errbuf, "too many errors");
358 strcat(dl_errbuf, "can't load library");
359 strcat(dl_errbuf, p);
362 strcat(dl_errbuf, "can't find symbol");
363 strcat(dl_errbuf, p);
366 strcat(dl_errbuf, "bad RLD");
367 strcat(dl_errbuf, p);
370 strcat(dl_errbuf, "bad exec format in");
371 strcat(dl_errbuf, p);
374 strerrorcat(dl_errbuf, atoi(++p));
377 strcat(dl_errbuf, s);
382 void *dlsym(void *handle, const char *symbol)
386 register ModulePtr mp = (ModulePtr)handle;
387 register ExportPtr ep;
391 * Could speed up search, but I assume that one assigns
392 * the result to function pointers anyways.
394 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
395 if (strcmp(ep->name, symbol) == 0)
398 strcpy(dl_errbuf, "dlsym: undefined symbol ");
399 strcat(dl_errbuf, symbol);
414 int dlclose(void *handle)
418 register ModulePtr mp = (ModulePtr)handle;
420 register ModulePtr mp1;
422 if (--mp->refCnt > 0)
424 result = UNLOAD(mp->entry);
427 strerrorcpy(dl_errbuf, errno);
430 register ExportPtr ep;
432 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
435 safefree(mp->exports);
437 if (mp == dl_modList)
438 dl_modList = mp->next;
440 for (mp1 = dl_modList; mp1; mp1 = mp1->next)
441 if (mp1->next == mp) {
442 mp1->next = mp->next;
451 /* Added by Wayne Scott
452 * This is needed because the ldopen system call calls
453 * calloc to allocated a block of date. The ldclose call calls free.
454 * Without this we get this system calloc and perl's free, resulting
455 * in a "Bad free" message. This way we always use perl's malloc.
457 void *calloc(size_t ne, size_t sz)
461 out = (void *) safemalloc(ne*sz);
467 * Build the export table from the XCOFF .loader section.
469 static int readExports(ModulePtr mp)
481 if ((ldp = ldopen(mp->name, ldp)) == NULL) {
485 if (errno != ENOENT) {
487 strcpy(dl_errbuf, "readExports: ");
488 strerrorcat(dl_errbuf, errno);
492 * The module might be loaded due to the LIBPATH
493 * environment variable. Search for the loaded
494 * module using L_GETINFO.
496 if ((buf = safemalloc(size)) == NULL) {
498 strcpy(dl_errbuf, "readExports: ");
499 strerrorcat(dl_errbuf, errno);
502 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
505 if ((buf = safemalloc(size)) == NULL) {
507 strcpy(dl_errbuf, "readExports: ");
508 strerrorcat(dl_errbuf, errno);
514 strcpy(dl_errbuf, "readExports: ");
515 strerrorcat(dl_errbuf, errno);
520 * Traverse the list of loaded modules. The entry point
521 * returned by LOAD() does actually point to the data
524 lp = (struct ld_info *)buf;
526 if (lp->ldinfo_dataorg == mp->entry) {
527 ldp = ldopen(lp->ldinfo_filename, ldp);
530 if (lp->ldinfo_next == 0)
533 lp = (struct ld_info *)((char *)lp + lp->ldinfo_next);
538 strcpy(dl_errbuf, "readExports: ");
539 strerrorcat(dl_errbuf, errno);
543 #ifdef USE_64_BIT_ALL
544 if (TYPE(ldp) != U803XTOCMAGIC) {
546 if (TYPE(ldp) != U802TOCMAGIC) {
549 strcpy(dl_errbuf, "readExports: bad magic");
550 while(ldclose(ldp) == FAILURE)
554 if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
556 strcpy(dl_errbuf, "readExports: cannot read loader section header");
557 while(ldclose(ldp) == FAILURE)
562 * We read the complete loader section in one chunk, this makes
563 * finding long symbol names residing in the string table easier.
565 if ((ldbuf = (char *)safemalloc(sh.s_size)) == NULL) {
567 strcpy(dl_errbuf, "readExports: ");
568 strerrorcat(dl_errbuf, errno);
569 while(ldclose(ldp) == FAILURE)
573 if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
575 strcpy(dl_errbuf, "readExports: cannot seek to loader section");
577 while(ldclose(ldp) == FAILURE)
581 /* This first case is a hack, since it assumes that the 3rd parameter to
582 FREAD is 1. See the redefinition of FREAD above to see how this works. */
583 if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
585 strcpy(dl_errbuf, "readExports: cannot read loader section");
587 while(ldclose(ldp) == FAILURE)
591 lhp = (AIX_LDHDR *)ldbuf;
592 ls = (AIX_LDSYM *)(ldbuf+AIX_LDHDRSZ);
594 * Count the number of exports to include in our export table.
596 for (i = lhp->l_nsyms; i; i--, ls++) {
597 if (!LDR_EXPORT(*ls))
601 Newz(1001, mp->exports, mp->nExports, Export);
602 if (mp->exports == NULL) {
604 strcpy(dl_errbuf, "readExports: ");
605 strerrorcat(dl_errbuf, errno);
607 while(ldclose(ldp) == FAILURE)
612 * Fill in the export table. All entries are relative to
613 * the entry point we got from load.
616 ls = (AIX_LDSYM *)(ldbuf+AIX_LDHDRSZ);
617 for (i = lhp->l_nsyms; i; i--, ls++) {
619 if (!LDR_EXPORT(*ls))
621 #ifndef USE_64_BIT_ALL
622 if (ls->l_zeroes == 0)
624 symname = ls->l_offset+lhp->l_stoff+ldbuf;
625 #ifndef USE_64_BIT_ALL
627 symname = ls->l_name;
629 ep->name = savepv(symname);
630 ep->addr = (void *)((unsigned long)mp->entry + ls->l_value);
634 while(ldclose(ldp) == FAILURE)
640 * Find the main modules entry point. This is used as export pointer
641 * for loadbind() to be able to resolve references to the main part.
643 static void * findMain(void)
653 if ((buf = safemalloc(size)) == NULL) {
655 strcpy(dl_errbuf, "findMain: ");
656 strerrorcat(dl_errbuf, errno);
659 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
662 if ((buf = safemalloc(size)) == NULL) {
664 strcpy(dl_errbuf, "findMain: ");
665 strerrorcat(dl_errbuf, errno);
671 strcpy(dl_errbuf, "findMain: ");
672 strerrorcat(dl_errbuf, errno);
677 * The first entry is the main module. The entry point
678 * returned by load() does actually point to the data
681 lp = (struct ld_info *)buf;
682 ret = lp->ldinfo_dataorg;
686 #endif /* USE_NATIVE_DLOPEN */
690 * Platform: SunOS/Solaris, possibly others which use dlopen.
691 * Author: Paul Marquess (Paul.Marquess@btinternet.com)
692 * Created: 10th July 1994
695 * 15th July 1994 - Added code to explicitly save any error messages.
696 * 3rd August 1994 - Upgraded to v3 spec.
697 * 9th August 1994 - Changed to use IV
698 * 10th August 1994 - Tim Bunce: Added RTLD_LAZY, switchable debugging,
699 * basic FreeBSD support, removed ClearError
710 dl_private_init(pTHX)
712 (void)dl_generic_private_init(aTHX);
715 MODULE = DynaLoader PACKAGE = DynaLoader
718 (void)dl_private_init(aTHX);
722 dl_load_file(filename, flags=0)
726 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags));
728 Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
729 RETVAL = dlopen(filename, RTLD_GLOBAL|RTLD_LAZY) ;
730 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%x\n", RETVAL));
731 ST(0) = sv_newmortal() ;
733 SaveError(aTHX_ "%s",dlerror()) ;
735 sv_setiv( ST(0), PTR2IV(RETVAL) );
738 dl_unload_file(libref)
741 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_unload_file(%lx):\n", libref));
742 RETVAL = (dlclose(libref) == 0 ? 1 : 0);
744 SaveError(aTHX_ "%s", dlerror()) ;
745 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " retval = %d\n", RETVAL));
750 dl_find_symbol(libhandle, symbolname)
754 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_find_symbol(handle=%x, symbol=%s)\n",
755 libhandle, symbolname));
756 RETVAL = dlsym(libhandle, symbolname);
757 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " symbolref = %x\n", RETVAL));
758 ST(0) = sv_newmortal() ;
760 SaveError(aTHX_ "%s",dlerror()) ;
762 sv_setiv( ST(0), PTR2IV(RETVAL));
771 # These functions should not need changing on any platform:
774 dl_install_xsub(perl_name, symref, filename="$Package")
779 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n",
781 ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
782 (void(*)(pTHX_ CV *))symref,
790 RETVAL = dl_last_error ;