24 #if defined (HAVE_CONFIG_H)
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 1
67 : count (1),
file (f), tm_loaded (), fcn_names ()
72 (*current_liboctave_warning_with_id_handler)
73 (
"Octave:warn-future-time-stamp",
74 "timestamp on file %s is in the future",
file.c_str ());
81 return (fs && fs.
is_newer (tm_loaded));
91 tm_loaded = fs.
mtime ();
93 (*current_liboctave_warning_with_id_handler)
94 (
"Octave:library-reload",
95 "library %s not reloaded due to existing references",
file.c_str ());
103 std::map<std::string, dynlib_rep *>::iterator
p = instances.find (f);
104 if (p != instances.end ())
112 retval = new_instance (f);
120 auto p = fcn_names.find (name);
122 if (
p == fcn_names.end ())
133 auto p = fcn_names.find (fcn_name);
135 if (
p != fcn_names.end () && --(
p->second) == 0)
137 fcn_names.erase (fcn_name);
147 for (
auto& fcn_sz_p : fcn_names)
148 cl_hook (fcn_sz_p.first);
153 std::map<std::string, dynamic_library::dynlib_rep *>
158 #if defined (HAVE_DLOPEN_API)
167 ~octave_dlopen_shlib (
void);
176 bool is_open (
void)
const {
return (library != 0); }
182 octave_dlopen_shlib (
const octave_dlopen_shlib&);
184 octave_dlopen_shlib&
operator = (
const octave_dlopen_shlib&);
189 octave_dlopen_shlib::octave_dlopen_shlib (
const std::string&
f)
198 # if defined (RTLD_NOW)
204 # if defined (RTLD_GLOBAL)
205 flags |= RTLD_GLOBAL;
208 library = dlopen (
file.c_str (), flags);
212 const char *msg = dlerror ();
215 (*current_liboctave_error_handler) (
"%s: failed to load: %s",
223 octave_dlopen_shlib::~octave_dlopen_shlib (
void)
236 (*current_liboctave_error_handler)
237 (
"shared library %s is not open",
file.c_str ());
242 sym_name = mangler (name);
244 function = dlsym (library, sym_name.c_str ());
249 #elif defined (HAVE_SHL_LOAD_API)
252 octave_shl_load_shlib :
public dynamic_library::dynlib_rep
258 ~octave_shl_load_shlib (
void);
263 bool is_open (
void)
const {
return (library != 0); }
269 octave_shl_load_shlib (
const octave_shl_load_shlib&);
271 octave_shl_load_shlib&
operator = (
const octave_shl_load_shlib&);
276 octave_shl_load_shlib::octave_shl_load_shlib (
const std::string& f)
281 library = shl_load (
file.c_str (), BIND_IMMEDIATE, 0L);
286 (*current_liboctave_error_handler) (
"%s", std::strerror (errno));
290 octave_shl_load_shlib::~octave_shl_load_shlib (
void)
293 shl_unload (library);
303 (*current_liboctave_error_handler)
304 (
"shared library %s is not open",
file.c_str ());
309 sym_name = mangler (name);
311 int status = shl_findsym (&library, sym_name.c_str (),
312 TYPE_UNDEFINED, &
function);
317 #elif defined (HAVE_LOADLIBRARY_API)
320 octave_w32_shlib:
public dynamic_library::dynlib_rep
326 ~octave_w32_shlib (
void);
331 bool is_open (
void)
const {
return (handle != 0); }
337 octave_w32_shlib (
const octave_w32_shlib&);
339 octave_w32_shlib&
operator = (
const octave_w32_shlib&);
347 SetDllDirectory (dir.empty () ? 0 : dir.c_str ());
350 octave_w32_shlib::octave_w32_shlib (
const std::string& f)
355 set_dll_directory (dir);
357 handle = LoadLibrary (
file.c_str ());
359 set_dll_directory ();
363 DWORD lastError = GetLastError ();
368 case ERROR_MOD_NOT_FOUND:
369 case ERROR_DLL_NOT_FOUND:
370 msg =
"could not find library or dependencies";
373 case ERROR_INVALID_DLL:
374 msg =
"library or its dependencies are damaged";
377 case ERROR_DLL_INIT_FAILED:
378 msg =
"library initialization routine failed";
382 msg =
"library open failed";
385 (*current_liboctave_error_handler) (
"%s: %s", msg,
file.c_str ());
389 octave_w32_shlib::~octave_w32_shlib (
void)
392 FreeLibrary (handle);
402 (*current_liboctave_error_handler)
403 (
"shared library %s is not open",
file.c_str ());
408 sym_name = mangler (name);
410 function =
reinterpret_cast<void *
> (GetProcAddress (handle,
416 #elif defined (HAVE_DYLD_API)
419 octave_dyld_shlib :
public dynamic_library::dynlib_rep
423 octave_dyld_shlib (
void);
425 ~octave_dyld_shlib (
void);
434 bool is_open (
void)
const {
return (handle != 0); }
440 octave_dyld_shlib (
const octave_dyld_shlib&);
442 octave_dyld_shlib&
operator = (
const octave_dyld_shlib&);
444 NSObjectFileImage img;
448 octave_dyld_shlib::octave_dyld_shlib (
const std::string& f)
451 int returnCode = NSCreateObjectFileImageFromFile (
file.c_str (), &img);
453 if (NSObjectFileImageSuccess != returnCode)
455 (*current_liboctave_error_handler)
456 (
"got NSObjectFileImageReturnCode %d", returnCode);
462 handle = NSLinkModule (img,
file.c_str (),
463 (NSLINKMODULE_OPTION_RETURN_ON_ERROR
464 | NSLINKMODULE_OPTION_PRIVATE));
467 NSLinkEditErrors ler;
470 const char *errstr = 0;
472 NSLinkEditError (&ler, &lerno, &file2, &errstr);
475 errstr =
"unspecified error";
477 (*current_liboctave_error_handler) (
"%s: %s",
file.c_str (), errstr);
481 octave_dyld_shlib::~octave_dyld_shlib (
void)
484 NSUnLinkModule (handle, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES);
486 NSDestroyObjectFileImage (img);
496 (*current_liboctave_error_handler)
497 (
"bundle %s is not open",
file.c_str ());
502 sym_name = mangler (name);
504 NSSymbol symbol = NSLookupSymbolInModule (handle, sym_name.c_str ());
508 function = NSAddressOfSymbol (symbol);
516 dynamic_library::dynlib_rep *
519 #if defined (HAVE_DLOPEN_API)
520 return new octave_dlopen_shlib (f);
521 #elif defined (HAVE_SHL_LOAD_API)
522 return new octave_shl_load_shlib (f);
523 #elif defined (HAVE_LOADLIBRARY_API)
524 return new octave_w32_shlib (f);
525 #elif defined (HAVE_DYLD_API)
526 return new octave_dyld_shlib (f);
528 (*current_liboctave_error_handler)
529 (
"support for dynamically loaded libraries was unavailable or disabled when liboctave was built");
virtual bool is_open(void) const
virtual void * search(const std::string &, name_mangler=0)
Octave interface to the compression and uncompression libraries.
For example cd octave end example noindent changes the current working directory to file
static std::string dirname(const std::string &path)
octave::sys::time mtime(void) const
void do_close_hook(close_hook cl_hook)
void * search(const std::string &nm, name_mangler mangler=0) const
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE * f
OCTAVE_NORETURN liboctave_error_handler current_liboctave_error_handler
static std::map< std::string, dynlib_rep * > instances
bool is_newer(const octave::sys::time &time) const
void add_fcn_name(const std::string &)
bool is_out_of_date(void) const
void(* close_hook)(const std::string &)
octave_refcount< int > count
static std::list< std::string > search(const std::string &path, const std::string &original_name, bool all)
static dynlib_rep nil_rep
void open(const std::string &f)
octave::sys::file_stat fs(filename)
void close(close_hook cl_hook=0)
bool remove_fcn_name(const std::string &)
static dynlib_rep * get_instance(const std::string &f, bool fake)
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
std::string(* name_mangler)(const std::string &)
dynamic_library & operator=(const dynamic_library &sl)
static dynlib_rep * new_instance(const std::string &f)