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);
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);
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_load will hang hard if passed a NULL fname. */
118 if (fname == NULL) return NULL;
120 return (dll_handle) shl_load (fname, BIND_DEFERRED,0L);
124 dll_close (dll_handle h)
126 return shl_unload ((shl_t) h);
130 dll_function (dll_handle h, const char *n)
134 if (shl_findsym ((shl_t *) &h, n, TYPE_PROCEDURE, &handle))
137 return (dll_func) handle;
141 dll_variable (dll_handle h, const char *n)
145 if (shl_findsym ((shl_t *) &h, n, TYPE_DATA, &handle))
148 return (dll_var) handle;
152 dll_error (dll_handle h)
154 /* #### WTF?! Shouldn't this at least attempt to get strerror or
155 something? --hniksic */
156 return "Generic shared library error";
159 #elif defined(HAVE_INIT_DLD)
162 dll_init (const char *arg)
164 char *real_exe = dld_find_executable (arg);
167 rc = dld_init (real_exe);
177 dll_open (const char *fname)
179 rc = dld_link (fname);
183 return (dll_handle) 1;
187 dll_close (dll_handle h)
189 /* *sigh* DLD is pretty lame and doesn't return a handle that you can use
190 ** later on to free the file - you have to remember the filename and
191 ** use that as the unlinker. We should eventually keep a linked list
192 ** of loaded modules and then use the node pointer as the unique id
193 ** for the shared library. Wheeee. But not now.
199 dll_function (dll_handle h, const char *n)
201 return dld_get_func (n);
205 dll_variable (dll_handle h, const char *n)
207 return dld_get_symbol (n);
209 #elif defined (WIN32_NATIVE)
211 #define WIN32_LEAN_AND_MEAN
213 #undef WIN32_LEAN_AND_MEAN
216 dll_init (const char *arg)
222 dll_open (const char *fname)
224 return (dll_handle) LoadLibrary (fname);
228 dll_close (dll_handle h)
230 return FreeLibrary (h);
234 dll_function (dll_handle h, const char *n)
236 return (dll_func) GetProcAddress (h, n);
240 dll_variable (dll_handle h, const char *n)
242 return (dll_func) GetProcAddress (h, n);
246 dll_error (dll_handle h)
248 return "Windows DLL Error";
251 /* Catchall if we don't know about this systems method of dynamic loading */
253 dll_init (const char *arg)
259 dll_open (const char *fname)
265 dll_close (dll_handle h)
271 dll_function (dll_handle h, const char *n)
277 dll_variable (dll_handle h, const char *n)
283 dll_error (dll_handle h)
285 return "Shared libraries not implemented on this system";
287 #endif /* System conditionals */
289 #endif /* HAVE_SHLIB */