3326
|
1 /* |
|
2 |
|
3 Copyright (C) 1999 John W. Eaton |
|
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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
4110
|
27 #if defined (HAVE_SHL_LOAD_API) |
3326
|
28 #include <cerrno> |
|
29 #include <cstring> |
|
30 #endif |
|
31 |
4162
|
32 #if defined (HAVE_DYLD_API) |
|
33 #include <Mach-O/dyld.h> |
|
34 #endif |
|
35 |
3326
|
36 extern "C" |
|
37 { |
4110
|
38 #if defined (HAVE_DLOPEN_API) |
3326
|
39 #if defined (HAVE_DLFCN_H) |
|
40 #include <dlfcn.h> |
|
41 #else |
|
42 extern void *dlopen (const char *, int); |
|
43 extern const char *dlerror (void); |
|
44 extern void *dlsym (void *, const char *); |
|
45 extern int dlclose (void *); |
|
46 #endif |
|
47 #ifndef RTLD_LAZY |
|
48 #define RTLD_LAZY 1 |
|
49 #endif |
4110
|
50 #elif defined (HAVE_SHL_LOAD_API) |
3326
|
51 #include <dl.h> |
|
52 #endif |
|
53 } |
|
54 |
|
55 #include "file-stat.h" |
|
56 #include "lo-error.h" |
|
57 #include "oct-shlib.h" |
|
58 #include "str-vec.h" |
|
59 |
|
60 class |
|
61 octave_base_shlib : public octave_shlib |
|
62 { |
|
63 public: |
|
64 |
|
65 octave_base_shlib (void) |
|
66 : octave_shlib (octave_xshlib ()), file (), fcn_names (), |
|
67 tm_loaded (static_cast<time_t> (0)) |
|
68 { count = 1; } |
|
69 |
3504
|
70 octave_base_shlib (const std::string& f) |
3326
|
71 : octave_shlib (octave_xshlib ()), file (f), fcn_names (), |
|
72 tm_loaded (static_cast<time_t> (0)) |
|
73 { count = 1; } |
|
74 |
|
75 ~octave_base_shlib (void) { } |
|
76 |
3504
|
77 void open (const std::string&, bool = false) { } |
3326
|
78 |
3504
|
79 void *search (const std::string&, name_mangler = 0) { return 0; } |
3326
|
80 |
|
81 void close (octave_shlib::close_hook = 0) { } |
|
82 |
3504
|
83 bool remove (const std::string& fcn_name); |
3326
|
84 |
|
85 bool is_open (void) const { return false; } |
|
86 |
|
87 bool is_out_of_date (void) const; |
|
88 |
|
89 int number_of_functions_loaded (void) const { return fcn_names.length (); } |
|
90 |
3504
|
91 std::string file_name (void) const { return file; } |
3326
|
92 |
|
93 octave_time time_loaded (void) const { return tm_loaded; } |
|
94 |
|
95 protected: |
|
96 |
3504
|
97 std::string file; |
3326
|
98 |
|
99 string_vector fcn_names; |
|
100 |
|
101 octave_time tm_loaded; |
|
102 |
|
103 void stamp_time (bool warn_future = false); |
|
104 |
3504
|
105 void add_to_fcn_names (const std::string& name); |
3326
|
106 |
|
107 void do_close_hook (octave_shlib::close_hook = 0); |
|
108 |
|
109 void tabula_rasa (void); |
|
110 |
|
111 // No copying! |
|
112 |
|
113 octave_base_shlib (const octave_base_shlib&); |
|
114 |
|
115 octave_base_shlib& operator = (const octave_base_shlib&); |
|
116 }; |
|
117 |
|
118 bool |
3504
|
119 octave_base_shlib::remove (const std::string& fcn_name) |
3326
|
120 { |
|
121 bool retval = false; |
|
122 |
|
123 int n = number_of_functions_loaded (); |
|
124 |
|
125 string_vector new_fcn_names (n); |
|
126 |
|
127 int k = 0; |
|
128 |
|
129 for (int i = 0; i < n; i++) |
|
130 { |
|
131 if (fcn_names(i) == fcn_name) |
|
132 retval = true; |
|
133 else |
|
134 new_fcn_names(k++) = fcn_names(i); |
|
135 } |
|
136 |
|
137 new_fcn_names.resize (k); |
|
138 |
|
139 fcn_names = new_fcn_names; |
|
140 |
|
141 return retval; |
|
142 } |
|
143 |
|
144 bool |
|
145 octave_base_shlib::is_out_of_date (void) const |
|
146 { |
|
147 file_stat fs (file); |
|
148 |
|
149 return fs.is_newer (tm_loaded); |
|
150 } |
|
151 |
|
152 void |
|
153 octave_base_shlib::stamp_time (bool warn_future) |
|
154 { |
|
155 tm_loaded.stamp (); |
|
156 |
|
157 if (warn_future) |
|
158 { |
|
159 file_stat fs (file); |
|
160 |
|
161 if (fs.is_newer (tm_loaded)) |
|
162 (*current_liboctave_warning_handler) |
|
163 ("timestamp on file %s is in the future", file.c_str ()); |
|
164 } |
|
165 } |
|
166 |
|
167 void |
3504
|
168 octave_base_shlib::add_to_fcn_names (const std::string& name) |
3326
|
169 { |
|
170 int n = number_of_functions_loaded (); |
|
171 |
|
172 for (int i = 0; i < n; i++) |
|
173 if (fcn_names(i) == name) |
|
174 return; |
|
175 |
|
176 fcn_names.resize (n+1); |
|
177 |
|
178 fcn_names(n) = name; |
|
179 } |
|
180 |
|
181 void |
|
182 octave_base_shlib::do_close_hook (octave_shlib::close_hook cl_hook) |
|
183 { |
|
184 int n = number_of_functions_loaded (); |
|
185 |
|
186 for (int i = 0; i < n; i++) |
|
187 cl_hook (fcn_names(i)); |
|
188 } |
|
189 |
|
190 void |
|
191 octave_base_shlib::tabula_rasa (void) |
|
192 { |
|
193 file = ""; |
|
194 |
|
195 fcn_names.resize (0); |
|
196 |
|
197 tm_loaded = static_cast<time_t> (0); |
|
198 } |
|
199 |
4110
|
200 #if defined (HAVE_DLOPEN_API) |
3326
|
201 |
|
202 class |
|
203 octave_dlopen_shlib : public octave_base_shlib |
|
204 { |
|
205 public: |
|
206 |
|
207 octave_dlopen_shlib (void); |
|
208 |
|
209 ~octave_dlopen_shlib (void); |
|
210 |
3504
|
211 void open (const std::string& f, bool warn_future = false); |
3326
|
212 |
3504
|
213 void *search (const std::string& name, name_mangler mangler = 0); |
3326
|
214 |
|
215 void close (octave_shlib::close_hook cl_hook = 0); |
|
216 |
|
217 bool is_open (void) const { return (library != 0); } |
|
218 |
|
219 private: |
|
220 |
|
221 // No copying! |
|
222 |
|
223 octave_dlopen_shlib (const octave_dlopen_shlib&); |
|
224 |
|
225 octave_dlopen_shlib& operator = (const octave_dlopen_shlib&); |
|
226 |
|
227 void *library; |
|
228 }; |
|
229 |
|
230 octave_dlopen_shlib::octave_dlopen_shlib (void) |
|
231 : octave_base_shlib (), library (0) |
|
232 { |
|
233 } |
|
234 |
|
235 octave_dlopen_shlib::~octave_dlopen_shlib (void) |
|
236 { |
|
237 close (); |
|
238 } |
|
239 |
|
240 void |
3504
|
241 octave_dlopen_shlib::open (const std::string& f, bool warn_future) |
3326
|
242 { |
|
243 if (! is_open ()) |
|
244 { |
|
245 file = f; |
|
246 |
|
247 library = dlopen (file.c_str (), RTLD_LAZY); |
|
248 |
|
249 if (library) |
|
250 stamp_time (warn_future); |
|
251 else |
|
252 { |
|
253 const char *msg = dlerror (); |
|
254 |
|
255 if (msg) |
|
256 (*current_liboctave_error_handler) ("%s", msg); |
|
257 } |
|
258 } |
|
259 else |
|
260 (*current_liboctave_error_handler) |
|
261 ("shared library %s is already open", file.c_str ()); |
|
262 } |
|
263 |
|
264 void * |
3504
|
265 octave_dlopen_shlib::search (const std::string& name, |
3326
|
266 octave_shlib::name_mangler mangler) |
|
267 { |
|
268 void *function = 0; |
|
269 |
|
270 if (is_open ()) |
|
271 { |
3504
|
272 std::string sym_name = name; |
3326
|
273 |
|
274 if (mangler) |
|
275 sym_name = mangler (name); |
|
276 |
|
277 function = dlsym (library, sym_name.c_str ()); |
|
278 |
|
279 if (function) |
|
280 add_to_fcn_names (name); |
|
281 } |
|
282 else |
|
283 (*current_liboctave_error_handler) |
|
284 ("shared library %s is not open", file.c_str ()); |
|
285 |
|
286 return function; |
|
287 } |
|
288 |
|
289 void |
|
290 octave_dlopen_shlib::close (octave_shlib::close_hook cl_hook) |
|
291 { |
|
292 if (is_open ()) |
|
293 { |
|
294 do_close_hook (cl_hook); |
|
295 |
|
296 dlclose (library); |
|
297 |
|
298 library = 0; |
|
299 |
|
300 tabula_rasa (); |
|
301 } |
|
302 } |
|
303 |
4110
|
304 #elif defined (HAVE_SHL_LOAD_API) |
3326
|
305 |
|
306 class |
|
307 octave_shl_load_shlib : public octave_base_shlib |
|
308 { |
|
309 public: |
|
310 |
|
311 octave_shl_load_shlib (void); |
|
312 |
|
313 ~octave_shl_load_shlib (void); |
|
314 |
3504
|
315 void open (const std::string& f, bool warn_future = false); |
3326
|
316 |
3504
|
317 void *search (const std::string& name, name_mangler mangler = 0); |
3326
|
318 |
|
319 void close (octave_shlib::close_hook cl_hook = 0); |
|
320 |
3609
|
321 bool is_open (void) const { return (library != 0); } |
3326
|
322 |
|
323 private: |
|
324 |
|
325 // No copying! |
|
326 |
|
327 octave_shl_load_shlib (const octave_shl_load_shlib&); |
|
328 |
|
329 octave_shl_load_shlib& operator = (const octave_shl_load_shlib&); |
|
330 |
|
331 shl_t library; |
|
332 }; |
|
333 |
|
334 octave_shl_load_shlib::octave_shl_load_shlib (void) |
|
335 : octave_base_shlib (), library (0) |
|
336 { |
|
337 } |
|
338 |
|
339 octave_shl_load_shlib::~octave_shl_load_shlib (void) |
|
340 { |
|
341 close (); |
|
342 } |
|
343 |
|
344 void |
3504
|
345 octave_shl_load_shlib::open (const std::string& f, bool warn_future) |
3326
|
346 { |
|
347 if (! is_open ()) |
|
348 { |
|
349 file = f; |
|
350 |
|
351 library = shl_load (file.c_str (), BIND_DEFERRED, 0L); |
|
352 |
|
353 if (library) |
|
354 stamp_time (warn_future); |
|
355 else |
3504
|
356 { |
|
357 using namespace std; |
|
358 (*current_liboctave_error_handler) ("%s", strerror (errno)); |
|
359 } |
3326
|
360 } |
|
361 else |
|
362 (*current_liboctave_error_handler) |
|
363 ("shared library %s is already open", file.c_str ()); |
|
364 } |
|
365 |
|
366 void * |
3504
|
367 octave_shl_load_shlib::search (const std::string& name, |
3326
|
368 octave_shlib::name_mangler mangler) |
|
369 { |
|
370 void *function = 0; |
|
371 |
|
372 if (is_open ()) |
|
373 { |
3609
|
374 std::string sym_name = name; |
3326
|
375 |
|
376 if (mangler) |
|
377 sym_name = mangler (name); |
|
378 |
|
379 int status = shl_findsym (&library, sym_name.c_str (), |
|
380 TYPE_UNDEFINED, &function); |
|
381 |
|
382 if (status == 0) |
|
383 add_to_fcn_names (name); |
|
384 } |
|
385 else |
|
386 (*current_liboctave_error_handler) |
|
387 ("shared library %s is not open", file.c_str ()); |
|
388 |
|
389 return function; |
|
390 } |
|
391 |
|
392 void |
|
393 octave_shl_load_shlib::close (octave_shlib::close_hook cl_hook) |
|
394 { |
|
395 if (is_open ()) |
|
396 { |
|
397 do_close_hook (cl_hook); |
|
398 |
|
399 shl_unload (library); |
|
400 |
|
401 library = 0; |
|
402 |
|
403 tabula_rasa (); |
|
404 } |
|
405 } |
|
406 |
4110
|
407 #elif defined (HAVE_LOADLIBRARY_API) |
|
408 |
|
409 class |
|
410 octave_w32_shlib: public octave_base_shlib |
|
411 { |
|
412 public: |
|
413 |
|
414 octave_w32_shlib (void); |
|
415 |
|
416 ~octave_w32_shlib (void); |
|
417 |
|
418 void open (const std::string& f, bool warn_future = false); |
|
419 |
|
420 void *search (const std::string& name, name_mangler mangler = 0); |
|
421 |
|
422 void close (octave_shlib::close_hook cl_hook = 0); |
|
423 |
|
424 bool is_open (void) const { return (handle != 0); } |
|
425 |
|
426 private: |
|
427 |
|
428 // No copying! |
|
429 |
|
430 octave_w32_shlib (const octave_w32_shlib&); |
|
431 |
|
432 octave_w32_shlib& operator = (const octave_w32_shlib&); |
|
433 |
|
434 HINSTANCE handle; |
|
435 }; |
|
436 |
|
437 octave_w32_shlib::octave_w32_shlib (void) |
|
438 : octave_base_shlib (), handle (0) |
|
439 { |
|
440 } |
|
441 |
|
442 octave_w32_shlib::~octave_w32_shlib (void) |
|
443 { |
|
444 close (); |
|
445 } |
|
446 |
|
447 void |
|
448 octave_w32_shlib::open (const std::string& f, bool warn_future) |
|
449 { |
|
450 if (! is_open ()) |
|
451 { |
|
452 file = f; |
|
453 |
|
454 handle = LoadLibrary (file.c_str ()); |
|
455 |
|
456 if (handle != NULL) |
|
457 stamp_time (warn_future); |
|
458 else |
|
459 { |
|
460 DWORD lastError = GetLastError (); |
|
461 char *msg; |
|
462 |
|
463 switch (lastError) |
|
464 { |
|
465 case ERROR_MOD_NOT_FOUND: |
|
466 case ERROR_DLL_NOT_FOUND: |
|
467 msg = "could not find library or dependents"; |
|
468 break; |
|
469 |
|
470 case ERROR_INVALID_DLL: |
|
471 msg = "library or its dependents are damaged"; |
|
472 break; |
|
473 |
|
474 case ERROR_DLL_INIT_FAILED: |
|
475 msg = "library initialization routine failed"; |
|
476 break; |
|
477 |
|
478 default: |
|
479 msg = "library open failed"; |
|
480 } |
|
481 |
|
482 (*current_liboctave_error_handler) ("%s: %s", msg, file.c_str ()); |
|
483 } |
|
484 } |
|
485 else |
|
486 (*current_liboctave_error_handler) |
|
487 ("shared library %s is already open", file.c_str ()); |
|
488 } |
|
489 |
|
490 void * |
|
491 octave_w32_shlib::search (const std::string& name, |
|
492 octave_shlib::name_mangler mangler) |
|
493 { |
|
494 void *function = 0; |
|
495 |
|
496 if (is_open ()) |
|
497 { |
|
498 std::string sym_name = name; |
|
499 |
|
500 if (mangler) |
|
501 sym_name = mangler (name); |
|
502 |
|
503 function |
|
504 = static_cast<void *> (GetProcAddress (handle, sym_name.c_str ())); |
|
505 |
|
506 if (function) |
|
507 add_to_fcn_names (name); |
|
508 } |
|
509 else |
|
510 (*current_liboctave_error_handler) |
|
511 ("shared library %s is not open", file.c_str ()); |
|
512 |
|
513 return function; |
|
514 } |
|
515 |
|
516 void |
|
517 octave_w32_shlib::close (octave_shlib::close_hook cl_hook) |
|
518 { |
|
519 if (is_open ()) |
|
520 { |
|
521 do_close_hook (cl_hook); |
|
522 |
|
523 FreeLibrary (handle); |
|
524 |
|
525 handle = 0; |
|
526 |
|
527 tabula_rasa (); |
|
528 } |
|
529 } |
|
530 |
4162
|
531 #elif defined (HAVE_DYLD_API) |
|
532 |
|
533 class |
|
534 octave_dyld_shlib : public octave_base_shlib |
|
535 { |
|
536 public: |
|
537 |
|
538 octave_dyld_shlib (void); |
|
539 |
|
540 ~octave_dyld_shlib (void); |
|
541 |
|
542 void open (const std::string& f, bool warn_future = false); |
|
543 |
|
544 void *search (const std::string& name, name_mangler mangler = 0); |
|
545 |
|
546 void close (octave_shlib::close_hook cl_hook = 0); |
|
547 |
|
548 bool is_open (void) const {return (isOpen); } |
|
549 |
|
550 private: |
|
551 |
|
552 // No copying! |
|
553 |
|
554 octave_dyld_shlib (const octave_dyld_shlib&); |
|
555 |
|
556 octave_dyld_shlib& operator = (const octave_dyld_shlib&); |
|
557 |
|
558 bool isOpen; |
|
559 NSObjectFileImage img; |
|
560 NSModule handle; |
|
561 }; |
|
562 |
|
563 octave_dyld_shlib::octave_dyld_shlib (void) |
|
564 : octave_base_shlib (), isOpen (false), handle (0) |
|
565 { |
|
566 } |
|
567 |
|
568 octave_dyld_shlib::~octave_dyld_shlib (void) |
|
569 { |
|
570 close (); |
|
571 } |
|
572 |
|
573 void |
|
574 octave_dyld_shlib::open (const std::string& f, bool warn_future) |
|
575 { |
|
576 int returnCode; |
|
577 |
|
578 if (! is_open ()) |
|
579 { |
|
580 file = f; |
|
581 |
|
582 returnCode = NSCreateObjectFileImageFromFile (file.c_str (), &img); |
|
583 |
|
584 if (NSObjectFileImageSuccess == returnCode) |
|
585 { |
|
586 handle = NSLinkModule (img, file.c_str (), |
|
587 (NSLINKMODULE_OPTION_RETURN_ON_ERROR |
|
588 | NSLINKMODULE_OPTION_PRIVATE)); |
|
589 if (handle) |
|
590 { |
|
591 stamp_time (warn_future); |
|
592 isOpen = true; |
|
593 } |
|
594 else |
|
595 { |
|
596 (*current_liboctave_error_handler) |
|
597 ("couldn't link module %s", file.c_str ()); |
|
598 } |
|
599 } |
|
600 else |
|
601 { |
|
602 (*current_liboctave_error_handler) |
|
603 ("got NSObjectFileImageReturnCode %d", returnCode); |
|
604 |
|
605 // XXX FIXME XXX -- should use NSLinkEditError () to get |
|
606 // more info on what went wrong. |
|
607 } |
|
608 } |
|
609 else |
|
610 { |
|
611 (*current_liboctave_error_handler) |
|
612 ("bundle %s is already open", file.c_str ()); |
|
613 } |
|
614 } |
|
615 |
|
616 void * |
|
617 octave_dyld_shlib::search (const std::string& name, |
|
618 octave_shlib::name_mangler mangler) |
|
619 { |
|
620 void *function = 0; |
|
621 |
|
622 if (is_open ()) |
|
623 { |
|
624 std::string sym_name = name; |
|
625 |
|
626 if (mangler) |
|
627 sym_name = mangler (name); |
|
628 |
|
629 NSSymbol symbol = NSLookupSymbolInModule (handle, sym_name.c_str ()); |
|
630 |
|
631 if (symbol) |
|
632 { |
|
633 function = NSAddressOfSymbol (symbol); |
|
634 add_to_fcn_names (name); |
|
635 } |
|
636 } |
|
637 else |
|
638 (*current_liboctave_error_handler) |
|
639 ("bundle %s is not open", file.c_str ()); |
|
640 |
|
641 return function; |
|
642 } |
|
643 |
|
644 void |
|
645 octave_dyld_shlib::close (octave_shlib::close_hook cl_hook) |
|
646 { |
|
647 if (is_open ()) |
|
648 { |
|
649 do_close_hook (cl_hook); |
|
650 |
|
651 NSUnLinkModule (handle, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); |
|
652 |
|
653 handle = 0; |
|
654 |
|
655 if (NSDestroyObjectFileImage (img)) |
|
656 isOpen = false; |
|
657 |
|
658 tabula_rasa (); |
|
659 } |
|
660 } |
|
661 |
3326
|
662 #endif |
|
663 |
|
664 octave_shlib * |
|
665 octave_shlib::make_shlib (void) |
|
666 { |
4110
|
667 #if defined (HAVE_DLOPEN_API) |
3326
|
668 return new octave_dlopen_shlib (); |
4110
|
669 #elif defined (HAVE_SHL_LOAD_API) |
3326
|
670 return new octave_shl_load_shlib (); |
4110
|
671 #elif defined (HAVE_LOADLIBRARY_API) |
|
672 return new octave_w32_shlib (); |
4162
|
673 #elif defined (HAVE_DYLD_API) |
|
674 return new octave_dyld_shlib (); |
3326
|
675 #else |
|
676 return new octave_base_shlib (); |
|
677 #endif |
|
678 } |
|
679 |
|
680 /* |
|
681 ;;; Local Variables: *** |
|
682 ;;; mode: C++ *** |
|
683 ;;; End: *** |
|
684 */ |