1
0
mirror of https://github.com/fachat/xa65.git synced 2025-04-10 00:37:02 +00:00

Fix crazy memory corruption bug caused by listing not interpreting the

internal data structures correctly. .dsb and .bin listing is still wrong
This commit is contained in:
fachat 2012-08-15 13:42:58 +02:00
parent 6ec6887e3d
commit 20b1e2cc08
5 changed files with 89 additions and 37 deletions

View File

@ -681,6 +681,8 @@ static int pass2(void)
l=afile->mn.tmp[afile->mn.tmpe++];
ll=l;
// printf("%p: l=%d first=%02x\n", afile->mn.tmp+afile->mn.tmpe-1, l, 0xff & afile->mn.tmp[afile->mn.tmpe]);
if(!l)
{
if(afile->mn.tmp[afile->mn.tmpe]==T_LINE)
@ -1041,7 +1043,9 @@ static int x_init(void)
static int puttmp(int c)
{
int er=E_NOMEM;
/*printf("puttmp: afile=%p, tmp=%p, tmpz=%d\n",afile, afile?afile->mn.tmp:0, afile?afile->mn.tmpz:0);*/
//printf("puttmp: %02x -> %p \n",0xff & c, afile->mn.tmp+afile->mn.tmpz);
if(afile->mn.tmpz<TMPMEM)
{
afile->mn.tmp[afile->mn.tmpz++]=c;
@ -1053,14 +1057,19 @@ static int puttmp(int c)
static int puttmps(signed char *s, int l)
{
int i=0,er=E_NOMEM;
// printf("puttmps %d bytes from %p to %p:", l, s, afile->mn.tmp+afile->mn.tmpz);
if(afile->mn.tmpz+l<TMPMEM)
{
while(i<l)
afile->mn.tmp[afile->mn.tmpz++]=s[i++];
while(i<l) {
//printf(" %02x", 0xff & s[i]);
afile->mn.tmp[afile->mn.tmpz++]=s[i++];
}
er=E_OK;
}
// printf("\n");
return(er);
}

View File

@ -448,6 +448,10 @@ void l_addocc(int n, int *v, int *afl) {
/* for the list functionality */
char *l_get_name(int n, label_t *is_cll) {
if (n > afile->la.ltm) {
fprintf(stderr, "Corrupted structures! n=%d, but max=%d\n", n, afile->la.ltm);
exit(1);
}
ltp=afile->la.lt+n;
*is_cll = ltp->is_cll;
return ltp->n;

View File

@ -339,15 +339,21 @@ void do_listing(signed char *listing, int listing_len, signed char *bincode, int
buf += list_tokens(buf, listing + 3, listing_len - 3);
#ifdef LISTING_DEBUG
#if 1 //def LISTING_DEBUG
/* for now only do a hex dump so we see what actually happens */
i = buf - outline;
if (i<80) buf += list_nchar(buf, ' ', 80-i);
{
char valbuf[32];
i = buf - outline;
if (i<80) buf += list_nchar(buf, ' ', 80-i);
buf += list_string(buf, " >>");
for (i = 3; i < listing_len; i++) {
buf += list_string(buf, " >>");
sprintf(valbuf, "%p", listing+3);
buf += list_string(buf, valbuf);
buf += list_sp(buf);
for (i = 3; i < listing_len; i++) {
buf = buf + list_byte(buf, listing[i]);
buf = buf + list_sp(buf);
}
}
#endif
buf[0] = 0;
@ -464,7 +470,7 @@ int list_tokens(char *buf, signed char *input, int len) {
outp += list_char(buf+outp, ':');
}
if (is_cll != UNNAMED) {
outp += list_string(buf+outp, name);
outp += list_string(buf+outp, name == NULL ? "<null>" : name);
}
if (formatp->end_label != NULL) outp += formatp->end_label(buf+outp);
@ -566,6 +572,12 @@ end:
}
int list_string(char *buf, char *string) {
if (buf == NULL || string == NULL) {
fprintf(stderr, "NULL pointer: buf=%p, string=%p\n", buf, string);
fflush(stderr);
exit(1);
}
int p = 0;
while (string[p] != 0) {
buf[p] = string[p];

View File

@ -41,6 +41,7 @@
/* define this for recursive evaluation output */
#undef DEBUG_RECMAC
#undef DEBUG_REPLACE
char s[MAXLINE];
Datei *filep;
@ -975,11 +976,11 @@ int pgetline(char *t)
er= (er==1) ? E_OK : er ;
if(!er) {
#ifdef DEBUG_RECMAC
printf("<<<: %s\n", in_line);
#ifdef DEBUG_REPLACE
// printf("<<<: %s\n", in_line);
#endif
er=pp_replace(t,in_line,-1,rlist);
#ifdef DEBUG_RECMAC
#ifdef DEBUG_REPLACE
printf(">>>: %s\n", t);
#endif
}

View File

@ -430,6 +430,15 @@ fprintf(stderr, "- p1 %d starting -\n", pc[segment]);
}
/* copy the buffer */
memcpy(t+tlen, t+6+inp, l-inp);
#if 0
printf("t_conv (er=%d, t=%p, tlen=%d, inp=%d):",er, t, tlen, inp);
for(i=0;i<l+6;i++)
printf("%02x,",t[i] & 0xff);
printf("\n");
#endif
// update pointers
t=t+tlen;
l-=inp;
/* the result of this is that we always have a Klisting entry in the buffer
@ -440,13 +449,6 @@ fprintf(stderr, "- p1 %d starting -\n", pc[segment]);
/* return length default is input length */
*ll=l;
#if 0
printf("t_conv (er=%d):",er);
for(i=0;i<l;i++)
printf("%02x,",t[i] & 0xff);
printf("\n");
#endif
/* if text/data produced, then no more fopt allowed in romable mode */
/* TODO: need to check, Kbyte is being remapped to Kbyt. What is the effect here? */
if((romable>1) && (t[inp]<Kopen || t[inp]==Kbyte || t[inp]==Kpcdef)) {
@ -1008,6 +1010,14 @@ fprintf(stderr, "guessing instruction length is %d\n", bl);
if(segment==SEG_ABS) pc[SEG_TEXT]+=bl;
/* adjust length by token listing buffer length */
#if 0
printf("converted: (er=%d, t=%p, ll=%d):",er, t, *ll);
for(i=0;i<*ll;i++)
printf("%02x,",t[i] & 0xff);
printf("\n");
printf("adjusted len=%d\n", *ll+tlen);
#endif
*ll = *ll + tlen;
return(er);
}
@ -1034,32 +1044,48 @@ fprintf(stderr, "guessing instruction length is %d\n", bl);
* *t is the input token list
* *ll is the input length of the token list,
* and the output of how many bytes of the buffer are to be taken
* into the file
* into the file; note that for .dsb and .bin, this does NOT match
* the length in the internal data structures!
*/
int t_p2_l(signed char *t, int *ll, int *al)
{
int er = E_OK;
int l = *ll;
if (l < 0) l = -l;
#if 0
{
printf("t_p2_l (ll=%d, t=%p):", *ll, t);
for(int i=0;i<l;i++)
printf("%02x,",t[i] & 0xff);
printf("\n");
}
#endif
if (t[0] == T_LISTING) {
int tlen;
tlen=((t[2]&255)<<8) | (t[1]&255);
if (*ll<0) {
*ll=(*ll) + tlen;
} else {
*ll=(*ll) - tlen;
}
int tlen;
tlen=((t[2]&255)<<8) | (t[1]&255);
if (*ll<0) {
*ll=(*ll) + tlen;
} else {
*ll=(*ll) - tlen;
}
if (*ll != 0) {
er = t_p2(t+tlen, ll, 1, al);
}
if (*ll != 0) {
er = t_p2(t+tlen, ll, 1, al);
}
/* do the actual listing (*ll-2 as we need to substract the place for the tlen value) */
do_listing(t+3, tlen-3, t+tlen, *ll);
/* do the actual listing (*ll-2 as we need to substract the place for the tlen value) */
do_listing(t+3, tlen-3, t+tlen, *ll);
// adapt back, i.e. remove token listing
// Use the input token length as delimiter.
if (*ll != 0) {
memmove(t, t+tlen, l-tlen);
}
/* adapt back, i.e. remove token listing */
if (*ll != 0) {
memmove(t, t+tlen, abs(*ll));
}
} else {
er = t_p2(t, ll, 1, al);
}