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
98 if ((flags & MLONG) == 0)
99 error("illegal word relocatable (in .PRG mode)");
102 errors("illegal external reference (in .PRG mode) to '%s'",
106 // Dump crap into the mark
107 *markptr.wp++ = flags;
109 mcused += sizeof(uint16_t) + sizeof(uint32_t);
114 *markptr.wp++ = section;
115 mcused += sizeof(uint16_t);
120 *markptr.sy++ = symbol;
121 mcused += sizeof(SYM *);
124 // Increment # of relocs in this section
125 sect[section].relocs++;
127 // Not sure what this is about (making sure the next mark is clear until
128 // it's marked as the end--I think)...
129 *markptr.wp = 0x0000;
136 // Allocate another chunk of mark space
138 uint32_t AllocateMark(void)
140 // Alloc mark block header (and data) and set it up.
141 MCHUNK * p = malloc(sizeof(MCHUNK) + MARK_ALLOC_INCR);
143 p->mcalloc = MARK_ALLOC_INCR;
144 p->mcptr.cp = (uint8_t *)p + sizeof(MCHUNK);
147 if (firstmch == NULL)
152 // Link onto previous chunk
153 *markptr.wp++ = MCHEND; // Mark end of block
154 curmch->mcused = mcused;
161 mcalloc = MARK_ALLOC_INCR;
169 // Make mark image for Alcyon .o file
170 // okflag: 1, ok to deposit reloc information
172 uint32_t MarkImage(register uint8_t * mp, uint32_t siz, uint32_t tsize, int okflag)
174 uint16_t from = 0; // Section fixups are currently FROM
175 uint32_t loc; // Location (temp)
176 uint32_t lastloc; // Last location fixed up (RELMOD)
177 uint8_t * wp; // Pointer into raw relocation information
178 register uint8_t * dp; // Deposit point for RELMOD information
181 memset(mp, 0, siz); // zero relocation buffer
183 for(MCHUNK * mch=firstmch; mch!=NULL; mch=mch->mcnext)
185 for(PTR p=mch->mcptr;;)
187 uint16_t w = *p.wp++;// w = next mark entry
189 if (w & MCHEND) // (end of mark chunk)
194 loc = *p.lp++; // mark location
196 if (w & MCHFROM) // maybe change "from" section
199 if (w & MSYMBOL) // maybe includes a symbol
202 // Compute mark position in relocation information; in RELMOD mode,
203 // get address of data to fix up.
207 wp = (uint8_t *)(mp + loc);
209 if (okflag && (w & MLONG)) // indicate first word of long
217 // Deposit external reference
221 w = 6; // PC-relative fixup
223 w = 4; // Absolute fixup
225 w |= symbol->senv << 3;
232 // Deposit section-relative mark; in RELMOD mode, fix it up in
233 // the chunk, kind of like a sleazoid linker.
235 // In RELMOD mode, marks to words (MWORDs) "cannot happen,"
236 // checks are made when mark() is called, so we don't have to
242 else if (prg_flag && (w & (DATA | BSS)))
244 uint32_t diff = GETBE32(wp, 0);
246 DEBUG printf("diff=%lx ==> ", diff);
248 diff += sect[TEXT].sloc;
251 diff += sect[DATA].sloc;
255 DEBUG printf("%lx\n", diff);
262 // Generate ".PRG" relocation information in place in the relocation words
263 // (the "RELMOD" operation).
264 if (okflag && prg_flag)
271 if ((wp[1] & 7) == 5)
283 for(diff=loc-lastloc; diff>254; diff-=254)
286 *dp++ = (uint8_t)diff;
300 // Terminate relocation list with 0L (if there was no relocation) or
301 // 0.B (if relocation information was written).
305 for(firstp=0; firstp<4; firstp++)
308 // Return size of relocation information
318 // Make mark image for BSD .o file
320 // Assumptions about mark records (for BSD): if there is a symbol, the mark is
321 // for an undefined symbol, otherwise it's just a normal TDB relocation.
322 // N.B.: tsize is only used if reqseg is DATA
324 uint32_t MarkBSDImage(uint8_t * mp, uint32_t siz, uint32_t tsize, int reqseg)
326 uint16_t from = 0; // Section fixups are currently FROM
327 uint32_t rsize = 0; // Relocation table size (written to mp)
328 int validsegment = 0; // We are not yet in a valid segment...
330 #ifdef DEBUG_IMAGE_MARKING
331 printf("MarkBSDImage():\n");
333 // Initialize relocation table point (for D_foo macros)
336 // Run through all the relocation mark chunks
337 for(MCHUNK * mch=firstmch; mch!=NULL; mch=mch->mcnext)
339 for(PTR p=mch->mcptr;;)
342 uint16_t w = *p.wp++; // Next mark entry
344 // If we hit the end of a chunk, go get the next one
348 // Get the rest of the mark record
349 uint32_t loc = *p.lp++; // Mark location
351 // Maybe change "from" section
356 if (((reqseg == TEXT) && (from == TEXT))
357 || ((reqseg == DATA) && (from == DATA)))
363 // Maybe includes a symbol
370 #ifdef DEBUG_IMAGE_MARKING
371 printf(" validsegment: raddr = $%08X\n", raddr);
373 uint32_t rflag = 0x00000040; // Absolute relocation
376 rflag = 0x000000A0; // PC-relative relocation
378 // This flag tells the linker to WORD swap the LONG when doing the
385 // Deposit external reference
386 rflag |= 0x00000010; // Set external reloc flag bit
387 rflag |= (symbol->senv << 8); // Put symbol index in flags
389 #ifdef DEBUG_IMAGE_MARKING
390 printf(" validsegment(2): rflag = $%08X\n", rflag);
395 #ifdef DEBUG_IMAGE_MARKING
396 printf(" w = $%04X\n", w);
398 w &= TDB; // Set reloc flags to segment
402 case TEXT: rflag |= 0x00000400; break;
403 case DATA: rflag |= 0x00000600; break;
404 case BSS: rflag |= 0x00000800; break;
407 #ifdef DEBUG_IMAGE_MARKING
408 printf(" validsegment(3): rflag = $%08X\n", rflag);
410 // Fix relocation by adding in start of TEXT segment, since it's
411 // currently relative to the start of the DATA (or BSS) segment
412 if (w & (DATA | BSS))
414 uint8_t * dp = objImage + BSDHDRSIZE + loc;
416 // Bump the start of the section if it's DATA (& not TEXT)
420 uint32_t diff = GETBE32(dp, 0);
421 DEBUG printf("diff=%uX ==> ", diff);
422 #ifdef DEBUG_IMAGE_MARKING
423 printf(" validsegment(4): diff = $%08X --> ", diff);
426 diff = WORDSWAP32(diff);
428 diff += sect[TEXT].sloc;
431 diff += sect[DATA].sloc;
434 diff = WORDSWAP32(diff);
436 SETBE32(dp, 0, diff);
437 DEBUG printf("%uX\n", diff);
438 #ifdef DEBUG_IMAGE_MARKING
439 printf("$%08X\n", diff);
444 D_long(loc); // Write relocation address
445 D_long(rflag); // Write relocation flags
446 rsize += 0x08; // Increment relocation size
450 // Return relocation table's size
451 #ifdef DEBUG_IMAGE_MARKING
452 printf(" rsize = $%X\n", rsize);
459 // Make relocation record for ELF .o file.
460 // Returns the size of the relocation record.
462 uint32_t CreateELFRelocationRecord(uint8_t * buf, uint8_t * secBuf, uint16_t section)
464 uint16_t from = 0; // Section fixups are currently FROM
465 uint32_t rsize = 0; // Size of the relocation table
467 // Setup pointer for D_long/word/byte macros
470 for(MCHUNK * mch=firstmch; mch!=NULL; mch=mch->mcnext)
472 for(register PTR p=mch->mcptr;;)
474 register uint16_t w = *p.wp++; // w = next mark entry
476 if (w & MCHEND) // (end of mark chunk)
481 uint16_t symFlags = 0;
482 uint32_t r_offset = *p.lp++; // Mark's location
484 if (w & MCHFROM) // Maybe change "from" section
487 if (w & MSYMBOL) // Maybe includes a symbol
492 symFlags = symbol->sattr;
495 // Create relocation record for ELF object, if the mark is in the
501 uint32_t r_addend = 0;
503 // Since we're chucking all symbols here for ELF objects by
504 // default (cf. sect.c), we discriminate here (normally, if
505 // there is a symbol in the mark record, it means an undefined
507 if (symbol && !(symFlags & DEFINED) && (symFlags & GLOBAL))
508 r_sym = symbol->senv + extraSyms;
510 r_sym = elfHdrNum[ES_TEXT]; // Mark TEXT segment
512 r_sym = elfHdrNum[ES_DATA]; // Mark DATA segment
514 r_sym = elfHdrNum[ES_BSS]; // Mark BSS segment
516 // Set the relocation type next
518 r_type = 5; // R_68K_PC16
519 // N.B.: Since we've established that (from & section) is non-
520 // zero, this condition will *never* be satisfied... :-P
521 // It might be better to check the symbol's senv; that is,
522 // if this is a real problem that needs addressing...
523 else if ((from & section) == 0)
524 // In the case of a section referring to a label in another
525 // section (for example text->data) use a R_68K_PC32 mark.
526 r_type = 4; // R_68K_PC32
528 r_type = 1; // R_68K_32
531 r_addend = symbol->svalue; // Mark offset into section
533 r_addend = GETBE32(secBuf + r_offset, 0);
535 // Deposit the relocation record
537 D_long((r_sym << 8) | r_type);