1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 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 |
1352
|
46 #include "defaults.h" |
|
47 #include "dirfns.h" |
|
48 #include "dynamic-ld.h" |
|
49 #include "error.h" |
1670
|
50 #include "toplev.h" |
1155
|
51 #include "pathsearch.h" |
1742
|
52 #include "pt-const.h" |
1
|
53 #include "user-prefs.h" |
1352
|
54 #include "utils.h" |
707
|
55 #include "variables.h" |
1
|
56 |
707
|
57 typedef builtin_function * (*Octave_builtin_fcn_struct_fcn)(void); |
|
58 |
1664
|
59 #if defined (WITH_DYNAMIC_LINKING) |
|
60 |
|
61 // XXX FIXME XXX -- need to provide some way to ensure that functions |
|
62 // that we are going to use will use the same naming convention as |
|
63 // Octave's internal functions. It needs to be simpler than the |
|
64 // current DEFUN_DLD() macro, which assumes you know how to name the |
|
65 // function, the struct, and the helper function. |
|
66 |
1755
|
67 static string |
|
68 mangle_octave_oct_file_name (const string& name) |
1664
|
69 { |
1755
|
70 string retval ("FS"); |
|
71 retval.append (name); |
|
72 retval.append ("__Fv"); |
1664
|
73 return retval; |
|
74 } |
|
75 |
|
76 #if defined (WITH_DL) |
|
77 |
|
78 static void * |
1755
|
79 dl_resolve_octave_reference (const string& name, const string& file) |
1664
|
80 { |
1665
|
81 void *retval = 0; |
|
82 |
1664
|
83 // Dynamic linking with dlopen/dlsym doesn't require specification |
|
84 // of the libraries at runtime. Instead, they are specified when |
|
85 // the .oct file is created. |
|
86 |
1755
|
87 void *handle = dlopen (file.c_str (), RTLD_LAZY); |
1664
|
88 |
|
89 if (handle) |
|
90 { |
1755
|
91 retval = dlsym (handle, name.c_str ()); |
1664
|
92 |
1665
|
93 if (! retval) |
1664
|
94 { |
|
95 const char *errmsg = dlerror (); |
|
96 |
|
97 if (errmsg) |
1755
|
98 error("%s: `%s'", name.c_str (), errmsg); |
1664
|
99 else |
1755
|
100 error("unable to link function `%s'", name.c_str ()); |
1664
|
101 |
|
102 dlclose (handle); |
|
103 } |
|
104 } |
|
105 else |
1755
|
106 error ("%s: %s `%s'", dlerror (), file.c_str (), name.c_str ()); |
1665
|
107 |
|
108 return retval; |
1664
|
109 } |
|
110 |
|
111 #elif defined (WITH_SHL) |
|
112 |
|
113 static void * |
1755
|
114 shl_resolve_octave_reference (const string& name, const string& file) |
1664
|
115 { |
1665
|
116 void *retval = 0; |
|
117 |
1664
|
118 // Dynamic linking with shl_load/shl_findsym doesn't require |
|
119 // specification of the libraries at runtime. Instead, they are |
|
120 // specified when the .oct file is created. |
|
121 |
1755
|
122 void *handle = shl_load (file.c_str (), BIND_DEFERRED, 0L); |
1664
|
123 |
|
124 if (handle) |
|
125 { |
1755
|
126 int status = shl_findsym ((shl_t *) &handle, name.c_str (), |
1665
|
127 TYPE_UNDEFINED, retval); |
1664
|
128 |
|
129 if (status < 0) |
|
130 { |
|
131 const char *errmsg = strerror (errno); |
|
132 |
|
133 if (errmsg) |
1755
|
134 error("%s: `%s'", name.c_str (), errmsg); |
1664
|
135 else |
1755
|
136 error("unable to link function `%s'", name.c_str ()); |
1664
|
137 |
1665
|
138 retval = 0; |
1664
|
139 } |
|
140 } |
|
141 else |
1755
|
142 error ("%s: %s `%s'", strerror (errno), file.c_str (), name.c_str ()); |
1665
|
143 |
|
144 return retval; |
1664
|
145 } |
|
146 |
|
147 #endif |
1742
|
148 #endif |
1664
|
149 |
1742
|
150 #if defined (WITH_DYNAMIC_LINKING) |
1664
|
151 static void * |
1755
|
152 resolve_octave_reference (const string& name, const string& file) |
1664
|
153 { |
|
154 #if defined (WITH_DL) |
|
155 |
1665
|
156 return dl_resolve_octave_reference (name, file); |
1664
|
157 |
|
158 #elif defined (WITH_SHL) |
|
159 |
1665
|
160 return shl_resolve_octave_reference (name, file); |
1664
|
161 |
|
162 #endif |
|
163 } |
1742
|
164 #endif |
1664
|
165 |
|
166 int |
1755
|
167 load_octave_oct_file (const string& name) |
707
|
168 { |
1664
|
169 int retval = 0; |
|
170 |
|
171 #if defined (WITH_DYNAMIC_LINKING) |
|
172 |
1755
|
173 string oct_file = oct_file_in_path (name); |
707
|
174 |
1907
|
175 if (! oct_file.empty ()) |
707
|
176 { |
1755
|
177 string mangled_name = mangle_octave_oct_file_name (name); |
707
|
178 |
|
179 Octave_builtin_fcn_struct_fcn f = |
1664
|
180 (Octave_builtin_fcn_struct_fcn) resolve_octave_reference |
707
|
181 (mangled_name, oct_file); |
|
182 |
|
183 if (f) |
|
184 { |
|
185 builtin_function *s = f (); |
|
186 |
|
187 if (s) |
|
188 { |
1907
|
189 install_builtin_function (*s); |
1664
|
190 retval = 1; |
707
|
191 } |
|
192 } |
|
193 } |
|
194 |
1664
|
195 #else |
|
196 |
|
197 (void) name; |
707
|
198 |
|
199 #endif |
|
200 |
1664
|
201 return retval; |
707
|
202 } |
|
203 |
|
204 void |
|
205 init_dynamic_linker (void) |
|
206 { |
1755
|
207 // Nothing to do anymore... |
707
|
208 } |
|
209 |
1
|
210 /* |
|
211 ;;; Local Variables: *** |
|
212 ;;; mode: C++ *** |
|
213 ;;; End: *** |
|
214 */ |