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 * @(#)dlfcn.c 1.5 revision of 93/02/14 20:14:17
18 * This is an unpublished work copyright (c) 1992 Helios Software GmbH
19 * 3000 Hannover 1, Germany
25 /* When building as a 64-bit binary on AIX, define this to get the
26 * correct structure definitions. Also determines the field-name
27 * macros and gates some logic in readEntries(). -- Steven N. Hirsch
28 * <hirschs@btv.ibm.com> */
38 #include <sys/types.h>
46 # define AIX_SCNHDR SCNHDR_64
47 # define AIX_LDHDR LDHDR_64
48 # define AIX_LDSYM LDSYM_64
49 # define AIX_LDHDRSZ LDHDRSZ_64
51 # define AIX_SCNHDR SCNHDR
52 # define AIX_LDHDR LDHDR
53 # define AIX_LDSYM LDSYM
54 # define AIX_LDHDRSZ LDHDRSZ
57 /* When using Perl extensions written in C++ the longer versions
58 * of load() and unload() from libC and libC_r need to be used,
59 * otherwise statics in the extensions won't get initialized right.
60 * -- Stephanie Beals <bealzy@us.ibm.com> */
62 /* Older AIX C compilers cannot deal with C++ double-slash comments in
63 the ibmcxx and/or xlC includes. Since we only need a single file,
64 be more fine-grained about what's included <hirschs@btv.ibm.com> */
66 #ifdef USE_libC /* The define comes, when it comes, from hints/aix.pl. */
67 # define LOAD loadAndInit
68 # define UNLOAD terminateAndUnload
69 # if defined(USE_vacpp_load_h)
70 # include "/usr/vacpp/include/load.h"
71 # elif defined(USE_ibmcxx_load_h)
72 # include "/usr/ibmcxx/include/load.h"
73 # elif defined(USE_xlC_load_h)
74 # include "/usr/lpp/xlC/include/load.h"
75 # elif defined(USE_load_h)
76 # include "/usr/include/load.h"
80 # define UNLOAD unload
84 * AIX 4.3 does remove some useful definitions from ldfcn.h. Define
85 * these here to compensate for that lossage.
88 # define BEGINNING SEEK_SET
91 # define FSEEK(ldptr,o,p) fseek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr) +o):o,p)
94 # define FREAD(p,s,n,ldptr) fread(p,s,n,IOPTR(ldptr))
98 * We simulate dlopen() et al. through a call to load. Because AIX has
99 * no call to find an exported symbol we read the loader section of the
100 * loaded module and build a list of exported symbols and their virtual
105 char *name; /* the symbols's name */
106 void *addr; /* its relocated virtual address */
107 } Export, *ExportPtr;
110 * The void * handle returned from dlopen is actually a ModulePtr.
112 typedef struct Module {
114 char *name; /* module name for refcounting */
115 int refCnt; /* the number of references */
116 void *entry; /* entry point from load */
117 int nExports; /* the number of exports found */
118 ExportPtr exports; /* the array of exports */
119 } Module, *ModulePtr;
122 * We keep a list of all loaded modules to be able to reference count
123 * duplicate dlopen's.
125 static ModulePtr modList; /* XXX threaded */
128 * The last error from one of the dl* routines is kept in static
129 * variables here. Each error is returned only once to the caller.
131 static char errbuf[BUFSIZ]; /* XXX threaded */
132 static int errvalid; /* XXX threaded */
134 static void caterr(char *);
135 static int readExports(ModulePtr);
136 static void *findMain(void);
138 static char *strerror_failed = "(strerror failed)";
139 static char *strerror_r_failed = "(strerror_r failed)";
141 char *strerrorcat(char *str, int err) {
142 int strsiz = strlen(str);
147 char *buf = malloc(BUFSIZ);
151 if (strerror_r(err, buf, BUFSIZ) == 0)
154 msg = strerror_r_failed;
155 msgsiz = strlen(msg);
156 if (strsiz + msgsiz < BUFSIZ)
160 if ((msg = strerror(err)) == 0)
161 msg = strerror_failed;
162 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
163 if (strsiz + msgsiz < BUFSIZ) /* Do not move this after #endif. */
170 char *strerrorcpy(char *str, int err) {
175 char *buf = malloc(BUFSIZ);
179 if (strerror_r(err, buf, BUFSIZ) == 0)
182 msg = strerror_r_failed;
183 msgsiz = strlen(msg);
188 if ((msg = strerror(err)) == 0)
189 msg = strerror_failed;
190 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
191 if (msgsiz < BUFSIZ) /* Do not move this after #endif. */
199 void *dlopen(char *path, int mode)
202 register ModulePtr mp;
203 static void *mainModule; /* XXX threaded */
206 * Upon the first call register a terminate handler that will
207 * close all libraries.
209 if (mainModule == NULL) {
210 if ((mainModule = findMain()) == NULL)
214 * Scan the list of modules if have the module already loaded.
216 for (mp = modList; mp; mp = mp->next)
217 if (strcmp(mp->name, path) == 0) {
221 Newz(1000,mp,1,Module);
224 strcpy(errbuf, "Newz: ");
225 strerrorcat(errbuf, errno);
229 if ((mp->name = savepv(path)) == NULL) {
231 strcpy(errbuf, "savepv: ");
232 strerrorcat(errbuf, errno);
238 * load should be declared load(const char *...). Thus we
239 * cast the path to a normal char *. Ugly.
241 if ((mp->entry = (void *)LOAD((char *)path,
242 #ifdef L_LIBPATH_EXEC
247 int saverrno = errno;
252 strcpy(errbuf, "dlopen: ");
253 strcat(errbuf, path);
254 strcat(errbuf, ": ");
256 * If AIX says the file is not executable, the error
257 * can be further described by querying the loader about
260 if (saverrno == ENOEXEC) {
261 char *moreinfo[BUFSIZ/sizeof(char *)];
262 if (loadquery(L_GETMESSAGES, moreinfo, sizeof(moreinfo)) == -1)
263 strerrorcpy(errbuf, saverrno);
266 for (p = moreinfo; *p; p++)
270 strerrorcat(errbuf, saverrno);
277 * Assume anonymous exports come from the module this dlopen
278 * is linked into, that holds true as long as dlopen and all
279 * of the perl core are in the same shared object. Also bind
280 * against the main part, in the case a perl is not the main
281 * part, e.g mod_perl as DSO in Apache so perl modules can
282 * also reference Apache symbols.
284 if (loadbind(0, (void *)dlopen, mp->entry) == -1 ||
285 loadbind(0, mainModule, mp->entry)) {
286 int saverrno = errno;
290 strcpy(errbuf, "loadbind: ");
291 strerrorcat(errbuf, saverrno);
294 if (readExports(mp) == -1) {
302 * Attempt to decipher an AIX loader error message and append it
303 * to our static error message buffer.
305 static void caterr(char *s)
307 register char *p = s;
309 while (*p >= '0' && *p <= '9')
312 case L_ERROR_TOOMANY:
313 strcat(errbuf, "too many errors");
316 strcat(errbuf, "can't load library");
320 strcat(errbuf, "can't find symbol");
324 strcat(errbuf, "bad RLD");
328 strcat(errbuf, "bad exec format in");
332 strerrorcat(errbuf, atoi(++p));
340 void *dlsym(void *handle, const char *symbol)
342 register ModulePtr mp = (ModulePtr)handle;
343 register ExportPtr ep;
347 * Could speed up search, but I assume that one assigns
348 * the result to function pointers anyways.
350 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
351 if (strcmp(ep->name, symbol) == 0)
354 strcpy(errbuf, "dlsym: undefined symbol ");
355 strcat(errbuf, symbol);
368 int dlclose(void *handle)
370 register ModulePtr mp = (ModulePtr)handle;
372 register ModulePtr mp1;
374 if (--mp->refCnt > 0)
376 result = UNLOAD(mp->entry);
379 strerrorcpy(errbuf, errno);
382 register ExportPtr ep;
384 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
387 safefree(mp->exports);
392 for (mp1 = modList; mp1; mp1 = mp1->next)
393 if (mp1->next == mp) {
394 mp1->next = mp->next;
403 /* Added by Wayne Scott
404 * This is needed because the ldopen system call calls
405 * calloc to allocated a block of date. The ldclose call calls free.
406 * Without this we get this system calloc and perl's free, resulting
407 * in a "Bad free" message. This way we always use perl's malloc.
409 void *calloc(size_t ne, size_t sz)
413 out = (void *) safemalloc(ne*sz);
419 * Build the export table from the XCOFF .loader section.
421 static int readExports(ModulePtr mp)
432 if ((ldp = ldopen(mp->name, ldp)) == NULL) {
436 if (errno != ENOENT) {
438 strcpy(errbuf, "readExports: ");
439 strerrorcat(errbuf, errno);
443 * The module might be loaded due to the LIBPATH
444 * environment variable. Search for the loaded
445 * module using L_GETINFO.
447 if ((buf = safemalloc(size)) == NULL) {
449 strcpy(errbuf, "readExports: ");
450 strerrorcat(errbuf, errno);
453 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
456 if ((buf = safemalloc(size)) == NULL) {
458 strcpy(errbuf, "readExports: ");
459 strerrorcat(errbuf, errno);
465 strcpy(errbuf, "readExports: ");
466 strerrorcat(errbuf, errno);
471 * Traverse the list of loaded modules. The entry point
472 * returned by LOAD() does actually point to the data
475 lp = (struct ld_info *)buf;
477 if (lp->ldinfo_dataorg == mp->entry) {
478 ldp = ldopen(lp->ldinfo_filename, ldp);
481 if (lp->ldinfo_next == 0)
484 lp = (struct ld_info *)((char *)lp + lp->ldinfo_next);
489 strcpy(errbuf, "readExports: ");
490 strerrorcat(errbuf, errno);
494 #ifdef USE_64_BIT_ALL
495 if (TYPE(ldp) != U803XTOCMAGIC) {
497 if (TYPE(ldp) != U802TOCMAGIC) {
500 strcpy(errbuf, "readExports: bad magic");
501 while(ldclose(ldp) == FAILURE)
505 if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
507 strcpy(errbuf, "readExports: cannot read loader section header");
508 while(ldclose(ldp) == FAILURE)
513 * We read the complete loader section in one chunk, this makes
514 * finding long symbol names residing in the string table easier.
516 if ((ldbuf = (char *)safemalloc(sh.s_size)) == NULL) {
518 strcpy(errbuf, "readExports: ");
519 strerrorcat(errbuf, errno);
520 while(ldclose(ldp) == FAILURE)
524 if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
526 strcpy(errbuf, "readExports: cannot seek to loader section");
528 while(ldclose(ldp) == FAILURE)
532 /* This first case is a hack, since it assumes that the 3rd parameter to
533 FREAD is 1. See the redefinition of FREAD above to see how this works. */
534 if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
536 strcpy(errbuf, "readExports: cannot read loader section");
538 while(ldclose(ldp) == FAILURE)
542 lhp = (AIX_LDHDR *)ldbuf;
543 ls = (AIX_LDSYM *)(ldbuf+AIX_LDHDRSZ);
545 * Count the number of exports to include in our export table.
547 for (i = lhp->l_nsyms; i; i--, ls++) {
548 if (!LDR_EXPORT(*ls))
552 Newz(1001, mp->exports, mp->nExports, Export);
553 if (mp->exports == NULL) {
555 strcpy(errbuf, "readExports: ");
556 strerrorcat(errbuf, errno);
558 while(ldclose(ldp) == FAILURE)
563 * Fill in the export table. All entries are relative to
564 * the entry point we got from load.
567 ls = (AIX_LDSYM *)(ldbuf+AIX_LDHDRSZ);
568 for (i = lhp->l_nsyms; i; i--, ls++) {
570 if (!LDR_EXPORT(*ls))
572 #ifndef USE_64_BIT_ALL
573 if (ls->l_zeroes == 0)
575 symname = ls->l_offset+lhp->l_stoff+ldbuf;
576 #ifndef USE_64_BIT_ALL
578 symname = ls->l_name;
580 ep->name = savepv(symname);
581 ep->addr = (void *)((unsigned long)mp->entry + ls->l_value);
585 while(ldclose(ldp) == FAILURE)
591 * Find the main modules entry point. This is used as export pointer
592 * for loadbind() to be able to resolve references to the main part.
594 static void * findMain(void)
602 if ((buf = safemalloc(size)) == NULL) {
604 strcpy(errbuf, "findMain: ");
605 strerrorcat(errbuf, errno);
608 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
611 if ((buf = safemalloc(size)) == NULL) {
613 strcpy(errbuf, "findMain: ");
614 strerrorcat(errbuf, errno);
620 strcpy(errbuf, "findMain: ");
621 strerrorcat(errbuf, errno);
626 * The first entry is the main module. The entry point
627 * returned by load() does actually point to the data
630 lp = (struct ld_info *)buf;
631 ret = lp->ldinfo_dataorg;
638 * Platform: SunOS/Solaris, possibly others which use dlopen.
639 * Author: Paul Marquess (Paul.Marquess@btinternet.com)
640 * Created: 10th July 1994
643 * 15th July 1994 - Added code to explicitly save any error messages.
644 * 3rd August 1994 - Upgraded to v3 spec.
645 * 9th August 1994 - Changed to use IV
646 * 10th August 1994 - Tim Bunce: Added RTLD_LAZY, switchable debugging,
647 * basic FreeBSD support, removed ClearError
657 #include "dlutils.c" /* SaveError() etc */
661 dl_private_init(pTHX)
663 (void)dl_generic_private_init(aTHX);
666 MODULE = DynaLoader PACKAGE = DynaLoader
669 (void)dl_private_init(aTHX);
673 dl_load_file(filename, flags=0)
677 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags));
679 Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
680 RETVAL = dlopen(filename, 1) ;
681 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%x\n", RETVAL));
682 ST(0) = sv_newmortal() ;
684 SaveError(aTHX_ "%s",dlerror()) ;
686 sv_setiv( ST(0), PTR2IV(RETVAL) );
689 dl_unload_file(libref)
692 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_unload_file(%lx):\n", libref));
693 RETVAL = (dlclose(libref) == 0 ? 1 : 0);
695 SaveError(aTHX_ "%s", dlerror()) ;
696 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " retval = %d\n", RETVAL));
701 dl_find_symbol(libhandle, symbolname)
705 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_find_symbol(handle=%x, symbol=%s)\n",
706 libhandle, symbolname));
707 RETVAL = dlsym(libhandle, symbolname);
708 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " symbolref = %x\n", RETVAL));
709 ST(0) = sv_newmortal() ;
711 SaveError(aTHX_ "%s",dlerror()) ;
713 sv_setiv( ST(0), PTR2IV(RETVAL));
722 # These functions should not need changing on any platform:
725 dl_install_xsub(perl_name, symref, filename="$Package")
730 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n",
732 ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
733 (void(*)(pTHX_ CV *))symref,