| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/vmimage.cpp,v 1.3 1999/07/11 00:46:59 MJRoberts Exp $"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | vmimage.cpp - VM image file loader |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 12/12/98 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <memory.h> |
| 26 | |
| 27 | #include "os.h" |
| 28 | #include "t3std.h" |
| 29 | #include "vmtype.h" |
| 30 | #include "vminit.h" |
| 31 | #include "vmimage.h" |
| 32 | #include "vmerr.h" |
| 33 | #include "vmerrnum.h" |
| 34 | #include "vmfile.h" |
| 35 | #include "vmmeta.h" |
| 36 | #include "vmbif.h" |
| 37 | #include "vmpredef.h" |
| 38 | #include "vmrun.h" |
| 39 | #include "vmtobj.h" |
| 40 | #include "vmhost.h" |
| 41 | #include "vmobj.h" |
| 42 | #include "vmstr.h" |
| 43 | #include "vmlst.h" |
| 44 | #include "vmsave.h" |
| 45 | #include "vmrunsym.h" |
| 46 | #include "vmlookup.h" |
| 47 | #include "vmstack.h" |
| 48 | #include "tcprstyp.h" |
| 49 | #include "vmhash.h" |
| 50 | #include "vmsrcf.h" |
| 51 | #include "vmvec.h" |
| 52 | #include "charmap.h" |
| 53 | |
| 54 | |
| 55 | /* ------------------------------------------------------------------------ */ |
| 56 | /* |
| 57 | * Hash table entry for the exported symbols table |
| 58 | */ |
| 59 | class CVmHashEntryExport: public CVmHashEntryCS |
| 60 | { |
| 61 | public: |
| 62 | CVmHashEntryExport(const char *nm, size_t len, int copy_nm, |
| 63 | const vm_val_t *val) |
| 64 | : CVmHashEntryCS(nm, len, copy_nm) |
| 65 | { |
| 66 | /* remember the value */ |
| 67 | val_ = *val; |
| 68 | } |
| 69 | |
| 70 | /* the value of this symbol */ |
| 71 | vm_val_t val_; |
| 72 | }; |
| 73 | |
| 74 | /* ------------------------------------------------------------------------ */ |
| 75 | /* |
| 76 | * Implementation of CVmImageLoaderMres interface for regular image file |
| 77 | * resource loading |
| 78 | */ |
| 79 | class CVmImageLoaderMres_std: public CVmImageLoaderMres |
| 80 | { |
| 81 | public: |
| 82 | CVmImageLoaderMres_std(int fileno, CVmHostIfc *hostifc) |
| 83 | { |
| 84 | fileno_ = fileno; |
| 85 | hostifc_ = hostifc; |
| 86 | } |
| 87 | |
| 88 | /* add a resource */ |
| 89 | void add_resource(uint32_t seek_ofs, uint32_t siz, |
| 90 | const char *res_name, size_t res_name_len) |
| 91 | { |
| 92 | /* call the host system interface to add the resource */ |
| 93 | hostifc_->add_resource(seek_ofs, siz, res_name, res_name_len, |
| 94 | fileno_); |
| 95 | } |
| 96 | |
| 97 | /* add a local file resource link */ |
| 98 | void add_resource(const char *fname, size_t fnamelen, |
| 99 | const char *res_name, size_t res_name_len) |
| 100 | { |
| 101 | /* call the host system inetrface to add it */ |
| 102 | hostifc_->add_resource(fname, fnamelen, res_name, res_name_len); |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | /* host interface object */ |
| 107 | CVmHostIfc *hostifc_; |
| 108 | |
| 109 | /* file number to pass to host interface */ |
| 110 | int fileno_; |
| 111 | }; |
| 112 | |
| 113 | |
| 114 | /* ------------------------------------------------------------------------ */ |
| 115 | /* |
| 116 | * Image file loader implementation |
| 117 | */ |
| 118 | |
| 119 | /* |
| 120 | * instantiation |
| 121 | */ |
| 122 | CVmImageLoader::CVmImageLoader(CVmImageFile *fp, const char *fname, |
| 123 | long base_ofs) |
| 124 | { |
| 125 | size_t i; |
| 126 | char fname_abs[OSFNMAX], path[OSFNMAX]; |
| 127 | |
| 128 | /* remember the underlying image file interface */ |
| 129 | fp_ = fp; |
| 130 | |
| 131 | /* remember the image filename and the base seek offset */ |
| 132 | fname_ = lib_copy_str(fname); |
| 133 | base_seek_ofs_ = base_ofs; |
| 134 | |
| 135 | /* save the file's fully-qualified, absolute directory path */ |
| 136 | os_get_abs_filename(fname_abs, sizeof(fname_abs), fname); |
| 137 | os_get_path_name(path, sizeof(path), fname_abs); |
| 138 | path_ = lib_copy_str(path); |
| 139 | |
| 140 | /* we don't know the file version yet */ |
| 141 | ver_ = 0; |
| 142 | |
| 143 | /* allocate the pool tracking objects */ |
| 144 | for (i = 0 ; i < sizeof(pools_)/sizeof(pools_[0]) ; ++i) |
| 145 | pools_[i] = new CVmImagePool(); |
| 146 | |
| 147 | /* no metaclass dependency table loaded yet */ |
| 148 | loaded_meta_dep_ = FALSE; |
| 149 | |
| 150 | /* no function set dependency table loaded yet */ |
| 151 | loaded_funcset_dep_ = FALSE; |
| 152 | |
| 153 | /* no entrypoint loaded yet */ |
| 154 | loaded_entrypt_ = FALSE; |
| 155 | |
| 156 | /* no GSYM block yet */ |
| 157 | has_gsym_ = FALSE; |
| 158 | |
| 159 | /* no runtime symbols loaded yet */ |
| 160 | runtime_symtab_ = 0; |
| 161 | runtime_macros_ = 0; |
| 162 | |
| 163 | /* there's no reflection LookupTable yet */ |
| 164 | reflection_symtab_ = VM_INVALID_OBJ; |
| 165 | reflection_macros_ = VM_INVALID_OBJ; |
| 166 | |
| 167 | /* create the exported symbol hash table */ |
| 168 | exports_ = new CVmHashTable(64, new CVmHashFuncCS(), TRUE); |
| 169 | |
| 170 | /* create the synthesized exports hash table */ |
| 171 | synth_exports_ = new CVmHashTable(16, new CVmHashFuncCS(), TRUE); |
| 172 | |
| 173 | /* no static initializer pages yet */ |
| 174 | static_head_ = static_tail_ = 0; |
| 175 | |
| 176 | /* we don't have a static initializer code offset yet */ |
| 177 | static_cs_ofs_ = 0; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * destruction |
| 182 | */ |
| 183 | CVmImageLoader::~CVmImageLoader() |
| 184 | { |
| 185 | size_t i; |
| 186 | |
| 187 | /* delete the pool tracking objects */ |
| 188 | for (i = 0 ; i < sizeof(pools_)/sizeof(pools_[0]) ; ++i) |
| 189 | delete pools_[i]; |
| 190 | |
| 191 | /* delete the filename string and path */ |
| 192 | lib_free_str(fname_); |
| 193 | lib_free_str(path_); |
| 194 | |
| 195 | /* if we have our own runtime symbol/macro tables, delete them */ |
| 196 | if (runtime_symtab_ != 0) |
| 197 | delete runtime_symtab_; |
| 198 | if (runtime_macros_ != 0) |
| 199 | delete runtime_macros_; |
| 200 | |
| 201 | /* delete the exports tables */ |
| 202 | delete exports_; |
| 203 | delete synth_exports_; |
| 204 | |
| 205 | /* delete the static initializer pages */ |
| 206 | while (static_head_ != 0) |
| 207 | { |
| 208 | CVmStaticInitPage *nxt; |
| 209 | |
| 210 | /* remember the next one */ |
| 211 | nxt = static_head_->nxt_; |
| 212 | |
| 213 | /* free the image-allocated data associated with the page */ |
| 214 | fp_->free_mem(static_head_->data_); |
| 215 | |
| 216 | /* delete this one */ |
| 217 | delete static_head_; |
| 218 | |
| 219 | /* move on to the next */ |
| 220 | static_head_ = nxt; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /* ------------------------------------------------------------------------ */ |
| 226 | /* |
| 227 | * load the image |
| 228 | */ |
| 229 | void CVmImageLoader::load(VMG0_) |
| 230 | { |
| 231 | char buf[128]; |
| 232 | int done; |
| 233 | |
| 234 | /* set myself to be the global image loader */ |
| 235 | G_image_loader = this; |
| 236 | |
| 237 | /* |
| 238 | * perform additional initialization now that we're about to load |
| 239 | * the image file |
| 240 | */ |
| 241 | vm_init_before_load(vmg_ fname_); |
| 242 | |
| 243 | /* tell the host application the name of the image file */ |
| 244 | G_host_ifc->set_image_name(fname_); |
| 245 | |
| 246 | /* read and validate the header */ |
| 247 | read_image_header(); |
| 248 | |
| 249 | /* load the data blocks */ |
| 250 | for (done = FALSE ; !done ; ) |
| 251 | { |
| 252 | ulong siz; |
| 253 | uint flags; |
| 254 | |
| 255 | /* read the next data block header */ |
| 256 | fp_->copy_data(buf, 10); |
| 257 | |
| 258 | /* get the size */ |
| 259 | siz = t3rp4u(buf + 4); |
| 260 | |
| 261 | /* get the flags */ |
| 262 | flags = osrp2(buf + 8); |
| 263 | |
| 264 | /* load the block according to its type */ |
| 265 | if (block_type_is(buf, "CPPG")) |
| 266 | { |
| 267 | /* load the constant pool page block */ |
| 268 | load_const_pool_page(siz); |
| 269 | } |
| 270 | else if (block_type_is(buf, "ENTP")) |
| 271 | { |
| 272 | /* load the entrypoint */ |
| 273 | load_entrypt(vmg_ siz); |
| 274 | } |
| 275 | else if (block_type_is(buf, "OBJS")) |
| 276 | { |
| 277 | /* load static object block */ |
| 278 | load_static_objs(vmg_ siz); |
| 279 | } |
| 280 | else if (block_type_is(buf, "CPDF")) |
| 281 | { |
| 282 | /* load the constant pool definition block */ |
| 283 | load_const_pool_def(siz); |
| 284 | } |
| 285 | else if (block_type_is(buf, "MRES")) |
| 286 | { |
| 287 | /* |
| 288 | * load the multimedia resource block (the main image file |
| 289 | * is always file number zero) |
| 290 | */ |
| 291 | CVmImageLoaderMres_std res_ifc(0, G_host_ifc); |
| 292 | load_mres(siz, &res_ifc); |
| 293 | } |
| 294 | else if (block_type_is(buf, "MREL")) |
| 295 | { |
| 296 | /* load the multimedia resource link block */ |
| 297 | CVmImageLoaderMres_std res_ifc(0, G_host_ifc); |
| 298 | load_mres_link(siz, &res_ifc); |
| 299 | } |
| 300 | else if (block_type_is(buf, "MCLD")) |
| 301 | { |
| 302 | /* load metaclass dependency block */ |
| 303 | load_meta_dep(vmg_ siz); |
| 304 | } |
| 305 | else if (block_type_is(buf, "FNSD")) |
| 306 | { |
| 307 | /* load function set dependency list block */ |
| 308 | load_funcset_dep(vmg_ siz); |
| 309 | } |
| 310 | else if (block_type_is(buf, "SYMD")) |
| 311 | { |
| 312 | /* load symbolic names export block */ |
| 313 | load_sym_names(vmg_ siz); |
| 314 | } |
| 315 | else if (block_type_is(buf, "SRCF")) |
| 316 | { |
| 317 | /* load the source file list */ |
| 318 | load_srcfiles(vmg_ siz); |
| 319 | } |
| 320 | else if (block_type_is(buf, "GSYM")) |
| 321 | { |
| 322 | /* load the global symbols block */ |
| 323 | load_gsym(vmg_ siz); |
| 324 | } |
| 325 | else if (block_type_is(buf, "MACR")) |
| 326 | { |
| 327 | /* load the macro symbols block */ |
| 328 | load_macros(vmg_ siz); |
| 329 | } |
| 330 | else if (block_type_is(buf, "MHLS")) |
| 331 | { |
| 332 | /* load the method header list block */ |
| 333 | load_mhls(vmg_ siz); |
| 334 | } |
| 335 | else if (block_type_is(buf, "SINI")) |
| 336 | { |
| 337 | /* load the static initializer block */ |
| 338 | load_sini(vmg_ siz); |
| 339 | } |
| 340 | else if (block_type_is(buf, "EOF ")) |
| 341 | { |
| 342 | /* end of file - we can stop looking now */ |
| 343 | done = TRUE; |
| 344 | } |
| 345 | else |
| 346 | { |
| 347 | /* |
| 348 | * This block type is unknown, so ignore it. If a new block |
| 349 | * type is added in the future, an older VM version won't |
| 350 | * recognize the new block, but it can still load the image |
| 351 | * file simply by omitting any unrecognized new blocks. Since |
| 352 | * the image file will not be complete in this case, it may not |
| 353 | * work properly. |
| 354 | * |
| 355 | * To allow for future block types which contain advisory data, |
| 356 | * which can safely be ignored by older VM versions, while also |
| 357 | * allowing for the possibility of changes that create |
| 358 | * incompatibilities, we have a flag in the header that |
| 359 | * indicates whether the block is required or not. If this |
| 360 | * block is marked as mandatory, throw an error, since we don't |
| 361 | * recognize the block. Note that this is a version-related |
| 362 | * error (i.e., a VM update should fix it), so set the version |
| 363 | * flag in the exception. |
| 364 | */ |
| 365 | if ((flags & VMIMAGE_DBF_MANDATORY) != 0) |
| 366 | err_throw_a(VMERR_UNKNOWN_IMAGE_BLOCK, 1, |
| 367 | ERR_TYPE_VERSION_FLAG); |
| 368 | |
| 369 | /* skip past the block */ |
| 370 | fp_->skip_ahead(siz); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /* the image file is required to contain an entrypoint definition */ |
| 375 | if (!loaded_entrypt_) |
| 376 | err_throw(VMERR_IMAGE_NO_ENTRYPT); |
| 377 | |
| 378 | /* the image file is required to contain a metaclass dependency table */ |
| 379 | if (!loaded_meta_dep_) |
| 380 | err_throw(VMERR_IMAGE_NO_METADEP); |
| 381 | |
| 382 | /* the image is required to have a function set dependency table */ |
| 383 | if (!loaded_funcset_dep_) |
| 384 | err_throw(VMERR_IMAGE_NO_FUNCDEP); |
| 385 | |
| 386 | /* complete the dynamic linking */ |
| 387 | do_dynamic_link(vmg0_); |
| 388 | |
| 389 | /* fix up the global symbol table metaclasses, if applicable */ |
| 390 | fix_gsym_meta(vmg0_); |
| 391 | |
| 392 | /* |
| 393 | * create an IntrinsicClass instance for each metaclass that the |
| 394 | * image file is using and for which the compiler didn't supply its |
| 395 | * own IntrinsicClass object |
| 396 | */ |
| 397 | G_meta_table->create_intrinsic_class_instances(vmg0_); |
| 398 | |
| 399 | /* |
| 400 | * Cache certain metaclass method table entries that are used by the |
| 401 | * VM: Iterator.getNext [Iterator#1], Iterator.isNextAvailable |
| 402 | * [Iterator#2]. |
| 403 | */ |
| 404 | vm_meta_entry_t *mcoll = G_meta_table->get_entry_by_id("iterator"); |
| 405 | if (mcoll != 0) |
| 406 | { |
| 407 | G_iter_get_next = mcoll->xlat_func(1); |
| 408 | G_iter_next_avail = mcoll->xlat_func(2); |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Attach the code pool and constant pool to their backing stores, |
| 413 | * which are the pool objects we loaded from the image file. |
| 414 | */ |
| 415 | G_code_pool->attach_backing_store(pools_[0]); |
| 416 | G_const_pool->attach_backing_store(pools_[1]); |
| 417 | |
| 418 | /* perform any requested post-load object initializations */ |
| 419 | G_obj_table->do_all_post_load_init(vmg0_); |
| 420 | |
| 421 | /* load external resource files if possible */ |
| 422 | if (G_host_ifc->can_add_resfiles()) |
| 423 | load_ext_resfiles(vmg0_); |
| 424 | |
| 425 | /* |
| 426 | * perform additional initialization now that we've finished |
| 427 | * loading the image file |
| 428 | */ |
| 429 | vm_init_after_load(vmg0_); |
| 430 | |
| 431 | /* |
| 432 | * Let the UI inspect the built-ins linked by the program. The UI |
| 433 | * might use different initial display configurations depending on the |
| 434 | * function sets and/or intrinsic classes the program uses. For |
| 435 | * example, Workbench on Windows initially hides the HTML TADS window |
| 436 | * if the network functions are linked, since these are normally used |
| 437 | * to implement a Web UI, which uses a separate display window. |
| 438 | */ |
| 439 | os_init_ui_after_load(G_bif_table, G_meta_table); |
| 440 | |
| 441 | /* forget the image loader */ |
| 442 | G_image_loader = 0; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | /* ------------------------------------------------------------------------ */ |
| 447 | /* |
| 448 | * Load external resource files associated with the image file |
| 449 | */ |
| 450 | void CVmImageLoader::load_ext_resfiles(VMG0_) |
| 451 | { |
| 452 | int i; |
| 453 | char suffix_lc[4]; |
| 454 | char suffix_uc[4]; |
| 455 | |
| 456 | /* set up the templates for the resource file suffix */ |
| 457 | strcpy(suffix_lc, "3r0"); |
| 458 | strcpy(suffix_uc, "3R0"); |
| 459 | |
| 460 | /* |
| 461 | * Search for resource files with the same name as the image file, but |
| 462 | * with the extension replaced with .3r0 through .3r9. Try both |
| 463 | * lower-case and upper-case names (in that order), in case the file |
| 464 | * system is case-sensitive. |
| 465 | */ |
| 466 | for (i = 0 ; i < 10 ; ++i) |
| 467 | { |
| 468 | char resfile[OSFNMAX]; |
| 469 | |
| 470 | /* substitute the current suffix number */ |
| 471 | suffix_lc[2] = suffix_uc[2] = i + '0'; |
| 472 | |
| 473 | /* |
| 474 | * if there's an explicit resource path, use it, otherwise use |
| 475 | * the same directory that contains the image file |
| 476 | */ |
| 477 | if (G_host_ifc->get_res_path() != 0) |
| 478 | { |
| 479 | /* |
| 480 | * there's an explicit resource path - build the full path to |
| 481 | * the resource file using the resource path and the root name |
| 482 | * of the image file |
| 483 | */ |
| 484 | os_build_full_path(resfile, sizeof(resfile), |
| 485 | G_host_ifc->get_res_path(), |
| 486 | os_get_root_name(fname_)); |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | /* |
| 491 | * there's no resoruce path - use the image file full name, |
| 492 | * including any directory path information |
| 493 | */ |
| 494 | strcpy(resfile, fname_); |
| 495 | } |
| 496 | |
| 497 | /* replace the old image file extension with the resource suffix */ |
| 498 | os_remext(resfile); |
| 499 | os_addext(resfile, suffix_lc); |
| 500 | |
| 501 | /* if this file doesn't exist, try the upper-case name */ |
| 502 | if (osfacc(resfile)) |
| 503 | { |
| 504 | /* replace the suffix with the upper-case version */ |
| 505 | os_remext(resfile); |
| 506 | os_addext(resfile, suffix_uc); |
| 507 | } |
| 508 | |
| 509 | /* check to see if this file exists */ |
| 510 | if (!osfacc(resfile)) |
| 511 | { |
| 512 | int fileno; |
| 513 | CVmFile *fp; |
| 514 | CVmImageFile *volatile imagefp = 0; |
| 515 | CVmImageLoader *volatile loader = 0; |
| 516 | |
| 517 | /* ask the host system to assign a file number */ |
| 518 | fileno = G_host_ifc->add_resfile(resfile); |
| 519 | |
| 520 | /* create a file object for reading the file */ |
| 521 | fp = new CVmFile(); |
| 522 | |
| 523 | err_try |
| 524 | { |
| 525 | CVmImageLoaderMres_std res_ifc(fileno, G_host_ifc); |
| 526 | |
| 527 | /* open the file */ |
| 528 | fp->open_read(resfile, OSFTT3IMG); |
| 529 | |
| 530 | /* set up the loader */ |
| 531 | imagefp = new CVmImageFileExt(fp); |
| 532 | loader = new CVmImageLoader(imagefp, resfile, 0); |
| 533 | |
| 534 | /* load the resource-only file */ |
| 535 | loader->load_resource_file(&res_ifc); |
| 536 | } |
| 537 | err_finally |
| 538 | { |
| 539 | /* delete the objects we created */ |
| 540 | if (loader != 0) |
| 541 | delete loader; |
| 542 | if (imagefp != 0) |
| 543 | delete imagefp; |
| 544 | delete fp; |
| 545 | } |
| 546 | err_end; |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * Load a resource file from the current seek location in the given file |
| 553 | * handle |
| 554 | */ |
| 555 | void CVmImageLoader::load_resources_from_fp(osfildef *fp, |
| 556 | const char *fname, |
| 557 | CVmHostIfc *hostifc) |
| 558 | { |
| 559 | int fileno; |
| 560 | |
| 561 | /* ask the host system to assign a number to the file */ |
| 562 | fileno = hostifc->add_resfile(fname); |
| 563 | |
| 564 | /* set up a resource interface with the file number */ |
| 565 | CVmImageLoaderMres_std res_ifc(fileno, hostifc); |
| 566 | |
| 567 | err_try |
| 568 | { |
| 569 | /* load the resources */ |
| 570 | load_resources_from_fp(fp, fname, &res_ifc); |
| 571 | } |
| 572 | err_catch_disc |
| 573 | { |
| 574 | /* ignore the error */ |
| 575 | } |
| 576 | err_end; |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * Load a resource file from the current seek location in the given file |
| 581 | * handle |
| 582 | */ |
| 583 | void CVmImageLoader::load_resources_from_fp(osfildef *fp, |
| 584 | const char *fname, |
| 585 | CVmImageLoaderMres *res_ifc) |
| 586 | { |
| 587 | CVmFile *file; |
| 588 | CVmImageFile *volatile imagefp = 0; |
| 589 | CVmImageLoader *volatile loader = 0; |
| 590 | |
| 591 | /* create a file object for reading the file */ |
| 592 | file = new CVmFile(); |
| 593 | |
| 594 | /* set up the file with our file handler */ |
| 595 | file->set_file(fp, 0); |
| 596 | |
| 597 | err_try |
| 598 | { |
| 599 | /* set up the loader */ |
| 600 | imagefp = new CVmImageFileExt(file); |
| 601 | loader = new CVmImageLoader(imagefp, fname, 0); |
| 602 | |
| 603 | /* load the resource-only file */ |
| 604 | loader->load_resource_file(res_ifc); |
| 605 | } |
| 606 | err_finally |
| 607 | { |
| 608 | /* |
| 609 | * detach our CVmFile object from the caller's file handle, |
| 610 | * since we want to leave the caller's file handle open |
| 611 | */ |
| 612 | file->detach_file(); |
| 613 | |
| 614 | /* delete the objects we created */ |
| 615 | if (loader != 0) |
| 616 | delete loader; |
| 617 | if (imagefp != 0) |
| 618 | delete imagefp; |
| 619 | delete file; |
| 620 | } |
| 621 | err_end; |
| 622 | } |
| 623 | |
| 624 | |
| 625 | /* ------------------------------------------------------------------------ */ |
| 626 | /* |
| 627 | * Load a resource-only file. 'fileno' is the file number assigned by |
| 628 | * the host application (via the add_resfile() interface). |
| 629 | */ |
| 630 | void CVmImageLoader::load_resource_file(CVmImageLoaderMres *res_ifc) |
| 631 | { |
| 632 | int done; |
| 633 | |
| 634 | /* read and validate the image header */ |
| 635 | read_image_header(); |
| 636 | |
| 637 | /* load the blocks */ |
| 638 | for (done = FALSE ; !done ; ) |
| 639 | { |
| 640 | ulong siz; |
| 641 | uint flags; |
| 642 | char buf[128]; |
| 643 | |
| 644 | /* read the next data block header */ |
| 645 | fp_->copy_data(buf, 10); |
| 646 | |
| 647 | /* get the size */ |
| 648 | siz = t3rp4u(buf + 4); |
| 649 | |
| 650 | /* get the flags */ |
| 651 | flags = osrp2(buf + 8); |
| 652 | |
| 653 | /* check the block type */ |
| 654 | if (block_type_is(buf, "EOF ")) |
| 655 | { |
| 656 | /* we're done */ |
| 657 | done = TRUE; |
| 658 | } |
| 659 | else if (block_type_is(buf, "MRES")) |
| 660 | { |
| 661 | /* load the multimedia resource block */ |
| 662 | load_mres(siz, res_ifc); |
| 663 | } |
| 664 | else if (block_type_is(buf, "MREL")) |
| 665 | { |
| 666 | /* load the multimedia resource link block */ |
| 667 | load_mres_link(siz, res_ifc); |
| 668 | } |
| 669 | else |
| 670 | { |
| 671 | /* |
| 672 | * Unknown block type - ignore it, unless it's mandatory, in |
| 673 | * which case this is an error. This is a version-related |
| 674 | * error (fixable by updating to the latest version), so set |
| 675 | * the version flag in the exception, if we throw one. |
| 676 | */ |
| 677 | if ((flags & VMIMAGE_DBF_MANDATORY) != 0) |
| 678 | err_throw_a(VMERR_UNKNOWN_IMAGE_BLOCK, 1, |
| 679 | ERR_TYPE_VERSION_FLAG); |
| 680 | |
| 681 | /* skip past the block */ |
| 682 | fp_->skip_ahead(siz); |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /* ------------------------------------------------------------------------ */ |
| 688 | /* |
| 689 | * Read and validate an image file header. |
| 690 | */ |
| 691 | void CVmImageLoader::read_image_header() |
| 692 | { |
| 693 | char buf[128]; |
| 694 | |
| 695 | /* |
| 696 | * Read the header. The header consists of the signature string, a |
| 697 | * UINT2 with the file format version number, 32 reserved bytes, |
| 698 | * then the compilation timestamp (24 bytes). |
| 699 | */ |
| 700 | fp_->copy_data(buf, sizeof(VMIMAGE_SIG)-1 + 2 + 32 + 24); |
| 701 | |
| 702 | /* verify the signature */ |
| 703 | if (memcmp(buf, VMIMAGE_SIG, sizeof(VMIMAGE_SIG)-1) != 0) |
| 704 | err_throw(VMERR_NOT_AN_IMAGE_FILE); |
| 705 | |
| 706 | /* get the version number */ |
| 707 | ver_ = osrp2(buf + sizeof(VMIMAGE_SIG)-1); |
| 708 | |
| 709 | /* store the timestamp */ |
| 710 | memcpy(timestamp_, buf + sizeof(VMIMAGE_SIG)-1 + 2 + 32, 24); |
| 711 | |
| 712 | /* |
| 713 | * Check the version to ensure that it's within the range that this |
| 714 | * loader implementation supports. If not, throw an error, and mark is |
| 715 | * as a version-related error. |
| 716 | */ |
| 717 | if (ver_ > 1) |
| 718 | err_throw_a(VMERR_IMAGE_INCOMPAT_VSN, 1, ERR_TYPE_VERSION_FLAG); |
| 719 | } |
| 720 | |
| 721 | /* ------------------------------------------------------------------------ */ |
| 722 | /* |
| 723 | * Execute the image |
| 724 | */ |
| 725 | void CVmImageLoader::run(VMG_ const char *const *argv, int argc, |
| 726 | CVmRuntimeSymbols *global_symtab, |
| 727 | CVmRuntimeSymbols *macro_symtab, |
| 728 | const char *saved_state) |
| 729 | { |
| 730 | CVmRuntimeSymbols *orig_runtime_symtab, *orig_runtime_macros; |
| 731 | pool_ofs_t entry_code_ofs; |
| 732 | int entry_code_argc; |
| 733 | CCharmapToUni *argmap = 0; |
| 734 | char *argstr = 0; |
| 735 | |
| 736 | /* make sure we found a code pool definition */ |
| 737 | if (pools_[0]->vmpbs_get_page_count() < 1) |
| 738 | err_throw(VMERR_IMAGE_NO_CODE); |
| 739 | |
| 740 | /* |
| 741 | * Find the entrypoint. If we have a saved state, call the function |
| 742 | * given by the exported symbol "mainRestore". Otherwise, call the |
| 743 | * exported main entrypoint function. |
| 744 | */ |
| 745 | if (saved_state != 0) |
| 746 | { |
| 747 | /* |
| 748 | * We're restoring a saved state file immediately on startup - call |
| 749 | * the exported "mainRestore" function. If there is no such |
| 750 | * export, we can't run the program with an initial saved state. |
| 751 | */ |
| 752 | if (G_predef->main_restore_func == 0) |
| 753 | err_throw(VMERR_NO_MAINRESTORE); |
| 754 | |
| 755 | /* use the mainRestore export */ |
| 756 | entry_code_ofs = G_predef->main_restore_func; |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | /* ordinary startup - use the exported primary entrypoint */ |
| 761 | entry_code_ofs = entrypt_; |
| 762 | } |
| 763 | |
| 764 | /* we have no arguments to the entrypoint function yet */ |
| 765 | entry_code_argc = 0; |
| 766 | |
| 767 | /* set myself as the global image loader */ |
| 768 | G_image_loader = this; |
| 769 | |
| 770 | /* remember the original runtime symbol table */ |
| 771 | orig_runtime_symtab = runtime_symtab_; |
| 772 | orig_runtime_macros = runtime_macros_; |
| 773 | |
| 774 | /* we haven't set the file path */ |
| 775 | int file_path_set = FALSE; |
| 776 | |
| 777 | /* catch any errors so we restore globals on the way out */ |
| 778 | err_try |
| 779 | { |
| 780 | int i; |
| 781 | |
| 782 | /* if there's no file path, use the image file folder by default */ |
| 783 | if (G_file_path == 0) |
| 784 | { |
| 785 | G_file_path = lib_copy_str(get_path()); |
| 786 | file_path_set = TRUE; |
| 787 | } |
| 788 | |
| 789 | /* |
| 790 | * if the caller gave us a runtime symbol table, use it over any |
| 791 | * we have already stored |
| 792 | */ |
| 793 | if (global_symtab != 0) |
| 794 | runtime_symtab_ = global_symtab; |
| 795 | if (macro_symtab != 0) |
| 796 | runtime_macros_ = macro_symtab; |
| 797 | |
| 798 | /* create a LookupTable for the reflection symbols, if any */ |
| 799 | create_global_symtab_lookup_table(vmg0_); |
| 800 | |
| 801 | /* |
| 802 | * if we successfully created a reflection symbol table, push it |
| 803 | * onto the stack - this will ensure that the object won't be |
| 804 | * discarded as long as the program is running |
| 805 | */ |
| 806 | if (reflection_symtab_ != VM_INVALID_OBJ) |
| 807 | { |
| 808 | /* set up an object value for the table and push it */ |
| 809 | G_stk->push()->set_obj(reflection_symtab_); |
| 810 | } |
| 811 | |
| 812 | /* create and stack the reflection LookupTable for macros */ |
| 813 | create_macro_symtab_lookup_table(vmg0_); |
| 814 | if (reflection_macros_ != VM_INVALID_OBJ) |
| 815 | G_stk->push()->set_obj(reflection_macros_); |
| 816 | |
| 817 | /* |
| 818 | * run static initializers (do this after creating the symbol |
| 819 | * table, in case any of the initializers want to access the |
| 820 | * symbol table) |
| 821 | */ |
| 822 | run_static_init(vmg0_); |
| 823 | |
| 824 | /* if there's a saved state file to restore, push it */ |
| 825 | if (saved_state != 0) |
| 826 | { |
| 827 | /* create a string object for the filename, and push it */ |
| 828 | G_stk->push()->set_obj( |
| 829 | CVmObjString::create(vmg_ FALSE, |
| 830 | saved_state, strlen(saved_state))); |
| 831 | |
| 832 | /* count the extra startup argument */ |
| 833 | ++entry_code_argc; |
| 834 | } |
| 835 | |
| 836 | /* get a character mapper for the command-line arguments */ |
| 837 | if (argc > 0) |
| 838 | { |
| 839 | /* |
| 840 | * If we have a net configuration, assume that the arguments |
| 841 | * are in UTF-8 format, since they're coming from a web server. |
| 842 | * Otherwise, ask the OS which character set to use for |
| 843 | * command-line parameters. |
| 844 | */ |
| 845 | char argcs[40]; |
| 846 | if (G_net_config != 0) |
| 847 | strcpy(argcs, "utf8"); |
| 848 | else |
| 849 | os_get_charmap(argcs, OS_CHARMAP_CMDLINE); |
| 850 | |
| 851 | /* load the character set */ |
| 852 | argmap = CCharmapToUni::load( |
| 853 | G_host_ifc->get_sys_res_loader(), argcs); |
| 854 | } |
| 855 | |
| 856 | /* |
| 857 | * push a string object for each argument - push them onto the |
| 858 | * stack to ensure that they're referenced in case garbage |
| 859 | * collection occurs while we're working |
| 860 | */ |
| 861 | for (i = 0 ; i < argc ; ++i) |
| 862 | { |
| 863 | /* if we have a character mapper, map the argument string */ |
| 864 | size_t slen; |
| 865 | if (argmap != 0) |
| 866 | { |
| 867 | /* map the argument to UTF8 */ |
| 868 | slen = argmap->map_str_alo(&argstr, argv[i]); |
| 869 | } |
| 870 | else |
| 871 | { |
| 872 | /* make a copy of the string */ |
| 873 | slen = strlen(argv[i]); |
| 874 | argstr = (char *)t3malloc(slen + 1); |
| 875 | memcpy(argstr, argv[i], slen + 1); |
| 876 | |
| 877 | /* make sure it's well-formed UTF-8 */ |
| 878 | CCharmapToUni::validate(argstr, slen); |
| 879 | } |
| 880 | |
| 881 | /* push the mapped/adjusted string argument */ |
| 882 | G_interpreter->push_string(vmg_ argstr, slen); |
| 883 | |
| 884 | /* done with the mapped/adjusted string */ |
| 885 | t3free(argstr); |
| 886 | argstr = 0; |
| 887 | } |
| 888 | |
| 889 | /* create a list to hold the strings */ |
| 890 | vm_obj_id_t lst_obj = CVmObjList::create(vmg_ FALSE, argc); |
| 891 | CVmObjList *lst = (CVmObjList *)vm_objp(vmg_ lst_obj); |
| 892 | |
| 893 | /* set the list elements to the strings */ |
| 894 | for (i = 0 ; i < argc ; ++i) |
| 895 | { |
| 896 | vm_val_t *strp; |
| 897 | |
| 898 | /* |
| 899 | * Get this item from the stack. Note that the most recently |
| 900 | * pushed item is number 0, then number 1, and so on - the |
| 901 | * last argument string (at index argc) is thus number 0, and |
| 902 | * the first (at index 0) is number argc. |
| 903 | */ |
| 904 | strp = G_stk->get(argc - i - 1); |
| 905 | |
| 906 | /* set this list element */ |
| 907 | lst->cons_set_element(i, strp); |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * we don't need the strings themselves any more, as we can reach |
| 912 | * them through the list, so drop the strings from the stack |
| 913 | */ |
| 914 | G_stk->discard(argc); |
| 915 | |
| 916 | /* push the list - it's the argument to the program entrypoint */ |
| 917 | G_stk->push()->set_obj(lst_obj); |
| 918 | ++entry_code_argc; |
| 919 | |
| 920 | /* |
| 921 | * Invoke the entrypoint function - it takes two arguments (the |
| 922 | * list of argument strings, and the name of the initial saved |
| 923 | * state file to restore, if any). |
| 924 | */ |
| 925 | G_stk->push()->set_propid(VM_INVALID_PROP); |
| 926 | G_stk->push()->set_nil(); |
| 927 | G_stk->push()->set_nil(); |
| 928 | G_stk->push()->set_nil_obj(); |
| 929 | G_stk->push()->set_fnptr(entry_code_ofs); |
| 930 | vm_rcdesc rc("Main entrypoint"); |
| 931 | G_interpreter->do_call( |
| 932 | vmg_ 0, |
| 933 | (const uchar *)G_code_pool->get_ptr(entry_code_ofs), |
| 934 | entry_code_argc, &rc); |
| 935 | |
| 936 | /* |
| 937 | * if we pushed a global symbol table LookupTable object, pop it - |
| 938 | * we left it on the stack to protect it from the garbage |
| 939 | * collector, since it's always implicitly referenced from the |
| 940 | * intrinsic function that retrieves the symbol table object |
| 941 | */ |
| 942 | if (reflection_macros_ != VM_INVALID_OBJ) |
| 943 | G_stk->discard(); |
| 944 | if (reflection_symtab_ != VM_INVALID_OBJ) |
| 945 | G_stk->discard(); |
| 946 | } |
| 947 | err_finally |
| 948 | { |
| 949 | /* forget the image loader */ |
| 950 | G_image_loader = 0; |
| 951 | |
| 952 | /* restore the original runtime symbol tables */ |
| 953 | runtime_symtab_ = orig_runtime_symtab; |
| 954 | runtime_macros_ = orig_runtime_macros; |
| 955 | |
| 956 | /* release the command-line character mapper */ |
| 957 | if (argmap != 0) |
| 958 | argmap->release_ref(); |
| 959 | |
| 960 | /* release any argument string we were working on */ |
| 961 | if (argstr != 0) |
| 962 | t3free(argstr); |
| 963 | |
| 964 | /* |
| 965 | * if we set the file path, un-set it, so that callers don't think |
| 966 | * they have to do so |
| 967 | */ |
| 968 | if (file_path_set) |
| 969 | { |
| 970 | lib_free_str(G_file_path); |
| 971 | G_file_path = 0; |
| 972 | } |
| 973 | } |
| 974 | err_end; |
| 975 | } |
| 976 | |
| 977 | /* |
| 978 | * Run static initializers |
| 979 | */ |
| 980 | void CVmImageLoader::run_static_init(VMG0_) |
| 981 | { |
| 982 | CVmStaticInitPage *pg; |
| 983 | |
| 984 | /* run through the list of static initializers */ |
| 985 | for (pg = static_head_ ; pg != 0 ; pg = pg->nxt_) |
| 986 | { |
| 987 | size_t i; |
| 988 | |
| 989 | /* run through the initializers on this page */ |
| 990 | for (i = 0 ; i < pg->cnt_ ; ++i) |
| 991 | { |
| 992 | vm_obj_id_t obj; |
| 993 | vm_prop_id_t prop; |
| 994 | vm_val_t target; |
| 995 | |
| 996 | /* get this initializer's object and property ID */ |
| 997 | obj = pg->get_obj_id(i); |
| 998 | prop = pg->get_prop_id(i); |
| 999 | target.set_obj(obj); |
| 1000 | |
| 1001 | /* invoke this object.property */ |
| 1002 | err_try |
| 1003 | { |
| 1004 | /* evaluate the property */ |
| 1005 | G_interpreter->get_prop(vmg_ 0, &target, prop, &target, 0, 0); |
| 1006 | } |
| 1007 | err_catch(exc) |
| 1008 | { |
| 1009 | /* |
| 1010 | * If the "error" is a debugger terminate, pass it through |
| 1011 | * - this simply means that the user is manually ending the |
| 1012 | * program via the debugger UI. Likewise for restarts or |
| 1013 | * interrupts. |
| 1014 | */ |
| 1015 | if (exc->get_error_code() == VMERR_DBG_HALT |
| 1016 | || exc->get_error_code() == VMERR_DBG_RESTART |
| 1017 | || exc->get_error_code() == VMERR_DBG_INTERRUPT) |
| 1018 | err_rethrow(); |
| 1019 | |
| 1020 | /* get the message for the exception that occurred */ |
| 1021 | char errbuf[512]; |
| 1022 | CVmRun::get_exc_message(vmg_ exc, errbuf, sizeof(errbuf), |
| 1023 | FALSE); |
| 1024 | |
| 1025 | /* presume we won't find names */ |
| 1026 | size_t obj_len = 0, prop_len = 0; |
| 1027 | const char *obj_name = 0, *prop_name = 0; |
| 1028 | |
| 1029 | /* find the obj and prop names in the global symbols */ |
| 1030 | if (runtime_symtab_ != 0) |
| 1031 | { |
| 1032 | /* find the object name */ |
| 1033 | obj_name = runtime_symtab_ |
| 1034 | ->find_obj_name(vmg_ obj, &obj_len); |
| 1035 | |
| 1036 | /* find the property name */ |
| 1037 | prop_name = runtime_symtab_ |
| 1038 | ->find_prop_name(vmg_ prop, &prop_len); |
| 1039 | } |
| 1040 | |
| 1041 | /* if we didn't find the names, use placeholders */ |
| 1042 | if (obj_name == 0) |
| 1043 | { |
| 1044 | obj_name = "[object name not available]"; |
| 1045 | obj_len = strlen(obj_name); |
| 1046 | } |
| 1047 | if (prop_name == 0) |
| 1048 | { |
| 1049 | prop_name = "[property name not available]"; |
| 1050 | prop_len = strlen(prop_name); |
| 1051 | } |
| 1052 | |
| 1053 | /* throw a new error for the static initializer failure */ |
| 1054 | err_throw_a(VMERR_EXC_IN_STATIC_INIT, 3, |
| 1055 | ERR_TYPE_TEXTCHAR_LEN, obj_name, obj_len, |
| 1056 | ERR_TYPE_TEXTCHAR_LEN, prop_name, prop_len, |
| 1057 | ERR_TYPE_CHAR, errbuf); |
| 1058 | } |
| 1059 | err_end; |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | /* |
| 1065 | * Unload the image. This detaches the pools from the backing stores, |
| 1066 | * which must be done before the loaded copy of the image file can be |
| 1067 | * deleted. |
| 1068 | */ |
| 1069 | void CVmImageLoader::unload(VMG0_) |
| 1070 | { |
| 1071 | /* detach the code pool from its backing store */ |
| 1072 | G_code_pool->detach_backing_store(); |
| 1073 | |
| 1074 | /* detach the constant pool from its backing store */ |
| 1075 | G_const_pool->detach_backing_store(); |
| 1076 | } |
| 1077 | |
| 1078 | /* ------------------------------------------------------------------------ */ |
| 1079 | /* |
| 1080 | * Create a LookupTable to hold the symbols in the global symbol table. |
| 1081 | */ |
| 1082 | void CVmImageLoader::create_global_symtab_lookup_table(VMG0_) |
| 1083 | { |
| 1084 | CVmObjLookupTable *lookup; |
| 1085 | vm_runtime_sym *sym; |
| 1086 | |
| 1087 | /* |
| 1088 | * if we don't have a runtime symbol table, we can't create the |
| 1089 | * reflection LookupTable |
| 1090 | */ |
| 1091 | if (runtime_symtab_ == 0) |
| 1092 | return; |
| 1093 | |
| 1094 | /* |
| 1095 | * if we already have a reflection symbol table, there's no need to |
| 1096 | * create another |
| 1097 | */ |
| 1098 | if (reflection_symtab_ != VM_INVALID_OBJ) |
| 1099 | return; |
| 1100 | |
| 1101 | /* create a LookupTable to hold the symbols */ |
| 1102 | reflection_symtab_ = CVmObjLookupTable:: |
| 1103 | create(vmg_ FALSE, 256, runtime_symtab_->get_sym_count()); |
| 1104 | |
| 1105 | /* get the object, properly cast */ |
| 1106 | lookup = (CVmObjLookupTable *)vm_objp(vmg_ reflection_symtab_); |
| 1107 | |
| 1108 | /* push the lookup table onto the stack for gc protection */ |
| 1109 | G_stk->push()->set_obj(reflection_symtab_); |
| 1110 | |
| 1111 | /* run through the symbols and populate the LookupTable */ |
| 1112 | for (sym = runtime_symtab_->get_head() ; sym != 0 ; sym = sym->nxt) |
| 1113 | { |
| 1114 | vm_val_t str; |
| 1115 | |
| 1116 | /* |
| 1117 | * skip intrinsic class modifier objects (for the same reason we |
| 1118 | * filter these out of firstObj/nextObj iterations: they're not |
| 1119 | * meaningful objects as far as the program is concerned, so |
| 1120 | * there's no reason for the program to see them) |
| 1121 | */ |
| 1122 | if (sym->val.typ == VM_OBJ |
| 1123 | && CVmObjIntClsMod::is_intcls_mod_obj(vmg_ sym->val.val.obj)) |
| 1124 | { |
| 1125 | /* it's an intrinsic class modifier - skip it */ |
| 1126 | continue; |
| 1127 | } |
| 1128 | |
| 1129 | /* create a string object to hold this symbol */ |
| 1130 | str.set_obj(CVmObjString::create(vmg_ FALSE, sym->sym, sym->len)); |
| 1131 | |
| 1132 | /* |
| 1133 | * add it to the lookup table - the symbol string is the key, and |
| 1134 | * the symbol's value is the lookup entry value |
| 1135 | */ |
| 1136 | lookup->add_entry(vmg_ &str, &sym->val); |
| 1137 | } |
| 1138 | |
| 1139 | /* done working - discard our gc protection */ |
| 1140 | G_stk->discard(); |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * Create a LookupTable to hold the symbols in the macro table |
| 1145 | */ |
| 1146 | void CVmImageLoader::create_macro_symtab_lookup_table(VMG0_) |
| 1147 | { |
| 1148 | CVmObjLookupTable *lookup; |
| 1149 | vm_runtime_sym *sym; |
| 1150 | |
| 1151 | /* |
| 1152 | * if we don't have a runtime macro table, we can't create the |
| 1153 | * reflection LookupTable |
| 1154 | */ |
| 1155 | if (runtime_macros_ == 0) |
| 1156 | return; |
| 1157 | |
| 1158 | /* if we already created the table, there's no need to create another */ |
| 1159 | if (reflection_macros_ != VM_INVALID_OBJ) |
| 1160 | return; |
| 1161 | |
| 1162 | /* create a LookupTable to hold the symbols */ |
| 1163 | reflection_macros_ = CVmObjLookupTable:: |
| 1164 | create(vmg_ FALSE, 256, runtime_macros_->get_sym_count()); |
| 1165 | |
| 1166 | /* get the object, properly cast */ |
| 1167 | lookup = (CVmObjLookupTable *)vm_objp(vmg_ reflection_macros_); |
| 1168 | |
| 1169 | /* push the lookup table onto the stack for gc protection */ |
| 1170 | G_stk->push()->set_obj(reflection_macros_); |
| 1171 | |
| 1172 | /* run through the symbols and populate the LookupTable */ |
| 1173 | for (sym = runtime_macros_->get_head() ; sym != 0 ; sym = sym->nxt) |
| 1174 | { |
| 1175 | vm_val_t str; |
| 1176 | vm_val_t lst; |
| 1177 | vm_val_t args; |
| 1178 | vm_val_t v; |
| 1179 | int i; |
| 1180 | |
| 1181 | /* create a string object to hold this symbol */ |
| 1182 | str.set_obj(CVmObjString::create(vmg_ FALSE, sym->sym, sym->len)); |
| 1183 | |
| 1184 | /* stack it for gc protection */ |
| 1185 | G_stk->push(&str); |
| 1186 | |
| 1187 | /* |
| 1188 | * Create a list to hold the definition. We represent this as |
| 1189 | * [expansion_string, [argument_list], flags]. |
| 1190 | */ |
| 1191 | lst.set_obj(CVmObjList::create(vmg_ FALSE, 3)); |
| 1192 | G_stk->push(&lst); |
| 1193 | CVmObjList *lstp = (CVmObjList *)vm_objp(vmg_ lst.val.obj); |
| 1194 | lstp->cons_clear(); |
| 1195 | |
| 1196 | /* create and store the expansion string */ |
| 1197 | v.set_obj(CVmObjString::create( |
| 1198 | vmg_ FALSE, sym->macro_expansion, sym->macro_exp_len)); |
| 1199 | lstp->cons_set_element(0, &v); |
| 1200 | |
| 1201 | /* create the argument list */ |
| 1202 | args.set_obj(CVmObjList::create(vmg_ FALSE, sym->macro_argc)); |
| 1203 | CVmObjList *argp = (CVmObjList *)vm_objp(vmg_ args.val.obj); |
| 1204 | argp->cons_clear(); |
| 1205 | |
| 1206 | /* store the argument list in the definition list */ |
| 1207 | lstp->cons_set_element(1, &args); |
| 1208 | |
| 1209 | /* store the arguments */ |
| 1210 | for (i = 0 ; i < sym->macro_argc ; ++i) |
| 1211 | { |
| 1212 | /* create the string */ |
| 1213 | char *p = sym->macro_args[i]; |
| 1214 | size_t len = strlen(p); |
| 1215 | v.set_obj(CVmObjString::create(vmg_ FALSE, p, len)); |
| 1216 | |
| 1217 | /* store it in the argument list */ |
| 1218 | argp->cons_set_element(i, &v); |
| 1219 | } |
| 1220 | |
| 1221 | /* store the flags */ |
| 1222 | v.set_int(sym->macro_flags); |
| 1223 | lstp->cons_set_element(2, &v); |
| 1224 | |
| 1225 | /* |
| 1226 | * add the macro to the lookup table - the symbol string is the |
| 1227 | * key, and the symbol's value is the definition list |
| 1228 | */ |
| 1229 | lookup->add_entry(vmg_ &str, &lst); |
| 1230 | |
| 1231 | /* done with the symbol and list gc protection */ |
| 1232 | G_stk->discard(2); |
| 1233 | } |
| 1234 | |
| 1235 | /* done working - discard our gc protection */ |
| 1236 | G_stk->discard(); |
| 1237 | } |
| 1238 | |
| 1239 | |
| 1240 | /* ------------------------------------------------------------------------ */ |
| 1241 | /* |
| 1242 | * Load an Entrypoint (ENTP) block |
| 1243 | */ |
| 1244 | void CVmImageLoader::load_entrypt(VMG_ ulong siz) |
| 1245 | { |
| 1246 | char buf[32]; |
| 1247 | |
| 1248 | /* if we've already loaded an entrypoint, throw an error */ |
| 1249 | if (loaded_entrypt_) |
| 1250 | err_throw(VMERR_IMAGE_ENTRYPT_REDEF); |
| 1251 | |
| 1252 | /* read the entrypoint offset */ |
| 1253 | read_data(buf, 16, &siz); |
| 1254 | |
| 1255 | /* set the entrypoint */ |
| 1256 | entrypt_ = t3rp4u(buf); |
| 1257 | |
| 1258 | /* set the method header size in the interpreter */ |
| 1259 | G_interpreter->set_funchdr_size(osrp2(buf+4)); |
| 1260 | |
| 1261 | /* set the exception table entry size global */ |
| 1262 | G_exc_entry_size = osrp2(buf+6); |
| 1263 | |
| 1264 | /* set the debugger source line record size global */ |
| 1265 | G_line_entry_size = osrp2(buf+8); |
| 1266 | |
| 1267 | /* set the debug table header size */ |
| 1268 | G_dbg_hdr_size = osrp2(buf+10); |
| 1269 | |
| 1270 | /* set the debug local symbol header size */ |
| 1271 | G_dbg_lclsym_hdr_size = osrp2(buf+12); |
| 1272 | |
| 1273 | /* set the debug version ID */ |
| 1274 | G_dbg_fmt_vsn = osrp2(buf+14); |
| 1275 | |
| 1276 | /* set the frame size */ |
| 1277 | G_dbg_frame_size = 4; |
| 1278 | if (siz >= 2) |
| 1279 | { |
| 1280 | read_data(buf, 2, &siz); |
| 1281 | G_dbg_frame_size = osrp2(buf); |
| 1282 | } |
| 1283 | |
| 1284 | /* note that we've loaded it */ |
| 1285 | loaded_entrypt_ = TRUE; |
| 1286 | } |
| 1287 | |
| 1288 | /* ------------------------------------------------------------------------ */ |
| 1289 | /* |
| 1290 | * Load a Constant Pool Definition (CPDF) data block |
| 1291 | */ |
| 1292 | void CVmImageLoader::load_const_pool_def(ulong siz) |
| 1293 | { |
| 1294 | char buf[32]; |
| 1295 | uint pool_id; |
| 1296 | ulong page_count; |
| 1297 | ulong page_size; |
| 1298 | |
| 1299 | /* read the block data */ |
| 1300 | read_data(buf, 10, &siz); |
| 1301 | |
| 1302 | /* decode the pool identifier, page count, and page size */ |
| 1303 | pool_id = osrp2(buf); |
| 1304 | page_count = t3rp4u(buf + 2); |
| 1305 | page_size = t3rp4u(buf + 6); |
| 1306 | |
| 1307 | /* ensure that the pool ID is valid */ |
| 1308 | if (pool_id < 1 || pool_id > sizeof(pools_)/sizeof(pools_[0])) |
| 1309 | err_throw(VMERR_IMAGE_BAD_POOL_ID); |
| 1310 | |
| 1311 | /* adjust the pool to 0-based range */ |
| 1312 | --pool_id; |
| 1313 | |
| 1314 | /* initialize the pool */ |
| 1315 | pools_[pool_id]->init(fp_, page_count, page_size); |
| 1316 | } |
| 1317 | |
| 1318 | |
| 1319 | /* ------------------------------------------------------------------------ */ |
| 1320 | /* |
| 1321 | * Load a Constant Pool Page (CPPG) data block |
| 1322 | */ |
| 1323 | void CVmImageLoader::load_const_pool_page(ulong siz) |
| 1324 | { |
| 1325 | char buf[32]; |
| 1326 | uint pool_id; |
| 1327 | ulong idx; |
| 1328 | uchar xor_mask; |
| 1329 | |
| 1330 | /* read the header */ |
| 1331 | read_data(buf, 7, &siz); |
| 1332 | |
| 1333 | /* decode the pool ID and page index */ |
| 1334 | pool_id = osrp2(buf); |
| 1335 | idx = t3rp4u(buf + 2); |
| 1336 | xor_mask = buf[6]; |
| 1337 | |
| 1338 | /* ensure that the pool ID is valid */ |
| 1339 | if (pool_id < 1 || pool_id > sizeof(pools_)/sizeof(pools_[0])) |
| 1340 | err_throw(VMERR_IMAGE_BAD_POOL_ID); |
| 1341 | |
| 1342 | /* adjust the pool to 0-based range */ |
| 1343 | --pool_id; |
| 1344 | |
| 1345 | /* set the page information */ |
| 1346 | pools_[pool_id]->set_page_info(idx, fp_->get_seek(), siz, xor_mask); |
| 1347 | |
| 1348 | /* |
| 1349 | * Skip past the page data, which follow the header - we don't need |
| 1350 | * to load the page data right now, but merely note where it is for |
| 1351 | * later loading. The caller may choose to load page data only as |
| 1352 | * required, rather than all at once, so we don't want to bring it |
| 1353 | * into memory right now. |
| 1354 | */ |
| 1355 | fp_->skip_ahead(siz); |
| 1356 | } |
| 1357 | |
| 1358 | |
| 1359 | /* ------------------------------------------------------------------------ */ |
| 1360 | /* |
| 1361 | * Load a Static Object (OBJS) data block |
| 1362 | */ |
| 1363 | void CVmImageLoader::load_static_objs(VMG_ ulong siz) |
| 1364 | { |
| 1365 | char buf[32]; |
| 1366 | uint obj_count; |
| 1367 | uint meta_idx; |
| 1368 | int header_size; |
| 1369 | int large_objs; |
| 1370 | uint flags; |
| 1371 | int trans; |
| 1372 | |
| 1373 | /* read the number of objects, the metaclass index, and the flags */ |
| 1374 | read_data(buf, 6, &siz); |
| 1375 | |
| 1376 | /* decode the object count and metaclass index values */ |
| 1377 | obj_count = osrp2(buf); |
| 1378 | meta_idx = osrp2(buf + 2); |
| 1379 | |
| 1380 | /* decode the flags */ |
| 1381 | flags = osrp2(buf + 4); |
| 1382 | large_objs = ((flags & 1) != 0); |
| 1383 | trans = ((flags & 2) != 0); |
| 1384 | |
| 1385 | /* calculate the per-object header size */ |
| 1386 | header_size = 4 + (large_objs ? 4 : 2); |
| 1387 | |
| 1388 | /* read the objects */ |
| 1389 | for ( ; obj_count != 0 ; --obj_count) |
| 1390 | { |
| 1391 | vm_obj_id_t obj_id; |
| 1392 | size_t obj_size; |
| 1393 | const char *data_ptr; |
| 1394 | |
| 1395 | /* read the object ID and data size */ |
| 1396 | read_data(buf, header_size, &siz); |
| 1397 | |
| 1398 | /* get the object ID */ |
| 1399 | obj_id = (vm_obj_id_t)t3rp4u(buf); |
| 1400 | |
| 1401 | /* get the size */ |
| 1402 | if (large_objs) |
| 1403 | { |
| 1404 | /* |
| 1405 | * Make sure the object size doesn't overflow the local |
| 1406 | * hardware limits. (This test is only necessary if such an |
| 1407 | * overflow is possible given the datatypes. On most modern |
| 1408 | * architectures, it simply isn't possible because OSMALMAX is |
| 1409 | * at least as large as any 32-bit value we could read here. |
| 1410 | * Note that the value we read here is explicitly a 32-bit |
| 1411 | * value, so the biggest it could possibly be is 0xFFFFFFFF.) |
| 1412 | */ |
| 1413 | #if 0xFFFFFFFFU > OSMALMAX |
| 1414 | if (t3rp4u(buf + 4) > (unsigned long)OSMALMAX) |
| 1415 | err_throw(VMERR_OBJ_SIZE_OVERFLOW); |
| 1416 | #endif |
| 1417 | |
| 1418 | /* read the object size */ |
| 1419 | obj_size = (size_t)t3rp4u(buf + 4); |
| 1420 | } |
| 1421 | else |
| 1422 | { |
| 1423 | /* read the 16-bit object size */ |
| 1424 | obj_size = osrp2(buf + 4); |
| 1425 | } |
| 1426 | |
| 1427 | /* load the data, allocating space for it */ |
| 1428 | data_ptr = alloc_and_read(obj_size, &siz); |
| 1429 | |
| 1430 | /* create a new object of the required metaclass */ |
| 1431 | G_meta_table->create_from_image(vmg_ meta_idx, obj_id, |
| 1432 | data_ptr, obj_size); |
| 1433 | |
| 1434 | /* mark the object as transient, if appropriate */ |
| 1435 | if (trans) |
| 1436 | G_obj_table->set_obj_transient(obj_id); |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | /* ------------------------------------------------------------------------ */ |
| 1441 | /* |
| 1442 | * Load a multi-media resource (MRES) block |
| 1443 | */ |
| 1444 | void CVmImageLoader::load_mres(ulong siz, CVmImageLoaderMres *res_ifc) |
| 1445 | { |
| 1446 | char buf[16]; |
| 1447 | uint entry_cnt; |
| 1448 | long base_ofs; |
| 1449 | uint i; |
| 1450 | |
| 1451 | /* |
| 1452 | * Note the current physical seek position - each entry's seek position |
| 1453 | * is stored in the table of contents as an offset from this location. |
| 1454 | * |
| 1455 | * Compute the physical base offset: this is the logical base offset in |
| 1456 | * our image stream, plus the offset of the image stream within the |
| 1457 | * image file. We need the physical base offset because the underlying |
| 1458 | * interpreter's resource loader works with the physical file, not the |
| 1459 | * logical image stream. |
| 1460 | */ |
| 1461 | base_ofs = fp_->get_seek() + base_seek_ofs_; |
| 1462 | |
| 1463 | /* read the entry count and size of the table of contents */ |
| 1464 | read_data(buf, 2, &siz); |
| 1465 | entry_cnt = osrp2(buf); |
| 1466 | |
| 1467 | /* read the entries */ |
| 1468 | for (i = 0 ; i < entry_cnt ; ++i) |
| 1469 | { |
| 1470 | uint32_t entry_ofs; |
| 1471 | uint32_t entry_size; |
| 1472 | uint entry_name_len; |
| 1473 | char name_buf[256]; |
| 1474 | char *p; |
| 1475 | size_t rem; |
| 1476 | |
| 1477 | /* read the fixed part of the table-of-contents entry */ |
| 1478 | read_data(buf, 9, &siz); |
| 1479 | entry_ofs = t3rp4u(buf); |
| 1480 | entry_size = t3rp4u(buf + 4); |
| 1481 | entry_name_len = (uchar)*(buf + 8); |
| 1482 | |
| 1483 | /* read the name */ |
| 1484 | read_data(name_buf, entry_name_len, &siz); |
| 1485 | |
| 1486 | /* null-terminate the name */ |
| 1487 | name_buf[entry_name_len] = '\0'; |
| 1488 | |
| 1489 | /* XOR the bytes of the name with 0xFF */ |
| 1490 | for (p = name_buf, rem = entry_name_len ; rem != 0 ; ++p, --rem) |
| 1491 | *p ^= 0xFF; |
| 1492 | |
| 1493 | /* add the resource to the resource interface */ |
| 1494 | res_ifc->add_resource(base_ofs + entry_ofs, entry_size, |
| 1495 | name_buf, entry_name_len); |
| 1496 | } |
| 1497 | |
| 1498 | /* |
| 1499 | * skip the data portion of the block, since we now have a map of |
| 1500 | * the data and can load individual resources on demand |
| 1501 | */ |
| 1502 | if (siz != 0) |
| 1503 | fp_->skip_ahead(siz); |
| 1504 | } |
| 1505 | |
| 1506 | /* ------------------------------------------------------------------------ */ |
| 1507 | /* |
| 1508 | * Load a multi-media resource link (MREL) block |
| 1509 | */ |
| 1510 | void CVmImageLoader::load_mres_link(ulong siz, CVmImageLoaderMres *res_ifc) |
| 1511 | { |
| 1512 | char buf[16]; |
| 1513 | uint entry_cnt; |
| 1514 | uint i; |
| 1515 | |
| 1516 | /* read the entry count and size of the table of contents */ |
| 1517 | read_data(buf, 2, &siz); |
| 1518 | entry_cnt = osrp2(buf); |
| 1519 | |
| 1520 | /* read the entries */ |
| 1521 | for (i = 0 ; i < entry_cnt ; ++i) |
| 1522 | { |
| 1523 | char buf[1]; |
| 1524 | uint resname_len, fname_len; |
| 1525 | char resname_buf[256], fname_buf[256]; |
| 1526 | |
| 1527 | /* read the resource name */ |
| 1528 | read_data(buf, 1, &siz); |
| 1529 | resname_len = (uchar)buf[0]; |
| 1530 | read_data(resname_buf, resname_len, &siz); |
| 1531 | resname_buf[resname_len] = '\0'; |
| 1532 | |
| 1533 | /* read the local filename this resource is linked to */ |
| 1534 | read_data(buf, 1, &siz); |
| 1535 | fname_len = (uchar)buf[0]; |
| 1536 | read_data(fname_buf, fname_len, &siz); |
| 1537 | fname_buf[fname_len] = '\0'; |
| 1538 | |
| 1539 | /* add the resource to the resource interface */ |
| 1540 | res_ifc->add_resource(fname_buf, fname_len, resname_buf, resname_len); |
| 1541 | } |
| 1542 | |
| 1543 | /* |
| 1544 | * skip the data portion of the block, since we now have a map of |
| 1545 | * the data and can load individual resources on demand |
| 1546 | */ |
| 1547 | if (siz != 0) |
| 1548 | fp_->skip_ahead(siz); |
| 1549 | } |
| 1550 | |
| 1551 | /* ------------------------------------------------------------------------ */ |
| 1552 | /* |
| 1553 | * load a Metaclass Dependency block |
| 1554 | */ |
| 1555 | void CVmImageLoader::load_meta_dep(VMG_ ulong siz) |
| 1556 | { |
| 1557 | char buf[256 + 10]; |
| 1558 | uint entry_cnt; |
| 1559 | uint i; |
| 1560 | |
| 1561 | /* it's an error if we've already seen a dependency block */ |
| 1562 | if (loaded_meta_dep_) |
| 1563 | err_throw(VMERR_IMAGE_METADEP_REDEF); |
| 1564 | |
| 1565 | /* note that we've loaded a dependency block */ |
| 1566 | loaded_meta_dep_ = TRUE; |
| 1567 | |
| 1568 | /* |
| 1569 | * reset the global metaclass table, in case there was anything |
| 1570 | * defined by a previously-loaded image - since a metaclass's index |
| 1571 | * in the table is implicit in the load order, we need to make sure |
| 1572 | * we're starting with a completely clean configuration |
| 1573 | */ |
| 1574 | G_meta_table->clear(); |
| 1575 | |
| 1576 | /* read the number of entries in the table */ |
| 1577 | read_data(buf, 2, &siz); |
| 1578 | entry_cnt = osrp2(buf); |
| 1579 | |
| 1580 | /* read the entries */ |
| 1581 | for (i = 0 ; i < entry_cnt ; ++i) |
| 1582 | { |
| 1583 | ushort j; |
| 1584 | uint len; |
| 1585 | uint prop_cnt; |
| 1586 | uint prop_len; |
| 1587 | vm_meta_entry_t *entry; |
| 1588 | char *p; |
| 1589 | ulong done_size; |
| 1590 | long prop_start_ofs; |
| 1591 | ulong prop_start_siz; |
| 1592 | vm_prop_id_t max_prop; |
| 1593 | vm_prop_id_t min_prop; |
| 1594 | |
| 1595 | /* |
| 1596 | * read the record size, and calculate the value of 'siz' that |
| 1597 | * we expect when we've parsed this entire record - it's the |
| 1598 | * current size minus the size of the record (note that we have |
| 1599 | * to add back in the two bytes of the size record itself, since |
| 1600 | * 'siz' will have been moved past the size record once we've |
| 1601 | * read it) |
| 1602 | */ |
| 1603 | read_data(buf, 2, &siz); |
| 1604 | done_size = (siz + 2) - osrp2(buf); |
| 1605 | |
| 1606 | /* read the length of the entry */ |
| 1607 | read_data(buf, 1, &siz); |
| 1608 | len = (uchar)buf[0]; |
| 1609 | |
| 1610 | /* read the name and null-terminate it */ |
| 1611 | read_data(buf, len, &siz); |
| 1612 | buf[len] = '\0'; |
| 1613 | p = buf + len + 1; |
| 1614 | |
| 1615 | /* read the property table information */ |
| 1616 | read_data(p, 4, &siz); |
| 1617 | prop_cnt = osrp2(p); |
| 1618 | prop_len = osrp2(p + 2); |
| 1619 | |
| 1620 | /* |
| 1621 | * set min and max to 'invalid' to start with, in case there are |
| 1622 | * no properties at all in the table |
| 1623 | */ |
| 1624 | min_prop = max_prop = VM_INVALID_PROP; |
| 1625 | |
| 1626 | /* |
| 1627 | * Read the properties - pass one: find the minimum and maximum |
| 1628 | * property ID in the table. Before we begin, remember where |
| 1629 | * the properties start in the file, so we can go back and read |
| 1630 | * the table again on the second pass. |
| 1631 | */ |
| 1632 | prop_start_ofs = fp_->get_seek(); |
| 1633 | prop_start_siz = siz; |
| 1634 | for (j = 0 ; j < prop_cnt ; ++j) |
| 1635 | { |
| 1636 | vm_prop_id_t cur; |
| 1637 | |
| 1638 | /* read the next entry */ |
| 1639 | read_data(p, 2, &siz); |
| 1640 | |
| 1641 | /* skip any additional data in the record */ |
| 1642 | if (prop_len > 2) |
| 1643 | skip_data(prop_len - 2, &siz); |
| 1644 | |
| 1645 | /* get the property */ |
| 1646 | cur = (vm_prop_id_t)osrp2(p); |
| 1647 | |
| 1648 | /* |
| 1649 | * if this is the first property, it's the max and min so |
| 1650 | * far; otherwise, remember it if it's the highest or lowest |
| 1651 | * so far |
| 1652 | */ |
| 1653 | if (j == 0) |
| 1654 | { |
| 1655 | /* this is the first one, so note it unconditionally */ |
| 1656 | min_prop = max_prop = cur; |
| 1657 | } |
| 1658 | else |
| 1659 | { |
| 1660 | /* if it's below the current minimum, it's the new minimum */ |
| 1661 | if (cur < min_prop) |
| 1662 | min_prop = cur; |
| 1663 | |
| 1664 | /* if it's above the current maximum, it's the new maximum */ |
| 1665 | if (cur > max_prop) |
| 1666 | max_prop = cur; |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | /* |
| 1671 | * Now that we know the minimum and maximum properties in the |
| 1672 | * table, we can create the dependency record. |
| 1673 | */ |
| 1674 | G_meta_table->add_entry(buf, prop_cnt, min_prop, max_prop); |
| 1675 | |
| 1676 | /* get a pointer to my new entry */ |
| 1677 | entry = G_meta_table->get_entry(i); |
| 1678 | |
| 1679 | /* |
| 1680 | * Pass two: read the actual properties into the translation |
| 1681 | * table. Seek back to the start of the properties in the file, |
| 1682 | * then read the ID's. |
| 1683 | * |
| 1684 | * Note that add_prop_xlat() expects a 1-based function table |
| 1685 | * index. We are in fact reading a function table, so run our |
| 1686 | * function index counter from 1 to the entry count to yield the |
| 1687 | * proper 1-based index values. |
| 1688 | */ |
| 1689 | fp_->seek(prop_start_ofs); |
| 1690 | siz = prop_start_siz; |
| 1691 | for (j = 1 ; j <= prop_cnt ; ++j) |
| 1692 | { |
| 1693 | /* read the next entry */ |
| 1694 | read_data(p, 2, &siz); |
| 1695 | |
| 1696 | /* skip any additional data in the record */ |
| 1697 | if (prop_len > 2) |
| 1698 | skip_data(prop_len - 2, &siz); |
| 1699 | |
| 1700 | /* add this entry */ |
| 1701 | entry->add_prop_xlat((vm_prop_id_t)osrp2(p), j); |
| 1702 | } |
| 1703 | |
| 1704 | /* skip any remaining data in the record */ |
| 1705 | if (siz > done_size) |
| 1706 | skip_data(siz - done_size, &siz); |
| 1707 | } |
| 1708 | |
| 1709 | /* |
| 1710 | * always add the root object metaclass to the table, whether the |
| 1711 | * image file defines it or not |
| 1712 | */ |
| 1713 | G_meta_table |
| 1714 | ->add_entry_if_new(CVmObject::metaclass_reg_->get_reg_idx(), |
| 1715 | 0, VM_INVALID_PROP, VM_INVALID_PROP); |
| 1716 | } |
| 1717 | |
| 1718 | /* ------------------------------------------------------------------------ */ |
| 1719 | /* |
| 1720 | * load a Function Set Dependency block |
| 1721 | */ |
| 1722 | void CVmImageLoader::load_funcset_dep(VMG_ ulong siz) |
| 1723 | { |
| 1724 | char buf[256]; |
| 1725 | uint entry_cnt; |
| 1726 | uint i; |
| 1727 | |
| 1728 | /* it's an error if we've already seen a dependency block */ |
| 1729 | if (loaded_funcset_dep_) |
| 1730 | err_throw(VMERR_IMAGE_FUNCDEP_REDEF); |
| 1731 | |
| 1732 | /* note that we've loaded a dependency block */ |
| 1733 | loaded_funcset_dep_ = TRUE; |
| 1734 | |
| 1735 | /* |
| 1736 | * reset the global function set dependency table, in case there was |
| 1737 | * anything defined by a previously-loaded image - since a function |
| 1738 | * set index in the table is implicit in the load order, we need to |
| 1739 | * make sure we're starting with a completely clean configuration |
| 1740 | */ |
| 1741 | G_bif_table->clear(vmg0_); |
| 1742 | |
| 1743 | /* read the number of entries in the table */ |
| 1744 | read_data(buf, 2, &siz); |
| 1745 | entry_cnt = osrp2(buf); |
| 1746 | |
| 1747 | /* read the entries */ |
| 1748 | for (i = 0 ; i < entry_cnt ; ++i) |
| 1749 | { |
| 1750 | uint len; |
| 1751 | |
| 1752 | /* read the length of the entry */ |
| 1753 | read_data(buf, 1, &siz); |
| 1754 | len = (uchar)buf[0]; |
| 1755 | |
| 1756 | /* read the name and null-terminate it */ |
| 1757 | read_data(buf, len, &siz); |
| 1758 | buf[len] = '\0'; |
| 1759 | |
| 1760 | /* add the dependency */ |
| 1761 | G_bif_table->add_entry(vmg_ buf); |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | /* ------------------------------------------------------------------------ */ |
| 1766 | /* |
| 1767 | * load a Symbolic Names export block |
| 1768 | */ |
| 1769 | void CVmImageLoader::load_sym_names(VMG_ ulong siz) |
| 1770 | { |
| 1771 | char buf[256]; |
| 1772 | uint entry_cnt; |
| 1773 | uint i; |
| 1774 | |
| 1775 | /* read the number of entries */ |
| 1776 | read_data(buf, 2, &siz); |
| 1777 | entry_cnt = osrp2(buf); |
| 1778 | |
| 1779 | /* read the entries */ |
| 1780 | for (i = 0 ; i < entry_cnt ; ++i) |
| 1781 | { |
| 1782 | vm_val_t val; |
| 1783 | uint namelen; |
| 1784 | |
| 1785 | /* read the data holder and name length */ |
| 1786 | read_data(buf, VMB_DATAHOLDER + 1, &siz); |
| 1787 | vmb_get_dh(buf, &val); |
| 1788 | namelen = (uchar)*(buf + VMB_DATAHOLDER); |
| 1789 | |
| 1790 | /* read the name */ |
| 1791 | read_data(buf, namelen, &siz); |
| 1792 | |
| 1793 | /* add this as a new entry to our exports table */ |
| 1794 | exports_->add(new CVmHashEntryExport(buf, namelen, TRUE, &val)); |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | /* ------------------------------------------------------------------------ */ |
| 1799 | /* |
| 1800 | * Load a Global Symbols (GSYM) block into the runtime symbol table |
| 1801 | */ |
| 1802 | void CVmImageLoader::load_runtime_symtab_from_gsym(VMG_ ulong siz) |
| 1803 | { |
| 1804 | const size_t TOK_SYM_MAX_LEN = 80; |
| 1805 | char buf[TOK_SYM_MAX_LEN + 128]; |
| 1806 | ulong cnt; |
| 1807 | |
| 1808 | /* allocate the symbol table if we haven't already done so */ |
| 1809 | if (runtime_symtab_ == 0) |
| 1810 | runtime_symtab_ = new CVmRuntimeSymbols(); |
| 1811 | |
| 1812 | /* read the symbol count */ |
| 1813 | read_data(buf, 4, &siz); |
| 1814 | cnt = t3rp4u(buf); |
| 1815 | |
| 1816 | /* read the symbols and populate the symbol table */ |
| 1817 | for ( ; cnt != 0 ; --cnt) |
| 1818 | { |
| 1819 | char *sym_name; |
| 1820 | size_t sym_len; |
| 1821 | size_t dat_len; |
| 1822 | tc_symtype_t sym_type; |
| 1823 | char *dat; |
| 1824 | vm_val_t val; |
| 1825 | |
| 1826 | /* read the symbol's length, extra data length, and type code */ |
| 1827 | read_data(buf, 6, &siz); |
| 1828 | sym_len = osrp2(buf); |
| 1829 | dat_len = osrp2(buf + 2); |
| 1830 | sym_type = (tc_symtype_t)osrp2(buf + 4); |
| 1831 | |
| 1832 | /* check the lengths to make sure they don't overflow our buffer */ |
| 1833 | if (sym_len > TOK_SYM_MAX_LEN) |
| 1834 | { |
| 1835 | /* |
| 1836 | * this symbol name is too long - skip the symbol and its |
| 1837 | * extra data entirely |
| 1838 | */ |
| 1839 | skip_data(sym_len + dat_len, &siz); |
| 1840 | |
| 1841 | /* go on to the next symbol */ |
| 1842 | continue; |
| 1843 | } |
| 1844 | else if (dat_len + sym_len > sizeof(buf)) |
| 1845 | { |
| 1846 | /* |
| 1847 | * The extra data block is too long for our fixed buffer. |
| 1848 | * Truncate the extra data so that we don't overflow our |
| 1849 | * buffer, but proceed anyway with the truncated extra data. |
| 1850 | * Note that we won't lose any data we care about, since any |
| 1851 | * extra data beyond our buffer size is additional future |
| 1852 | * format data that we don't know how to handle anyway. By |
| 1853 | * design, we're allowed to skip extra data that we don't know |
| 1854 | * how to read. |
| 1855 | */ |
| 1856 | read_data(buf, sizeof(buf), &siz); |
| 1857 | |
| 1858 | /* skip the remainder of the extra data */ |
| 1859 | skip_data(sym_len + dat_len - sizeof(buf), &siz); |
| 1860 | } |
| 1861 | else |
| 1862 | { |
| 1863 | /* read the symbol's name and its type-specific data */ |
| 1864 | read_data(buf, sym_len + dat_len, &siz); |
| 1865 | } |
| 1866 | |
| 1867 | /* the symbol name is at the start of the buffer */ |
| 1868 | sym_name = buf; |
| 1869 | |
| 1870 | /* get the pointer to the extra data (it comes after the name) */ |
| 1871 | dat = buf + sym_len; |
| 1872 | |
| 1873 | /* create the new symbol table entry, depending on its type */ |
| 1874 | switch(sym_type) |
| 1875 | { |
| 1876 | case TC_SYM_FUNC: |
| 1877 | /* set up the function pointer value */ |
| 1878 | val.set_fnptr(t3rp4u(dat)); |
| 1879 | break; |
| 1880 | |
| 1881 | case TC_SYM_OBJ: |
| 1882 | /* set up the object value */ |
| 1883 | val.set_obj(t3rp4u(dat)); |
| 1884 | break; |
| 1885 | |
| 1886 | case TC_SYM_PROP: |
| 1887 | /* set up the property value */ |
| 1888 | val.set_propid(osrp2(dat)); |
| 1889 | |
| 1890 | /* check the 'dictionary property' flag */ |
| 1891 | if (dat_len >= 3 && (dat[0] & 0x01) != 0) |
| 1892 | { |
| 1893 | // $$$ it's a dictionary property |
| 1894 | } |
| 1895 | break; |
| 1896 | |
| 1897 | case TC_SYM_ENUM: |
| 1898 | /* set up the enum value */ |
| 1899 | val.set_enum(t3rp4u(dat)); |
| 1900 | |
| 1901 | /* check the 'enum token' flag */ |
| 1902 | if (dat_len >= 5 && (dat[0] & 0x01) != 0) |
| 1903 | { |
| 1904 | // $$$ it's an 'enum token' |
| 1905 | } |
| 1906 | break; |
| 1907 | |
| 1908 | case TC_SYM_METACLASS: |
| 1909 | /* set up the metaclass object value */ |
| 1910 | val.set_obj(t3rp4u(dat + 2)); |
| 1911 | break; |
| 1912 | |
| 1913 | case TC_SYM_BIF: |
| 1914 | /* built-in function */ |
| 1915 | val.set_bifptr(osrp2(dat + 2), osrp2(dat)); |
| 1916 | break; |
| 1917 | |
| 1918 | default: |
| 1919 | /* |
| 1920 | * ignore other types; mark the value as 'empty' so we know we |
| 1921 | * don't have anything to add to the table |
| 1922 | */ |
| 1923 | val.set_empty(); |
| 1924 | break; |
| 1925 | } |
| 1926 | |
| 1927 | /* if we found a valid type, add it to the table */ |
| 1928 | if (val.typ != VM_EMPTY) |
| 1929 | runtime_symtab_->add_sym(sym_name, sym_len, &val); |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | /* ------------------------------------------------------------------------ */ |
| 1934 | /* |
| 1935 | * Load a Macro Definitions (MACR) block into the runtime symbol table |
| 1936 | */ |
| 1937 | void CVmImageLoader::load_runtime_symtab_from_macr(VMG_ ulong siz) |
| 1938 | { |
| 1939 | const size_t TOK_SYM_MAX_LEN = 80; |
| 1940 | char buf[10]; |
| 1941 | char sym[TOK_SYM_MAX_LEN]; |
| 1942 | ulong cnt; |
| 1943 | |
| 1944 | /* allocate the macro table if we haven't already done so */ |
| 1945 | if (runtime_macros_ == 0) |
| 1946 | runtime_macros_ = new CVmRuntimeSymbols(); |
| 1947 | |
| 1948 | /* read the symbol count */ |
| 1949 | read_data(buf, 4, &siz); |
| 1950 | cnt = t3rp4u(buf); |
| 1951 | |
| 1952 | /* read the symbols and populate the symbol table */ |
| 1953 | for ( ; cnt != 0 ; --cnt) |
| 1954 | { |
| 1955 | size_t sym_len; |
| 1956 | size_t exp_len; |
| 1957 | int argc; |
| 1958 | unsigned int flags; |
| 1959 | int i; |
| 1960 | |
| 1961 | /* read the symbol's length */ |
| 1962 | read_data(buf, 2, &siz); |
| 1963 | sym_len = osrp2(buf); |
| 1964 | |
| 1965 | /* if it fits, read it */ |
| 1966 | if (sym_len <= TOK_SYM_MAX_LEN) |
| 1967 | { |
| 1968 | /* read the name */ |
| 1969 | read_data(sym, sym_len, &siz); |
| 1970 | } |
| 1971 | else |
| 1972 | { |
| 1973 | /* it's too long - skip it */ |
| 1974 | skip_data(sym_len, &siz); |
| 1975 | } |
| 1976 | |
| 1977 | /* read the flags and argument count */ |
| 1978 | read_data(buf, 4, &siz); |
| 1979 | flags = osrp2(buf); |
| 1980 | argc = osrp2(buf + 2); |
| 1981 | |
| 1982 | /* remember the current position */ |
| 1983 | long start_pos = fp_->get_seek(); |
| 1984 | ulong start_siz = siz; |
| 1985 | |
| 1986 | /* count up the argument sizes */ |
| 1987 | size_t arg_len = 0; |
| 1988 | for (i = 0 ; i < argc ; ++i) |
| 1989 | { |
| 1990 | /* read this argument length */ |
| 1991 | read_data(buf, 2, &siz); |
| 1992 | size_t l = osrp2(buf); |
| 1993 | |
| 1994 | /* add it to the total, plus a null byte */ |
| 1995 | arg_len += l + 1; |
| 1996 | |
| 1997 | /* skip it in the file */ |
| 1998 | skip_data(l, &siz); |
| 1999 | } |
| 2000 | |
| 2001 | /* read the expansion size */ |
| 2002 | read_data(buf, 4, &siz); |
| 2003 | exp_len = osrp4(buf); |
| 2004 | |
| 2005 | /* allocate the symbol entry */ |
| 2006 | vm_runtime_sym *entry = runtime_macros_->add_macro( |
| 2007 | sym, sym_len, exp_len, flags, argc, arg_len); |
| 2008 | |
| 2009 | /* seek back to the start of the argument list */ |
| 2010 | fp_->seek(start_pos); |
| 2011 | siz = start_siz; |
| 2012 | |
| 2013 | /* read the arguments */ |
| 2014 | for (i = 0 ; i < argc ; ++i) |
| 2015 | { |
| 2016 | /* read the argument length */ |
| 2017 | read_data(buf, 2, &siz); |
| 2018 | size_t l = osrp2(buf); |
| 2019 | |
| 2020 | /* read this argument, and null-terminate it */ |
| 2021 | read_data(entry->macro_args[i], l, &siz); |
| 2022 | entry->macro_args[i][l] = '\0'; |
| 2023 | |
| 2024 | /* commit the storage */ |
| 2025 | entry->commit_macro_arg(i, l + 1); |
| 2026 | } |
| 2027 | |
| 2028 | /* read the expansion */ |
| 2029 | skip_data(4, &siz); |
| 2030 | read_data(entry->macro_expansion, exp_len, &siz); |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | /* ------------------------------------------------------------------------ */ |
| 2035 | /* |
| 2036 | * load a Source File List block |
| 2037 | */ |
| 2038 | void CVmImageLoader::load_srcfiles(VMG_ ulong siz) |
| 2039 | { |
| 2040 | size_t i; |
| 2041 | char buf[64]; |
| 2042 | size_t entry_cnt; |
| 2043 | size_t line_size; |
| 2044 | |
| 2045 | /* |
| 2046 | * if there's no source file table, we're not in debug mode and hence |
| 2047 | * have no use for this type of information, so skip the block |
| 2048 | */ |
| 2049 | if (G_srcf_table == 0) |
| 2050 | { |
| 2051 | /* skip the entire block in the file */ |
| 2052 | fp_->skip_ahead(siz); |
| 2053 | |
| 2054 | /* we're done */ |
| 2055 | return; |
| 2056 | } |
| 2057 | |
| 2058 | /* clear any existing information in the table */ |
| 2059 | G_srcf_table->clear(); |
| 2060 | |
| 2061 | /* read the number of entries in the table, and the line record size */ |
| 2062 | read_data(buf, 4, &siz); |
| 2063 | entry_cnt = osrp2(buf); |
| 2064 | line_size = osrp2(buf + 2); |
| 2065 | |
| 2066 | /* read the entries */ |
| 2067 | for (i = 0 ; i < entry_cnt ; ++i) |
| 2068 | { |
| 2069 | long start_pos; |
| 2070 | long next_pos; |
| 2071 | int orig_index; |
| 2072 | size_t len; |
| 2073 | CVmSrcfEntry *entry; |
| 2074 | ulong line_cnt; |
| 2075 | ulong skip_amt; |
| 2076 | |
| 2077 | /* note the starting file location of this record */ |
| 2078 | start_pos = fp_->get_seek(); |
| 2079 | |
| 2080 | /* |
| 2081 | * read the size of this file record, the original index, and the |
| 2082 | * length of the filename |
| 2083 | */ |
| 2084 | read_data(buf, 8, &siz); |
| 2085 | orig_index = osrp2(buf + 4); |
| 2086 | len = osrp2(buf + 6); |
| 2087 | |
| 2088 | /* |
| 2089 | * calculate the file location of the start of the next block by |
| 2090 | * adding the starting position and size of this block |
| 2091 | */ |
| 2092 | next_pos = start_pos + t3rp4u(buf); |
| 2093 | |
| 2094 | /* allocate the entry */ |
| 2095 | entry = G_srcf_table->add_entry(orig_index, len); |
| 2096 | |
| 2097 | /* read the data into this entry's buffer */ |
| 2098 | read_data(entry->get_name_buf(), len, &siz); |
| 2099 | |
| 2100 | /* null-terminate the buffer */ |
| 2101 | entry->get_name_buf()[len] = '\0'; |
| 2102 | |
| 2103 | /* read the number of line records */ |
| 2104 | read_data(buf, 4, &siz); |
| 2105 | line_cnt = t3rp4u(buf); |
| 2106 | |
| 2107 | /* |
| 2108 | * if the line records are too big for our buffer, or smaller than |
| 2109 | * our required minimum size, don't bother reading them - we'll |
| 2110 | * just skip them entirely |
| 2111 | */ |
| 2112 | if (line_size <= sizeof(buf) && line_size >= 8) |
| 2113 | { |
| 2114 | /* tell the source file table entry how many lines we have */ |
| 2115 | entry->alloc_line_records(line_cnt); |
| 2116 | |
| 2117 | /* read the line records */ |
| 2118 | for ( ; line_cnt != 0 ; --line_cnt) |
| 2119 | { |
| 2120 | ulong linenum; |
| 2121 | ulong code_addr; |
| 2122 | |
| 2123 | /* read the record */ |
| 2124 | read_data(buf, line_size, &siz); |
| 2125 | |
| 2126 | /* get the source line number and byte code offset */ |
| 2127 | linenum = t3rp4u(buf); |
| 2128 | code_addr = t3rp4u(buf + 4); |
| 2129 | |
| 2130 | /* add the line to the source file table entry */ |
| 2131 | entry->add_line_record(linenum, code_addr); |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | /* skip ahead to the start of the next record */ |
| 2136 | skip_amt = next_pos - fp_->get_seek(); |
| 2137 | if (skip_amt != 0) |
| 2138 | skip_data(skip_amt, &siz); |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | /* ------------------------------------------------------------------------ */ |
| 2143 | /* |
| 2144 | * Perform dynamic linking after loading. |
| 2145 | */ |
| 2146 | void CVmImageLoader::do_dynamic_link(VMG0_) |
| 2147 | { |
| 2148 | struct sym_assoc_t |
| 2149 | { |
| 2150 | /* the symbol name */ |
| 2151 | const char *sym; |
| 2152 | |
| 2153 | /* datatype */ |
| 2154 | vm_datatype_t typ; |
| 2155 | |
| 2156 | /* address of predef table entry for this value */ |
| 2157 | void *addr; |
| 2158 | |
| 2159 | /* |
| 2160 | * flag: this is a synthetic import, which means that we don't |
| 2161 | * look for it in the image file, but only in the restored state |
| 2162 | */ |
| 2163 | int is_synthetic; |
| 2164 | }; |
| 2165 | sym_assoc_t imports[] = |
| 2166 | { |
| 2167 | /* build the table by including the import list */ |
| 2168 | #define VM_IMPORT_OBJ(sym, mem) { sym, VM_OBJ, &G_predef->mem, FALSE }, |
| 2169 | #define VM_NOIMPORT_OBJ(sym, mem) { sym, VM_OBJ, &G_predef->mem, TRUE }, |
| 2170 | #define VM_IMPORT_PROP(sym, mem) { sym, VM_PROP, &G_predef->mem, FALSE }, |
| 2171 | #define VM_NOIMPORT_PROP(sym, mem) { sym, VM_PROP, &G_predef->mem, TRUE }, |
| 2172 | #define VM_IMPORT_FUNC(sym, mem) { sym, VM_FUNCPTR, &G_predef->mem, FALSE }, |
| 2173 | #include "vmimport.h" |
| 2174 | |
| 2175 | /* end the table */ |
| 2176 | { 0, VM_NIL, 0, FALSE } |
| 2177 | }; |
| 2178 | const sym_assoc_t *ip; |
| 2179 | |
| 2180 | /* reset all of the predefs to undefined */ |
| 2181 | G_predef->reset(); |
| 2182 | |
| 2183 | /* |
| 2184 | * Run through our list of symbols to import from the load file, and |
| 2185 | * load each one. |
| 2186 | */ |
| 2187 | for (ip = imports ; ip->sym != 0 ; ++ip) |
| 2188 | { |
| 2189 | CVmHashEntryExport *entry; |
| 2190 | |
| 2191 | /* presume we won't find it */ |
| 2192 | entry = 0; |
| 2193 | |
| 2194 | /* |
| 2195 | * if it's not synthetic, look up this symbol in the image file's |
| 2196 | * export table (don't look up synthetic symbols, because we |
| 2197 | * always want to create these ourselves, not load them from an |
| 2198 | * image file) |
| 2199 | */ |
| 2200 | if (!ip->is_synthetic) |
| 2201 | { |
| 2202 | /* it's a regular import - look for it in the image exports */ |
| 2203 | entry = (CVmHashEntryExport *)exports_ |
| 2204 | ->find(ip->sym, strlen(ip->sym)); |
| 2205 | } |
| 2206 | |
| 2207 | /* |
| 2208 | * if we didn't find the entry in the image file's exports, look |
| 2209 | * in the synthesized exports, in case we're restoring state from |
| 2210 | * a previous session |
| 2211 | */ |
| 2212 | if (entry == 0) |
| 2213 | entry = (CVmHashEntryExport *)synth_exports_ |
| 2214 | ->find(ip->sym, strlen(ip->sym)); |
| 2215 | |
| 2216 | /* if the symbol isn't exported, skip it and proceed to the next */ |
| 2217 | if (entry == 0) |
| 2218 | continue; |
| 2219 | |
| 2220 | /* |
| 2221 | * make sure the type of the symbol exported by the image file |
| 2222 | * matches the type of value we want to import for the symbol |
| 2223 | */ |
| 2224 | if (ip->typ != entry->val_.typ) |
| 2225 | { |
| 2226 | /* the exported symbol has the wrong type - throw an error */ |
| 2227 | err_throw_a(VMERR_INVAL_EXPORT_TYPE, 1, ERR_TYPE_CHAR, ip->sym); |
| 2228 | } |
| 2229 | |
| 2230 | /* set the value according to its type */ |
| 2231 | switch(ip->typ) |
| 2232 | { |
| 2233 | case VM_OBJ: |
| 2234 | /* store the object value */ |
| 2235 | *(vm_obj_id_t *)ip->addr = entry->val_.val.obj; |
| 2236 | break; |
| 2237 | |
| 2238 | case VM_PROP: |
| 2239 | *(vm_prop_id_t *)ip->addr = entry->val_.val.prop; |
| 2240 | break; |
| 2241 | |
| 2242 | case VM_FUNCPTR: |
| 2243 | *(pool_ofs_t *)ip->addr = entry->val_.val.ofs; |
| 2244 | break; |
| 2245 | |
| 2246 | default: |
| 2247 | /* we don't import any other types */ |
| 2248 | break; |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | /* |
| 2253 | * If we don't already have one, create a vector to keep track of the |
| 2254 | * last property ID allocated. We store this in a vector object so |
| 2255 | * that the property allocation mechanism easily integrates with the |
| 2256 | * undo and save/restore mechanisms. |
| 2257 | */ |
| 2258 | if (G_predef->last_prop_obj == VM_INVALID_OBJ) |
| 2259 | { |
| 2260 | /* create the object */ |
| 2261 | G_predef->last_prop_obj = CVmObjVector::create(vmg_ FALSE, 1); |
| 2262 | |
| 2263 | /* add it to the synthesized export list */ |
| 2264 | add_synth_export_obj(VM_IMPORT_NAME_LASTPROPOBJ, |
| 2265 | G_predef->last_prop_obj); |
| 2266 | |
| 2267 | /* |
| 2268 | * set up the object with the current last property, as indicated |
| 2269 | * in the image file |
| 2270 | */ |
| 2271 | set_last_prop(vmg_ G_predef->last_prop); |
| 2272 | } |
| 2273 | |
| 2274 | /* |
| 2275 | * if the image didn't define an exceptionMessage property, allocate a |
| 2276 | * new property ID for it |
| 2277 | */ |
| 2278 | if (G_predef->rterrmsg_prop == VM_INVALID_PROP) |
| 2279 | { |
| 2280 | /* allocate the property ID */ |
| 2281 | G_predef->rterrmsg_prop = alloc_new_prop(vmg0_); |
| 2282 | |
| 2283 | /* add it to the synthesized export list */ |
| 2284 | add_synth_export_prop(VM_IMPORT_NAME_RTERRMSG, |
| 2285 | G_predef->rterrmsg_prop); |
| 2286 | } |
| 2287 | |
| 2288 | /* |
| 2289 | * Create the constant string and list placeholder objects - these are |
| 2290 | * never imported from the image file, but must be dynamically created |
| 2291 | * after loading is complete. |
| 2292 | * |
| 2293 | * Create these objects without values, since the values are |
| 2294 | * irrelevant - they're needed only for their type information. |
| 2295 | */ |
| 2296 | if (G_predef->const_str_obj == VM_INVALID_OBJ) |
| 2297 | { |
| 2298 | /* allocate it */ |
| 2299 | G_predef->const_str_obj = CVmObjString::create(vmg_ FALSE, 0); |
| 2300 | |
| 2301 | /* add it to the synthesized export list */ |
| 2302 | add_synth_export_obj(VM_IMPORT_NAME_CONSTSTR, |
| 2303 | G_predef->const_str_obj); |
| 2304 | } |
| 2305 | if (G_predef->const_lst_obj == VM_INVALID_OBJ) |
| 2306 | { |
| 2307 | /* allocate it */ |
| 2308 | G_predef->const_lst_obj = CVmObjList::create(vmg_ FALSE, (size_t)0); |
| 2309 | |
| 2310 | /* add it to the synthesized export list */ |
| 2311 | add_synth_export_obj(VM_IMPORT_NAME_CONSTLST, |
| 2312 | G_predef->const_lst_obj); |
| 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | |
| 2317 | /* ------------------------------------------------------------------------ */ |
| 2318 | /* |
| 2319 | * Load a static initializer list block |
| 2320 | */ |
| 2321 | void CVmImageLoader::load_sini(VMG_ ulong siz) |
| 2322 | { |
| 2323 | char hdr[12]; |
| 2324 | ulong siz_rem; |
| 2325 | ulong hdr_siz; |
| 2326 | ulong init_cnt; |
| 2327 | ulong init_rem; |
| 2328 | |
| 2329 | /* |
| 2330 | * read the header size, static code starting offset, and |
| 2331 | * initializer count |
| 2332 | */ |
| 2333 | fp_->copy_data(hdr, 12); |
| 2334 | hdr_siz = t3rp4u(hdr); |
| 2335 | static_cs_ofs_ = t3rp4u(hdr + 4); |
| 2336 | init_cnt = t3rp4u(hdr + 8); |
| 2337 | |
| 2338 | /* skip any extra header information */ |
| 2339 | if (hdr_siz > 12) |
| 2340 | fp_->skip_ahead(hdr_siz - 12); |
| 2341 | |
| 2342 | /* account for having read the header in our size remaining */ |
| 2343 | siz_rem = siz - hdr_siz; |
| 2344 | |
| 2345 | /* read in chunks of VM_STATIC_INIT_PAGE_MAX initializers */ |
| 2346 | for (init_rem = init_cnt ; init_rem != 0 ; ) |
| 2347 | { |
| 2348 | size_t cur; |
| 2349 | CVmStaticInitPage *pg; |
| 2350 | |
| 2351 | /* read the page max, or the actual number remaining if fewer */ |
| 2352 | cur = VM_STATIC_INIT_PAGE_MAX; |
| 2353 | if (init_rem < cur) |
| 2354 | cur = (size_t)init_rem; |
| 2355 | |
| 2356 | /* allocate the next page */ |
| 2357 | pg = new CVmStaticInitPage(cur); |
| 2358 | |
| 2359 | /* link in the page */ |
| 2360 | if (static_tail_ != 0) |
| 2361 | static_tail_->nxt_ = pg; |
| 2362 | else |
| 2363 | static_head_ = pg; |
| 2364 | static_tail_ = pg; |
| 2365 | |
| 2366 | /* read the page */ |
| 2367 | pg->data_ = fp_->alloc_and_read(cur * 6, 0, siz_rem); |
| 2368 | |
| 2369 | /* adjust our counters for the chunk */ |
| 2370 | init_rem -= cur; |
| 2371 | siz_rem -= cur * 6; |
| 2372 | } |
| 2373 | |
| 2374 | /* skip any data we don't use in this version */ |
| 2375 | if (siz_rem != 0) |
| 2376 | fp_->skip_ahead(siz_rem); |
| 2377 | } |
| 2378 | |
| 2379 | /* ------------------------------------------------------------------------ */ |
| 2380 | /* |
| 2381 | * Add a synthesized object export |
| 2382 | */ |
| 2383 | void CVmImageLoader::add_synth_export_obj(const char *nm, vm_obj_id_t obj) |
| 2384 | { |
| 2385 | vm_val_t val; |
| 2386 | |
| 2387 | /* add the new entry with an object value */ |
| 2388 | val.set_obj(obj); |
| 2389 | synth_exports_->add(new CVmHashEntryExport(nm, strlen(nm), TRUE, &val)); |
| 2390 | } |
| 2391 | |
| 2392 | /* |
| 2393 | * Add a synthesized property export |
| 2394 | */ |
| 2395 | void CVmImageLoader::add_synth_export_prop(const char *nm, vm_prop_id_t prop) |
| 2396 | { |
| 2397 | vm_val_t val; |
| 2398 | |
| 2399 | /* add the new entry with a property value */ |
| 2400 | val.set_propid(prop); |
| 2401 | synth_exports_->add(new CVmHashEntryExport(nm, strlen(nm), TRUE, &val)); |
| 2402 | } |
| 2403 | |
| 2404 | /* |
| 2405 | * Discard all synthesized exports. When we're resetting to the image |
| 2406 | * file state, or when we're about to restore a saved state, we must |
| 2407 | * discard the synthesized exports from the prior load so that we start |
| 2408 | * from the base image file state again. |
| 2409 | */ |
| 2410 | void CVmImageLoader::discard_synth_exports() |
| 2411 | { |
| 2412 | /* delete all of the entries in the table */ |
| 2413 | synth_exports_->delete_all_entries(); |
| 2414 | } |
| 2415 | |
| 2416 | /* ------------------------------------------------------------------------ */ |
| 2417 | /* |
| 2418 | * Callback context for enumerating the synthesized export symbols for |
| 2419 | * saving the list to a saved state file |
| 2420 | */ |
| 2421 | struct synth_save_cb_ctx |
| 2422 | { |
| 2423 | /* the file to which we're writing */ |
| 2424 | CVmFile *fp; |
| 2425 | |
| 2426 | /* number of entries */ |
| 2427 | long cnt; |
| 2428 | }; |
| 2429 | |
| 2430 | /* |
| 2431 | * Save the synthesized exports to a saved state file |
| 2432 | */ |
| 2433 | void CVmImageLoader::save_synth_exports(VMG_ CVmFile *fp) |
| 2434 | { |
| 2435 | synth_save_cb_ctx ctx; |
| 2436 | long pos; |
| 2437 | long end_pos; |
| 2438 | |
| 2439 | /* set up our context */ |
| 2440 | ctx.fp = fp; |
| 2441 | ctx.cnt = 0; |
| 2442 | |
| 2443 | /* |
| 2444 | * write a placeholder count of zero, but remember where it is so we |
| 2445 | * can come back and fix it up later |
| 2446 | */ |
| 2447 | pos = fp->get_pos(); |
| 2448 | fp->write_uint4(0); |
| 2449 | |
| 2450 | /* enumerate all of the entries through our save callback */ |
| 2451 | synth_exports_->enum_entries(&save_synth_export_cb, &ctx); |
| 2452 | |
| 2453 | /* go back and fix up the count, then seek back to the end */ |
| 2454 | end_pos = fp->get_pos(); |
| 2455 | fp->set_pos(pos); |
| 2456 | fp->write_uint4(ctx.cnt); |
| 2457 | fp->set_pos(end_pos); |
| 2458 | } |
| 2459 | |
| 2460 | /* |
| 2461 | * Synthesized export enumeration callback - save to file |
| 2462 | */ |
| 2463 | void CVmImageLoader::save_synth_export_cb(void *ctx0, CVmHashEntry *entry0) |
| 2464 | { |
| 2465 | synth_save_cb_ctx *ctx = (synth_save_cb_ctx *)ctx0; |
| 2466 | CVmHashEntryExport *entry = (CVmHashEntryExport *)entry0; |
| 2467 | char buf[VMB_DATAHOLDER]; |
| 2468 | |
| 2469 | /* write this entry's length and name to the file */ |
| 2470 | ctx->fp->write_uint2(entry->getlen()); |
| 2471 | ctx->fp->write_bytes(entry->getstr(), entry->getlen()); |
| 2472 | |
| 2473 | /* write the value as a dataholder */ |
| 2474 | vmb_put_dh(buf, &entry->val_); |
| 2475 | ctx->fp->write_bytes(buf, VMB_DATAHOLDER); |
| 2476 | |
| 2477 | /* count the entry */ |
| 2478 | ++(ctx->cnt); |
| 2479 | } |
| 2480 | |
| 2481 | /* |
| 2482 | * Restore the synthesized exports from a saved state file |
| 2483 | */ |
| 2484 | int CVmImageLoader::restore_synth_exports(VMG_ CVmFile *fp, |
| 2485 | CVmObjFixup *fixups) |
| 2486 | { |
| 2487 | long cnt; |
| 2488 | |
| 2489 | /* |
| 2490 | * discard the old synthesized exports - we want to entirely replace |
| 2491 | * these with what we're about to load from the file |
| 2492 | */ |
| 2493 | G_image_loader->discard_synth_exports(); |
| 2494 | |
| 2495 | /* read the number of synthesized exports */ |
| 2496 | cnt = fp->read_uint4(); |
| 2497 | |
| 2498 | /* read the exports */ |
| 2499 | for ( ; cnt != 0 ; --cnt) |
| 2500 | { |
| 2501 | size_t len; |
| 2502 | char buf[256]; |
| 2503 | char dh[VMB_DATAHOLDER]; |
| 2504 | vm_val_t val; |
| 2505 | |
| 2506 | /* read the length of the name and the name */ |
| 2507 | len = fp->read_uint2(); |
| 2508 | fp->read_bytes(buf, len > sizeof(buf) ? sizeof(buf) : len); |
| 2509 | |
| 2510 | /* skip any part of the name that didn't fit in the buffer */ |
| 2511 | if (len > sizeof(buf)) |
| 2512 | { |
| 2513 | /* skip the extra bytes */ |
| 2514 | fp->set_pos(fp->get_pos() + (len - sizeof(buf))); |
| 2515 | |
| 2516 | /* limit the length we use to the buffer size */ |
| 2517 | len = sizeof(buf); |
| 2518 | } |
| 2519 | |
| 2520 | /* read the value */ |
| 2521 | fp->read_bytes(dh, VMB_DATAHOLDER); |
| 2522 | |
| 2523 | /* if the value is an object reference, fix it up */ |
| 2524 | fixups->fix_dh(vmg_ dh); |
| 2525 | |
| 2526 | /* get the value */ |
| 2527 | vmb_get_dh(dh, &val); |
| 2528 | |
| 2529 | /* add the export */ |
| 2530 | synth_exports_->add(new CVmHashEntryExport(buf, len, TRUE, &val)); |
| 2531 | } |
| 2532 | |
| 2533 | /* success */ |
| 2534 | return 0; |
| 2535 | } |
| 2536 | |
| 2537 | /* ------------------------------------------------------------------------ */ |
| 2538 | /* |
| 2539 | * Allocate a new property ID. |
| 2540 | */ |
| 2541 | vm_prop_id_t CVmImageLoader::alloc_new_prop(VMG0_) |
| 2542 | { |
| 2543 | CVmObjVector *vec; |
| 2544 | vm_val_t idx_val; |
| 2545 | vm_val_t val; |
| 2546 | vm_val_t new_cont; |
| 2547 | |
| 2548 | /* get the LastPropObj vector */ |
| 2549 | vec = (CVmObjVector *)vm_objp(vmg_ G_predef->last_prop_obj); |
| 2550 | |
| 2551 | /* |
| 2552 | * get the element at index 1 - our last property value is always at |
| 2553 | * the first element |
| 2554 | */ |
| 2555 | idx_val.set_int(1); |
| 2556 | |
| 2557 | /* get the value from the vector */ |
| 2558 | vec->index_val(vmg_ &val, G_predef->last_prop_obj, &idx_val); |
| 2559 | |
| 2560 | /* if we've allocated every available property ID, throw an error */ |
| 2561 | if (val.val.prop == 65535) |
| 2562 | err_throw(VMERR_OUT_OF_PROPIDS); |
| 2563 | |
| 2564 | /* allocate the new property ID by incrementing the last used ID */ |
| 2565 | ++val.val.prop; |
| 2566 | |
| 2567 | /* |
| 2568 | * update the vector with the new value (this new property ID is now |
| 2569 | * the last property ID in use) |
| 2570 | */ |
| 2571 | vec->set_index_val(vmg_ &new_cont, G_predef->last_prop_obj, |
| 2572 | &idx_val, &val); |
| 2573 | |
| 2574 | /* return the new last ID */ |
| 2575 | return val.val.prop; |
| 2576 | } |
| 2577 | |
| 2578 | /* |
| 2579 | * Get the last property ID. This returns the last valid property ID |
| 2580 | * currently in use in the program. |
| 2581 | */ |
| 2582 | vm_prop_id_t CVmImageLoader::get_last_prop(VMG0_) |
| 2583 | { |
| 2584 | /* get LastPropObj[1] */ |
| 2585 | vm_val_t vec, ele; |
| 2586 | vec.set_obj(G_predef->last_prop_obj); |
| 2587 | vec.ll_index(vmg_ &ele, 1); |
| 2588 | |
| 2589 | /* return the value */ |
| 2590 | return ele.val.prop; |
| 2591 | } |
| 2592 | |
| 2593 | /* |
| 2594 | * Set the last property ID. This updates the LastPropObj synthetic |
| 2595 | * export object, so that the last property ID is properly integrated with |
| 2596 | * the undo and save/restore mechanisms. |
| 2597 | */ |
| 2598 | void CVmImageLoader::set_last_prop(VMG_ vm_prop_id_t last_prop) |
| 2599 | { |
| 2600 | /* get the LastPropObj vector */ |
| 2601 | CVmObjVector *vec = (CVmObjVector *)vm_objp(vmg_ G_predef->last_prop_obj); |
| 2602 | |
| 2603 | /* |
| 2604 | * set up the values - the index is always 1, since the vector |
| 2605 | * contains only one element; and the value contains the new last |
| 2606 | * property ID |
| 2607 | */ |
| 2608 | vm_val_t idx_val, new_val; |
| 2609 | idx_val.set_int(1); |
| 2610 | new_val.set_propid(G_predef->last_prop); |
| 2611 | |
| 2612 | /* update the vector */ |
| 2613 | vm_val_t new_cont; |
| 2614 | vec->set_index_val(vmg_ &new_cont, G_predef->last_prop_obj, |
| 2615 | &idx_val, &new_val); |
| 2616 | } |
| 2617 | |
| 2618 | /* ------------------------------------------------------------------------ */ |
| 2619 | /* |
| 2620 | * Copy data from the file into a buffer, decrementing a size counter. |
| 2621 | * We'll throw a BLOCK_TOO_SMALL error if the read length exceeds the |
| 2622 | * remaining size. |
| 2623 | */ |
| 2624 | void CVmImageLoader::read_data(char *buf, size_t read_len, |
| 2625 | ulong *remaining_size) |
| 2626 | { |
| 2627 | /* ensure we have enough data left in our block */ |
| 2628 | if (read_len > *remaining_size) |
| 2629 | err_throw(VMERR_IMAGE_BLOCK_TOO_SMALL); |
| 2630 | |
| 2631 | /* decrement the remaining size counter for the data we just read */ |
| 2632 | *remaining_size -= read_len; |
| 2633 | |
| 2634 | /* read the data */ |
| 2635 | fp_->copy_data(buf, read_len); |
| 2636 | } |
| 2637 | |
| 2638 | /* |
| 2639 | * skip data |
| 2640 | */ |
| 2641 | void CVmImageLoader::skip_data(size_t skip_len, ulong *remaining_size) |
| 2642 | { |
| 2643 | /* ensure we have enough data left in our block */ |
| 2644 | if (skip_len > *remaining_size) |
| 2645 | err_throw(VMERR_IMAGE_BLOCK_TOO_SMALL); |
| 2646 | |
| 2647 | /* decrement the remaining size counter for the data we're skipping */ |
| 2648 | *remaining_size -= skip_len; |
| 2649 | |
| 2650 | /* skip ahead in the underlying file */ |
| 2651 | fp_->skip_ahead(skip_len); |
| 2652 | } |
| 2653 | |
| 2654 | /* |
| 2655 | * Allocate memory for data and read the data from the file, |
| 2656 | * decrementing the amount read from a size counter. Throws |
| 2657 | * BLOCK_TOO_SMALL if the read length exceeds the remaining size. |
| 2658 | */ |
| 2659 | const char *CVmImageLoader::alloc_and_read(size_t read_len, |
| 2660 | ulong *remaining_size) |
| 2661 | { |
| 2662 | const char *p; |
| 2663 | |
| 2664 | /* ensure we have enough data left in our block */ |
| 2665 | if (read_len > *remaining_size) |
| 2666 | err_throw(VMERR_IMAGE_BLOCK_TOO_SMALL); |
| 2667 | |
| 2668 | /* allocate space, and read the data into the new space */ |
| 2669 | p = fp_->alloc_and_read(read_len, 0, *remaining_size); |
| 2670 | |
| 2671 | /* decrement the remaining size counter for the data we just read */ |
| 2672 | *remaining_size -= read_len; |
| 2673 | |
| 2674 | /* return the pointer to the new space */ |
| 2675 | return p; |
| 2676 | } |
| 2677 | |
| 2678 | |
| 2679 | /* ------------------------------------------------------------------------ */ |
| 2680 | /* |
| 2681 | * Image Pool tracker implementation |
| 2682 | */ |
| 2683 | |
| 2684 | CVmImagePool::CVmImagePool() |
| 2685 | { |
| 2686 | /* we don't have any information about the pool yet */ |
| 2687 | page_count_ = 0; |
| 2688 | page_size_ = 0; |
| 2689 | page_info_ = 0; |
| 2690 | fp_ = 0; |
| 2691 | } |
| 2692 | |
| 2693 | CVmImagePool::~CVmImagePool() |
| 2694 | { |
| 2695 | /* if we allocated a set of page offset arrays, delete them */ |
| 2696 | if (page_info_ != 0) |
| 2697 | { |
| 2698 | size_t i; |
| 2699 | size_t cnt; |
| 2700 | |
| 2701 | /* compute the number of subarrays we have */ |
| 2702 | cnt = get_subarray_count(); |
| 2703 | |
| 2704 | /* delete each subarray */ |
| 2705 | for (i = 0 ; i < cnt ; ++i) |
| 2706 | t3free(page_info_[i]); |
| 2707 | |
| 2708 | /* delete the master array */ |
| 2709 | t3free(page_info_); |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | /* |
| 2714 | * initialize |
| 2715 | */ |
| 2716 | void CVmImagePool::init(CVmImageFile *fp, ulong page_count, ulong page_size) |
| 2717 | { |
| 2718 | size_t cnt; |
| 2719 | size_t i; |
| 2720 | |
| 2721 | /* remember the page count and page size */ |
| 2722 | page_count_ = page_count; |
| 2723 | page_size_ = page_size; |
| 2724 | |
| 2725 | /* remember the underlying image file */ |
| 2726 | fp_ = fp; |
| 2727 | |
| 2728 | /* if this pool has already been defined, throw an error */ |
| 2729 | if (page_info_ != 0) |
| 2730 | err_throw(VMERR_IMAGE_POOL_REDEF); |
| 2731 | |
| 2732 | /* |
| 2733 | * Allocate the main page array. We need one entry for each |
| 2734 | * subarray, so figure the subarray count and allocate the |
| 2735 | * appropriate amount of space. |
| 2736 | */ |
| 2737 | cnt = get_subarray_count(); |
| 2738 | page_info_ = (CVmImagePool_pg **)t3malloc(cnt * sizeof(page_info_[0])); |
| 2739 | |
| 2740 | /* throw an error if that failed */ |
| 2741 | if (page_info_ == 0) |
| 2742 | err_throw(VMERR_OUT_OF_MEMORY); |
| 2743 | |
| 2744 | /* clear the array */ |
| 2745 | memset(page_info_, 0, cnt * sizeof(page_info_[0])); |
| 2746 | |
| 2747 | /* allocate the subarrays */ |
| 2748 | for (i = 0 ; i < cnt ; ++i) |
| 2749 | { |
| 2750 | /* allocate this page, which contains file offsets */ |
| 2751 | page_info_[i] = |
| 2752 | (CVmImagePool_pg *)t3malloc(VMIMAGE_POOL_SUBARRAY_SIZE |
| 2753 | * sizeof(page_info_[i][0])); |
| 2754 | |
| 2755 | /* throw an error if that failed */ |
| 2756 | if (page_info_[i] == 0) |
| 2757 | err_throw(VMERR_OUT_OF_MEMORY); |
| 2758 | |
| 2759 | /* clear the memory */ |
| 2760 | memset(page_info_[i], 0, |
| 2761 | VMIMAGE_POOL_SUBARRAY_SIZE * sizeof(page_info_[i][0])); |
| 2762 | } |
| 2763 | } |
| 2764 | |
| 2765 | /* |
| 2766 | * Set a page seek offset |
| 2767 | */ |
| 2768 | void CVmImagePool::set_page_info(ulong page_idx, long seek_pos, |
| 2769 | size_t page_size, uchar xor_mask) |
| 2770 | { |
| 2771 | CVmImagePool_pg *info; |
| 2772 | |
| 2773 | /* |
| 2774 | * make sure we've allocated the page array and that this index is |
| 2775 | * in range -- throw an error if not |
| 2776 | */ |
| 2777 | if (page_info_ == 0) |
| 2778 | err_throw(VMERR_IMAGE_POOL_BEFORE_DEF); |
| 2779 | if (page_idx >= page_count_) |
| 2780 | err_throw(VMERR_IMAGE_POOL_BAD_PAGE); |
| 2781 | |
| 2782 | /* get the entry for this page */ |
| 2783 | info = get_page_info(page_idx); |
| 2784 | |
| 2785 | /* set the information */ |
| 2786 | info->seek_pos = seek_pos; |
| 2787 | info->page_size = page_size; |
| 2788 | info->xor_mask = xor_mask; |
| 2789 | } |
| 2790 | |
| 2791 | /* |
| 2792 | * allocate and load a page |
| 2793 | */ |
| 2794 | const char *CVmImagePool:: |
| 2795 | vmpbs_alloc_and_load_page(pool_ofs_t ofs, size_t /*page_size*/, |
| 2796 | size_t load_size) |
| 2797 | { |
| 2798 | CVmImagePool_pg *info; |
| 2799 | |
| 2800 | /* get the page information */ |
| 2801 | info = get_page_info_ofs(ofs); |
| 2802 | |
| 2803 | /* seek to the correct location in the image file */ |
| 2804 | seek_page_ofs(ofs); |
| 2805 | |
| 2806 | /* ask the underlying file to load the data */ |
| 2807 | return fp_->alloc_and_read(load_size, info->xor_mask, load_size); |
| 2808 | } |
| 2809 | |
| 2810 | /* |
| 2811 | * load a page into the given memory block |
| 2812 | */ |
| 2813 | void CVmImagePool::vmpbs_load_page(pool_ofs_t ofs, size_t /*page_size*/, |
| 2814 | size_t load_size, char *mem) |
| 2815 | { |
| 2816 | CVmImagePool_pg *info; |
| 2817 | |
| 2818 | /* get the page information */ |
| 2819 | info = get_page_info_ofs(ofs); |
| 2820 | |
| 2821 | /* seek to the correct location in the image file */ |
| 2822 | seek_page_ofs(ofs); |
| 2823 | |
| 2824 | /* ask the underlying file to load the data */ |
| 2825 | fp_->copy_data(mem, load_size); |
| 2826 | |
| 2827 | /* apply the XOR mask to the loaded data */ |
| 2828 | apply_xor_mask(mem, load_size, info->xor_mask); |
| 2829 | } |
| 2830 | |
| 2831 | /* |
| 2832 | * free a page previously loaded |
| 2833 | */ |
| 2834 | void CVmImagePool::vmpbs_free_page(const char *mem, pool_ofs_t /*ofs*/, |
| 2835 | size_t /*page_size*/) |
| 2836 | { |
| 2837 | /* tell the file to free the memory */ |
| 2838 | fp_->free_mem(mem); |
| 2839 | } |
| 2840 | |
| 2841 | /* |
| 2842 | * Determine if the backing store pages are writable. Let the |
| 2843 | * underlying file decide. |
| 2844 | */ |
| 2845 | int CVmImagePool::vmpbs_is_writable() |
| 2846 | { |
| 2847 | /* |
| 2848 | * if the underlying file allows writing to its allocated pages, |
| 2849 | * allow writing |
| 2850 | */ |
| 2851 | return fp_->allow_write_to_alloc(); |
| 2852 | } |
| 2853 | |
| 2854 | /* |
| 2855 | * seek to the image file data for the page at the given pool offset |
| 2856 | */ |
| 2857 | void CVmImagePool::seek_page_ofs(pool_ofs_t ofs) |
| 2858 | { |
| 2859 | ulong page_idx; |
| 2860 | CVmImagePool_pg *info; |
| 2861 | |
| 2862 | /* |
| 2863 | * get the page index - pages are all of a fixed length, so we can |
| 2864 | * find the page index simply by dividing the offset by the page |
| 2865 | * size |
| 2866 | */ |
| 2867 | page_idx = ofs / page_size_; |
| 2868 | |
| 2869 | /* make sure the page is valid */ |
| 2870 | if (page_idx >= page_count_) |
| 2871 | err_throw(VMERR_LOAD_BAD_PAGE_IDX); |
| 2872 | |
| 2873 | /* make sure the page has been defined */ |
| 2874 | info = get_page_info(page_idx); |
| 2875 | |
| 2876 | /* make sure this page's information has been loaded */ |
| 2877 | if (info->seek_pos == 0) |
| 2878 | err_throw(VMERR_LOAD_UNDEF_PAGE); |
| 2879 | |
| 2880 | /* seek to this block's position in the file */ |
| 2881 | fp_->seek(info->seek_pos); |
| 2882 | } |
| 2883 | |
| 2884 | |
| 2885 | /* ------------------------------------------------------------------------ */ |
| 2886 | /* |
| 2887 | * Image file interface - external disk file implementation |
| 2888 | */ |
| 2889 | |
| 2890 | /* |
| 2891 | * Delete the image file loader. This deletes all of the memory |
| 2892 | * associated with loaded objects, so the image file loader must not be |
| 2893 | * deleted until all of the loaded root-set objects are deleted. |
| 2894 | */ |
| 2895 | CVmImageFileExt::~CVmImageFileExt() |
| 2896 | { |
| 2897 | CVmImageFileExt_blk *cur; |
| 2898 | CVmImageFileExt_blk *nxt; |
| 2899 | |
| 2900 | /* run through the list of allocated blocks and delete each one */ |
| 2901 | for (cur = mem_head_ ; cur != 0 ; cur = nxt) |
| 2902 | { |
| 2903 | /* remember the next block, since we're deleting this one */ |
| 2904 | nxt = cur->nxt_; |
| 2905 | |
| 2906 | /* delete this one */ |
| 2907 | delete cur; |
| 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | |
| 2912 | /* |
| 2913 | * copy data to the caller's buffer |
| 2914 | */ |
| 2915 | void CVmImageFileExt::copy_data(char *buf, size_t len) |
| 2916 | { |
| 2917 | /* read directly from the file into the caller's buffer */ |
| 2918 | fp_->read_bytes(buf, len); |
| 2919 | } |
| 2920 | |
| 2921 | /* |
| 2922 | * allocate memory for and read data |
| 2923 | */ |
| 2924 | const char *CVmImageFileExt::alloc_and_read(size_t len, uchar xor_mask, |
| 2925 | ulong remaining_in_page) |
| 2926 | { |
| 2927 | char *mem; |
| 2928 | |
| 2929 | /* allocate memory */ |
| 2930 | mem = alloc_mem(len, remaining_in_page); |
| 2931 | if (mem == 0) |
| 2932 | err_throw(VMERR_OUT_OF_MEMORY); |
| 2933 | |
| 2934 | /* read the data */ |
| 2935 | fp_->read_bytes(mem, len); |
| 2936 | |
| 2937 | /* if there's an XOR mask, apply it to each byte we read */ |
| 2938 | CVmImagePool::apply_xor_mask(mem, len, xor_mask); |
| 2939 | |
| 2940 | /* return the memory block */ |
| 2941 | return mem; |
| 2942 | } |
| 2943 | |
| 2944 | /* |
| 2945 | * seek |
| 2946 | */ |
| 2947 | void CVmImageFileExt::seek(long pos) |
| 2948 | { |
| 2949 | /* seek to the given position in the underlying file */ |
| 2950 | fp_->set_pos(pos); |
| 2951 | } |
| 2952 | |
| 2953 | /* |
| 2954 | * get current seek position |
| 2955 | */ |
| 2956 | long CVmImageFileExt::get_seek() const |
| 2957 | { |
| 2958 | /* get the position from the underlying file */ |
| 2959 | return fp_->get_pos(); |
| 2960 | } |
| 2961 | |
| 2962 | /* |
| 2963 | * skip bytes |
| 2964 | */ |
| 2965 | void CVmImageFileExt::skip_ahead(long len) |
| 2966 | { |
| 2967 | /* seek ahead in the underlying file */ |
| 2968 | fp_->set_pos_from_cur(len); |
| 2969 | } |
| 2970 | |
| 2971 | /* |
| 2972 | * Allocate data for loading |
| 2973 | */ |
| 2974 | char *CVmImageFileExt::alloc_mem(size_t siz, ulong remaining_in_page) |
| 2975 | { |
| 2976 | /* |
| 2977 | * If the requested size is more than an eigth of our block size, |
| 2978 | * give the new allocation its own block. Our blocks are intended |
| 2979 | * to reduce overhead for small blocks; large blocks not only won't |
| 2980 | * create a lot of overhead as individual "malloc" allocations, but |
| 2981 | * would probably leave our block underutilized by leaving a lot of |
| 2982 | * it free, defeating the purpose. |
| 2983 | */ |
| 2984 | if (siz > VMIMAGE_EXT_BLK_SIZE / 8) |
| 2985 | { |
| 2986 | CVmImageFileExt_blk *new_block; |
| 2987 | |
| 2988 | /* it's a big request, so give it its own block */ |
| 2989 | new_block = new CVmImageFileExt_blk(siz); |
| 2990 | |
| 2991 | /* |
| 2992 | * link it at the tail of the list (we don't want to suballocate |
| 2993 | * anything more out of this, obviously, since we're going to |
| 2994 | * consume the entire block, but we do want to keep it in our |
| 2995 | * list so that we can track and eventually delete the memory) |
| 2996 | */ |
| 2997 | new_block->nxt_ = 0; |
| 2998 | new_block->prv_ = mem_tail_; |
| 2999 | |
| 3000 | /* set the forward pointer of the previous entry */ |
| 3001 | if (mem_tail_ != 0) |
| 3002 | mem_tail_->nxt_ = new_block; |
| 3003 | else |
| 3004 | mem_head_ = new_block; |
| 3005 | |
| 3006 | /* it's the new tail */ |
| 3007 | mem_tail_ = new_block; |
| 3008 | |
| 3009 | /* "suballocate" out of the new block */ |
| 3010 | return mem_tail_->suballoc(siz); |
| 3011 | } |
| 3012 | else |
| 3013 | { |
| 3014 | /* |
| 3015 | * It's a small request, so suballocate it out of a block. |
| 3016 | * First, make sure we have space in the current block; if not, |
| 3017 | * allocate a new block. |
| 3018 | */ |
| 3019 | if (mem_head_ == 0 || siz > mem_head_->rem_) |
| 3020 | { |
| 3021 | CVmImageFileExt_blk *new_block; |
| 3022 | size_t new_block_size; |
| 3023 | |
| 3024 | /* |
| 3025 | * There's no space left in the current block - allocate a new |
| 3026 | * block. Leave space in the new block for further |
| 3027 | * allocations, so we don't have to allocate lots of little |
| 3028 | * blocks; however, as an upper bound, allocate only as much |
| 3029 | * space as remains in the current page being read out of the |
| 3030 | * image file, so that we don't leave a bunch of unused space |
| 3031 | * after reading the last objects from the image. |
| 3032 | */ |
| 3033 | new_block_size = VMIMAGE_EXT_BLK_SIZE; |
| 3034 | if (new_block_size > remaining_in_page) |
| 3035 | new_block_size = (size_t)remaining_in_page; |
| 3036 | |
| 3037 | /* allocate the new block */ |
| 3038 | new_block = new CVmImageFileExt_blk(new_block_size); |
| 3039 | |
| 3040 | /* link it in at the head of our list */ |
| 3041 | new_block->prv_ = 0; |
| 3042 | new_block->nxt_ = mem_head_; |
| 3043 | |
| 3044 | /* set the back pointer of the next entry */ |
| 3045 | if (mem_head_ != 0) |
| 3046 | mem_head_->prv_ = new_block; |
| 3047 | else |
| 3048 | mem_tail_ = new_block; |
| 3049 | |
| 3050 | /* it's the new head */ |
| 3051 | mem_head_ = new_block; |
| 3052 | } |
| 3053 | |
| 3054 | /* suballocate it */ |
| 3055 | return mem_head_->suballoc(siz); |
| 3056 | } |
| 3057 | } |
| 3058 | |
| 3059 | |
| 3060 | /* ------------------------------------------------------------------------ */ |
| 3061 | /* |
| 3062 | * Memory tracker for external file loader |
| 3063 | */ |
| 3064 | |
| 3065 | CVmImageFileExt_blk::CVmImageFileExt_blk(size_t siz) |
| 3066 | { |
| 3067 | /* allocate the associated memory block */ |
| 3068 | block_ptr_ = (char *)t3malloc(siz); |
| 3069 | |
| 3070 | /* remember the amount of space remaining */ |
| 3071 | rem_ = siz; |
| 3072 | |
| 3073 | /* the next byte free is the first byte */ |
| 3074 | free_ptr_ = block_ptr_; |
| 3075 | |
| 3076 | /* nothing else is in our list yet */ |
| 3077 | nxt_ = prv_ = 0; |
| 3078 | } |
| 3079 | |
| 3080 | CVmImageFileExt_blk::~CVmImageFileExt_blk() |
| 3081 | { |
| 3082 | /* free our memory block */ |
| 3083 | t3free(block_ptr_); |
| 3084 | } |
| 3085 | |
| 3086 | char *CVmImageFileExt_blk::suballoc(size_t siz) |
| 3087 | { |
| 3088 | char *ret; |
| 3089 | |
| 3090 | /* if we don't have enough memory available, fail the request */ |
| 3091 | if (rem_ < siz) |
| 3092 | return 0; |
| 3093 | |
| 3094 | /* our return value will be the current free pointer */ |
| 3095 | ret = free_ptr_; |
| 3096 | |
| 3097 | /* move the free pointer past the allocated block */ |
| 3098 | free_ptr_ += siz; |
| 3099 | |
| 3100 | /* deduct the amount we just allocated from the space available */ |
| 3101 | rem_ -= siz; |
| 3102 | |
| 3103 | /* return the memory */ |
| 3104 | return ret; |
| 3105 | } |
| 3106 | |
| 3107 | /* ------------------------------------------------------------------------ */ |
| 3108 | /* |
| 3109 | * Image file interface - memory-mapped file implementation |
| 3110 | */ |
| 3111 | |
| 3112 | /* |
| 3113 | * copy data to the caller's buffer |
| 3114 | */ |
| 3115 | void CVmImageFileMem::copy_data(char *buf, size_t len) |
| 3116 | { |
| 3117 | /* if we're past the end of the file, throw an error */ |
| 3118 | if (pos_ + len > len_) |
| 3119 | err_throw(VMERR_READ_PAST_IMG_END); |
| 3120 | |
| 3121 | /* copy data into the caller's buffer */ |
| 3122 | memcpy(buf, mem_ + pos_, len); |
| 3123 | |
| 3124 | /* seek past the data */ |
| 3125 | pos_ += len; |
| 3126 | } |
| 3127 | |
| 3128 | /* |
| 3129 | * allocate memory for and read data |
| 3130 | */ |
| 3131 | const char *CVmImageFileMem::alloc_and_read(size_t len, uchar xor_mask, |
| 3132 | ulong /*remaining_in_page*/) |
| 3133 | { |
| 3134 | const char *ret; |
| 3135 | |
| 3136 | /* if we're past the end of the file, throw an error */ |
| 3137 | if (pos_ + len > len_) |
| 3138 | err_throw(VMERR_READ_PAST_IMG_END); |
| 3139 | |
| 3140 | /* the data are already in memory - figure out where */ |
| 3141 | ret = mem_ + pos_; |
| 3142 | |
| 3143 | /* seek past the data */ |
| 3144 | pos_ += len; |
| 3145 | |
| 3146 | /* |
| 3147 | * we can't apply an xor mask to an in-memory page - if the mask is |
| 3148 | * non-zero, it's an error |
| 3149 | */ |
| 3150 | if (xor_mask != 0) |
| 3151 | err_throw(VMERR_XOR_MASK_BAD_IN_MEM); |
| 3152 | |
| 3153 | /* return the pointer */ |
| 3154 | return ret; |
| 3155 | } |
| 3156 | |
| 3157 | /* ------------------------------------------------------------------------ */ |
| 3158 | /* |
| 3159 | * Generic stream implementation for an image file block |
| 3160 | */ |
| 3161 | |
| 3162 | /* |
| 3163 | * read bytes from the stream |
| 3164 | */ |
| 3165 | void CVmImageFileStream::read_bytes(char *buf, size_t len) |
| 3166 | { |
| 3167 | /* if we don't have enough data to satisfy the request, it's an error */ |
| 3168 | if (len > len_) |
| 3169 | err_throw(VMERR_IMAGE_BLOCK_TOO_SMALL); |
| 3170 | |
| 3171 | /* deduct the space we're reading from the remaining data size */ |
| 3172 | len_ -= len; |
| 3173 | |
| 3174 | /* copy the data from the image file to the caller's buffer */ |
| 3175 | fp_->copy_data(buf, len); |
| 3176 | } |
| 3177 | |
| 3178 | /* |
| 3179 | * read bytes, allowing short reads without error |
| 3180 | */ |
| 3181 | size_t CVmImageFileStream::read_nbytes(char *buf, size_t len) |
| 3182 | { |
| 3183 | /* limit the read to the available size */ |
| 3184 | if (len > len_) |
| 3185 | len = len_; |
| 3186 | |
| 3187 | /* deduct the space we're reading from the remaining data size */ |
| 3188 | len_ -= len; |
| 3189 | |
| 3190 | /* copy the data from the image file to the caller's buffer */ |
| 3191 | if (len != 0) |
| 3192 | fp_->copy_data(buf, len); |
| 3193 | |
| 3194 | /* return the length actually read */ |
| 3195 | return len; |
| 3196 | } |
| 3197 | |
| 3198 | /* |
| 3199 | * write bytes to the stream |
| 3200 | */ |
| 3201 | void CVmImageFileStream::write_bytes(const char *, size_t) |
| 3202 | { |
| 3203 | /* this is a read-only stream, so writing isn't allowed */ |
| 3204 | err_throw(VMERR_WRITE_FILE); |
| 3205 | } |