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>
33 * AIX 4.3 does remove some useful definitions from ldfcn.h. Define
34 * these here to compensate for that lossage.
37 # define BEGINNING SEEK_SET
40 # define FSEEK(ldptr,o,p) fseek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr) +o):o,p)
43 # define FREAD(p,s,n,ldptr) fread(p,s,n,IOPTR(ldptr))
46 /* If using PerlIO, redefine these macros from <ldfcn.h> */
48 #define FSEEK(ldptr,o,p) PerlIO_seek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr)+o):o,p)
49 #define FREAD(p,s,n,ldptr) PerlIO_read(IOPTR(ldptr),p,s*n)
53 * We simulate dlopen() et al. through a call to load. Because AIX has
54 * no call to find an exported symbol we read the loader section of the
55 * loaded module and build a list of exported symbols and their virtual
60 char *name; /* the symbols's name */
61 void *addr; /* its relocated virtual address */
65 * The void * handle returned from dlopen is actually a ModulePtr.
67 typedef struct Module {
69 char *name; /* module name for refcounting */
70 int refCnt; /* the number of references */
71 void *entry; /* entry point from load */
72 int nExports; /* the number of exports found */
73 ExportPtr exports; /* the array of exports */
77 * We keep a list of all loaded modules to be able to call the fini
78 * handlers at atexit() time.
80 static ModulePtr modList; /* XXX threadead */
83 * The last error from one of the dl* routines is kept in static
84 * variables here. Each error is returned only once to the caller.
86 static char errbuf[BUFSIZ]; /* XXX threadead */
87 static int errvalid; /* XXX threadead */
89 static void caterr(char *);
90 static int readExports(ModulePtr);
91 static void terminate(void);
93 static char *strerror_failed = "(strerror failed)";
94 static char *strerror_r_failed = "(strerror_r failed)";
96 char *strerrorcat(char *str, int err) {
97 int strsiz = strlen(str);
102 char *buf = malloc(BUFSIZ);
106 if (strerror_r(err, buf, sizeof(buf)) == 0)
109 msg = strerror_r_failed;
110 msgsiz = strlen(msg);
111 if (strsiz + msgsiz < BUFSIZ)
115 if ((msg = strerror(err)) == 0)
116 msg = strerror_failed;
117 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
118 if (strsiz + msgsiz < BUFSIZ) /* Do not move this after #endif. */
125 char *strerrorcpy(char *str, int err) {
130 char *buf = malloc(BUFSIZ);
134 if (strerror_r(err, buf, sizeof(buf)) == 0)
137 msg = strerror_r_failed;
138 msgsiz = strlen(msg);
143 if ((msg = strerror(err)) == 0)
144 msg = strerror_failed;
145 msgsiz = strlen(msg); /* Note msg = buf and free() above. */
146 if (msgsiz < BUFSIZ) /* Do not move this after #endif. */
154 void *dlopen(char *path, int mode)
156 register ModulePtr mp;
157 static int inited; /* XXX threadead */
160 * Upon the first call register a terminate handler that will
161 * close all libraries.
168 * Scan the list of modules if have the module already loaded.
170 for (mp = modList; mp; mp = mp->next)
171 if (strcmp(mp->name, path) == 0) {
175 Newz(1000,mp,1,Module);
178 strcpy(errbuf, "Newz: ");
179 strerrorcat(errbuf, errno);
183 if ((mp->name = savepv(path)) == NULL) {
185 strcpy(errbuf, "savepv: ");
186 strerrorcat(errbuf, errno);
191 * load should be declared load(const char *...). Thus we
192 * cast the path to a normal char *. Ugly.
194 if ((mp->entry = (void *)load((char *)path, L_NOAUTODEFER, NULL)) == NULL) {
198 strcpy(errbuf, "dlopen: ");
199 strcat(errbuf, path);
200 strcat(errbuf, ": ");
202 * If AIX says the file is not executable, the error
203 * can be further described by querying the loader about
206 if (errno == ENOEXEC) {
207 char *tmp[BUFSIZ/sizeof(char *)];
208 if (loadquery(L_GETMESSAGES, tmp, sizeof(tmp)) == -1)
209 strerrorcpy(errbuf, errno);
212 for (p = tmp; *p; p++)
216 strerrorcat(errbuf, errno);
223 * Assume anonymous exports come from the module this dlopen
224 * is linked into, that holds true as long as dlopen and all
225 * of the perl core are in the same shared object.
227 if (loadbind(0, (void *)dlopen, mp->entry) == -1) {
230 strcpy(errbuf, "loadbind: ");
231 strerrorcat(errbuf, errno);
234 if (readExports(mp) == -1) {
242 * Attempt to decipher an AIX loader error message and append it
243 * to our static error message buffer.
245 static void caterr(char *s)
247 register char *p = s;
249 while (*p >= '0' && *p <= '9')
252 case L_ERROR_TOOMANY:
253 strcat(errbuf, "to many errors");
256 strcat(errbuf, "can't load library");
260 strcat(errbuf, "can't find symbol");
264 strcat(errbuf, "bad RLD");
268 strcat(errbuf, "bad exec format in");
272 strerrorcat(errbuf, atoi(++p));
280 void *dlsym(void *handle, const char *symbol)
282 register ModulePtr mp = (ModulePtr)handle;
283 register ExportPtr ep;
287 * Could speed up search, but I assume that one assigns
288 * the result to function pointers anyways.
290 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
291 if (strcmp(ep->name, symbol) == 0)
294 strcpy(errbuf, "dlsym: undefined symbol ");
295 strcat(errbuf, symbol);
308 int dlclose(void *handle)
310 register ModulePtr mp = (ModulePtr)handle;
312 register ModulePtr mp1;
314 if (--mp->refCnt > 0)
316 result = unload(mp->entry);
319 strerrorcpy(errbuf, errno);
322 register ExportPtr ep;
324 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
327 safefree(mp->exports);
332 for (mp1 = modList; mp1; mp1 = mp1->next)
333 if (mp1->next == mp) {
334 mp1->next = mp->next;
343 static void terminate(void)
349 /* Added by Wayne Scott
350 * This is needed because the ldopen system call calls
351 * calloc to allocated a block of date. The ldclose call calls free.
352 * Without this we get this system calloc and perl's free, resulting
353 * in a "Bad free" message. This way we always use perl's malloc.
355 void *calloc(size_t ne, size_t sz)
359 out = (void *) safemalloc(ne*sz);
365 * Build the export table from the XCOFF .loader section.
367 static int readExports(ModulePtr mp)
377 if ((ldp = ldopen(mp->name, ldp)) == NULL) {
381 if (errno != ENOENT) {
383 strcpy(errbuf, "readExports: ");
384 strerrorcat(errbuf, errno);
388 * The module might be loaded due to the LIBPATH
389 * environment variable. Search for the loaded
390 * module using L_GETINFO.
392 if ((buf = safemalloc(size)) == NULL) {
394 strcpy(errbuf, "readExports: ");
395 strerrorcat(errbuf, errno);
398 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
401 if ((buf = safemalloc(size)) == NULL) {
403 strcpy(errbuf, "readExports: ");
404 strerrorcat(errbuf, errno);
410 strcpy(errbuf, "readExports: ");
411 strerrorcat(errbuf, errno);
416 * Traverse the list of loaded modules. The entry point
417 * returned by load() does actually point to the data
420 lp = (struct ld_info *)buf;
422 if (lp->ldinfo_dataorg == mp->entry) {
423 ldp = ldopen(lp->ldinfo_filename, ldp);
426 if (lp->ldinfo_next == 0)
429 lp = (struct ld_info *)((char *)lp + lp->ldinfo_next);
434 strcpy(errbuf, "readExports: ");
435 strerrorcat(errbuf, errno);
439 if (TYPE(ldp) != U802TOCMAGIC) {
441 strcpy(errbuf, "readExports: bad magic");
442 while(ldclose(ldp) == FAILURE)
446 if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
448 strcpy(errbuf, "readExports: cannot read loader section header");
449 while(ldclose(ldp) == FAILURE)
454 * We read the complete loader section in one chunk, this makes
455 * finding long symbol names residing in the string table easier.
457 if ((ldbuf = (char *)safemalloc(sh.s_size)) == NULL) {
459 strcpy(errbuf, "readExports: ");
460 strerrorcat(errbuf, errno);
461 while(ldclose(ldp) == FAILURE)
465 if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
467 strcpy(errbuf, "readExports: cannot seek to loader section");
469 while(ldclose(ldp) == FAILURE)
473 /* This first case is a hack, since it assumes that the 3rd parameter to
474 FREAD is 1. See the redefinition of FREAD above to see how this works. */
476 if (FREAD(ldbuf, sh.s_size, 1, ldp) != sh.s_size) {
478 if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
481 strcpy(errbuf, "readExports: cannot read loader section");
483 while(ldclose(ldp) == FAILURE)
487 lhp = (LDHDR *)ldbuf;
488 ls = (LDSYM *)(ldbuf+LDHDRSZ);
490 * Count the number of exports to include in our export table.
492 for (i = lhp->l_nsyms; i; i--, ls++) {
493 if (!LDR_EXPORT(*ls))
497 Newz(1001, mp->exports, mp->nExports, Export);
498 if (mp->exports == NULL) {
500 strcpy(errbuf, "readExports: ");
501 strerrorcat(errbuf, errno);
503 while(ldclose(ldp) == FAILURE)
508 * Fill in the export table. All entries are relative to
509 * the entry point we got from load.
512 ls = (LDSYM *)(ldbuf+LDHDRSZ);
513 for (i = lhp->l_nsyms; i; i--, ls++) {
515 if (!LDR_EXPORT(*ls))
517 if (ls->l_zeroes == 0)
518 symname = ls->l_offset+lhp->l_stoff+ldbuf;
520 symname = ls->l_name;
521 ep->name = savepv(symname);
522 ep->addr = (void *)((unsigned long)mp->entry + ls->l_value);
526 while(ldclose(ldp) == FAILURE)
533 * Platform: SunOS/Solaris, possibly others which use dlopen.
534 * Author: Paul Marquess (pmarquess@bfsec.bt.co.uk)
535 * Created: 10th July 1994
538 * 15th July 1994 - Added code to explicitly save any error messages.
539 * 3rd August 1994 - Upgraded to v3 spec.
540 * 9th August 1994 - Changed to use IV
541 * 10th August 1994 - Tim Bunce: Added RTLD_LAZY, switchable debugging,
542 * basic FreeBSD support, removed ClearError
552 #include "dlutils.c" /* SaveError() etc */
556 dl_private_init(pTHX)
558 (void)dl_generic_private_init(aTHX);
561 MODULE = DynaLoader PACKAGE = DynaLoader
564 (void)dl_private_init(aTHX);
568 dl_load_file(filename, flags=0)
572 DLDEBUG(1,PerlIO_printf(PerlIO_stderr(), "dl_load_file(%s,%x):\n", filename,flags));
574 Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
575 RETVAL = dlopen(filename, 1) ;
576 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), " libref=%x\n", RETVAL));
577 ST(0) = sv_newmortal() ;
579 SaveError(aTHX_ "%s",dlerror()) ;
581 sv_setiv( ST(0), (IV)RETVAL);
585 dl_find_symbol(libhandle, symbolname)
589 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_find_symbol(handle=%x, symbol=%s)\n",
590 libhandle, symbolname));
591 RETVAL = dlsym(libhandle, symbolname);
592 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), " symbolref = %x\n", RETVAL));
593 ST(0) = sv_newmortal() ;
595 SaveError(aTHX_ "%s",dlerror()) ;
597 sv_setiv( ST(0), (IV)RETVAL);
606 # These functions should not need changing on any platform:
609 dl_install_xsub(perl_name, symref, filename="$Package")
614 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_install_xsub(name=%s, symref=%x)\n",
616 ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
617 (void(*)(pTHX_ CV *))symref,