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