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 * We simulate dlopen() et al. through a call to load. Because AIX has
34 * no call to find an exported symbol we read the loader section of the
35 * loaded module and build a list of exported symbols and their virtual
40 char *name; /* the symbols's name */
41 void *addr; /* its relocated virtual address */
45 * The void * handle returned from dlopen is actually a ModulePtr.
47 typedef struct Module {
49 char *name; /* module name for refcounting */
50 int refCnt; /* the number of references */
51 void *entry; /* entry point from load */
52 int nExports; /* the number of exports found */
53 ExportPtr exports; /* the array of exports */
57 * We keep a list of all loaded modules to be able to call the fini
58 * handlers at atexit() time.
60 static ModulePtr modList;
63 * The last error from one of the dl* routines is kept in static
64 * variables here. Each error is returned only once to the caller.
66 static char errbuf[BUFSIZ];
69 static void caterr(char *);
70 static int readExports(ModulePtr);
71 static void terminate(void);
72 static void *findMain(void);
76 void *dlopen(char *path, int mode)
78 register ModulePtr mp;
79 static void *mainModule;
82 * Upon the first call register a terminate handler that will
83 * close all libraries. Also get a reference to the main module
84 * for use with loadbind.
87 if ((mainModule = findMain()) == NULL)
92 * Scan the list of modules if have the module already loaded.
94 for (mp = modList; mp; mp = mp->next)
95 if (strcmp(mp->name, path) == 0) {
99 Newz(1000,mp,1,Module);
102 strcpy(errbuf, "Newz: ");
103 strcat(errbuf, strerror(errno));
107 if ((mp->name = savepv(path)) == NULL) {
109 strcpy(errbuf, "savepv: ");
110 strcat(errbuf, strerror(errno));
115 * load should be declared load(const char *...). Thus we
116 * cast the path to a normal char *. Ugly.
118 if ((mp->entry = (void *)load((char *)path, L_NOAUTODEFER, NULL)) == NULL) {
122 strcpy(errbuf, "dlopen: ");
123 strcat(errbuf, path);
124 strcat(errbuf, ": ");
126 * If AIX says the file is not executable, the error
127 * can be further described by querying the loader about
130 if (errno == ENOEXEC) {
131 char *tmp[BUFSIZ/sizeof(char *)];
132 if (loadquery(L_GETMESSAGES, tmp, sizeof(tmp)) == -1)
133 strcpy(errbuf, strerror(errno));
136 for (p = tmp; *p; p++)
140 strcat(errbuf, strerror(errno));
146 if (loadbind(0, mainModule, mp->entry) == -1) {
149 strcpy(errbuf, "loadbind: ");
150 strcat(errbuf, strerror(errno));
153 if (readExports(mp) == -1) {
161 * Attempt to decipher an AIX loader error message and append it
162 * to our static error message buffer.
164 static void caterr(char *s)
166 register char *p = s;
168 while (*p >= '0' && *p <= '9')
171 case L_ERROR_TOOMANY:
172 strcat(errbuf, "to many errors");
175 strcat(errbuf, "can't load library");
179 strcat(errbuf, "can't find symbol");
183 strcat(errbuf, "bad RLD");
187 strcat(errbuf, "bad exec format in");
191 strcat(errbuf, strerror(atoi(++p)));
199 void *dlsym(void *handle, const char *symbol)
201 register ModulePtr mp = (ModulePtr)handle;
202 register ExportPtr ep;
206 * Could speed up search, but I assume that one assigns
207 * the result to function pointers anyways.
209 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
210 if (strcmp(ep->name, symbol) == 0)
213 strcpy(errbuf, "dlsym: undefined symbol ");
214 strcat(errbuf, symbol);
227 int dlclose(void *handle)
229 register ModulePtr mp = (ModulePtr)handle;
231 register ModulePtr mp1;
233 if (--mp->refCnt > 0)
235 result = unload(mp->entry);
238 strcpy(errbuf, strerror(errno));
241 register ExportPtr ep;
243 for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
246 safefree(mp->exports);
251 for (mp1 = modList; mp1; mp1 = mp1->next)
252 if (mp1->next == mp) {
253 mp1->next = mp->next;
262 static void terminate(void)
268 /* Added by Wayne Scott
269 * This is needed because the ldopen system call calls
270 * calloc to allocated a block of date. The ldclose call calls free.
271 * Without this we get this system calloc and perl's free, resulting
272 * in a "Bad free" message. This way we always use perl's malloc.
274 void *calloc(size_t ne, size_t sz)
278 out = (void *) safemalloc(ne*sz);
284 * Build the export table from the XCOFF .loader section.
286 static int readExports(ModulePtr mp)
296 if ((ldp = ldopen(mp->name, ldp)) == NULL) {
300 if (errno != ENOENT) {
302 strcpy(errbuf, "readExports: ");
303 strcat(errbuf, strerror(errno));
307 * The module might be loaded due to the LIBPATH
308 * environment variable. Search for the loaded
309 * module using L_GETINFO.
311 if ((buf = safemalloc(size)) == NULL) {
313 strcpy(errbuf, "readExports: ");
314 strcat(errbuf, strerror(errno));
317 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
320 if ((buf = safemalloc(size)) == NULL) {
322 strcpy(errbuf, "readExports: ");
323 strcat(errbuf, strerror(errno));
329 strcpy(errbuf, "readExports: ");
330 strcat(errbuf, strerror(errno));
335 * Traverse the list of loaded modules. The entry point
336 * returned by load() does actually point to the data
339 lp = (struct ld_info *)buf;
341 if (lp->ldinfo_dataorg == mp->entry) {
342 ldp = ldopen(lp->ldinfo_filename, ldp);
345 if (lp->ldinfo_next == 0)
348 lp = (struct ld_info *)((char *)lp + lp->ldinfo_next);
353 strcpy(errbuf, "readExports: ");
354 strcat(errbuf, strerror(errno));
358 if (TYPE(ldp) != U802TOCMAGIC) {
360 strcpy(errbuf, "readExports: bad magic");
361 while(ldclose(ldp) == FAILURE)
365 if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
367 strcpy(errbuf, "readExports: cannot read loader section header");
368 while(ldclose(ldp) == FAILURE)
373 * We read the complete loader section in one chunk, this makes
374 * finding long symbol names residing in the string table easier.
376 if ((ldbuf = (char *)safemalloc(sh.s_size)) == NULL) {
378 strcpy(errbuf, "readExports: ");
379 strcat(errbuf, strerror(errno));
380 while(ldclose(ldp) == FAILURE)
384 if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
386 strcpy(errbuf, "readExports: cannot seek to loader section");
388 while(ldclose(ldp) == FAILURE)
392 if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
394 strcpy(errbuf, "readExports: cannot read loader section");
396 while(ldclose(ldp) == FAILURE)
400 lhp = (LDHDR *)ldbuf;
401 ls = (LDSYM *)(ldbuf+LDHDRSZ);
403 * Count the number of exports to include in our export table.
405 for (i = lhp->l_nsyms; i; i--, ls++) {
406 if (!LDR_EXPORT(*ls))
410 Newz(1001, mp->exports, mp->nExports, Export);
411 if (mp->exports == NULL) {
413 strcpy(errbuf, "readExports: ");
414 strcat(errbuf, strerror(errno));
416 while(ldclose(ldp) == FAILURE)
421 * Fill in the export table. All entries are relative to
422 * the entry point we got from load.
425 ls = (LDSYM *)(ldbuf+LDHDRSZ);
426 for (i = lhp->l_nsyms; i; i--, ls++) {
428 if (!LDR_EXPORT(*ls))
430 if (ls->l_zeroes == 0)
431 symname = ls->l_offset+lhp->l_stoff+ldbuf;
433 symname = ls->l_name;
434 ep->name = savepv(symname);
435 ep->addr = (void *)((unsigned long)mp->entry + ls->l_value);
439 while(ldclose(ldp) == FAILURE)
445 * Find the main modules entry point. This is used as export pointer
446 * for loadbind() to be able to resolve references to the main part.
448 static void * findMain(void)
456 if ((buf = safemalloc(size)) == NULL) {
458 strcpy(errbuf, "findMain: ");
459 strcat(errbuf, strerror(errno));
462 while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
465 if ((buf = safemalloc(size)) == NULL) {
467 strcpy(errbuf, "findMain: ");
468 strcat(errbuf, strerror(errno));
474 strcpy(errbuf, "findMain: ");
475 strcat(errbuf, strerror(errno));
480 * The first entry is the main module. The entry point
481 * returned by load() does actually point to the data
484 lp = (struct ld_info *)buf;
485 ret = lp->ldinfo_dataorg;
492 * Platform: SunOS/Solaris, possibly others which use dlopen.
493 * Author: Paul Marquess (pmarquess@bfsec.bt.co.uk)
494 * Created: 10th July 1994
497 * 15th July 1994 - Added code to explicitly save any error messages.
498 * 3rd August 1994 - Upgraded to v3 spec.
499 * 9th August 1994 - Changed to use IV
500 * 10th August 1994 - Tim Bunce: Added RTLD_LAZY, switchable debugging,
501 * basic FreeBSD support, removed ClearError
511 #include "dlutils.c" /* SaveError() etc */
517 (void)dl_generic_private_init();
520 MODULE = DynaLoader PACKAGE = DynaLoader
523 (void)dl_private_init();
527 dl_load_file(filename)
530 DLDEBUG(1,PerlIO_printf(PerlIO_stderr(), "dl_load_file(%s):\n", filename));
531 RETVAL = dlopen(filename, 1) ;
532 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), " libref=%x\n", RETVAL));
533 ST(0) = sv_newmortal() ;
535 SaveError("%s",dlerror()) ;
537 sv_setiv( ST(0), (IV)RETVAL);
541 dl_find_symbol(libhandle, symbolname)
545 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_find_symbol(handle=%x, symbol=%s)\n",
546 libhandle, symbolname));
547 RETVAL = dlsym(libhandle, symbolname);
548 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), " symbolref = %x\n", RETVAL));
549 ST(0) = sv_newmortal() ;
551 SaveError("%s",dlerror()) ;
553 sv_setiv( ST(0), (IV)RETVAL);
562 # These functions should not need changing on any platform:
565 dl_install_xsub(perl_name, symref, filename="$Package")
570 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_install_xsub(name=%s, symref=%x)\n",
572 ST(0)=sv_2mortal(newRV((SV*)newXS(perl_name, (void(*)())symref, filename)));