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) |
|
37 #include <dlfcn.h> |
1755
|
38 #ifndef RTLD_LAZY |
|
39 #define RTLD_LAZY 1 |
|
40 #endif |
1664
|
41 #elif defined (WITH_SHL) |
|
42 #include <dl.h> |
1102
|
43 #endif |
1
|
44 } |
|
45 |
2492
|
46 #include <defaults.h> |
1352
|
47 #include "dirfns.h" |
|
48 #include "dynamic-ld.h" |
|
49 #include "error.h" |
1670
|
50 #include "toplev.h" |
1155
|
51 #include "pathsearch.h" |
2862
|
52 #include "oct-obj.h" |
2969
|
53 #include "ov-builtin.h" |
2370
|
54 #include "ov.h" |
1352
|
55 #include "utils.h" |
707
|
56 #include "variables.h" |
1
|
57 |
1664
|
58 #if defined (WITH_DL) |
|
59 |
2894
|
60 class |
|
61 octave_dlopen_dynamic_loader : public octave_dynamic_loader |
1664
|
62 { |
2894
|
63 public: |
|
64 |
|
65 octave_dlopen_dynamic_loader (void) |
|
66 : octave_dynamic_loader () { } |
|
67 |
|
68 ~octave_dlopen_dynamic_loader (void) { } |
|
69 |
|
70 private: |
|
71 |
2969
|
72 octave_dynamic_loader::builtin_fcn_installer |
2894
|
73 resolve_reference (const string& mangled_name, const string& file); |
|
74 |
|
75 // No copying! |
|
76 |
|
77 octave_dlopen_dynamic_loader (const octave_dlopen_dynamic_loader&); |
|
78 |
|
79 octave_dlopen_dynamic_loader& |
|
80 operator = (const octave_dlopen_dynamic_loader&); |
|
81 }; |
|
82 |
2969
|
83 octave_dynamic_loader::builtin_fcn_installer |
2894
|
84 octave_dlopen_dynamic_loader::resolve_reference (const string& name, |
|
85 const string& file) |
|
86 { |
2969
|
87 octave_dynamic_loader::builtin_fcn_installer retval = 0; |
1665
|
88 |
1664
|
89 // Dynamic linking with dlopen/dlsym doesn't require specification |
|
90 // of the libraries at runtime. Instead, they are specified when |
|
91 // the .oct file is created. |
|
92 |
1755
|
93 void *handle = dlopen (file.c_str (), RTLD_LAZY); |
1664
|
94 |
2894
|
95 const char *nm = name.c_str (); |
|
96 |
1664
|
97 if (handle) |
|
98 { |
2895
|
99 void *tmp = dlsym (handle, nm); |
|
100 |
2969
|
101 retval = static_cast<octave_dynamic_loader::builtin_fcn_installer> (tmp); |
1664
|
102 |
1665
|
103 if (! retval) |
1664
|
104 { |
|
105 const char *errmsg = dlerror (); |
|
106 |
|
107 if (errmsg) |
2894
|
108 error("%s: `%s'", nm, errmsg); |
1664
|
109 else |
2894
|
110 error("unable to link function `%s'", nm); |
1664
|
111 |
|
112 dlclose (handle); |
|
113 } |
|
114 } |
|
115 else |
2894
|
116 error ("%s: %s `%s'", dlerror (), file.c_str (), nm); |
1665
|
117 |
|
118 return retval; |
1664
|
119 } |
|
120 |
|
121 #elif defined (WITH_SHL) |
|
122 |
2894
|
123 class |
|
124 octave_shl_load_dynamic_loader : public octave_dynamic_loader |
1664
|
125 { |
2894
|
126 public: |
|
127 |
|
128 octave_shl_load_dynamic_loader (void) |
|
129 : octave_dynamic_loader () { } |
|
130 |
|
131 ~octave_shl_load_dynamic_loader (void) { } |
|
132 |
|
133 private: |
|
134 |
2969
|
135 octave_dynamic_loader::builtin_fcn_installer |
2894
|
136 resolve_reference (const string& mangled_name, const string& file); |
|
137 |
|
138 // No copying! |
|
139 |
|
140 octave_shl_load_dynamic_loader (const octave_shl_load_dynamic_loader&); |
|
141 |
|
142 octave_shl_load_dynamic_loader& |
|
143 operator = (const octave_shl_load_dynamic_loader&); |
|
144 }; |
|
145 |
2969
|
146 octave_dynamic_loader::builtin_fcn_installer |
2894
|
147 octave_shl_load_dynamic_loader::resolve_reference (const string& name, |
|
148 const string& file) |
|
149 { |
2969
|
150 octave_dynamic_loader::builtin_fcn_installer retval = 0; |
1665
|
151 |
1664
|
152 // Dynamic linking with shl_load/shl_findsym doesn't require |
|
153 // specification of the libraries at runtime. Instead, they are |
|
154 // specified when the .oct file is created. |
|
155 |
2800
|
156 shl_t handle = shl_load (file.c_str (), BIND_DEFERRED, 0L); |
1664
|
157 |
2894
|
158 const char *nm = name.c_str (); |
|
159 |
1664
|
160 if (handle) |
|
161 { |
2894
|
162 int status = shl_findsym (&handle, nm, TYPE_UNDEFINED, retval); |
1664
|
163 |
|
164 if (status < 0) |
|
165 { |
|
166 const char *errmsg = strerror (errno); |
|
167 |
|
168 if (errmsg) |
2894
|
169 error("%s: `%s'", nm, errmsg); |
1664
|
170 else |
2894
|
171 error("unable to link function `%s'", nm); |
1664
|
172 |
1665
|
173 retval = 0; |
1664
|
174 } |
|
175 } |
|
176 else |
2894
|
177 error ("%s: %s `%s'", strerror (errno), file.c_str (), nm); |
1665
|
178 |
|
179 return retval; |
1664
|
180 } |
|
181 |
1742
|
182 #endif |
1664
|
183 |
2926
|
184 octave_dynamic_loader *octave_dynamic_loader::instance = 0; |
|
185 |
|
186 bool |
|
187 octave_dynamic_loader::instance_ok (void) |
|
188 { |
|
189 bool retval = true; |
|
190 |
|
191 if (! instance) |
|
192 make_dynamic_loader (); |
|
193 |
|
194 if (! instance) |
|
195 { |
|
196 error ("unable to create command history object!"); |
|
197 |
|
198 retval = false; |
|
199 } |
|
200 |
|
201 return retval; |
|
202 } |
|
203 |
|
204 void |
|
205 octave_dynamic_loader::make_dynamic_loader (void) |
2894
|
206 { |
|
207 #if defined (WITH_DL) |
2926
|
208 instance = new octave_dlopen_dynamic_loader (); |
2894
|
209 #elif defined (WITH_SHL) |
2926
|
210 instance = new octave_sh_load_dynamic_loader (); |
2894
|
211 #else |
2926
|
212 instance = new octave_dynamic_loader (); |
2894
|
213 #endif |
|
214 } |
|
215 |
2969
|
216 bool |
2894
|
217 octave_dynamic_loader::load_fcn_from_dot_oct_file (const string& fcn_name) |
707
|
218 { |
2926
|
219 if (! instance_ok ()) |
|
220 make_dynamic_loader (); |
2894
|
221 |
2969
|
222 bool retval = false; |
1664
|
223 |
2894
|
224 string oct_file = oct_file_in_path (fcn_name); |
707
|
225 |
1907
|
226 if (! oct_file.empty ()) |
707
|
227 { |
2894
|
228 string mangled_name = instance->mangle_name (fcn_name); |
707
|
229 |
2969
|
230 builtin_fcn_installer f |
|
231 = instance->resolve_reference (mangled_name, oct_file); |
707
|
232 |
|
233 if (f) |
2969
|
234 retval = f (); |
707
|
235 } |
|
236 |
2893
|
237 return retval; |
|
238 } |
|
239 |
2969
|
240 octave_dynamic_loader::builtin_fcn_installer |
2894
|
241 octave_dynamic_loader::resolve_reference (const string&, const string&) |
2893
|
242 { |
|
243 return 0; |
|
244 } |
707
|
245 |
2894
|
246 string |
|
247 octave_dynamic_loader::mangle_name (const string& name) |
|
248 { |
|
249 string retval ("FS"); |
|
250 retval.append (name); |
|
251 retval.append ("__Fv"); |
|
252 return retval; |
|
253 } |
707
|
254 |
1
|
255 /* |
|
256 ;;; Local Variables: *** |
|
257 ;;; mode: C++ *** |
|
258 ;;; End: *** |
|
259 */ |