30 #if defined (HAVE_SHL_LOAD_API)
35 #if defined (HAVE_DYLD_API)
36 #include <mach-o/dyld.h>
41 #if defined (HAVE_DLOPEN_API)
42 #if defined (HAVE_DLFCN_H)
45 extern void *dlopen (
const char *,
int);
46 extern const char *dlerror (
void);
47 extern void *dlsym (
void *,
const char *);
48 extern int dlclose (
void *);
50 #elif defined (HAVE_SHL_LOAD_API)
52 #elif defined (HAVE_LOADLIBRARY_API)
53 #define WIN32_LEAN_AND_MEAN
64 : count (1), file (f), tm_loaded (), fcn_names ()
69 (*current_liboctave_warning_with_id_handler)
70 (
"Octave:warn-future-time-stamp",
71 "timestamp on file %s is in the future",
file.c_str ());
78 return (fs && fs.
is_newer (tm_loaded));
88 tm_loaded = fs.
mtime ();
90 (*current_liboctave_warning_with_id_handler)
91 (
"Octave:library-reload",
92 "library %s not reloaded due to existing references", file.c_str ());
100 std::map<std::string, shlib_rep *>::iterator p = instances.find (f);
101 if (p != instances.end ())
109 retval = new_instance (f);
119 if (p == fcn_names.end ())
132 if (p != fcn_names.end () && --(p->second) == 0)
134 fcn_names.erase (fcn_name);
154 #if defined (HAVE_DLOPEN_API)
161 octave_dlopen_shlib (
const std::string&
f);
163 ~octave_dlopen_shlib (
void);
165 void *
search (
const std::string& name,
172 bool is_open (
void)
const {
return (library != 0); }
178 octave_dlopen_shlib (
const octave_dlopen_shlib&);
180 octave_dlopen_shlib&
operator = (
const octave_dlopen_shlib&);
185 octave_dlopen_shlib::octave_dlopen_shlib (
const std::string&
f)
194 #if defined (RTLD_NOW)
198 library = dlopen (
file.c_str (), flags);
202 const char *msg = dlerror ();
205 (*current_liboctave_error_handler) (
"%s: failed to load: %s",
213 octave_dlopen_shlib::~octave_dlopen_shlib (
void)
227 std::string sym_name = name;
230 sym_name = mangler (name);
232 function = dlsym (library, sym_name.c_str ());
236 (
"shared library %s is not open", file.c_str ());
241 #elif defined (HAVE_SHL_LOAD_API)
248 octave_shl_load_shlib (
const std::string& f);
250 ~octave_shl_load_shlib (
void);
252 void *
search (
const std::string& name,
255 bool is_open (
void)
const {
return (library != 0); }
261 octave_shl_load_shlib (
const octave_shl_load_shlib&);
263 octave_shl_load_shlib&
operator = (
const octave_shl_load_shlib&);
268 octave_shl_load_shlib::octave_shl_load_shlib (
const std::string& f)
273 library = shl_load (
file.c_str (), BIND_IMMEDIATE, 0L);
278 (*current_liboctave_error_handler) (
"%s", gnulib::strerror (errno));
282 octave_shl_load_shlib::~octave_shl_load_shlib (
void)
285 shl_unload (library);
296 std::string sym_name = name;
299 sym_name = mangler (name);
301 int status = shl_findsym (&library, sym_name.c_str (),
302 TYPE_UNDEFINED, &
function);
306 (
"shared library %s is not open",
file.c_str ());
311 #elif defined (HAVE_LOADLIBRARY_API)
318 octave_w32_shlib (
const std::string& f);
320 ~octave_w32_shlib (
void);
322 void *
search (
const std::string& name,
325 bool is_open (
void)
const {
return (handle != 0); }
331 octave_w32_shlib (
const octave_w32_shlib&);
333 octave_w32_shlib&
operator = (
const octave_w32_shlib&);
338 octave_w32_shlib::octave_w32_shlib (
const std::string& f)
341 handle = LoadLibrary (
file.c_str ());
345 DWORD lastError = GetLastError ();
350 case ERROR_MOD_NOT_FOUND:
351 case ERROR_DLL_NOT_FOUND:
352 msg =
"could not find library or dependents";
355 case ERROR_INVALID_DLL:
356 msg =
"library or its dependents are damaged";
359 case ERROR_DLL_INIT_FAILED:
360 msg =
"library initialization routine failed";
364 msg =
"library open failed";
367 (*current_liboctave_error_handler) (
"%s: %s", msg,
file.c_str ());
371 octave_w32_shlib::~octave_w32_shlib (
void)
374 FreeLibrary (handle);
379 void * octave_w32_search (HINSTANCE handle,
const char * name);
390 std::string sym_name = name;
393 sym_name = mangler (name);
395 function = octave_w32_library_search (handle, sym_name.c_str ());
399 (
"shared library %s is not open", file.c_str ());
404 #elif defined (HAVE_DYLD_API)
411 octave_dyld_shlib (
void);
413 ~octave_dyld_shlib (
void);
415 void open (
const std::string& f);
417 void *
search (
const std::string& name,
422 bool is_open (
void)
const {
return (handle != 0); }
428 octave_dyld_shlib (
const octave_dyld_shlib&);
430 octave_dyld_shlib&
operator = (
const octave_dyld_shlib&);
432 NSObjectFileImage img;
436 octave_dyld_shlib::octave_dyld_shlib (
const std::string& f)
439 int returnCode = NSCreateObjectFileImageFromFile (
file.c_str (), &img);
441 if (NSObjectFileImageSuccess == returnCode)
443 handle = NSLinkModule (img,
file.c_str (),
444 (NSLINKMODULE_OPTION_RETURN_ON_ERROR
445 | NSLINKMODULE_OPTION_PRIVATE));
448 NSLinkEditErrors ler;
451 const char *errstr = 0;
453 NSLinkEditError (&ler, &lerno, &file2, &errstr);
456 errstr =
"unspecified error";
458 (*current_liboctave_error_handler)
459 (
"%s: %s",
file.c_str (), errstr);
464 (*current_liboctave_error_handler)
465 (
"got NSObjectFileImageReturnCode %d", returnCode);
472 octave_dyld_shlib::~octave_dyld_shlib (
void)
475 NSUnLinkModule (handle, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES);
477 NSDestroyObjectFileImage (img);
488 std::string sym_name = name;
491 sym_name = mangler (name);
493 NSSymbol symbol = NSLookupSymbolInModule (handle, sym_name.c_str ());
497 function = NSAddressOfSymbol (symbol);
502 (
"bundle %s is not open", file.c_str ());
512 #if defined (HAVE_DLOPEN_API)
513 return new octave_dlopen_shlib (f);
514 #elif defined (HAVE_SHL_LOAD_API)
515 return new octave_shl_load_shlib (f);
516 #elif defined (HAVE_LOADLIBRARY_API)
517 return new octave_w32_shlib (f);
518 #elif defined (HAVE_DYLD_API)
519 return new octave_dyld_shlib (f);
521 (*current_liboctave_error_handler)
522 (
"no API for dynamic loading is available");
void * search(const std::string &nm, name_mangler mangler=0) const
bool remove_fcn_name(const std::string &)
static shlib_rep * new_instance(const std::string &f)
octave_refcount< int > count
void(* close_hook)(const std::string &)
static shlib_rep * get_instance(const std::string &f, bool fake)
bool is_newer(const octave_time &time) const
void do_close_hook(close_hook cl_hook)
static string_vector search(const std::string &path, const std::string &original_name, bool must_exist, bool all)
std::map< std::string, size_t >::iterator fcn_names_iterator
liboctave_error_handler current_liboctave_error_handler
F77_RET_T const double const double * f
octave_shlib & operator=(const octave_shlib &sl)
static std::map< std::string, shlib_rep * > instances
void add_fcn_name(const std::string &)
std::string(* name_mangler)(const std::string &)
octave_time mtime(void) const
void open(const std::string &f)
virtual void * search(const std::string &, name_mangler=0)
virtual bool is_open(void) const
void close(close_hook cl_hook=0)
bool is_out_of_date(void) const