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 startup... It can probably be trimmed more.
14 #define PERLIO_NOT_STDIO 0
16 #define PERL_IN_DL_AIX_XS
19 * On AIX 4.3 and above the emulation layer is not needed any more, and
20 * indeed if perl uses its emulation and perl is linked into apache
21 * which is supposed to use the native dlopen conflicts arise.
22 * Jens-Uwe Mager jum@helios.de
24 #ifdef USE_NATIVE_DLOPEN
31 #include "dlutils.c" /* SaveError() etc */
36 * @(#)dlfcn.c 1.5 revision of 93/02/14 20:14:17
37 * This is an unpublished work copyright (c) 1992 Helios Software GmbH
38 * 3000 Hannover 1, Germany
44 /* When building as a 64-bit binary on AIX, define this to get the
45 * correct structure definitions. Also determines the field-name
46 * macros and gates some logic in readEntries(). -- Steven N. Hirsch
47 * <hirschs@btv.ibm.com> */
57 #include <sys/types.h>
65 # define AIX_SCNHDR SCNHDR_64
66 # define AIX_LDHDR LDHDR_64
67 # define AIX_LDSYM LDSYM_64
68 # define AIX_LDHDRSZ LDHDRSZ_64
70 # define AIX_SCNHDR SCNHDR
71 # define AIX_LDHDR LDHDR
72 # define AIX_LDSYM LDSYM
73 # define AIX_LDHDRSZ LDHDRSZ
76 /* When using Perl extensions written in C++ the longer versions
77 * of load() and unload() from libC and libC_r need to be used,
78 * otherwise statics in the extensions won't get initialized right.
79 * -- Stephanie Beals <bealzy@us.ibm.com> */
81 /* Older AIX C compilers cannot deal with C++ double-slash comments in
82 the ibmcxx and/or xlC includes. Since we only need a single file,
83 be more fine-grained about what's included <hirschs@btv.ibm.com> */
85 #ifdef USE_libC /* The define comes, when it comes, from hints/aix.pl. */
86 # define LOAD loadAndInit
87 # define UNLOAD terminateAndUnload
88 # if defined(USE_vacpp_load_h)
89 # include "/usr/vacpp/include/load.h"
90 # elif defined(USE_ibmcxx_load_h)
91 # include "/usr/ibmcxx/include/load.h"
92 # elif defined(USE_xlC_load_h)
93 # include "/usr/lpp/xlC/include/load.h"
94 # elif defined(USE_load_h)
95 # include "/usr/include/load.h"
99 # define UNLOAD unload
103 * AIX 4.3 does remove some useful definitions from ldfcn.h. Define
104 * these here to compensate for that lossage.
107 # define BEGINNING SEEK_SET
110 # define FSEEK(ldptr,o,p) fseek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr) +o):o,p)
113 # define FREAD(p,s,n,ldptr) fread(p,s,n,IOPTR(ldptr))
120 # define RTLD_GLOBAL 0
124 * We simulate dlopen() et al. through a call to load. Because AIX has
125 * no call to find an exported symbol we read the loader section of the
126 * loaded module and build a list of exported symbols and their virtual
131 char *name; /* the symbols's name */
132 void *addr; /* its relocated virtual address */
133 } Export, *ExportPtr;
136 * The void * handle returned from dlopen is actually a ModulePtr.
138 typedef struct Module {
140 char *name; /* module name for refcounting */
141 int refCnt; /* the number of references */
142 void *entry; /* entry point from load */
143 int nExports; /* the number of exports found */
144 ExportPtr exports; /* the array of exports */
145 } Module, *ModulePtr;
149 * We keep a list of all loaded modules to be able to reference count
150 * duplicate dlopen's.
155 * The last error from one of the dl* routines is kept in static
156 * variables here. Each error is returned only once to the caller.
158 char x_errbuf[BUFSIZ];
161 } my_cxtx_t; /* this *must* be named my_cxtx_t */
163 #define DL_CXT_EXTRA /* ask for dl_cxtx to be defined in dlutils.c */
164 #include "dlutils.c" /* SaveError() etc */
166 #define dl_modList (dl_cxtx.x_modList)
167 #define dl_errbuf (dl_cxtx.x_errbuf)
168 #define dl_errvalid (dl_cxtx.x_errvalid)
169 #define dl_mainModule (dl_cxtx.x_mainModule)
171 static void caterr(char *);
172 static int readExports(ModulePtr);
173 static void *findMain(void);
175 /* these statics are ok because they're constants */
176 static char *strerror_failed = "(strerror failed)";
177 static char *strerror_r_failed = "(strerror_r failed)";
179 char *strerrorcat(char *str, int err) {
180 int strsiz = strlen(str);
186 if ((msg = strerror(err)) == 0)
187 msg = strerror_failed;
188 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
189 if (strsiz + msgsiz < BUFSIZ) /* Do not move this after #endif. */
195 char *strerrorcpy(char *str, int err) {
201 if ((msg = strerror(err)) == 0)
202 msg = strerror_failed;
203 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
204 if (msgsiz < BUFSIZ) /* Do not move this after #endif. */
211 void *dlopen(char *path, int mode)
218 * Upon the first call register a terminate handler that will
219 * close all libraries.
221 if (dl_mainModule == NULL) {
222 if ((dl_mainModule = findMain()) == NULL)
226 * Scan the list of modules if have the module already loaded.
228 for (mp = dl_modList; mp; mp = mp->next)
229 if (strcmp(mp->name, path) == 0) {
236 strcpy(dl_errbuf, "Newz: ");
237 strerrorcat(dl_errbuf, errno);
241 if ((mp->name = savepv(path)) == NULL) {
243 strcpy(dl_errbuf, "savepv: ");
244 strerrorcat(dl_errbuf, errno);
250 * load should be declared load(const char *...). Thus we
251 * cast the path to a normal char *. Ugly.
253 if ((mp->entry = (void *)LOAD((char *)path,
254 #ifdef L_LIBPATH_EXEC
259 int saverrno = errno;
264 strcpy(dl_errbuf, "dlopen: ");
265 strcat(dl_errbuf, path);
266 strcat(dl_errbuf, ": ");
268 * If AIX says the file is not executable, the error
269 * can be further described by querying the loader about
272 if (saverrno == ENOEXEC) {
273 char *moreinfo[BUFSIZ/sizeof(char *)];
274 if (loadquery(L_GETMESSAGES, moreinfo, sizeof(moreinfo)) == -1)
275 strerrorcpy(dl_errbuf, saverrno);
278 for (p = moreinfo; *p; p++)
282 strerrorcat(dl_errbuf, saverrno);
286 mp->next = dl_modList;
289 * Assume anonymous exports come from the module this dlopen
290 * is linked into, that holds true as long as dlopen and all
291 * of the perl core are in the same shared object. Also bind
292 * against the main part, in the case a perl is not the main
293 * part, e.g mod_perl as DSO in Apache so perl modules can
294 * also reference Apache symbols.
296 if (loadbind(0, (void *)dlopen, mp->entry) == -1 ||
297 loadbind(0, dl_mainModule, mp->entry)) {
298 int saverrno = errno;
302 strcpy(dl_errbuf, "loadbind: ");
303 strerrorcat(dl_errbuf, saverrno);
306 if (readExports(mp) == -1) {
314 * Attempt to decipher an AIX loader error message and append it
315 * to our static error message buffer.
317 static void caterr(char *s)
323 while (*p >= '0' && *p <= '9')
326 case L_ERROR_TOOMANY:
327 strcat(dl_errbuf, "too many errors");
330 strcat(dl_errbuf, "can't load library");
331 strcat(dl_errbuf, p);
334 strcat(dl_errbuf, "can't find symbol");
335 strcat(dl_errbuf, p);
338 strcat(dl_errbuf, "bad RLD");
339 strcat(dl_errbuf, p);
342 strcat(dl_errbuf, "bad exec format in");
343 strcat(dl_errbuf, p);
346 strerrorcat(dl_errbuf, atoi(++p));
349 strcat(dl_errbuf, s);
354 void *dlsym(void *handle, const char *symbol)
358 ModulePtr mp = (ModulePtr)handle;
363 * Could speed up search, but I assume that one assigns
364 * the result to function pointers anyways.
366 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
367 if (strcmp(ep->name, symbol) == 0)
370 strcpy(dl_errbuf, "dlsym: undefined symbol ");
371 strcat(dl_errbuf, symbol);
386 int dlclose(void *handle)
390 ModulePtr mp = (ModulePtr)handle;
394 if (--mp->refCnt > 0)
396 result = UNLOAD(mp->entry);
399 strerrorcpy(dl_errbuf, errno);
404 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
407 safefree(mp->exports);
409 if (mp == dl_modList)
410 dl_modList = mp->next;
412 for (mp1 = dl_modList; mp1; mp1 = mp1->next)
413 if (mp1->next == mp) {
414 mp1->next = mp->next;
423 /* Added by Wayne Scott
424 * This is needed because the ldopen system call calls
425 * calloc to allocated a block of date. The ldclose call calls free.
426 * Without this we get this system calloc and perl's free, resulting
427 * in a "Bad free" message. This way we always use perl's malloc.
429 void *calloc(size_t ne, size_t sz)
433 out = (void *) safemalloc(ne*sz);
439 * Build the export table from the XCOFF .loader section.
441 static int readExports(ModulePtr mp)
453 if ((ldp = ldopen(mp->name, ldp)) == NULL) {
457 if (errno != ENOENT) {
459 strcpy(dl_errbuf, "readExports: ");
460 strerrorcat(dl_errbuf, errno);
464 * The module might be loaded due to the LIBPATH
465 * environment variable. Search for the loaded
466 * module using L_GETINFO.
468 if ((buf = safemalloc(size)) == NULL) {
470 strcpy(dl_errbuf, "readExports: ");
471 strerrorcat(dl_errbuf, errno);
474 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
477 if ((buf = safemalloc(size)) == NULL) {
479 strcpy(dl_errbuf, "readExports: ");
480 strerrorcat(dl_errbuf, errno);
486 strcpy(dl_errbuf, "readExports: ");
487 strerrorcat(dl_errbuf, errno);
492 * Traverse the list of loaded modules. The entry point
493 * returned by LOAD() does actually point to the data
496 lp = (struct ld_info *)buf;
498 if (lp->ldinfo_dataorg == mp->entry) {
499 ldp = ldopen(lp->ldinfo_filename, ldp);
502 if (lp->ldinfo_next == 0)
505 lp = (struct ld_info *)((char *)lp + lp->ldinfo_next);
510 strcpy(dl_errbuf, "readExports: ");
511 strerrorcat(dl_errbuf, errno);
515 #ifdef USE_64_BIT_ALL
516 if (TYPE(ldp) != U803XTOCMAGIC) {
518 if (TYPE(ldp) != U802TOCMAGIC) {
521 strcpy(dl_errbuf, "readExports: bad magic");
522 while(ldclose(ldp) == FAILURE)
526 if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
528 strcpy(dl_errbuf, "readExports: cannot read loader section header");
529 while(ldclose(ldp) == FAILURE)
534 * We read the complete loader section in one chunk, this makes
535 * finding long symbol names residing in the string table easier.
537 if ((ldbuf = (char *)safemalloc(sh.s_size)) == NULL) {
539 strcpy(dl_errbuf, "readExports: ");
540 strerrorcat(dl_errbuf, errno);
541 while(ldclose(ldp) == FAILURE)
545 if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
547 strcpy(dl_errbuf, "readExports: cannot seek to loader section");
549 while(ldclose(ldp) == FAILURE)
553 /* This first case is a hack, since it assumes that the 3rd parameter to
554 FREAD is 1. See the redefinition of FREAD above to see how this works. */
555 if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
557 strcpy(dl_errbuf, "readExports: cannot read loader section");
559 while(ldclose(ldp) == FAILURE)
563 lhp = (AIX_LDHDR *)ldbuf;
564 ls = (AIX_LDSYM *)(ldbuf+AIX_LDHDRSZ);
566 * Count the number of exports to include in our export table.
568 for (i = lhp->l_nsyms; i; i--, ls++) {
569 if (!LDR_EXPORT(*ls))
573 Newxz(mp->exports, mp->nExports, Export);
574 if (mp->exports == NULL) {
576 strcpy(dl_errbuf, "readExports: ");
577 strerrorcat(dl_errbuf, errno);
579 while(ldclose(ldp) == FAILURE)
584 * Fill in the export table. All entries are relative to
585 * the entry point we got from load.
588 ls = (AIX_LDSYM *)(ldbuf+AIX_LDHDRSZ);
589 for (i = lhp->l_nsyms; i; i--, ls++) {
591 if (!LDR_EXPORT(*ls))
593 #ifndef USE_64_BIT_ALL
594 if (ls->l_zeroes == 0)
596 symname = ls->l_offset+lhp->l_stoff+ldbuf;
597 #ifndef USE_64_BIT_ALL
599 symname = ls->l_name;
601 ep->name = savepv(symname);
602 ep->addr = (void *)((unsigned long)mp->entry + ls->l_value);
606 while(ldclose(ldp) == FAILURE)
612 * Find the main modules entry point. This is used as export pointer
613 * for loadbind() to be able to resolve references to the main part.
615 static void * findMain(void)
625 if ((buf = safemalloc(size)) == NULL) {
627 strcpy(dl_errbuf, "findMain: ");
628 strerrorcat(dl_errbuf, errno);
631 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
634 if ((buf = safemalloc(size)) == NULL) {
636 strcpy(dl_errbuf, "findMain: ");
637 strerrorcat(dl_errbuf, errno);
643 strcpy(dl_errbuf, "findMain: ");
644 strerrorcat(dl_errbuf, errno);
649 * The first entry is the main module. The entry point
650 * returned by load() does actually point to the data
653 lp = (struct ld_info *)buf;
654 ret = lp->ldinfo_dataorg;
658 #endif /* USE_NATIVE_DLOPEN */
662 * Platform: SunOS/Solaris, possibly others which use dlopen.
663 * Author: Paul Marquess (Paul.Marquess@btinternet.com)
664 * Created: 10th July 1994
667 * 15th July 1994 - Added code to explicitly save any error messages.
668 * 3rd August 1994 - Upgraded to v3 spec.
669 * 9th August 1994 - Changed to use IV
670 * 10th August 1994 - Tim Bunce: Added RTLD_LAZY, switchable debugging,
671 * basic FreeBSD support, removed ClearError
682 dl_private_init(pTHX)
684 (void)dl_generic_private_init(aTHX);
687 MODULE = DynaLoader PACKAGE = DynaLoader
690 (void)dl_private_init(aTHX);
694 dl_load_file(filename, flags=0)
700 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags));
702 Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
703 retv = dlopen(filename, RTLD_GLOBAL|RTLD_LAZY) ;
704 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%x\n", retv));
705 ST(0) = sv_newmortal() ;
707 SaveError(aTHX_ "%s",dlerror()) ;
709 sv_setiv( ST(0), PTR2IV(retv) );
713 dl_unload_file(libref)
716 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_unload_file(%lx):\n", libref));
717 RETVAL = (dlclose(libref) == 0 ? 1 : 0);
719 SaveError(aTHX_ "%s", dlerror()) ;
720 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " retval = %d\n", RETVAL));
725 dl_find_symbol(libhandle, symbolname, ign_err=0)
732 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_find_symbol(handle=%x, symbol=%s)\n",
733 libhandle, symbolname));
734 retv = dlsym(libhandle, symbolname);
735 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " symbolref = %x\n", retv));
736 ST(0) = sv_newmortal();
739 SaveError(aTHX_ "%s", dlerror());
741 sv_setiv( ST(0), PTR2IV(retv));
750 # These functions should not need changing on any platform:
753 dl_install_xsub(perl_name, symref, filename="$Package")
756 const char * filename
758 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n",
760 ST(0) = sv_2mortal(newRV((SV*)newXS_flags(perl_name,
761 (void(*)(pTHX_ CV *))symref,
763 XS_DYNAMIC_FILENAME)));
770 RETVAL = newSVsv(MY_CXT.x_dl_last_error);
774 #if defined(USE_ITHREADS)
781 PERL_UNUSED_VAR(items);
783 /* MY_CXT_CLONE just does a memcpy on the whole structure, so to avoid
784 * using Perl variables that belong to another thread, we create our
785 * own for this thread.
787 MY_CXT.x_dl_last_error = newSVpvs("");