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