2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // MARK.C - A record of things that are defined relative to any of the sections
4 // Copyright (C) 199x Landon Dyer, 2011-2012 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
15 MCHUNK * firstmch; // First mark chunk
16 MCHUNK * curmch; // Current mark chunk
17 PTR markptr; // Deposit point in current mark chunk
18 LONG mcalloc; // #bytes alloc'd to current mark chunk
19 LONG mcused; // #bytes used in current mark chunk
20 uint16_t curfrom; // Current "from" section
25 extern int prg_flag; // 1, write ".PRG" relocatable executable
27 //#define DEBUG_IMAGE_MARKING
35 firstmch = curmch = NULL;
42 // Wrap up marker (called after final mark is made)
48 *markptr.wp = MCHEND; // Mark end of block
49 curmch->mcused = mcused; // Update #used in mark block
55 // Mark a word or longword relocatable
57 int rmark(uint16_t from, uint32_t loc, uint16_t to, uint16_t size, SYM * symbol)
59 #ifdef DEBUG_IMAGE_MARKING
60 printf("rmark: from=%i, loc=$%X, to=$%X, size=$%x, symbol=$%X\n", from, loc, to, size, symbol);
62 printf(" symbol->stype=$%02X, sattr=$%04X, sattre=$%08X, svalue=%i, sname=%s\n", symbol->stype, symbol->sattr, symbol->sattre, symbol->svalue, symbol->sname);
65 if ((mcalloc - mcused) < MIN_MARK_MEM)
68 uint16_t flags = (size | to);
77 // Complain about some things are not allowed in `-p' mode:
78 // o marks that aren't to LONGs;
79 // o external references.
83 if ((flags & MLONG) == 0)
84 error("illegal word relocatable (in .PRG mode)");
87 errors("illegal external reference (in .PRG mode) to '%s'",
91 mcused += sizeof(WORD) + sizeof(LONG);
92 *markptr.wp++ = flags;
99 mcused += sizeof(WORD);
104 *markptr.sy++ = symbol;
105 mcused += sizeof(SYM *);
108 *markptr.wp = 0x0000;
115 // Allocate another chunk of mark space
121 // Alloc mark block header (and data) and set it up.
122 // p = (MCHUNK *)amem((long)(sizeof(MCHUNK)) + MARK_ALLOC_INCR);
123 MCHUNK * p = (MCHUNK *)malloc(sizeof(MCHUNK) + MARK_ALLOC_INCR);
125 p->mcalloc = MARK_ALLOC_INCR;
126 p->mcptr.cp = (char *)(((char *)p) + sizeof(MCHUNK));
130 // Link onto previous chunk
131 *markptr.wp++ = MCHEND; // Mark end of block
132 curmch->mcused = mcused;
139 curmch = p; // Setup global vars
141 mcalloc = MARK_ALLOC_INCR;
149 * Table to convert from TDB to fixup triad
152 static char mark_tr[] = {
154 2, /* TEXT relocatable */
155 1, 0, /* DATA relocatable */
156 3 /* BSS relocatable */
161 * Make mark image for Alcyon .o file
162 * okflag -- 1, ok to deposit reloc information
164 LONG markimg(register char * mp, LONG siz, LONG tsize, int okflag)
166 MCHUNK * mch; /* -> mark chunk */
167 register PTR p; /* source point from within mark chunk */
168 WORD from; /* section fixups are currently FROM */
169 register WORD w; /* a word (temp) */
170 LONG loc; /* location (temp) */
171 LONG lastloc; /* last location fixed up (RELMOD) */
172 SYM * symbol; /* -> symbols (temp) */
173 char * wp; /* pointer into raw relocation information */
174 register char * dp; /* deposit point for RELMOD information */
175 int firstp; /* 1, first relocation (RELMOD) */
176 LONG diff; /* difference to relocate (RELMOD) */
179 //clear(mp, siz); /* zero relocation buffer */
180 memset(mp, 0, siz); /* zero relocation buffer */
184 for(mch=firstmch; mch!=NULL; mch=mch->mcnext)
188 w = *p.wp++; /* w = next mark entry */
190 if (w & MCHEND) /* (end of mark chunk) */
197 loc = *p.lp++; /* mark location */
199 if (w & MCHFROM) /* maybe change "from" section */
202 if (w & MSYMBOL) /* maybe includes a symbol */
206 * Compute mark position in relocation information;
207 * in RELMOD mode, get address of data to fix up.
212 wp = (char *)(mp + loc);
214 if (okflag && (w & MLONG)) /* indicate first word of long */
223 * Deposit external reference
228 w = 6; /* pc-relative fixup */
230 w = 4; /* absolute fixup */
232 w |= symbol->senv << 3;
240 * Deposit section-relative mark;
241 * in RELMOD mode, fix it up in the chunk,
242 * kind of like a sleazoid linker.
244 * In RELMOD mode, marks to words (MWORDs) "cannot happen,"
245 * checks are made when mark() is called, so we don't have
246 * to check again here.
252 else if (prg_flag && (w & (DATA | BSS)))
255 diff = ((LONG)(*dp++ & 0xff)) << 24;
256 diff |= ((LONG)(*dp++ & 0xff)) << 16;
257 diff |= ((LONG)(*dp++ & 0xff)) << 8;
258 diff |= (LONG)(*dp & 0xff);
261 DEBUG printf("diff=%lx ==> ", diff);
263 diff += sect[TEXT].sloc;
266 diff += sect[DATA].sloc;
269 *dp++ = (char)(diff >> 24);
270 *dp++ = (char)(diff >> 16);
271 *dp++ = (char)(diff >> 8);
274 DEBUG printf("%lx\n", diff);
282 * Generate ".PRG" relocation information in place in
283 * the relocation words (the ``RELMOD'' operation).
285 if (okflag && prg_flag)
293 if ((wp[1] & 7) == 5)
297 *dp++ = (char)(loc >> 24);
298 *dp++ = (char)(loc >> 16);
299 *dp++ = (char)(loc >> 8);
305 for(diff=loc-lastloc; diff>254; diff-= 254)
323 * Terminate relocation list with 0L (if there was no
324 * relocation) or 0.B (if relocation information was
329 else for (firstp = 0; firstp < 4; ++firstp)
333 * Return size of relocation information
344 // Make mark image for BSD .o file
346 uint32_t bsdmarkimg(char * mp, LONG siz, LONG tsize, int reqseg)
348 MCHUNK * mch; // Mark chunk
349 PTR p; // Source point from within mark chunk
350 uint16_t from; // Section fixups are currently FROM
351 uint16_t w; // A word (temp)
352 uint32_t loc; // Location (temp)
353 SYM * symbol; // Symbols (temp)
354 uint8_t * wp; // Pointer into raw relocation info
355 uint8_t * dp; // Deposit point for RELMOD info
356 uint32_t diff; // Difference to relocate (RELMOD)
357 uint32_t raddr, rflag = 0; // BSD relocation address and flags
358 uint32_t rsize; // Relocation size
359 int validsegment = 0; // Valid segment being processed
361 #ifdef DEBUG_IMAGE_MARKING
362 printf("bsdmarkimg():\n");
364 // Initialise relocation size
369 for(mch=firstmch; mch!=NULL; mch=mch->mcnext)
373 w = *p.wp++; // Next mark entry
376 break; // End of mark chunk
380 loc = *p.lp++; // Mark location
384 // Maybe change "from" section
387 if (obj_format == BSD)
391 // Requested segment is TEXT
399 // Requested segment is DATA
408 if (w & MSYMBOL) // Maybe includes a symbol
411 if (obj_format == BSD)
413 raddr = loc; // Set relocation address
416 #ifdef DEBUG_IMAGE_MARKING
418 printf(" validsegment: raddr = $%08X\n", raddr);
420 D_long(raddr); // Write relocation address
421 #ifdef DEBUG_IMAGE_MARKING
426 rflag = 0x000000A0; // PC-relative fixup
428 rflag = 0x00000040; // Absolute fixup
430 // This flag tells the linker to WORD swap the LONG when doing the fixup.
433 //printf("bsdmarkimg: ORing $01 to rflag (MMOVEI) [symbol=%s]...\n", symbol->sname);
438 // Compute mark position in relocation information;
439 // in RELMOD mode, get address of data to fix up.
443 wp = (uint8_t *)(mp + loc);
447 // Deposit external reference
448 if (obj_format == BSD)
450 rflag |= 0x00000010; // Set external reloc flag bit
451 rflag |= (symbol->senv << 8); // Put symbol index in flags
453 // Looks like this is completely unnecessary (considering it does the wrong thing!)
455 if (symbol->sattre & RISCSYM)
457 printf("bsdmarkimg: ORing $01 to rflag (RISCSYM) [symbol=%s]...\n", symbol->sname);
464 #ifdef DEBUG_IMAGE_MARKING
465 printf(" validsegment(2): rflag = $%08X\n", rflag);
467 D_long(rflag); // Write relocation flags
468 rsize += 8; // Increment relocation size
474 if (obj_format == BSD)
476 #ifdef DEBUG_IMAGE_MARKING
477 printf(" w = $%04X\n", w);
479 w &= TDB; // Set reloc flags to segment
483 case TEXT: rflag |= 0x00000400; break;
484 case DATA: rflag |= 0x00000600; break;
485 case BSS: rflag |= 0x00000800; break;
490 #ifdef DEBUG_IMAGE_MARKING
491 printf(" validsegment(3): rflag = $%08X\n", rflag);
493 D_long(rflag); // Write relocation flags
494 rsize += 8; // Increment relocation size
503 dp = objImage + BSDHDRSIZE + loc;
504 diff = ((LONG)(*dp++ & 0xFF)) << 24;
505 diff |= ((LONG)(*dp++ & 0xFF)) << 16;
506 diff |= ((LONG)(*dp++ & 0xFF)) << 8;
507 diff |= (LONG)(*dp & 0xFF);
508 DEBUG printf("diff=%ux ==> ", diff);
509 #ifdef DEBUG_IMAGE_MARKING
510 printf(" validsegment(4): diff = $%08X --> ", diff);
514 diff = ((diff >> 16) & 0x0000FFFF) | ((diff << 16) & 0xFFFF0000);
516 diff += sect[TEXT].sloc;
519 diff += sect[DATA].sloc;
522 diff = ((diff >> 16) & 0x0000FFFF) | ((diff << 16) & 0xFFFF0000);
524 dp = objImage + BSDHDRSIZE + loc;
525 *dp++ = (char)(diff >> 24);
526 *dp++ = (char)(diff >> 16);
527 *dp++ = (char)(diff >> 8);
529 DEBUG printf("%ux\n", diff);
530 #ifdef DEBUG_IMAGE_MARKING
531 printf("$%08X\n", diff);
540 // Return relocation size
541 if (obj_format == BSD)
542 #ifdef DEBUG_IMAGE_MARKING
544 printf(" rsize = $%X\n", rsize);
547 #ifdef DEBUG_IMAGE_MARKING