1
|
1 // dynamic-ld.cc -*- C++ -*- |
|
2 /* |
|
3 |
499
|
4 Copyright (C) 1993, 1994 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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
707
|
28 #include <strstream.h> |
|
29 |
1
|
30 extern "C" |
|
31 { |
707
|
32 #include <dld/dld.h> |
|
33 |
|
34 #define boolean kpathsea_boolean |
|
35 #define false kpathsea_false |
|
36 #define true kpathsea_true |
|
37 #include <kpathsea/pathsearch.h> |
1
|
38 } |
|
39 |
|
40 #include "dynamic-ld.h" |
|
41 #include "tree-const.h" |
|
42 #include "user-prefs.h" |
707
|
43 #include "variables.h" |
|
44 #include "defaults.h" |
1
|
45 #include "octave.h" |
|
46 #include "utils.h" |
|
47 #include "error.h" |
|
48 |
707
|
49 typedef builtin_function * (*Octave_builtin_fcn_struct_fcn)(void); |
|
50 |
|
51 // XXX FIXME XXX -- should this list be in a user-level variable, |
|
52 // with default taken from the environment? |
|
53 |
|
54 #ifndef STD_LIB_PATH |
|
55 #define STD_LIB_PATH "/lib:/usr/lib:/usr/local/lib" |
|
56 #endif |
|
57 |
|
58 #ifndef OCTAVE_LIB_PATH |
|
59 #define OCTAVE_LIB_PATH OCTAVE_LIBDIR ":" FLIB_PATH ":" CXXLIB_PATH |
|
60 #endif |
|
61 |
|
62 static char *lib_dir_path = OCTAVE_LIB_PATH ":" STD_LIB_PATH; |
|
63 |
|
64 // This is the list of interesting libraries that Octave is linked |
|
65 // with. Maybe it should include the readline, info, and kpathsea |
|
66 // libraries. Would there ever be a time that they would really be |
|
67 // needed? |
|
68 |
|
69 #ifndef SYSTEM_LIB_LIST |
|
70 #define SYSTEM_LIB_LIST "libtermcap.a:libm.a" ":" CXXLIB_LIST |
|
71 #endif |
|
72 |
|
73 #ifndef OCTAVE_LIB_LIST |
|
74 #define OCTAVE_LIB_LIST "liboctdld.a:liboctave.a:libcruft.a:libdld.a" |
|
75 #endif |
|
76 |
|
77 static char *lib_list = OCTAVE_LIB_LIST ":" FLIB_LIST ":" SYSTEM_LIB_LIST; |
|
78 |
|
79 static char * |
|
80 mangle_octave_builtin_name (const char *name) |
1
|
81 { |
707
|
82 char *tmp = strconcat (name, "__FRC13Octave_objecti"); |
|
83 char *retval = strconcat ("F", tmp); |
|
84 delete [] tmp; |
|
85 return retval; |
1
|
86 } |
|
87 |
707
|
88 static char * |
|
89 mangle_octave_oct_file_name (const char *name) |
1
|
90 { |
707
|
91 char *tmp = strconcat (name, "__Fv"); |
|
92 char *retval = strconcat ("FS", tmp); |
|
93 delete [] tmp; |
|
94 return retval; |
1
|
95 } |
|
96 |
707
|
97 #ifdef WITH_DLD |
|
98 |
1
|
99 static void |
|
100 octave_dld_init (void) |
|
101 { |
|
102 static int initialized = 0; |
|
103 |
|
104 if (! initialized) |
|
105 { |
|
106 char *full_path = dld_find_executable (raw_prog_name); |
707
|
107 |
527
|
108 if (full_path) |
1
|
109 { |
|
110 int status = dld_init (full_path); |
707
|
111 |
1
|
112 if (status != 0) |
707
|
113 error ("failed to load symbols from `%s'", full_path); |
1
|
114 else |
|
115 initialized = 1; |
|
116 } |
|
117 else |
707
|
118 error ("octave_dld_init: can't find full path to `%s'", prog_name); |
|
119 } |
|
120 } |
|
121 |
|
122 static void |
|
123 octave_list_undefined_symbols (ostream& os) |
|
124 { |
|
125 char **list = dld_list_undefined_sym (); |
|
126 |
|
127 if (list) |
|
128 { |
|
129 os << "undefined symbols:\n\n"; |
|
130 for (int i = 0; i < dld_undefined_sym_count; i++) |
|
131 os << list[i] << "\n"; |
|
132 os << "\n"; |
1
|
133 } |
|
134 } |
|
135 |
707
|
136 static void * |
|
137 dld_octave_resolve_reference (const char *name, const char *file = 0) |
|
138 { |
|
139 dld_create_reference (name); |
|
140 |
|
141 if (file) |
|
142 { |
|
143 if (dld_link (file) != 0) |
|
144 { |
|
145 error ("failed to link file %s", file); |
|
146 return 0; |
|
147 } |
|
148 |
|
149 if (dld_function_executable_p (name)) |
|
150 return (void *) dld_get_func (name); |
|
151 } |
|
152 |
|
153 // For each library, try to find it in a list of directories, then |
|
154 // link to it. It would have been nice to use the kpathsea functions |
|
155 // here too, but calls to them can't be nested as they would need to |
|
156 // be here... |
|
157 |
|
158 char **libs = pathstring_to_vector (lib_list); |
|
159 char **ptr = libs; |
|
160 char *lib_list_elt; |
|
161 |
|
162 while ((lib_list_elt = *ptr++)) |
|
163 { |
|
164 char *lib = kpse_path_search (lib_dir_path, lib_list_elt, |
|
165 kpathsea_true); |
|
166 |
|
167 if (lib && dld_link (lib) != 0) |
|
168 { |
|
169 error ("failed to link library %s", lib); |
|
170 return 0; |
|
171 } |
|
172 |
|
173 if (dld_function_executable_p (name)) |
|
174 return (void *) dld_get_func (name); |
|
175 } |
|
176 |
|
177 // If we get here, there was a problem. |
|
178 |
|
179 ostrstream output_buf; |
|
180 octave_list_undefined_symbols (output_buf); |
|
181 char *msg = output_buf.str (); |
|
182 error (msg); |
|
183 delete [] msg; |
|
184 |
|
185 return 0; |
|
186 } |
|
187 |
|
188 static Octave_builtin_fcn |
|
189 dld_octave_builtin (const char *name) |
|
190 { |
|
191 Octave_builtin_fcn retval = 0; |
|
192 |
|
193 char *mangled_name = mangle_octave_builtin_name (name); |
|
194 |
|
195 retval = (Octave_builtin_fcn) dld_octave_resolve_reference (mangled_name); |
|
196 |
|
197 delete [] mangled_name; |
|
198 |
|
199 return retval; |
|
200 } |
|
201 |
|
202 static int |
|
203 dld_octave_oct_file (const char *name) |
|
204 { |
|
205 char *oct_file = oct_file_in_path (name); |
|
206 |
|
207 if (oct_file) |
|
208 { |
|
209 char *mangled_name = mangle_octave_oct_file_name (name); |
|
210 |
|
211 Octave_builtin_fcn_struct_fcn f = |
|
212 (Octave_builtin_fcn_struct_fcn) dld_octave_resolve_reference |
|
213 (mangled_name, oct_file); |
|
214 |
|
215 if (f) |
|
216 { |
|
217 builtin_function *s = f (); |
|
218 |
|
219 if (s) |
|
220 { |
|
221 install_builtin_function (s); |
|
222 return 1; |
|
223 } |
|
224 } |
|
225 |
|
226 delete [] oct_file; |
|
227 } |
|
228 |
|
229 return 0; |
|
230 } |
|
231 |
|
232 #endif |
|
233 |
|
234 Octave_builtin_fcn |
|
235 load_octave_builtin (const char *name) |
|
236 { |
|
237 #ifdef WITH_DLD |
|
238 return dld_octave_builtin (name); |
|
239 #else |
|
240 return 0; |
|
241 #endif |
|
242 } |
|
243 |
|
244 int |
|
245 load_octave_oct_file (const char *name) |
|
246 { |
|
247 #ifdef WITH_DLD |
|
248 return dld_octave_oct_file (name); |
|
249 #endif |
|
250 return 0; |
|
251 } |
|
252 |
|
253 void |
|
254 init_dynamic_linker (void) |
|
255 { |
|
256 #ifdef WITH_DLD |
|
257 octave_dld_init (); |
|
258 #endif |
|
259 } |
|
260 |
|
261 // OLD: |
|
262 |
|
263 #if 0 |
|
264 |
|
265 void |
|
266 octave_dld_tc2_unlink_by_symbol (const char *name, int hard) |
|
267 { |
|
268 // XXX FIXME XXX -- need to determine the name mangling scheme |
|
269 // automatically, in case it changes, or is different on different |
|
270 // systems, even if they have g++. |
|
271 |
|
272 char *mangled_fcn_name = strconcat (name, "__FRC13Octave_objecti"); |
|
273 |
|
274 int status = dld_unlink_by_symbol (mangled_fcn_name, hard); |
|
275 |
|
276 if (status != 0) |
|
277 dld_perror ("octave_dld_tc2_unlink_by_symbol"); |
|
278 |
|
279 delete [] mangled_fcn_name; |
|
280 } |
|
281 |
|
282 void |
|
283 octave_dld_tc2_unlink_by_file (const char *name, int hard) |
|
284 { |
|
285 int status = dld_unlink_by_file (name, hard); |
|
286 |
|
287 if (status != 0) |
|
288 dld_perror ("octave_dld_tc2_unlink_by_file"); |
|
289 } |
|
290 |
|
291 // Look for object in path. It should provide a definition for the |
|
292 // function we just marked as undefined. If we find it, we'll also |
|
293 // try to load the remaining undefined symbols. |
|
294 |
1
|
295 static int |
164
|
296 octave_dld_link (const char *object) |
1
|
297 { |
527
|
298 char *file = file_in_path (object, 0); |
707
|
299 |
1
|
300 int status = dld_link (file); |
707
|
301 |
1
|
302 if (status != 0) |
|
303 dld_perror ("octave_dld_link"); |
|
304 |
|
305 return status; |
|
306 } |
|
307 |
|
308 int |
164
|
309 octave_dld_tc2_link (const char *object) |
1
|
310 { |
707
|
311 static char *ol = octave_lib_dir (); |
|
312 static char *liboctave = strconcat (ol, "/liboctave.a"); |
|
313 static char *libcruft = strconcat (ol, "/libcruft.a"); |
|
314 |
1
|
315 int status = octave_dld_link (object); |
707
|
316 |
1
|
317 if (status == 0) |
|
318 { |
707
|
319 status = octave_dld_link (liboctave); |
|
320 |
1
|
321 if (status == 0) |
707
|
322 octave_dld_link (libcruft); |
1
|
323 } |
707
|
324 |
1
|
325 return status; |
|
326 } |
|
327 |
707
|
328 Octave_builtin_fcn |
|
329 octave_dld_tc2 (const char *name) |
1
|
330 { |
707
|
331 Octave_builtin_fcn retval = 0; |
1
|
332 |
|
333 octave_dld_init (); |
|
334 |
527
|
335 // XXX FIXME XXX -- need to determine the name mangling scheme |
|
336 // automatically, in case it changes, or is different on different |
|
337 // systems, even if they have g++. |
707
|
338 char *mangled_fcn_name = strconcat (name, "__FRC13Octave_objecti"); |
1
|
339 |
|
340 // See if the function has already been loaded. If not, mark it as |
|
341 // undefined. |
|
342 |
|
343 if (dld_get_func (mangled_fcn_name) == 0) |
|
344 dld_create_reference (mangled_fcn_name); |
|
345 |
707
|
346 static char *ol = octave_lib_dir (); |
|
347 static char *liboctdld = strconcat (ol, "/liboctdld.a"); |
|
348 |
|
349 int status = octave_dld_tc2_link (liboctdld); |
|
350 |
1
|
351 if (status == 0) |
|
352 { |
|
353 // Return a pointer to the function we just loaded. If we can\'t find |
|
354 // it, this will return NULL. |
|
355 |
707
|
356 retval = (Octave_builtin_fcn) dld_get_func (mangled_fcn_name); |
1
|
357 } |
|
358 |
|
359 delete [] mangled_fcn_name; |
|
360 |
|
361 return retval; |
|
362 |
|
363 } |
|
364 |
499
|
365 Octave_object |
506
|
366 octave_dld_tc2_and_go (const Octave_object& args, int nargout, |
707
|
367 const char *name) |
1
|
368 { |
499
|
369 Octave_object retval; |
1
|
370 |
707
|
371 Octave_builtin_fcn fcn_to_call = octave_dld_tc2 (name); |
1
|
372 |
527
|
373 if (fcn_to_call) |
506
|
374 retval = (*fcn_to_call) (args, nargout); |
1
|
375 else |
|
376 error ("octave_dld_tc2_and_go: failed to load `%s'", name); |
|
377 |
|
378 return retval; |
|
379 } |
|
380 |
707
|
381 #endif |
|
382 |
1
|
383 /* |
|
384 ;;; Local Variables: *** |
|
385 ;;; mode: C++ *** |
|
386 ;;; page-delimiter: "^/\\*" *** |
|
387 ;;; End: *** |
|
388 */ |