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