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> |
3326
|
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 |
5781
|
77 void open (const std::string&) { } |
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 |
5781
|
103 void stamp_time (void); |
3326
|
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 |
5781
|
153 octave_base_shlib::stamp_time (void) |
3326
|
154 { |
|
155 tm_loaded.stamp (); |
|
156 |
5781
|
157 file_stat fs (file); |
3326
|
158 |
5781
|
159 if (fs.is_newer (tm_loaded)) |
|
160 (*current_liboctave_warning_with_id_handler) |
|
161 ("Octave:warn-future-time-stamp", |
|
162 "timestamp on file %s is in the future", file.c_str ()); |
3326
|
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 |
5781
|
209 void open (const std::string& f); |
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 |
5781
|
239 octave_dlopen_shlib::open (const std::string& f) |
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) |
5781
|
258 stamp_time (); |
3326
|
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 { |
5864
|
302 if (cl_hook) |
|
303 do_close_hook (cl_hook); |
3326
|
304 |
|
305 dlclose (library); |
|
306 |
|
307 library = 0; |
|
308 |
|
309 tabula_rasa (); |
|
310 } |
|
311 } |
|
312 |
4110
|
313 #elif defined (HAVE_SHL_LOAD_API) |
3326
|
314 |
|
315 class |
|
316 octave_shl_load_shlib : public octave_base_shlib |
|
317 { |
|
318 public: |
|
319 |
|
320 octave_shl_load_shlib (void); |
|
321 |
|
322 ~octave_shl_load_shlib (void); |
|
323 |
5781
|
324 void open (const std::string& f); |
3326
|
325 |
3504
|
326 void *search (const std::string& name, name_mangler mangler = 0); |
3326
|
327 |
|
328 void close (octave_shlib::close_hook cl_hook = 0); |
|
329 |
3609
|
330 bool is_open (void) const { return (library != 0); } |
3326
|
331 |
|
332 private: |
|
333 |
|
334 // No copying! |
|
335 |
|
336 octave_shl_load_shlib (const octave_shl_load_shlib&); |
|
337 |
|
338 octave_shl_load_shlib& operator = (const octave_shl_load_shlib&); |
|
339 |
|
340 shl_t library; |
|
341 }; |
|
342 |
|
343 octave_shl_load_shlib::octave_shl_load_shlib (void) |
|
344 : octave_base_shlib (), library (0) |
|
345 { |
|
346 } |
|
347 |
|
348 octave_shl_load_shlib::~octave_shl_load_shlib (void) |
|
349 { |
|
350 close (); |
|
351 } |
|
352 |
|
353 void |
5781
|
354 octave_shl_load_shlib::open (const std::string& f) |
3326
|
355 { |
|
356 if (! is_open ()) |
|
357 { |
|
358 file = f; |
|
359 |
|
360 library = shl_load (file.c_str (), BIND_DEFERRED, 0L); |
|
361 |
|
362 if (library) |
5781
|
363 stamp_time (); |
3326
|
364 else |
3504
|
365 { |
|
366 using namespace std; |
|
367 (*current_liboctave_error_handler) ("%s", strerror (errno)); |
|
368 } |
3326
|
369 } |
|
370 else |
|
371 (*current_liboctave_error_handler) |
|
372 ("shared library %s is already open", file.c_str ()); |
|
373 } |
|
374 |
|
375 void * |
3504
|
376 octave_shl_load_shlib::search (const std::string& name, |
3326
|
377 octave_shlib::name_mangler mangler) |
|
378 { |
|
379 void *function = 0; |
|
380 |
|
381 if (is_open ()) |
|
382 { |
3609
|
383 std::string sym_name = name; |
3326
|
384 |
|
385 if (mangler) |
|
386 sym_name = mangler (name); |
|
387 |
|
388 int status = shl_findsym (&library, sym_name.c_str (), |
|
389 TYPE_UNDEFINED, &function); |
|
390 |
|
391 if (status == 0) |
|
392 add_to_fcn_names (name); |
|
393 } |
|
394 else |
|
395 (*current_liboctave_error_handler) |
|
396 ("shared library %s is not open", file.c_str ()); |
|
397 |
|
398 return function; |
|
399 } |
|
400 |
|
401 void |
|
402 octave_shl_load_shlib::close (octave_shlib::close_hook cl_hook) |
|
403 { |
|
404 if (is_open ()) |
|
405 { |
5864
|
406 if (cl_hook) |
|
407 do_close_hook (cl_hook); |
3326
|
408 |
|
409 shl_unload (library); |
|
410 |
|
411 library = 0; |
|
412 |
|
413 tabula_rasa (); |
|
414 } |
|
415 } |
|
416 |
4110
|
417 #elif defined (HAVE_LOADLIBRARY_API) |
|
418 |
|
419 class |
|
420 octave_w32_shlib: public octave_base_shlib |
|
421 { |
|
422 public: |
|
423 |
|
424 octave_w32_shlib (void); |
|
425 |
|
426 ~octave_w32_shlib (void); |
|
427 |
5781
|
428 void open (const std::string& f); |
4110
|
429 |
|
430 void *search (const std::string& name, name_mangler mangler = 0); |
|
431 |
|
432 void close (octave_shlib::close_hook cl_hook = 0); |
|
433 |
|
434 bool is_open (void) const { return (handle != 0); } |
|
435 |
|
436 private: |
|
437 |
|
438 // No copying! |
|
439 |
|
440 octave_w32_shlib (const octave_w32_shlib&); |
|
441 |
|
442 octave_w32_shlib& operator = (const octave_w32_shlib&); |
|
443 |
|
444 HINSTANCE handle; |
|
445 }; |
|
446 |
|
447 octave_w32_shlib::octave_w32_shlib (void) |
|
448 : octave_base_shlib (), handle (0) |
|
449 { |
|
450 } |
|
451 |
|
452 octave_w32_shlib::~octave_w32_shlib (void) |
|
453 { |
|
454 close (); |
|
455 } |
|
456 |
|
457 void |
5781
|
458 octave_w32_shlib::open (const std::string& f) |
4110
|
459 { |
|
460 if (! is_open ()) |
|
461 { |
|
462 file = f; |
|
463 |
|
464 handle = LoadLibrary (file.c_str ()); |
|
465 |
|
466 if (handle != NULL) |
5781
|
467 stamp_time (); |
4110
|
468 else |
|
469 { |
|
470 DWORD lastError = GetLastError (); |
|
471 char *msg; |
|
472 |
|
473 switch (lastError) |
|
474 { |
|
475 case ERROR_MOD_NOT_FOUND: |
|
476 case ERROR_DLL_NOT_FOUND: |
|
477 msg = "could not find library or dependents"; |
|
478 break; |
|
479 |
|
480 case ERROR_INVALID_DLL: |
|
481 msg = "library or its dependents are damaged"; |
|
482 break; |
|
483 |
|
484 case ERROR_DLL_INIT_FAILED: |
|
485 msg = "library initialization routine failed"; |
|
486 break; |
|
487 |
|
488 default: |
|
489 msg = "library open failed"; |
|
490 } |
|
491 |
|
492 (*current_liboctave_error_handler) ("%s: %s", msg, file.c_str ()); |
|
493 } |
|
494 } |
|
495 else |
|
496 (*current_liboctave_error_handler) |
|
497 ("shared library %s is already open", file.c_str ()); |
|
498 } |
|
499 |
5451
|
500 extern "C" |
|
501 { |
|
502 void * octave_w32_search (HINSTANCE handle, const char * name); |
|
503 } |
|
504 |
4110
|
505 void * |
|
506 octave_w32_shlib::search (const std::string& name, |
|
507 octave_shlib::name_mangler mangler) |
|
508 { |
|
509 void *function = 0; |
|
510 |
|
511 if (is_open ()) |
|
512 { |
|
513 std::string sym_name = name; |
|
514 |
|
515 if (mangler) |
|
516 sym_name = mangler (name); |
|
517 |
5451
|
518 function = octave_w32_library_search (handle, sym_name.c_str ()); |
4110
|
519 |
|
520 if (function) |
|
521 add_to_fcn_names (name); |
|
522 } |
|
523 else |
|
524 (*current_liboctave_error_handler) |
|
525 ("shared library %s is not open", file.c_str ()); |
|
526 |
|
527 return function; |
|
528 } |
|
529 |
|
530 void |
|
531 octave_w32_shlib::close (octave_shlib::close_hook cl_hook) |
|
532 { |
|
533 if (is_open ()) |
|
534 { |
5864
|
535 if (cl_hook) |
|
536 do_close_hook (cl_hook); |
4110
|
537 |
|
538 FreeLibrary (handle); |
|
539 |
|
540 handle = 0; |
|
541 |
|
542 tabula_rasa (); |
|
543 } |
|
544 } |
|
545 |
4162
|
546 #elif defined (HAVE_DYLD_API) |
|
547 |
|
548 class |
|
549 octave_dyld_shlib : public octave_base_shlib |
|
550 { |
|
551 public: |
|
552 |
|
553 octave_dyld_shlib (void); |
|
554 |
|
555 ~octave_dyld_shlib (void); |
|
556 |
5781
|
557 void open (const std::string& f); |
4162
|
558 |
|
559 void *search (const std::string& name, name_mangler mangler = 0); |
|
560 |
|
561 void close (octave_shlib::close_hook cl_hook = 0); |
|
562 |
|
563 bool is_open (void) const {return (isOpen); } |
|
564 |
|
565 private: |
|
566 |
|
567 // No copying! |
|
568 |
|
569 octave_dyld_shlib (const octave_dyld_shlib&); |
|
570 |
|
571 octave_dyld_shlib& operator = (const octave_dyld_shlib&); |
|
572 |
|
573 bool isOpen; |
|
574 NSObjectFileImage img; |
|
575 NSModule handle; |
|
576 }; |
|
577 |
|
578 octave_dyld_shlib::octave_dyld_shlib (void) |
|
579 : octave_base_shlib (), isOpen (false), handle (0) |
|
580 { |
|
581 } |
|
582 |
|
583 octave_dyld_shlib::~octave_dyld_shlib (void) |
|
584 { |
|
585 close (); |
|
586 } |
|
587 |
|
588 void |
5781
|
589 octave_dyld_shlib::open (const std::string& f) |
4162
|
590 { |
|
591 int returnCode; |
|
592 |
|
593 if (! is_open ()) |
|
594 { |
|
595 file = f; |
|
596 |
|
597 returnCode = NSCreateObjectFileImageFromFile (file.c_str (), &img); |
|
598 |
|
599 if (NSObjectFileImageSuccess == returnCode) |
|
600 { |
|
601 handle = NSLinkModule (img, file.c_str (), |
|
602 (NSLINKMODULE_OPTION_RETURN_ON_ERROR |
|
603 | NSLINKMODULE_OPTION_PRIVATE)); |
|
604 if (handle) |
|
605 { |
5781
|
606 stamp_time (); |
4162
|
607 isOpen = true; |
|
608 } |
|
609 else |
|
610 { |
|
611 (*current_liboctave_error_handler) |
|
612 ("couldn't link module %s", file.c_str ()); |
|
613 } |
|
614 } |
|
615 else |
|
616 { |
|
617 (*current_liboctave_error_handler) |
|
618 ("got NSObjectFileImageReturnCode %d", returnCode); |
|
619 |
5775
|
620 // FIXME -- should use NSLinkEditError () to get |
4162
|
621 // more info on what went wrong. |
|
622 } |
|
623 } |
|
624 else |
|
625 { |
|
626 (*current_liboctave_error_handler) |
|
627 ("bundle %s is already open", file.c_str ()); |
|
628 } |
|
629 } |
|
630 |
|
631 void * |
|
632 octave_dyld_shlib::search (const std::string& name, |
|
633 octave_shlib::name_mangler mangler) |
|
634 { |
|
635 void *function = 0; |
|
636 |
|
637 if (is_open ()) |
|
638 { |
|
639 std::string sym_name = name; |
|
640 |
|
641 if (mangler) |
|
642 sym_name = mangler (name); |
|
643 |
|
644 NSSymbol symbol = NSLookupSymbolInModule (handle, sym_name.c_str ()); |
|
645 |
|
646 if (symbol) |
|
647 { |
|
648 function = NSAddressOfSymbol (symbol); |
|
649 add_to_fcn_names (name); |
|
650 } |
|
651 } |
|
652 else |
|
653 (*current_liboctave_error_handler) |
|
654 ("bundle %s is not open", file.c_str ()); |
|
655 |
|
656 return function; |
|
657 } |
|
658 |
|
659 void |
|
660 octave_dyld_shlib::close (octave_shlib::close_hook cl_hook) |
|
661 { |
|
662 if (is_open ()) |
|
663 { |
|
664 do_close_hook (cl_hook); |
|
665 |
|
666 NSUnLinkModule (handle, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); |
|
667 |
|
668 handle = 0; |
|
669 |
|
670 if (NSDestroyObjectFileImage (img)) |
|
671 isOpen = false; |
|
672 |
|
673 tabula_rasa (); |
|
674 } |
|
675 } |
|
676 |
3326
|
677 #endif |
|
678 |
|
679 octave_shlib * |
|
680 octave_shlib::make_shlib (void) |
|
681 { |
4110
|
682 #if defined (HAVE_DLOPEN_API) |
3326
|
683 return new octave_dlopen_shlib (); |
4110
|
684 #elif defined (HAVE_SHL_LOAD_API) |
3326
|
685 return new octave_shl_load_shlib (); |
4110
|
686 #elif defined (HAVE_LOADLIBRARY_API) |
|
687 return new octave_w32_shlib (); |
4162
|
688 #elif defined (HAVE_DYLD_API) |
|
689 return new octave_dyld_shlib (); |
3326
|
690 #else |
|
691 return new octave_base_shlib (); |
|
692 #endif |
|
693 } |
|
694 |
|
695 /* |
|
696 ;;; Local Variables: *** |
|
697 ;;; mode: C++ *** |
|
698 ;;; End: *** |
|
699 */ |