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