1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave 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 Octave 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 Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1664
|
27 #if defined (WITH_SHL) |
|
28 #include <cerrno> |
|
29 #include <cstring> |
|
30 #endif |
|
31 |
707
|
32 #include <strstream.h> |
|
33 |
1
|
34 extern "C" |
|
35 { |
1664
|
36 #if defined (WITH_DL) |
3167
|
37 #if defined (HAVE_DLFCN_H) |
1664
|
38 #include <dlfcn.h> |
3174
|
39 #else |
|
40 extern void *dlopen (const char *, int); |
|
41 extern const char *dlerror (void); |
|
42 extern void *dlsym (void *, const char *); |
|
43 extern int dlclose (void *); |
3167
|
44 #endif |
1755
|
45 #ifndef RTLD_LAZY |
|
46 #define RTLD_LAZY 1 |
|
47 #endif |
1664
|
48 #elif defined (WITH_SHL) |
|
49 #include <dl.h> |
1102
|
50 #endif |
1
|
51 } |
|
52 |
2492
|
53 #include <defaults.h> |
1352
|
54 #include "dirfns.h" |
|
55 #include "dynamic-ld.h" |
|
56 #include "error.h" |
1670
|
57 #include "toplev.h" |
1155
|
58 #include "pathsearch.h" |
2862
|
59 #include "oct-obj.h" |
2969
|
60 #include "ov-builtin.h" |
2370
|
61 #include "ov.h" |
1352
|
62 #include "utils.h" |
707
|
63 #include "variables.h" |
1
|
64 |
1664
|
65 #if defined (WITH_DL) |
|
66 |
2894
|
67 class |
|
68 octave_dlopen_dynamic_loader : public octave_dynamic_loader |
1664
|
69 { |
2894
|
70 public: |
|
71 |
|
72 octave_dlopen_dynamic_loader (void) |
|
73 : octave_dynamic_loader () { } |
|
74 |
|
75 ~octave_dlopen_dynamic_loader (void) { } |
|
76 |
|
77 private: |
|
78 |
2969
|
79 octave_dynamic_loader::builtin_fcn_installer |
2894
|
80 resolve_reference (const string& mangled_name, const string& file); |
|
81 |
|
82 // No copying! |
|
83 |
|
84 octave_dlopen_dynamic_loader (const octave_dlopen_dynamic_loader&); |
|
85 |
|
86 octave_dlopen_dynamic_loader& |
|
87 operator = (const octave_dlopen_dynamic_loader&); |
|
88 }; |
|
89 |
2969
|
90 octave_dynamic_loader::builtin_fcn_installer |
2894
|
91 octave_dlopen_dynamic_loader::resolve_reference (const string& name, |
|
92 const string& file) |
|
93 { |
2969
|
94 octave_dynamic_loader::builtin_fcn_installer retval = 0; |
1665
|
95 |
1664
|
96 // Dynamic linking with dlopen/dlsym doesn't require specification |
|
97 // of the libraries at runtime. Instead, they are specified when |
|
98 // the .oct file is created. |
|
99 |
1755
|
100 void *handle = dlopen (file.c_str (), RTLD_LAZY); |
1664
|
101 |
2894
|
102 const char *nm = name.c_str (); |
|
103 |
1664
|
104 if (handle) |
|
105 { |
2895
|
106 void *tmp = dlsym (handle, nm); |
|
107 |
3145
|
108 retval = X_CAST (octave_dynamic_loader::builtin_fcn_installer, tmp); |
1664
|
109 |
1665
|
110 if (! retval) |
1664
|
111 { |
|
112 const char *errmsg = dlerror (); |
|
113 |
|
114 if (errmsg) |
2894
|
115 error("%s: `%s'", nm, errmsg); |
1664
|
116 else |
2894
|
117 error("unable to link function `%s'", nm); |
1664
|
118 |
|
119 dlclose (handle); |
|
120 } |
|
121 } |
|
122 else |
2894
|
123 error ("%s: %s `%s'", dlerror (), file.c_str (), nm); |
1665
|
124 |
|
125 return retval; |
1664
|
126 } |
|
127 |
|
128 #elif defined (WITH_SHL) |
|
129 |
2894
|
130 class |
|
131 octave_shl_load_dynamic_loader : public octave_dynamic_loader |
1664
|
132 { |
2894
|
133 public: |
|
134 |
|
135 octave_shl_load_dynamic_loader (void) |
|
136 : octave_dynamic_loader () { } |
|
137 |
|
138 ~octave_shl_load_dynamic_loader (void) { } |
|
139 |
|
140 private: |
|
141 |
2969
|
142 octave_dynamic_loader::builtin_fcn_installer |
2894
|
143 resolve_reference (const string& mangled_name, const string& file); |
|
144 |
|
145 // No copying! |
|
146 |
|
147 octave_shl_load_dynamic_loader (const octave_shl_load_dynamic_loader&); |
|
148 |
|
149 octave_shl_load_dynamic_loader& |
|
150 operator = (const octave_shl_load_dynamic_loader&); |
|
151 }; |
|
152 |
2969
|
153 octave_dynamic_loader::builtin_fcn_installer |
2894
|
154 octave_shl_load_dynamic_loader::resolve_reference (const string& name, |
|
155 const string& file) |
|
156 { |
2969
|
157 octave_dynamic_loader::builtin_fcn_installer retval = 0; |
1665
|
158 |
1664
|
159 // Dynamic linking with shl_load/shl_findsym doesn't require |
|
160 // specification of the libraries at runtime. Instead, they are |
|
161 // specified when the .oct file is created. |
|
162 |
2800
|
163 shl_t handle = shl_load (file.c_str (), BIND_DEFERRED, 0L); |
1664
|
164 |
2894
|
165 const char *nm = name.c_str (); |
|
166 |
1664
|
167 if (handle) |
|
168 { |
3033
|
169 // Don't use TYPE_PROCEDURE here. The man page says that future |
|
170 // versions of HP-UX may not support it. |
|
171 |
|
172 int status = shl_findsym (&handle, nm, TYPE_UNDEFINED, &retval); |
1664
|
173 |
|
174 if (status < 0) |
|
175 { |
|
176 const char *errmsg = strerror (errno); |
|
177 |
|
178 if (errmsg) |
2894
|
179 error("%s: `%s'", nm, errmsg); |
1664
|
180 else |
2894
|
181 error("unable to link function `%s'", nm); |
1664
|
182 |
1665
|
183 retval = 0; |
1664
|
184 } |
|
185 } |
|
186 else |
2894
|
187 error ("%s: %s `%s'", strerror (errno), file.c_str (), nm); |
1665
|
188 |
|
189 return retval; |
1664
|
190 } |
|
191 |
1742
|
192 #endif |
1664
|
193 |
2926
|
194 octave_dynamic_loader *octave_dynamic_loader::instance = 0; |
|
195 |
|
196 bool |
|
197 octave_dynamic_loader::instance_ok (void) |
|
198 { |
|
199 bool retval = true; |
|
200 |
|
201 if (! instance) |
|
202 make_dynamic_loader (); |
|
203 |
|
204 if (! instance) |
|
205 { |
|
206 error ("unable to create command history object!"); |
|
207 |
|
208 retval = false; |
|
209 } |
|
210 |
|
211 return retval; |
|
212 } |
|
213 |
|
214 void |
|
215 octave_dynamic_loader::make_dynamic_loader (void) |
2894
|
216 { |
|
217 #if defined (WITH_DL) |
2926
|
218 instance = new octave_dlopen_dynamic_loader (); |
2894
|
219 #elif defined (WITH_SHL) |
3130
|
220 instance = new octave_shl_load_dynamic_loader (); |
2894
|
221 #else |
2926
|
222 instance = new octave_dynamic_loader (); |
2894
|
223 #endif |
|
224 } |
|
225 |
2969
|
226 bool |
2894
|
227 octave_dynamic_loader::load_fcn_from_dot_oct_file (const string& fcn_name) |
707
|
228 { |
2926
|
229 if (! instance_ok ()) |
|
230 make_dynamic_loader (); |
2894
|
231 |
2969
|
232 bool retval = false; |
1664
|
233 |
2894
|
234 string oct_file = oct_file_in_path (fcn_name); |
707
|
235 |
1907
|
236 if (! oct_file.empty ()) |
707
|
237 { |
2894
|
238 string mangled_name = instance->mangle_name (fcn_name); |
707
|
239 |
2969
|
240 builtin_fcn_installer f |
|
241 = instance->resolve_reference (mangled_name, oct_file); |
707
|
242 |
3072
|
243 |
|
244 // XXX FIXME XXX -- this should probably be handled correctly by |
|
245 // mangle_octave_oct_file_name using a configure test. |
|
246 |
3157
|
247 // Perhaps we should always check for both forms of the name and |
|
248 // issue a warning if they both exist? (I still think it would |
|
249 // be best to use some configure test to determine exactly what |
|
250 // form of the symbol name we should be looking for...) |
|
251 |
3072
|
252 if (! f) |
|
253 { |
|
254 string t = "_"; |
|
255 |
|
256 mangled_name = t.append (mangled_name); |
|
257 |
|
258 f = instance->resolve_reference (mangled_name, oct_file); |
|
259 } |
|
260 |
707
|
261 if (f) |
2969
|
262 retval = f (); |
707
|
263 } |
|
264 |
2893
|
265 return retval; |
|
266 } |
|
267 |
2969
|
268 octave_dynamic_loader::builtin_fcn_installer |
2894
|
269 octave_dynamic_loader::resolve_reference (const string&, const string&) |
2893
|
270 { |
|
271 return 0; |
|
272 } |
707
|
273 |
2894
|
274 string |
|
275 octave_dynamic_loader::mangle_name (const string& name) |
|
276 { |
|
277 string retval ("FS"); |
|
278 retval.append (name); |
|
279 retval.append ("__Fv"); |
|
280 return retval; |
|
281 } |
707
|
282 |
1
|
283 /* |
|
284 ;;; Local Variables: *** |
|
285 ;;; mode: C++ *** |
|
286 ;;; End: *** |
|
287 */ |