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" |
721
|
45 #include "dirfns.h" |
1
|
46 #include "octave.h" |
|
47 #include "utils.h" |
|
48 #include "error.h" |
|
49 |
707
|
50 typedef builtin_function * (*Octave_builtin_fcn_struct_fcn)(void); |
|
51 |
|
52 // XXX FIXME XXX -- should this list be in a user-level variable, |
|
53 // with default taken from the environment? |
|
54 |
|
55 #ifndef STD_LIB_PATH |
|
56 #define STD_LIB_PATH "/lib:/usr/lib:/usr/local/lib" |
|
57 #endif |
|
58 |
|
59 #ifndef OCTAVE_LIB_PATH |
|
60 #define OCTAVE_LIB_PATH OCTAVE_LIBDIR ":" FLIB_PATH ":" CXXLIB_PATH |
|
61 #endif |
|
62 |
|
63 static char *lib_dir_path = OCTAVE_LIB_PATH ":" STD_LIB_PATH; |
|
64 |
|
65 // This is the list of interesting libraries that Octave is linked |
|
66 // with. Maybe it should include the readline, info, and kpathsea |
|
67 // libraries. Would there ever be a time that they would really be |
|
68 // needed? |
|
69 |
|
70 #ifndef SYSTEM_LIB_LIST |
|
71 #define SYSTEM_LIB_LIST "libtermcap.a:libm.a" ":" CXXLIB_LIST |
|
72 #endif |
|
73 |
|
74 #ifndef OCTAVE_LIB_LIST |
|
75 #define OCTAVE_LIB_LIST "liboctdld.a:liboctave.a:libcruft.a:libdld.a" |
|
76 #endif |
|
77 |
|
78 static char *lib_list = OCTAVE_LIB_LIST ":" FLIB_LIST ":" SYSTEM_LIB_LIST; |
|
79 |
|
80 static char * |
|
81 mangle_octave_builtin_name (const char *name) |
1
|
82 { |
707
|
83 char *tmp = strconcat (name, "__FRC13Octave_objecti"); |
|
84 char *retval = strconcat ("F", tmp); |
|
85 delete [] tmp; |
|
86 return retval; |
1
|
87 } |
|
88 |
707
|
89 static char * |
|
90 mangle_octave_oct_file_name (const char *name) |
1
|
91 { |
707
|
92 char *tmp = strconcat (name, "__Fv"); |
|
93 char *retval = strconcat ("FS", tmp); |
|
94 delete [] tmp; |
|
95 return retval; |
1
|
96 } |
|
97 |
707
|
98 #ifdef WITH_DLD |
|
99 |
1
|
100 static void |
|
101 octave_dld_init (void) |
|
102 { |
|
103 static int initialized = 0; |
|
104 |
|
105 if (! initialized) |
|
106 { |
721
|
107 static char *prog = make_absolute (raw_prog_name, |
|
108 the_current_working_directory); |
|
109 |
|
110 char *full_path = dld_find_executable (prog); |
707
|
111 |
527
|
112 if (full_path) |
1
|
113 { |
|
114 int status = dld_init (full_path); |
707
|
115 |
1
|
116 if (status != 0) |
707
|
117 error ("failed to load symbols from `%s'", full_path); |
1
|
118 else |
|
119 initialized = 1; |
|
120 } |
|
121 else |
721
|
122 { |
|
123 error ("octave_dld_init: can't find full path to `%s'", |
|
124 raw_prog_name); |
|
125 } |
707
|
126 } |
|
127 } |
|
128 |
|
129 static void |
|
130 octave_list_undefined_symbols (ostream& os) |
|
131 { |
|
132 char **list = dld_list_undefined_sym (); |
|
133 |
|
134 if (list) |
|
135 { |
|
136 os << "undefined symbols:\n\n"; |
|
137 for (int i = 0; i < dld_undefined_sym_count; i++) |
|
138 os << list[i] << "\n"; |
|
139 os << "\n"; |
1
|
140 } |
|
141 } |
|
142 |
707
|
143 static void * |
|
144 dld_octave_resolve_reference (const char *name, const char *file = 0) |
|
145 { |
|
146 dld_create_reference (name); |
|
147 |
|
148 if (file) |
|
149 { |
|
150 if (dld_link (file) != 0) |
|
151 { |
|
152 error ("failed to link file %s", file); |
|
153 return 0; |
|
154 } |
|
155 |
|
156 if (dld_function_executable_p (name)) |
|
157 return (void *) dld_get_func (name); |
|
158 } |
|
159 |
|
160 // For each library, try to find it in a list of directories, then |
|
161 // link to it. It would have been nice to use the kpathsea functions |
|
162 // here too, but calls to them can't be nested as they would need to |
|
163 // be here... |
|
164 |
|
165 char **libs = pathstring_to_vector (lib_list); |
|
166 char **ptr = libs; |
|
167 char *lib_list_elt; |
|
168 |
|
169 while ((lib_list_elt = *ptr++)) |
|
170 { |
|
171 char *lib = kpse_path_search (lib_dir_path, lib_list_elt, |
|
172 kpathsea_true); |
|
173 |
|
174 if (lib && dld_link (lib) != 0) |
|
175 { |
|
176 error ("failed to link library %s", lib); |
|
177 return 0; |
|
178 } |
|
179 |
|
180 if (dld_function_executable_p (name)) |
|
181 return (void *) dld_get_func (name); |
|
182 } |
|
183 |
|
184 // If we get here, there was a problem. |
|
185 |
|
186 ostrstream output_buf; |
|
187 octave_list_undefined_symbols (output_buf); |
|
188 char *msg = output_buf.str (); |
|
189 error (msg); |
|
190 delete [] msg; |
|
191 |
|
192 return 0; |
|
193 } |
|
194 |
|
195 static Octave_builtin_fcn |
|
196 dld_octave_builtin (const char *name) |
|
197 { |
|
198 Octave_builtin_fcn retval = 0; |
|
199 |
|
200 char *mangled_name = mangle_octave_builtin_name (name); |
|
201 |
|
202 retval = (Octave_builtin_fcn) dld_octave_resolve_reference (mangled_name); |
|
203 |
|
204 delete [] mangled_name; |
|
205 |
|
206 return retval; |
|
207 } |
|
208 |
|
209 static int |
|
210 dld_octave_oct_file (const char *name) |
|
211 { |
|
212 char *oct_file = oct_file_in_path (name); |
|
213 |
|
214 if (oct_file) |
|
215 { |
|
216 char *mangled_name = mangle_octave_oct_file_name (name); |
|
217 |
|
218 Octave_builtin_fcn_struct_fcn f = |
|
219 (Octave_builtin_fcn_struct_fcn) dld_octave_resolve_reference |
|
220 (mangled_name, oct_file); |
|
221 |
|
222 if (f) |
|
223 { |
|
224 builtin_function *s = f (); |
|
225 |
|
226 if (s) |
|
227 { |
|
228 install_builtin_function (s); |
|
229 return 1; |
|
230 } |
|
231 } |
|
232 |
|
233 delete [] oct_file; |
|
234 } |
|
235 |
|
236 return 0; |
|
237 } |
|
238 |
|
239 #endif |
|
240 |
|
241 Octave_builtin_fcn |
|
242 load_octave_builtin (const char *name) |
|
243 { |
|
244 #ifdef WITH_DLD |
|
245 return dld_octave_builtin (name); |
|
246 #else |
|
247 return 0; |
|
248 #endif |
|
249 } |
|
250 |
|
251 int |
|
252 load_octave_oct_file (const char *name) |
|
253 { |
|
254 #ifdef WITH_DLD |
|
255 return dld_octave_oct_file (name); |
|
256 #endif |
|
257 return 0; |
|
258 } |
|
259 |
|
260 void |
|
261 init_dynamic_linker (void) |
|
262 { |
|
263 #ifdef WITH_DLD |
|
264 octave_dld_init (); |
|
265 #endif |
|
266 } |
|
267 |
|
268 // OLD: |
|
269 |
|
270 #if 0 |
|
271 |
|
272 void |
|
273 octave_dld_tc2_unlink_by_symbol (const char *name, int hard) |
|
274 { |
|
275 // XXX FIXME XXX -- need to determine the name mangling scheme |
|
276 // automatically, in case it changes, or is different on different |
|
277 // systems, even if they have g++. |
|
278 |
|
279 char *mangled_fcn_name = strconcat (name, "__FRC13Octave_objecti"); |
|
280 |
|
281 int status = dld_unlink_by_symbol (mangled_fcn_name, hard); |
|
282 |
|
283 if (status != 0) |
|
284 dld_perror ("octave_dld_tc2_unlink_by_symbol"); |
|
285 |
|
286 delete [] mangled_fcn_name; |
|
287 } |
|
288 |
|
289 void |
|
290 octave_dld_tc2_unlink_by_file (const char *name, int hard) |
|
291 { |
|
292 int status = dld_unlink_by_file (name, hard); |
|
293 |
|
294 if (status != 0) |
|
295 dld_perror ("octave_dld_tc2_unlink_by_file"); |
|
296 } |
|
297 |
|
298 #endif |
|
299 |
1
|
300 /* |
|
301 ;;; Local Variables: *** |
|
302 ;;; mode: C++ *** |
|
303 ;;; page-delimiter: "^/\\*" *** |
|
304 ;;; End: *** |
|
305 */ |