1
|
1 // dynamic-ld.cc -*- C++ -*- |
|
2 /* |
|
3 |
1884
|
4 Copyright (C) 1996 John W. Eaton |
1
|
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 2, or (at your option) any |
|
11 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, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1664
|
28 #if defined (WITH_SHL) |
|
29 #include <cerrno> |
|
30 #include <cstring> |
|
31 #endif |
|
32 |
707
|
33 #include <strstream.h> |
|
34 |
1
|
35 extern "C" |
|
36 { |
1664
|
37 #if defined (WITH_DL) |
|
38 #include <dlfcn.h> |
1755
|
39 #ifndef RTLD_LAZY |
|
40 #define RTLD_LAZY 1 |
|
41 #endif |
1664
|
42 #elif defined (WITH_SHL) |
|
43 #include <dl.h> |
1102
|
44 #endif |
1
|
45 } |
|
46 |
1352
|
47 #include "defaults.h" |
|
48 #include "dirfns.h" |
|
49 #include "dynamic-ld.h" |
|
50 #include "error.h" |
1670
|
51 #include "toplev.h" |
1155
|
52 #include "pathsearch.h" |
1742
|
53 #include "pt-const.h" |
1
|
54 #include "user-prefs.h" |
1352
|
55 #include "utils.h" |
707
|
56 #include "variables.h" |
1
|
57 |
707
|
58 typedef builtin_function * (*Octave_builtin_fcn_struct_fcn)(void); |
|
59 |
1664
|
60 #if defined (WITH_DYNAMIC_LINKING) |
|
61 |
|
62 // XXX FIXME XXX -- need to provide some way to ensure that functions |
|
63 // that we are going to use will use the same naming convention as |
|
64 // Octave's internal functions. It needs to be simpler than the |
|
65 // current DEFUN_DLD() macro, which assumes you know how to name the |
|
66 // function, the struct, and the helper function. |
|
67 |
1755
|
68 static string |
|
69 mangle_octave_builtin_name (const string& name) |
1664
|
70 { |
1755
|
71 string retval ("F"); |
|
72 retval.append (name); |
|
73 retval.append ("__FRC13Octave_objecti"); |
1664
|
74 return retval; |
|
75 } |
|
76 |
1755
|
77 static string |
|
78 mangle_octave_oct_file_name (const string& name) |
1664
|
79 { |
1755
|
80 string retval ("FS"); |
|
81 retval.append (name); |
|
82 retval.append ("__Fv"); |
1664
|
83 return retval; |
|
84 } |
|
85 |
|
86 #if defined (WITH_DL) |
|
87 |
|
88 static void * |
1755
|
89 dl_resolve_octave_reference (const string& name, const string& file) |
1664
|
90 { |
1665
|
91 void *retval = 0; |
|
92 |
1664
|
93 // Dynamic linking with dlopen/dlsym doesn't require specification |
|
94 // of the libraries at runtime. Instead, they are specified when |
|
95 // the .oct file is created. |
|
96 |
1755
|
97 void *handle = dlopen (file.c_str (), RTLD_LAZY); |
1664
|
98 |
|
99 if (handle) |
|
100 { |
1755
|
101 retval = dlsym (handle, name.c_str ()); |
1664
|
102 |
1665
|
103 if (! retval) |
1664
|
104 { |
|
105 const char *errmsg = dlerror (); |
|
106 |
|
107 if (errmsg) |
1755
|
108 error("%s: `%s'", name.c_str (), errmsg); |
1664
|
109 else |
1755
|
110 error("unable to link function `%s'", name.c_str ()); |
1664
|
111 |
|
112 dlclose (handle); |
|
113 } |
|
114 } |
|
115 else |
1755
|
116 error ("%s: %s `%s'", dlerror (), file.c_str (), name.c_str ()); |
1665
|
117 |
|
118 return retval; |
1664
|
119 } |
|
120 |
|
121 #elif defined (WITH_SHL) |
|
122 |
|
123 static void * |
1755
|
124 shl_resolve_octave_reference (const string& name, const string& file) |
1664
|
125 { |
1665
|
126 void *retval = 0; |
|
127 |
1664
|
128 // Dynamic linking with shl_load/shl_findsym doesn't require |
|
129 // specification of the libraries at runtime. Instead, they are |
|
130 // specified when the .oct file is created. |
|
131 |
1755
|
132 void *handle = shl_load (file.c_str (), BIND_DEFERRED, 0L); |
1664
|
133 |
|
134 if (handle) |
|
135 { |
1755
|
136 int status = shl_findsym ((shl_t *) &handle, name.c_str (), |
1665
|
137 TYPE_UNDEFINED, retval); |
1664
|
138 |
|
139 if (status < 0) |
|
140 { |
|
141 const char *errmsg = strerror (errno); |
|
142 |
|
143 if (errmsg) |
1755
|
144 error("%s: `%s'", name.c_str (), errmsg); |
1664
|
145 else |
1755
|
146 error("unable to link function `%s'", name.c_str ()); |
1664
|
147 |
1665
|
148 retval = 0; |
1664
|
149 } |
|
150 } |
|
151 else |
1755
|
152 error ("%s: %s `%s'", strerror (errno), file.c_str (), name.c_str ()); |
1665
|
153 |
|
154 return retval; |
1664
|
155 } |
|
156 |
|
157 #endif |
1742
|
158 #endif |
1664
|
159 |
1742
|
160 #if defined (WITH_DYNAMIC_LINKING) |
1664
|
161 static void * |
1755
|
162 resolve_octave_reference (const string& name, const string& file) |
1664
|
163 { |
|
164 #if defined (WITH_DL) |
|
165 |
1665
|
166 return dl_resolve_octave_reference (name, file); |
1664
|
167 |
|
168 #elif defined (WITH_SHL) |
|
169 |
1665
|
170 return shl_resolve_octave_reference (name, file); |
1664
|
171 |
|
172 #endif |
|
173 } |
1742
|
174 #endif |
1664
|
175 |
|
176 Octave_builtin_fcn |
1742
|
177 #if defined (WITH_DYNAMIC_LINKING) |
1755
|
178 load_octave_builtin (const string& name) |
1742
|
179 #else |
1755
|
180 load_octave_builtin (const string&) |
1742
|
181 #endif |
707
|
182 { |
|
183 Octave_builtin_fcn retval = 0; |
|
184 |
1664
|
185 #if defined (WITH_DYNAMIC_LINKING) |
|
186 |
1755
|
187 string mangled_name = mangle_octave_builtin_name (name); |
707
|
188 |
1664
|
189 retval = (Octave_builtin_fcn) resolve_octave_reference (mangled_name); |
707
|
190 |
1664
|
191 #endif |
|
192 |
707
|
193 return retval; |
|
194 } |
|
195 |
1664
|
196 int |
1755
|
197 load_octave_oct_file (const string& name) |
707
|
198 { |
1664
|
199 int retval = 0; |
|
200 |
|
201 #if defined (WITH_DYNAMIC_LINKING) |
|
202 |
1755
|
203 string oct_file = oct_file_in_path (name); |
707
|
204 |
1873
|
205 if (oct_file.empty ()) |
707
|
206 { |
1755
|
207 string mangled_name = mangle_octave_oct_file_name (name); |
707
|
208 |
|
209 Octave_builtin_fcn_struct_fcn f = |
1664
|
210 (Octave_builtin_fcn_struct_fcn) resolve_octave_reference |
707
|
211 (mangled_name, oct_file); |
|
212 |
|
213 if (f) |
|
214 { |
|
215 builtin_function *s = f (); |
|
216 |
|
217 if (s) |
|
218 { |
|
219 install_builtin_function (s); |
1664
|
220 retval = 1; |
707
|
221 } |
|
222 } |
|
223 } |
|
224 |
1664
|
225 #else |
|
226 |
|
227 (void) name; |
707
|
228 |
|
229 #endif |
|
230 |
1664
|
231 return retval; |
707
|
232 } |
|
233 |
|
234 void |
|
235 init_dynamic_linker (void) |
|
236 { |
1755
|
237 // Nothing to do anymore... |
707
|
238 } |
|
239 |
1
|
240 /* |
|
241 ;;; Local Variables: *** |
|
242 ;;; mode: C++ *** |
|
243 ;;; page-delimiter: "^/\\*" *** |
|
244 ;;; End: *** |
|
245 */ |