| 1 | /* $Header: d:/cvsroot/tads/tads3/vmimage.h,v 1.3 1999/07/11 00:46:59 MJRoberts Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | vmimage.h - VM image file loader |
| 12 | Function |
| 13 | Loads an image file for execution. |
| 14 | |
| 15 | The VM loads and executes an image by creating an appropriate |
| 16 | concrete CVmImageFile subclass, then creating a CVmImageLoader using |
| 17 | the CVmImageFile object. CVmImageLoader parses the input file, sets |
| 18 | up page mappings for constant pools, and initializes objects found in |
| 19 | the file. |
| 20 | |
| 21 | The lifespan of the CVmImageLoader object is the lifespan of the |
| 22 | loaded image. As long as the VM is executing the image, it must keep |
| 23 | the CVmImageLoader object in existence. The CVmImageLoader object |
| 24 | can be deleted after the VM terminates execution of the image. |
| 25 | |
| 26 | The loader comes in two varieties: an external file loader, and a |
| 27 | memory-mapped file loader. The external file loader reads data from |
| 28 | a disk file, allocating memory for the information read from the file. |
| 29 | The memory-mapped file loader takes a chunk of memory that's already |
| 30 | been loaded, and initializes the VM to use the pre-allocated memory, |
| 31 | rather than allocating a separate copy of the image data. |
| 32 | |
| 33 | The memory-mapped loader is meant for systems with no external storage, |
| 34 | such as hand-held devices. It can also be used on systems with large, |
| 35 | flat address spaces to speed up loading by isolating all disk access |
| 36 | into a single bulk load of the image into memory. |
| 37 | |
| 38 | The external file loader is useful for systems with smaller address |
| 39 | spaces, and can be used with a swapping pool implementation to allow |
| 40 | the VM to operate when available memory is smaller than the image |
| 41 | file. |
| 42 | Notes |
| 43 | |
| 44 | Modified |
| 45 | 12/12/98 MJRoberts - Creation |
| 46 | */ |
| 47 | |
| 48 | #ifndef VMIMAGE_H |
| 49 | #define VMIMAGE_H |
| 50 | |
| 51 | #include <memory.h> |
| 52 | |
| 53 | #include "vmpool.h" |
| 54 | #include "vmglob.h" |
| 55 | #include "vmtype.h" |
| 56 | #include "vmfile.h" |
| 57 | |
| 58 | |
| 59 | /* ------------------------------------------------------------------------ */ |
| 60 | /* |
| 61 | * Image file constants |
| 62 | */ |
| 63 | |
| 64 | /* |
| 65 | * signature - this is at the beginning of every image file so that we |
| 66 | * can easily detect a completely invalid file |
| 67 | */ |
| 68 | #define VMIMAGE_SIG "T3-image\015\012\032" |
| 69 | |
| 70 | |
| 71 | /* ------------------------------------------------------------------------ */ |
| 72 | /* |
| 73 | * Data Block Flags |
| 74 | */ |
| 75 | |
| 76 | /* |
| 77 | * Mandatory: this block must be recognized and loaded. When new types |
| 78 | * of blocks are added in future versions, the compiler can use this |
| 79 | * flag to indicate whether or not an old VM can safely ignore the new |
| 80 | * block type. An old VM will not be able to load a new block, since |
| 81 | * that new block won't have been part of the spec the VM was designed |
| 82 | * for; in some cases, an image file can be successfully loaded and used |
| 83 | * even when a particular block is ignored (possibly with the loss of |
| 84 | * the new feature enabled by the data in the block). When a block can |
| 85 | * be ignored by an old VM without losing the ability to correctly load |
| 86 | * the image for the old VM's functionality, the mandatory flag will be |
| 87 | * set to 0; when a new block carries information without which the |
| 88 | * image cannot be properly loaded, the mandatory flag will be set to 1. |
| 89 | */ |
| 90 | #define VMIMAGE_DBF_MANDATORY 0x0001 |
| 91 | |
| 92 | |
| 93 | /* ------------------------------------------------------------------------ */ |
| 94 | /* |
| 95 | * Constant pool image tracking structure. For each constant pool, we |
| 96 | * maintain information on the locations in the image file of the pages |
| 97 | * in the pool. |
| 98 | */ |
| 99 | |
| 100 | /* number of entries in each subarray */ |
| 101 | const size_t VMIMAGE_POOL_SUBARRAY_SIZE = 4096; |
| 102 | |
| 103 | /* page information structure */ |
| 104 | struct CVmImagePool_pg |
| 105 | { |
| 106 | /* seek offset of the page */ |
| 107 | long seek_pos; |
| 108 | |
| 109 | /* size of the page */ |
| 110 | size_t page_size; |
| 111 | |
| 112 | /* XOR mask for the page */ |
| 113 | uchar xor_mask; |
| 114 | }; |
| 115 | |
| 116 | class CVmImagePool: public CVmPoolBackingStore |
| 117 | { |
| 118 | public: |
| 119 | CVmImagePool(); |
| 120 | ~CVmImagePool(); |
| 121 | |
| 122 | /* |
| 123 | * Initialize the pool with a given number of pages and page size. |
| 124 | * This can only be called once, and must be called before any page |
| 125 | * locations can be established. |
| 126 | */ |
| 127 | void init(class CVmImageFile *fp, ulong page_count, ulong page_size); |
| 128 | |
| 129 | /* set a page's information */ |
| 130 | void set_page_info(ulong page_idx, long seek_pos, size_t page_size, |
| 131 | uchar xor_mask); |
| 132 | |
| 133 | /* apply an XOR mask to a block of bytes */ |
| 134 | static void apply_xor_mask(char *p, size_t len, uchar xor_mask) |
| 135 | { |
| 136 | /* |
| 137 | * apply the mask only if it's non-zero - xor'ing zero with |
| 138 | * anything yields the original value, so we can avoid a lot of |
| 139 | * pointless memory traversal by checking this first |
| 140 | */ |
| 141 | if (xor_mask != 0) |
| 142 | { |
| 143 | /* xor each byte with the xor mask */ |
| 144 | for ( ; len != 0 ; --len, ++p) |
| 145 | *p ^= xor_mask; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /* -------------------------------------------------------------------- */ |
| 150 | /* |
| 151 | * CVmPoolBackingStore implementation |
| 152 | */ |
| 153 | |
| 154 | /* get the total number of pages */ |
| 155 | size_t vmpbs_get_page_count() { return page_count_; } |
| 156 | |
| 157 | /* get the common page size */ |
| 158 | size_t vmpbs_get_common_page_size() { return page_size_; } |
| 159 | |
| 160 | /* get the size of a given page */ |
| 161 | size_t vmpbs_get_page_size(pool_ofs_t ofs, size_t page_size) |
| 162 | { |
| 163 | return get_page_info_ofs(ofs)->page_size; |
| 164 | } |
| 165 | |
| 166 | /* allocate and load a given page */ |
| 167 | const char *vmpbs_alloc_and_load_page(pool_ofs_t ofs, size_t page_size, |
| 168 | size_t load_size); |
| 169 | |
| 170 | /* free a page */ |
| 171 | void vmpbs_free_page(const char *mem, pool_ofs_t ofs, size_t page_size); |
| 172 | |
| 173 | /* load a page into a given memory block */ |
| 174 | void vmpbs_load_page(pool_ofs_t ofs, size_t page_size, |
| 175 | size_t load_size, char *mem); |
| 176 | |
| 177 | /* determine if the backing store pages are writable */ |
| 178 | int vmpbs_is_writable(); |
| 179 | |
| 180 | |
| 181 | /* -------------------------------------------------------------------- */ |
| 182 | |
| 183 | private: |
| 184 | /* compute the number of subarray pages we have */ |
| 185 | size_t get_subarray_count() const |
| 186 | { |
| 187 | return (size_t)((page_count_ + VMIMAGE_POOL_SUBARRAY_SIZE - 1) |
| 188 | / VMIMAGE_POOL_SUBARRAY_SIZE); |
| 189 | } |
| 190 | |
| 191 | /* given a page index, get the information structure at the index */ |
| 192 | CVmImagePool_pg *get_page_info(ulong idx) const |
| 193 | { |
| 194 | return &(page_info_[idx / VMIMAGE_POOL_SUBARRAY_SIZE] |
| 195 | [idx % VMIMAGE_POOL_SUBARRAY_SIZE]); |
| 196 | } |
| 197 | |
| 198 | /* given a pool offset, get the information structure for the page */ |
| 199 | CVmImagePool_pg *get_page_info_ofs(pool_ofs_t ofs) const |
| 200 | { return get_page_info(ofs / page_size_); } |
| 201 | |
| 202 | /* |
| 203 | * Given a pool offset, seek to the image file data for the page, in |
| 204 | * preparation for loading the data from the image file into memory. |
| 205 | */ |
| 206 | void seek_page_ofs(pool_ofs_t ofs); |
| 207 | |
| 208 | /* number of pages in the pool */ |
| 209 | ulong page_count_; |
| 210 | |
| 211 | /* page size - each page in the pool has a common size */ |
| 212 | ulong page_size_; |
| 213 | |
| 214 | /* |
| 215 | * Page seek array. To accommodate 16-bit platforms, we keep this as a |
| 216 | * set of arrays, with each subarray smaller than 64k. |
| 217 | */ |
| 218 | CVmImagePool_pg **page_info_; |
| 219 | |
| 220 | /* underlying image file */ |
| 221 | class CVmImageFile *fp_; |
| 222 | }; |
| 223 | |
| 224 | /* ------------------------------------------------------------------------ */ |
| 225 | /* |
| 226 | * Image loader. This takes an image file interface object (see below), |
| 227 | * and loads the underlying image data into memory. |
| 228 | */ |
| 229 | class CVmImageLoader |
| 230 | { |
| 231 | public: |
| 232 | /* initialize with an image file interface */ |
| 233 | CVmImageLoader(class CVmImageFile *fp, const char *fname, long base_ofs); |
| 234 | |
| 235 | /* destruction */ |
| 236 | ~CVmImageLoader(); |
| 237 | |
| 238 | /* |
| 239 | * Load the image. |
| 240 | */ |
| 241 | void load(VMG0_); |
| 242 | |
| 243 | /* |
| 244 | * Load a resource-only image file. 'fileno' is the file number |
| 245 | * assigned by the host application (via the add_resfile() |
| 246 | * interface) to the resource file. |
| 247 | */ |
| 248 | void load_resource_file(class CVmImageLoaderMres *res_ifc); |
| 249 | |
| 250 | /* load resources from a file handle at the current seek location */ |
| 251 | static void load_resources_from_fp(osfildef *fp, const char *fname, |
| 252 | class CVmHostIfc *hostifc); |
| 253 | |
| 254 | /* load resources from a file handle at the current seek location */ |
| 255 | static void load_resources_from_fp(osfildef *fp, const char *fname, |
| 256 | class CVmImageLoaderMres *res_ifc); |
| 257 | |
| 258 | /* |
| 259 | * Run the image. This transfers control to the entrypoint defined in |
| 260 | * the image file. This function doesn't return until the program |
| 261 | * defined in the image terminates its execution by returning from the |
| 262 | * entrypoint function or throwing an unhandled exception. |
| 263 | * |
| 264 | * If 'saved_state' is not null, it gives a null-terminated character |
| 265 | * string with the name of a saved state file to be restored |
| 266 | * immediately. We'll pass this information to the program's |
| 267 | * entrypoint so it can handle the restore appropriately. |
| 268 | * |
| 269 | * The caller must create the code and constant pools before invoking |
| 270 | * this. We'll set up the pools with their backing stores as loaded |
| 271 | * from the image file. |
| 272 | * |
| 273 | * The global_symtab argument optionally provides the global symbol |
| 274 | * table; if this is null, we'll use our own global symbol table that |
| 275 | * we loaded from the debug records, if we found any. |
| 276 | * |
| 277 | * If an unhandled exception is thrown, this function throws |
| 278 | * VMERR_UNHANDLED_EXC, with the exception object as the first |
| 279 | * parameter. |
| 280 | */ |
| 281 | void run(VMG_ const char *const *argv, int argc, |
| 282 | class CVmRuntimeSymbols *global_symtab, |
| 283 | class CVmRuntimeSymbols *macro_symtab, |
| 284 | const char *saved_state); |
| 285 | |
| 286 | /* run all static initializers */ |
| 287 | void run_static_init(VMG0_); |
| 288 | |
| 289 | /* |
| 290 | * Unload the image. This should be called after execution is |
| 291 | * finished to disactivate the pools, which must be done before the |
| 292 | * image file is deleted. |
| 293 | */ |
| 294 | void unload(VMG0_); |
| 295 | |
| 296 | /* |
| 297 | * Create a global LookupTable to hold the symbols in the global |
| 298 | * symbol table. |
| 299 | */ |
| 300 | void create_global_symtab_lookup_table(VMG0_); |
| 301 | |
| 302 | /* create a global LookupTable to hold the symbols in the macro table */ |
| 303 | void create_macro_symtab_lookup_table(VMG0_); |
| 304 | |
| 305 | /* determine if the given block type identifiers match */ |
| 306 | static int block_type_is(const char *type1, const char *type2) |
| 307 | { |
| 308 | /* compare the four-byte identifiers to see if they're identical */ |
| 309 | return (memcmp(type1, type2, 4) == 0); |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Get the image file's timestamp. This is a 24-byte array in the |
| 314 | * format "Sun Aug 01 17:05:20 1999". The purpose of the image file |
| 315 | * timestamp is to provide a reasonably unique identifier that can |
| 316 | * be stored in a saved state file and then checked upon loading the |
| 317 | * file to ensure that it was created by the identical version of |
| 318 | * the image file. |
| 319 | */ |
| 320 | const char *get_timestamp() const { return ×tamp_[0]; } |
| 321 | |
| 322 | /* get the filename of the loaded image */ |
| 323 | const char *get_filename() const { return fname_; } |
| 324 | |
| 325 | /* get the fully-qualified, absolute directory path of the loaded image */ |
| 326 | const char *get_path() const { return path_; } |
| 327 | |
| 328 | /* |
| 329 | * check to see if the image file has a global symbol table (GSYM |
| 330 | * block) |
| 331 | */ |
| 332 | int has_gsym() const { return has_gsym_ != 0; } |
| 333 | |
| 334 | /* |
| 335 | * get the object ID of the LookupTable with the global symbol table |
| 336 | * for reflection purposes |
| 337 | */ |
| 338 | vm_obj_id_t get_reflection_symtab() const { return reflection_symtab_; } |
| 339 | |
| 340 | /* get the object ID of the LookupTable with the macro table */ |
| 341 | vm_obj_id_t get_reflection_macros() const { return reflection_macros_; } |
| 342 | |
| 343 | /* |
| 344 | * perform dynamic linking after loading, resetting, or restoring |
| 345 | */ |
| 346 | void do_dynamic_link(VMG0_); |
| 347 | |
| 348 | /* |
| 349 | * delete all synthesized exports - this must be called just prior to |
| 350 | * resetting to image file state or loading a saved state |
| 351 | */ |
| 352 | void discard_synth_exports(); |
| 353 | |
| 354 | /* allocate a new property ID */ |
| 355 | vm_prop_id_t alloc_new_prop(VMG0_); |
| 356 | |
| 357 | /* get the last property ID currently in use */ |
| 358 | vm_prop_id_t get_last_prop(VMG0_); |
| 359 | |
| 360 | /* save/restore the synthesized export table */ |
| 361 | void save_synth_exports(VMG_ class CVmFile *fp); |
| 362 | int restore_synth_exports(VMG_ class CVmFile *fp, |
| 363 | class CVmObjFixup *fixups); |
| 364 | |
| 365 | /* get the starting offset of static initializers in the code pool */ |
| 366 | ulong get_static_cs_ofs() const { return static_cs_ofs_; } |
| 367 | |
| 368 | /* get the entrypoint function's code pool offset */ |
| 369 | uint32_t get_entrypt() const { return entrypt_; } |
| 370 | |
| 371 | private: |
| 372 | /* load external resource files associated with an image file */ |
| 373 | void load_ext_resfiles(VMG0_); |
| 374 | |
| 375 | /* read and verify an image file header */ |
| 376 | void read_image_header(); |
| 377 | |
| 378 | /* load an Entrypoint block */ |
| 379 | void load_entrypt(VMG_ ulong siz); |
| 380 | |
| 381 | /* load a Static Object (OBJS) block */ |
| 382 | void load_static_objs(VMG_ ulong siz); |
| 383 | |
| 384 | /* load a Constant Pool Definition (CPDF) block */ |
| 385 | void load_const_pool_def(ulong siz); |
| 386 | |
| 387 | /* load a Constant Pool Page (CPPG) block */ |
| 388 | void load_const_pool_page(ulong siz); |
| 389 | |
| 390 | /* load a Multimedia Resource (MRES) block */ |
| 391 | void load_mres(ulong siz, class CVmImageLoaderMres *res_ifc); |
| 392 | |
| 393 | /* load a Multimedia Resource Link (MREL) block */ |
| 394 | void load_mres_link(ulong size, class CVmImageLoaderMres *res_ifc); |
| 395 | |
| 396 | /* load a Metaclass Dependency block */ |
| 397 | void load_meta_dep(VMG_ ulong siz); |
| 398 | |
| 399 | /* load a Function Set Dependency block */ |
| 400 | void load_funcset_dep(VMG_ ulong siz); |
| 401 | |
| 402 | /* load a Symbolic Names block */ |
| 403 | void load_sym_names(VMG_ ulong siz); |
| 404 | |
| 405 | /* load a Source Filenames block */ |
| 406 | void load_srcfiles(VMG_ ulong siz); |
| 407 | |
| 408 | /* load a Global Symbols block */ |
| 409 | void load_gsym(VMG_ ulong siz); |
| 410 | |
| 411 | /* load a Global Symbols (GSYM) block into the runtime symbol table */ |
| 412 | void load_runtime_symtab_from_gsym(VMG_ ulong siz); |
| 413 | |
| 414 | /* load a Macro Symbols (MACR) block into the runtime symbol table */ |
| 415 | void load_runtime_symtab_from_macr(VMG_ ulong siz); |
| 416 | |
| 417 | /* load a Macro Symbols (MACR) block */ |
| 418 | void load_macros(VMG_ ulong siz); |
| 419 | |
| 420 | /* load a Method Header List block */ |
| 421 | void load_mhls(VMG_ ulong siz); |
| 422 | |
| 423 | /* load a Static Initializer List block */ |
| 424 | void load_sini(VMG_ ulong siz); |
| 425 | |
| 426 | /* |
| 427 | * Fix up the debugging global symbol table's object entries with the |
| 428 | * correct metaclass IDs. This has to wait until the whole image file |
| 429 | * is loaded so that we're sure we have all of the objects loaded |
| 430 | * already. |
| 431 | */ |
| 432 | void fix_gsym_meta(VMG0_); |
| 433 | |
| 434 | /* |
| 435 | * Copy data from the file into a buffer, decrementing a size |
| 436 | * counter. We'll throw a BLOCK_TOO_SMALL error if the read length |
| 437 | * exceeds the remaining size. |
| 438 | */ |
| 439 | void read_data(char *buf, size_t read_len, ulong *remaining_size); |
| 440 | |
| 441 | /* skip data */ |
| 442 | void skip_data(size_t skipo_len, ulong *remaining_size); |
| 443 | |
| 444 | /* |
| 445 | * Allocate memory for data and read the data from the file, |
| 446 | * decrementing the amount read from a size counter. Throws |
| 447 | * BLOCK_TOO_SMALL if the read length exceeds the remaining size. |
| 448 | */ |
| 449 | const char *alloc_and_read(size_t read_len, ulong *remaining_size); |
| 450 | |
| 451 | /* add a resource to our resource table */ |
| 452 | void add_resource(class CVmResource *res); |
| 453 | |
| 454 | /* |
| 455 | * Add a symbol to the list of synthesized exports. Each time we |
| 456 | * synthesize a value because we didn't find the associated symbol |
| 457 | * exported from the image file, we must add an entry to this table. |
| 458 | * On saving state, we'll save these symbols to the saved state file |
| 459 | * so they will be restored on load. |
| 460 | */ |
| 461 | void add_synth_export_obj(const char *nm, vm_obj_id_t val); |
| 462 | void add_synth_export_prop(const char *nm, vm_prop_id_t val); |
| 463 | |
| 464 | /* set the last property ID value */ |
| 465 | void set_last_prop(VMG_ vm_prop_id_t last_prop); |
| 466 | |
| 467 | /* callback for synthesized export enumeration: save to file */ |
| 468 | static void save_synth_export_cb(void *ctx, class CVmHashEntry *entry); |
| 469 | |
| 470 | /* underlying image file interface */ |
| 471 | class CVmImageFile *fp_; |
| 472 | |
| 473 | /* image filename */ |
| 474 | char *fname_; |
| 475 | |
| 476 | /* |
| 477 | * fully-qualified, absolute directory path to the file (this is just |
| 478 | * the directory path, sans the filename) |
| 479 | */ |
| 480 | char *path_; |
| 481 | |
| 482 | /* the base seek offset of the image stream within the image file */ |
| 483 | long base_seek_ofs_; |
| 484 | |
| 485 | /* image file version number */ |
| 486 | uint ver_; |
| 487 | |
| 488 | /* image file timestamp */ |
| 489 | char timestamp_[24]; |
| 490 | |
| 491 | /* code pool offset of entrypoint function */ |
| 492 | uint32_t entrypt_; |
| 493 | |
| 494 | /* pool tracking objects */ |
| 495 | class CVmImagePool *pools_[2]; |
| 496 | |
| 497 | /* |
| 498 | * The image's exported symbols. These are the symbols that the |
| 499 | * program explicitly exported for dynamic linking from the VM. |
| 500 | */ |
| 501 | class CVmHashTable *exports_; |
| 502 | |
| 503 | /* |
| 504 | * List of exports synthesized after loading by the VM. These exports |
| 505 | * are not in the image file, so they must be saved in the saved state |
| 506 | * file so that we can reattach to the same objects and properties on |
| 507 | * restore. |
| 508 | */ |
| 509 | class CVmHashTable *synth_exports_; |
| 510 | |
| 511 | /* |
| 512 | * The runtime global symbol table, if we have one. We'll build this |
| 513 | * from the debug records if we find any, or from the records passed |
| 514 | * in from the compiler when we run preinitialization. |
| 515 | * |
| 516 | * Note that the runtime global symbols are not the same as the |
| 517 | * exported symbols. The exports are the symbols explicitly exported |
| 518 | * for dynamic linking, so that the VM can attach to particular |
| 519 | * objects defined in the image file. The runtime globals are all of |
| 520 | * the compile-time global symbols as reflected in the debugging |
| 521 | * records, and are used for reflection-type operations. |
| 522 | */ |
| 523 | class CVmRuntimeSymbols *runtime_symtab_; |
| 524 | |
| 525 | /* |
| 526 | * The runtime macro definitions table, if we have one. As with the |
| 527 | * runtime global symbol table, we build this from the debug records, |
| 528 | * or from the records passed in from the compiler during preinit. |
| 529 | */ |
| 530 | class CVmRuntimeSymbols *runtime_macros_; |
| 531 | |
| 532 | /* object ID of LookupTable containing the global symbol table */ |
| 533 | vm_obj_id_t reflection_symtab_; |
| 534 | |
| 535 | /* object ID of LookupTable containing the macro symbol table */ |
| 536 | vm_obj_id_t reflection_macros_; |
| 537 | |
| 538 | /* head/tail of list of static initializer pages */ |
| 539 | class CVmStaticInitPage *static_head_; |
| 540 | class CVmStaticInitPage *static_tail_; |
| 541 | |
| 542 | /* |
| 543 | * starting offset in code pool of static initializer code - the |
| 544 | * compiler groups all static initializer code, and only static |
| 545 | * initializer code, above this point, so after preinit, we can omit |
| 546 | * all code above this point from the rewritten image file |
| 547 | */ |
| 548 | ulong static_cs_ofs_; |
| 549 | |
| 550 | /* flag: metaclass dependency table loaded */ |
| 551 | uint loaded_meta_dep_ : 1; |
| 552 | |
| 553 | /* flag: function set dependency table loaded */ |
| 554 | uint loaded_funcset_dep_ : 1; |
| 555 | |
| 556 | /* flag: entrypoint loaded */ |
| 557 | uint loaded_entrypt_ : 1; |
| 558 | |
| 559 | /* |
| 560 | * flag: the image file has a GSYM (global symbol table), which |
| 561 | * implies that it was compiled for debugging |
| 562 | */ |
| 563 | uint has_gsym_ : 1; |
| 564 | }; |
| 565 | |
| 566 | |
| 567 | /* ------------------------------------------------------------------------ */ |
| 568 | /* |
| 569 | * Static initializer page. Each page contains a fixed number of |
| 570 | * initializers. |
| 571 | */ |
| 572 | const size_t VM_STATIC_INIT_PAGE_MAX = 1000; |
| 573 | class CVmStaticInitPage |
| 574 | { |
| 575 | public: |
| 576 | CVmStaticInitPage(size_t cnt) |
| 577 | { |
| 578 | /* remember the number of records */ |
| 579 | cnt_ = cnt; |
| 580 | |
| 581 | /* no data yet */ |
| 582 | data_ = 0; |
| 583 | |
| 584 | /* not in a list yet */ |
| 585 | nxt_ = 0; |
| 586 | } |
| 587 | |
| 588 | /* get an object/property ID given the index of a record */ |
| 589 | vm_obj_id_t get_obj_id(size_t idx) |
| 590 | { return vmb_get_objid(get_rec(idx)); } |
| 591 | vm_prop_id_t get_prop_id(size_t idx) |
| 592 | { return vmb_get_propid(get_rec(idx) + VMB_OBJECT_ID); } |
| 593 | |
| 594 | /* get a raw record pointer given an index */ |
| 595 | const char *get_rec(size_t idx) { return data_ + (idx * 6); } |
| 596 | |
| 597 | /* next page in list */ |
| 598 | CVmStaticInitPage *nxt_; |
| 599 | |
| 600 | /* number of records in the page */ |
| 601 | size_t cnt_; |
| 602 | |
| 603 | /* |
| 604 | * The data of the page. Each record is six bytes long, in portable |
| 605 | * format: a UINT4 for the object ID, and a UINT2 for the property |
| 606 | * ID. |
| 607 | */ |
| 608 | const char *data_; |
| 609 | }; |
| 610 | |
| 611 | |
| 612 | /* ------------------------------------------------------------------------ */ |
| 613 | /* |
| 614 | * Image file resource loader interface. This is an abstract class |
| 615 | * interface that must be provided to load_resource_file() to provide |
| 616 | * per-resource loading. |
| 617 | */ |
| 618 | class CVmImageLoaderMres |
| 619 | { |
| 620 | public: |
| 621 | virtual ~CVmImageLoaderMres() { } |
| 622 | |
| 623 | /* load a resource */ |
| 624 | virtual void add_resource(uint32_t seek_ofs, uint32_t siz, |
| 625 | const char *res_name, size_t res_name_len) = 0; |
| 626 | |
| 627 | /* load a resource link */ |
| 628 | virtual void add_resource(const char *fname, size_t fnamelen, |
| 629 | const char *res_name, size_t res_name_len) = 0; |
| 630 | }; |
| 631 | |
| 632 | |
| 633 | /* ------------------------------------------------------------------------ */ |
| 634 | /* |
| 635 | * Image file interface. This is an abstract interface that provides |
| 636 | * access to the data in an image file independently of the location of |
| 637 | * the data. |
| 638 | */ |
| 639 | class CVmImageFile |
| 640 | { |
| 641 | public: |
| 642 | /* delete the loader */ |
| 643 | virtual ~CVmImageFile() { } |
| 644 | |
| 645 | /* duplicate the image file interface, a la stdio freopen() */ |
| 646 | virtual CVmImageFile *dup(const char *mode) = 0; |
| 647 | |
| 648 | /* |
| 649 | * Copy data from the image file to the caller's buffer. Reads from |
| 650 | * the current file position. |
| 651 | */ |
| 652 | virtual void copy_data(char *buf, size_t len) = 0; |
| 653 | |
| 654 | /* |
| 655 | * Allocate memory for and load data from the image file. Reads from |
| 656 | * the current file position. Returns a pointer to the allocated data. |
| 657 | * |
| 658 | * 'remaining_in_page' is the amount of space remaining in the current |
| 659 | * block being read from the image, including the space being read |
| 660 | * here; this can be used as an upper bound for a new allocation if |
| 661 | * the concrete subclass wishes to allocate blocks for suballocation. |
| 662 | */ |
| 663 | virtual const char *alloc_and_read(size_t len, uchar xor_mask, |
| 664 | ulong remaining_in_page) = 0; |
| 665 | |
| 666 | /* |
| 667 | * Determine if memory read with alloc_and_read() is writable. |
| 668 | * Returns true if so, false if not. If the memory that |
| 669 | * alloc_and_read() returns is a copy of the external file (rather |
| 670 | * than mapped to the original external data, as might be the case |
| 671 | * on a small machine without external storage, such as a palm-top |
| 672 | * computer), this should return true. |
| 673 | */ |
| 674 | virtual int allow_write_to_alloc() = 0; |
| 675 | |
| 676 | /* free memory previously allocated by alloc_and_read */ |
| 677 | virtual void free_mem(const char *mem) = 0; |
| 678 | |
| 679 | /* seek to a new file position, as an offset from the start of the file */ |
| 680 | virtual void seek(long pos) = 0; |
| 681 | |
| 682 | /* get the current seek position */ |
| 683 | virtual long get_seek() const = 0; |
| 684 | |
| 685 | /* skip the given number of bytes */ |
| 686 | virtual void skip_ahead(long len) = 0; |
| 687 | }; |
| 688 | |
| 689 | |
| 690 | /* |
| 691 | * Implementation of the generic stream interface for an image file block. |
| 692 | * This will limit reading to the data in the block. |
| 693 | */ |
| 694 | class CVmImageFileStream: public CVmStream |
| 695 | { |
| 696 | public: |
| 697 | CVmImageFileStream(CVmImageFile *fp, size_t len) |
| 698 | { |
| 699 | /* |
| 700 | * remember the underlying image file, and the amount of space in |
| 701 | * our data block |
| 702 | */ |
| 703 | fp_ = fp; |
| 704 | len_ = len; |
| 705 | } |
| 706 | |
| 707 | CVmStream *clone(VMG_ const char *mode) |
| 708 | { |
| 709 | /* duplicate our file handle */ |
| 710 | CVmImageFile *fpdup = fp_->dup(mode); |
| 711 | if (fpdup == 0) |
| 712 | return 0; |
| 713 | |
| 714 | /* create a new image file stream wrapper for the duplicate handle */ |
| 715 | return new CVmImageFileStream(fpdup, len_); |
| 716 | } |
| 717 | |
| 718 | /* read bytes into a buffer */ |
| 719 | virtual void read_bytes(char *buf, size_t len); |
| 720 | virtual size_t read_nbytes(char *buf, size_t len); |
| 721 | |
| 722 | /* read a line (not used for this object) */ |
| 723 | virtual char *read_line(char *buf, size_t len) { return 0; } |
| 724 | |
| 725 | /* write bytes */ |
| 726 | virtual void write_bytes(const char *, size_t); |
| 727 | |
| 728 | /* get/set the seek position */ |
| 729 | virtual long get_seek_pos() const { return fp_->get_seek(); } |
| 730 | virtual void set_seek_pos(long pos) { fp_->seek(pos); } |
| 731 | |
| 732 | virtual long get_len() { return len_; } |
| 733 | |
| 734 | private: |
| 735 | /* our underlying image file reader */ |
| 736 | CVmImageFile *fp_; |
| 737 | |
| 738 | /* remaining data length in the underlying block */ |
| 739 | size_t len_; |
| 740 | }; |
| 741 | |
| 742 | |
| 743 | /* ------------------------------------------------------------------------ */ |
| 744 | /* |
| 745 | * Image file interface - external disk file |
| 746 | */ |
| 747 | class CVmImageFileExt: public CVmImageFile |
| 748 | { |
| 749 | public: |
| 750 | /* delete the image file loader */ |
| 751 | ~CVmImageFileExt(); |
| 752 | |
| 753 | /* initialize with an underlying file */ |
| 754 | CVmImageFileExt(class CVmFile *fp) |
| 755 | { |
| 756 | /* remember our file */ |
| 757 | fp_ = fp; |
| 758 | |
| 759 | /* we don't have any suballocation blocks yet */ |
| 760 | mem_head_ = mem_tail_ = 0; |
| 761 | } |
| 762 | |
| 763 | /* duplicate the file interface */ |
| 764 | virtual CVmImageFile *dup(const char *mode) |
| 765 | { |
| 766 | /* duplicate our file handle */ |
| 767 | CVmFile *fpdup = fp_->dup(mode); |
| 768 | if (fpdup == 0) |
| 769 | return 0; |
| 770 | |
| 771 | /* return a new wrapper object for the duplicate handle */ |
| 772 | return new CVmImageFileExt(fpdup); |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * CVmImageFile interface implementation |
| 777 | */ |
| 778 | |
| 779 | /* copy data to the caller's buffer */ |
| 780 | void copy_data(char *buf, size_t len); |
| 781 | |
| 782 | /* allocate memory for and read data */ |
| 783 | const char *alloc_and_read(size_t len, uchar xor_mask, |
| 784 | ulong remaining_in_page); |
| 785 | |
| 786 | /* allow writing to alloc_and_read blocks */ |
| 787 | virtual int allow_write_to_alloc() { return TRUE; } |
| 788 | |
| 789 | /* |
| 790 | * Free memory previously allocated with alloc_and_read. We don't |
| 791 | * need to do anything here; once we allocate and load a block, we |
| 792 | * keep it in memory until the entire load image is deleted, at |
| 793 | * which time we free all of the associated memory. |
| 794 | */ |
| 795 | void free_mem(const char *) { } |
| 796 | |
| 797 | /* seek to a new file position */ |
| 798 | void seek(long pos); |
| 799 | |
| 800 | /* get the current seek position */ |
| 801 | long get_seek() const; |
| 802 | |
| 803 | /* skip the given number of bytes */ |
| 804 | void skip_ahead(long len); |
| 805 | |
| 806 | private: |
| 807 | /* allocate memory for loading data */ |
| 808 | char *alloc_mem(size_t siz, ulong remaining_in_page); |
| 809 | |
| 810 | /* the underlying file */ |
| 811 | class CVmFile *fp_; |
| 812 | |
| 813 | /* |
| 814 | * Memory block list. We keep a set of memory blocks for loading |
| 815 | * data via the alloc_and_read() method. Rather than allocating an |
| 816 | * individual "malloc" block for each alloc_and_read() call, we |
| 817 | * allocate a large block, and suballocate memory out of the large |
| 818 | * block. Since we don't know exactly how much memory we'll need in |
| 819 | * advance, we take a guess at how large a block we need, |
| 820 | * suballocate from the block until we fill it up, then allocate |
| 821 | * another block and fill it up, then allocate another, and so on. |
| 822 | * We keep all of the blocks we allocate in a linked list. |
| 823 | */ |
| 824 | class CVmImageFileExt_blk *mem_head_; |
| 825 | class CVmImageFileExt_blk *mem_tail_; |
| 826 | }; |
| 827 | |
| 828 | /* |
| 829 | * aggregate allocation block size - use a size that should be reasonably |
| 830 | * safe for 16-bit platforms (not over 64k, and a bit less to allow for |
| 831 | * some malloc overhead) |
| 832 | */ |
| 833 | const size_t VMIMAGE_EXT_BLK_SIZE = 65000; |
| 834 | |
| 835 | /* |
| 836 | * Memory tracking structure for external file reader. For each large |
| 837 | * memory block we allocate (for suballocation), we allocate one of |
| 838 | * these structures. |
| 839 | */ |
| 840 | class CVmImageFileExt_blk |
| 841 | { |
| 842 | public: |
| 843 | /* create the block, allocating the given number of bytes for the block */ |
| 844 | CVmImageFileExt_blk(size_t siz); |
| 845 | |
| 846 | /* delete the block */ |
| 847 | ~CVmImageFileExt_blk(); |
| 848 | |
| 849 | /* suballocate memory out of the current block */ |
| 850 | char *suballoc(size_t siz); |
| 851 | |
| 852 | /* next block in the list */ |
| 853 | CVmImageFileExt_blk *nxt_; |
| 854 | |
| 855 | /* previous block in the list */ |
| 856 | CVmImageFileExt_blk *prv_; |
| 857 | |
| 858 | /* pointer to the start of the block */ |
| 859 | char *block_ptr_; |
| 860 | |
| 861 | /* number of bytes remaining in the block for future suballocations */ |
| 862 | size_t rem_; |
| 863 | |
| 864 | /* pointer to next free byte of the block */ |
| 865 | char *free_ptr_; |
| 866 | }; |
| 867 | |
| 868 | |
| 869 | /* ------------------------------------------------------------------------ */ |
| 870 | /* |
| 871 | * Image file interface - memory-mapped implementation. This |
| 872 | * implementation assumes that the file is loaded into memory in a |
| 873 | * contiguous chunk, which can be addressed linearly. |
| 874 | */ |
| 875 | class CVmImageFileMem: public CVmImageFile |
| 876 | { |
| 877 | public: |
| 878 | ~CVmImageFileMem() { } |
| 879 | |
| 880 | /* initialize with an underlying block of pre-loaded data */ |
| 881 | CVmImageFileMem(const char *mem, long len) |
| 882 | { |
| 883 | /* remember where our data are */ |
| 884 | mem_ = mem; |
| 885 | len_ = len; |
| 886 | |
| 887 | /* start at the beginning of the data */ |
| 888 | pos_ = 0; |
| 889 | } |
| 890 | |
| 891 | /* duplicate the file interface */ |
| 892 | CVmImageFile *dup(const char *mode) |
| 893 | { |
| 894 | return new CVmImageFileMem(mem_, len_); |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | * CVmImageFile interface implementation |
| 899 | */ |
| 900 | |
| 901 | /* copy data to the caller's buffer */ |
| 902 | void copy_data(char *buf, size_t len); |
| 903 | |
| 904 | /* allocate memory for and read data */ |
| 905 | const char *alloc_and_read(size_t len, uchar xor_mask, |
| 906 | ulong remaining_in_page); |
| 907 | |
| 908 | /* |
| 909 | * do not allow writing to alloc_and_read blocks, since we map these |
| 910 | * blocks directly to the underlying in-memory data |
| 911 | */ |
| 912 | virtual int allow_write_to_alloc() { return FALSE; } |
| 913 | |
| 914 | /* |
| 915 | * Free memory allocated by alloc_and_read. Since our underlying |
| 916 | * file is entirely in memory to start with, we don't actually ever |
| 917 | * allocate any memory; hence, we don't actually need to free any |
| 918 | * memory here. |
| 919 | */ |
| 920 | void free_mem(const char *) { } |
| 921 | |
| 922 | /* seek to a new file position */ |
| 923 | void seek(long pos) { pos_ = pos; } |
| 924 | |
| 925 | /* get the current seek position */ |
| 926 | long get_seek() const { return pos_; } |
| 927 | |
| 928 | /* skip the given number of bytes */ |
| 929 | void skip_ahead(long len) { pos_ += len; } |
| 930 | |
| 931 | private: |
| 932 | /* the underlying memory block */ |
| 933 | const char *mem_; |
| 934 | |
| 935 | /* size in bytes of the underlying memory block */ |
| 936 | ulong len_; |
| 937 | |
| 938 | /* current offset within the memory block */ |
| 939 | long pos_; |
| 940 | }; |
| 941 | |
| 942 | |
| 943 | #endif /* VMIMAGE_H */ |
| 944 |