-
Notifications
You must be signed in to change notification settings - Fork 9
/
supp.c
749 lines (606 loc) · 13.4 KB
/
supp.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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/* supp.c miscellaneous support routines */
/* (c) in 2008-2023 by Frank Wille */
#include <math.h>
#include "vasm.h"
#include "supp.h"
void initlist(struct list *l)
/* initializes a list structure */
{
l->first = (struct node *)&l->dummy;
l->dummy = NULL;
l->last = (struct node *)&l->first;
}
void addtail(struct list *l,struct node *n)
/* add node as last element of list */
{
struct node *ln = l->last;
n->next = ln->next;
ln->next = n;
n->pred = ln;
l->last = n;
}
struct node *remnode(struct node *n)
/* remove a node from a list */
{
n->next->pred = n->pred;
n->pred->next = n->next;
return n;
}
struct node *remhead(struct list *l)
/* remove first node in list and return a pointer to it */
{
struct node *n = l->first;
if (n->next) {
l->first = n->next;
n->next->pred = n->pred;
return n;
}
return NULL;
}
void *mymalloc(size_t sz)
{
size_t *p;
/* workaround for Electric Fence on 64-bit RISC */
if (sz)
sz = (sz + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1);
if (debug) {
if (sz == 0) {
printf("Warning! Allocating 0 bytes. Adjusted to 1 byte.\n");
sz = 1;
}
p = malloc(sz+2*sizeof(size_t));
if (!p)
general_error(17);
p++;
*p++ = sz;
memset(p,0xdd,sz); /* make it crash, when using uninitialized memory */
}
else {
p = malloc(sz?sz:1);
if(!p)
general_error(17);
}
return p;
}
void *mycalloc(size_t sz)
{
void *p = mymalloc(sz);
memset(p,0,sz);
return p;
}
void *myrealloc(void *old,size_t sz)
{
size_t *p;
if (debug) {
p = realloc(old?((size_t *)old)-2:0,sz+2*sizeof(size_t));
if (!p)
general_error(17);
p++;
*p++ = sz;
}
else {
p = realloc(old,sz);
if (!p)
general_error(17);
}
return p;
}
void myfree(void *p)
{
if (p) {
if (debug) {
size_t *myp = (size_t *)p;
size_t sz = *(--myp);
memset(p,0xff,sz); /* make it crash, when reusing deallocated memory */
free(--myp);
}
else
free(p);
}
}
int field_overflow(int signedbits,size_t numbits,taddr bitval)
{
if (signedbits) {
uint64_t mask = ~MAKEMASK(numbits - 1);
uint64_t val = (int64_t)bitval;
return (bitval < 0) ? (val & mask) != mask : (val & mask) != 0;
}
else
return (((uint64_t)(utaddr)bitval) & ~MAKEMASK(numbits)) != 0;
}
taddr bf_sign_extend(taddr val,int numbits)
/* sign-extend a bitfield value which fits into numbits bits */
{
taddr himask = ~MAKEMASK(numbits);
if (!(val & himask) && (val & (1LL<<(numbits-1))))
val |= himask; /* extend bitfield-sign over the taddr type */
return val;
}
uint64_t readval(int be,void *src,size_t size)
/* read value with given endianness */
{
unsigned char *s = src;
uint64_t val = 0;
if (size > sizeof(uint64_t))
ierror(0);
if (be) {
while (size--) {
val <<= 8;
val += (uint64_t)*s++;
}
}
else {
s += size;
while (size--) {
val <<= 8;
val += (uint64_t)*(--s);
}
}
return val;
}
void *setval(int be,void *dest,size_t size,uint64_t val)
/* write value to destination with desired endianness */
{
uint8_t *d = dest;
if (size > sizeof(uint64_t))
ierror(0);
if (be) {
d += size;
dest = d;
while (size--) {
*(--d) = (uint8_t)val;
val >>= 8;
}
}
else {
while (size--) {
*d++ = (uint8_t)val;
val >>= 8;
}
dest = d;
}
return dest;
}
void *setval_signext(int be,void *dest,size_t extsz,size_t valsz,int64_t val)
/* write a sign-extended value to destination with desired endianness */
{
uint8_t *d = dest;
int sign = val<0 ? 0xff : 0;
if (valsz > sizeof(uint64_t))
ierror(0);
if (be) {
memset(d,sign,extsz);
d += extsz + valsz;
dest = d;
while (valsz--) {
*(--d) = (uint8_t)val;
val >>= 8;
}
}
else {
while (valsz--) {
*d++ = (uint8_t)val;
val >>= 8;
}
memset(d,sign,extsz);
dest = d + extsz;
}
return dest;
}
uint64_t readbits(int be,void *p,unsigned bfsize,unsigned offset,unsigned size)
/* read value from a bitfield (max. 64 bits) */
{
if ((bfsize&7)==0 && offset+size<=bfsize) {
uint64_t mask = MAKEMASK(size);
uint64_t val = readval(be,p,bfsize>>3);
return be ? ((val >> (bfsize-(offset+size))) & mask)
: ((val >> offset) & mask);
}
ierror(0);
return 0;
}
void setbits(int be,void *p,unsigned bfsize,unsigned offset,unsigned size,
uint64_t d)
/* write value to a bitfield (max. 64 bits) */
{
if ((bfsize&7)==0 && offset+size<=bfsize) {
uint64_t mask = MAKEMASK(size);
uint64_t val = readval(be,p,bfsize>>3);
int s = be ? bfsize - (offset + size) : offset;
setval(be,p,bfsize>>3,(val & ~(mask<<s)) | ((d & mask) << s));
}
else
ierror(0);
}
int countbits(taddr val)
/* count number of bits in val */
{
int cnt = 0;
int len = sizeof(taddr) << 3;
while (len--) {
if (val & 1)
cnt++;
val >>= 1;
}
return cnt;
}
void copy_cpu_taddr(void *dest,taddr val,size_t bytes)
/* copy 'bytes' low-order bytes from val to dest in cpu's endianness */
{
uint8_t *d = dest;
int i;
if (bytes > sizeof(taddr))
ierror(0);
if (BIGENDIAN) {
for (i=bytes-1; i>=0; i--,val>>=8)
d[i] = (uint8_t)val;
}
else if (LITTLEENDIAN) {
for (i=0; i<(int)bytes; i++,val>>=8)
d[i] = (uint8_t)val;
}
else
ierror(0);
}
int patch_nreloc(atom *a,rlist *rl,int signedflag,taddr val,int be)
/* patch relocated value into the atom, when rlist contains an nreloc */
{
nreloc *nrel;
char *p;
if (rl->type > LAST_STANDARD_RELOC) {
unsupp_reloc_error(rl);
return 0;
}
nrel = (nreloc *)rl->reloc;
if (field_overflow(signedflag,nrel->size,val)) {
output_atom_error(12,a,rl->type,(unsigned long)nrel->mask,nrel->sym->name,
(unsigned long)nrel->addend,nrel->size);
return 0;
}
if (a->type == DATA)
p = (char *)a->content.db->data + nrel->byteoffset;
else if (a->type == SPACE)
p = (char *)a->content.sb->fill; /* @@@ ignore offset completely? */
else
return 1;
setbits(be,p,(nrel->bitoffset+nrel->size+7)&~7,
nrel->bitoffset,nrel->size,val);
return 1;
}
#if FLOAT_PARSER
void conv2ieee32(int be,uint8_t *buf,tfloat f)
/* single precision */
{
union {
float sp;
uint32_t x;
} conv;
conv.sp = (float)f;
setval(be,buf,4,conv.x);
}
void conv2ieee64(int be,uint8_t *buf,tfloat f)
/* double precision */
{
union {
double dp;
uint64_t x;
} conv;
conv.dp = (double)f;
setval(be,buf,8,conv.x);
}
/* check if float can be represented by bits, signed or unsigned,
ignoring the fractional part */
int flt_chkrange(tfloat f,int bits)
{
if (bits <= sizeof(taddr)*8) {
tfloat max = (utaddr)1LL<<(bits-1);
return (f<2.0*max && f>=-max);
}
ierror(0); /* FIXME - shouldn't happen? */
return 0;
}
#endif /* FLOAT_PARSER */
void fw8(FILE *f,uint8_t x)
{
if (fputc(x,f) == EOF)
output_error(2); /* write error */
}
void fw16(FILE *f,uint16_t x,int be)
{
if (be) {
fw8(f,(x>>8) & 0xff);
fw8(f,x & 0xff);
}
else {
fw8(f,x & 0xff);
fw8(f,(x>>8) & 0xff);
}
}
void fw24(FILE *f,uint32_t x,int be)
{
if (be) {
fw8(f,(x>>16) & 0xff);
fw16(f,(uint16_t)x,1);
}
else {
fw16(f,(uint16_t)x,0);
fw8(f,(x>>16) & 0xff);
}
}
void fw32(FILE *f,uint32_t x,int be)
{
if (be) {
fw8(f,(x>>24) & 0xff);
fw8(f,(x>>16) & 0xff);
fw8(f,(x>>8) & 0xff);
fw8(f,x & 0xff);
}
else {
fw8(f,x & 0xff);
fw8(f,(x>>8) & 0xff);
fw8(f,(x>>16) & 0xff);
fw8(f,(x>>24) & 0xff);
}
}
void fwdata(FILE *f,const void *d,size_t n)
{
if (n) {
if (!fwrite(d,1,n,f))
output_error(2); /* write error */
}
}
void fwsblock(FILE *f,sblock *sb)
{
size_t i;
for (i=0; i<sb->space; i++) {
if (!fwrite(sb->fill,sb->size,1,f))
output_error(2); /* write error */
}
}
void fwspace(FILE *f,size_t n)
{
size_t i;
for (i=0; i<n; i++) {
if (fputc(0,f) == EOF)
output_error(2); /* write error */
}
}
void fwalign(FILE *f,taddr n,taddr align)
{
fwspace(f,balign(n,align));
}
int fwalignpattern(FILE *f,taddr n,uint8_t *pat,int patlen)
{
int align_warning = 0;
while (n % patlen) {
align_warning = 1;
fw8(f,0);
n--;
}
/* write alignment pattern */
while (n >= patlen) {
if (!fwrite(pat,patlen,1,f))
output_error(2); /* write error */
n -= patlen;
}
while (n--) {
align_warning = 1;
fw8(f,0);
}
#if 0
if (align_warning)
output_error(9,sec->name,(unsigned long)n,(unsigned long)patlen,
ULLTADDR(pc));
#endif
return align_warning;
}
taddr fwpcalign(FILE *f,atom *a,section *sec,taddr pc)
{
taddr n = balign(pc,a->align);
taddr patlen;
uint8_t *pat;
if (n == 0)
return pc;
if (a->type==SPACE && a->content.sb->space==0) { /* space align atom */
if (a->content.sb->maxalignbytes!=0 && n>a->content.sb->maxalignbytes)
return pc;
pat = a->content.sb->fill;
patlen = a->content.sb->size;
}
else {
pat = sec->pad;
patlen = sec->padbytes;
}
pc += n;
fwalignpattern(f,n,pat,patlen);
return pc;
}
size_t filesize(FILE *fp)
/* @@@ Warning! filesize() only works reliably on binary streams! @@@ */
{
long size;
if (fgetc(fp) != EOF)
if (fseek(fp,0,SEEK_END) >= 0)
if ((size = ftell(fp)) >= 0)
if (fseek(fp,0,SEEK_SET) >= 0)
return (size_t)size;
return 0;
}
int abs_path(char *path)
/* return true, when path is absolute */
{
return *path=='/' || *path=='\\' || strchr(path,':')!=NULL;
}
int stricmp(const char *str1,const char *str2)
{
while (tolower((unsigned char)*str1) == tolower((unsigned char)*str2)) {
if (!*str1) return 0;
str1++; str2++;
}
return tolower(*(unsigned char *)str1) - tolower(*(unsigned char *)str2);
}
int strnicmp(const char *str1,const char *str2,size_t n)
{
if (n==0) return 0;
while (--n && tolower((unsigned char)*str1) == tolower((unsigned char)*str2)) {
if (!*str1) return 0;
str1++; str2++;
}
return tolower(*(unsigned char *)str1) - tolower(*(unsigned char *)str2);
}
char *mystrdup(const char *name)
{
char *p=mymalloc(strlen(name)+1);
strcpy(p,name);
return p;
}
char *cnvstr(const char *name,int l)
/* converts a pair of pointer/length to a null-terminated string */
{
char *p=mymalloc(l+1);
memcpy(p,name,l);
p[l]=0;
return p;
}
char *strbuf_alloc(strbuf *buf,size_t sz)
/* make sure static strbuf has space for 'sz' bytes */
{
if (sz > buf->size) {
buf->size = (sz+(STRBUFINC-1)) & ~(STRBUFINC-1);
return buf->str = myrealloc(buf->str,buf->size);
}
return buf->str;
}
char *cutstr(strbuf *buf,const char *name,size_t len)
{
if (len >= buf->size) {
buf->size = (len+STRBUFINC) & ~(STRBUFINC-1);
buf->str = myrealloc(buf->str,buf->size);
}
buf->str[len] = 0;
buf->len = len;
return memcpy(buf->str,name,len);
}
char *strtolower(char *s)
/* convert a whole string to lower case */
{
char *p;
for (p=s; *p; p++)
*p = tolower((unsigned char)*p);
return s;
}
int str_is_graph(const char *s)
/* tests if whole string has printable characters and no spaces */
{
while (*s != '\0') {
if (!isgraph((unsigned char)*s))
return 0;
s++;
}
return 1;
}
const char *trim(const char *s)
/* trim blanks before s */
{
while (isspace((unsigned char )*(s-1)))
s--;
return s;
}
char *get_str_arg(const char *s)
/* get string argument from the command line, optionally in quotes */
{
int term = 0;
char *e;
if (*s == '\"')
term = *s++;
if (!(e = strchr(s,term)))
e = strchr(s,0);
return cnvstr(s,e-s);
}
taddr balign(taddr addr,taddr a)
/* return number of bytes required to achieve alignment */
{
if (a) {
if (addr %= a)
return a - addr;
}
return 0;
}
taddr palign(taddr addr,int a)
/* return number of bytes required to achieve alignment */
{
return balign(addr,((taddr)1)<<a);
}
taddr pcalign(atom *a,taddr pc)
{
taddr n = balign(pc,a->align);
if (a->type==SPACE && a->content.sb->maxalignbytes!=0)
if (n > a->content.sb->maxalignbytes)
n = 0;
return pc + n;
}
int make_padding(taddr val,uint8_t *pad,int maxlen)
/* fill a padding array from a given padding value, return length in bytes */
{
utaddr uval;
int len;
for (len=0,uval=(utaddr)val; uval!=0; uval>>=8,len++);
if (len > maxlen)
len = maxlen;
copy_cpu_taddr(pad,val,len);
return len;
}
taddr get_sym_value(symbol *s)
/* determine symbol's value, returns alignment for common symbols */
{
if (s->flags & COMMON) {
return (taddr)s->align;
}
else if (s->type == LABSYM) {
return s->pc;
}
else if (s->type == EXPRESSION) {
if (s->expr) {
taddr val;
eval_expr(s->expr,&val,NULL,0);
return val;
}
else
ierror(0);
}
return 0;
}
taddr get_sym_size(symbol *s)
/* determine symbol's size */
{
if (s->size) {
taddr val;
eval_expr(s->size,&val,NULL,0);
return val;
}
return 0;
}
utaddr get_sec_size(section *sec)
{
/* section size is assumed to be in in (sec->pc - sec->org), otherwise
we would have to calculate it from the atoms and store it there */
return sec ? (utaddr)sec->pc - (utaddr)sec->org : 0;
}
int get_sec_type(section *s)
/* determine section type from its attributes */
{
char *a = s->attr;
if (s->flags & ABSOLUTE)
return S_ABS;
while (*a) {
switch (*a++) {
case 'c':
return S_TEXT;
case 'd':
return S_DATA;
case 'u':
return S_BSS;
}
}
return S_MISS; /* type is missing */
}