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> */
65 #ifdef USE_libC /* The define comes, when it comes, from hints/aix.pl. */
66 # define LOAD loadAndInit
67 # define UNLOAD terminateAndUnload
68 # if defined(USE_xlC_load_h)
69 # include "/usr/lpp/xlC/include/load.h"
70 # elif defined(USE_ibmcxx_load_h)
71 # include "/usr/ibmcxx/include/load.h"
75 # define UNLOAD unload
79 * AIX 4.3 does remove some useful definitions from ldfcn.h. Define
80 * these here to compensate for that lossage.
83 # define BEGINNING SEEK_SET
86 # define FSEEK(ldptr,o,p) fseek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr) +o):o,p)
89 # define FREAD(p,s,n,ldptr) fread(p,s,n,IOPTR(ldptr))
93 * We simulate dlopen() et al. through a call to load. Because AIX has
94 * no call to find an exported symbol we read the loader section of the
95 * loaded module and build a list of exported symbols and their virtual
100 char *name; /* the symbols's name */
101 void *addr; /* its relocated virtual address */
102 } Export, *ExportPtr;
105 * The void * handle returned from dlopen is actually a ModulePtr.
107 typedef struct Module {
109 char *name; /* module name for refcounting */
110 int refCnt; /* the number of references */
111 void *entry; /* entry point from load */
112 int nExports; /* the number of exports found */
113 ExportPtr exports; /* the array of exports */
114 } Module, *ModulePtr;
117 * We keep a list of all loaded modules to be able to reference count
118 * duplicate dlopen's.
120 static ModulePtr modList; /* XXX threaded */
123 * The last error from one of the dl* routines is kept in static
124 * variables here. Each error is returned only once to the caller.
126 static char errbuf[BUFSIZ]; /* XXX threaded */
127 static int errvalid; /* XXX threaded */
129 static void caterr(char *);
130 static int readExports(ModulePtr);
131 static void *findMain(void);
133 static char *strerror_failed = "(strerror failed)";
134 static char *strerror_r_failed = "(strerror_r failed)";
136 char *strerrorcat(char *str, int err) {
137 int strsiz = strlen(str);
142 char *buf = malloc(BUFSIZ);
146 if (strerror_r(err, buf, BUFSIZ) == 0)
149 msg = strerror_r_failed;
150 msgsiz = strlen(msg);
151 if (strsiz + msgsiz < BUFSIZ)
155 if ((msg = strerror(err)) == 0)
156 msg = strerror_failed;
157 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
158 if (strsiz + msgsiz < BUFSIZ) /* Do not move this after #endif. */
165 char *strerrorcpy(char *str, int err) {
170 char *buf = malloc(BUFSIZ);
174 if (strerror_r(err, buf, BUFSIZ) == 0)
177 msg = strerror_r_failed;
178 msgsiz = strlen(msg);
183 if ((msg = strerror(err)) == 0)
184 msg = strerror_failed;
185 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
186 if (msgsiz < BUFSIZ) /* Do not move this after #endif. */
194 void *dlopen(char *path, int mode)
197 register ModulePtr mp;
198 static void *mainModule; /* XXX threaded */
201 * Upon the first call register a terminate handler that will
202 * close all libraries.
204 if (mainModule == NULL) {
205 if ((mainModule = findMain()) == NULL)
209 * Scan the list of modules if have the module already loaded.
211 for (mp = modList; mp; mp = mp->next)
212 if (strcmp(mp->name, path) == 0) {
216 Newz(1000,mp,1,Module);
219 strcpy(errbuf, "Newz: ");
220 strerrorcat(errbuf, errno);
224 if ((mp->name = savepv(path)) == NULL) {
226 strcpy(errbuf, "savepv: ");
227 strerrorcat(errbuf, errno);
233 * load should be declared load(const char *...). Thus we
234 * cast the path to a normal char *. Ugly.
236 if ((mp->entry = (void *)LOAD((char *)path,
237 #ifdef L_LIBPATH_EXEC
242 int saverrno = errno;
247 strcpy(errbuf, "dlopen: ");
248 strcat(errbuf, path);
249 strcat(errbuf, ": ");
251 * If AIX says the file is not executable, the error
252 * can be further described by querying the loader about
255 if (saverrno == ENOEXEC) {
256 char *moreinfo[BUFSIZ/sizeof(char *)];
257 if (loadquery(L_GETMESSAGES, moreinfo, sizeof(moreinfo)) == -1)
258 strerrorcpy(errbuf, saverrno);
261 for (p = moreinfo; *p; p++)
265 strerrorcat(errbuf, saverrno);
272 * Assume anonymous exports come from the module this dlopen
273 * is linked into, that holds true as long as dlopen and all
274 * of the perl core are in the same shared object. Also bind
275 * against the main part, in the case a perl is not the main
276 * part, e.g mod_perl as DSO in Apache so perl modules can
277 * also reference Apache symbols.
279 if (loadbind(0, (void *)dlopen, mp->entry) == -1 ||
280 loadbind(0, mainModule, mp->entry)) {
281 int saverrno = errno;
285 strcpy(errbuf, "loadbind: ");
286 strerrorcat(errbuf, saverrno);
289 if (readExports(mp) == -1) {
297 * Attempt to decipher an AIX loader error message and append it
298 * to our static error message buffer.
300 static void caterr(char *s)
302 register char *p = s;
304 while (*p >= '0' && *p <= '9')
307 case L_ERROR_TOOMANY:
308 strcat(errbuf, "too many errors");
311 strcat(errbuf, "can't load library");
315 strcat(errbuf, "can't find symbol");
319 strcat(errbuf, "bad RLD");
323 strcat(errbuf, "bad exec format in");
327 strerrorcat(errbuf, atoi(++p));
335 void *dlsym(void *handle, const char *symbol)
337 register ModulePtr mp = (ModulePtr)handle;
338 register ExportPtr ep;
342 * Could speed up search, but I assume that one assigns
343 * the result to function pointers anyways.
345 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
346 if (strcmp(ep->name, symbol) == 0)
349 strcpy(errbuf, "dlsym: undefined symbol ");
350 strcat(errbuf, symbol);
363 int dlclose(void *handle)
365 register ModulePtr mp = (ModulePtr)handle;
367 register ModulePtr mp1;
369 if (--mp->refCnt > 0)
371 result = UNLOAD(mp->entry);
374 strerrorcpy(errbuf, errno);
377 register ExportPtr ep;
379 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
382 safefree(mp->exports);
387 for (mp1 = modList; mp1; mp1 = mp1->next)
388 if (mp1->next == mp) {
389 mp1->next = mp->next;
398 /* Added by Wayne Scott
399 * This is needed because the ldopen system call calls
400 * calloc to allocated a block of date. The ldclose call calls free.
401 * Without this we get this system calloc and perl's free, resulting
402 * in a "Bad free" message. This way we always use perl's malloc.
404 void *calloc(size_t ne, size_t sz)
408 out = (void *) safemalloc(ne*sz);
414 * Build the export table from the XCOFF .loader section.
416 static int readExports(ModulePtr mp)
427 if ((ldp = ldopen(mp->name, ldp)) == NULL) {
431 if (errno != ENOENT) {
433 strcpy(errbuf, "readExports: ");
434 strerrorcat(errbuf, errno);
438 * The module might be loaded due to the LIBPATH
439 * environment variable. Search for the loaded
440 * module using L_GETINFO.
442 if ((buf = safemalloc(size)) == NULL) {
444 strcpy(errbuf, "readExports: ");
445 strerrorcat(errbuf, errno);
448 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
451 if ((buf = safemalloc(size)) == NULL) {
453 strcpy(errbuf, "readExports: ");
454 strerrorcat(errbuf, errno);
460 strcpy(errbuf, "readExports: ");
461 strerrorcat(errbuf, errno);
466 * Traverse the list of loaded modules. The entry point
467 * returned by LOAD() does actually point to the data
470 lp = (struct ld_info *)buf;
472 if (lp->ldinfo_dataorg == mp->entry) {
473 ldp = ldopen(lp->ldinfo_filename, ldp);
476 if (lp->ldinfo_next == 0)
479 lp = (struct ld_info *)((char *)lp + lp->ldinfo_next);
484 strcpy(errbuf, "readExports: ");
485 strerrorcat(errbuf, errno);
489 #ifdef USE_64_BIT_ALL
490 if (TYPE(ldp) != U803XTOCMAGIC) {
492 if (TYPE(ldp) != U802TOCMAGIC) {
495 strcpy(errbuf, "readExports: bad magic");
496 while(ldclose(ldp) == FAILURE)
500 if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
502 strcpy(errbuf, "readExports: cannot read loader section header");
503 while(ldclose(ldp) == FAILURE)
508 * We read the complete loader section in one chunk, this makes
509 * finding long symbol names residing in the string table easier.
511 if ((ldbuf = (char *)safemalloc(sh.s_size)) == NULL) {
513 strcpy(errbuf, "readExports: ");
514 strerrorcat(errbuf, errno);
515 while(ldclose(ldp) == FAILURE)
519 if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
521 strcpy(errbuf, "readExports: cannot seek to loader section");
523 while(ldclose(ldp) == FAILURE)
527 /* This first case is a hack, since it assumes that the 3rd parameter to
528 FREAD is 1. See the redefinition of FREAD above to see how this works. */
529 if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
531 strcpy(errbuf, "readExports: cannot read loader section");
533 while(ldclose(ldp) == FAILURE)
537 lhp = (AIX_LDHDR *)ldbuf;
538 ls = (AIX_LDSYM *)(ldbuf+AIX_LDHDRSZ);
540 * Count the number of exports to include in our export table.
542 for (i = lhp->l_nsyms; i; i--, ls++) {
543 if (!LDR_EXPORT(*ls))
547 Newz(1001, mp->exports, mp->nExports, Export);
548 if (mp->exports == NULL) {
550 strcpy(errbuf, "readExports: ");
551 strerrorcat(errbuf, errno);
553 while(ldclose(ldp) == FAILURE)
558 * Fill in the export table. All entries are relative to
559 * the entry point we got from load.
562 ls = (AIX_LDSYM *)(ldbuf+AIX_LDHDRSZ);
563 for (i = lhp->l_nsyms; i; i--, ls++) {
565 if (!LDR_EXPORT(*ls))
567 #ifndef USE_64_BIT_ALL
568 if (ls->l_zeroes == 0)
570 symname = ls->l_offset+lhp->l_stoff+ldbuf;
571 #ifndef USE_64_BIT_ALL
573 symname = ls->l_name;
575 ep->name = savepv(symname);
576 ep->addr = (void *)((unsigned long)mp->entry + ls->l_value);
580 while(ldclose(ldp) == FAILURE)
586 * Find the main modules entry point. This is used as export pointer
587 * for loadbind() to be able to resolve references to the main part.
589 static void * findMain(void)
597 if ((buf = safemalloc(size)) == NULL) {
599 strcpy(errbuf, "findMain: ");
600 strerrorcat(errbuf, errno);
603 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
606 if ((buf = safemalloc(size)) == NULL) {
608 strcpy(errbuf, "findMain: ");
609 strerrorcat(errbuf, errno);
615 strcpy(errbuf, "findMain: ");
616 strerrorcat(errbuf, errno);
621 * The first entry is the main module. The entry point
622 * returned by load() does actually point to the data
625 lp = (struct ld_info *)buf;
626 ret = lp->ldinfo_dataorg;
633 * Platform: SunOS/Solaris, possibly others which use dlopen.
634 * Author: Paul Marquess (Paul.Marquess@btinternet.com)
635 * Created: 10th July 1994
638 * 15th July 1994 - Added code to explicitly save any error messages.
639 * 3rd August 1994 - Upgraded to v3 spec.
640 * 9th August 1994 - Changed to use IV
641 * 10th August 1994 - Tim Bunce: Added RTLD_LAZY, switchable debugging,
642 * basic FreeBSD support, removed ClearError
652 #include "dlutils.c" /* SaveError() etc */
656 dl_private_init(pTHX)
658 (void)dl_generic_private_init(aTHX);
661 MODULE = DynaLoader PACKAGE = DynaLoader
664 (void)dl_private_init(aTHX);
668 dl_load_file(filename, flags=0)
672 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags));
674 Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
675 RETVAL = dlopen(filename, 1) ;
676 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%x\n", RETVAL));
677 ST(0) = sv_newmortal() ;
679 SaveError(aTHX_ "%s",dlerror()) ;
681 sv_setiv( ST(0), PTR2IV(RETVAL) );
684 dl_unload_file(libref)
687 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_unload_file(%lx):\n", libref));
688 RETVAL = (dlclose(libref) == 0 ? 1 : 0);
690 SaveError(aTHX_ "%s", dlerror()) ;
691 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " retval = %d\n", RETVAL));
696 dl_find_symbol(libhandle, symbolname)
700 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_find_symbol(handle=%x, symbol=%s)\n",
701 libhandle, symbolname));
702 RETVAL = dlsym(libhandle, symbolname);
703 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " symbolref = %x\n", RETVAL));
704 ST(0) = sv_newmortal() ;
706 SaveError(aTHX_ "%s",dlerror()) ;
708 sv_setiv( ST(0), PTR2IV(RETVAL));
717 # These functions should not need changing on any platform:
720 dl_install_xsub(perl_name, symref, filename="$Package")
725 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n",
727 ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
728 (void(*)(pTHX_ CV *))symref,