| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/vmwrtimg.cpp,v 1.4 1999/07/11 00:46:59 MJRoberts Exp $"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999, 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 | vmwrtimg.cpp - T3 Image File Writer utility functions |
| 15 | Function |
| 16 | Provides functions to write an image file |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 04/04/99 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | #include <string.h> |
| 25 | #include <time.h> |
| 26 | |
| 27 | #include "t3std.h" |
| 28 | #include "vmfile.h" |
| 29 | #include "vmwrtimg.h" |
| 30 | #include "vmimage.h" |
| 31 | |
| 32 | /* ------------------------------------------------------------------------ */ |
| 33 | /* |
| 34 | * initialize |
| 35 | */ |
| 36 | CVmImageWriter::CVmImageWriter(CVmFile *fp) |
| 37 | { |
| 38 | /* remember the underlying file */ |
| 39 | fp_ = fp; |
| 40 | |
| 41 | /* no block is currently open */ |
| 42 | block_start_ = 0; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * delete |
| 47 | */ |
| 48 | CVmImageWriter::~CVmImageWriter() |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | /* ------------------------------------------------------------------------ */ |
| 53 | /* |
| 54 | * get the current seek position in the underlying file |
| 55 | */ |
| 56 | long CVmImageWriter::get_pos() const |
| 57 | { |
| 58 | return fp_->get_pos(); |
| 59 | } |
| 60 | |
| 61 | /* ------------------------------------------------------------------------ */ |
| 62 | /* |
| 63 | * Prepare the file - write the header. |
| 64 | */ |
| 65 | void CVmImageWriter::prepare(uint vsn, const char tool_data[4]) |
| 66 | { |
| 67 | /* write the signature */ |
| 68 | fp_->write_bytes(VMIMAGE_SIG, sizeof(VMIMAGE_SIG)-1); |
| 69 | |
| 70 | /* write the version number */ |
| 71 | fp_->write_uint2(vsn); |
| 72 | |
| 73 | /* write the 28 reserved bytes, setting all to zero */ |
| 74 | char buf[32]; |
| 75 | memset(buf, 0, 28); |
| 76 | fp_->write_bytes(buf, 28); |
| 77 | |
| 78 | /* write the additional 4 bytes reserved for tool use */ |
| 79 | fp_->write_bytes(tool_data, 4); |
| 80 | |
| 81 | /* write the compilation timestamp */ |
| 82 | os_time_t timer = os_time(NULL); |
| 83 | struct tm *tblock = os_localtime(&timer); |
| 84 | fp_->write_bytes(asctime(tblock), 24); |
| 85 | } |
| 86 | |
| 87 | /* ------------------------------------------------------------------------ */ |
| 88 | /* |
| 89 | * Begin a block |
| 90 | */ |
| 91 | void CVmImageWriter::begin_block(const char *block_id, int mandatory) |
| 92 | { |
| 93 | char buf[10]; |
| 94 | uint flags; |
| 95 | |
| 96 | /* if there's a block currently open, close it */ |
| 97 | end_block(); |
| 98 | |
| 99 | /* remember the seek location of the start of the block */ |
| 100 | block_start_ = fp_->get_pos(); |
| 101 | |
| 102 | /* store the type string */ |
| 103 | memcpy(buf, block_id, 4); |
| 104 | |
| 105 | /* store four bytes of zeros as a placeholder for the size */ |
| 106 | memset(buf+4, 0, 4); |
| 107 | |
| 108 | /* compute the flags */ |
| 109 | flags = 0; |
| 110 | if (mandatory) |
| 111 | flags |= VMIMAGE_DBF_MANDATORY; |
| 112 | |
| 113 | /* store the flags */ |
| 114 | oswp2(buf+8, flags); |
| 115 | |
| 116 | /* write the header */ |
| 117 | fp_->write_bytes(buf, 10); |
| 118 | } |
| 119 | |
| 120 | /* ------------------------------------------------------------------------ */ |
| 121 | /* |
| 122 | * End the current block. If no block is open, this does nothing. |
| 123 | */ |
| 124 | void CVmImageWriter::end_block() |
| 125 | { |
| 126 | long end_pos; |
| 127 | uint32_t siz; |
| 128 | |
| 129 | /* if there's no block open, there's nothing we need to do */ |
| 130 | if (block_start_ == 0) |
| 131 | return; |
| 132 | |
| 133 | /* |
| 134 | * note the current file position - this will let us compute the |
| 135 | * size of the block, and we'll need to seek back here when we're |
| 136 | * done updating the block header |
| 137 | */ |
| 138 | end_pos = fp_->get_pos(); |
| 139 | |
| 140 | /* |
| 141 | * Since the block is finished, we can now compute its size. The |
| 142 | * size of the data block is the end seek position minus the |
| 143 | * starting seek position. 'block_start_' contains the seek |
| 144 | * position of the block header, which takes up ten bytes; we want |
| 145 | * to store the size of the block's data, excluding the header, |
| 146 | * which is (end_pos - block_header_pos - 10). |
| 147 | */ |
| 148 | siz = (uint32_t)(end_pos - block_start_ - 10); |
| 149 | |
| 150 | /* |
| 151 | * Seek back to the location of the size field in the block header; |
| 152 | * this is four bytes into the block header. Then, update the size |
| 153 | * field with the size of the block's data. |
| 154 | */ |
| 155 | fp_->set_pos(block_start_ + 4); |
| 156 | fp_->write_uint4(siz); |
| 157 | |
| 158 | /* |
| 159 | * seek back to the end of the block, so we can resume writing data |
| 160 | * following the block |
| 161 | */ |
| 162 | fp_->set_pos(end_pos); |
| 163 | |
| 164 | /* the block is now closed, so forget about it */ |
| 165 | block_start_ = 0; |
| 166 | } |
| 167 | |
| 168 | /* ------------------------------------------------------------------------ */ |
| 169 | /* |
| 170 | * Write raw bytes to the file |
| 171 | */ |
| 172 | void CVmImageWriter::write_bytes(const char *ptr, uint32_t siz) |
| 173 | { |
| 174 | /* write in 64k chunks, to accommodate 16-bit platforms */ |
| 175 | while (siz != 0) |
| 176 | { |
| 177 | size_t cur; |
| 178 | |
| 179 | /* get the next 64k, or the remainder if less than 64k is left */ |
| 180 | cur = 65535; |
| 181 | if (siz < cur) |
| 182 | cur = (size_t)siz; |
| 183 | |
| 184 | /* write this chunk */ |
| 185 | fp_->write_bytes(ptr, cur); |
| 186 | |
| 187 | /* advance past this chunk */ |
| 188 | ptr += cur; |
| 189 | siz -= cur; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* ------------------------------------------------------------------------ */ |
| 194 | /* |
| 195 | * Finish the file. Closes the current block if one is open, and writes |
| 196 | * the end-of-file marker to the file. |
| 197 | */ |
| 198 | void CVmImageWriter::finish() |
| 199 | { |
| 200 | /* if there's a block open, close it */ |
| 201 | end_block(); |
| 202 | |
| 203 | /* |
| 204 | * write the EOF block - the block contains no data, so simply begin |
| 205 | * and end it |
| 206 | */ |
| 207 | begin_block("EOF ", TRUE); |
| 208 | end_block(); |
| 209 | } |
| 210 | |
| 211 | /* ------------------------------------------------------------------------ */ |
| 212 | /* |
| 213 | * Write an entrypoint (ENTP) block |
| 214 | */ |
| 215 | void CVmImageWriter::write_entrypt(uint32_t entry_ofs, size_t method_hdr_size, |
| 216 | size_t exc_entry_size, |
| 217 | size_t line_entry_size, |
| 218 | size_t dbg_hdr_size, |
| 219 | size_t dbg_lclsym_hdr_size, |
| 220 | size_t dbg_frame_hdr_size, |
| 221 | int dbg_vsn_id) |
| 222 | { |
| 223 | char buf[32]; |
| 224 | |
| 225 | /* prepare the block's contents */ |
| 226 | oswp4(buf, entry_ofs); |
| 227 | oswp2(buf+4, method_hdr_size); |
| 228 | oswp2(buf+6, exc_entry_size); |
| 229 | oswp2(buf+8, line_entry_size); |
| 230 | oswp2(buf+10, dbg_hdr_size); |
| 231 | oswp2(buf+12, dbg_lclsym_hdr_size); |
| 232 | oswp2(buf+14, dbg_vsn_id); |
| 233 | oswp2(buf+16, dbg_frame_hdr_size); |
| 234 | |
| 235 | /* open the block, write the data, and close the block */ |
| 236 | begin_block("ENTP", TRUE); |
| 237 | fp_->write_bytes(buf, 18); |
| 238 | end_block(); |
| 239 | } |
| 240 | |
| 241 | /* ------------------------------------------------------------------------ */ |
| 242 | /* |
| 243 | * Write a function set dependency block |
| 244 | */ |
| 245 | void CVmImageWriter::write_func_dep(const char **funcset_names, int count) |
| 246 | { |
| 247 | /* write a FNSD block */ |
| 248 | write_dep_block("FNSD", funcset_names, count); |
| 249 | } |
| 250 | |
| 251 | /* ------------------------------------------------------------------------ */ |
| 252 | /* |
| 253 | * begin a metaclass dependency block |
| 254 | */ |
| 255 | void CVmImageWriter::begin_meta_dep(int count) |
| 256 | { |
| 257 | /* begin the dependency block */ |
| 258 | begin_dep_block("MCLD", count); |
| 259 | |
| 260 | /* we're not in a property list yet */ |
| 261 | mcld_propcnt_pos_ = 0; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Write a metaclass dependency block |
| 266 | */ |
| 267 | void CVmImageWriter::write_meta_dep(const char **meta_names, int count) |
| 268 | { |
| 269 | /* write a MCLD block */ |
| 270 | write_dep_block("MCLD", meta_names, count); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * write a metaclass dependency block item |
| 275 | */ |
| 276 | void CVmImageWriter::write_meta_dep_item(const char *metaclass_name) |
| 277 | { |
| 278 | /* if we didn't end the previous item's property list, end it now */ |
| 279 | end_meta_prop_list(); |
| 280 | |
| 281 | /* write a placeholder next record offset */ |
| 282 | mcld_ofs_pos_ = fp_->get_pos(); |
| 283 | fp_->write_uint2(0); |
| 284 | |
| 285 | /* write the metaclass name */ |
| 286 | write_dep_block_item(metaclass_name); |
| 287 | |
| 288 | /* write a placeholder property vector count */ |
| 289 | mcld_propcnt_pos_ = fp_->get_pos(); |
| 290 | fp_->write_uint2(0); |
| 291 | |
| 292 | /* write the property record size (2 bytes) */ |
| 293 | fp_->write_uint2(2); |
| 294 | |
| 295 | /* no properties yet */ |
| 296 | mcld_prop_cnt_ = 0; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * write a metaclass dependency property list item |
| 301 | */ |
| 302 | void CVmImageWriter::write_meta_item_prop(uint prop_id) |
| 303 | { |
| 304 | /* write the property ID */ |
| 305 | fp_->write_uint2(prop_id); |
| 306 | |
| 307 | /* count it */ |
| 308 | ++mcld_prop_cnt_; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * end a metaclass prop list |
| 313 | */ |
| 314 | void CVmImageWriter::end_meta_prop_list() |
| 315 | { |
| 316 | /* if we have a count pending, go write it */ |
| 317 | if (mcld_propcnt_pos_ != 0) |
| 318 | { |
| 319 | long pos; |
| 320 | |
| 321 | /* remember the current position */ |
| 322 | pos = fp_->get_pos(); |
| 323 | |
| 324 | /* go back and write the property count */ |
| 325 | fp_->set_pos(mcld_propcnt_pos_); |
| 326 | fp_->write_uint2(mcld_prop_cnt_); |
| 327 | |
| 328 | /* we no longer have a property count fixup to apply */ |
| 329 | mcld_propcnt_pos_ = 0; |
| 330 | |
| 331 | /* go back and write the next-record offset */ |
| 332 | fp_->set_pos(mcld_ofs_pos_); |
| 333 | fp_->write_int2((int)(pos - mcld_ofs_pos_)); |
| 334 | |
| 335 | /* go back to the end of the record */ |
| 336 | fp_->set_pos(pos); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * end a metaclass dependency block |
| 342 | */ |
| 343 | void CVmImageWriter::end_meta_dep() |
| 344 | { |
| 345 | /* end the last metaclass item */ |
| 346 | end_meta_prop_list(); |
| 347 | |
| 348 | /* end the dependency block */ |
| 349 | end_dep_block(); |
| 350 | } |
| 351 | |
| 352 | /* ------------------------------------------------------------------------ */ |
| 353 | /* |
| 354 | * Begin a dependency block |
| 355 | */ |
| 356 | void CVmImageWriter::begin_dep_block(const char *block_id, int count) |
| 357 | { |
| 358 | char buf[4]; |
| 359 | |
| 360 | /* open the block */ |
| 361 | begin_block(block_id, TRUE); |
| 362 | |
| 363 | /* write the number of entries */ |
| 364 | oswp2(buf, count); |
| 365 | fp_->write_bytes(buf, 2); |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Write a dependency block item |
| 370 | */ |
| 371 | void CVmImageWriter::write_dep_block_item(const char *nm) |
| 372 | { |
| 373 | size_t len; |
| 374 | char buf[4]; |
| 375 | |
| 376 | /* get the length of this name, and truncate to 255 bytes */ |
| 377 | len = strlen(nm); |
| 378 | if (len > 255) |
| 379 | len = 255; |
| 380 | |
| 381 | /* write the length, followed by the name */ |
| 382 | buf[0] = (char)(uchar)len; |
| 383 | fp_->write_bytes(buf, 1); |
| 384 | fp_->write_bytes(nm, len); |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * End a dependency block |
| 389 | */ |
| 390 | void CVmImageWriter::end_dep_block() |
| 391 | { |
| 392 | /* end the block */ |
| 393 | end_block(); |
| 394 | } |
| 395 | |
| 396 | /* ------------------------------------------------------------------------ */ |
| 397 | /* |
| 398 | * Write a generic dependency list block |
| 399 | */ |
| 400 | void CVmImageWriter::write_dep_block(const char *block_id, |
| 401 | const char **names, int count) |
| 402 | { |
| 403 | /* open the block */ |
| 404 | begin_dep_block(block_id, count); |
| 405 | |
| 406 | /* write each entry */ |
| 407 | for ( ; count > 0 ; ++names, --count) |
| 408 | write_dep_block_item(*names); |
| 409 | |
| 410 | /* end the block */ |
| 411 | end_dep_block(); |
| 412 | } |
| 413 | |
| 414 | /* ------------------------------------------------------------------------ */ |
| 415 | /* |
| 416 | * Write a constant pool definition block |
| 417 | */ |
| 418 | void CVmImageWriter::write_pool_def(uint pool_id, uint32_t page_count, |
| 419 | uint32_t page_size, int mandatory) |
| 420 | { |
| 421 | char buf[16]; |
| 422 | |
| 423 | /* prepare the block's data */ |
| 424 | oswp2(buf, pool_id); |
| 425 | oswp4(buf+2, page_count); |
| 426 | oswp4(buf+6, page_size); |
| 427 | |
| 428 | /* open the block, write the data, and end the block */ |
| 429 | begin_block("CPDF", mandatory); |
| 430 | fp_->write_bytes(buf, 10); |
| 431 | end_block(); |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Fix up a pool definition block with the actual page count |
| 436 | */ |
| 437 | void CVmImageWriter::fix_pool_def(long def_seek_ofs, uint32_t page_count) |
| 438 | { |
| 439 | long old_pos; |
| 440 | char buf[4]; |
| 441 | |
| 442 | /* note the file position at entry */ |
| 443 | old_pos = fp_->get_pos(); |
| 444 | |
| 445 | /* |
| 446 | * seek to the original definition block location, plus the size of |
| 447 | * the block header (10 bytes), plus the offset within the block of |
| 448 | * the pool page count (it starts 2 bytes into the block data) |
| 449 | */ |
| 450 | fp_->set_pos(def_seek_ofs + 10 + 2); |
| 451 | |
| 452 | /* write the page count */ |
| 453 | oswp4(buf, page_count); |
| 454 | fp_->write_bytes(buf, 4); |
| 455 | |
| 456 | /* seek back to our location at entry */ |
| 457 | fp_->set_pos(old_pos); |
| 458 | } |
| 459 | |
| 460 | |
| 461 | /* ------------------------------------------------------------------------ */ |
| 462 | /* |
| 463 | * Write a constant pool page |
| 464 | */ |
| 465 | void CVmImageWriter::write_pool_page(uint pool_id, uint32_t page_index, |
| 466 | const char *page_data, |
| 467 | uint32_t page_data_size, int mandatory, |
| 468 | uchar xor_mask) |
| 469 | { |
| 470 | char buf[16]; |
| 471 | |
| 472 | /* begin the block */ |
| 473 | begin_block("CPPG", mandatory); |
| 474 | |
| 475 | /* prepare the prefix */ |
| 476 | oswp2(buf, pool_id); |
| 477 | oswp4(buf+2, page_index); |
| 478 | buf[6] = xor_mask; |
| 479 | |
| 480 | /* write the prefix */ |
| 481 | fp_->write_bytes(buf, 7); |
| 482 | |
| 483 | /* write the page data, XOR'ing the data with the mask byte */ |
| 484 | xor_and_write_bytes(page_data, page_data_size, xor_mask); |
| 485 | |
| 486 | /* end the block */ |
| 487 | end_block(); |
| 488 | } |
| 489 | |
| 490 | /* ------------------------------------------------------------------------ */ |
| 491 | /* |
| 492 | * Begin writing a constant pool page. This constructs the header and |
| 493 | * prepares for writing the bytes making up the page. |
| 494 | */ |
| 495 | void CVmImageWriter::begin_pool_page(uint pool_id, uint32_t page_index, |
| 496 | int mandatory, uchar xor_mask) |
| 497 | { |
| 498 | char buf[16]; |
| 499 | |
| 500 | /* begin the block */ |
| 501 | begin_block("CPPG", mandatory); |
| 502 | |
| 503 | /* prepare the prefix */ |
| 504 | oswp2(buf, pool_id); |
| 505 | oswp4(buf+2, page_index); |
| 506 | buf[6] = xor_mask; |
| 507 | |
| 508 | /* write the prefix */ |
| 509 | fp_->write_bytes(buf, 7); |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * write bytes to a pool page under construction |
| 514 | */ |
| 515 | void CVmImageWriter::write_pool_page_bytes(const char *buf, uint32_t siz, |
| 516 | uchar xor_mask) |
| 517 | { |
| 518 | /* write the page data, XOR'ing the data with the mask byte */ |
| 519 | xor_and_write_bytes(buf, siz, xor_mask); |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * XOR and write a block of data - we will XOR each byte of the data |
| 524 | * with the given mask byte before writing it to the file |
| 525 | */ |
| 526 | void CVmImageWriter::xor_and_write_bytes(const char *mem, uint32_t siz, |
| 527 | uchar xor_mask) |
| 528 | { |
| 529 | /* |
| 530 | * if there's no mask, simply write the data directly - anything XOR |
| 531 | * zero equals the original value |
| 532 | */ |
| 533 | if (xor_mask == 0) |
| 534 | { |
| 535 | /* write the data to the page */ |
| 536 | fp_->write_bytes(mem, siz); |
| 537 | } |
| 538 | else |
| 539 | { |
| 540 | /* |
| 541 | * copy the data in chunks into our buffer, XOR it with the |
| 542 | * mask, and write the results |
| 543 | */ |
| 544 | while (siz != 0) |
| 545 | { |
| 546 | char buf[1024]; |
| 547 | size_t cur; |
| 548 | size_t rem; |
| 549 | char *dst; |
| 550 | |
| 551 | /* |
| 552 | * limit this chunk to the buffer size or the remainder of |
| 553 | * the input, whichever is smaller |
| 554 | */ |
| 555 | cur = sizeof(buf); |
| 556 | if (cur > siz) |
| 557 | cur = (size_t)siz; |
| 558 | |
| 559 | /* copy this chunk, xor'ing each byte with the mask */ |
| 560 | for (dst = buf, rem = cur ; rem != 0 ; --rem, ++dst, ++mem) |
| 561 | *dst = *mem ^ xor_mask; |
| 562 | |
| 563 | /* write out this chunk */ |
| 564 | fp_->write_bytes(buf, cur); |
| 565 | |
| 566 | /* subtract this chunk from the length remaining */ |
| 567 | siz -= cur; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * finish writing a pool page |
| 574 | */ |
| 575 | void CVmImageWriter::end_pool_page() |
| 576 | { |
| 577 | /* end the block */ |
| 578 | end_block(); |
| 579 | } |
| 580 | |
| 581 | /* ------------------------------------------------------------------------ */ |
| 582 | /* |
| 583 | * Begin a symbolic names block |
| 584 | */ |
| 585 | void CVmImageWriter::begin_sym_block() |
| 586 | { |
| 587 | char buf[4]; |
| 588 | |
| 589 | /* begin the block */ |
| 590 | begin_block("SYMD", FALSE); |
| 591 | |
| 592 | /* remember where our placeholder goes */ |
| 593 | symd_prefix_ = fp_->get_pos(); |
| 594 | |
| 595 | /* prepare the placeholder prefix, using a zero count for now */ |
| 596 | oswp2(buf, 0); |
| 597 | |
| 598 | /* write the prefix */ |
| 599 | fp_->write_bytes(buf, 2); |
| 600 | |
| 601 | /* we haven't written any symbolic name items yet */ |
| 602 | symd_cnt_ = 0; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * write a symbolic name for an object ID |
| 607 | */ |
| 608 | void CVmImageWriter::write_sym_item_objid(const char *nm, size_t len, |
| 609 | ulong obj_id) |
| 610 | { |
| 611 | vm_val_t val; |
| 612 | |
| 613 | /* set up the object ID value */ |
| 614 | val.set_obj((vm_obj_id_t)obj_id); |
| 615 | |
| 616 | /* write it out */ |
| 617 | write_sym_item(nm, len, &val); |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * write a symbolic name for a property ID |
| 622 | */ |
| 623 | void CVmImageWriter::write_sym_item_propid(const char *nm, size_t len, |
| 624 | uint prop_id) |
| 625 | { |
| 626 | vm_val_t val; |
| 627 | |
| 628 | /* set up the property ID value */ |
| 629 | val.set_propid((vm_prop_id_t)prop_id); |
| 630 | |
| 631 | /* write it out */ |
| 632 | write_sym_item(nm, len, &val); |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * write a symbolic name for a function |
| 637 | */ |
| 638 | void CVmImageWriter::write_sym_item_func(const char *nm, size_t len, |
| 639 | ulong code_ofs) |
| 640 | { |
| 641 | vm_val_t val; |
| 642 | |
| 643 | /* set up the property ID value */ |
| 644 | val.set_fnptr((pool_ofs_t)code_ofs); |
| 645 | |
| 646 | /* write it out */ |
| 647 | write_sym_item(nm, len, &val); |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * write a symbolic name item |
| 652 | */ |
| 653 | void CVmImageWriter::write_sym_item(const char *nm, size_t len, |
| 654 | const vm_val_t *val) |
| 655 | { |
| 656 | char buf[VMB_DATAHOLDER + 1]; |
| 657 | |
| 658 | /* prepare the data holder in the prefix */ |
| 659 | vmb_put_dh(buf, val); |
| 660 | |
| 661 | /* limit the length to 255 bytes */ |
| 662 | if (len > 255) |
| 663 | len = 255; |
| 664 | |
| 665 | /* add the length to the prefix */ |
| 666 | buf[VMB_DATAHOLDER] = (char)len; |
| 667 | |
| 668 | /* write the prefix */ |
| 669 | fp_->write_bytes(buf, VMB_DATAHOLDER + 1); |
| 670 | |
| 671 | /* write the string */ |
| 672 | fp_->write_bytes(nm, len); |
| 673 | |
| 674 | /* count it */ |
| 675 | ++symd_cnt_; |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * end a symbolic names block |
| 680 | */ |
| 681 | void CVmImageWriter::end_sym_block() |
| 682 | { |
| 683 | long old_pos; |
| 684 | char buf[4]; |
| 685 | |
| 686 | /* end the block */ |
| 687 | end_block(); |
| 688 | |
| 689 | /* |
| 690 | * Go back and fix the header with the number of items we wrote. |
| 691 | * First, remember our current position, then seek back to the count |
| 692 | * prefix. |
| 693 | */ |
| 694 | old_pos = fp_->get_pos(); |
| 695 | fp_->set_pos(symd_prefix_); |
| 696 | |
| 697 | /* prepare the prefix, and write it out */ |
| 698 | oswp2(buf, symd_cnt_); |
| 699 | fp_->write_bytes(buf, 2); |
| 700 | |
| 701 | /* restore the file position */ |
| 702 | fp_->set_pos(old_pos); |
| 703 | } |
| 704 | |
| 705 | /* ------------------------------------------------------------------------ */ |
| 706 | /* |
| 707 | * Begin an object static data block |
| 708 | */ |
| 709 | void CVmImageWriter::begin_objs_block(uint metaclass_idx, int large_objects, |
| 710 | int trans) |
| 711 | { |
| 712 | char buf[16]; |
| 713 | uint flags; |
| 714 | |
| 715 | /* begin the block */ |
| 716 | begin_block("OBJS", TRUE); |
| 717 | |
| 718 | /* prepare the flags */ |
| 719 | flags = 0; |
| 720 | if (large_objects) |
| 721 | flags |= 1; |
| 722 | if (trans) |
| 723 | flags |= 2; |
| 724 | |
| 725 | /* remember where the prefix goes so we can fix it up later */ |
| 726 | objs_prefix_ = fp_->get_pos(); |
| 727 | |
| 728 | /* |
| 729 | * write a placeholder object count, the metaclass dependency table |
| 730 | * index, and the OBJS flags |
| 731 | */ |
| 732 | oswp2(buf, 0); |
| 733 | oswp2(buf + 2, metaclass_idx); |
| 734 | oswp2(buf + 4, flags); |
| 735 | |
| 736 | /* write the prefix */ |
| 737 | fp_->write_bytes(buf, 6); |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Write bytes to an OBJS (object static data) block |
| 742 | */ |
| 743 | void CVmImageWriter::write_objs_bytes(const char *buf, uint32_t siz) |
| 744 | { |
| 745 | /* write the buffer */ |
| 746 | fp_->write_bytes(buf, siz); |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * end an object static data block |
| 751 | */ |
| 752 | void CVmImageWriter::end_objs_block(uint object_count) |
| 753 | { |
| 754 | long pos; |
| 755 | |
| 756 | /* remember the current file write position for a moment */ |
| 757 | pos = fp_->get_pos(); |
| 758 | |
| 759 | /* go back and fix up the object count in the header */ |
| 760 | fp_->set_pos(objs_prefix_); |
| 761 | fp_->write_uint2(object_count); |
| 762 | |
| 763 | /* seek back to the original position */ |
| 764 | fp_->set_pos(pos); |
| 765 | |
| 766 | /* end the block */ |
| 767 | end_block(); |
| 768 | } |
| 769 | |
| 770 | /* ------------------------------------------------------------------------ */ |
| 771 | /* |
| 772 | * SRCF blocks - source file descriptors |
| 773 | */ |
| 774 | |
| 775 | /* |
| 776 | * begin a SRCF block |
| 777 | */ |
| 778 | void CVmImageWriter::begin_srcf_block(int count) |
| 779 | { |
| 780 | /* |
| 781 | * begin the block - SRCF blocks are always optional, since they're |
| 782 | * purely for debugging purposes |
| 783 | */ |
| 784 | begin_block("SRCF", FALSE); |
| 785 | |
| 786 | /* write the number of entries */ |
| 787 | fp_->write_uint2(count); |
| 788 | |
| 789 | /* each source line record is 8 bytes in the current format */ |
| 790 | fp_->write_uint2(8); |
| 791 | } |
| 792 | |
| 793 | /* |
| 794 | * begin a SRCF file entry |
| 795 | */ |
| 796 | void CVmImageWriter::begin_srcf_entry(int orig_index, const char *fname) |
| 797 | { |
| 798 | size_t len; |
| 799 | |
| 800 | /* remember where this entry starts, so we can fix up the size later */ |
| 801 | srcf_entry_pos_ = fp_->get_pos(); |
| 802 | |
| 803 | /* write a placeholder size entry */ |
| 804 | fp_->write_uint4(0); |
| 805 | |
| 806 | /* write the original index */ |
| 807 | fp_->write_uint2(orig_index); |
| 808 | |
| 809 | /* write the length of the name */ |
| 810 | len = get_strlen(fname); |
| 811 | fp_->write_uint2(len); |
| 812 | |
| 813 | /* write the filename */ |
| 814 | fp_->write_bytes(fname, len); |
| 815 | |
| 816 | /* we have no line record yet, so write a placeholder count */ |
| 817 | srcf_line_pos_ = fp_->get_pos(); |
| 818 | fp_->write_uint4(0); |
| 819 | srcf_line_cnt_ = 0; |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | * write a SRCF line record entry |
| 824 | */ |
| 825 | void CVmImageWriter::write_srcf_line_entry(ulong linenum, ulong addr) |
| 826 | { |
| 827 | /* write the line number and address */ |
| 828 | fp_->write_uint4(linenum); |
| 829 | fp_->write_uint4(addr); |
| 830 | |
| 831 | /* count it */ |
| 832 | ++srcf_line_cnt_; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * end a SRCF file entry |
| 837 | */ |
| 838 | void CVmImageWriter::end_srcf_entry() |
| 839 | { |
| 840 | ulong pos; |
| 841 | |
| 842 | /* go back and fix up the line record count */ |
| 843 | pos = fp_->get_pos(); |
| 844 | fp_->set_pos(srcf_line_pos_); |
| 845 | fp_->write_uint4(srcf_line_cnt_); |
| 846 | |
| 847 | /* go back and fix up the total entry size record */ |
| 848 | fp_->set_pos(srcf_entry_pos_); |
| 849 | fp_->write_uint4(pos - srcf_entry_pos_); |
| 850 | |
| 851 | /* seek back to the end of the block */ |
| 852 | fp_->set_pos(pos); |
| 853 | |
| 854 | } |
| 855 | |
| 856 | /* |
| 857 | * end a SRCF block |
| 858 | */ |
| 859 | void CVmImageWriter::end_srcf_block() |
| 860 | { |
| 861 | /* end the block using the generic mechanism */ |
| 862 | end_block(); |
| 863 | } |
| 864 | |
| 865 | /* ------------------------------------------------------------------------ */ |
| 866 | /* |
| 867 | * MACR blocks - global preprocess macro symbol table |
| 868 | */ |
| 869 | |
| 870 | /* |
| 871 | * begin a MACR block |
| 872 | */ |
| 873 | void CVmImageWriter::begin_macr_block() |
| 874 | { |
| 875 | /* |
| 876 | * write the header - it's an optional block since it's for the |
| 877 | * debugger's use only |
| 878 | */ |
| 879 | begin_block("MACR", FALSE); |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * end a MACR block |
| 884 | */ |
| 885 | void CVmImageWriter::end_macr_block() |
| 886 | { |
| 887 | /* end the block using the generic mechanism */ |
| 888 | end_block(); |
| 889 | } |
| 890 | |
| 891 | /* ------------------------------------------------------------------------ */ |
| 892 | /* |
| 893 | * GSYM blocks - global symbol table |
| 894 | */ |
| 895 | |
| 896 | /* |
| 897 | * begin a GSYM block |
| 898 | */ |
| 899 | void CVmImageWriter::begin_gsym_block() |
| 900 | { |
| 901 | /* |
| 902 | * begin the block - GSYM blocks are always optional, since they're |
| 903 | * purely for debugging purposes |
| 904 | */ |
| 905 | begin_block("GSYM", FALSE); |
| 906 | |
| 907 | /* remember where the prefix goes so we can fix it up later */ |
| 908 | gsym_prefix_ = fp_->get_pos(); |
| 909 | |
| 910 | /* write a placehodler object count and the metaclass index */ |
| 911 | fp_->write_uint4(0); |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | * write a GSYM entry |
| 916 | */ |
| 917 | void CVmImageWriter::write_gsym_entry(const char *sym, size_t sym_len, |
| 918 | int type_id, |
| 919 | const char *dat, size_t dat_len) |
| 920 | { |
| 921 | /* |
| 922 | * write the length of the symbol, length of the extra data, and the |
| 923 | * symbol type |
| 924 | */ |
| 925 | fp_->write_uint2(sym_len); |
| 926 | fp_->write_uint2(dat_len); |
| 927 | fp_->write_uint2(type_id); |
| 928 | |
| 929 | /* write the symbol name */ |
| 930 | fp_->write_bytes(sym, sym_len); |
| 931 | |
| 932 | /* write the extra data */ |
| 933 | fp_->write_bytes(dat, dat_len); |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * end a GSYM block |
| 938 | */ |
| 939 | void CVmImageWriter::end_gsym_block(ulong cnt) |
| 940 | { |
| 941 | long pos; |
| 942 | |
| 943 | /* remember the current file write position for a moment */ |
| 944 | pos = fp_->get_pos(); |
| 945 | |
| 946 | /* go back and fix up the count in the header */ |
| 947 | fp_->set_pos(gsym_prefix_); |
| 948 | fp_->write_uint4(cnt); |
| 949 | |
| 950 | /* seek back to the original position */ |
| 951 | fp_->set_pos(pos); |
| 952 | |
| 953 | /* end the block using the generic mechanism */ |
| 954 | end_block(); |
| 955 | } |
| 956 | |
| 957 | /* ------------------------------------------------------------------------ */ |
| 958 | /* |
| 959 | * MHLS blocks - method header list |
| 960 | */ |
| 961 | |
| 962 | /* |
| 963 | * begin an MHLS block |
| 964 | */ |
| 965 | void CVmImageWriter::begin_mhls_block() |
| 966 | { |
| 967 | /* |
| 968 | * begin the block - MHLS blocks are always optional, since they're |
| 969 | * purely for debugging purposes |
| 970 | */ |
| 971 | begin_block("MHLS", FALSE); |
| 972 | |
| 973 | /* remember where the count goes so we can fix it up later */ |
| 974 | mhls_cnt_pos_ = fp_->get_pos(); |
| 975 | |
| 976 | /* write a placehodler count */ |
| 977 | fp_->write_uint4(0); |
| 978 | |
| 979 | /* there are no entries yet */ |
| 980 | mhls_cnt_ = 0; |
| 981 | } |
| 982 | |
| 983 | /* |
| 984 | * write an MHLS entry |
| 985 | */ |
| 986 | void CVmImageWriter::write_mhls_entry(ulong addr) |
| 987 | { |
| 988 | /* write the address */ |
| 989 | fp_->write_uint4(addr); |
| 990 | |
| 991 | /* count the entry */ |
| 992 | ++mhls_cnt_; |
| 993 | } |
| 994 | |
| 995 | /* |
| 996 | * end an MHLS block |
| 997 | */ |
| 998 | void CVmImageWriter::end_mhls_block() |
| 999 | { |
| 1000 | long pos; |
| 1001 | |
| 1002 | /* remember the current file write position for a moment */ |
| 1003 | pos = fp_->get_pos(); |
| 1004 | |
| 1005 | /* go back and fix up the count in the header */ |
| 1006 | fp_->set_pos(mhls_cnt_pos_); |
| 1007 | fp_->write_uint4(mhls_cnt_); |
| 1008 | |
| 1009 | /* seek back to the original position */ |
| 1010 | fp_->set_pos(pos); |
| 1011 | |
| 1012 | /* end the block using the generic mechanism */ |
| 1013 | end_block(); |
| 1014 | } |
| 1015 | |
| 1016 | /* ------------------------------------------------------------------------ */ |
| 1017 | /* |
| 1018 | * SINI block - static initializer list |
| 1019 | */ |
| 1020 | |
| 1021 | /* |
| 1022 | * begin an SINI block |
| 1023 | */ |
| 1024 | void CVmImageWriter::begin_sini_block(ulong static_cs_ofs, ulong init_cnt) |
| 1025 | { |
| 1026 | /* |
| 1027 | * begin the block - SINI blocks are mandatory, since the program |
| 1028 | * depends upon static initializers being evaluated immediately |
| 1029 | * after compilation |
| 1030 | */ |
| 1031 | begin_block("SINI", TRUE); |
| 1032 | |
| 1033 | /* |
| 1034 | * write the size of our header (including the size prefix); this |
| 1035 | * serves as a simple versioning flag so we can tell if fields added |
| 1036 | * at a later date are part of a given image file's data or not (if |
| 1037 | * the header is too small to contain them, they're not present) |
| 1038 | */ |
| 1039 | fp_->write_uint4(12); |
| 1040 | |
| 1041 | /* write the starting static code segment offset */ |
| 1042 | fp_->write_uint4(static_cs_ofs); |
| 1043 | |
| 1044 | /* write the initializer count */ |
| 1045 | fp_->write_uint4(init_cnt); |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | * end an SINI block |
| 1050 | */ |
| 1051 | void CVmImageWriter::end_sini_block() |
| 1052 | { |
| 1053 | /* end the block using the generic mechanism */ |
| 1054 | end_block(); |
| 1055 | } |
| 1056 |