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