| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 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 | vmimg_d.cpp - T3 VM image file implementation - DEBUG version |
| 15 | Function |
| 16 | This implements certain functions for the debugger version of the |
| 17 | T3 VM. These functions can be omitted (but must be stubbed out) |
| 18 | for stand-alone non-debug versions. |
| 19 | Notes |
| 20 | |
| 21 | Modified |
| 22 | 12/03/99 MJRoberts - Creation |
| 23 | */ |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <memory.h> |
| 28 | |
| 29 | #include "t3std.h" |
| 30 | #include "vmtype.h" |
| 31 | #include "vminit.h" |
| 32 | #include "vmimage.h" |
| 33 | #include "vmerr.h" |
| 34 | #include "vmerrnum.h" |
| 35 | #include "vmfile.h" |
| 36 | #include "tctok.h" |
| 37 | #include "tcprs.h" |
| 38 | #include "tctarg.h" |
| 39 | #include "tcglob.h" |
| 40 | #include "vmdbg.h" |
| 41 | #include "vmmeta.h" |
| 42 | #include "vmmcreg.h" |
| 43 | |
| 44 | |
| 45 | /* ------------------------------------------------------------------------ */ |
| 46 | /* |
| 47 | * load a Global Symbols block |
| 48 | */ |
| 49 | void CVmImageLoader::load_gsym(VMG_ ulong siz) |
| 50 | { |
| 51 | char buf[TOK_SYM_MAX_LEN + 128]; |
| 52 | |
| 53 | /* |
| 54 | * if there's no global symbol table, merely load into the runtime |
| 55 | * symbol table, since we don't seem to need the information for |
| 56 | * debugging |
| 57 | */ |
| 58 | if (G_prs->get_global_symtab() == 0) |
| 59 | { |
| 60 | /* load it into the runtime reflection symbol table */ |
| 61 | load_runtime_symtab_from_gsym(vmg_ siz); |
| 62 | |
| 63 | /* we're done */ |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * remember the starting seek position and size, so we can re-read the |
| 69 | * block into the runtime symbol table |
| 70 | */ |
| 71 | long init_seek_pos = fp_->get_seek(); |
| 72 | ulong init_siz = siz; |
| 73 | |
| 74 | /* |
| 75 | * note that we have a GSYM block - this implies that the image was |
| 76 | * linked for debugging |
| 77 | */ |
| 78 | has_gsym_ = TRUE; |
| 79 | |
| 80 | /* read the symbol count */ |
| 81 | read_data(buf, 4, &siz); |
| 82 | ulong cnt = t3rp4u(buf); |
| 83 | |
| 84 | /* read the symbols and populate the global symbol table */ |
| 85 | for ( ; cnt != 0 ; --cnt) |
| 86 | { |
| 87 | char *sym_name; |
| 88 | size_t sym_len; |
| 89 | size_t dat_len; |
| 90 | CTcSymbol *sym; |
| 91 | tc_symtype_t sym_type; |
| 92 | char *dat; |
| 93 | |
| 94 | /* read the symbol's length, extra data length, and type code */ |
| 95 | read_data(buf, 6, &siz); |
| 96 | sym_len = osrp2(buf); |
| 97 | dat_len = osrp2(buf + 2); |
| 98 | sym_type = (tc_symtype_t)osrp2(buf + 4); |
| 99 | |
| 100 | /* check the lengths to make sure they don't overflow our buffer */ |
| 101 | if (sym_len > TOK_SYM_MAX_LEN) |
| 102 | { |
| 103 | /* |
| 104 | * this symbol name is too long - skip the symbol and its |
| 105 | * extra data entirely |
| 106 | */ |
| 107 | skip_data(sym_len + dat_len, &siz); |
| 108 | |
| 109 | /* go on to the next symbol */ |
| 110 | continue; |
| 111 | } |
| 112 | else if (dat_len + sym_len > sizeof(buf)) |
| 113 | { |
| 114 | /* |
| 115 | * the extra data block is too long - truncate the extra |
| 116 | * data so that we don't overflow our buffer, but proceed |
| 117 | * anyway with the truncated extra data |
| 118 | */ |
| 119 | read_data(buf, sizeof(buf), &siz); |
| 120 | |
| 121 | /* skip the remainder of the extra data */ |
| 122 | skip_data(sym_len + dat_len - sizeof(buf), &siz); |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | /* read the symbol's name and its type-specific data */ |
| 127 | read_data(buf, sym_len + dat_len, &siz); |
| 128 | } |
| 129 | |
| 130 | /* get the pointer to the extra data (after the name) */ |
| 131 | dat = buf + sym_len; |
| 132 | |
| 133 | /* |
| 134 | * allocate a copy of the symbol name in parser memory - this is |
| 135 | * necessary because the symbol objects themselves always come |
| 136 | * from parser memory, and hence never get individually deleted |
| 137 | */ |
| 138 | sym_name = (char *)G_prsmem->alloc(sym_len); |
| 139 | memcpy(sym_name, buf, sym_len); |
| 140 | |
| 141 | /* create the new symbol table entry, depending on its type */ |
| 142 | switch(sym_type) |
| 143 | { |
| 144 | case TC_SYM_FUNC: |
| 145 | /* create the function symbol */ |
| 146 | sym = new CTcSymFunc( |
| 147 | sym_name, sym_len, FALSE, |
| 148 | (int)osrp2(dat + 4), /* argc */ |
| 149 | |
| 150 | /* |
| 151 | * the optional argument count is present in 3.1+ records, |
| 152 | * which are at least 10 bytes; otherwise there aren't any |
| 153 | * optional arguments |
| 154 | */ |
| 155 | (dat_len >= 10 ? osrp2(dat+8) : 0), /* opt_argc */ |
| 156 | |
| 157 | dat[6] != 0, /* varargs */ |
| 158 | dat[7] != 0, /* has_retval */ |
| 159 | FALSE, /* is_multimethod */ |
| 160 | FALSE, /* is_multimethod_base */ |
| 161 | FALSE, /* is_extern */ |
| 162 | TRUE); /* has_proto */ |
| 163 | |
| 164 | /* add the reverse mapping entry */ |
| 165 | G_debugger->add_rev_sym(sym_name, sym_len, sym_type, t3rp4u(dat)); |
| 166 | |
| 167 | /* set the function's absolute address */ |
| 168 | ((CTcSymFunc *)sym)->set_abs_addr(t3rp4u(dat)); |
| 169 | break; |
| 170 | |
| 171 | case TC_SYM_OBJ: |
| 172 | /* create the object symbol */ |
| 173 | sym = new CTcSymObj(sym_name, sym_len, FALSE, t3rp4u(dat), FALSE, |
| 174 | TC_META_TADSOBJ, 0); |
| 175 | |
| 176 | /* if there's a modifying object, store it */ |
| 177 | if (dat_len >= 8) |
| 178 | ((CTcSymObj *)sym)->set_modifying_obj_id(t3rp4u(dat + 4)); |
| 179 | else |
| 180 | ((CTcSymObj *)sym)->set_modifying_obj_id(VM_INVALID_OBJ); |
| 181 | |
| 182 | /* add the reverse mapping entry */ |
| 183 | G_debugger->add_rev_sym(sym_name, sym_len, sym_type, t3rp4u(dat)); |
| 184 | break; |
| 185 | |
| 186 | case TC_SYM_PROP: |
| 187 | /* create the property symbol */ |
| 188 | sym = new CTcSymProp(sym_name, sym_len, FALSE, osrp2(dat)); |
| 189 | |
| 190 | /* set the 'dictionary property' flag if present */ |
| 191 | if (dat_len >= 3 && (buf[2] & 1) != 0) |
| 192 | ((CTcSymProp *)sym)->set_vocab(TRUE); |
| 193 | |
| 194 | /* add the reverse mapping entry */ |
| 195 | G_debugger->add_rev_sym(sym_name, sym_len, sym_type, osrp2(dat)); |
| 196 | break; |
| 197 | |
| 198 | case TC_SYM_ENUM: |
| 199 | /* create the enumerator symbol */ |
| 200 | sym = new CTcSymEnum(sym_name, sym_len, FALSE, |
| 201 | t3rp4u(dat), |
| 202 | (dat_len >= 5 && (buf[4] & 1) != 0)); |
| 203 | |
| 204 | /* add the reverse mapping entry */ |
| 205 | G_debugger->add_rev_sym(sym_name, sym_len, sym_type, t3rp4u(dat)); |
| 206 | break; |
| 207 | |
| 208 | case TC_SYM_BIF: |
| 209 | /* create the built-in function symbol */ |
| 210 | sym = new CTcSymBif(sym_name, sym_len, FALSE, |
| 211 | osrp2(dat + 2), /* function set ID */ |
| 212 | osrp2(dat), /* function index */ |
| 213 | dat[4] != 0, /* has_retval */ |
| 214 | osrp2(dat + 5), /* min_argc */ |
| 215 | osrp2(dat + 7), /* max_argc */ |
| 216 | dat[9] != 0); /* varargs */ |
| 217 | |
| 218 | /* add the reverse mapping entry */ |
| 219 | G_debugger->add_rev_sym(sym_name, sym_len, sym_type, |
| 220 | (osrp2(dat+2) << 16) | osrp2(dat)); |
| 221 | break; |
| 222 | |
| 223 | case TC_SYM_EXTFN: |
| 224 | /* not currently supported */ |
| 225 | sym = 0; |
| 226 | break; |
| 227 | |
| 228 | case TC_SYM_METACLASS: |
| 229 | /* create the metaclass symbol */ |
| 230 | sym = new CTcSymMetaclass(sym_name, sym_len, FALSE, |
| 231 | osrp2(dat), t3rp4u(dat + 2)); |
| 232 | |
| 233 | /* |
| 234 | * add a reverse mapping symbol for the object instance |
| 235 | * (note that we add this as an object, not a metaclass, |
| 236 | * since it refers to the IntrinsicClass instance) |
| 237 | */ |
| 238 | if ((vm_obj_id_t)t3rp4u(dat + 2) != VM_INVALID_OBJ) |
| 239 | G_debugger->add_rev_sym(sym_name, sym_len, |
| 240 | TC_SYM_OBJ, t3rp4u(dat + 2)); |
| 241 | break; |
| 242 | |
| 243 | default: |
| 244 | /* ignore other types of symbols */ |
| 245 | sym = 0; |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | /* if the symbol was valid, add it to the global symbol table */ |
| 250 | if (sym != 0) |
| 251 | G_prs->get_global_symtab()->add_entry(sym); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Seek back to the starting position, and then load the GSYM data |
| 256 | * into the runtime reflection symbol table |
| 257 | */ |
| 258 | fp_->seek(init_seek_pos); |
| 259 | load_runtime_symtab_from_gsym(vmg_ init_siz); |
| 260 | } |
| 261 | |
| 262 | /* ------------------------------------------------------------------------ */ |
| 263 | /* |
| 264 | * Callback context structure for fix_obj_meta_cb() |
| 265 | */ |
| 266 | struct fix_obj_meta_cb_ctx |
| 267 | { |
| 268 | /* holder for VM globals structure pointer */ |
| 269 | vm_globals *vmg; |
| 270 | |
| 271 | /* translation table - metaclass index to compiler TC_META_xxx ID */ |
| 272 | const tc_metaclass_t *xlat; |
| 273 | }; |
| 274 | |
| 275 | /* |
| 276 | * Global symbol enumeration callback - fix up this symbol's compiler |
| 277 | * metaclass identifier, if this is an object symbol. |
| 278 | */ |
| 279 | static void fix_obj_meta_cb(void *ctx0, CTcSymbol *sym) |
| 280 | { |
| 281 | /* cast our context and set up to access VM globals */ |
| 282 | fix_obj_meta_cb_ctx *ctx = (fix_obj_meta_cb_ctx *)ctx0; |
| 283 | VMGLOB_PTR(ctx->vmg); |
| 284 | |
| 285 | /* if this is an object symbol, fix up its metaclass identifier */ |
| 286 | if (sym->get_type() == TC_SYM_OBJ) |
| 287 | { |
| 288 | CVmObject *obj; |
| 289 | CVmMetaclass *meta; |
| 290 | uint idx; |
| 291 | tc_metaclass_t id; |
| 292 | |
| 293 | /* cast the symbol to an object symbol */ |
| 294 | CTcSymObj *obj_sym = (CTcSymObj *)sym; |
| 295 | |
| 296 | /* get the object */ |
| 297 | obj = vm_objp(vmg_ (vm_obj_id_t)obj_sym->get_obj_id()); |
| 298 | |
| 299 | /* look up its metaclass */ |
| 300 | meta = obj->get_metaclass_reg(); |
| 301 | |
| 302 | /* get the registration table index */ |
| 303 | idx = meta->get_reg_idx(); |
| 304 | |
| 305 | /* that's our index into the translation table - look it up */ |
| 306 | id = ctx->xlat[idx]; |
| 307 | |
| 308 | /* store the compiler metaclass ID back in the symbol */ |
| 309 | obj_sym->set_metaclass(id); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Fix up the global symbol table to set the correct metaclasses for all |
| 315 | * object symbols. This has to wait until we've finished reading the whole |
| 316 | * image file, because we have need to finish reading all of the actual |
| 317 | * objects (specifically the OBJS records) before we can be sure we'll have |
| 318 | * access to all of the metaclass information. |
| 319 | */ |
| 320 | void CVmImageLoader::fix_gsym_meta(VMG0_) |
| 321 | { |
| 322 | tc_metaclass_t *xlat; |
| 323 | size_t i, cnt; |
| 324 | fix_obj_meta_cb_ctx ctx; |
| 325 | |
| 326 | /* if there's no global symbol table, there's nothing to do */ |
| 327 | if (G_prs->get_global_symtab() == 0) |
| 328 | return; |
| 329 | |
| 330 | /* |
| 331 | * Tell the debugger code generator about the actual loaded metaclass |
| 332 | * dependency table. This is necessary so that the code generator can |
| 333 | * generate the correct metaclass index references for this object |
| 334 | * file. First, start the table. |
| 335 | */ |
| 336 | G_cg->start_image_file_meta_table(); |
| 337 | |
| 338 | /* now add each entry from the table */ |
| 339 | for (cnt = G_meta_table->get_count(), i = 0 ; i < cnt ; ++i) |
| 340 | { |
| 341 | /* get this item's name */ |
| 342 | const char *nm = G_meta_table->get_entry(i)->image_meta_name_; |
| 343 | |
| 344 | /* find the version suffix, if any */ |
| 345 | const char *p; |
| 346 | for (p = nm ; *p != '\0' && *p != '/' ; ++p) ; |
| 347 | |
| 348 | /* tell the code generator about the entry */ |
| 349 | G_cg->load_image_file_meta_table(nm, p - nm, i); |
| 350 | } |
| 351 | |
| 352 | /* done with the table */ |
| 353 | G_cg->end_image_file_meta_table(); |
| 354 | |
| 355 | /* build a translation from metaclass index to compiler TC_META_xxx ID */ |
| 356 | xlat = CVmMetaTable::build_runtime_to_compiler_id_table(vmg0_); |
| 357 | |
| 358 | /* now run through the symbol table and fix up each object symbol */ |
| 359 | ctx.vmg = VMGLOB_ADDR; |
| 360 | ctx.xlat = xlat; |
| 361 | G_prs->get_global_symtab()->enum_entries(&fix_obj_meta_cb, &ctx); |
| 362 | |
| 363 | /* free our temporary translation table */ |
| 364 | t3free(xlat); |
| 365 | } |
| 366 | |
| 367 | /* ------------------------------------------------------------------------ */ |
| 368 | /* |
| 369 | * Load a method header list block |
| 370 | */ |
| 371 | void CVmImageLoader::load_mhls(VMG_ ulong siz) |
| 372 | { |
| 373 | char buf[512]; |
| 374 | ulong cnt; |
| 375 | ulong i; |
| 376 | |
| 377 | /* read the count */ |
| 378 | read_data(buf, 4, &siz); |
| 379 | cnt = t3rp4u(buf); |
| 380 | |
| 381 | /* allocate space for the list */ |
| 382 | G_debugger->alloc_method_header_list(cnt); |
| 383 | |
| 384 | /* read the entries */ |
| 385 | for (i = 0 ; cnt != 0 ; ) |
| 386 | { |
| 387 | size_t cur; |
| 388 | char *p; |
| 389 | |
| 390 | /* read as many as we can fit, up to the number remaining */ |
| 391 | cur = sizeof(buf) / 4; |
| 392 | if (cur > cnt) |
| 393 | cur = (size_t)cnt; |
| 394 | |
| 395 | /* read the chunk */ |
| 396 | read_data(buf, cur * 4, &siz); |
| 397 | |
| 398 | /* deduct the number we just read from the number remaining */ |
| 399 | cnt -= cur; |
| 400 | |
| 401 | /* process these entries */ |
| 402 | for (p = buf ; cur != 0 ; --cur, ++i, p += 4) |
| 403 | G_debugger->set_method_header(i, t3rp4u(p)); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /* ------------------------------------------------------------------------ */ |
| 408 | /* |
| 409 | * Error handler for macro loader |
| 410 | */ |
| 411 | class MyLoadMacErr: public CTcTokLoadMacErr |
| 412 | { |
| 413 | public: |
| 414 | /* log an error */ |
| 415 | virtual void log_error(int err) |
| 416 | { |
| 417 | /* throw an error */ |
| 418 | err_throw_a(VMERR_INVAL_IMAGE_MACRO, 1, ERR_TYPE_INT, err); |
| 419 | } |
| 420 | }; |
| 421 | |
| 422 | /* |
| 423 | * Load a Macro Symbols (MACR) block |
| 424 | */ |
| 425 | void CVmImageLoader::load_macros(VMG_ ulong siz) |
| 426 | { |
| 427 | /* |
| 428 | * remember the starting position, so we can load the same data for |
| 429 | * reflection after we load it for the debugger |
| 430 | */ |
| 431 | long init_seek_pos = fp_->get_seek(); |
| 432 | ulong init_siz = siz; |
| 433 | |
| 434 | /* set up a stream object for reading from the MACR block */ |
| 435 | CVmImageFileStream str(fp_, siz); |
| 436 | |
| 437 | /* read the macros */ |
| 438 | MyLoadMacErr err_handler; |
| 439 | G_tok->load_macros_from_file(&str, &err_handler); |
| 440 | |
| 441 | /* |
| 442 | * Seek back to the starting position, and then load the MACR data into |
| 443 | * the runtime reflection symbol table as well |
| 444 | */ |
| 445 | fp_->seek(init_seek_pos); |
| 446 | load_runtime_symtab_from_macr(vmg_ init_siz); |
| 447 | } |