This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new perldelta
[perl5.git] / ext / DynaLoader / dl_vms.xs
CommitLineData
a0d0e21e
LW
1/* dl_vms.xs
2 *
a491bdcd 3 * Platform: OpenVMS, VAX or AXP or IA64
bd3fa61c 4 * Author: Charles Bailey bailey@newman.upenn.edu
a491bdcd 5 * Revised: See http://public.activestate.com/cgi-bin/perlbrowse
a0d0e21e
LW
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
73e43954 48#define PERL_EXT
a0d0e21e 49#include "EXTERN.h"
d96ba2c4 50#define PERL_IN_DL_VMS_XS
a0d0e21e
LW
51#include "perl.h"
52#include "XSUB.h"
53
a0d0e21e
LW
54#include <descrip.h>
55#include <fscndef.h>
56#include <lib$routines.h>
57#include <rms.h>
58#include <ssdef.h>
748a9306 59#include <starlet.h>
a0d0e21e 60
b6837a3b
CB
61#if defined(VMS_WE_ARE_CASE_SENSITIVE)
62#define DL_CASE_SENSITIVE 1<<4
63#else
64#define DL_CASE_SENSITIVE 0
65#endif
66
a0d0e21e
LW
67typedef unsigned long int vmssts;
68
69struct libref {
70 struct dsc$descriptor_s name;
71 struct dsc$descriptor_s defspec;
72};
73
cdc73a10
JH
74typedef struct {
75 AV * x_require_symbols;
76/* "Static" data for dl_expand_filespec() - This is static to save
a0d0e21e
LW
77 * initialization on each call; if you need context-independence,
78 * just make these auto variables in dl_expandspec() and dl_load_file()
79 */
cdc73a10
JH
80 char x_esa[NAM$C_MAXRSS];
81 char x_rsa[NAM$C_MAXRSS];
82 struct FAB x_fab;
83 struct NAM x_nam;
84} my_cxtx_t; /* this *must* be named my_cxtx_t */
85
86#define DL_CXT_EXTRA /* ask for dl_cxtx to be defined in dlutils.c */
87#include "dlutils.c" /* dl_debug, dl_last_error; SaveError not used */
88
89#define dl_require_symbols (dl_cxtx.x_require_symbols)
90#define dl_esa (dl_cxtx.x_esa)
91#define dl_rsa (dl_cxtx.x_rsa)
92#define dl_fab (dl_cxtx.x_fab)
93#define dl_nam (dl_cxtx.x_nam)
94
95/* $PutMsg action routine - records error message in dl_last_error */
a0d0e21e 96static vmssts
f53cfc92 97copy_errmsg(struct dsc$descriptor_s *msg, vmssts unused)
a0d0e21e 98{
ecdc15d9 99 dTHX;
cdc73a10 100 dMY_CXT;
748a9306 101 if (*(msg->dsc$a_pointer) == '%') { /* first line */
ec6cc21d 102 sv_setpvn(MY_CXT.x_dl_last_error, msg->dsc$a_pointer, (STRLEN)msg->dsc$w_length);
a0d0e21e
LW
103 }
104 else { /* continuation line */
ec6cc21d 105 sv_catpvn(MY_CXT.x_dl_last_error, msg->dsc$a_pointer, (STRLEN)msg->dsc$w_length);
a0d0e21e 106 }
8381071f 107 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "Saved error message: %s\n", dl_last_error));
2708a6a3 108 return 0;
a0d0e21e
LW
109}
110
111/* Use $PutMsg to retrieve error message for failure status code */
112static void
f53cfc92 113dl_set_error(vmssts sts, vmssts stv)
a0d0e21e 114{
748a9306 115 vmssts vec[3];
5c84aa53 116 dTHX;
a0d0e21e
LW
117
118 vec[0] = stv ? 2 : 1;
119 vec[1] = sts; vec[2] = stv;
748a9306 120 _ckvmssts(sys$putmsg(vec,copy_errmsg,0,0));
a0d0e21e
LW
121}
122
2708a6a3 123/* wrapper for lib$find_image_symbol, so signalled errors can be saved
124 * for dl_error and then returned */
125static unsigned long int
126my_find_image_symbol(struct dsc$descriptor_s *imgname,
127 struct dsc$descriptor_s *symname,
128 void (**entry)(),
129 struct dsc$descriptor_s *defspec)
130{
131 unsigned long int retsts;
7d08496d 132 VAXC$ESTABLISH((__vms_handler)lib$sig_to_ret);
b6837a3b 133 retsts = lib$find_image_symbol(imgname,symname,entry,defspec,DL_CASE_SENSITIVE);
2708a6a3 134 return retsts;
135}
136
137
a0d0e21e 138static void
cea2e8a9 139dl_private_init(pTHX)
a0d0e21e 140{
cea2e8a9 141 dl_generic_private_init(aTHX);
cdc73a10
JH
142 {
143 dMY_CXT;
8c472fc1 144 dl_require_symbols = get_av("DynaLoader::dl_require_symbols", GV_ADDMULTI);
cdc73a10
JH
145 /* Set up the static control blocks for dl_expand_filespec() */
146 dl_fab = cc$rms_fab;
147 dl_nam = cc$rms_nam;
148 dl_fab.fab$l_nam = &dl_nam;
149 dl_nam.nam$l_esa = dl_esa;
150 dl_nam.nam$b_ess = sizeof dl_esa;
151 dl_nam.nam$l_rsa = dl_rsa;
152 dl_nam.nam$b_rss = sizeof dl_rsa;
153 }
a0d0e21e
LW
154}
155MODULE = DynaLoader PACKAGE = DynaLoader
156
157BOOT:
cea2e8a9 158 (void)dl_private_init(aTHX);
a0d0e21e 159
748a9306 160void
a0d0e21e
LW
161dl_expandspec(filespec)
162 char * filespec
163 CODE:
164 char vmsspec[NAM$C_MAXRSS], defspec[NAM$C_MAXRSS];
165 size_t deflen;
166 vmssts sts;
cdc73a10 167 dMY_CXT;
a0d0e21e
LW
168
169 tovmsspec(filespec,vmsspec);
cdc73a10
JH
170 dl_fab.fab$l_fna = vmsspec;
171 dl_fab.fab$b_fns = strlen(vmsspec);
172 dl_fab.fab$l_dna = 0;
173 dl_fab.fab$b_dns = 0;
146174a9 174 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_expand_filespec(%s):\n",vmsspec));
a0d0e21e 175 /* On the first pass, just parse the specification string */
cdc73a10
JH
176 dl_nam.nam$b_nop = NAM$M_SYNCHK;
177 sts = sys$parse(&dl_fab);
146174a9 178 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tSYNCHK sys$parse = %d\n",sts));
a0d0e21e 179 if (!(sts & 1)) {
cdc73a10 180 dl_set_error(dl_fab.fab$l_sts,dl_fab.fab$l_stv);
6b88bc9c 181 ST(0) = &PL_sv_undef;
a0d0e21e
LW
182 }
183 else {
184 /* Now set up a default spec - everything but the name */
cdc73a10
JH
185 deflen = dl_nam.nam$l_name - dl_esa;
186 memcpy(defspec,dl_esa,deflen);
187 memcpy(defspec+deflen,dl_nam.nam$l_type,
188 dl_nam.nam$b_type + dl_nam.nam$b_ver);
189 deflen += dl_nam.nam$b_type + dl_nam.nam$b_ver;
190 memcpy(vmsspec,dl_nam.nam$l_name,dl_nam.nam$b_name);
146174a9 191 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tsplit filespec: name = %.*s, default = %.*s\n",
cdc73a10 192 dl_nam.nam$b_name,vmsspec,deflen,defspec));
a0d0e21e 193 /* . . . and go back to expand it */
cdc73a10
JH
194 dl_nam.nam$b_nop = 0;
195 dl_fab.fab$l_dna = defspec;
196 dl_fab.fab$b_dns = deflen;
197 dl_fab.fab$b_fns = dl_nam.nam$b_name;
198 sts = sys$parse(&dl_fab);
146174a9 199 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tname/default sys$parse = %d\n",sts));
a0d0e21e 200 if (!(sts & 1)) {
cdc73a10 201 dl_set_error(dl_fab.fab$l_sts,dl_fab.fab$l_stv);
6b88bc9c 202 ST(0) = &PL_sv_undef;
a0d0e21e
LW
203 }
204 else {
205 /* Now find the actual file */
cdc73a10 206 sts = sys$search(&dl_fab);
146174a9 207 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tsys$search = %d\n",sts));
2708a6a3 208 if (!(sts & 1)) {
cdc73a10 209 dl_set_error(dl_fab.fab$l_sts,dl_fab.fab$l_stv);
6b88bc9c 210 ST(0) = &PL_sv_undef;
a0d0e21e
LW
211 }
212 else {
cdc73a10 213 ST(0) = sv_2mortal(newSVpvn(dl_nam.nam$l_rsa,dl_nam.nam$b_rsl));
146174a9 214 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "\tresult = \\%.*s\\\n",
cdc73a10 215 dl_nam.nam$b_rsl,dl_nam.nam$l_rsa));
a0d0e21e
LW
216 }
217 }
218 }
219
748a9306 220void
a491bdcd
CB
221dl_load_file(filename, flags=0)
222 char * filename
ff7f3c60
NIS
223 int flags
224 PREINIT:
626727d5 225 dTHX;
ecdc15d9 226 dMY_CXT;
a0d0e21e 227 char vmsspec[NAM$C_MAXRSS];
a0d0e21e
LW
228 SV *reqSV, **reqSVhndl;
229 STRLEN deflen;
230 struct dsc$descriptor_s
231 specdsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0},
232 symdsc = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
233 struct fscnlst {
234 unsigned short int len;
235 unsigned short int code;
236 char *string;
748a9306 237 } namlst[2] = {{0,FSCN$_NAME,0},{0,0,0}};
a0d0e21e
LW
238 struct libref *dlptr;
239 vmssts sts, failed = 0;
2708a6a3 240 void (*entry)();
ff7f3c60 241 CODE:
a0d0e21e 242
a491bdcd
CB
243 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags));
244 specdsc.dsc$a_pointer = tovmsspec(filename,vmsspec);
a0d0e21e 245 specdsc.dsc$w_length = strlen(specdsc.dsc$a_pointer);
a491bdcd
CB
246 if (specdsc.dsc$w_length == 0) { /* undef in, empty out */
247 XSRETURN_EMPTY;
248 }
249 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tVMS-ified filename is %s\n",
a0d0e21e 250 specdsc.dsc$a_pointer));
a02a5408 251 Newx(dlptr,1,struct libref);
a0d0e21e
LW
252 dlptr->name.dsc$b_dtype = dlptr->defspec.dsc$b_dtype = DSC$K_DTYPE_T;
253 dlptr->name.dsc$b_class = dlptr->defspec.dsc$b_class = DSC$K_CLASS_S;
254 sts = sys$filescan(&specdsc,namlst,0);
146174a9 255 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tsys$filescan: returns %d, name is %.*s\n",
a0d0e21e
LW
256 sts,namlst[0].len,namlst[0].string));
257 if (!(sts & 1)) {
258 failed = 1;
259 dl_set_error(sts,0);
260 }
261 else {
262 dlptr->name.dsc$w_length = namlst[0].len;
263 dlptr->name.dsc$a_pointer = savepvn(namlst[0].string,namlst[0].len);
264 dlptr->defspec.dsc$w_length = specdsc.dsc$w_length - namlst[0].len;
a02a5408 265 Newx(dlptr->defspec.dsc$a_pointer, dlptr->defspec.dsc$w_length + 1, char);
a0d0e21e
LW
266 deflen = namlst[0].string - specdsc.dsc$a_pointer;
267 memcpy(dlptr->defspec.dsc$a_pointer,specdsc.dsc$a_pointer,deflen);
268 memcpy(dlptr->defspec.dsc$a_pointer + deflen,
269 namlst[0].string + namlst[0].len,
270 dlptr->defspec.dsc$w_length - deflen);
146174a9 271 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tlibref = name: %s, defspec: %.*s\n",
a0d0e21e
LW
272 dlptr->name.dsc$a_pointer,
273 dlptr->defspec.dsc$w_length,
274 dlptr->defspec.dsc$a_pointer));
8e07c86e 275 if (!(reqSVhndl = av_fetch(dl_require_symbols,0,FALSE)) || !(reqSV = *reqSVhndl)) {
146174a9 276 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\t@dl_require_symbols empty, returning untested libref\n"));
a0d0e21e
LW
277 }
278 else {
279 symdsc.dsc$w_length = SvCUR(reqSV);
280 symdsc.dsc$a_pointer = SvPVX(reqSV);
146174a9 281 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\t$dl_require_symbols[0] = %.*s\n",
a0d0e21e 282 symdsc.dsc$w_length, symdsc.dsc$a_pointer));
2708a6a3 283 sts = my_find_image_symbol(&(dlptr->name),&symdsc,
a0d0e21e 284 &entry,&(dlptr->defspec));
146174a9 285 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tlib$find_image_symbol returns %d\n",sts));
a0d0e21e
LW
286 if (!(sts&1)) {
287 failed = 1;
288 dl_set_error(sts,0);
289 }
290 }
291 }
292
293 if (failed) {
294 Safefree(dlptr->name.dsc$a_pointer);
295 Safefree(dlptr->defspec.dsc$a_pointer);
296 Safefree(dlptr);
6b88bc9c 297 ST(0) = &PL_sv_undef;
a0d0e21e
LW
298 }
299 else {
c529f79d 300 ST(0) = sv_2mortal(newSViv(PTR2IV(dlptr)));
a0d0e21e
LW
301 }
302
303
748a9306 304void
fd46a708 305dl_find_symbol(librefptr,symname,ign_err=0)
a0d0e21e
LW
306 void * librefptr
307 SV * symname
fd46a708 308 int ign_err
3e584864 309 PREINIT:
a0d0e21e
LW
310 struct libref thislib = *((struct libref *)librefptr);
311 struct dsc$descriptor_s
312 symdsc = {SvCUR(symname),DSC$K_DTYPE_T,DSC$K_CLASS_S,SvPVX(symname)};
313 void (*entry)();
314 vmssts sts;
3e584864 315 CODE:
a0d0e21e 316
a491bdcd 317 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_find_symbol(%.*s,%.*s):\n",
a0d0e21e
LW
318 thislib.name.dsc$w_length, thislib.name.dsc$a_pointer,
319 symdsc.dsc$w_length,symdsc.dsc$a_pointer));
2708a6a3 320 sts = my_find_image_symbol(&(thislib.name),&symdsc,
321 &entry,&(thislib.defspec));
146174a9
CB
322 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tlib$find_image_symbol returns %d\n",sts));
323 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "\tentry point is %d\n",
a0d0e21e
LW
324 (unsigned long int) entry));
325 if (!(sts & 1)) {
fd46a708 326 if (!ign_err) dl_set_error(sts,0);
6b88bc9c 327 ST(0) = &PL_sv_undef;
a0d0e21e 328 }
c529f79d 329 else ST(0) = sv_2mortal(newSViv(PTR2IV(entry)));
a0d0e21e
LW
330
331
332void
333dl_undef_symbols()
334 PPCODE:
335
336
337# These functions should not need changing on any platform:
338
339void
340dl_install_xsub(perl_name, symref, filename="$Package")
341 char * perl_name
342 void * symref
d3f5e399 343 const char * filename
a0d0e21e 344 CODE:
146174a9 345 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n",
a0d0e21e 346 perl_name, symref));
77004dee
NC
347 ST(0) = sv_2mortal(newRV((SV*)newXS_flags(perl_name,
348 (void(*)(pTHX_ CV *))symref,
349 filename, NULL,
350 XS_DYNAMIC_FILENAME)));
a0d0e21e
LW
351
352
bb6a367a 353SV *
a0d0e21e
LW
354dl_error()
355 CODE:
cdc73a10 356 dMY_CXT;
bb6a367a 357 RETVAL = newSVsv(MY_CXT.x_dl_last_error);
a0d0e21e 358 OUTPUT:
bb6a367a 359 RETVAL
a0d0e21e 360
8c472fc1
CB
361#if defined(USE_ITHREADS)
362
363void
364CLONE(...)
365 CODE:
366 MY_CXT_CLONE;
367
3bd46979
DM
368 PERL_UNUSED_VAR(items);
369
8c472fc1
CB
370 /* MY_CXT_CLONE just does a memcpy on the whole structure, so to avoid
371 * using Perl variables that belong to another thread, we create our
372 * own for this thread.
373 */
c2b90b61 374 MY_CXT.x_dl_last_error = newSVpvs("");
8c472fc1
CB
375 dl_require_symbols = get_av("DynaLoader::dl_require_symbols", GV_ADDMULTI);
376
7bd4cafe
CB
377 /* Set up the "static" control blocks for dl_expand_filespec() */
378 dl_fab = cc$rms_fab;
379 dl_nam = cc$rms_nam;
380 dl_fab.fab$l_nam = &dl_nam;
381 dl_nam.nam$l_esa = dl_esa;
382 dl_nam.nam$b_ess = sizeof dl_esa;
383 dl_nam.nam$l_rsa = dl_rsa;
384 dl_nam.nam$b_rss = sizeof dl_rsa;
385
8c472fc1
CB
386#endif
387
a0d0e21e 388# end.