-
Notifications
You must be signed in to change notification settings - Fork 5
/
as4.c
599 lines (550 loc) · 11.7 KB
/
as4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* Output FUZIX object files for 8/16bit machines.
*
* Currently we understand little endian 8 and 16bit formats along with
* PC relative.
*
* FIXME: We need to manage dot[segment] properly for word addressed machines
* so that we track and write in words not bytes. Right now word addressing
* is broken
*/
#include "as.h"
static addr_t segsize[OSEG];
static uint32_t truesize[OSEG];
static off_t segbase[OSEG];
static uint16_t segpad[OSEG];
static uint8_t full[OSEG]; /* So we can tell a full wrap from a 0 start */
#ifdef ADDR32
static addr_t mask = 0xFFFFFFFF;
#else
static addr_t mask = 0xFFFF;
#endif
static int_fast8_t shift = 0;
static unsigned rel_check = 0;
static struct objhdr obh;
static void dumpseginfo(void)
{
#if 0
int i;
printf("Pass %d:\n", pass);
for (i = 0; i < 8; i++) {
printf("\t%d: %04X %d@%ld (pad %d)\n", i, truesize[i],
segsize[i], segbase[i], segpad[i]);
}
#endif
}
static void numbersymbols(void);
int outpass(void)
{
off_t base = sizeof(obh);
int i;
/* Skip any passes we don't want to do for this platform */
if (!passbegin(pass))
return 0;
if (pass)
dumpseginfo();
/* Pass 2 locks everything down so we can then set up truesize. For
a 2 pass (0/3 CPU) we are fine as pass 0 will lock down and truesize
is already clear */
if (pass < 3) {
for (i = 0; i < OSEG; i++) {
truesize[i] = 0;
segsize[i] = 0;
segpad[i] = 0;
}
}
if (pass == 3) {
/* Lay the file out */
for (i = 0; i < OSEG; i++) {
segbase[i] = base;
if (i != BSS) {
obh.o_segbase[i] = base;
base += segsize[i] + segpad[i] + 2; /* 2 for the EOF mark */
}
obh.o_size[i] = truesize[i];
/* This will then count up again so we don't get
a bogus segment limit error */
truesize[i] = 0;
segsize[i] = 0;
}
obh.o_magic = 0;
obh.o_arch = ARCH;
obh.o_flags = ARCH_FLAGS;
obh.o_cpuflags = cpu_flags;
obh.o_symbase = base;
obh.o_dbgbase = 0; /* for now */
/* Number the symbols for output */
numbersymbols();
dumpseginfo();
outsegment(segment);
}
return 1;
}
/*
* Absolute address change
*/
void outabsolute(addr_t addr)
{
if (segment != ABSOLUTE)
qerr(MUST_BE_ABSOLUTE);
else {
outbyte(REL_ESC);
outbyte(REL_ORG);
outbyte(addr);
outbyte(addr >> 8);
#ifdef ARCH32
outbyte(addr >> 16);
outbyte(addr >> 24);
#endif
}
}
/*
* Segment change
*/
void outsegment(int seg)
{
/* Seek to the current writing address for this segment */
if (pass == 3) {
// fprintf(stderr, "Writing segment %d(%x) at %ld\n",
// seg, segsize[seg],(long)segbase[seg]);
fseek(ofp, segbase[seg], SEEK_SET);
}
}
/*
* Output a word. Use the target word order with padding as needed
*/
static void check_store_allowed(uint8_t segment, uint16_t value)
{
if (value == 0)
return;
if (segment == BSS)
err('b', DATA_IN_BSS);
}
/* Write an address out according to size */
static void outaddr(addr_t a, unsigned s)
{
#ifdef TARGET_BIGENDIAN
unsigned shift = 8 * (s - 1);
while(s--) {
outab(a >> shift);
a <<= 8;
}
#else
while(s--) {
outab(a);
a >>= 8;
}
#endif
}
/* Ditto allowing for quoting changes */
static void outqaddr(addr_t a, unsigned s)
{
#ifdef TARGET_BIGENDIAN
unsigned shift = 8 * (s - 1);
while(s--) {
outabyte(a >> shift);
a <<= 8;
}
#else
while(s--) {
outabyte(a);
a >>= 8;
}
#endif
}
static void outqcaddr(addr_t a, unsigned s)
{
#ifdef TARGET_BIGENDIAN
unsigned shift = 8 * (s - 1);
while(s--) {
outab2(a >> shift);
a <<= 8;
}
#else
while(s--) {
outab2(a);
a >>= 8;
}
#endif
}
/*
* Symbol numbers and relocations are always written little endian
* for simplicity.
*
* A_LOW and A_HIGH indicate 8bit partial relocations. We handle these
* internally.
*/
static void outreloc(register ADDR *a, int bytes)
{
int s = (bytes - 1) << 4;
unsigned n = a->a_value;
unsigned f = a->a_flags;
/* We must insert a relocation record for anything relocatable,
but also for anything which is a symbol, as the linker may
need to do absolute resolution between modules */
if (a->a_segment != ABSOLUTE || a->a_sym) {
outbyte(REL_ESC);
check_store_allowed(segment, 1);
#ifdef TARGET_RELOC_OVERFLOW_OK
outbyte(REL_OVERFLOW);
#endif
/* TODO: Better error ? */
if (bytes != 1 && (f & (A_LOW|A_HIGH)))
qerr(CONSTANT_RANGE);
/* low bits of 16 bit is an 8bit relocation with
overflow suppressed */
if (f & A_LOW) {
#ifndef TARGET_RELOC_OVERFLOW_OK
outbyte(REL_OVERFLOW);
#endif
s = 0 << 4;
n &= 0xFF;
}
if (f & A_HIGH) {
outbyte(REL_HIGH);
/* High relocations are two byte to allow the linker
to calculate carry between the low/high byte */
s = 1 << 4;
}
if (a->a_sym == NULL) {
/* low bits of 16 bit is an 8bit relocation with
overflow suppressed */
/* Trap bogus relocation records */
if (a->a_segment == 15)
aerr(PHASE_ERROR);
outbyte(s | REL_SIMPLE | a->a_segment);
} else {
outbyte(s | REL_SYMBOL);
outbyte(a->a_sym->s_number & 0xFF);
outbyte(a->a_sym->s_number >> 8);
}
/* Relocatable constant, store unquoted as know the size */
if (f & A_LOW)
outabyte(n);
else if (f & A_HIGH) {
/* FIXME: > 2 bytes */
#ifdef TARGET_BIGENDIAN
outabyte(n >> 8);
/* We need this to relocate but it is not really in
the program stream so don't count it as such */
outbyte(n);
#else
outbyte(n);
outabyte(n >> 8);
#endif
} else
outqaddr(n, bytes);
} else {
/* Relocatable constant. This may change and thus need
to be padded */
if (shift) {
if (shift < 0)
n <<= shift;
else
n >>= shift;
}
if (rel_check && (n & ~mask))
err('o', CONSTANT_RANGE);
n &= mask;
/* FIXME: need to deal with 24/32bit sizes wit HIGH */
if (f & A_HIGH) {
outab2(n >> 8);
} else if (f & A_LOW) {
if (bytes == 1)
outab2(n & 0xFF);
else
outab2(n & 0xFF);
} else {
if (bytes == 1)
outab(n);
else
outqcaddr(n, bytes);
}
}
}
void outrad(ADDR *a)
{
outreloc(a, 4);
}
void outrat(ADDR *a)
{
outreloc(a, 3);
}
void outraw(ADDR *a)
{
outreloc(a, 2);
}
void outrab(ADDR *a)
{
outreloc(a, 1);
}
/*
* Check the bytes being added actually fit.
*/
void sizecheck(int segment)
{
if (full[segment])
err('o', SEGMENT_OVERFLOW);
if (pass == 3 && (truesize[segment] == SEGMENT_LIMIT || dot[segment] == SEGMENT_LIMIT))
full[segment] = 1;
}
/*
* Output an absolute
* byte to the code and listing
* streams.
*/
void outab(uint8_t b)
{
/* Not allowed to put data in the BSS except zero */
check_store_allowed(segment, b);
sizecheck(segment);
outbyte(b);
if (b == REL_ESC) /* Quote relocation markers */
outbyte(REL_REL);
++dot[segment];
++truesize[segment];
list_addbyte(b);
}
/*
* Output an absolute unquoted program byte to the code and listing
* streams.
*/
void outabyte(uint8_t b)
{
/* Not allowed to put data in the BSS except zero */
check_store_allowed(segment, b);
sizecheck(segment);
outbyte(b);
++dot[segment];
++truesize[segment];
list_addbyte(b);
}
/*
* Output an absolute byte to the code and listing
* streams. This version is used when the caller does not know the final
* value until the last pass. In that case we allocate an extra byte for
* unquoted values just in case. As the stream has an EOF marker a little bit
* of padding is fine and does no harm.
*/
void outab2(uint8_t b)
{
/* Not allowed to put data in the BSS except zero */
check_store_allowed(segment, b);
sizecheck(segment);
outbyte(b);
if (b == REL_ESC) /* Quote relocation markers */
outbyte(REL_REL);
else
reservebyte();
++dot[segment];
++truesize[segment];
list_addbyte(b);
}
void outabchk2(register ADDR *a)
{
uint8_t b;
register unsigned v = a->a_value;
if (a->a_flags & A_LOW)
b = v;
else if (a->a_flags & A_HIGH)
b = v >> 8;
else {
if (v > 255)
err('o', CONSTANT_RANGE);
b = v;
}
outab(b);
}
void outrabrel(ADDR *a)
{
int16_t v = (int16_t)a->a_value;
check_store_allowed(segment, 1);
if (a->a_sym) {
outbyte(REL_ESC);
outbyte((0 << 4 ) | REL_PCREL);
outbyte(a->a_sym->s_number & 0xFF);
outbyte(a->a_sym->s_number >> 8);
/* TODO: look into ordering here */
outabyte(a->a_value);
outbyte(a->a_value >> 8);
return;
}
if (shift) {
if (shift < 0)
v <<= shift;
else
v >>= shift;
}
if (rel_check && (v & ~mask))
err('o', CONSTANT_RANGE);
v &= mask;
if (v < -128 || v > 127)
err('o', CONSTANT_RANGE);
/* relatives without a symbol don't need relocation but they
still may need a pad byte */
outab2(v);
}
/* Write a segment relative */
void outrel(ADDR *a, unsigned len)
{
addr_t av = (addr_t)a->a_value;
unsigned sc = (len - 1) << 4;
check_store_allowed(segment, 1);
if (a->a_sym) {
outbyte(REL_ESC);
outbyte(sc | REL_PCREL);
outbyte(a->a_sym->s_number & 0xFF);
outbyte(a->a_sym->s_number >> 8);
outaddr(av, len);
return;
} else if (segment == a->a_segment) {
/* We don't need to issue a relocation if it's within
segment */
if (obh.o_flags & OF_WORDMACHINE)
av -= dot[segment] / 2;
else
av -= dot[segment];
if (shift) {
if (shift < 0)
av <<= shift;
else
av >>= shift;
}
if (rel_check && (av & ~mask))
err('o', CONSTANT_RANGE);
av &= mask;
outaddr(av, len);
} else {
outbyte(REL_ESC);
outbyte(REL_PCR);
outbyte(sc | REL_SIMPLE | a->a_segment);
outaddr(av, len);
}
}
void outrawrel(ADDR *a)
{
outrel(a, 2);
}
void outratrel(ADDR *a)
{
outrel(a, 3);
}
void outradrel(ADDR *a)
{
outrel(a, 4);
}
static void putsymbol(SYM *s, FILE *ofp)
{
uint8_t flag = 0;
if (s->s_type == TNEW)
flag |= S_UNKNOWN;
else {
if (s->s_type & TPUBLIC)
flag |= S_PUBLIC;
}
/* 0 absolute, 1-n segments, 15 don't care */
flag |= (s->s_segment & S_SEGMENT);
putc(flag, ofp);
fwrite(s->s_id, NAMELEN, 1, ofp);
putc(s->s_value, ofp);
putc(s->s_value >> 8, ofp);
#ifdef ARCH32
putc(s->s_value >> 16, ofp);
putc(s->s_value >> 24, ofp);
#endif
}
static void enumerate(SYM *s, FILE *dummy)
{
static int sym = 0;
s->s_number = sym++;
}
static void dosymbols(SYM *hash[], FILE *ofp, int flag, void (*op)(SYM *, FILE *f))
{
int i;
for (i = 0; i < NHASH; i++) {
SYM *s;
for (s = hash[i]; s != NULL; s = s->s_fp) {
int t = s->s_type & TMMODE;
int n;
if (t != TUSER && t != TNEW)
continue;
n = (t == TNEW) || (t == TUSER && (s->s_type & TPUBLIC));
if (n == flag)
op(s, ofp);
}
}
}
static void writesymbols(SYM *hash[], FILE *ofp)
{
fseek(ofp, obh.o_symbase, SEEK_SET);
dosymbols(hash, ofp, 1, putsymbol);
obh.o_dbgbase = ftell(ofp);
if (debug_write) {
dosymbols(uhash, ofp, 0, putsymbol);
}
}
static void numbersymbols(void)
{
dosymbols(uhash, NULL, 1, enumerate);
}
/*
* Put out the end of file
* hex item at the very end of
* the object file.
*/
void outeof(void)
{
int i;
/* We don't do the final write out if there was an error. That
leaves the magic wrong on the object file so it can't be used */
if (noobj || pass < 3)
return;
/* Target specifc flush of anything pending */
doflush();
dumpseginfo();
for (i = 0; i < OSEG; i++) {
/* The BSS is not written out */
if (i == BSS)
continue;
segment = i;
outsegment(i);
outbyte(REL_ESC);
outbyte(REL_EOF);
}
writesymbols(uhash, ofp);
rewind(ofp);
obh.o_magic = MAGIC_OBJ;
fwrite(&obh, sizeof(obh), 1, ofp);
// printf("Abs %d bytes: Code %d bytes: Data %d bytes: BSS %d bytes\n",
// truesize[ABSOLUTE], truesize[CODE], truesize[DATA], truesize[BSS]);
}
/*
* Output a byte and track our position. For BSS we care about sizes
* only.
*/
void outbyte(uint8_t b)
{
if (pass == 3 && segment != BSS)
putc(b, ofp);
segbase[segment]++;
segsize[segment]++;
}
/* Reserve space for a possible size change on the final pass */
void reservebyte(void)
{
segpad[segment]++;
}
void outscale(unsigned s, unsigned m)
{
outbyte(REL_ESC);
outbyte(REL_MOD);
outbyte(m);
outbyte(s);
mask = (1UL << (m & 0x1F)) - 1;
if (m & 0x80)
mask = ~mask;
shift = s & 0x0F;
if (shift & 0x80)
s = -s;
rel_check = m & 0x40;
}