XEmacs 21.2.36 "Notos"
[chise/xemacs-chise.git.1] / src / sysdll.c
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>
4
5 This file is part of XEmacs.
6
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
10 later version.
11
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
15 for more details.
16
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
20 02111-1307, USA.  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include "sysdll.h"
28
29 /* This whole file is conditional upon HAVE_SHLIB */
30 #ifdef HAVE_SHLIB
31
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.
36 **
37 ** Anybody who wants to use this on SCO needs to have their configure.in
38 ** look for _dlopen() as well as dlopen()
39 */
40 #if defined(HAVE_DLOPEN) || defined(HAVE__DLOPEN) || defined(HAVE_DLFCN_H)
41 #include <dlfcn.h>
42
43 #ifndef RTLD_LAZY
44 # define RTLD_LAZY 1
45 #endif /* RTLD_LAZY isn't defined under FreeBSD - ick */
46
47 #ifndef RTLD_GLOBAL
48 # define RTLD_GLOBAL 0
49 #endif
50
51 int
52 dll_init (const char *arg)
53 {
54   return 0;
55 }
56
57 dll_handle
58 dll_open (const char *fname)
59 {
60   return (dll_handle) dlopen (fname, RTLD_LAZY | RTLD_GLOBAL);
61 }
62
63 int
64 dll_close (dll_handle h)
65 {
66   return dlclose ((void *) h);
67 }
68
69 dll_func
70 dll_function (dll_handle h, const char *n)
71 {
72 #ifdef DLSYM_NEEDS_UNDERSCORE
73   char *buf = alloca_array (char, strlen (n) + 2);
74   *buf = '_';
75   strcpy (buf + 1, n);
76   n = buf;
77 #endif
78   return (dll_func) dlsym ((void *) h, n);
79 }
80
81 dll_var
82 dll_variable (dll_handle h, const char *n)
83 {
84 #ifdef DLSYM_NEEDS_UNDERSCORE
85   char *buf = alloca_array (char, strlen (n) + 2);
86   *buf = '_';
87   strcpy (buf + 1, n);
88   n = buf;
89 #endif
90   return (dll_var)dlsym ((void *)h, n);
91 }
92
93 const char *
94 dll_error (dll_handle h)
95 {
96 #if defined(HAVE_DLERROR) || defined(dlerror)
97   return (const char *) dlerror ();
98 #elif defined(HAVE__DLERROR)
99   return (const char *) _dlerror();
100 #else
101   return "Shared library error";
102 #endif
103 }
104
105 #elif defined(HAVE_SHL_LOAD)
106 /* This is the HP/UX version */
107 #include <dl.h>
108 int
109 dll_init (const char *arg)
110 {
111   return 0;
112 }
113
114 dll_handle
115 dll_open (const char *fname)
116 {
117   /* shl_load will hang hard if passed a NULL fname. */
118   if (fname == NULL) return NULL;
119
120   return (dll_handle) shl_load (fname, BIND_DEFERRED,0L);
121 }
122
123 int
124 dll_close (dll_handle h)
125 {
126   return shl_unload ((shl_t) h);
127 }
128
129 dll_func
130 dll_function (dll_handle h, const char *n)
131 {
132   long handle = 0L;
133
134   if (shl_findsym ((shl_t *) &h, n, TYPE_PROCEDURE, &handle))
135     return NULL;
136
137   return (dll_func) handle;
138 }
139
140 dll_var
141 dll_variable (dll_handle h, const char *n)
142 {
143   long handle = 0L;
144
145   if (shl_findsym ((shl_t *) &h, n, TYPE_DATA, &handle))
146     return NULL;
147
148   return (dll_var) handle;
149 }
150
151 const char *
152 dll_error (dll_handle h)
153 {
154   /* #### WTF?!  Shouldn't this at least attempt to get strerror or
155      something?  --hniksic */
156   return "Generic shared library error";
157 }
158
159 #elif defined(HAVE_INIT_DLD)
160 #include <dld.h>
161 int
162 dll_init (const char *arg)
163 {
164   char *real_exe = dld_find_executable (arg);
165   int rc;
166
167   rc = dld_init (real_exe);
168   if (rc)
169     {
170       dld_perror (exe);
171       return -1;
172     }
173   return 0;
174 }
175
176 dll_handle
177 dll_open (const char *fname)
178 {
179   rc = dld_link (fname);
180   if (rc)
181     return NULL;
182
183   return (dll_handle) 1;
184 }
185
186 int
187 dll_close (dll_handle h)
188 {
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.
194   */
195   return 1;
196 }
197
198 DLL_FUNC
199 dll_function (dll_handle h, const char *n)
200 {
201   return dld_get_func (n);
202 }
203
204 DLL_FUNC
205 dll_variable (dll_handle h, const char *n)
206 {
207   return dld_get_symbol (n);
208 }
209 #elif defined (WIN32_NATIVE)
210
211 #define WIN32_LEAN_AND_MEAN
212 #include <windows.h>
213 #undef WIN32_LEAN_AND_MEAN
214
215 int
216 dll_init (const char *arg)
217 {
218   return 0;
219 }
220
221 dll_handle
222 dll_open (const char *fname)
223 {
224   return (dll_handle) LoadLibrary (fname);
225 }
226
227 int
228 dll_close (dll_handle h)
229 {
230   return FreeLibrary (h);
231 }
232
233 dll_func
234 dll_function (dll_handle h, const char *n)
235 {
236   return (dll_func) GetProcAddress (h, n);
237 }
238
239 dll_func
240 dll_variable (dll_handle h, const char *n)
241 {
242   return (dll_func) GetProcAddress (h, n);
243 }
244
245 const char *
246 dll_error (dll_handle h)
247 {
248   return "Windows DLL Error";
249 }
250 #else
251 /* Catchall if we don't know about this systems method of dynamic loading */
252 int
253 dll_init (const char *arg)
254 {
255   return -1;
256 }
257
258 dll_handle
259 dll_open (const char *fname)
260 {
261   return NULL;
262 }
263
264 int
265 dll_close (dll_handle h)
266 {
267   return 0;
268 }
269
270 dll_func
271 dll_function (dll_handle h, const char *n)
272 {
273   return NULL;
274 }
275
276 dll_func
277 dll_variable (dll_handle h, const char *n)
278 {
279   return NULL;
280 }
281
282 const char *
283 dll_error (dll_handle h)
284 {
285   return "Shared libraries not implemented on this system";
286 }
287 #endif /* System conditionals */
288
289 #endif /* HAVE_SHLIB */