1
|
1 // dynamic-ld.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1664
|
28 #if defined (WITH_SHL) |
|
29 #include <cerrno> |
|
30 #include <cstring> |
|
31 #endif |
|
32 |
707
|
33 #include <strstream.h> |
|
34 |
1
|
35 extern "C" |
|
36 { |
1664
|
37 #if defined (WITH_DL) |
|
38 #include <dlfcn.h> |
|
39 #elif defined (WITH_SHL) |
|
40 #include <dl.h> |
|
41 #elif defined (WITH_DLD) |
707
|
42 #include <dld/dld.h> |
1102
|
43 #endif |
1
|
44 } |
|
45 |
1352
|
46 #include "defaults.h" |
|
47 #include "dirfns.h" |
|
48 #include "dynamic-ld.h" |
|
49 #include "error.h" |
|
50 #include "octave.h" |
1155
|
51 #include "pathsearch.h" |
1
|
52 #include "tree-const.h" |
|
53 #include "user-prefs.h" |
1352
|
54 #include "utils.h" |
707
|
55 #include "variables.h" |
1
|
56 |
707
|
57 typedef builtin_function * (*Octave_builtin_fcn_struct_fcn)(void); |
|
58 |
1664
|
59 #if defined (WITH_DYNAMIC_LINKING) |
|
60 |
|
61 // XXX FIXME XXX -- need to provide some way to ensure that functions |
|
62 // that we are going to use will use the same naming convention as |
|
63 // Octave's internal functions. It needs to be simpler than the |
|
64 // current DEFUN_DLD() macro, which assumes you know how to name the |
|
65 // function, the struct, and the helper function. |
|
66 |
|
67 static char * |
|
68 mangle_octave_builtin_name (const char *name) |
|
69 { |
|
70 char *tmp = strconcat (name, "__FRC13Octave_objecti"); |
|
71 char *retval = strconcat ("F", tmp); |
|
72 delete [] tmp; |
|
73 return retval; |
|
74 } |
|
75 |
|
76 static char * |
|
77 mangle_octave_oct_file_name (const char *name) |
|
78 { |
|
79 char *tmp = strconcat (name, "__Fv"); |
|
80 char *retval = strconcat ("FS", tmp); |
|
81 delete [] tmp; |
|
82 return retval; |
|
83 } |
|
84 |
|
85 #endif |
|
86 |
|
87 #if defined (WITH_DL) |
|
88 |
|
89 static void * |
|
90 dl_resolve_octave_reference (const char *name, const char *file) |
|
91 { |
1665
|
92 void *retval = 0; |
|
93 |
1664
|
94 // Dynamic linking with dlopen/dlsym doesn't require specification |
|
95 // of the libraries at runtime. Instead, they are specified when |
|
96 // the .oct file is created. |
|
97 |
|
98 void *handle = dlopen (file, RTLD_LAZY); |
|
99 |
|
100 if (handle) |
|
101 { |
1665
|
102 retval = dlsym (handle, name); |
1664
|
103 |
1665
|
104 if (! retval) |
1664
|
105 { |
|
106 const char *errmsg = dlerror (); |
|
107 |
|
108 if (errmsg) |
|
109 error("%s: `%s'", name, errmsg); |
|
110 else |
|
111 error("unable to link function `%s'", name); |
|
112 |
|
113 dlclose (handle); |
|
114 } |
|
115 } |
|
116 else |
1665
|
117 error ("%s: %s `%s'", dlerror (), file, name); |
|
118 |
|
119 return retval; |
1664
|
120 } |
|
121 |
|
122 #elif defined (WITH_SHL) |
|
123 |
|
124 static void * |
|
125 shl_resolve_octave_reference (const char *name, const char *file) |
|
126 { |
1665
|
127 void *retval = 0; |
|
128 |
1664
|
129 // Dynamic linking with shl_load/shl_findsym doesn't require |
|
130 // specification of the libraries at runtime. Instead, they are |
|
131 // specified when the .oct file is created. |
|
132 |
|
133 void *handle = shl_load (file, BIND_DEFERRED, 0L); |
|
134 |
|
135 if (handle) |
|
136 { |
|
137 int status = shl_findsym ((shl_t *) &handle, name, |
1665
|
138 TYPE_UNDEFINED, retval); |
1664
|
139 |
|
140 if (status < 0) |
|
141 { |
|
142 const char *errmsg = strerror (errno); |
|
143 |
|
144 if (errmsg) |
|
145 error("%s: `%s'", name, errmsg); |
|
146 else |
|
147 error("unable to link function `%s'", name); |
|
148 |
1665
|
149 retval = 0; |
1664
|
150 } |
|
151 } |
|
152 else |
1665
|
153 error ("%s: %s `%s'", strerror (errno), file, name); |
|
154 |
|
155 return retval; |
1664
|
156 } |
|
157 |
|
158 #elif defined (WITH_DLD) |
|
159 |
|
160 // Now that we have the code above to do dynamic linking with the |
|
161 // dlopen/dlsym interface and Linux uses elf, I doubt that this code |
|
162 // will be used very much. Maybe it will be able to go away |
|
163 // eventually. Consider it unsupported... |
|
164 |
707
|
165 // XXX FIXME XXX -- should this list be in a user-level variable, |
|
166 // with default taken from the environment? |
|
167 |
|
168 #ifndef STD_LIB_PATH |
|
169 #define STD_LIB_PATH "/lib:/usr/lib:/usr/local/lib" |
|
170 #endif |
|
171 |
|
172 #ifndef OCTAVE_LIB_PATH |
|
173 #define OCTAVE_LIB_PATH OCTAVE_LIBDIR ":" FLIB_PATH ":" CXXLIB_PATH |
|
174 #endif |
|
175 |
|
176 static char *lib_dir_path = OCTAVE_LIB_PATH ":" STD_LIB_PATH; |
|
177 |
|
178 // This is the list of interesting libraries that Octave is linked |
|
179 // with. Maybe it should include the readline, info, and kpathsea |
|
180 // libraries. Would there ever be a time that they would really be |
|
181 // needed? |
|
182 |
|
183 #ifndef SYSTEM_LIB_LIST |
|
184 #define SYSTEM_LIB_LIST "libtermcap.a:libm.a" ":" CXXLIB_LIST |
|
185 #endif |
|
186 |
|
187 #ifndef OCTAVE_LIB_LIST |
|
188 #define OCTAVE_LIB_LIST "liboctdld.a:liboctave.a:libcruft.a:libdld.a" |
|
189 #endif |
|
190 |
|
191 static char *lib_list = OCTAVE_LIB_LIST ":" FLIB_LIST ":" SYSTEM_LIB_LIST; |
|
192 |
1
|
193 static void |
|
194 octave_dld_init (void) |
|
195 { |
|
196 static int initialized = 0; |
|
197 |
|
198 if (! initialized) |
|
199 { |
825
|
200 char *full_path = 0; |
721
|
201 |
825
|
202 char *tmp = dld_find_executable (raw_prog_name); |
|
203 if (tmp) |
|
204 { |
|
205 full_path = make_absolute (tmp, the_current_working_directory); |
|
206 free (tmp); |
|
207 } |
707
|
208 |
527
|
209 if (full_path) |
1
|
210 { |
|
211 int status = dld_init (full_path); |
707
|
212 |
1
|
213 if (status != 0) |
707
|
214 error ("failed to load symbols from `%s'", full_path); |
1
|
215 else |
|
216 initialized = 1; |
|
217 } |
|
218 else |
721
|
219 { |
|
220 error ("octave_dld_init: can't find full path to `%s'", |
|
221 raw_prog_name); |
|
222 } |
707
|
223 } |
|
224 } |
|
225 |
|
226 static void |
|
227 octave_list_undefined_symbols (ostream& os) |
|
228 { |
|
229 char **list = dld_list_undefined_sym (); |
|
230 |
|
231 if (list) |
|
232 { |
|
233 os << "undefined symbols:\n\n"; |
|
234 for (int i = 0; i < dld_undefined_sym_count; i++) |
|
235 os << list[i] << "\n"; |
|
236 os << "\n"; |
1
|
237 } |
|
238 } |
|
239 |
707
|
240 static void * |
1664
|
241 dld_resolve_octave_reference (const char *name, const char *file) |
707
|
242 { |
|
243 dld_create_reference (name); |
|
244 |
|
245 if (file) |
|
246 { |
|
247 if (dld_link (file) != 0) |
|
248 { |
1664
|
249 error ("failed to link file `%s'", file); |
707
|
250 return 0; |
|
251 } |
|
252 |
|
253 if (dld_function_executable_p (name)) |
|
254 return (void *) dld_get_func (name); |
|
255 } |
|
256 |
1358
|
257 // For each library, try to find it in a list of directories, then |
|
258 // link to it. It would have been nice to use the kpathsea |
|
259 // functions here too, but calls to them can't be nested as they |
|
260 // would need to be here... |
707
|
261 |
|
262 char **libs = pathstring_to_vector (lib_list); |
|
263 char **ptr = libs; |
|
264 char *lib_list_elt; |
|
265 |
|
266 while ((lib_list_elt = *ptr++)) |
|
267 { |
|
268 char *lib = kpse_path_search (lib_dir_path, lib_list_elt, |
|
269 kpathsea_true); |
|
270 |
|
271 if (lib && dld_link (lib) != 0) |
|
272 { |
|
273 error ("failed to link library %s", lib); |
|
274 return 0; |
|
275 } |
|
276 |
|
277 if (dld_function_executable_p (name)) |
|
278 return (void *) dld_get_func (name); |
|
279 } |
|
280 |
1358
|
281 // If we get here, there was a problem. |
707
|
282 |
|
283 ostrstream output_buf; |
|
284 octave_list_undefined_symbols (output_buf); |
|
285 char *msg = output_buf.str (); |
|
286 error (msg); |
|
287 delete [] msg; |
|
288 |
|
289 return 0; |
|
290 } |
|
291 |
1664
|
292 #endif |
|
293 |
|
294 static void * |
|
295 resolve_octave_reference (const char *name, const char *file = 0) |
|
296 { |
|
297 #if defined (WITH_DL) |
|
298 |
1665
|
299 return dl_resolve_octave_reference (name, file); |
1664
|
300 |
|
301 #elif defined (WITH_SHL) |
|
302 |
1665
|
303 return shl_resolve_octave_reference (name, file); |
1664
|
304 |
|
305 #elif defined (WITH_DLD) |
|
306 |
1665
|
307 return dld_resolve_octave_reference (name, file); |
1664
|
308 |
|
309 #endif |
|
310 } |
|
311 |
|
312 Octave_builtin_fcn |
|
313 load_octave_builtin (const char *name) |
707
|
314 { |
|
315 Octave_builtin_fcn retval = 0; |
|
316 |
1664
|
317 #if defined (WITH_DYNAMIC_LINKING) |
|
318 |
707
|
319 char *mangled_name = mangle_octave_builtin_name (name); |
|
320 |
1664
|
321 retval = (Octave_builtin_fcn) resolve_octave_reference (mangled_name); |
707
|
322 |
|
323 delete [] mangled_name; |
|
324 |
1664
|
325 #endif |
|
326 |
707
|
327 return retval; |
|
328 } |
|
329 |
1664
|
330 int |
|
331 load_octave_oct_file (const char *name) |
707
|
332 { |
1664
|
333 int retval = 0; |
|
334 |
|
335 #if defined (WITH_DYNAMIC_LINKING) |
|
336 |
707
|
337 char *oct_file = oct_file_in_path (name); |
|
338 |
|
339 if (oct_file) |
|
340 { |
|
341 char *mangled_name = mangle_octave_oct_file_name (name); |
|
342 |
|
343 Octave_builtin_fcn_struct_fcn f = |
1664
|
344 (Octave_builtin_fcn_struct_fcn) resolve_octave_reference |
707
|
345 (mangled_name, oct_file); |
|
346 |
|
347 if (f) |
|
348 { |
|
349 builtin_function *s = f (); |
|
350 |
|
351 if (s) |
|
352 { |
|
353 install_builtin_function (s); |
1664
|
354 retval = 1; |
707
|
355 } |
|
356 } |
|
357 |
|
358 delete [] oct_file; |
|
359 } |
|
360 |
1664
|
361 #else |
|
362 |
|
363 (void) name; |
707
|
364 |
|
365 #endif |
|
366 |
1664
|
367 return retval; |
707
|
368 } |
|
369 |
|
370 void |
|
371 init_dynamic_linker (void) |
|
372 { |
1664
|
373 #if defined (WITH_DLD) |
|
374 |
707
|
375 octave_dld_init (); |
1664
|
376 |
707
|
377 #endif |
|
378 } |
|
379 |
1
|
380 /* |
|
381 ;;; Local Variables: *** |
|
382 ;;; mode: C++ *** |
|
383 ;;; page-delimiter: "^/\\*" *** |
|
384 ;;; End: *** |
|
385 */ |