| 1 | /* $Header: d:/cvsroot/tads/tads3/vmwrtimg.h,v 1.4 1999/07/11 00:46:59 MJRoberts Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1999, 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 | vmwrtimg.h - T3 Image File Writer utility functions |
| 12 | Function |
| 13 | |
| 14 | Notes |
| 15 | |
| 16 | Modified |
| 17 | 04/04/99 MJRoberts - Creation |
| 18 | */ |
| 19 | |
| 20 | #ifndef VMWRTIMG_H |
| 21 | #define VMWRTIMG_H |
| 22 | |
| 23 | #include "t3std.h" |
| 24 | #include "vmtype.h" |
| 25 | |
| 26 | |
| 27 | class CVmImageWriter |
| 28 | { |
| 29 | public: |
| 30 | /* create an image writer with a given output file */ |
| 31 | CVmImageWriter(class CVmFile *fp); |
| 32 | |
| 33 | /* delete the image writer */ |
| 34 | ~CVmImageWriter(); |
| 35 | |
| 36 | /* get the current seek location in the underlying file */ |
| 37 | long get_pos() const; |
| 38 | |
| 39 | /* |
| 40 | * Prepare the file: write out the fixed header information. This |
| 41 | * should be called once, before doing any other writing. 'vsn' is |
| 42 | * the image file version number to store in the file. |
| 43 | */ |
| 44 | void prepare(uint vsn, const char tool_data[4]); |
| 45 | |
| 46 | /* |
| 47 | * Begin a block with the specified ID (a four-byte string). Writes |
| 48 | * the block header, and remembers where the block starts so that |
| 49 | * the size prefix can be stored when the block is finished. Blocks |
| 50 | * cannot be nested; starting a new block automatically ends the |
| 51 | * previous block. |
| 52 | */ |
| 53 | void begin_block(const char *block_id, int mandatory); |
| 54 | |
| 55 | /* |
| 56 | * End the current block. |
| 57 | */ |
| 58 | void end_block(); |
| 59 | |
| 60 | /* write raw bytes to the image file */ |
| 61 | void write_bytes(const char *ptr, uint32_t siz); |
| 62 | |
| 63 | /* |
| 64 | * Write a complete entrypoint block, given the code offset of the |
| 65 | * entrypoint and the various table entry sizes. If a block is in |
| 66 | * progress, this will terminate it. |
| 67 | */ |
| 68 | void write_entrypt(uint32_t entry_ofs, size_t method_header_size, |
| 69 | size_t exc_entry_size, size_t line_entry_size, |
| 70 | size_t dbg_hdr_size, size_t dbg_lclsym_hdr_size, |
| 71 | size_t dbg_frame_hdr_size, |
| 72 | int dbg_vsn_id); |
| 73 | |
| 74 | /* |
| 75 | * Write a complete function set dependency block, given an array of |
| 76 | * function set names. If a block is in progress, this will |
| 77 | * terminate it. |
| 78 | */ |
| 79 | void write_func_dep(const char **funcset_names, int count); |
| 80 | |
| 81 | /* |
| 82 | * Write a complete metaclass dependency block, given an array of |
| 83 | * metaclass names. If a block is in progress, this will terminate |
| 84 | * it. |
| 85 | */ |
| 86 | void write_meta_dep(const char **metaclass_names, int count); |
| 87 | |
| 88 | /* |
| 89 | * Write a metaclass dependency block in pieces. Call |
| 90 | * begin_meta_dep() with the number of metaclasses, then call |
| 91 | * write_meta_dep_item() to write each item, and finally call |
| 92 | * end_meta_dep() to finish the block. |
| 93 | */ |
| 94 | void begin_meta_dep(int count); |
| 95 | void write_meta_dep_item(const char *metaclass_name); |
| 96 | void write_meta_item_prop(uint prop_id); |
| 97 | void end_meta_prop_list(); |
| 98 | void end_meta_dep(); |
| 99 | |
| 100 | /* |
| 101 | * Write a function set dependency block in pieces |
| 102 | */ |
| 103 | void begin_func_dep(int count) |
| 104 | { begin_dep_block("FNSD", count); } |
| 105 | void write_func_dep_item(const char *funcset_name) |
| 106 | { write_dep_block_item(funcset_name); } |
| 107 | void end_func_dep() |
| 108 | { end_dep_block(); } |
| 109 | |
| 110 | /* |
| 111 | * Write a constant pool definition block. |
| 112 | */ |
| 113 | void write_pool_def(uint pool_id, uint32_t page_count, uint32_t page_size, |
| 114 | int mandatory); |
| 115 | |
| 116 | /* |
| 117 | * Fix up a constant pool definition block with the actual number of |
| 118 | * pages. This can be used, if desired, to wait to determine the |
| 119 | * actual number of pages in a given constant pool until after |
| 120 | * writing the pages. Since the pool definition block must precede |
| 121 | * the pool's first page block in the image file, it's impossible to |
| 122 | * wait until after writing all of the pages to write the definition |
| 123 | * block. Instead, the caller must write the pool definition block |
| 124 | * first, using a temporary placeholder value for the page_count (0 |
| 125 | * will suffice), then write the pool pages, then use this to fix up |
| 126 | * the pool definition block with the actual number of pages. The |
| 127 | * caller must note the seek position prior to writing the pool |
| 128 | * definition block, so that we can seek back to that position to |
| 129 | * fix up the definition block. |
| 130 | * |
| 131 | * Callers need not use this function if they know the actual number |
| 132 | * of pages in the pool when writing the original pool definition |
| 133 | * block. |
| 134 | */ |
| 135 | void fix_pool_def(long def_seek_ofs, uint32_t page_count); |
| 136 | |
| 137 | /* |
| 138 | * Write a constant/code pool page. This writes the entire page |
| 139 | * with its header and data in a single operation; this is the |
| 140 | * easiest way to write a page when the page has been fully |
| 141 | * constructed in a single memory block in advance. |
| 142 | */ |
| 143 | void write_pool_page(uint pool_id, uint32_t page_index, |
| 144 | const char *page_data, uint32_t page_data_size, |
| 145 | int mandatory, uchar xor_mask); |
| 146 | |
| 147 | /* |
| 148 | * Write a constant/code pool page in pieces. These routines can be |
| 149 | * used when the data in the page are not contiguous in memory and |
| 150 | * must be written in pieces. Start by calling begin_pool_page(), |
| 151 | * then call write_pool_page_bytes() for each item; the items are |
| 152 | * written contiguously to the page. Finish by calling |
| 153 | * end_pool_page(). |
| 154 | */ |
| 155 | void begin_pool_page(uint pool_id, uint32_t page_index, int mandatory, |
| 156 | uchar xor_mask); |
| 157 | void write_pool_page_bytes(const char *buf, uint32_t siz, uchar xor_mask); |
| 158 | void end_pool_page(); |
| 159 | |
| 160 | /* |
| 161 | * Write items in the symbolic names block. Start with |
| 162 | * begin_sym_block(), then call the write_sym_item_xxx() functions |
| 163 | * to write the names. Finally, call end_sym_block() when done. |
| 164 | */ |
| 165 | void begin_sym_block(); |
| 166 | |
| 167 | void write_sym_item_objid(const char *nm, size_t len, ulong obj_id); |
| 168 | void write_sym_item_propid(const char *nm, size_t len, uint prop_id); |
| 169 | void write_sym_item_func(const char *nm, size_t len, ulong code_ofs); |
| 170 | void write_sym_item(const char *nm, size_t nmlen, |
| 171 | const struct vm_val_t *val); |
| 172 | |
| 173 | void write_sym_item_objid(const char *nm, ulong obj_id) |
| 174 | { write_sym_item_objid(nm, get_strlen(nm), obj_id); } |
| 175 | void write_sym_item_propid(const char *nm, uint prop_id) |
| 176 | { write_sym_item_propid(nm, get_strlen(nm), prop_id); } |
| 177 | void write_sym_item(const char *nm, const struct vm_val_t *val) |
| 178 | { write_sym_item(nm, get_strlen(nm), val); } |
| 179 | |
| 180 | void end_sym_block(); |
| 181 | |
| 182 | |
| 183 | /* |
| 184 | * Write items in an OBJS (static object data) block. Start with |
| 185 | * begin_objs_block(), then call write_objs_bytes() repeatedly to |
| 186 | * write the bytes. Finally, call end_objs_block() when done. |
| 187 | * |
| 188 | * If the 'large_objects' field is set, the objects in the block use |
| 189 | * 32-bit size fields; otherwise the objects use 16-bit size fields. |
| 190 | * |
| 191 | * If 'trans' is true, the objects in this block are transient; |
| 192 | * otherwise, the objects are non-transient (i.e., persistent). |
| 193 | */ |
| 194 | void begin_objs_block(uint metaclass_idx, int large_objects, int trans); |
| 195 | void write_objs_bytes(const char *buf, uint32_t siz); |
| 196 | void end_objs_block(uint object_count); |
| 197 | |
| 198 | /* |
| 199 | * Write the items in a SRCF (source file descriptor) block. Start |
| 200 | * with begin_srcf_block(), then write the file entries. For each |
| 201 | * entry, call begin_src_entry(), then call write_src_line_entry() |
| 202 | * for each source line debug record, then call end_srcf_entry(). |
| 203 | * Call end_srcf_block() when done with all file entries. |
| 204 | */ |
| 205 | void begin_srcf_block(int count); |
| 206 | void begin_srcf_entry(int orig_index, const char *fname); |
| 207 | void write_srcf_line_entry(ulong linenum, ulong addr); |
| 208 | void end_srcf_entry(); |
| 209 | void end_srcf_block(); |
| 210 | |
| 211 | /* |
| 212 | * Write the items in a GSYM (global symbol table) block. Start |
| 213 | * with begin_gsym_block(), then call write_gsym_entry() repeatedly |
| 214 | * to write the entries. Call end_gsym_block() when done. |
| 215 | */ |
| 216 | void begin_gsym_block(); |
| 217 | void write_gsym_entry(const char *sym, size_t sym_len, |
| 218 | int type_id, const char *dat, size_t dat_len); |
| 219 | void end_gsym_block(ulong count); |
| 220 | |
| 221 | /* |
| 222 | * Begin MHLS block, write an item, and end the block |
| 223 | */ |
| 224 | void begin_mhls_block(); |
| 225 | void write_mhls_entry(ulong code_addr); |
| 226 | void end_mhls_block(); |
| 227 | |
| 228 | /* |
| 229 | * Begin/end SINI block. static_cs_ofs is the offset in the code |
| 230 | * segment of the first static initializer; this is useful in the |
| 231 | * image file because we can delete all of the code pages starting |
| 232 | * at this point after pre-initialization is complete. |
| 233 | */ |
| 234 | void begin_sini_block(ulong static_cs_ofs, ulong init_cnt); |
| 235 | void end_sini_block(); |
| 236 | |
| 237 | /* begin/end a MACR (macro symbols) block */ |
| 238 | void begin_macr_block(); |
| 239 | void end_macr_block(); |
| 240 | |
| 241 | /* |
| 242 | * Finish the file. Automatically ends the current block if a block |
| 243 | * is open, and writes the end-of-file marker. |
| 244 | */ |
| 245 | void finish(); |
| 246 | |
| 247 | /* |
| 248 | * get the underlying file object; for some types of blocks, it's |
| 249 | * simplest for the caller to write the data directly to the underlying |
| 250 | * file stream without any help from us |
| 251 | */ |
| 252 | class CVmFile *get_fp() const { return fp_; } |
| 253 | |
| 254 | private: |
| 255 | /* write a generic dependency (function set, metaclass) block */ |
| 256 | void write_dep_block(const char *block_id, const char **names, int count); |
| 257 | |
| 258 | /* write a dependency block in pieces */ |
| 259 | void begin_dep_block(const char *block_id, int count); |
| 260 | void write_dep_block_item(const char *nm); |
| 261 | void end_dep_block(); |
| 262 | |
| 263 | /* XOR a block of bytes with a mask and write the results to the file */ |
| 264 | void xor_and_write_bytes(const char *p, uint32_t len, uchar xor_mask); |
| 265 | |
| 266 | /* underlying file */ |
| 267 | class CVmFile *fp_; |
| 268 | |
| 269 | /* |
| 270 | * Seek position of start of current block. If this is zero, no |
| 271 | * block is currently open. |
| 272 | */ |
| 273 | long block_start_; |
| 274 | |
| 275 | /* count of symbolic names written so far (for writing SYMD block) */ |
| 276 | int symd_cnt_; |
| 277 | |
| 278 | /* location of metaclass entry next-record offset */ |
| 279 | long mcld_ofs_pos_; |
| 280 | |
| 281 | /* location of metaclass property list count prefix */ |
| 282 | long mcld_propcnt_pos_; |
| 283 | |
| 284 | /* count of properties writeen in a metaclass prop list so far */ |
| 285 | int mcld_prop_cnt_; |
| 286 | |
| 287 | /* seek location of SYMD count prefix, for fixing up at end of block */ |
| 288 | long symd_prefix_; |
| 289 | |
| 290 | /* seek location of OBJS count prefix, for fixup up at end of block */ |
| 291 | long objs_prefix_; |
| 292 | |
| 293 | /* seek location of GSYM count prefix, for fixup at end of block */ |
| 294 | long gsym_prefix_; |
| 295 | |
| 296 | /* start of current SRCF file entry */ |
| 297 | long srcf_entry_pos_; |
| 298 | |
| 299 | /* count of SRCF line entries */ |
| 300 | long srcf_line_cnt_; |
| 301 | |
| 302 | /* position of SRCF line entry for the current file */ |
| 303 | long srcf_line_pos_; |
| 304 | |
| 305 | /* position of MHLS block count entry, and MHLS entry count so far */ |
| 306 | long mhls_cnt_pos_; |
| 307 | ulong mhls_cnt_; |
| 308 | }; |
| 309 | |
| 310 | #endif /* VMWRTIMG_H */ |
| 311 |