265
|
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 dll_init(CONST char *arg) {
|
|
55 return(0);
|
|
56 }
|
|
57
|
|
58 dll_handle dll_open(CONST char *fname) {
|
|
59 return((dll_handle)dlopen(fname,RTLD_LAZY|RTLD_GLOBAL));
|
|
60 }
|
|
61
|
|
62 int dll_close(dll_handle h) {
|
|
63 return(dlclose((void *)h));
|
|
64 }
|
|
65
|
|
66 dll_func dll_function(dll_handle h,CONST char *n) {
|
|
67 #ifdef DLSYM_NEEDS_UNDERSCORE
|
|
68 char buf[1024];
|
|
69 *buf = '_';
|
|
70 (void)strcpy(buf + 1, n);
|
|
71 n = buf;
|
|
72 #endif
|
|
73 return((dll_func)dlsym((void *)h,n));
|
|
74 }
|
|
75
|
|
76 dll_var dll_variable(dll_handle h,CONST char *n) {
|
|
77 return((dll_var)dlsym((void *)h,n));
|
|
78 }
|
|
79
|
|
80 CONST char *dll_error(dll_handle h) {
|
|
81 #ifdef HAVE_DLERROR
|
|
82 return((CONST char *)dl_error());
|
|
83 #else
|
|
84 return("Shared library error");
|
|
85 #endif
|
|
86 }
|
|
87
|
|
88 #elif defined(HAVE_SHL_LOAD)
|
|
89 /* This is the HP/UX version */
|
|
90 #include <dl.h>
|
|
91 int dll_init(CONST char *arg) {
|
|
92 return(0);
|
|
93 }
|
|
94
|
|
95 dll_handle dll_open(CONST char *fname) {
|
267
|
96 shl_t h = shl_load(fname,BIND_DEFERRED,0L);
|
|
97 shl_t *hp = NULL;
|
|
98
|
|
99 if (h) {
|
|
100 hp = (shl_t *)malloc(sizeof(shl_t));
|
|
101 if (!hp) {
|
|
102 shl_unload(h);
|
|
103 } else {
|
|
104 *hp = h;
|
|
105 }
|
|
106 }
|
|
107 return((dll_handle)hp);
|
265
|
108 }
|
|
109
|
|
110 int dll_close(dll_handle h) {
|
272
|
111 shl_t hp = *((shl_t *)h);
|
267
|
112 free(hp);
|
|
113 return (shl_unload(h));
|
265
|
114 }
|
|
115
|
|
116 dll_func dll_function(dll_handle h,CONST char *n) {
|
|
117 long handle = 0L;
|
|
118
|
267
|
119 if (shl_findsym((shl_t *)h,n,TYPE_PROCEDURE,&handle))
|
265
|
120 return(NULL);
|
|
121
|
|
122 return((dll_func)handle);
|
|
123 }
|
|
124
|
|
125 dll_var dll_variable(dll_handle h,CONST char *n) {
|
|
126 long handle = 0L;
|
|
127
|
267
|
128 if (shl_findsym((shl_t *)h,n,TYPE_DATA,&handle))
|
265
|
129 return(NULL);
|
|
130
|
|
131 return((dll_var)handle);
|
|
132 }
|
|
133
|
|
134 CONST char *dll_error(dll_handle h) {
|
|
135 return("Generic shared library error");
|
|
136 }
|
|
137
|
|
138 #elif defined(HAVE_INIT_DLD)
|
|
139 #include <dld.h>
|
|
140 int dll_init(CONST char *arg) {
|
|
141 char *real_exe = dld_find_executable(arg);
|
|
142 int rc;
|
|
143
|
|
144 rc = dld_init(real_exe);
|
|
145 if (rc) {
|
|
146 dld_perror (exe);
|
|
147 return(-1);
|
|
148 }
|
|
149 return(0);
|
|
150 }
|
|
151
|
|
152 dll_handle dll_open(CONST char *fname) {
|
|
153 rc = dld_link(fname);
|
|
154 if (rc) {
|
|
155 return (NULL);
|
|
156 }
|
|
157 return((dll_handle)1);
|
|
158 }
|
|
159
|
|
160 int dll_close(dll_handle h) {
|
|
161 /* *sigh* DLD is pretty lame and doesn't return a handle that you can use
|
|
162 ** later on to free the file - you have to remember the filename and
|
|
163 ** use that as the unlinker. We should eventually keep a linked list
|
|
164 ** of loaded modules and then use the node pointer as the unique id
|
|
165 ** for the shared library. Wheeee. But not now.
|
|
166 */
|
|
167 return(1);
|
|
168 }
|
|
169
|
|
170 DLL_FUNC dll_function(dll_handle h,CONST char *n) {
|
|
171 return (dld_get_func(n));
|
|
172 }
|
|
173
|
|
174 DLL_FUNC dll_variable(dll_handle h,CONST char *n) {
|
|
175 return (dld_get_symbol(n));
|
|
176 }
|
|
177 #elif defined(_WINDOWS) || defined(WIN32)
|
|
178 int dll_init(CONST char *arg) {
|
|
179 return(0);
|
|
180 }
|
|
181
|
|
182 dll_handle dll_open(CONST char *fname) {
|
|
183 return((dll_handle)LoadLibrary(fname));
|
|
184 }
|
|
185
|
|
186 int dll_close(dll_handle h) {
|
|
187 return(FreeLibrary(h));
|
|
188 }
|
|
189
|
|
190 dll_func dll_function(dll_handle h,CONST char *n) {
|
|
191 return((dll_func)GetProcAddress(h,n));
|
|
192 }
|
|
193
|
|
194 dll_func dll_variable(dll_handle h,CONST char *n) {
|
|
195 return((dll_func)GetProcAddress(h,n));
|
|
196 }
|
|
197
|
|
198 CONST char *dll_error(dll_handle h) {
|
|
199 return("Windows DLL Error");
|
|
200 }
|
|
201 #else
|
|
202 /* Catchall if we don't know about this systems method of dynamic loading */
|
|
203 int dll_init(CONST char *arg) {
|
|
204 return(-1);
|
|
205 }
|
|
206
|
|
207 dll_handle dll_open(CONST char *fname) {
|
|
208 return(NULL);
|
|
209 }
|
|
210
|
|
211 int dll_close(dll_handle h) {
|
|
212 return(0);
|
|
213 }
|
|
214
|
|
215 dll_func dll_function(dll_handle h,CONST char *n) {
|
|
216 return(NULL);
|
|
217 }
|
|
218
|
|
219 dll_func dll_variable(dll_handle h,CONST char *n) {
|
|
220 return(NULL);
|
|
221 }
|
|
222
|
|
223 CONST char *dll_error(dll_handle h) {
|
|
224 return("Shared libraries not implemented on this system.");
|
|
225 }
|
|
226 #endif /* System conditionals */
|
|
227
|
|
228 #endif /* HAVE_SHLIB */
|