3326
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1999, 2000, 2002, 2003, 2005, 2006, 2007 John W. Eaton |
3326
|
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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
3326
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
3326
|
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> |
5451
|
49 #elif defined (HAVE_LOADLIBRARY_API) |
|
50 #include <windows.h> |
6123
|
51 #undef min |
|
52 #undef max |
3326
|
53 #endif |
|
54 } |
|
55 |
|
56 #include "file-stat.h" |
|
57 #include "lo-error.h" |
|
58 #include "oct-shlib.h" |
|
59 #include "str-vec.h" |
|
60 |
|
61 class |
|
62 octave_base_shlib : public octave_shlib |
|
63 { |
|
64 public: |
|
65 |
|
66 octave_base_shlib (void) |
|
67 : octave_shlib (octave_xshlib ()), file (), fcn_names (), |
|
68 tm_loaded (static_cast<time_t> (0)) |
|
69 { count = 1; } |
|
70 |
3504
|
71 octave_base_shlib (const std::string& f) |
3326
|
72 : octave_shlib (octave_xshlib ()), file (f), fcn_names (), |
|
73 tm_loaded (static_cast<time_t> (0)) |
|
74 { count = 1; } |
|
75 |
|
76 ~octave_base_shlib (void) { } |
|
77 |
5781
|
78 void open (const std::string&) { } |
3326
|
79 |
3504
|
80 void *search (const std::string&, name_mangler = 0) { return 0; } |
3326
|
81 |
|
82 void close (octave_shlib::close_hook = 0) { } |
|
83 |
3504
|
84 bool remove (const std::string& fcn_name); |
3326
|
85 |
|
86 bool is_open (void) const { return false; } |
|
87 |
|
88 bool is_out_of_date (void) const; |
|
89 |
|
90 int number_of_functions_loaded (void) const { return fcn_names.length (); } |
|
91 |
3504
|
92 std::string file_name (void) const { return file; } |
3326
|
93 |
|
94 octave_time time_loaded (void) const { return tm_loaded; } |
|
95 |
|
96 protected: |
|
97 |
3504
|
98 std::string file; |
3326
|
99 |
|
100 string_vector fcn_names; |
|
101 |
|
102 octave_time tm_loaded; |
|
103 |
5781
|
104 void stamp_time (void); |
3326
|
105 |
3504
|
106 void add_to_fcn_names (const std::string& name); |
3326
|
107 |
|
108 void do_close_hook (octave_shlib::close_hook = 0); |
|
109 |
|
110 void tabula_rasa (void); |
|
111 |
|
112 // No copying! |
|
113 |
|
114 octave_base_shlib (const octave_base_shlib&); |
|
115 |
|
116 octave_base_shlib& operator = (const octave_base_shlib&); |
|
117 }; |
|
118 |
|
119 bool |
3504
|
120 octave_base_shlib::remove (const std::string& fcn_name) |
3326
|
121 { |
|
122 bool retval = false; |
|
123 |
|
124 int n = number_of_functions_loaded (); |
|
125 |
|
126 string_vector new_fcn_names (n); |
|
127 |
|
128 int k = 0; |
|
129 |
|
130 for (int i = 0; i < n; i++) |
|
131 { |
|
132 if (fcn_names(i) == fcn_name) |
|
133 retval = true; |
|
134 else |
|
135 new_fcn_names(k++) = fcn_names(i); |
|
136 } |
|
137 |
|
138 new_fcn_names.resize (k); |
|
139 |
|
140 fcn_names = new_fcn_names; |
|
141 |
|
142 return retval; |
|
143 } |
|
144 |
|
145 bool |
|
146 octave_base_shlib::is_out_of_date (void) const |
|
147 { |
|
148 file_stat fs (file); |
|
149 |
|
150 return fs.is_newer (tm_loaded); |
|
151 } |
|
152 |
|
153 void |
5781
|
154 octave_base_shlib::stamp_time (void) |
3326
|
155 { |
|
156 tm_loaded.stamp (); |
|
157 |
5781
|
158 file_stat fs (file); |
3326
|
159 |
5781
|
160 if (fs.is_newer (tm_loaded)) |
|
161 (*current_liboctave_warning_with_id_handler) |
|
162 ("Octave:warn-future-time-stamp", |
|
163 "timestamp on file %s is in the future", file.c_str ()); |
3326
|
164 } |
|
165 |
|
166 void |
3504
|
167 octave_base_shlib::add_to_fcn_names (const std::string& name) |
3326
|
168 { |
|
169 int n = number_of_functions_loaded (); |
|
170 |
|
171 for (int i = 0; i < n; i++) |
|
172 if (fcn_names(i) == name) |
|
173 return; |
|
174 |
|
175 fcn_names.resize (n+1); |
|
176 |
|
177 fcn_names(n) = name; |
|
178 } |
|
179 |
|
180 void |
|
181 octave_base_shlib::do_close_hook (octave_shlib::close_hook cl_hook) |
|
182 { |
|
183 int n = number_of_functions_loaded (); |
|
184 |
|
185 for (int i = 0; i < n; i++) |
|
186 cl_hook (fcn_names(i)); |
|
187 } |
|
188 |
|
189 void |
|
190 octave_base_shlib::tabula_rasa (void) |
|
191 { |
|
192 file = ""; |
|
193 |
|
194 fcn_names.resize (0); |
|
195 |
|
196 tm_loaded = static_cast<time_t> (0); |
|
197 } |
|
198 |
4110
|
199 #if defined (HAVE_DLOPEN_API) |
3326
|
200 |
|
201 class |
|
202 octave_dlopen_shlib : public octave_base_shlib |
|
203 { |
|
204 public: |
|
205 |
|
206 octave_dlopen_shlib (void); |
|
207 |
|
208 ~octave_dlopen_shlib (void); |
|
209 |
5781
|
210 void open (const std::string& f); |
3326
|
211 |
3504
|
212 void *search (const std::string& name, name_mangler mangler = 0); |
3326
|
213 |
|
214 void close (octave_shlib::close_hook cl_hook = 0); |
|
215 |
|
216 bool is_open (void) const { return (library != 0); } |
|
217 |
|
218 private: |
|
219 |
|
220 // No copying! |
|
221 |
|
222 octave_dlopen_shlib (const octave_dlopen_shlib&); |
|
223 |
|
224 octave_dlopen_shlib& operator = (const octave_dlopen_shlib&); |
|
225 |
|
226 void *library; |
|
227 }; |
|
228 |
|
229 octave_dlopen_shlib::octave_dlopen_shlib (void) |
|
230 : octave_base_shlib (), library (0) |
|
231 { |
|
232 } |
|
233 |
|
234 octave_dlopen_shlib::~octave_dlopen_shlib (void) |
|
235 { |
|
236 close (); |
|
237 } |
|
238 |
|
239 void |
5781
|
240 octave_dlopen_shlib::open (const std::string& f) |
3326
|
241 { |
|
242 if (! is_open ()) |
|
243 { |
|
244 file = f; |
|
245 |
4184
|
246 int flags = 0; |
|
247 |
|
248 #if defined (RTLD_LAZY) |
4193
|
249 flags |= RTLD_LAZY; |
4184
|
250 #endif |
|
251 |
|
252 #if defined (RTLD_GLOBAL) |
4193
|
253 flags |= RTLD_GLOBAL; |
4184
|
254 #endif |
|
255 |
|
256 library = dlopen (file.c_str (), flags); |
3326
|
257 |
|
258 if (library) |
5781
|
259 stamp_time (); |
3326
|
260 else |
|
261 { |
|
262 const char *msg = dlerror (); |
|
263 |
|
264 if (msg) |
|
265 (*current_liboctave_error_handler) ("%s", msg); |
|
266 } |
|
267 } |
|
268 else |
|
269 (*current_liboctave_error_handler) |
|
270 ("shared library %s is already open", file.c_str ()); |
|
271 } |
|
272 |
|
273 void * |
3504
|
274 octave_dlopen_shlib::search (const std::string& name, |
3326
|
275 octave_shlib::name_mangler mangler) |
|
276 { |
|
277 void *function = 0; |
|
278 |
|
279 if (is_open ()) |
|
280 { |
3504
|
281 std::string sym_name = name; |
3326
|
282 |
|
283 if (mangler) |
|
284 sym_name = mangler (name); |
|
285 |
|
286 function = dlsym (library, sym_name.c_str ()); |
|
287 |
|
288 if (function) |
|
289 add_to_fcn_names (name); |
|
290 } |
|
291 else |
|
292 (*current_liboctave_error_handler) |
|
293 ("shared library %s is not open", file.c_str ()); |
|
294 |
|
295 return function; |
|
296 } |
|
297 |
|
298 void |
|
299 octave_dlopen_shlib::close (octave_shlib::close_hook cl_hook) |
|
300 { |
|
301 if (is_open ()) |
|
302 { |
5864
|
303 if (cl_hook) |
|
304 do_close_hook (cl_hook); |
3326
|
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 |
5781
|
325 void open (const std::string& f); |
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 |
5781
|
355 octave_shl_load_shlib::open (const std::string& f) |
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) |
5781
|
364 stamp_time (); |
3326
|
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 { |
5864
|
407 if (cl_hook) |
|
408 do_close_hook (cl_hook); |
3326
|
409 |
|
410 shl_unload (library); |
|
411 |
|
412 library = 0; |
|
413 |
|
414 tabula_rasa (); |
|
415 } |
|
416 } |
|
417 |
4110
|
418 #elif defined (HAVE_LOADLIBRARY_API) |
|
419 |
|
420 class |
|
421 octave_w32_shlib: public octave_base_shlib |
|
422 { |
|
423 public: |
|
424 |
|
425 octave_w32_shlib (void); |
|
426 |
|
427 ~octave_w32_shlib (void); |
|
428 |
5781
|
429 void open (const std::string& f); |
4110
|
430 |
|
431 void *search (const std::string& name, name_mangler mangler = 0); |
|
432 |
|
433 void close (octave_shlib::close_hook cl_hook = 0); |
|
434 |
|
435 bool is_open (void) const { return (handle != 0); } |
|
436 |
|
437 private: |
|
438 |
|
439 // No copying! |
|
440 |
|
441 octave_w32_shlib (const octave_w32_shlib&); |
|
442 |
|
443 octave_w32_shlib& operator = (const octave_w32_shlib&); |
|
444 |
|
445 HINSTANCE handle; |
|
446 }; |
|
447 |
|
448 octave_w32_shlib::octave_w32_shlib (void) |
|
449 : octave_base_shlib (), handle (0) |
|
450 { |
|
451 } |
|
452 |
|
453 octave_w32_shlib::~octave_w32_shlib (void) |
|
454 { |
|
455 close (); |
|
456 } |
|
457 |
|
458 void |
5781
|
459 octave_w32_shlib::open (const std::string& f) |
4110
|
460 { |
|
461 if (! is_open ()) |
|
462 { |
|
463 file = f; |
|
464 |
|
465 handle = LoadLibrary (file.c_str ()); |
|
466 |
7520
|
467 if (handle) |
5781
|
468 stamp_time (); |
4110
|
469 else |
|
470 { |
|
471 DWORD lastError = GetLastError (); |
|
472 char *msg; |
|
473 |
|
474 switch (lastError) |
|
475 { |
|
476 case ERROR_MOD_NOT_FOUND: |
|
477 case ERROR_DLL_NOT_FOUND: |
|
478 msg = "could not find library or dependents"; |
|
479 break; |
|
480 |
|
481 case ERROR_INVALID_DLL: |
|
482 msg = "library or its dependents are damaged"; |
|
483 break; |
|
484 |
|
485 case ERROR_DLL_INIT_FAILED: |
|
486 msg = "library initialization routine failed"; |
|
487 break; |
|
488 |
|
489 default: |
|
490 msg = "library open failed"; |
|
491 } |
|
492 |
|
493 (*current_liboctave_error_handler) ("%s: %s", msg, file.c_str ()); |
|
494 } |
|
495 } |
|
496 else |
|
497 (*current_liboctave_error_handler) |
|
498 ("shared library %s is already open", file.c_str ()); |
|
499 } |
|
500 |
5451
|
501 extern "C" |
|
502 { |
|
503 void * octave_w32_search (HINSTANCE handle, const char * name); |
|
504 } |
|
505 |
4110
|
506 void * |
|
507 octave_w32_shlib::search (const std::string& name, |
|
508 octave_shlib::name_mangler mangler) |
|
509 { |
|
510 void *function = 0; |
|
511 |
|
512 if (is_open ()) |
|
513 { |
|
514 std::string sym_name = name; |
|
515 |
|
516 if (mangler) |
|
517 sym_name = mangler (name); |
|
518 |
5451
|
519 function = octave_w32_library_search (handle, sym_name.c_str ()); |
4110
|
520 |
|
521 if (function) |
|
522 add_to_fcn_names (name); |
|
523 } |
|
524 else |
|
525 (*current_liboctave_error_handler) |
|
526 ("shared library %s is not open", file.c_str ()); |
|
527 |
|
528 return function; |
|
529 } |
|
530 |
|
531 void |
|
532 octave_w32_shlib::close (octave_shlib::close_hook cl_hook) |
|
533 { |
|
534 if (is_open ()) |
|
535 { |
5864
|
536 if (cl_hook) |
|
537 do_close_hook (cl_hook); |
4110
|
538 |
|
539 FreeLibrary (handle); |
|
540 |
|
541 handle = 0; |
|
542 |
|
543 tabula_rasa (); |
|
544 } |
|
545 } |
|
546 |
4162
|
547 #elif defined (HAVE_DYLD_API) |
|
548 |
|
549 class |
|
550 octave_dyld_shlib : public octave_base_shlib |
|
551 { |
|
552 public: |
|
553 |
|
554 octave_dyld_shlib (void); |
|
555 |
|
556 ~octave_dyld_shlib (void); |
|
557 |
5781
|
558 void open (const std::string& f); |
4162
|
559 |
|
560 void *search (const std::string& name, name_mangler mangler = 0); |
|
561 |
|
562 void close (octave_shlib::close_hook cl_hook = 0); |
|
563 |
|
564 bool is_open (void) const {return (isOpen); } |
|
565 |
|
566 private: |
|
567 |
|
568 // No copying! |
|
569 |
|
570 octave_dyld_shlib (const octave_dyld_shlib&); |
|
571 |
|
572 octave_dyld_shlib& operator = (const octave_dyld_shlib&); |
|
573 |
|
574 bool isOpen; |
|
575 NSObjectFileImage img; |
|
576 NSModule handle; |
|
577 }; |
|
578 |
|
579 octave_dyld_shlib::octave_dyld_shlib (void) |
|
580 : octave_base_shlib (), isOpen (false), handle (0) |
|
581 { |
|
582 } |
|
583 |
|
584 octave_dyld_shlib::~octave_dyld_shlib (void) |
|
585 { |
|
586 close (); |
|
587 } |
|
588 |
|
589 void |
5781
|
590 octave_dyld_shlib::open (const std::string& f) |
4162
|
591 { |
|
592 int returnCode; |
|
593 |
|
594 if (! is_open ()) |
|
595 { |
|
596 file = f; |
|
597 |
|
598 returnCode = NSCreateObjectFileImageFromFile (file.c_str (), &img); |
|
599 |
|
600 if (NSObjectFileImageSuccess == returnCode) |
|
601 { |
|
602 handle = NSLinkModule (img, file.c_str (), |
|
603 (NSLINKMODULE_OPTION_RETURN_ON_ERROR |
|
604 | NSLINKMODULE_OPTION_PRIVATE)); |
|
605 if (handle) |
|
606 { |
5781
|
607 stamp_time (); |
4162
|
608 isOpen = true; |
|
609 } |
|
610 else |
|
611 { |
|
612 (*current_liboctave_error_handler) |
|
613 ("couldn't link module %s", file.c_str ()); |
|
614 } |
|
615 } |
|
616 else |
|
617 { |
|
618 (*current_liboctave_error_handler) |
|
619 ("got NSObjectFileImageReturnCode %d", returnCode); |
|
620 |
5775
|
621 // FIXME -- should use NSLinkEditError () to get |
4162
|
622 // more info on what went wrong. |
|
623 } |
|
624 } |
|
625 else |
|
626 { |
|
627 (*current_liboctave_error_handler) |
|
628 ("bundle %s is already open", file.c_str ()); |
|
629 } |
|
630 } |
|
631 |
|
632 void * |
|
633 octave_dyld_shlib::search (const std::string& name, |
|
634 octave_shlib::name_mangler mangler) |
|
635 { |
|
636 void *function = 0; |
|
637 |
|
638 if (is_open ()) |
|
639 { |
|
640 std::string sym_name = name; |
|
641 |
|
642 if (mangler) |
|
643 sym_name = mangler (name); |
|
644 |
|
645 NSSymbol symbol = NSLookupSymbolInModule (handle, sym_name.c_str ()); |
|
646 |
|
647 if (symbol) |
|
648 { |
|
649 function = NSAddressOfSymbol (symbol); |
|
650 add_to_fcn_names (name); |
|
651 } |
|
652 } |
|
653 else |
|
654 (*current_liboctave_error_handler) |
|
655 ("bundle %s is not open", file.c_str ()); |
|
656 |
|
657 return function; |
|
658 } |
|
659 |
|
660 void |
|
661 octave_dyld_shlib::close (octave_shlib::close_hook cl_hook) |
|
662 { |
|
663 if (is_open ()) |
|
664 { |
|
665 do_close_hook (cl_hook); |
|
666 |
|
667 NSUnLinkModule (handle, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); |
|
668 |
|
669 handle = 0; |
|
670 |
|
671 if (NSDestroyObjectFileImage (img)) |
|
672 isOpen = false; |
|
673 |
|
674 tabula_rasa (); |
|
675 } |
|
676 } |
|
677 |
3326
|
678 #endif |
|
679 |
|
680 octave_shlib * |
|
681 octave_shlib::make_shlib (void) |
|
682 { |
4110
|
683 #if defined (HAVE_DLOPEN_API) |
3326
|
684 return new octave_dlopen_shlib (); |
4110
|
685 #elif defined (HAVE_SHL_LOAD_API) |
3326
|
686 return new octave_shl_load_shlib (); |
4110
|
687 #elif defined (HAVE_LOADLIBRARY_API) |
|
688 return new octave_w32_shlib (); |
4162
|
689 #elif defined (HAVE_DYLD_API) |
|
690 return new octave_dyld_shlib (); |
3326
|
691 #else |
|
692 return new octave_base_shlib (); |
|
693 #endif |
|
694 } |
|
695 |
|
696 /* |
|
697 ;;; Local Variables: *** |
|
698 ;;; mode: C++ *** |
|
699 ;;; End: *** |
|
700 */ |