]> Shamusworld >> Repos - rmac/blob - mark.c
Various small fixes including:
[rmac] / mark.c
1 //
2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // MARK.C - A record of things that are defined relative to any of the sections
4 // Copyright (C) 199x Landon Dyer, 2011-2017 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
7 //
8
9 #include "mark.h"
10 #include "error.h"
11 #include "object.h"
12 #include "riscasm.h"
13 #include "sect.h"
14
15
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 *))
18
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
25
26 // Table to convert from TDB to fixup triad
27 static uint8_t mark_tr[] = {
28         0,              // (N/A)
29         2,              // TEXT relocatable
30         1, 0,   // DATA relocatable
31         3               // BSS relocatable
32 };
33
34 //#define DEBUG_IMAGE_MARKING
35
36
37 //
38 // Initialize marker
39 //
40 void InitMark(void)
41 {
42         firstmch = curmch = NULL;
43         mcalloc = mcused = 0;
44         curfrom = 0;
45         sect[TEXT].relocs = sect[DATA].relocs = sect[BSS].relocs = 0;
46 }
47
48
49 //
50 // Wrap up marker (called after final mark is made)
51 //
52 void StopMark(void)
53 {
54         if (curmch)
55         {
56                 *markptr.wp = MCHEND;           // Mark end of block
57                 curmch->mcused = mcused;        // Update # used in mark block
58         }
59 }
60
61
62 //
63 // Mark a word or longword as relocatable
64 //
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)
70 //
71 uint32_t MarkRelocatable(uint16_t section, uint32_t loc, uint16_t to, uint16_t flags, SYM * symbol)
72 {
73 #ifdef DEBUG_IMAGE_MARKING
74 printf("MarkRelocatable: section=%i, loc=$%X, to=$%X, flags=$%x, symbol=$%X\n", section, loc, to, flags, symbol);
75 if (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);
77 #endif
78
79         if ((mcalloc - mcused) < MIN_MARK_MEM)
80                 AllocateMark();
81
82         // Set up flags
83         flags |= to;
84
85         if (section != curfrom)
86                 flags |= MCHFROM;
87
88         if (symbol != NULL)
89                 flags |= MSYMBOL;
90
91         //
92         // Complain about some things are not allowed in '-p' (PRG) mode:
93         //  o  Marks that aren't to LONGs
94         //  o  External references
95         //
96         if (prg_flag)
97         {
98                 if (symbol != NULL)
99                         error("illegal external reference (in .PRG mode) to '%s'",
100                                 symbol->sname);
101         }
102
103         // Dump crap into the mark
104         *markptr.wp++ = flags;
105         *markptr.lp++ = loc;
106         mcused += sizeof(uint16_t) + sizeof(uint32_t);
107
108         if (flags & MCHFROM)
109         {
110                 curfrom = section;
111                 *markptr.wp++ = section;
112                 mcused += sizeof(uint16_t);
113         }
114
115         if (flags & MSYMBOL)
116         {
117                 *markptr.sy++ = symbol;
118                 mcused += sizeof(SYM *);
119         }
120
121         // Increment # of relocs in this section
122         sect[section].relocs++;
123
124         // Not sure what this is about (making sure the next mark is clear until
125         // it's marked as the end--I think)...
126         *markptr.wp = 0x0000;
127
128         return 0;
129 }
130
131
132 //
133 // Allocate another chunk of mark space
134 //
135 uint32_t AllocateMark(void)
136 {
137         // Alloc mark block header (and data) and set it up.
138         MCHUNK * p = malloc(sizeof(MCHUNK) + MARK_ALLOC_INCR);
139         p->mcnext = NULL;
140         p->mcalloc = MARK_ALLOC_INCR;
141         p->mcptr.cp = (uint8_t *)p + sizeof(MCHUNK);
142         p->mcused = 0;
143
144         if (firstmch == NULL)
145                 firstmch = p;
146
147         if (curmch)
148         {
149                 // Link onto previous chunk
150                 *markptr.wp++ = MCHEND;         // Mark end of block
151                 curmch->mcused = mcused;
152                 curmch->mcnext = p;
153         }
154
155         // Setup global vars
156         curmch = p;
157         markptr = p->mcptr;
158         mcalloc = MARK_ALLOC_INCR;
159         mcused = 0;
160
161         return 0;
162 }
163
164
165 //
166 // Make mark image for Alcyon .o file
167 // okflag: 1, ok to deposit reloc information
168 //
169 uint32_t MarkImage(register uint8_t * mp, uint32_t siz, uint32_t tsize, int okflag)
170 {
171         uint16_t from = 0;              // Section fixups are currently FROM
172         uint32_t loc;                   // Location (temp)
173         uint32_t lastloc;               // Last location fixed up (RELMOD)
174         uint8_t * wp;                   // Pointer into raw relocation information
175         register uint8_t * dp;  // Deposit point for RELMOD information
176
177         if (okflag)
178                 memset(mp, 0, siz);             // zero relocation buffer
179
180         for(MCHUNK * mch=firstmch; mch!=NULL; mch=mch->mcnext)
181         {
182                 for(PTR p=mch->mcptr;;)
183                 {
184                         uint16_t w = *p.wp++;// w = next mark entry
185
186                         if (w & MCHEND)         // (end of mark chunk)
187                                 break;
188
189                         // Get mark record
190                         SYM * symbol = NULL;
191                         loc = *p.lp++;          // mark location
192
193                         if (w & MCHFROM)        // maybe change "from" section
194                                 from = *p.wp++;
195
196                         if (w & MSYMBOL)        // maybe includes a symbol
197                                 symbol = *p.sy++;
198
199                         // Compute mark position in relocation information; in RELMOD mode,
200                         // get address of data to fix up.
201                         if (from == DATA)
202                                 loc += tsize;
203
204                         wp = (uint8_t *)(mp + loc);
205
206                         if (okflag && (w & MLONG)) // indicate first word of long
207                         {
208                                 wp[1] = 5;
209                                 wp += 2;
210                         }
211
212                         if (symbol)
213                         {
214                                 // Deposit external reference
215                                 if (okflag)
216                                 {
217                                         if (w & MPCREL)
218                                                 w = 6;          // PC-relative fixup
219                                         else
220                                                 w = 4;          // Absolute fixup
221
222                                         w |= symbol->senv << 3;
223                                         *wp++ = w >> 8;
224                                         *wp = (uint8_t)w;
225                                 }
226                         }
227                         else
228                         {
229                                 // Deposit section-relative mark; in RELMOD mode, fix it up in
230                                 // the chunk, kind of like a sleazoid linker.
231                                 //
232                                 // In RELMOD mode, marks to words (MWORDs) "cannot happen,"
233                                 // checks are made when mark() is called, so we don't have to
234                                 // check again here.
235                                 w &= TDB;
236
237                                 if (okflag)
238                                         wp[1] = mark_tr[w];
239                                 else if (prg_flag && (w & (DATA | BSS)))
240                                 {
241                                         uint32_t diff = GETBE32(wp, 0);
242 #ifdef DO_DEBUG
243                                         DEBUG printf("diff=%lx ==> ", diff);
244 #endif
245                                         diff += sect[TEXT].sloc;
246
247                                         if (w == BSS)
248                                                 diff += sect[DATA].sloc;
249
250                                         SETBE32(wp, 0, diff)
251 #ifdef DO_DEBUG
252                                         DEBUG printf("%lx\n", diff);
253 #endif
254                                 }
255                         }
256                 }
257         }
258
259         // Generate ".PRG" relocation information in place in the relocation words
260         // (the "RELMOD" operation).
261         if (okflag && prg_flag)
262         {
263                 int firstp = 1;
264                 wp = dp = mp;
265
266                 for(loc=0; loc<siz;)
267                 {
268                         if ((wp[1] & 7) == 5)
269                         {
270                                 if (firstp)
271                                 {
272                                         SETBE32(dp, 0, loc);
273                                         dp += 4;
274                                         firstp = 0;
275                                 }
276                                 else
277                                 {
278                                         uint32_t diff;
279
280                                         for(diff=loc-lastloc; diff>254; diff-=254)
281                                                 *dp++ = 1;
282
283                                         *dp++ = (uint8_t)diff;
284                                 }
285
286                                 lastloc = loc;
287                                 loc += 4;
288                                 wp += 4;
289                         }
290                         else
291                         {
292                                 loc += 2;
293                                 wp += 2;
294                         }
295                 }
296
297                 // Terminate relocation list with 0L (if there was no relocation) or
298                 // 0.B (if relocation information was written).
299                 if (!firstp)
300                         *dp++ = 0;
301                 else
302                         for(firstp=0; firstp<4; firstp++)
303                                 *dp++ = 0;
304
305                 // Return size of relocation information
306                 loc = dp - mp;
307                 return loc;
308         }
309
310         return siz;
311 }
312
313
314 //
315 // Make mark image for BSD .o file
316 //
317 // Assumptions about mark records (for BSD): if there is a symbol, the mark is
318 // for an undefined symbol, otherwise it's just a normal TDB relocation.
319 // N.B.: tsize is only used if reqseg is DATA
320 //
321 uint32_t MarkBSDImage(uint8_t * mp, uint32_t siz, uint32_t tsize, int reqseg)
322 {
323         uint16_t from = 0;                      // Section fixups are currently FROM
324         uint32_t rsize = 0;                     // Relocation table size (written to mp)
325         int validsegment = 0;           // We are not yet in a valid segment...
326
327 #ifdef DEBUG_IMAGE_MARKING
328 printf("MarkBSDImage():\n");
329 #endif
330         // Initialize relocation table point (for D_foo macros)
331         chptr = mp;
332
333         // Run through all the relocation mark chunks
334         for(MCHUNK * mch=firstmch; mch!=NULL; mch=mch->mcnext)
335         {
336                 for(PTR p=mch->mcptr;;)
337                 {
338                         SYM * symbol = NULL;
339                         uint16_t w = *p.wp++;   // Next mark entry
340
341                         // If we hit the end of a chunk, go get the next one
342                         if (w & MCHEND)
343                                 break;
344
345                         // Get the rest of the mark record
346                         uint32_t loc = *p.lp++; // Mark location
347
348                         // Maybe change "from" section
349                         if (w & MCHFROM)
350                         {
351                                 from = *p.wp++;
352
353                                 if (((reqseg == TEXT) && (from == TEXT))
354                                         || ((reqseg == DATA) && (from == DATA)))
355                                         validsegment = 1;
356                                 else
357                                         validsegment = 0;
358                         }
359
360                         // Maybe includes a symbol
361                         if (w & MSYMBOL)
362                                 symbol = *p.sy++;
363
364                         if (!validsegment)
365                                 continue;
366
367 #ifdef DEBUG_IMAGE_MARKING
368 printf(" validsegment: raddr = $%08X\n", loc);
369 #endif
370                         uint32_t rflag = 0x00000040;    // Absolute relocation
371
372                         if (w & MPCREL)
373                                 rflag = 0x000000A0;                     // PC-relative relocation
374
375                         // This flag tells the linker to WORD swap the LONG when doing the
376                         // relocation.
377                         if (w & MMOVEI)
378                                 rflag |= 0x00000001;
379
380                         // This tells the linker to do a WORD relocation (otherwise it
381                         // defaults to doing a LONG, throwing things off for WORD sized
382                         // fixups)
383                         if (!(w & MLONG))
384                                 rflag |= 0x00000002;
385
386                         if (symbol != NULL)
387                         {
388                                 // Deposit external reference
389                                 rflag |= 0x00000010;                    // Set external reloc flag bit
390                                 rflag |= (symbol->senv << 8);   // Put symbol index in flags
391
392 #ifdef DEBUG_IMAGE_MARKING
393 printf("  validsegment(2): rflag = $%08X\n", rflag);
394 #endif
395                         }
396                         else
397                         {
398 #ifdef DEBUG_IMAGE_MARKING
399 printf("  w = $%04X\n", w);
400 #endif
401                                 w &= TDB;                               // Set reloc flags to segment
402
403                                 switch (w)
404                                 {
405                                 case TEXT: rflag |= 0x00000400; break;
406                                 case DATA: rflag |= 0x00000600; break;
407                                 case BSS:  rflag |= 0x00000800; break;
408                                 }
409
410 #ifdef DEBUG_IMAGE_MARKING
411 printf("  validsegment(3): rflag = $%08X\n", rflag);
412 #endif
413                                 // Fix relocation by adding in start of TEXT segment, since it's
414                                 // currently relative to the start of the DATA (or BSS) segment
415                                 if (w & (DATA | BSS))
416                                 {
417                                         uint8_t * dp = objImage + BSDHDRSIZE + loc;
418
419                                         // Bump the start of the section if it's DATA (& not TEXT)
420                                         if (from == DATA)
421                                                 dp += tsize;
422
423                                         uint32_t diff = (rflag & 0x02 ? GETBE16(dp, 0) : GETBE32(dp, 0));
424                                         DEBUG printf("diff=%uX ==> ", diff);
425 #ifdef DEBUG_IMAGE_MARKING
426 printf("  validsegment(4): diff = $%08X --> ", diff);
427 #endif
428                                         if (rflag & 0x01)
429                                                 diff = WORDSWAP32(diff);
430
431                                         diff += sect[TEXT].sloc;
432
433                                         if (w == BSS)
434                                                 diff += sect[DATA].sloc;
435
436                                         if (rflag & 0x01)
437                                                 diff = WORDSWAP32(diff);
438
439                                         // Make sure to deposit the correct size payload
440                                         // N.B.: The braces around the SETBExx macros are needed
441                                         //       because the macro supplies its own set of braces,
442                                         //       thus leaving a naked semicolon afterwards to
443                                         //       screw up the if/else structure. This is the price
444                                         //       you pay when using macros pretending to be code.
445                                         if (rflag & 0x02)
446                                         {
447                                                 SETBE16(dp, 0, diff);
448                                         }
449                                         else
450                                         {
451                                                 SETBE32(dp, 0, diff);
452                                         }
453
454                                         DEBUG printf("%uX\n", diff);
455 #ifdef DEBUG_IMAGE_MARKING
456 printf("$%08X\n", diff);
457 #endif
458                                 }
459                         }
460
461                         D_long(loc);            // Write relocation address
462                         D_long(rflag);          // Write relocation flags
463                         rsize += 0x08;          // Increment relocation size
464                 }
465         }
466
467         // Return relocation table's size
468 #ifdef DEBUG_IMAGE_MARKING
469 printf("  rsize = $%X\n", rsize);
470 #endif
471         return rsize;
472 }
473
474
475 //
476 // Make relocation record for ELF .o file.
477 // Returns the size of the relocation record.
478 //
479 uint32_t CreateELFRelocationRecord(uint8_t * buf, uint8_t * secBuf, uint16_t section)
480 {
481         uint16_t from = 0;              // Section fixups are currently FROM
482         uint32_t rsize = 0;             // Size of the relocation table
483
484         // Setup pointer for D_long/word/byte macros
485         chptr = buf;
486         ch_size = 0;
487
488         for(MCHUNK * mch=firstmch; mch!=NULL; mch=mch->mcnext)
489         {
490                 for(register PTR p=mch->mcptr;;)
491                 {
492                         register uint16_t w = *p.wp++;  // w = next mark entry
493
494                         if (w & MCHEND)         // (end of mark chunk)
495                                 break;
496
497                         // Get mark record
498                         SYM * symbol = NULL;
499                         uint16_t symFlags = 0;
500                         uint32_t r_offset = *p.lp++;    // Mark's location
501
502                         if (w & MCHFROM)                // Maybe change "from" section
503                                 from = *p.wp++;
504
505                         if (w & MSYMBOL)                // Maybe includes a symbol
506                         {
507                                 symbol = *p.sy++;
508
509                                 if (symbol)
510                                         symFlags = symbol->sattr;
511                         }
512
513                         // Create relocation record for ELF object, if the mark is in the
514                         // current section.
515                         if (from & section)
516                         {
517                                 uint32_t r_sym = 0;
518                                 uint32_t r_type = 0;
519                                 uint32_t r_addend = 0;
520
521                                 // Since we're chucking all symbols here for ELF objects by
522                                 // default (cf. sect.c), we discriminate here (normally, if
523                                 // there is a symbol in the mark record, it means an undefined
524                                 // symbol) :-P
525                                 if (symbol && !(symFlags & DEFINED) && (symFlags & GLOBAL))
526                                         r_sym = symbol->senv + extraSyms;
527                                 else if (w & TEXT)
528                                         r_sym = elfHdrNum[ES_TEXT];     // Mark TEXT segment
529                                 else if (w & DATA)
530                                         r_sym = elfHdrNum[ES_DATA];     // Mark DATA segment
531                                 else if (w & BSS)
532                                         r_sym = elfHdrNum[ES_BSS];      // Mark BSS segment
533
534                                 // Set the relocation type next
535                                 if (w & MPCREL)
536                                         r_type = 5;  // R_68K_PC16
537                                 // N.B.: Since we've established that (from & section) is non-
538                                 //       zero, this condition will *never* be satisfied... :-P
539                                 //       It might be better to check the symbol's senv; that is,
540                                 //       if this is a real problem that needs addressing...
541                                 else if ((from & section) == 0)
542                                         // In the case of a section referring to a label in another
543                                         // section (for example text->data) use a R_68K_PC32 mark.
544                                         r_type = 4;  // R_68K_PC32
545                                 else
546                                         r_type = 1;  // R_68K_32
547
548                                 r_addend = GETBE32(secBuf + r_offset, 0);
549
550                                 // Deposit the relocation record
551                                 D_long(r_offset);
552                                 D_long(((r_sym << 8) | r_type));
553                                 D_long(r_addend);
554                                 rsize += 0x0C;
555                         }
556                 }
557         }
558
559         return rsize;
560 }
561