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
23 //#define DEBUG_IMAGE_MARKING
31 firstmch = curmch = NULL;
38 // Wrap up marker (called after final mark is made)
44 *markptr.wp = MCHEND; // Mark end of block
45 curmch->mcused = mcused; // Update #used in mark block
51 // Mark a word or longword relocatable
53 int rmark(uint16_t from, uint32_t loc, uint16_t to, uint16_t size, SYM * symbol)
55 #ifdef DEBUG_IMAGE_MARKING
56 printf("rmark: from=%i, loc=$%X, to=$%X, size=$%x, symbol=$%X\n", from, loc, to, size, symbol);
60 if ((mcalloc - mcused) < MIN_MARK_MEM)
63 uint16_t flags = (size | to);
71 mcused += sizeof(WORD) + sizeof(LONG);
72 *markptr.wp++ = flags;
79 mcused += sizeof(WORD);
84 *markptr.sy++ = symbol;
85 mcused += sizeof(SYM *);
95 // Allocate another chunk of mark space
101 // Alloc mark block header (and data) and set it up.
102 // p = (MCHUNK *)amem((long)(sizeof(MCHUNK)) + MARK_ALLOC_INCR);
103 MCHUNK * p = (MCHUNK *)malloc(sizeof(MCHUNK) + MARK_ALLOC_INCR);
105 p->mcalloc = MARK_ALLOC_INCR;
106 p->mcptr.cp = (char *)(((char *)p) + sizeof(MCHUNK));
110 // Link onto previous chunk
111 *markptr.wp++ = MCHEND; // Mark end of block
112 curmch->mcused = mcused;
119 curmch = p; // Setup global vars
121 mcalloc = MARK_ALLOC_INCR;
129 // Make mark image for BSD .o file
131 uint32_t bsdmarkimg(char * mp, LONG siz, LONG tsize, int reqseg)
133 MCHUNK * mch; // Mark chunk
134 PTR p; // Source point from within mark chunk
135 uint16_t from; // Section fixups are currently FROM
136 uint16_t w; // A word (temp)
137 uint32_t loc; // Location (temp)
138 SYM * symbol; // Symbols (temp)
139 uint8_t * wp; // Pointer into raw relocation info
140 uint8_t * dp; // Deposit point for RELMOD info
141 uint32_t diff; // Difference to relocate (RELMOD)
142 uint32_t raddr, rflag = 0; // BSD relocation address and flags
143 uint32_t rsize; // Relocation size
144 int validsegment = 0; // Valid segment being processed
146 #ifdef DEBUG_IMAGE_MARKING
147 printf("bsdmarkimg():\n");
149 // Initialise relocation size
154 for(mch=firstmch; mch!=NULL; mch=mch->mcnext)
158 w = *p.wp++; // Next mark entry
161 break; // End of mark chunk
165 loc = *p.lp++; // Mark location
169 // Maybe change "from" section
172 if (obj_format == BSD)
176 // Requested segment is TEXT
184 // Requested segment is DATA
193 if (w & MSYMBOL) // Maybe includes a symbol
196 if (obj_format == BSD)
198 raddr = loc; // Set relocation address
201 #ifdef DEBUG_IMAGE_MARKING
203 printf(" validsegment: raddr = $%08X\n", raddr);
205 D_long(raddr); // Write relocation address
206 #ifdef DEBUG_IMAGE_MARKING
211 rflag = 0x000000A0; // PC-relative fixup
213 rflag = 0x00000040; // Absolute fixup
219 // Compute mark position in relocation information;
220 // in RELMOD mode, get address of data to fix up.
224 wp = (uint8_t *)(mp + loc);
228 // Deposit external reference
229 if (obj_format == BSD)
231 rflag |= 0x00000010; // Set external reloc flag bit
232 rflag |= (symbol->senv << 8); // Put symbol index in flags
234 if (symbol->sattre & RISCSYM)
239 #ifdef DEBUG_IMAGE_MARKING
240 printf(" validsegment(2): rflag = $%08X\n", rflag);
242 D_long(rflag); // Write relocation flags
243 rsize += 8; // Increment relocation size
249 if (obj_format == BSD)
251 #ifdef DEBUG_IMAGE_MARKING
252 printf(" w = $%04X\n", w);
254 w &= TDB; // Set reloc flags to segment
258 case TEXT: rflag |= 0x00000400; break;
259 case DATA: rflag |= 0x00000600; break;
260 case BSS: rflag |= 0x00000800; break;
265 #ifdef DEBUG_IMAGE_MARKING
266 printf(" validsegment(3): rflag = $%08X\n", rflag);
268 D_long(rflag); // Write relocation flags
269 rsize += 8; // Increment relocation size
278 dp = objimage + BSDHDRSIZE + loc;
279 diff = ((LONG)(*dp++ & 0xFF)) << 24;
280 diff |= ((LONG)(*dp++ & 0xFF)) << 16;
281 diff |= ((LONG)(*dp++ & 0xFF)) << 8;
282 diff |= (LONG)(*dp & 0xFF);
283 DEBUG printf("diff=%ux ==> ", diff);
284 #ifdef DEBUG_IMAGE_MARKING
285 printf(" validsegment(4): diff = $%08X --> ", diff);
289 diff = ((diff >> 16) & 0x0000FFFF) | ((diff << 16) & 0xFFFF0000);
291 diff += sect[TEXT].sloc;
294 diff += sect[DATA].sloc;
297 diff = ((diff >> 16) & 0x0000FFFF) | ((diff << 16) & 0xFFFF0000);
299 dp = objimage + BSDHDRSIZE + loc;
300 *dp++ = (char)(diff >> 24);
301 *dp++ = (char)(diff >> 16);
302 *dp++ = (char)(diff >> 8);
304 DEBUG printf("%ux\n", diff);
305 #ifdef DEBUG_IMAGE_MARKING
306 printf("$%08X\n", diff);
315 // Return relocation size
316 if (obj_format == BSD)
317 #ifdef DEBUG_IMAGE_MARKING
319 printf(" rsize = $%X\n", rsize);
322 #ifdef DEBUG_IMAGE_MARKING