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