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