Imported support for new build system.
10 #include "zlib/zlib.h"
19 FS_Code, /* required */
20 FS_Data, /* required */
24 FS_Nametable, /* required */
39 int pc_printf(const char *message,...);
40 int pc_compile(int argc, char **argv);
41 void sfwrite(const void *buf, size_t size, size_t count, sp_file_t *spf);
43 memfile_t *bin_file = NULL;
46 #define sARGS_MAX 32 /* number of arguments a function can have, max */
47 #define sDIMEN_MAX 4 /* maximum number of array dimensions */
49 typedef struct t_arg_s
55 sp_fdbg_arraydim_t dims[sDIMEN_MAX];
58 typedef struct t_native_s
63 t_arg args[sARGS_MAX];
66 t_native *native_list = NULL;
68 int main(int argc, char *argv[])
73 if (pc_compile(argc,argv) == 0)
76 AMX_DBG_HDR *dbg = NULL;
80 memfile_t *dbgtab = NULL; //dbgcrab
81 unsigned char *dbgptr = NULL;
82 uint32_t sections[FS_Number] = {1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0};
90 hdr = (AMX_HEADER *)bin_file->base;
92 if ((spf=spfw_create(bin_file->name, NULL)) == NULL)
94 pc_printf("Error creating binary file!\n");
95 memfile_destroy(bin_file);
99 if ((err=setjmp(brkout))!=0)
104 spfw_add_section(spf, ".code");
105 spfw_add_section(spf, ".data");
107 sections[FS_Publics] = (hdr->natives - hdr->publics) / hdr->defsize;
108 if (sections[FS_Publics])
110 spfw_add_section(spf, ".publics");
112 sections[FS_Pubvars] = (hdr->tags - hdr->pubvars) / hdr->defsize;
113 if (sections[FS_Pubvars])
115 spfw_add_section(spf, ".pubvars");
117 sections[FS_Natives] = (hdr->libraries - hdr->natives) / hdr->defsize;
118 if (sections[FS_Natives])
120 spfw_add_section(spf, ".natives");
122 sections[FS_Tags] = (hdr->nametable - hdr->tags) / hdr->defsize;
123 if (sections[FS_Tags])
125 spfw_add_section(spf, ".tags");
128 spfw_add_section(spf, ".names");
130 if (hdr->flags & AMX_FLAG_DEBUG)
132 dbg = (AMX_DBG_HDR *)((unsigned char *)hdr + hdr->size);
133 if (dbg->magic != AMX_DBG_MAGIC)
135 pc_printf("Error reading AMX_DBG_HDR, debug data will not be written.");
137 dbgtab = memfile_creat("", 512);
138 dbgptr = (unsigned char *)dbg + sizeof(AMX_DBG_HDR);
139 if ((sections[FS_DbgNatives] = sections[FS_Natives]) > 0)
141 spfw_add_section(spf, ".dbg.natives");
145 spfw_add_section(spf, ".dbg.files");
146 sections[FS_DbgFile] = dbg->files;
150 spfw_add_section(spf, ".dbg.lines");
151 sections[FS_DbgLine] = dbg->lines;
155 spfw_add_section(spf, ".dbg.symbols");
156 sections[FS_DbgSymbol] = dbg->symbols;
158 sections[FS_DbgInfo] = 1;
159 sections[FS_DbgStrings] = 1;
160 spfw_add_section(spf, ".dbg.info");
161 spfw_add_section(spf, ".dbg.strings");
165 spfw_finalize_header(spf);
168 * Begin writing each of our known tables out
171 if (sections[FS_Code])
174 unsigned char *cbase;
176 cod.cellsize = sizeof(cell);
178 cod.codesize = hdr->dat - hdr->cod;
179 cod.codeversion = hdr->amx_version;
181 if (hdr->flags & AMX_FLAG_DEBUG)
183 cod.flags |= SP_FLAG_DEBUG;
185 cod.code = sizeof(cod);
189 cbase = (unsigned char *)hdr + hdr->cod;
190 sfwrite(&cod, sizeof(cod), 1, spf);
191 sfwrite(cbase, cod.codesize, 1, spf);
193 spfw_next_section(spf);
196 if (sections[FS_Data])
199 unsigned char *dbase = (unsigned char *)hdr + hdr->dat;
201 dat.datasize = hdr->hea - hdr->dat;
202 dat.memsize = hdr->stp;
203 dat.data = sizeof(dat);
206 sfwrite(&dat, sizeof(dat), 1, spf);
211 sfwrite(dbase, dat.datasize, 1, spf);
214 spfw_next_section(spf);
217 if (sections[FS_Publics])
219 sp_file_publics_t *pbtbl;
220 AMX_FUNCSTUBNT *stub;
221 unsigned char *stubptr;
222 uint32_t publics = sections[FS_Publics];
224 pbtbl = (sp_file_publics_t *)malloc(sizeof(sp_file_publics_t) * publics);
225 stubptr = (unsigned char *)hdr + hdr->publics;
227 for (i=0; i<publics; i++)
229 stub = (AMX_FUNCSTUBNT *)stubptr;
230 pbtbl[i].address = stub->address;
231 pbtbl[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t));
233 stubptr += hdr->defsize;
237 sfwrite(pbtbl, sizeof(sp_file_publics_t), publics, spf);
241 spfw_next_section(spf);
244 if (sections[FS_Pubvars])
246 sp_file_pubvars_t *pbvars;
247 AMX_FUNCSTUBNT *stub;
248 unsigned char *stubptr;
249 uint32_t pubvars = sections[FS_Pubvars];
251 pbvars = (sp_file_pubvars_t *)malloc(sizeof(sp_file_pubvars_t) * pubvars);
252 stubptr = (unsigned char *)hdr + hdr->pubvars;
254 for (i=0; i<pubvars; i++)
256 stub = (AMX_FUNCSTUBNT *)stubptr;
257 pbvars[i].address = stub->address;
258 pbvars[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t));
260 stubptr += hdr->defsize;
264 sfwrite(pbvars, sizeof(sp_file_pubvars_t), pubvars, spf);
267 spfw_next_section(spf);
270 if (sections[FS_Natives])
272 sp_file_natives_t *nvtbl;
273 AMX_FUNCSTUBNT *stub;
274 unsigned char *stubptr;
275 uint32_t natives = (hdr->libraries - hdr->natives) / hdr->defsize;
277 nvtbl = (sp_file_natives_t *)malloc(sizeof(sp_file_natives_t) * natives);
278 stubptr = (unsigned char *)hdr + hdr->natives;
280 for (i=0; i<natives; i++)
282 stub = (AMX_FUNCSTUBNT *)stubptr;
283 nvtbl[i].name = stub->nameofs - (hdr->nametable + sizeof(uint16_t));
285 stubptr += hdr->defsize;
289 sfwrite(nvtbl, sizeof(sp_file_natives_t), natives, spf);
292 spfw_next_section(spf);
295 if (sections[FS_Tags])
297 uint32_t numTags = (uint32_t)sections[FS_Tags];
298 AMX_FUNCSTUBNT *stub;
301 for (i=0; i<numTags; i++)
303 stub = (AMX_FUNCSTUBNT *)((unsigned char *)hdr + hdr->tags + (i * hdr->defsize));
304 tag.tag_id = stub->address;
305 tag.name = stub->nameofs - (hdr->nametable + sizeof(uint16_t));
306 sfwrite(&tag, sizeof(sp_file_tag_t), 1, spf);
308 spfw_next_section(spf);
311 if (sections[FS_Nametable])
316 /* write the entire block */
317 base = (unsigned char *)hdr + hdr->nametable + sizeof(uint16_t);
319 * note - the name table will be padded to sizeof(cell) bytes.
320 * this may clip at most an extra three bytes in!
322 namelen = hdr->cod - hdr->nametable;
323 sfwrite(base, namelen, 1, spf);
324 spfw_next_section(spf);
327 if (hdr->flags & AMX_FLAG_DEBUG)
331 memset(&info, 0, sizeof(sp_fdbg_info_t));
333 if (sections[FS_Natives])
338 uint32_t natives = (hdr->libraries - hdr->natives) / hdr->defsize;
340 sfwrite(&natives, sizeof(uint32_t), 1, spf);
341 for (idx=0; idx<natives; idx++)
343 sfwrite(&idx, sizeof(uint32_t), 1, spf);
344 name = (uint32_t)memfile_tell(dbgtab);
345 memfile_write(dbgtab, native_list[idx].name, strlen(native_list[idx].name) + 1);
346 sfwrite(&name, sizeof(uint32_t), 1, spf);
347 sfwrite(&native_list[idx].ret_tag, sizeof(int16_t), 1, spf);
348 sfwrite(&native_list[idx].num_args, sizeof(uint16_t), 1, spf);
350 /* Go through arguments */
351 for (j = 0; j < native_list[idx].num_args; j++)
353 sfwrite(&native_list[idx].args[j].ident, sizeof(uint8_t), 1, spf);
354 sfwrite(&native_list[idx].args[j].tag, sizeof(int16_t), 1, spf);
355 sfwrite(&native_list[idx].args[j].dimcount, sizeof(uint16_t), 1, spf);
356 name = (uint32_t)memfile_tell(dbgtab);
357 sfwrite(&name, sizeof(uint32_t), 1, spf);
358 memfile_write(dbgtab,
359 native_list[idx].args[j].name,
360 strlen(native_list[idx].args[j].name) + 1);
361 if (native_list[idx].args[j].dimcount)
363 sfwrite(native_list[idx].args[j].dims,
364 sizeof(sp_fdbg_arraydim_t),
365 native_list[idx].args[j].dimcount,
368 free(native_list[idx].args[j].name);
370 free(native_list[idx].name);
373 spfw_next_section(spf);
376 if (sections[FS_DbgFile])
379 sp_fdbg_file_t dbgfile;
382 for (idx=0; idx<sections[FS_DbgFile]; idx++)
385 _ptr = (AMX_DBG_FILE *)dbgptr;
386 len = strlen(_ptr->name);
388 dbgfile.addr = _ptr->address;
389 dbgfile.name = (uint32_t)memfile_tell(dbgtab);
390 sfwrite(&dbgfile, sizeof(sp_fdbg_file_t), 1, spf);
391 /* write to tab, then move to next */
392 memfile_write(dbgtab, _ptr->name, len + 1);
393 dbgptr += sizeof(AMX_DBG_FILE) + len;
396 spfw_next_section(spf);
399 if (sections[FS_DbgLine])
403 sp_fdbg_line_t dbgline;
404 for (idx=0; idx<sections[FS_DbgLine]; idx++)
407 line = (AMX_DBG_LINE *)dbgptr;
409 dbgline.addr = (uint32_t)line->address;
410 dbgline.line = (uint32_t)line->line;
411 sfwrite(&dbgline, sizeof(sp_fdbg_line_t), 1, spf);
413 dbgptr += sizeof(AMX_DBG_LINE);
416 spfw_next_section(spf);
419 if (sections[FS_DbgSymbol])
425 sp_fdbg_symbol_t dbgsym;
426 sp_fdbg_arraydim_t dbgdim;
429 for (idx=0; idx<sections[FS_DbgSymbol]; idx++)
432 sym = (AMX_DBG_SYMBOL *)dbgptr;
434 dbgsym.addr = *(int32_t *)&sym->address;
435 dbgsym.tagid = sym->tag;
436 dbgsym.codestart = (uint32_t)sym->codestart;
437 dbgsym.codeend = (uint32_t)sym->codeend;
438 dbgsym.dimcount = (uint16_t)sym->dim;
439 dbgsym.vclass = (uint8_t)sym->vclass;
440 dbgsym.ident = (uint8_t)sym->ident;
441 dbgsym.name = (uint32_t)memfile_tell(dbgtab);
442 sfwrite(&dbgsym, sizeof(sp_fdbg_symbol_t), 1, spf);
444 len = strlen(sym->name);
445 memfile_write(dbgtab, sym->name, len + 1);
447 dbgptr += sizeof(AMX_DBG_SYMBOL) + len;
448 /* look for any dimensions */
450 for (dnum=0; dnum<dbgsym.dimcount; dnum++)
453 dim = (AMX_DBG_SYMDIM *)dbgptr;
455 dbgdim.size = (uint32_t)dim->size;
456 dbgdim.tagid = dim->tag;
457 sfwrite(&dbgdim, sizeof(sp_fdbg_arraydim_t), 1, spf);
459 dbgptr += sizeof(AMX_DBG_SYMDIM);
463 spfw_next_section(spf);
466 sfwrite(&info, sizeof(sp_fdbg_info_t), 1, spf);
467 spfw_next_section(spf);
469 if (sections[FS_DbgStrings])
471 sfwrite(dbgtab->base, sizeof(char), dbgtab->usedoffs, spf);
472 spfw_next_section(spf);
476 spfw_finalize_all(spf);
480 * new block for scoping only
483 memfile_t *pOrig = (memfile_t *)spf->handle;
485 unsigned char *proper;
492 /* reuse this memory block! */
493 memfile_reset(bin_file);
495 /* copy tip of header */
496 memfile_write(bin_file, pOrig->base, sizeof(sp_file_hdr_t));
498 /* get pointer to header */
499 pHdr = (sp_file_hdr_t *)bin_file->base;
501 /* copy the rest of the header */
502 memfile_write(bin_file,
503 (unsigned char *)pOrig->base + sizeof(sp_file_hdr_t),
504 pHdr->dataoffs - sizeof(sp_file_hdr_t));
506 header_size = pHdr->dataoffs;
507 size = pHdr->imagesize - header_size;
508 proper = (unsigned char *)pOrig->base + header_size;
510 /* get initial size estimate */
511 disksize = compressBound(pHdr->imagesize);
512 pHdr->disksize = (uint32_t)disksize;
513 zcmp = (Bytef *)malloc(pHdr->disksize);
515 if ((err=compress2(zcmp,
523 pc_printf("Unable to compress (Z): error %d\n", err);
524 pc_printf("Falling back to no compression.");
525 memfile_write(bin_file,
529 pHdr->disksize = (uint32_t)disksize + header_size;
530 pHdr->compression = SPFILE_COMPRESSION_GZ;
531 memfile_write(bin_file,
532 (unsigned char *)zcmp,
539 memfile_destroy(dbgtab);
544 if ((fp=fopen(bin_file->name, "wb")) != NULL)
546 fwrite(bin_file->base, bin_file->usedoffs, 1, fp);
549 pc_printf("Unable to open %s for writing!", bin_file->name);
552 memfile_destroy(bin_file);
557 pc_printf("Error writing to file: %s", bin_file->name);
560 unlink(bin_file->name);
561 memfile_destroy(bin_file);
562 memfile_destroy(dbgtab);
570 void sfwrite(const void *buf, size_t size, size_t count, sp_file_t *spf)
572 if (spf->funcs.fnWrite(buf, size, count, spf->handle) != count)
578 void sp_fdbg_ntv_start(int num_natives)
580 if (num_natives == 0)
585 native_list = (t_native *)malloc(sizeof(t_native) * num_natives);
586 memset(native_list, 0, sizeof(t_native) * num_natives);
591 void sp_fdbg_ntv_hook(int index, symbol *sym)
596 native = &native_list[index];
597 native->name = strdup(sym->name);
599 for (i = 0; i < sMAXARGS; i++)
601 if (sym->dim.arglist[i].ident == 0)
606 native->args[i].tag = sym->dim.arglist[i].tags == NULL ? 0 : sym->dim.arglist[i].tags[0];
607 native->args[i].name = strdup(sym->dim.arglist[i].name);
608 native->args[i].ident = sym->dim.arglist[i].ident;
609 native->args[i].dimcount = sym->dim.arglist[i].numdim;
610 for (j = 0; j < native->args[i].dimcount; j++)
612 native->args[i].dims[j].size = sym->dim.arglist[i].dim[j];
613 native->args[i].dims[j].tagid = sym->dim.arglist[i].idxtag[j];
617 native->ret_tag = sym->tag;