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
16 #define MARK_ALLOC_INCR 1024 // # bytes to alloc for more mark space
17 #define MIN_MARK_MEM (3 * sizeof(uint16_t) + 1 * sizeof(uint32_t) + sizeof(SYM *))
19 MCHUNK * firstmch; // First mark chunk
20 MCHUNK * curmch; // Current mark chunk
21 PTR markptr; // Deposit point in current mark chunk
22 uint32_t mcalloc; // # bytes alloc'd to current mark chunk
23 uint32_t mcused; // # bytes used in current mark chunk
24 uint16_t curfrom; // Current "from" section
26 // Table to convert from TDB to fixup triad
27 static uint8_t mark_tr[] = {
29 2, // TEXT relocatable
30 1, 0, // DATA relocatable
34 //#define DEBUG_IMAGE_MARKING
42 firstmch = curmch = NULL;
45 sect[TEXT].relocs = sect[DATA].relocs = sect[BSS].relocs = 0;
50 // Wrap up marker (called after final mark is made)
56 *markptr.wp = MCHEND; // Mark end of block
57 curmch->mcused = mcused; // Update # used in mark block
63 // Mark a word or longword as relocatable
65 // Record is either 2, 3, or 4 pieces of data long. A mark is of the form:
66 // .W <to+flags> section mark is relative to, and flags in upper byte
67 // .L <loc> location of mark in "from" section
68 // .W [from] new from section (if different from current)
69 // .L [symbol] symbol involved in external reference (if any)
71 uint32_t MarkRelocatable(uint16_t section, uint32_t loc, uint16_t to, uint16_t flags, SYM * symbol)
73 #ifdef DEBUG_IMAGE_MARKING
74 printf("MarkRelocatable: section=%i, loc=$%X, to=$%X, flags=$%x, symbol=$%X\n", section, loc, to, flags, symbol);
76 printf(" symbol->stype=$%02X, sattr=$%04X, sattre=$%08X, svalue=%i, sname=%s\n", symbol->stype, symbol->sattr, symbol->sattre, symbol->svalue, symbol->sname);
79 if ((mcalloc - mcused) < MIN_MARK_MEM)
85 if (section != curfrom)
92 // Complain about some things are not allowed in '-p' (PRG) mode:
93 // o Marks that aren't to LONGs
94 // o External references
99 if ((flags & MLONG) == 0)
100 error("illegal word relocatable (in .PRG mode)");
104 errors("illegal external reference (in .PRG mode) to '%s'",
108 // Dump crap into the mark
109 *markptr.wp++ = flags;
111 mcused += sizeof(uint16_t) + sizeof(uint32_t);
116 *markptr.wp++ = section;
117 mcused += sizeof(uint16_t);
122 *markptr.sy++ = symbol;
123 mcused += sizeof(SYM *);
126 // Increment # of relocs in this section
127 sect[section].relocs++;
129 // Not sure what this is about (making sure the next mark is clear until
130 // it's marked as the end--I think)...
131 *markptr.wp = 0x0000;
138 // Allocate another chunk of mark space
140 uint32_t AllocateMark(void)
142 // Alloc mark block header (and data) and set it up.
143 MCHUNK * p = malloc(sizeof(MCHUNK) + MARK_ALLOC_INCR);
145 p->mcalloc = MARK_ALLOC_INCR;
146 p->mcptr.cp = (uint8_t *)p + sizeof(MCHUNK);
149 if (firstmch == NULL)
154 // Link onto previous chunk
155 *markptr.wp++ = MCHEND; // Mark end of block
156 curmch->mcused = mcused;
163 mcalloc = MARK_ALLOC_INCR;
171 // Make mark image for Alcyon .o file
172 // okflag: 1, ok to deposit reloc information
174 uint32_t MarkImage(register uint8_t * mp, uint32_t siz, uint32_t tsize, int okflag)
176 uint16_t from = 0; // Section fixups are currently FROM
177 uint32_t loc; // Location (temp)
178 uint32_t lastloc; // Last location fixed up (RELMOD)
179 uint8_t * wp; // Pointer into raw relocation information
180 register uint8_t * dp; // Deposit point for RELMOD information
183 memset(mp, 0, siz); // zero relocation buffer
185 for(MCHUNK * mch=firstmch; mch!=NULL; mch=mch->mcnext)
187 for(PTR p=mch->mcptr;;)
189 uint16_t w = *p.wp++;// w = next mark entry
191 if (w & MCHEND) // (end of mark chunk)
196 loc = *p.lp++; // mark location
198 if (w & MCHFROM) // maybe change "from" section
201 if (w & MSYMBOL) // maybe includes a symbol
204 // Compute mark position in relocation information; in RELMOD mode,
205 // get address of data to fix up.
209 wp = (uint8_t *)(mp + loc);
211 if (okflag && (w & MLONG)) // indicate first word of long
219 // Deposit external reference
223 w = 6; // PC-relative fixup
225 w = 4; // Absolute fixup
227 w |= symbol->senv << 3;
234 // Deposit section-relative mark; in RELMOD mode, fix it up in
235 // the chunk, kind of like a sleazoid linker.
237 // In RELMOD mode, marks to words (MWORDs) "cannot happen,"
238 // checks are made when mark() is called, so we don't have to
244 else if (prg_flag && (w & (DATA | BSS)))
246 uint32_t diff = GETBE32(wp, 0);
248 DEBUG printf("diff=%lx ==> ", diff);
250 diff += sect[TEXT].sloc;
253 diff += sect[DATA].sloc;
257 DEBUG printf("%lx\n", diff);
264 // Generate ".PRG" relocation information in place in the relocation words
265 // (the "RELMOD" operation).
266 if (okflag && prg_flag)
273 if ((wp[1] & 7) == 5)
285 for(diff=loc-lastloc; diff>254; diff-=254)
288 *dp++ = (uint8_t)diff;
302 // Terminate relocation list with 0L (if there was no relocation) or
303 // 0.B (if relocation information was written).
307 for(firstp=0; firstp<4; firstp++)
310 // Return size of relocation information
320 // Make mark image for BSD .o file
322 // Assumptions about mark records (for BSD): if there is a symbol, the mark is
323 // for an undefined symbol, otherwise it's just a normal TDB relocation.
324 // N.B.: tsize is only used if reqseg is DATA
326 uint32_t MarkBSDImage(uint8_t * mp, uint32_t siz, uint32_t tsize, int reqseg)
328 uint16_t from = 0; // Section fixups are currently FROM
329 uint32_t rsize = 0; // Relocation table size (written to mp)
330 int validsegment = 0; // We are not yet in a valid segment...
332 #ifdef DEBUG_IMAGE_MARKING
333 printf("MarkBSDImage():\n");
335 // Initialize relocation table point (for D_foo macros)
338 // Run through all the relocation mark chunks
339 for(MCHUNK * mch=firstmch; mch!=NULL; mch=mch->mcnext)
341 for(PTR p=mch->mcptr;;)
344 uint16_t w = *p.wp++; // Next mark entry
346 // If we hit the end of a chunk, go get the next one
350 // Get the rest of the mark record
351 uint32_t loc = *p.lp++; // Mark location
353 // Maybe change "from" section
358 if (((reqseg == TEXT) && (from == TEXT))
359 || ((reqseg == DATA) && (from == DATA)))
365 // Maybe includes a symbol
372 #ifdef DEBUG_IMAGE_MARKING
373 printf(" validsegment: raddr = $%08X\n", loc);
375 uint32_t rflag = 0x00000040; // Absolute relocation
378 rflag = 0x000000A0; // PC-relative relocation
380 // This flag tells the linker to WORD swap the LONG when doing the
387 // Deposit external reference
388 rflag |= 0x00000010; // Set external reloc flag bit
389 rflag |= (symbol->senv << 8); // Put symbol index in flags
391 #ifdef DEBUG_IMAGE_MARKING
392 printf(" validsegment(2): rflag = $%08X\n", rflag);
397 #ifdef DEBUG_IMAGE_MARKING
398 printf(" w = $%04X\n", w);
400 w &= TDB; // Set reloc flags to segment
404 case TEXT: rflag |= 0x00000400; break;
405 case DATA: rflag |= 0x00000600; break;
406 case BSS: rflag |= 0x00000800; break;
409 #ifdef DEBUG_IMAGE_MARKING
410 printf(" validsegment(3): rflag = $%08X\n", rflag);
412 // Fix relocation by adding in start of TEXT segment, since it's
413 // currently relative to the start of the DATA (or BSS) segment
414 if (w & (DATA | BSS))
416 uint8_t * dp = objImage + BSDHDRSIZE + loc;
418 // Bump the start of the section if it's DATA (& not TEXT)
422 uint32_t diff = GETBE32(dp, 0);
423 DEBUG printf("diff=%uX ==> ", diff);
424 #ifdef DEBUG_IMAGE_MARKING
425 printf(" validsegment(4): diff = $%08X --> ", diff);
428 diff = WORDSWAP32(diff);
430 diff += sect[TEXT].sloc;
433 diff += sect[DATA].sloc;
436 diff = WORDSWAP32(diff);
438 SETBE32(dp, 0, diff);
439 DEBUG printf("%uX\n", diff);
440 #ifdef DEBUG_IMAGE_MARKING
441 printf("$%08X\n", diff);
446 D_long(loc); // Write relocation address
447 D_long(rflag); // Write relocation flags
448 rsize += 0x08; // Increment relocation size
452 // Return relocation table's size
453 #ifdef DEBUG_IMAGE_MARKING
454 printf(" rsize = $%X\n", rsize);
461 // Make relocation record for ELF .o file.
462 // Returns the size of the relocation record.
464 uint32_t CreateELFRelocationRecord(uint8_t * buf, uint8_t * secBuf, uint16_t section)
466 uint16_t from = 0; // Section fixups are currently FROM
467 uint32_t rsize = 0; // Size of the relocation table
469 // Setup pointer for D_long/word/byte macros
472 for(MCHUNK * mch=firstmch; mch!=NULL; mch=mch->mcnext)
474 for(register PTR p=mch->mcptr;;)
476 register uint16_t w = *p.wp++; // w = next mark entry
478 if (w & MCHEND) // (end of mark chunk)
483 uint16_t symFlags = 0;
484 uint32_t r_offset = *p.lp++; // Mark's location
486 if (w & MCHFROM) // Maybe change "from" section
489 if (w & MSYMBOL) // Maybe includes a symbol
494 symFlags = symbol->sattr;
497 // Create relocation record for ELF object, if the mark is in the
503 uint32_t r_addend = 0;
505 // Since we're chucking all symbols here for ELF objects by
506 // default (cf. sect.c), we discriminate here (normally, if
507 // there is a symbol in the mark record, it means an undefined
509 if (symbol && !(symFlags & DEFINED) && (symFlags & GLOBAL))
510 r_sym = symbol->senv + extraSyms;
512 r_sym = elfHdrNum[ES_TEXT]; // Mark TEXT segment
514 r_sym = elfHdrNum[ES_DATA]; // Mark DATA segment
516 r_sym = elfHdrNum[ES_BSS]; // Mark BSS segment
518 // Set the relocation type next
520 r_type = 5; // R_68K_PC16
521 // N.B.: Since we've established that (from & section) is non-
522 // zero, this condition will *never* be satisfied... :-P
523 // It might be better to check the symbol's senv; that is,
524 // if this is a real problem that needs addressing...
525 else if ((from & section) == 0)
526 // In the case of a section referring to a label in another
527 // section (for example text->data) use a R_68K_PC32 mark.
528 r_type = 4; // R_68K_PC32
530 r_type = 1; // R_68K_32
532 r_addend = GETBE32(secBuf + r_offset, 0);
534 // Deposit the relocation record
536 D_long((r_sym << 8) | r_type);