1 /* sysdll.c --- system dependent support for dynamic linked libraries
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 Author: William Perry <wmperry@aventail.com>
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 /* This whole file is conditional upon HAVE_SHLIB */
31 /* Thankfully, most systems follow the ELFish dlopen() method.
32 ** HAVE__DLOPEN is lame, but SCO has their dl* functions as _dl*, and
33 ** unless you include dlfcn.h you don't get the macros to mask them, and
34 ** autoconf fails to find them. No longer true as of 5.0.5.
36 ** Anybody who wants to use this on SCO needs to have their configure.in
37 ** look for _dlopen() as well as dlopen()
39 #if defined(HAVE_DLOPEN) || defined(HAVE__DLOPEN) || defined(HAVE_DLFCN_H)
44 #endif /* RTLD_LAZY isn't defined under FreeBSD - ick */
47 # define RTLD_GLOBAL 0
51 dll_init (CONST char *arg)
57 dll_open (CONST char *fname)
59 return (dll_handle)dlopen (fname, RTLD_LAZY | RTLD_GLOBAL);
63 dll_close (dll_handle h)
65 return dlclose((void *)h);
69 dll_function (dll_handle h, CONST char *n)
71 #ifdef DLSYM_NEEDS_UNDERSCORE
72 char *buf = alloca_array (char, strlen (n) + 2);
74 (void)strcpy(buf + 1, n);
77 return (dll_func)dlsym ((void *)h, n);
81 dll_variable (dll_handle h, CONST char *n)
83 #ifdef DLSYM_NEEDS_UNDERSCORE
84 char *buf = alloca_array (char, strlen (n) + 2);
86 (void)strcpy(buf + 1, n);
89 return (dll_var)dlsym ((void *)h, n);
93 dll_error (dll_handle h)
95 #if defined(HAVE_DLERROR) || defined(dlerror)
96 return (CONST char *)dlerror ();
97 #elif defined(HAVE__DLERROR)
98 return (const char *)_dlerror();
100 return "Shared library error";
104 #elif defined(HAVE_SHL_LOAD)
105 /* This is the HP/UX version */
108 dll_init (CONST char *arg)
114 dll_open (CONST char *fname)
116 shl_t h = shl_load (fname, BIND_DEFERRED,0L);
121 hp = (shl_t *)malloc (sizeof (shl_t));
127 return (dll_handle)hp;
131 dll_close (dll_handle h)
133 shl_t hp = *((shl_t *)h);
135 return shl_unload(h);
139 dll_function (dll_handle h, CONST char *n)
143 if (shl_findsym ((shl_t *)h, n, TYPE_PROCEDURE, &handle))
146 return (dll_func)handle;
150 dll_variable (dll_handle h, CONST char *n)
154 if (shl_findsym ((shl_t *)h, n, TYPE_DATA, &handle))
157 return (dll_var)handle;
161 dll_error (dll_handle h)
163 /* #### WTF?! Shouldn't this at least attempt to get strerror or
164 something? --hniksic */
165 return "Generic shared library error";
168 #elif defined(HAVE_INIT_DLD)
171 dll_init (CONST char *arg)
173 char *real_exe = dld_find_executable (arg);
176 rc = dld_init (real_exe);
186 dll_open (CONST char *fname)
188 rc = dld_link (fname);
192 return (dll_handle)1;
196 dll_close (dll_handle h)
198 /* *sigh* DLD is pretty lame and doesn't return a handle that you can use
199 ** later on to free the file - you have to remember the filename and
200 ** use that as the unlinker. We should eventually keep a linked list
201 ** of loaded modules and then use the node pointer as the unique id
202 ** for the shared library. Wheeee. But not now.
208 dll_function (dll_handle h, CONST char *n)
210 return dld_get_func(n);
214 dll_variable (dll_handle h, CONST char *n)
216 return dld_get_symbol(n);
218 #elif defined(_WINDOWS) || defined(WIN32)
220 dll_init (CONST char *arg)
226 dll_open (CONST char *fname)
228 return (dll_handle)LoadLibrary (fname);
232 dll_close (dll_handle h)
234 return FreeLibrary (h);
238 dll_function (dll_handle h, CONST char *n)
240 return (dll_func)GetProcAddress (h,n);
244 dll_variable (dll_handle h, CONST char *n)
246 return (dll_func)GetProcAddress (h,n);
250 dll_error (dll_handle h)
252 return "Windows DLL Error";
255 /* Catchall if we don't know about this systems method of dynamic loading */
257 dll_init (CONST char *arg)
263 dll_open (CONST char *fname)
269 dll_close (dll_handle h)
275 dll_function (dll_handle h, CONST char *n)
281 dll_variable (dll_handle h, CONST char *n)
287 dll_error (dll_handle h)
289 return "Shared libraries not implemented on this system";
291 #endif /* System conditionals */
293 #endif /* HAVE_SHLIB */