GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-shlib.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1999-2015 John W. Eaton
4 Copyright (C) 2009 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <map>
29 
30 #if defined (HAVE_SHL_LOAD_API)
31 #include <cerrno>
32 #include <cstring>
33 #endif
34 
35 #if defined (HAVE_DYLD_API)
36 #include <mach-o/dyld.h>
37 #endif
38 
39 extern "C"
40 {
41 #if defined (HAVE_DLOPEN_API)
42 #if defined (HAVE_DLFCN_H)
43 #include <dlfcn.h>
44 #else
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 *);
49 #endif
50 #elif defined (HAVE_SHL_LOAD_API)
51 #include <dl.h>
52 #elif defined (HAVE_LOADLIBRARY_API)
53 #define WIN32_LEAN_AND_MEAN
54 #include <windows.h>
55 #endif
56 }
57 
58 #include "file-stat.h"
59 #include "lo-error.h"
60 #include "oct-shlib.h"
61 #include "str-vec.h"
62 
64  : count (1), file (f), tm_loaded (), fcn_names ()
65 {
66  instances[f] = this;
67 
68  if (is_out_of_date ())
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 ());
72 }
73 
74 bool
76 {
77  file_stat fs (file);
78  return (fs && fs.is_newer (tm_loaded));
79 }
80 
81 void
83 {
84  // We can't actually reload the library, but we'll pretend we did.
85  file_stat fs (file);
86  if (fs && fs.is_newer (tm_loaded))
87  {
88  tm_loaded = fs.mtime ();
89 
90  (*current_liboctave_warning_with_id_handler)
91  ("Octave:library-reload",
92  "library %s not reloaded due to existing references", file.c_str ());
93  }
94 }
95 
97 octave_shlib::shlib_rep::get_instance (const std::string& f, bool fake)
98 {
99  shlib_rep *retval = 0;
100  std::map<std::string, shlib_rep *>::iterator p = instances.find (f);
101  if (p != instances.end ())
102  {
103  retval = p->second;
104  retval->count++;
105  if (fake)
106  retval->fake_reload ();
107  }
108  else
109  retval = new_instance (f);
110 
111  return retval;
112 }
113 
114 void
115 octave_shlib::shlib_rep::add_fcn_name (const std::string& name)
116 {
117  fcn_names_iterator p = fcn_names.find (name);
118 
119  if (p == fcn_names.end ())
120  fcn_names[name] = 1;
121  else
122  ++(p->second);
123 }
124 
125 bool
126 octave_shlib::shlib_rep::remove_fcn_name (const std::string& fcn_name)
127 {
128  bool retval = false;
129 
130  fcn_names_iterator p = fcn_names.find (fcn_name);
131 
132  if (p != fcn_names.end () && --(p->second) == 0)
133  {
134  fcn_names.erase (fcn_name);
135  retval = true;
136  }
137 
138  return retval;
139 }
140 
141 void
143 {
144  for (fcn_names_iterator p = fcn_names.begin (); p != fcn_names.end (); p++)
145  cl_hook (p->first);
146 
147  fcn_names.clear ();
148 }
149 
150 std::map<std::string, octave_shlib::shlib_rep *> octave_shlib::shlib_rep::instances;
151 
153 
154 #if defined (HAVE_DLOPEN_API)
155 
156 class
157 octave_dlopen_shlib : public octave_shlib::shlib_rep
158 {
159 public:
160 
161  octave_dlopen_shlib (const std::string& f);
162 
163  ~octave_dlopen_shlib (void);
164 
165  void *search (const std::string& name,
166  octave_shlib::name_mangler mangler = 0);
167 
168  // FIXME: this is possibly redundant because failure to open a library will
169  // normally throw an exception, avoiding the construction of an invalid
170  // library. Leave it here for possible future use.
171 
172  bool is_open (void) const { return (library != 0); }
173 
174 private:
175 
176  // No copying!
177 
178  octave_dlopen_shlib (const octave_dlopen_shlib&);
179 
180  octave_dlopen_shlib& operator = (const octave_dlopen_shlib&);
181 
182  void *library;
183 };
184 
185 octave_dlopen_shlib::octave_dlopen_shlib (const std::string& f)
186  : octave_shlib::shlib_rep (f), library (0)
187 {
188  int flags = 0;
189 
190  // Use RTLD_NOW to resolve all symbols before dlopen returns.
191  // By using this option, dlopen will detect errors and Octave
192  // won't exit if there are unresolved symbols in the file we are
193  // loading, and we may even get a useful diagnostic.
194 #if defined (RTLD_NOW)
195  flags |= RTLD_NOW;
196 #endif
197 
198  library = dlopen (file.c_str (), flags);
199 
200  if (! library)
201  {
202  const char *msg = dlerror ();
203 
204  if (msg)
205  (*current_liboctave_error_handler) ("%s: failed to load: %s",
206  file.c_str (), msg);
207  else
208  (*current_liboctave_error_handler) ("%s: failed to load",
209  file.c_str ());
210  }
211 }
212 
213 octave_dlopen_shlib::~octave_dlopen_shlib (void)
214 {
215  if (library)
216  dlclose (library);
217 }
218 
219 void *
220 octave_dlopen_shlib::search (const std::string& name,
222 {
223  void *function = 0;
224 
225  if (is_open ())
226  {
227  std::string sym_name = name;
228 
229  if (mangler)
230  sym_name = mangler (name);
231 
232  function = dlsym (library, sym_name.c_str ());
233  }
234  else
236  ("shared library %s is not open", file.c_str ());
237 
238  return function;
239 }
240 
241 #elif defined (HAVE_SHL_LOAD_API)
242 
243 class
244 octave_shl_load_shlib : public octave_shlib::shlib_rep
245 {
246 public:
247 
248  octave_shl_load_shlib (const std::string& f);
249 
250  ~octave_shl_load_shlib (void);
251 
252  void *search (const std::string& name,
253  octave_shlib::name_mangler mangler = 0);
254 
255  bool is_open (void) const { return (library != 0); }
256 
257 private:
258 
259  // No copying!
260 
261  octave_shl_load_shlib (const octave_shl_load_shlib&);
262 
263  octave_shl_load_shlib& operator = (const octave_shl_load_shlib&);
264 
265  shl_t library;
266 };
267 
268 octave_shl_load_shlib::octave_shl_load_shlib (const std::string& f)
269  : octave_shlib::shlib_rep (f), library (0)
270 {
271  file = f;
272 
273  library = shl_load (file.c_str (), BIND_IMMEDIATE, 0L);
274 
275  if (! library)
276  {
277  using namespace std;
278  (*current_liboctave_error_handler) ("%s", gnulib::strerror (errno));
279  }
280 }
281 
282 octave_shl_load_shlib::~octave_shl_load_shlib (void)
283 {
284  if (library)
285  shl_unload (library);
286 }
287 
288 void *
289 octave_shl_load_shlib::search (const std::string& name,
291 {
292  void *function = 0;
293 
294  if (is_open ())
295  {
296  std::string sym_name = name;
297 
298  if (mangler)
299  sym_name = mangler (name);
300 
301  int status = shl_findsym (&library, sym_name.c_str (),
302  TYPE_UNDEFINED, &function);
303  }
304  else
306  ("shared library %s is not open", file.c_str ());
307 
308  return function;
309 }
310 
311 #elif defined (HAVE_LOADLIBRARY_API)
312 
313 class
314 octave_w32_shlib: public octave_shlib::shlib_rep
315 {
316 public:
317 
318  octave_w32_shlib (const std::string& f);
319 
320  ~octave_w32_shlib (void);
321 
322  void *search (const std::string& name,
323  octave_shlib::name_mangler mangler = 0);
324 
325  bool is_open (void) const { return (handle != 0); }
326 
327 private:
328 
329  // No copying!
330 
331  octave_w32_shlib (const octave_w32_shlib&);
332 
333  octave_w32_shlib& operator = (const octave_w32_shlib&);
334 
335  HINSTANCE handle;
336 };
337 
338 octave_w32_shlib::octave_w32_shlib (const std::string& f)
339  : octave_shlib::shlib_rep (f), handle (0)
340 {
341  handle = LoadLibrary (file.c_str ());
342 
343  if (! handle)
344  {
345  DWORD lastError = GetLastError ();
346  char *msg;
347 
348  switch (lastError)
349  {
350  case ERROR_MOD_NOT_FOUND:
351  case ERROR_DLL_NOT_FOUND:
352  msg = "could not find library or dependents";
353  break;
354 
355  case ERROR_INVALID_DLL:
356  msg = "library or its dependents are damaged";
357  break;
358 
359  case ERROR_DLL_INIT_FAILED:
360  msg = "library initialization routine failed";
361  break;
362 
363  default:
364  msg = "library open failed";
365  }
366 
367  (*current_liboctave_error_handler) ("%s: %s", msg, file.c_str ());
368  }
369 }
370 
371 octave_w32_shlib::~octave_w32_shlib (void)
372 {
373  if (handle)
374  FreeLibrary (handle);
375 }
376 
377 extern "C"
378 {
379  void * octave_w32_search (HINSTANCE handle, const char * name);
380 }
381 
382 void *
383 octave_w32_shlib::search (const std::string& name,
385 {
386  void *function = 0;
387 
388  if (is_open ())
389  {
390  std::string sym_name = name;
391 
392  if (mangler)
393  sym_name = mangler (name);
394 
395  function = octave_w32_library_search (handle, sym_name.c_str ());
396  }
397  else
399  ("shared library %s is not open", file.c_str ());
400 
401  return function;
402 }
403 
404 #elif defined (HAVE_DYLD_API)
405 
406 class
407 octave_dyld_shlib : public octave_shlib::shlib_rep
408 {
409 public:
410 
411  octave_dyld_shlib (void);
412 
413  ~octave_dyld_shlib (void);
414 
415  void open (const std::string& f);
416 
417  void *search (const std::string& name,
418  octave_shlib::name_mangler mangler = 0);
419 
420  void close (octave_shlib::close_hook cl_hook = 0);
421 
422  bool is_open (void) const {return (handle != 0); }
423 
424 private:
425 
426  // No copying!
427 
428  octave_dyld_shlib (const octave_dyld_shlib&);
429 
430  octave_dyld_shlib& operator = (const octave_dyld_shlib&);
431 
432  NSObjectFileImage img;
433  NSModule handle;
434 };
435 
436 octave_dyld_shlib::octave_dyld_shlib (const std::string& f)
437  : octave_shlib::shlib_rep (f), handle (0)
438 {
439  int returnCode = NSCreateObjectFileImageFromFile (file.c_str (), &img);
440 
441  if (NSObjectFileImageSuccess == returnCode)
442  {
443  handle = NSLinkModule (img, file.c_str (),
444  (NSLINKMODULE_OPTION_RETURN_ON_ERROR
445  | NSLINKMODULE_OPTION_PRIVATE));
446  if (! handle)
447  {
448  NSLinkEditErrors ler;
449  int lerno;
450  const char *file2;
451  const char *errstr = 0;
452 
453  NSLinkEditError (&ler, &lerno, &file2, &errstr);
454 
455  if (! errstr)
456  errstr = "unspecified error";
457 
458  (*current_liboctave_error_handler)
459  ("%s: %s", file.c_str (), errstr);
460  }
461  }
462  else
463  {
464  (*current_liboctave_error_handler)
465  ("got NSObjectFileImageReturnCode %d", returnCode);
466 
467  // FIXME: should use NSLinkEditError () to get
468  // more info on what went wrong.
469  }
470 }
471 
472 octave_dyld_shlib::~octave_dyld_shlib (void)
473 {
474  if (handle)
475  NSUnLinkModule (handle, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES);
476 
477  NSDestroyObjectFileImage (img);
478 }
479 
480 void *
481 octave_dyld_shlib::search (const std::string& name,
483 {
484  void *function = 0;
485 
486  if (is_open ())
487  {
488  std::string sym_name = name;
489 
490  if (mangler)
491  sym_name = mangler (name);
492 
493  NSSymbol symbol = NSLookupSymbolInModule (handle, sym_name.c_str ());
494 
495  if (symbol)
496  {
497  function = NSAddressOfSymbol (symbol);
498  }
499  }
500  else
502  ("bundle %s is not open", file.c_str ());
503 
504  return function;
505 }
506 
507 #endif
508 
511 {
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);
520 #else
521  (*current_liboctave_error_handler)
522  ("no API for dynamic loading is available");
523  return new shlib_rep ();
524 #endif
525 }
void * search(const std::string &nm, name_mangler mangler=0) const
Definition: oct-shlib.h:160
bool remove_fcn_name(const std::string &)
Definition: oct-shlib.cc:126
static shlib_rep * new_instance(const std::string &f)
Definition: oct-shlib.cc:510
octave_refcount< int > count
Definition: oct-shlib.h:89
void(* close_hook)(const std::string &)
Definition: oct-shlib.h:40
static shlib_rep * get_instance(const std::string &f, bool fake)
Definition: oct-shlib.cc:97
STL namespace.
bool is_newer(const octave_time &time) const
Definition: file-stat.h:139
void do_close_hook(close_hook cl_hook)
Definition: oct-shlib.cc:142
static string_vector search(const std::string &path, const std::string &original_name, bool must_exist, bool all)
Definition: kpse.cc:872
static shlib_rep nil_rep
Definition: oct-shlib.h:109
std::map< std::string, size_t >::iterator fcn_names_iterator
Definition: oct-shlib.h:99
liboctave_error_handler current_liboctave_error_handler
Definition: lo-error.c:38
F77_RET_T const double const double * f
octave_shlib & operator=(const octave_shlib &sl)
Definition: oct-shlib.h:130
static std::map< std::string, shlib_rep * > instances
Definition: oct-shlib.h:104
void add_fcn_name(const std::string &)
Definition: oct-shlib.cc:115
std::string(* name_mangler)(const std::string &)
Definition: oct-shlib.h:39
void fake_reload(void)
Definition: oct-shlib.cc:82
octave_time mtime(void) const
Definition: file-stat.h:118
void open(const std::string &f)
Definition: oct-shlib.h:149
virtual void * search(const std::string &, name_mangler=0)
Definition: oct-shlib.h:63
virtual bool is_open(void) const
Definition: oct-shlib.h:60
void close(close_hook cl_hook=0)
Definition: oct-shlib.h:152
bool is_out_of_date(void) const
Definition: oct-shlib.cc:75