Commit | Line | Data |
a0d0e21e |
1 | /* dl_vms.xs |
2 | * |
3 | * Platform: OpenVMS, VAX or AXP |
bd3fa61c |
4 | * Author: Charles Bailey bailey@newman.upenn.edu |
748a9306 |
5 | * Revised: 12-Dec-1994 |
a0d0e21e |
6 | * |
7 | * Implementation Note |
8 | * This section is added as an aid to users and DynaLoader developers, in |
9 | * order to clarify the process of dynamic linking under VMS. |
10 | * dl_vms.xs uses the supported VMS dynamic linking call, which allows |
11 | * a running program to map an arbitrary file of executable code and call |
12 | * routines within that file. This is done via the VMS RTL routine |
13 | * lib$find_image_symbol, whose calling sequence is as follows: |
14 | * status = lib$find_image_symbol(imgname,symname,symval,defspec); |
15 | * where |
16 | * status = a standard VMS status value (unsigned long int) |
17 | * imgname = a fixed-length string descriptor, passed by |
18 | * reference, containing the NAME ONLY of the image |
19 | * file to be mapped. An attempt will be made to |
20 | * translate this string as a logical name, so it may |
21 | * not contain any characters which are not allowed in |
22 | * logical names. If no translation is found, imgname |
23 | * is used directly as the name of the image file. |
24 | * symname = a fixed-length string descriptor, passed by |
25 | * reference, containing the name of the routine |
26 | * to be located. |
27 | * symval = an unsigned long int, passed by reference, into |
28 | * which is written the entry point address of the |
29 | * routine whose name is specified in symname. |
30 | * defspec = a fixed-length string descriptor, passed by |
31 | * reference, containing a default file specification |
32 | * whichis used to fill in any missing parts of the |
33 | * image file specification after the imgname argument |
34 | * is processed. |
35 | * In order to accommodate the handling of the imgname argument, the routine |
36 | * dl_expandspec() is provided for use by perl code (e.g. dl_findfile) |
37 | * which wants to see what image file lib$find_image_symbol would use if |
38 | * it were passed a given file specification. The file specification passed |
39 | * to dl_expandspec() and dl_load_file() can be partial or complete, and can |
40 | * use VMS or Unix syntax; these routines perform the necessary conversions. |
41 | * In general, writers of perl extensions need only conform to the |
42 | * procedures set out in the DynaLoader documentation, and let the details |
43 | * be taken care of by the routines here and in DynaLoader.pm. If anyone |
44 | * comes across any incompatibilities, please let me know. Thanks. |
45 | * |
46 | */ |
47 | |
48 | #include "EXTERN.h" |
49 | #include "perl.h" |
50 | #include "XSUB.h" |
51 | |
52 | #include "dlutils.c" /* dl_debug, LastError; SaveError not used */ |
8e07c86e |
53 | |
54 | static AV *dl_require_symbols = Nullav; |
55 | |
a0d0e21e |
56 | /* N.B.: |
57 | * dl_debug and LastError are static vars; you'll need to deal |
58 | * with them appropriately if you need context independence |
59 | */ |
60 | |
61 | #include <descrip.h> |
62 | #include <fscndef.h> |
63 | #include <lib$routines.h> |
64 | #include <rms.h> |
65 | #include <ssdef.h> |
748a9306 |
66 | #include <starlet.h> |
a0d0e21e |
67 | |
68 | typedef unsigned long int vmssts; |
69 | |
70 | struct libref { |
71 | struct dsc$descriptor_s name; |
72 | struct dsc$descriptor_s defspec; |
73 | }; |
74 | |
75 | /* Static data for dl_expand_filespec() - This is static to save |
76 | * initialization on each call; if you need context-independence, |
77 | * just make these auto variables in dl_expandspec() and dl_load_file() |
78 | */ |
79 | static char dlesa[NAM$C_MAXRSS], dlrsa[NAM$C_MAXRSS]; |
80 | static struct FAB dlfab; |
81 | static struct NAM dlnam; |
82 | |
83 | /* $PutMsg action routine - records error message in LastError */ |
84 | static vmssts |
85 | copy_errmsg(msg,unused) |
86 | struct dsc$descriptor_s * msg; |
87 | vmssts unused; |
88 | { |
748a9306 |
89 | if (*(msg->dsc$a_pointer) == '%') { /* first line */ |
a0d0e21e |
90 | if (LastError) |
2708a6a3 |
91 | strncpy((LastError = saferealloc(LastError,msg->dsc$w_length+1)), |
a0d0e21e |
92 | msg->dsc$a_pointer, msg->dsc$w_length); |
93 | else |
2708a6a3 |
94 | strncpy((LastError = safemalloc(msg->dsc$w_length+1)), |
a0d0e21e |
95 | msg->dsc$a_pointer, msg->dsc$w_length); |
2708a6a3 |
96 | LastError[msg->dsc$w_length] = '\0'; |
a0d0e21e |
97 | } |
98 | else { /* continuation line */ |
99 | int errlen = strlen(LastError); |
2708a6a3 |
100 | LastError = saferealloc(LastError, errlen + msg->dsc$w_length + 2); |
a0d0e21e |
101 | LastError[errlen] = '\n'; LastError[errlen+1] = '\0'; |
102 | strncat(LastError, msg->dsc$a_pointer, msg->dsc$w_length); |
2708a6a3 |
103 | LastError[errlen+msg->dsc$w_length+1] = '\0'; |
a0d0e21e |
104 | } |
2708a6a3 |
105 | return 0; |
a0d0e21e |
106 | } |
107 | |
108 | /* Use $PutMsg to retrieve error message for failure status code */ |
109 | static void |
110 | dl_set_error(sts,stv) |
111 | vmssts sts; |
112 | vmssts stv; |
113 | { |
748a9306 |
114 | vmssts vec[3]; |
d28f7c37 |
115 | dTHX; |
a0d0e21e |
116 | |
117 | vec[0] = stv ? 2 : 1; |
118 | vec[1] = sts; vec[2] = stv; |
748a9306 |
119 | _ckvmssts(sys$putmsg(vec,copy_errmsg,0,0)); |
a0d0e21e |
120 | } |
121 | |
2708a6a3 |
122 | static unsigned int |
123 | findsym_handler(void *sig, void *mech) |
124 | { |
d28f7c37 |
125 | dTHX; |
2708a6a3 |
126 | unsigned long int myvec[8],args, *usig = (unsigned long int *) sig; |
127 | /* Be paranoid and assume signal vector passed in might be readonly */ |
128 | myvec[0] = args = usig[0] > 10 ? 9 : usig[0] - 1; |
129 | while (--args) myvec[args] = usig[args]; |
130 | _ckvmssts(sys$putmsg(myvec,copy_errmsg,0,0)); |
760ac839 |
131 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "findsym_handler: received\n\t%s\n",LastError)); |
2708a6a3 |
132 | return SS$_CONTINUE; |
133 | } |
134 | |
135 | /* wrapper for lib$find_image_symbol, so signalled errors can be saved |
136 | * for dl_error and then returned */ |
137 | static unsigned long int |
138 | my_find_image_symbol(struct dsc$descriptor_s *imgname, |
139 | struct dsc$descriptor_s *symname, |
140 | void (**entry)(), |
141 | struct dsc$descriptor_s *defspec) |
142 | { |
143 | unsigned long int retsts; |
144 | VAXC$ESTABLISH(findsym_handler); |
145 | retsts = lib$find_image_symbol(imgname,symname,entry,defspec); |
146 | return retsts; |
147 | } |
148 | |
149 | |
a0d0e21e |
150 | static void |
cea2e8a9 |
151 | dl_private_init(pTHX) |
a0d0e21e |
152 | { |
cea2e8a9 |
153 | dl_generic_private_init(aTHX); |
154 | dl_require_symbols = get_av("DynaLoader::dl_require_symbols", 0x4); |
a0d0e21e |
155 | /* Set up the static control blocks for dl_expand_filespec() */ |
156 | dlfab = cc$rms_fab; |
157 | dlnam = cc$rms_nam; |
158 | dlfab.fab$l_nam = &dlnam; |
159 | dlnam.nam$l_esa = dlesa; |
160 | dlnam.nam$b_ess = sizeof dlesa; |
161 | dlnam.nam$l_rsa = dlrsa; |
162 | dlnam.nam$b_rss = sizeof dlrsa; |
163 | } |
164 | MODULE = DynaLoader PACKAGE = DynaLoader |
165 | |
166 | BOOT: |
cea2e8a9 |
167 | (void)dl_private_init(aTHX); |
a0d0e21e |
168 | |
748a9306 |
169 | void |
a0d0e21e |
170 | dl_expandspec(filespec) |
171 | char * filespec |
172 | CODE: |
173 | char vmsspec[NAM$C_MAXRSS], defspec[NAM$C_MAXRSS]; |
174 | size_t deflen; |
175 | vmssts sts; |
176 | |
177 | tovmsspec(filespec,vmsspec); |
178 | dlfab.fab$l_fna = vmsspec; |
179 | dlfab.fab$b_fns = strlen(vmsspec); |
180 | dlfab.fab$l_dna = 0; |
181 | dlfab.fab$b_dns = 0; |
760ac839 |
182 | DLDEBUG(1,PerlIO_printf(PerlIO_stderr(), "dl_expand_filespec(%s):\n",vmsspec)); |
a0d0e21e |
183 | /* On the first pass, just parse the specification string */ |
184 | dlnam.nam$b_nop = NAM$M_SYNCHK; |
185 | sts = sys$parse(&dlfab); |
760ac839 |
186 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "\tSYNCHK sys$parse = %d\n",sts)); |
a0d0e21e |
187 | if (!(sts & 1)) { |
188 | dl_set_error(dlfab.fab$l_sts,dlfab.fab$l_stv); |
6b88bc9c |
189 | ST(0) = &PL_sv_undef; |
a0d0e21e |
190 | } |
191 | else { |
192 | /* Now set up a default spec - everything but the name */ |
748a9306 |
193 | deflen = dlnam.nam$l_name - dlesa; |
a0d0e21e |
194 | memcpy(defspec,dlesa,deflen); |
195 | memcpy(defspec+deflen,dlnam.nam$l_type, |
196 | dlnam.nam$b_type + dlnam.nam$b_ver); |
197 | deflen += dlnam.nam$b_type + dlnam.nam$b_ver; |
198 | memcpy(vmsspec,dlnam.nam$l_name,dlnam.nam$b_name); |
760ac839 |
199 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "\tsplit filespec: name = %.*s, default = %.*s\n", |
748a9306 |
200 | dlnam.nam$b_name,vmsspec,deflen,defspec)); |
a0d0e21e |
201 | /* . . . and go back to expand it */ |
202 | dlnam.nam$b_nop = 0; |
203 | dlfab.fab$l_dna = defspec; |
204 | dlfab.fab$b_dns = deflen; |
205 | dlfab.fab$b_fns = dlnam.nam$b_name; |
206 | sts = sys$parse(&dlfab); |
760ac839 |
207 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "\tname/default sys$parse = %d\n",sts)); |
a0d0e21e |
208 | if (!(sts & 1)) { |
209 | dl_set_error(dlfab.fab$l_sts,dlfab.fab$l_stv); |
6b88bc9c |
210 | ST(0) = &PL_sv_undef; |
a0d0e21e |
211 | } |
212 | else { |
213 | /* Now find the actual file */ |
214 | sts = sys$search(&dlfab); |
760ac839 |
215 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "\tsys$search = %d\n",sts)); |
2708a6a3 |
216 | if (!(sts & 1)) { |
a0d0e21e |
217 | dl_set_error(dlfab.fab$l_sts,dlfab.fab$l_stv); |
6b88bc9c |
218 | ST(0) = &PL_sv_undef; |
a0d0e21e |
219 | } |
220 | else { |
79cb57f6 |
221 | ST(0) = sv_2mortal(newSVpvn(dlnam.nam$l_rsa,dlnam.nam$b_rsl)); |
760ac839 |
222 | DLDEBUG(1,PerlIO_printf(PerlIO_stderr(), "\tresult = \\%.*s\\\n", |
a0d0e21e |
223 | dlnam.nam$b_rsl,dlnam.nam$l_rsa)); |
224 | } |
225 | } |
226 | } |
227 | |
748a9306 |
228 | void |
f86702cc |
229 | dl_load_file(filespec, flags) |
230 | char * filespec |
ff7f3c60 |
231 | int flags |
232 | PREINIT: |
ad6a8a52 |
233 | dTHX; |
a0d0e21e |
234 | char vmsspec[NAM$C_MAXRSS]; |
a0d0e21e |
235 | SV *reqSV, **reqSVhndl; |
236 | STRLEN deflen; |
237 | struct dsc$descriptor_s |
238 | specdsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0}, |
239 | symdsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0}; |
240 | struct fscnlst { |
241 | unsigned short int len; |
242 | unsigned short int code; |
243 | char *string; |
748a9306 |
244 | } namlst[2] = {{0,FSCN$_NAME,0},{0,0,0}}; |
a0d0e21e |
245 | struct libref *dlptr; |
246 | vmssts sts, failed = 0; |
2708a6a3 |
247 | void (*entry)(); |
ff7f3c60 |
248 | CODE: |
a0d0e21e |
249 | |
f86702cc |
250 | DLDEBUG(1,PerlIO_printf(PerlIO_stderr(), "dl_load_file(%s,%x):\n", filespec,flags)); |
a0d0e21e |
251 | specdsc.dsc$a_pointer = tovmsspec(filespec,vmsspec); |
252 | specdsc.dsc$w_length = strlen(specdsc.dsc$a_pointer); |
760ac839 |
253 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "\tVMS-ified filespec is %s\n", |
a0d0e21e |
254 | specdsc.dsc$a_pointer)); |
fc36a67e |
255 | New(1399,dlptr,1,struct libref); |
a0d0e21e |
256 | dlptr->name.dsc$b_dtype = dlptr->defspec.dsc$b_dtype = DSC$K_DTYPE_T; |
257 | dlptr->name.dsc$b_class = dlptr->defspec.dsc$b_class = DSC$K_CLASS_S; |
258 | sts = sys$filescan(&specdsc,namlst,0); |
760ac839 |
259 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "\tsys$filescan: returns %d, name is %.*s\n", |
a0d0e21e |
260 | sts,namlst[0].len,namlst[0].string)); |
261 | if (!(sts & 1)) { |
262 | failed = 1; |
263 | dl_set_error(sts,0); |
264 | } |
265 | else { |
266 | dlptr->name.dsc$w_length = namlst[0].len; |
267 | dlptr->name.dsc$a_pointer = savepvn(namlst[0].string,namlst[0].len); |
268 | dlptr->defspec.dsc$w_length = specdsc.dsc$w_length - namlst[0].len; |
8c52afec |
269 | New(1097, dlptr->defspec.dsc$a_pointer, dlptr->defspec.dsc$w_length + 1, char); |
a0d0e21e |
270 | deflen = namlst[0].string - specdsc.dsc$a_pointer; |
271 | memcpy(dlptr->defspec.dsc$a_pointer,specdsc.dsc$a_pointer,deflen); |
272 | memcpy(dlptr->defspec.dsc$a_pointer + deflen, |
273 | namlst[0].string + namlst[0].len, |
274 | dlptr->defspec.dsc$w_length - deflen); |
760ac839 |
275 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "\tlibref = name: %s, defspec: %.*s\n", |
a0d0e21e |
276 | dlptr->name.dsc$a_pointer, |
277 | dlptr->defspec.dsc$w_length, |
278 | dlptr->defspec.dsc$a_pointer)); |
8e07c86e |
279 | if (!(reqSVhndl = av_fetch(dl_require_symbols,0,FALSE)) || !(reqSV = *reqSVhndl)) { |
760ac839 |
280 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "\t@dl_require_symbols empty, returning untested libref\n")); |
a0d0e21e |
281 | } |
282 | else { |
283 | symdsc.dsc$w_length = SvCUR(reqSV); |
284 | symdsc.dsc$a_pointer = SvPVX(reqSV); |
760ac839 |
285 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "\t$dl_require_symbols[0] = %.*s\n", |
a0d0e21e |
286 | symdsc.dsc$w_length, symdsc.dsc$a_pointer)); |
2708a6a3 |
287 | sts = my_find_image_symbol(&(dlptr->name),&symdsc, |
a0d0e21e |
288 | &entry,&(dlptr->defspec)); |
760ac839 |
289 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "\tlib$find_image_symbol returns %d\n",sts)); |
a0d0e21e |
290 | if (!(sts&1)) { |
291 | failed = 1; |
292 | dl_set_error(sts,0); |
293 | } |
294 | } |
295 | } |
296 | |
297 | if (failed) { |
298 | Safefree(dlptr->name.dsc$a_pointer); |
299 | Safefree(dlptr->defspec.dsc$a_pointer); |
300 | Safefree(dlptr); |
6b88bc9c |
301 | ST(0) = &PL_sv_undef; |
a0d0e21e |
302 | } |
303 | else { |
748a9306 |
304 | ST(0) = sv_2mortal(newSViv((IV) dlptr)); |
a0d0e21e |
305 | } |
306 | |
307 | |
748a9306 |
308 | void |
a0d0e21e |
309 | dl_find_symbol(librefptr,symname) |
310 | void * librefptr |
311 | SV * symname |
312 | CODE: |
313 | struct libref thislib = *((struct libref *)librefptr); |
314 | struct dsc$descriptor_s |
315 | symdsc = {SvCUR(symname),DSC$K_DTYPE_T,DSC$K_CLASS_S,SvPVX(symname)}; |
316 | void (*entry)(); |
317 | vmssts sts; |
318 | |
760ac839 |
319 | DLDEBUG(1,PerlIO_printf(PerlIO_stderr(), "dl_find_dymbol(%.*s,%.*s):\n", |
a0d0e21e |
320 | thislib.name.dsc$w_length, thislib.name.dsc$a_pointer, |
321 | symdsc.dsc$w_length,symdsc.dsc$a_pointer)); |
2708a6a3 |
322 | sts = my_find_image_symbol(&(thislib.name),&symdsc, |
323 | &entry,&(thislib.defspec)); |
760ac839 |
324 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "\tlib$find_image_symbol returns %d\n",sts)); |
325 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "\tentry point is %d\n", |
a0d0e21e |
326 | (unsigned long int) entry)); |
327 | if (!(sts & 1)) { |
2708a6a3 |
328 | /* error message already saved by findsym_handler */ |
6b88bc9c |
329 | ST(0) = &PL_sv_undef; |
a0d0e21e |
330 | } |
748a9306 |
331 | else ST(0) = sv_2mortal(newSViv((IV) entry)); |
a0d0e21e |
332 | |
333 | |
334 | void |
335 | dl_undef_symbols() |
336 | PPCODE: |
337 | |
338 | |
339 | # These functions should not need changing on any platform: |
340 | |
341 | void |
342 | dl_install_xsub(perl_name, symref, filename="$Package") |
343 | char * perl_name |
344 | void * symref |
345 | char * filename |
346 | CODE: |
760ac839 |
347 | DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_install_xsub(name=%s, symref=%x)\n", |
a0d0e21e |
348 | perl_name, symref)); |
cea2e8a9 |
349 | ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name, |
350 | (void(*)(pTHX_ CV *))symref, |
351 | filename))); |
a0d0e21e |
352 | |
353 | |
354 | char * |
355 | dl_error() |
356 | CODE: |
357 | RETVAL = LastError ; |
358 | OUTPUT: |
359 | RETVAL |
360 | |
361 | # end. |