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.
15 * @(#)dlfcn.c 1.5 revision of 93/02/14 20:14:17
16 * This is an unpublished work copyright (c) 1992 Helios Software GmbH
17 * 3000 Hannover 1, Germany
27 #include <sys/types.h>
32 /* When using Perl extensions written in C++ the longer versions
33 * of load() and unload() from libC and libC_r need to be used,
34 * otherwise statics in the extensions won't get initialized right.
35 * -- Stephanie Beals <bealzy@us.ibm.com> */
36 #ifdef USE_libC /* The define comes, when it comes, from hints/aix.pl. */
37 # define LOAD loadAndInit
38 # define UNLOAD terminateAndUnload
44 # define UNLOAD unload
48 * AIX 4.3 does remove some useful definitions from ldfcn.h. Define
49 * these here to compensate for that lossage.
52 # define BEGINNING SEEK_SET
55 # define FSEEK(ldptr,o,p) fseek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr) +o):o,p)
58 # define FREAD(p,s,n,ldptr) fread(p,s,n,IOPTR(ldptr))
61 /* If using PerlIO, redefine these macros from <ldfcn.h> */
63 #define FSEEK(ldptr,o,p) PerlIO_seek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr)+o):o,p)
64 #define FREAD(p,s,n,ldptr) PerlIO_read(IOPTR(ldptr),p,s*n)
68 * We simulate dlopen() et al. through a call to load. Because AIX has
69 * no call to find an exported symbol we read the loader section of the
70 * loaded module and build a list of exported symbols and their virtual
75 char *name; /* the symbols's name */
76 void *addr; /* its relocated virtual address */
80 * The void * handle returned from dlopen is actually a ModulePtr.
82 typedef struct Module {
84 char *name; /* module name for refcounting */
85 int refCnt; /* the number of references */
86 void *entry; /* entry point from load */
87 int nExports; /* the number of exports found */
88 ExportPtr exports; /* the array of exports */
92 * We keep a list of all loaded modules to be able to call the fini
93 * handlers at atexit() time.
95 static ModulePtr modList; /* XXX threaded */
98 * The last error from one of the dl* routines is kept in static
99 * variables here. Each error is returned only once to the caller.
101 static char errbuf[BUFSIZ]; /* XXX threaded */
102 static int errvalid; /* XXX threaded */
104 static void caterr(char *);
105 static int readExports(ModulePtr);
106 static void terminate(void);
108 static char *strerror_failed = "(strerror failed)";
109 static char *strerror_r_failed = "(strerror_r failed)";
111 char *strerrorcat(char *str, int err) {
112 int strsiz = strlen(str);
117 char *buf = malloc(BUFSIZ);
121 if (strerror_r(err, buf, BUFSIZ) == 0)
124 msg = strerror_r_failed;
125 msgsiz = strlen(msg);
126 if (strsiz + msgsiz < BUFSIZ)
130 if ((msg = strerror(err)) == 0)
131 msg = strerror_failed;
132 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
133 if (strsiz + msgsiz < BUFSIZ) /* Do not move this after #endif. */
140 char *strerrorcpy(char *str, int err) {
145 char *buf = malloc(BUFSIZ);
149 if (strerror_r(err, buf, BUFSIZ) == 0)
152 msg = strerror_r_failed;
153 msgsiz = strlen(msg);
158 if ((msg = strerror(err)) == 0)
159 msg = strerror_failed;
160 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
161 if (msgsiz < BUFSIZ) /* Do not move this after #endif. */
169 void *dlopen(char *path, int mode)
172 register ModulePtr mp;
173 static int inited; /* XXX threaded */
176 * Upon the first call register a terminate handler that will
177 * close all libraries.
184 * Scan the list of modules if have the module already loaded.
186 for (mp = modList; mp; mp = mp->next)
187 if (strcmp(mp->name, path) == 0) {
191 Newz(1000,mp,1,Module);
194 strcpy(errbuf, "Newz: ");
195 strerrorcat(errbuf, errno);
199 if ((mp->name = savepv(path)) == NULL) {
201 strcpy(errbuf, "savepv: ");
202 strerrorcat(errbuf, errno);
208 * load should be declared load(const char *...). Thus we
209 * cast the path to a normal char *. Ugly.
211 if ((mp->entry = (void *)LOAD((char *)path,
212 #ifdef L_LIBPATH_EXEC
217 int saverrno = errno;
222 strcpy(errbuf, "dlopen: ");
223 strcat(errbuf, path);
224 strcat(errbuf, ": ");
226 * If AIX says the file is not executable, the error
227 * can be further described by querying the loader about
230 if (saverrno == ENOEXEC) {
231 char *moreinfo[BUFSIZ/sizeof(char *)];
232 if (loadquery(L_GETMESSAGES, moreinfo, sizeof(moreinfo)) == -1)
233 strerrorcpy(errbuf, saverrno);
236 for (p = moreinfo; *p; p++)
240 strerrorcat(errbuf, saverrno);
247 * Assume anonymous exports come from the module this dlopen
248 * is linked into, that holds true as long as dlopen and all
249 * of the perl core are in the same shared object.
251 if (loadbind(0, (void *)dlopen, mp->entry) == -1) {
252 int saverrno = errno;
256 strcpy(errbuf, "loadbind: ");
257 strerrorcat(errbuf, saverrno);
260 if (readExports(mp) == -1) {
268 * Attempt to decipher an AIX loader error message and append it
269 * to our static error message buffer.
271 static void caterr(char *s)
273 register char *p = s;
275 while (*p >= '0' && *p <= '9')
278 case L_ERROR_TOOMANY:
279 strcat(errbuf, "to many errors");
282 strcat(errbuf, "can't load library");
286 strcat(errbuf, "can't find symbol");
290 strcat(errbuf, "bad RLD");
294 strcat(errbuf, "bad exec format in");
298 strerrorcat(errbuf, atoi(++p));
306 void *dlsym(void *handle, const char *symbol)
308 register ModulePtr mp = (ModulePtr)handle;
309 register ExportPtr ep;
313 * Could speed up search, but I assume that one assigns
314 * the result to function pointers anyways.
316 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
317 if (strcmp(ep->name, symbol) == 0)
320 strcpy(errbuf, "dlsym: undefined symbol ");
321 strcat(errbuf, symbol);
334 int dlclose(void *handle)
336 register ModulePtr mp = (ModulePtr)handle;
338 register ModulePtr mp1;
340 if (--mp->refCnt > 0)
342 result = UNLOAD(mp->entry);
345 strerrorcpy(errbuf, errno);
348 register ExportPtr ep;
350 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
353 safefree(mp->exports);
358 for (mp1 = modList; mp1; mp1 = mp1->next)
359 if (mp1->next == mp) {
360 mp1->next = mp->next;
369 static void terminate(void)
375 /* Added by Wayne Scott
376 * This is needed because the ldopen system call calls
377 * calloc to allocated a block of date. The ldclose call calls free.
378 * Without this we get this system calloc and perl's free, resulting
379 * in a "Bad free" message. This way we always use perl's malloc.
381 void *calloc(size_t ne, size_t sz)
385 out = (void *) safemalloc(ne*sz);
391 * Build the export table from the XCOFF .loader section.
393 static int readExports(ModulePtr mp)
404 if ((ldp = ldopen(mp->name, ldp)) == NULL) {
408 if (errno != ENOENT) {
410 strcpy(errbuf, "readExports: ");
411 strerrorcat(errbuf, errno);
415 * The module might be loaded due to the LIBPATH
416 * environment variable. Search for the loaded
417 * module using L_GETINFO.
419 if ((buf = safemalloc(size)) == NULL) {
421 strcpy(errbuf, "readExports: ");
422 strerrorcat(errbuf, errno);
425 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
428 if ((buf = safemalloc(size)) == NULL) {
430 strcpy(errbuf, "readExports: ");
431 strerrorcat(errbuf, errno);
437 strcpy(errbuf, "readExports: ");
438 strerrorcat(errbuf, errno);
443 * Traverse the list of loaded modules. The entry point
444 * returned by LOAD() does actually point to the data
447 lp = (struct ld_info *)buf;
449 if (lp->ldinfo_dataorg == mp->entry) {
450 ldp = ldopen(lp->ldinfo_filename, ldp);
453 if (lp->ldinfo_next == 0)
456 lp = (struct ld_info *)((char *)lp + lp->ldinfo_next);
461 strcpy(errbuf, "readExports: ");
462 strerrorcat(errbuf, errno);
466 if (TYPE(ldp) != U802TOCMAGIC) {
468 strcpy(errbuf, "readExports: bad magic");
469 while(ldclose(ldp) == FAILURE)
473 if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
475 strcpy(errbuf, "readExports: cannot read loader section header");
476 while(ldclose(ldp) == FAILURE)
481 * We read the complete loader section in one chunk, this makes
482 * finding long symbol names residing in the string table easier.
484 if ((ldbuf = (char *)safemalloc(sh.s_size)) == NULL) {
486 strcpy(errbuf, "readExports: ");
487 strerrorcat(errbuf, errno);
488 while(ldclose(ldp) == FAILURE)
492 if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
494 strcpy(errbuf, "readExports: cannot seek to loader section");
496 while(ldclose(ldp) == FAILURE)
500 /* This first case is a hack, since it assumes that the 3rd parameter to
501 FREAD is 1. See the redefinition of FREAD above to see how this works. */
503 if (FREAD(ldbuf, sh.s_size, 1, ldp) != sh.s_size) {
505 if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
508 strcpy(errbuf, "readExports: cannot read loader section");
510 while(ldclose(ldp) == FAILURE)
514 lhp = (LDHDR *)ldbuf;
515 ls = (LDSYM *)(ldbuf+LDHDRSZ);
517 * Count the number of exports to include in our export table.
519 for (i = lhp->l_nsyms; i; i--, ls++) {
520 if (!LDR_EXPORT(*ls))
524 Newz(1001, mp->exports, mp->nExports, Export);
525 if (mp->exports == NULL) {
527 strcpy(errbuf, "readExports: ");
528 strerrorcat(errbuf, errno);
530 while(ldclose(ldp) == FAILURE)
535 * Fill in the export table. All entries are relative to
536 * the entry point we got from load.
539 ls = (LDSYM *)(ldbuf+LDHDRSZ);
540 for (i = lhp->l_nsyms; i; i--, ls++) {
542 if (!LDR_EXPORT(*ls))
544 if (ls->l_zeroes == 0)
545 symname = ls->l_offset+lhp->l_stoff+ldbuf;
547 symname = ls->l_name;
548 ep->name = savepv(symname);
549 ep->addr = (void *)((unsigned long)mp->entry + ls->l_value);
553 while(ldclose(ldp) == FAILURE)
560 * Platform: SunOS/Solaris, possibly others which use dlopen.
561 * Author: Paul Marquess (pmarquess@bfsec.bt.co.uk)
562 * Created: 10th July 1994
565 * 15th July 1994 - Added code to explicitly save any error messages.
566 * 3rd August 1994 - Upgraded to v3 spec.
567 * 9th August 1994 - Changed to use IV
568 * 10th August 1994 - Tim Bunce: Added RTLD_LAZY, switchable debugging,
569 * basic FreeBSD support, removed ClearError
579 #include "dlutils.c" /* SaveError() etc */
583 dl_private_init(pTHX)
585 (void)dl_generic_private_init(aTHX);
588 MODULE = DynaLoader PACKAGE = DynaLoader
591 (void)dl_private_init(aTHX);
595 dl_load_file(filename, flags=0)
599 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags));
601 Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
602 RETVAL = dlopen(filename, 1) ;
603 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%x\n", RETVAL));
604 ST(0) = sv_newmortal() ;
606 SaveError(aTHX_ "%s",dlerror()) ;
608 sv_setiv( ST(0), PTR2IV(RETVAL) );
612 dl_find_symbol(libhandle, symbolname)
616 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_find_symbol(handle=%x, symbol=%s)\n",
617 libhandle, symbolname));
618 RETVAL = dlsym(libhandle, symbolname);
619 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " symbolref = %x\n", RETVAL));
620 ST(0) = sv_newmortal() ;
622 SaveError(aTHX_ "%s",dlerror()) ;
624 sv_setiv( ST(0), PTR2IV(RETVAL));
633 # These functions should not need changing on any platform:
636 dl_install_xsub(perl_name, symref, filename="$Package")
641 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n",
643 ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
644 (void(*)(pTHX_ CV *))symref,