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