1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1664
|
27 #if defined (WITH_SHL) |
|
28 #include <cerrno> |
|
29 #include <cstring> |
|
30 #endif |
|
31 |
707
|
32 #include <strstream.h> |
|
33 |
1
|
34 extern "C" |
|
35 { |
1664
|
36 #if defined (WITH_DL) |
|
37 #include <dlfcn.h> |
1755
|
38 #ifndef RTLD_LAZY |
|
39 #define RTLD_LAZY 1 |
|
40 #endif |
1664
|
41 #elif defined (WITH_SHL) |
|
42 #include <dl.h> |
1102
|
43 #endif |
1
|
44 } |
|
45 |
2492
|
46 #include <defaults.h> |
1352
|
47 #include "dirfns.h" |
|
48 #include "dynamic-ld.h" |
|
49 #include "error.h" |
1670
|
50 #include "toplev.h" |
1155
|
51 #include "pathsearch.h" |
2862
|
52 #include "oct-obj.h" |
2893
|
53 #include "oct-builtin.h" |
2370
|
54 #include "ov.h" |
1352
|
55 #include "utils.h" |
707
|
56 #include "variables.h" |
1
|
57 |
2893
|
58 #if defined (WITH_DYNAMIC_LINKING) |
707
|
59 |
2893
|
60 typedef void * (*resolver_fcn) (const string& name, const string& file); |
1664
|
61 |
1755
|
62 static string |
|
63 mangle_octave_oct_file_name (const string& name) |
1664
|
64 { |
1755
|
65 string retval ("FS"); |
|
66 retval.append (name); |
|
67 retval.append ("__Fv"); |
1664
|
68 return retval; |
|
69 } |
|
70 |
|
71 #if defined (WITH_DL) |
|
72 |
|
73 static void * |
1755
|
74 dl_resolve_octave_reference (const string& name, const string& file) |
1664
|
75 { |
1665
|
76 void *retval = 0; |
|
77 |
1664
|
78 // Dynamic linking with dlopen/dlsym doesn't require specification |
|
79 // of the libraries at runtime. Instead, they are specified when |
|
80 // the .oct file is created. |
|
81 |
1755
|
82 void *handle = dlopen (file.c_str (), RTLD_LAZY); |
1664
|
83 |
|
84 if (handle) |
|
85 { |
1755
|
86 retval = dlsym (handle, name.c_str ()); |
1664
|
87 |
1665
|
88 if (! retval) |
1664
|
89 { |
|
90 const char *errmsg = dlerror (); |
|
91 |
|
92 if (errmsg) |
1755
|
93 error("%s: `%s'", name.c_str (), errmsg); |
1664
|
94 else |
1755
|
95 error("unable to link function `%s'", name.c_str ()); |
1664
|
96 |
|
97 dlclose (handle); |
|
98 } |
|
99 } |
|
100 else |
1755
|
101 error ("%s: %s `%s'", dlerror (), file.c_str (), name.c_str ()); |
1665
|
102 |
|
103 return retval; |
1664
|
104 } |
|
105 |
2893
|
106 static resolver_fcn resolve_octave_reference = dl_resolve_octave_reference; |
|
107 |
1664
|
108 #elif defined (WITH_SHL) |
|
109 |
|
110 static void * |
1755
|
111 shl_resolve_octave_reference (const string& name, const string& file) |
1664
|
112 { |
1665
|
113 void *retval = 0; |
|
114 |
1664
|
115 // Dynamic linking with shl_load/shl_findsym doesn't require |
|
116 // specification of the libraries at runtime. Instead, they are |
|
117 // specified when the .oct file is created. |
|
118 |
2800
|
119 shl_t handle = shl_load (file.c_str (), BIND_DEFERRED, 0L); |
1664
|
120 |
|
121 if (handle) |
|
122 { |
2800
|
123 int status = shl_findsym (&handle, name.c_str (), |
1665
|
124 TYPE_UNDEFINED, retval); |
1664
|
125 |
|
126 if (status < 0) |
|
127 { |
|
128 const char *errmsg = strerror (errno); |
|
129 |
|
130 if (errmsg) |
1755
|
131 error("%s: `%s'", name.c_str (), errmsg); |
1664
|
132 else |
1755
|
133 error("unable to link function `%s'", name.c_str ()); |
1664
|
134 |
1665
|
135 retval = 0; |
1664
|
136 } |
|
137 } |
|
138 else |
1755
|
139 error ("%s: %s `%s'", strerror (errno), file.c_str (), name.c_str ()); |
1665
|
140 |
|
141 return retval; |
1664
|
142 } |
|
143 |
2893
|
144 static resolver_fcn resolve_octave_reference = shl_resolve_octave_reference; |
|
145 |
1742
|
146 #endif |
1664
|
147 |
2893
|
148 typedef octave_builtin * (*builtin_obj_fcn) (void); |
1664
|
149 |
|
150 int |
1755
|
151 load_octave_oct_file (const string& name) |
707
|
152 { |
1664
|
153 int retval = 0; |
|
154 |
1755
|
155 string oct_file = oct_file_in_path (name); |
707
|
156 |
1907
|
157 if (! oct_file.empty ()) |
707
|
158 { |
1755
|
159 string mangled_name = mangle_octave_oct_file_name (name); |
707
|
160 |
2893
|
161 builtin_obj_fcn f |
|
162 = (builtin_obj_fcn) resolve_octave_reference (mangled_name, oct_file); |
707
|
163 |
|
164 if (f) |
|
165 { |
2893
|
166 octave_builtin *obj = f (); |
707
|
167 |
2893
|
168 if (obj) |
707
|
169 { |
2893
|
170 install_builtin_function (obj); |
1664
|
171 retval = 1; |
707
|
172 } |
|
173 } |
|
174 } |
|
175 |
2893
|
176 return retval; |
|
177 } |
|
178 |
1664
|
179 #else |
|
180 |
2893
|
181 int |
|
182 load_octave_oct_file (const string&) |
|
183 { |
|
184 return 0; |
|
185 } |
707
|
186 |
|
187 #endif |
|
188 |
1
|
189 /* |
|
190 ;;; Local Variables: *** |
|
191 ;;; mode: C++ *** |
|
192 ;;; End: *** |
|
193 */ |