XEmacs 21.2-b1
[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 <assert.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include "sysdll.h"
31
32 /* This whole file is conditional upon HAVE_DLL */
33 #ifdef HAVE_SHLIB
34
35 /* Thankfully, most systems follow the ELFish dlopen() method.
36 ** HAVE__DLOPEN is lame, but SCO has their dl* functions as _dl*, and
37 ** unless you include dlfcn.h you don't get the macros to mask them, and
38 ** autoconf fails to find them.
39 **
40 ** Anybody who wants to use this on SCO needs to have their configure.in
41 ** look for _dlopen() as well as dlopen()
42 */
43 #if defined(HAVE_DLOPEN) || defined(HAVE__DLOPEN)
44 #include <dlfcn.h>
45
46 #ifndef RTLD_LAZY
47 # define RTLD_LAZY 1
48 #endif /* RTLD_LAZY isn't defined under FreeBSD - ick */
49
50 #ifndef RTLD_GLOBAL
51 # define RTLD_GLOBAL 0
52 #endif
53
54 int
55 dll_init (CONST char *arg)
56 {
57   return 0;
58 }
59
60 dll_handle
61 dll_open (CONST char *fname)
62 {
63   return (dll_handle)dlopen (fname, RTLD_LAZY | RTLD_GLOBAL);
64 }
65
66 int
67 dll_close (dll_handle h)
68 {
69   return dlclose((void *)h);
70 }
71
72 dll_func
73 dll_function (dll_handle h, CONST char *n)
74 {
75 #ifdef DLSYM_NEEDS_UNDERSCORE
76   char *buf = alloca_array (char, strlen (n) + 2);
77   *buf = '_';
78   (void)strcpy(buf + 1, n);
79   n = buf;
80 #endif
81   return (dll_func)dlsym ((void *)h, n);
82 }
83
84 dll_var
85 dll_variable (dll_handle h, CONST char *n)
86 {
87   return (dll_var)dlsym ((void *)h, n);
88 }
89
90 CONST char *
91 dll_error (dll_handle h)
92 {
93 #ifdef HAVE_DLERROR
94   return (CONST char *)dlerror ();
95 #else
96   return "Shared library error";
97 #endif
98 }
99
100 #elif defined(HAVE_SHL_LOAD)
101 /* This is the HP/UX version */
102 #include <dl.h>
103 int
104 dll_init (CONST char *arg)
105 {
106   return 0;
107 }
108
109 dll_handle
110 dll_open (CONST char *fname)
111 {
112   shl_t h = shl_load (fname, BIND_DEFERRED,0L);
113   shl_t *hp = NULL;
114
115   if (h)
116     {
117       hp = (shl_t *)malloc (sizeof (shl_t));
118       if (!hp)
119         shl_unload(h);
120       else
121         *hp = h;
122     }
123   return (dll_handle)hp;
124 }
125
126 int
127 dll_close (dll_handle h)
128 {
129   shl_t hp = *((shl_t *)h);
130   free (hp);
131   return shl_unload(h);
132 }
133
134 dll_func
135 dll_function (dll_handle h, CONST char *n)
136 {
137   long handle = 0L;
138
139   if (shl_findsym ((shl_t *)h, n, TYPE_PROCEDURE, &handle))
140     return NULL;
141
142   return (dll_func)handle;
143 }
144
145 dll_var
146 dll_variable (dll_handle h, CONST char *n)
147 {
148   long handle = 0L;
149
150   if (shl_findsym ((shl_t *)h, n, TYPE_DATA, &handle))
151     return NULL;
152
153   return (dll_var)handle;
154 }
155
156 CONST char *
157 dll_error (dll_handle h)
158 {
159   /* #### WTF?!  Shouldn't this at least attempt to get strerror or
160      something?  --hniksic */
161   return "Generic shared library error";
162 }
163
164 #elif defined(HAVE_INIT_DLD)
165 #include <dld.h>
166 int
167 dll_init (CONST char *arg)
168 {
169   char *real_exe = dld_find_executable (arg);
170   int rc;
171
172   rc = dld_init (real_exe);
173   if (rc)
174     {
175       dld_perror (exe);
176       return -1;
177     }
178   return 0;
179 }
180
181 dll_handle
182 dll_open (CONST char *fname)
183 {
184   rc = dld_link (fname);
185   if (rc)
186     return NULL;
187
188   return (dll_handle)1;
189 }
190
191 int
192 dll_close (dll_handle h)
193 {
194   /* *sigh* DLD is pretty lame and doesn't return a handle that you can use
195   ** later on to free the file - you have to remember the filename and
196   ** use that as the unlinker.  We should eventually keep a linked list
197   ** of loaded modules and then use the node pointer as the unique id
198   ** for the shared library.  Wheeee.  But not now.
199   */
200   return 1;
201 }
202
203 DLL_FUNC
204 dll_function (dll_handle h, CONST char *n)
205 {
206   return dld_get_func(n);
207 }
208
209 DLL_FUNC
210 dll_variable (dll_handle h, CONST char *n)
211 {
212   return dld_get_symbol(n);
213 }
214 #elif defined(_WINDOWS) || defined(WIN32)
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 */