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
29 /* This whole file is conditional upon HAVE_SHLIB */
32 /* Thankfully, most systems follow the ELFish dlopen() method.
33 ** HAVE__DLOPEN is lame, but SCO has their dl* functions as _dl*, and
34 ** unless you include dlfcn.h you don't get the macros to mask them, and
35 ** autoconf fails to find them. No longer true as of 5.0.5.
37 ** Anybody who wants to use this on SCO needs to have their configure.in
38 ** look for _dlopen() as well as dlopen()
40 #if defined(HAVE_DLOPEN) || defined(HAVE__DLOPEN) || defined(HAVE_DLFCN_H)
45 #endif /* RTLD_LAZY isn't defined under FreeBSD - ick */
48 # define RTLD_GLOBAL 0
52 dll_init (CONST char *arg)
58 dll_open (CONST char *fname)
60 return (dll_handle)dlopen (fname, RTLD_LAZY | RTLD_GLOBAL);
64 dll_close (dll_handle h)
66 return dlclose((void *)h);
70 dll_function (dll_handle h, CONST char *n)
72 #ifdef DLSYM_NEEDS_UNDERSCORE
73 char *buf = alloca_array (char, strlen (n) + 2);
75 (void)strcpy(buf + 1, n);
78 return (dll_func)dlsym ((void *)h, n);
82 dll_variable (dll_handle h, CONST char *n)
84 #ifdef DLSYM_NEEDS_UNDERSCORE
85 char *buf = alloca_array (char, strlen (n) + 2);
87 (void)strcpy(buf + 1, n);
90 return (dll_var)dlsym ((void *)h, n);
94 dll_error (dll_handle h)
96 #if defined(HAVE_DLERROR) || defined(dlerror)
97 return (CONST char *)dlerror ();
98 #elif defined(HAVE__DLERROR)
99 return (const char *)_dlerror();
101 return "Shared library error";
105 #elif defined(HAVE_SHL_LOAD)
106 /* This is the HP/UX version */
109 dll_init (CONST char *arg)
115 dll_open (CONST char *fname)
117 shl_t h = shl_load (fname, BIND_DEFERRED,0L);
122 hp = (shl_t *)malloc (sizeof (shl_t));
128 return (dll_handle)hp;
132 dll_close (dll_handle h)
134 shl_t hp = *((shl_t *)h);
136 return shl_unload(h);
140 dll_function (dll_handle h, CONST char *n)
144 if (shl_findsym ((shl_t *)h, n, TYPE_PROCEDURE, &handle))
147 return (dll_func)handle;
151 dll_variable (dll_handle h, CONST char *n)
155 if (shl_findsym ((shl_t *)h, n, TYPE_DATA, &handle))
158 return (dll_var)handle;
162 dll_error (dll_handle h)
164 /* #### WTF?! Shouldn't this at least attempt to get strerror or
165 something? --hniksic */
166 return "Generic shared library error";
169 #elif defined(HAVE_INIT_DLD)
172 dll_init (CONST char *arg)
174 char *real_exe = dld_find_executable (arg);
177 rc = dld_init (real_exe);
187 dll_open (CONST char *fname)
189 rc = dld_link (fname);
193 return (dll_handle)1;
197 dll_close (dll_handle h)
199 /* *sigh* DLD is pretty lame and doesn't return a handle that you can use
200 ** later on to free the file - you have to remember the filename and
201 ** use that as the unlinker. We should eventually keep a linked list
202 ** of loaded modules and then use the node pointer as the unique id
203 ** for the shared library. Wheeee. But not now.
209 dll_function (dll_handle h, CONST char *n)
211 return dld_get_func(n);
215 dll_variable (dll_handle h, CONST char *n)
217 return dld_get_symbol(n);
219 #elif defined(_WINDOWS) || defined(WIN32)
221 dll_init (CONST char *arg)
227 dll_open (CONST char *fname)
229 return (dll_handle)LoadLibrary (fname);
233 dll_close (dll_handle h)
235 return FreeLibrary (h);
239 dll_function (dll_handle h, CONST char *n)
241 return (dll_func)GetProcAddress (h,n);
245 dll_variable (dll_handle h, CONST char *n)
247 return (dll_func)GetProcAddress (h,n);
251 dll_error (dll_handle h)
253 return "Windows DLL Error";
256 /* Catchall if we don't know about this systems method of dynamic loading */
258 dll_init (CONST char *arg)
264 dll_open (CONST char *fname)
270 dll_close (dll_handle h)
276 dll_function (dll_handle h, CONST char *n)
282 dll_variable (dll_handle h, CONST char *n)
288 dll_error (dll_handle h)
290 return "Shared libraries not implemented on this system";
292 #endif /* System conditionals */
294 #endif /* HAVE_SHLIB */