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 |
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 |
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) |
|
260 stamp_time (warn_future); |
|
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 { |
|
304 do_close_hook (cl_hook); |
|
305 |
|
306 dlclose (library); |
|
307 |
|
308 library = 0; |
|
309 |
|
310 tabula_rasa (); |
|
311 } |
|
312 } |
|
313 |
4110
|
314 #elif defined (HAVE_SHL_LOAD_API) |
3326
|
315 |
|
316 class |
|
317 octave_shl_load_shlib : public octave_base_shlib |
|
318 { |
|
319 public: |
|
320 |
|
321 octave_shl_load_shlib (void); |
|
322 |
|
323 ~octave_shl_load_shlib (void); |
|
324 |
3504
|
325 void open (const std::string& f, bool warn_future = false); |
3326
|
326 |
3504
|
327 void *search (const std::string& name, name_mangler mangler = 0); |
3326
|
328 |
|
329 void close (octave_shlib::close_hook cl_hook = 0); |
|
330 |
3609
|
331 bool is_open (void) const { return (library != 0); } |
3326
|
332 |
|
333 private: |
|
334 |
|
335 // No copying! |
|
336 |
|
337 octave_shl_load_shlib (const octave_shl_load_shlib&); |
|
338 |
|
339 octave_shl_load_shlib& operator = (const octave_shl_load_shlib&); |
|
340 |
|
341 shl_t library; |
|
342 }; |
|
343 |
|
344 octave_shl_load_shlib::octave_shl_load_shlib (void) |
|
345 : octave_base_shlib (), library (0) |
|
346 { |
|
347 } |
|
348 |
|
349 octave_shl_load_shlib::~octave_shl_load_shlib (void) |
|
350 { |
|
351 close (); |
|
352 } |
|
353 |
|
354 void |
3504
|
355 octave_shl_load_shlib::open (const std::string& f, bool warn_future) |
3326
|
356 { |
|
357 if (! is_open ()) |
|
358 { |
|
359 file = f; |
|
360 |
|
361 library = shl_load (file.c_str (), BIND_DEFERRED, 0L); |
|
362 |
|
363 if (library) |
|
364 stamp_time (warn_future); |
|
365 else |
3504
|
366 { |
|
367 using namespace std; |
|
368 (*current_liboctave_error_handler) ("%s", strerror (errno)); |
|
369 } |
3326
|
370 } |
|
371 else |
|
372 (*current_liboctave_error_handler) |
|
373 ("shared library %s is already open", file.c_str ()); |
|
374 } |
|
375 |
|
376 void * |
3504
|
377 octave_shl_load_shlib::search (const std::string& name, |
3326
|
378 octave_shlib::name_mangler mangler) |
|
379 { |
|
380 void *function = 0; |
|
381 |
|
382 if (is_open ()) |
|
383 { |
3609
|
384 std::string sym_name = name; |
3326
|
385 |
|
386 if (mangler) |
|
387 sym_name = mangler (name); |
|
388 |
|
389 int status = shl_findsym (&library, sym_name.c_str (), |
|
390 TYPE_UNDEFINED, &function); |
|
391 |
|
392 if (status == 0) |
|
393 add_to_fcn_names (name); |
|
394 } |
|
395 else |
|
396 (*current_liboctave_error_handler) |
|
397 ("shared library %s is not open", file.c_str ()); |
|
398 |
|
399 return function; |
|
400 } |
|
401 |
|
402 void |
|
403 octave_shl_load_shlib::close (octave_shlib::close_hook cl_hook) |
|
404 { |
|
405 if (is_open ()) |
|
406 { |
|
407 do_close_hook (cl_hook); |
|
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 |
|
428 void open (const std::string& f, bool warn_future = false); |
|
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 |
|
458 octave_w32_shlib::open (const std::string& f, bool warn_future) |
|
459 { |
|
460 if (! is_open ()) |
|
461 { |
|
462 file = f; |
|
463 |
|
464 handle = LoadLibrary (file.c_str ()); |
|
465 |
|
466 if (handle != NULL) |
|
467 stamp_time (warn_future); |
|
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 { |
|
535 do_close_hook (cl_hook); |
|
536 |
|
537 FreeLibrary (handle); |
|
538 |
|
539 handle = 0; |
|
540 |
|
541 tabula_rasa (); |
|
542 } |
|
543 } |
|
544 |
4162
|
545 #elif defined (HAVE_DYLD_API) |
|
546 |
|
547 class |
|
548 octave_dyld_shlib : public octave_base_shlib |
|
549 { |
|
550 public: |
|
551 |
|
552 octave_dyld_shlib (void); |
|
553 |
|
554 ~octave_dyld_shlib (void); |
|
555 |
|
556 void open (const std::string& f, bool warn_future = false); |
|
557 |
|
558 void *search (const std::string& name, name_mangler mangler = 0); |
|
559 |
|
560 void close (octave_shlib::close_hook cl_hook = 0); |
|
561 |
|
562 bool is_open (void) const {return (isOpen); } |
|
563 |
|
564 private: |
|
565 |
|
566 // No copying! |
|
567 |
|
568 octave_dyld_shlib (const octave_dyld_shlib&); |
|
569 |
|
570 octave_dyld_shlib& operator = (const octave_dyld_shlib&); |
|
571 |
|
572 bool isOpen; |
|
573 NSObjectFileImage img; |
|
574 NSModule handle; |
|
575 }; |
|
576 |
|
577 octave_dyld_shlib::octave_dyld_shlib (void) |
|
578 : octave_base_shlib (), isOpen (false), handle (0) |
|
579 { |
|
580 } |
|
581 |
|
582 octave_dyld_shlib::~octave_dyld_shlib (void) |
|
583 { |
|
584 close (); |
|
585 } |
|
586 |
|
587 void |
|
588 octave_dyld_shlib::open (const std::string& f, bool warn_future) |
|
589 { |
|
590 int returnCode; |
|
591 |
|
592 if (! is_open ()) |
|
593 { |
|
594 file = f; |
|
595 |
|
596 returnCode = NSCreateObjectFileImageFromFile (file.c_str (), &img); |
|
597 |
|
598 if (NSObjectFileImageSuccess == returnCode) |
|
599 { |
|
600 handle = NSLinkModule (img, file.c_str (), |
|
601 (NSLINKMODULE_OPTION_RETURN_ON_ERROR |
|
602 | NSLINKMODULE_OPTION_PRIVATE)); |
|
603 if (handle) |
|
604 { |
|
605 stamp_time (warn_future); |
|
606 isOpen = true; |
|
607 } |
|
608 else |
|
609 { |
|
610 (*current_liboctave_error_handler) |
|
611 ("couldn't link module %s", file.c_str ()); |
|
612 } |
|
613 } |
|
614 else |
|
615 { |
|
616 (*current_liboctave_error_handler) |
|
617 ("got NSObjectFileImageReturnCode %d", returnCode); |
|
618 |
5775
|
619 // FIXME -- should use NSLinkEditError () to get |
4162
|
620 // more info on what went wrong. |
|
621 } |
|
622 } |
|
623 else |
|
624 { |
|
625 (*current_liboctave_error_handler) |
|
626 ("bundle %s is already open", file.c_str ()); |
|
627 } |
|
628 } |
|
629 |
|
630 void * |
|
631 octave_dyld_shlib::search (const std::string& name, |
|
632 octave_shlib::name_mangler mangler) |
|
633 { |
|
634 void *function = 0; |
|
635 |
|
636 if (is_open ()) |
|
637 { |
|
638 std::string sym_name = name; |
|
639 |
|
640 if (mangler) |
|
641 sym_name = mangler (name); |
|
642 |
|
643 NSSymbol symbol = NSLookupSymbolInModule (handle, sym_name.c_str ()); |
|
644 |
|
645 if (symbol) |
|
646 { |
|
647 function = NSAddressOfSymbol (symbol); |
|
648 add_to_fcn_names (name); |
|
649 } |
|
650 } |
|
651 else |
|
652 (*current_liboctave_error_handler) |
|
653 ("bundle %s is not open", file.c_str ()); |
|
654 |
|
655 return function; |
|
656 } |
|
657 |
|
658 void |
|
659 octave_dyld_shlib::close (octave_shlib::close_hook cl_hook) |
|
660 { |
|
661 if (is_open ()) |
|
662 { |
|
663 do_close_hook (cl_hook); |
|
664 |
|
665 NSUnLinkModule (handle, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); |
|
666 |
|
667 handle = 0; |
|
668 |
|
669 if (NSDestroyObjectFileImage (img)) |
|
670 isOpen = false; |
|
671 |
|
672 tabula_rasa (); |
|
673 } |
|
674 } |
|
675 |
3326
|
676 #endif |
|
677 |
|
678 octave_shlib * |
|
679 octave_shlib::make_shlib (void) |
|
680 { |
4110
|
681 #if defined (HAVE_DLOPEN_API) |
3326
|
682 return new octave_dlopen_shlib (); |
4110
|
683 #elif defined (HAVE_SHL_LOAD_API) |
3326
|
684 return new octave_shl_load_shlib (); |
4110
|
685 #elif defined (HAVE_LOADLIBRARY_API) |
|
686 return new octave_w32_shlib (); |
4162
|
687 #elif defined (HAVE_DYLD_API) |
|
688 return new octave_dyld_shlib (); |
3326
|
689 #else |
|
690 return new octave_base_shlib (); |
|
691 #endif |
|
692 } |
|
693 |
|
694 /* |
|
695 ;;; Local Variables: *** |
|
696 ;;; mode: C++ *** |
|
697 ;;; End: *** |
|
698 */ |