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
32 * @(#)dlfcn.c 1.5 revision of 93/02/14 20:14:17
33 * This is an unpublished work copyright (c) 1992 Helios Software GmbH
34 * 3000 Hannover 1, Germany
40 /* When building as a 64-bit binary on AIX, define this to get the
41 * correct structure definitions. Also determines the field-name
42 * macros and gates some logic in readEntries(). -- Steven N. Hirsch
43 * <hirschs@btv.ibm.com> */
53 #include <sys/types.h>
61 # define AIX_SCNHDR SCNHDR_64
62 # define AIX_LDHDR LDHDR_64
63 # define AIX_LDSYM LDSYM_64
64 # define AIX_LDHDRSZ LDHDRSZ_64
66 # define AIX_SCNHDR SCNHDR
67 # define AIX_LDHDR LDHDR
68 # define AIX_LDSYM LDSYM
69 # define AIX_LDHDRSZ LDHDRSZ
72 /* When using Perl extensions written in C++ the longer versions
73 * of load() and unload() from libC and libC_r need to be used,
74 * otherwise statics in the extensions won't get initialized right.
75 * -- Stephanie Beals <bealzy@us.ibm.com> */
77 /* Older AIX C compilers cannot deal with C++ double-slash comments in
78 the ibmcxx and/or xlC includes. Since we only need a single file,
79 be more fine-grained about what's included <hirschs@btv.ibm.com> */
81 #ifdef USE_libC /* The define comes, when it comes, from hints/aix.pl. */
82 # define LOAD loadAndInit
83 # define UNLOAD terminateAndUnload
84 # if defined(USE_vacpp_load_h)
85 # include "/usr/vacpp/include/load.h"
86 # elif defined(USE_ibmcxx_load_h)
87 # include "/usr/ibmcxx/include/load.h"
88 # elif defined(USE_xlC_load_h)
89 # include "/usr/lpp/xlC/include/load.h"
90 # elif defined(USE_load_h)
91 # include "/usr/include/load.h"
95 # define UNLOAD unload
99 * AIX 4.3 does remove some useful definitions from ldfcn.h. Define
100 * these here to compensate for that lossage.
103 # define BEGINNING SEEK_SET
106 # define FSEEK(ldptr,o,p) fseek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr) +o):o,p)
109 # define FREAD(p,s,n,ldptr) fread(p,s,n,IOPTR(ldptr))
116 # define RTLD_GLOBAL 0
120 * We simulate dlopen() et al. through a call to load. Because AIX has
121 * no call to find an exported symbol we read the loader section of the
122 * loaded module and build a list of exported symbols and their virtual
127 char *name; /* the symbols's name */
128 void *addr; /* its relocated virtual address */
129 } Export, *ExportPtr;
132 * The void * handle returned from dlopen is actually a ModulePtr.
134 typedef struct Module {
136 char *name; /* module name for refcounting */
137 int refCnt; /* the number of references */
138 void *entry; /* entry point from load */
139 int nExports; /* the number of exports found */
140 ExportPtr exports; /* the array of exports */
141 } Module, *ModulePtr;
144 * We keep a list of all loaded modules to be able to reference count
145 * duplicate dlopen's.
147 static ModulePtr modList; /* XXX threaded */
150 * The last error from one of the dl* routines is kept in static
151 * variables here. Each error is returned only once to the caller.
153 static char errbuf[BUFSIZ]; /* XXX threaded */
154 static int errvalid; /* XXX threaded */
156 static void caterr(char *);
157 static int readExports(ModulePtr);
158 static void *findMain(void);
160 static char *strerror_failed = "(strerror failed)";
161 static char *strerror_r_failed = "(strerror_r failed)";
163 char *strerrorcat(char *str, int err) {
164 int strsiz = strlen(str);
168 #ifdef USE_5005THREADS
169 char *buf = malloc(BUFSIZ);
173 if (strerror_r(err, buf, BUFSIZ) == 0)
176 msg = strerror_r_failed;
177 msgsiz = strlen(msg);
178 if (strsiz + msgsiz < BUFSIZ)
182 if ((msg = strerror(err)) == 0)
183 msg = strerror_failed;
184 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
185 if (strsiz + msgsiz < BUFSIZ) /* Do not move this after #endif. */
192 char *strerrorcpy(char *str, int err) {
196 #ifdef USE_5005THREADS
197 char *buf = malloc(BUFSIZ);
201 if (strerror_r(err, buf, BUFSIZ) == 0)
204 msg = strerror_r_failed;
205 msgsiz = strlen(msg);
210 if ((msg = strerror(err)) == 0)
211 msg = strerror_failed;
212 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
213 if (msgsiz < BUFSIZ) /* Do not move this after #endif. */
221 void *dlopen(char *path, int mode)
224 register ModulePtr mp;
225 static void *mainModule; /* XXX threaded */
228 * Upon the first call register a terminate handler that will
229 * close all libraries.
231 if (mainModule == NULL) {
232 if ((mainModule = findMain()) == NULL)
236 * Scan the list of modules if have the module already loaded.
238 for (mp = modList; mp; mp = mp->next)
239 if (strcmp(mp->name, path) == 0) {
243 Newz(1000,mp,1,Module);
246 strcpy(errbuf, "Newz: ");
247 strerrorcat(errbuf, errno);
251 if ((mp->name = savepv(path)) == NULL) {
253 strcpy(errbuf, "savepv: ");
254 strerrorcat(errbuf, errno);
260 * load should be declared load(const char *...). Thus we
261 * cast the path to a normal char *. Ugly.
263 if ((mp->entry = (void *)LOAD((char *)path,
264 #ifdef L_LIBPATH_EXEC
269 int saverrno = errno;
274 strcpy(errbuf, "dlopen: ");
275 strcat(errbuf, path);
276 strcat(errbuf, ": ");
278 * If AIX says the file is not executable, the error
279 * can be further described by querying the loader about
282 if (saverrno == ENOEXEC) {
283 char *moreinfo[BUFSIZ/sizeof(char *)];
284 if (loadquery(L_GETMESSAGES, moreinfo, sizeof(moreinfo)) == -1)
285 strerrorcpy(errbuf, saverrno);
288 for (p = moreinfo; *p; p++)
292 strerrorcat(errbuf, saverrno);
299 * Assume anonymous exports come from the module this dlopen
300 * is linked into, that holds true as long as dlopen and all
301 * of the perl core are in the same shared object. Also bind
302 * against the main part, in the case a perl is not the main
303 * part, e.g mod_perl as DSO in Apache so perl modules can
304 * also reference Apache symbols.
306 if (loadbind(0, (void *)dlopen, mp->entry) == -1 ||
307 loadbind(0, mainModule, mp->entry)) {
308 int saverrno = errno;
312 strcpy(errbuf, "loadbind: ");
313 strerrorcat(errbuf, saverrno);
316 if (readExports(mp) == -1) {
324 * Attempt to decipher an AIX loader error message and append it
325 * to our static error message buffer.
327 static void caterr(char *s)
329 register char *p = s;
331 while (*p >= '0' && *p <= '9')
334 case L_ERROR_TOOMANY:
335 strcat(errbuf, "too many errors");
338 strcat(errbuf, "can't load library");
342 strcat(errbuf, "can't find symbol");
346 strcat(errbuf, "bad RLD");
350 strcat(errbuf, "bad exec format in");
354 strerrorcat(errbuf, atoi(++p));
362 void *dlsym(void *handle, const char *symbol)
364 register ModulePtr mp = (ModulePtr)handle;
365 register ExportPtr ep;
369 * Could speed up search, but I assume that one assigns
370 * the result to function pointers anyways.
372 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
373 if (strcmp(ep->name, symbol) == 0)
376 strcpy(errbuf, "dlsym: undefined symbol ");
377 strcat(errbuf, symbol);
390 int dlclose(void *handle)
392 register ModulePtr mp = (ModulePtr)handle;
394 register ModulePtr mp1;
396 if (--mp->refCnt > 0)
398 result = UNLOAD(mp->entry);
401 strerrorcpy(errbuf, errno);
404 register ExportPtr ep;
406 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
409 safefree(mp->exports);
414 for (mp1 = modList; mp1; mp1 = mp1->next)
415 if (mp1->next == mp) {
416 mp1->next = mp->next;
425 /* Added by Wayne Scott
426 * This is needed because the ldopen system call calls
427 * calloc to allocated a block of date. The ldclose call calls free.
428 * Without this we get this system calloc and perl's free, resulting
429 * in a "Bad free" message. This way we always use perl's malloc.
431 void *calloc(size_t ne, size_t sz)
435 out = (void *) safemalloc(ne*sz);
441 * Build the export table from the XCOFF .loader section.
443 static int readExports(ModulePtr mp)
454 if ((ldp = ldopen(mp->name, ldp)) == NULL) {
458 if (errno != ENOENT) {
460 strcpy(errbuf, "readExports: ");
461 strerrorcat(errbuf, errno);
465 * The module might be loaded due to the LIBPATH
466 * environment variable. Search for the loaded
467 * module using L_GETINFO.
469 if ((buf = safemalloc(size)) == NULL) {
471 strcpy(errbuf, "readExports: ");
472 strerrorcat(errbuf, errno);
475 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
478 if ((buf = safemalloc(size)) == NULL) {
480 strcpy(errbuf, "readExports: ");
481 strerrorcat(errbuf, errno);
487 strcpy(errbuf, "readExports: ");
488 strerrorcat(errbuf, errno);
493 * Traverse the list of loaded modules. The entry point
494 * returned by LOAD() does actually point to the data
497 lp = (struct ld_info *)buf;
499 if (lp->ldinfo_dataorg == mp->entry) {
500 ldp = ldopen(lp->ldinfo_filename, ldp);
503 if (lp->ldinfo_next == 0)
506 lp = (struct ld_info *)((char *)lp + lp->ldinfo_next);
511 strcpy(errbuf, "readExports: ");
512 strerrorcat(errbuf, errno);
516 #ifdef USE_64_BIT_ALL
517 if (TYPE(ldp) != U803XTOCMAGIC) {
519 if (TYPE(ldp) != U802TOCMAGIC) {
522 strcpy(errbuf, "readExports: bad magic");
523 while(ldclose(ldp) == FAILURE)
527 if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
529 strcpy(errbuf, "readExports: cannot read loader section header");
530 while(ldclose(ldp) == FAILURE)
535 * We read the complete loader section in one chunk, this makes
536 * finding long symbol names residing in the string table easier.
538 if ((ldbuf = (char *)safemalloc(sh.s_size)) == NULL) {
540 strcpy(errbuf, "readExports: ");
541 strerrorcat(errbuf, errno);
542 while(ldclose(ldp) == FAILURE)
546 if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
548 strcpy(errbuf, "readExports: cannot seek to loader section");
550 while(ldclose(ldp) == FAILURE)
554 /* This first case is a hack, since it assumes that the 3rd parameter to
555 FREAD is 1. See the redefinition of FREAD above to see how this works. */
556 if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
558 strcpy(errbuf, "readExports: cannot read loader section");
560 while(ldclose(ldp) == FAILURE)
564 lhp = (AIX_LDHDR *)ldbuf;
565 ls = (AIX_LDSYM *)(ldbuf+AIX_LDHDRSZ);
567 * Count the number of exports to include in our export table.
569 for (i = lhp->l_nsyms; i; i--, ls++) {
570 if (!LDR_EXPORT(*ls))
574 Newz(1001, mp->exports, mp->nExports, Export);
575 if (mp->exports == NULL) {
577 strcpy(errbuf, "readExports: ");
578 strerrorcat(errbuf, errno);
580 while(ldclose(ldp) == FAILURE)
585 * Fill in the export table. All entries are relative to
586 * the entry point we got from load.
589 ls = (AIX_LDSYM *)(ldbuf+AIX_LDHDRSZ);
590 for (i = lhp->l_nsyms; i; i--, ls++) {
592 if (!LDR_EXPORT(*ls))
594 #ifndef USE_64_BIT_ALL
595 if (ls->l_zeroes == 0)
597 symname = ls->l_offset+lhp->l_stoff+ldbuf;
598 #ifndef USE_64_BIT_ALL
600 symname = ls->l_name;
602 ep->name = savepv(symname);
603 ep->addr = (void *)((unsigned long)mp->entry + ls->l_value);
607 while(ldclose(ldp) == FAILURE)
613 * Find the main modules entry point. This is used as export pointer
614 * for loadbind() to be able to resolve references to the main part.
616 static void * findMain(void)
624 if ((buf = safemalloc(size)) == NULL) {
626 strcpy(errbuf, "findMain: ");
627 strerrorcat(errbuf, errno);
630 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
633 if ((buf = safemalloc(size)) == NULL) {
635 strcpy(errbuf, "findMain: ");
636 strerrorcat(errbuf, errno);
642 strcpy(errbuf, "findMain: ");
643 strerrorcat(errbuf, errno);
648 * The first entry is the main module. The entry point
649 * returned by load() does actually point to the data
652 lp = (struct ld_info *)buf;
653 ret = lp->ldinfo_dataorg;
657 #endif /* USE_NATIVE_DLOPEN */
661 * Platform: SunOS/Solaris, possibly others which use dlopen.
662 * Author: Paul Marquess (Paul.Marquess@btinternet.com)
663 * Created: 10th July 1994
666 * 15th July 1994 - Added code to explicitly save any error messages.
667 * 3rd August 1994 - Upgraded to v3 spec.
668 * 9th August 1994 - Changed to use IV
669 * 10th August 1994 - Tim Bunce: Added RTLD_LAZY, switchable debugging,
670 * basic FreeBSD support, removed ClearError
680 #include "dlutils.c" /* SaveError() etc */
684 dl_private_init(pTHX)
686 (void)dl_generic_private_init(aTHX);
689 MODULE = DynaLoader PACKAGE = DynaLoader
692 (void)dl_private_init(aTHX);
696 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 RETVAL = dlopen(filename, RTLD_GLOBAL|RTLD_LAZY) ;
704 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%x\n", RETVAL));
705 ST(0) = sv_newmortal() ;
707 SaveError(aTHX_ "%s",dlerror()) ;
709 sv_setiv( ST(0), PTR2IV(RETVAL) );
712 dl_unload_file(libref)
715 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_unload_file(%lx):\n", libref));
716 RETVAL = (dlclose(libref) == 0 ? 1 : 0);
718 SaveError(aTHX_ "%s", dlerror()) ;
719 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " retval = %d\n", RETVAL));
724 dl_find_symbol(libhandle, symbolname)
728 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_find_symbol(handle=%x, symbol=%s)\n",
729 libhandle, symbolname));
730 RETVAL = dlsym(libhandle, symbolname);
731 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " symbolref = %x\n", RETVAL));
732 ST(0) = sv_newmortal() ;
734 SaveError(aTHX_ "%s",dlerror()) ;
736 sv_setiv( ST(0), PTR2IV(RETVAL));
745 # These functions should not need changing on any platform:
748 dl_install_xsub(perl_name, symref, filename="$Package")
753 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n",
755 ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
756 (void(*)(pTHX_ CV *))symref,