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 |
|
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 { |
1102
|
32 #ifdef WITH_DLD |
707
|
33 #include <dld/dld.h> |
1102
|
34 #endif |
1
|
35 } |
|
36 |
1155
|
37 #include "pathsearch.h" |
1
|
38 #include "dynamic-ld.h" |
|
39 #include "tree-const.h" |
|
40 #include "user-prefs.h" |
707
|
41 #include "variables.h" |
|
42 #include "defaults.h" |
721
|
43 #include "dirfns.h" |
1
|
44 #include "octave.h" |
|
45 #include "utils.h" |
|
46 #include "error.h" |
|
47 |
707
|
48 typedef builtin_function * (*Octave_builtin_fcn_struct_fcn)(void); |
|
49 |
|
50 // XXX FIXME XXX -- should this list be in a user-level variable, |
|
51 // with default taken from the environment? |
|
52 |
733
|
53 #ifdef WITH_DLD |
|
54 |
707
|
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 |
|
98 static void |
|
99 octave_dld_init (void) |
|
100 { |
|
101 static int initialized = 0; |
|
102 |
|
103 if (! initialized) |
|
104 { |
825
|
105 char *full_path = 0; |
721
|
106 |
825
|
107 char *tmp = dld_find_executable (raw_prog_name); |
|
108 if (tmp) |
|
109 { |
|
110 full_path = make_absolute (tmp, the_current_working_directory); |
|
111 free (tmp); |
|
112 } |
707
|
113 |
527
|
114 if (full_path) |
1
|
115 { |
|
116 int status = dld_init (full_path); |
707
|
117 |
1
|
118 if (status != 0) |
707
|
119 error ("failed to load symbols from `%s'", full_path); |
1
|
120 else |
|
121 initialized = 1; |
|
122 } |
|
123 else |
721
|
124 { |
|
125 error ("octave_dld_init: can't find full path to `%s'", |
|
126 raw_prog_name); |
|
127 } |
707
|
128 } |
|
129 } |
|
130 |
|
131 static void |
|
132 octave_list_undefined_symbols (ostream& os) |
|
133 { |
|
134 char **list = dld_list_undefined_sym (); |
|
135 |
|
136 if (list) |
|
137 { |
|
138 os << "undefined symbols:\n\n"; |
|
139 for (int i = 0; i < dld_undefined_sym_count; i++) |
|
140 os << list[i] << "\n"; |
|
141 os << "\n"; |
1
|
142 } |
|
143 } |
|
144 |
707
|
145 static void * |
|
146 dld_octave_resolve_reference (const char *name, const char *file = 0) |
|
147 { |
|
148 dld_create_reference (name); |
|
149 |
|
150 if (file) |
|
151 { |
|
152 if (dld_link (file) != 0) |
|
153 { |
|
154 error ("failed to link file %s", file); |
|
155 return 0; |
|
156 } |
|
157 |
|
158 if (dld_function_executable_p (name)) |
|
159 return (void *) dld_get_func (name); |
|
160 } |
|
161 |
|
162 // For each library, try to find it in a list of directories, then |
|
163 // link to it. It would have been nice to use the kpathsea functions |
|
164 // here too, but calls to them can't be nested as they would need to |
|
165 // be here... |
|
166 |
|
167 char **libs = pathstring_to_vector (lib_list); |
|
168 char **ptr = libs; |
|
169 char *lib_list_elt; |
|
170 |
|
171 while ((lib_list_elt = *ptr++)) |
|
172 { |
|
173 char *lib = kpse_path_search (lib_dir_path, lib_list_elt, |
|
174 kpathsea_true); |
|
175 |
|
176 if (lib && dld_link (lib) != 0) |
|
177 { |
|
178 error ("failed to link library %s", lib); |
|
179 return 0; |
|
180 } |
|
181 |
|
182 if (dld_function_executable_p (name)) |
|
183 return (void *) dld_get_func (name); |
|
184 } |
|
185 |
|
186 // If we get here, there was a problem. |
|
187 |
|
188 ostrstream output_buf; |
|
189 octave_list_undefined_symbols (output_buf); |
|
190 char *msg = output_buf.str (); |
|
191 error (msg); |
|
192 delete [] msg; |
|
193 |
|
194 return 0; |
|
195 } |
|
196 |
|
197 static Octave_builtin_fcn |
|
198 dld_octave_builtin (const char *name) |
|
199 { |
|
200 Octave_builtin_fcn retval = 0; |
|
201 |
|
202 char *mangled_name = mangle_octave_builtin_name (name); |
|
203 |
|
204 retval = (Octave_builtin_fcn) dld_octave_resolve_reference (mangled_name); |
|
205 |
|
206 delete [] mangled_name; |
|
207 |
|
208 return retval; |
|
209 } |
|
210 |
|
211 static int |
|
212 dld_octave_oct_file (const char *name) |
|
213 { |
|
214 char *oct_file = oct_file_in_path (name); |
|
215 |
|
216 if (oct_file) |
|
217 { |
|
218 char *mangled_name = mangle_octave_oct_file_name (name); |
|
219 |
|
220 Octave_builtin_fcn_struct_fcn f = |
|
221 (Octave_builtin_fcn_struct_fcn) dld_octave_resolve_reference |
|
222 (mangled_name, oct_file); |
|
223 |
|
224 if (f) |
|
225 { |
|
226 builtin_function *s = f (); |
|
227 |
|
228 if (s) |
|
229 { |
|
230 install_builtin_function (s); |
|
231 return 1; |
|
232 } |
|
233 } |
|
234 |
|
235 delete [] oct_file; |
|
236 } |
|
237 |
|
238 return 0; |
|
239 } |
|
240 |
|
241 #endif |
|
242 |
|
243 Octave_builtin_fcn |
|
244 load_octave_builtin (const char *name) |
|
245 { |
|
246 #ifdef WITH_DLD |
|
247 return dld_octave_builtin (name); |
|
248 #else |
|
249 return 0; |
|
250 #endif |
|
251 } |
|
252 |
|
253 int |
|
254 load_octave_oct_file (const char *name) |
|
255 { |
|
256 #ifdef WITH_DLD |
|
257 return dld_octave_oct_file (name); |
|
258 #endif |
|
259 return 0; |
|
260 } |
|
261 |
|
262 void |
|
263 init_dynamic_linker (void) |
|
264 { |
|
265 #ifdef WITH_DLD |
|
266 octave_dld_init (); |
|
267 #endif |
|
268 } |
|
269 |
|
270 // OLD: |
|
271 |
|
272 #if 0 |
|
273 |
|
274 void |
|
275 octave_dld_tc2_unlink_by_symbol (const char *name, int hard) |
|
276 { |
|
277 // XXX FIXME XXX -- need to determine the name mangling scheme |
|
278 // automatically, in case it changes, or is different on different |
|
279 // systems, even if they have g++. |
|
280 |
|
281 char *mangled_fcn_name = strconcat (name, "__FRC13Octave_objecti"); |
|
282 |
|
283 int status = dld_unlink_by_symbol (mangled_fcn_name, hard); |
|
284 |
|
285 if (status != 0) |
762
|
286 error ("octave_dld_tc2_unlink_by_symbol: %s", dld_strerror (0)); |
707
|
287 |
|
288 delete [] mangled_fcn_name; |
|
289 } |
|
290 |
|
291 void |
|
292 octave_dld_tc2_unlink_by_file (const char *name, int hard) |
|
293 { |
|
294 int status = dld_unlink_by_file (name, hard); |
|
295 |
|
296 if (status != 0) |
762
|
297 error ("octave_dld_tc2_unlink_by_file: %s", dld_strerror (0)); |
707
|
298 } |
|
299 |
|
300 #endif |
|
301 |
1
|
302 /* |
|
303 ;;; Local Variables: *** |
|
304 ;;; mode: C++ *** |
|
305 ;;; page-delimiter: "^/\\*" *** |
|
306 ;;; End: *** |
|
307 */ |