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