-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_hunk.c
1063 lines (914 loc) · 26.2 KB
/
output_hunk.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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* output_hunk.c AmigaOS hunk format output driver for vasm */
/* (c) in 2002-2020 by Frank Wille */
#include "vasm.h"
#include "osdep.h"
#include "output_hunk.h"
#if defined(OUTHUNK) && (defined(VASM_CPU_M68K) || defined(VASM_CPU_PPC))
static char *copyright="vasm hunk format output module 2.13 (c) 2002-2020 Frank Wille";
int hunk_onlyglobal;
/* (currenty two-byte only) padding value for not 32-bit aligned code hunks */
#ifdef VASM_CPU_M68K
static uint16_t hunk_pad = 0x4e71;
#else
static uint16_t hunk_pad = 0;
#endif
static int databss;
static int kick1;
static int exthunk;
static int genlinedebug;
static int keep_empty_sects;
static uint32_t sec_cnt;
static symbol **secsyms;
static uint32_t strlen32(char *s)
/* calculate number of 32 bit words required for
a string without terminator */
{
return (strlen(s) + 3) >> 2;
}
static void fwname(FILE *f,char *name)
{
size_t n = strlen(name);
fwdata(f,name,n);
fwalign(f,n,4);
}
static void fwmemflags(FILE *f,section *s,uint32_t data)
/* write memory attributes with data (section size or type) */
{
uint32_t mem = s->memattr;
if ((mem & ~MEMF_PUBLIC) == 0) {
fw32(f,data,1);
}
else if ((mem & ~MEMF_PUBLIC) == MEMF_CHIP) {
fw32(f,HUNKF_CHIP|data,1);
}
else if ((mem & ~MEMF_PUBLIC) == MEMF_FAST) {
fw32(f,HUNKF_FAST|data,1);
}
else {
fw32(f,HUNKF_MEMTYPE|data,1);
fw32(f,mem,1);
}
}
static void fwnopalign(FILE *f,taddr n)
/* align a 68k code section with NOP instructions */
{
taddr i;
n = balign(n,4);
if (n & 1)
ierror(0);
for (i=0; i<n; i+=2)
fw16(f,hunk_pad,1);
}
static section *dummy_section(void)
{
return new_section(".text","acrx3",8);
}
static uint32_t scan_attr(section *sec)
/* extract hunk-type from section attributes */
{
uint32_t type = 0;
char *p = sec->attr;
if (*p != '\0') {
while (*p) {
switch (*p++) {
#if defined(VASM_CPU_PPC)
case 'c': type = HUNK_PPC_CODE; break;
#else
case 'c': type = HUNK_CODE; break;
#endif
case 'd': type = HUNK_DATA; break;
case 'u': type = HUNK_BSS; break;
}
}
}
else
type = HUNK_DATA;
return type;
}
static section *prepare_sections(section *first_sec,symbol *sym)
/* Remove empty sections.
Assign all symbol definitions to their respective section.
Make sure that every common-symbol is referenced, otherwise there
is no possibility to represent such a symbol in hunk format.
Additionally we have to guarantee that at least one section exists,
when there are any symbols.
Remember the total number of sections left in sec_cnt. */
{
section *sec,*first_nonbss;
symbol *nextsym;
rlist *rl;
atom *a;
for (sec_cnt=0,sec=first_sec,first_nonbss=NULL; sec!=NULL; sec=sec->next) {
/* ignore empty sections without symbols, unless -keepempty was given */
if (keep_empty_sects || get_sec_size(sec)!=0 || (sec->flags&HAS_SYMBOLS)) {
sec->idx = sec_cnt++;
}
else {
sec->flags |= SEC_DELETED;
continue;
}
/* determine first initialized section for common-symbol references */
if (first_nonbss==NULL && scan_attr(sec)!=HUNK_BSS)
first_nonbss = sec;
/* flag all present common-symbol references from this section */
for (a=sec->first; a; a=a->next) {
if (a->type == DATA) {
for (rl=a->content.db->relocs; rl; rl=rl->next) {
if (rl->type==REL_ABS || rl->type==REL_PC) {
if (((nreloc *)rl->reloc)->size==32) {
symbol *s = ((nreloc *)rl->reloc)->sym;
if (s->flags & COMMON)
s->flags |= COMM_REFERENCED;
}
}
}
}
}
}
/* Allocate symbol lists for all sections.
Get one more for a potential dummy section. */
secsyms = mycalloc((sec_cnt+1)*sizeof(symbol *));
/* Assign all valid symbol definitions to their section symbol list.
Check for missing common-symbol references. */
while (sym != NULL) {
nextsym = sym->next;
if (*sym->name != ' ') { /* internal symbols will be ignored */
if ((sym->flags & COMMON) && !(sym->flags & COMM_REFERENCED)) {
/* create a dummy reference for each unreferenced common symbol */
dblock *db = new_dblock();
nreloc *r = new_nreloc();
rlist *rl = mymalloc(sizeof(rlist));
db->size = 4;
db->data = mycalloc(db->size);
db->relocs = rl;
rl->next = NULL;
rl->type = REL_ABS;
rl->reloc = r;
r->size = 32;
r->sym = sym;
if (first_nonbss == NULL) {
first_nonbss = dummy_section();
if (first_sec == NULL)
first_sec = first_nonbss;
first_nonbss->idx = sec_cnt++;
}
add_atom(first_nonbss,new_data_atom(db,4));
}
else if (sym->flags & WEAK) {
/* weak symbols are not supported, make them global */
sym->flags &= ~WEAK;
sym->flags |= EXPORT;
output_error(10,sym->name);
}
if (sym->type==LABSYM ||
(sym->type==EXPRESSION && (sym->flags&EXPORT))) {
if (sym->type == EXPRESSION) {
/* put absolute globals symbols into the first section */
if (first_sec == NULL) {
first_sec = first_nonbss = dummy_section();
first_sec->idx = sec_cnt++;
}
first_sec->flags |= HAS_SYMBOLS;
sym->sec = first_sec;
}
/* assign symbols to the section they are defined in */
sym->next = secsyms[sym->sec->idx];
secsyms[sym->sec->idx] = sym;
}
}
sym = nextsym;
}
return first_sec;
}
static utaddr file_size(section *sec)
/* determine a section's initialized data size, which occupies space in
the executable file */
{
utaddr pc=0,zpc=0,npc;
atom *a;
for (a=sec->first; a; a=a->next) {
int zerodata = 1;
unsigned char *d;
npc = pcalign(a,pc);
if (a->type == DATA) {
/* do we have relocations or non-zero data in this atom? */
if (a->content.db->relocs) {
zerodata = 0;
}
else {
for (d=a->content.db->data;
d<a->content.db->data+a->content.db->size; d++) {
if (*d) {
zerodata = 0;
break;
}
}
}
}
else if (a->type == SPACE) {
/* do we have relocations or non-zero data in this atom? */
if (a->content.sb->relocs) {
zerodata = 0;
}
else {
for (d=a->content.sb->fill;
d<a->content.sb->fill+a->content.sb->size; d++) {
if (*d) {
zerodata = 0;
break;
}
}
}
}
pc = npc + atom_size(a,sec,npc);
if (!zerodata)
zpc = pc;
}
return zpc;
}
static utaddr sect_size(section *sec)
/* recalculate full section size, subtracted by the space explicitely
reserved for databss (DX directive) */
{
utaddr pc=0,dxpc=0;
atom *a;
for (a=sec->first; a; a=a->next) {
taddr sz;
pc = pcalign(a,pc);
sz = atom_size(a,sec,pc);
if (a->type == SPACE) {
sblock *sb = a->content.sb;
if (sb->flags & SPC_DATABSS) {
if (dxpc == 0)
dxpc = pc;
}
else if (sz > 0 && dxpc != 0) {
output_atom_error(13,a); /* warn about data following a DX directive */
dxpc = 0;
}
}
else if (a->type == DATA) {
if (dxpc != 0) {
output_atom_error(13,a); /* warn about data following a DX directive */
dxpc = 0;
}
}
else if (sz != 0 && dxpc != 0)
ierror(0); /* only DATA and SPACE can have a size!? */
pc += sz;
}
return dxpc ? dxpc : pc;
}
static struct hunkreloc *convert_reloc(rlist *rl,utaddr pc)
{
nreloc *r = (nreloc *)rl->reloc;
if (rl->type <= LAST_STANDARD_RELOC
#if defined(VASM_CPU_PPC)
|| rl->type==REL_PPCEABI_SDA2
#endif
) {
if (LOCREF(r->sym)) {
struct hunkreloc *hr;
uint32_t type;
uint32_t offs = pc + r->byteoffset;
switch (rl->type) {
case REL_ABS:
if (r->size!=32 || r->bitoffset!=0 || r->mask!=-1)
return NULL;
type = HUNK_ABSRELOC32;
break;
case REL_PC:
switch (r->size) {
case 8:
if (r->bitoffset!=0 || r->mask!=-1)
return NULL;
type = HUNK_RELRELOC8;
break;
#if defined(VASM_CPU_PPC)
case 14:
if (r->bitoffset!=0 || r->mask!=0xfffc)
return NULL;
type = HUNK_RELRELOC16;
break;
#endif
case 16:
if (r->bitoffset!=0 || r->mask!=-1)
return NULL;
type = HUNK_RELRELOC16;
break;
#if defined(VASM_CPU_PPC)
case 24:
if (r->bitoffset!=6 || r->mask!=0x3fffffc)
return NULL;
type = HUNK_RELRELOC26;
break;
#endif
case 32:
if (kick1 || r->bitoffset!=0 || r->mask!=-1)
return NULL;
type = HUNK_RELRELOC32;
break;
}
break;
#if defined(VASM_CPU_PPC)
case REL_PPCEABI_SDA2: /* treat as REL_SD for WarpOS/EHF */
#endif
case REL_SD:
if (r->size!=16 || r->bitoffset!=0 || r->mask!=-1)
return NULL;
type = HUNK_DREL16;
break;
default:
return NULL;
}
hr = mymalloc(sizeof(struct hunkreloc));
hr->rl = rl;
hr->hunk_id = type;
hr->hunk_offset = offs;
hr->hunk_index = r->sym->sec->idx;
return hr;
}
}
return NULL;
}
static struct hunkxref *convert_xref(rlist *rl,utaddr pc)
{
nreloc *r = (nreloc *)rl->reloc;
if (rl->type <= LAST_STANDARD_RELOC
#if defined(VASM_CPU_PPC)
|| rl->type==REL_PPCEABI_SDA2
#endif
) {
if (EXTREF(r->sym)) {
struct hunkxref *xref;
uint32_t type,size=0;
uint32_t offs = pc + r->byteoffset;
int com = (r->sym->flags & COMMON) != 0;
switch (rl->type) {
case REL_ABS:
if (r->bitoffset!=0 || r->mask!=-1 || (com && r->size!=32))
return NULL;
switch (r->size) {
case 8:
type = kick1 ? EXT_RELREF8 : EXT_ABSREF8;
break;
case 16:
type = kick1 ? EXT_RELREF16 : EXT_ABSREF16;
break;
case 32:
if (com) {
type = EXT_ABSCOMMON;
size = get_sym_size(r->sym);
}
else
type = EXT_ABSREF32;
break;
}
break;
case REL_PC:
switch (r->size) {
case 8:
if (r->bitoffset!=0 || r->mask!=-1 || com)
return NULL;
type = EXT_RELREF8;
break;
#if defined(VASM_CPU_PPC)
case 14:
if (r->bitoffset!=0 || r->mask!=0xfffc || com)
return NULL;
type = EXT_RELREF16;
break;
#endif
case 16:
if (r->bitoffset!=0 || r->mask!=-1 || com)
return NULL;
type = EXT_RELREF16;
break;
#if defined(VASM_CPU_PPC)
case 24:
if (r->bitoffset!=6 || r->mask!=0x3fffffc || com)
return NULL;
type = EXT_RELREF26;
break;
#endif
case 32:
if (kick1 || r->bitoffset!=0 || r->mask!=-1)
return NULL;
if (com) {
type = EXT_RELCOMMON;
size = get_sym_size(r->sym);
}
else
type = EXT_RELREF32;
break;
}
break;
#if defined(VASM_CPU_PPC)
case REL_PPCEABI_SDA2: /* treat as REL_SD for WarpOS/EHF */
#endif
case REL_SD:
if (r->size!=16 || r->bitoffset!=0 || r->mask!=-1)
return NULL;
type = EXT_DEXT16;
break;
default:
return NULL;
}
xref = mymalloc(sizeof(struct hunkxref));
xref->name = r->sym->name;
xref->type = type;
xref->size = size;
xref->offset = offs;
return xref;
}
}
return NULL;
}
static void process_relocs(atom *a,struct list *reloclist,
struct list *xreflist,section *sec,utaddr pc)
/* convert an atom's rlist into relocations and xrefs */
{
rlist *rl;
if (a->type == DATA)
rl = a->content.db->relocs;
else if (a->type == SPACE)
rl = a->content.sb->relocs;
else
return;
if (rl == NULL)
return;
do {
struct hunkreloc *hr = convert_reloc(rl,pc);
if (hr!=NULL && (xreflist!=NULL || hr->hunk_id==HUNK_ABSRELOC32 ||
hr->hunk_id==HUNK_RELRELOC32)) {
addtail(reloclist,&hr->n); /* add new relocation */
}
else {
struct hunkxref *xref = convert_xref(rl,pc);
if (xref) {
if (xreflist)
addtail(xreflist,&xref->n); /* add new external reference */
else
output_atom_error(8,a,xref->name,sec->name,xref->offset,rl->type);
}
else
unsupp_reloc_error(rl); /* reloc not supported */
}
}
while (rl = rl->next);
}
static void reloc_hunk(FILE *f,uint32_t type,int shrt,struct list *reloclist)
/* write all section-offsets for one relocation type */
{
int bytes = 0;
uint32_t idx;
for (idx=0; idx<sec_cnt; idx++) {
struct hunkreloc *r,*next;
uint32_t n;
int off16;
for (r=(struct hunkreloc *)reloclist->first,n=0,off16=1;
r->n.next; r=(struct hunkreloc *)r->n.next) {
if (r->hunk_id==type && r->hunk_index==idx) {
n++;
if (r->hunk_offset >= 0x10000)
off16 = 0;
}
}
if (shrt && (n>=0x10000 || off16==0))
continue; /* relocs for this hunk don't fit into 16-bit entries */
if (n > 0) {
if (bytes == 0) {
if (shrt && type==HUNK_ABSRELOC32)
fw32(f,HUNK_DREL32,1); /* RELOC32SHORT is DREL32 for OS2.0 */
else
fw32(f,type,1);
bytes = 4;
}
if (shrt) {
fw16(f,n,1);
fw16(f,idx,1);
bytes += 4;
}
else {
fw32(f,n,1);
fw32(f,idx,1);
bytes += 8;
}
r = (struct hunkreloc *)reloclist->first;
while (next = (struct hunkreloc *)r->n.next) {
if (r->hunk_id==type && r->hunk_index==idx) {
if (shrt) {
fw16(f,r->hunk_offset,1);
bytes += 2;
}
else {
fw32(f,r->hunk_offset,1);
bytes += 4;
}
remnode(&r->n);
myfree(r);
}
r = next;
}
}
}
if (bytes) {
if (shrt) {
fw16(f,0,1);
fwalign(f,bytes+2,4);
}
else
fw32(f,0,1);
}
}
static void add_linedebug(struct list *ldblist,source *src,int line,
uint32_t off)
{
char pathbuf[MAXPATHLEN];
struct linedb_block *ldbblk;
struct linedb_entry *ldbent;
/* get full source path and fix line number for macros and repetitions */
if (src != NULL) {
/* Submitted by Soren Hannibal:
Use parent source/line when no source level debugging is allowed
for this source text instance. */
while (!src->srcdebug && src->parent!=NULL) {
line = src->parent_line;
src = src->parent;
}
if (src->defsrc != NULL) {
line += src->defline;
src = src->defsrc;
}
pathbuf[0] = '\0';
if (src->srcfile->incpath != NULL) {
if (src->srcfile->incpath->compdir_based)
strcpy(pathbuf,compile_dir);
strcat(pathbuf,src->srcfile->incpath->path);
}
strcat(pathbuf,src->srcfile->name);
}
else /* line numbers and source defined by directive */
strcpy(pathbuf,getdebugname());
/* make a new LINE debug block whenever the source path changes */
if (ldblist->first->next==NULL
|| filenamecmp(pathbuf,
((struct linedb_block *)ldblist->last)->filename)) {
ldbblk = mymalloc(sizeof(struct linedb_block));
ldbblk->filename = mystrdup(pathbuf);
ldbblk->offset = off;
ldbblk->entries = 0;
initlist(&ldbblk->lines);
addtail(ldblist,&ldbblk->n);
}
else
ldbblk = (struct linedb_block *)ldblist->last;
/* append line/offset pair as a new entry for the debug block */
ldbent = mymalloc(sizeof(struct linedb_entry));
ldbent->line = (uint32_t)line;
ldbent->offset = off - ldbblk->offset;
addtail(&ldbblk->lines,&ldbent->n);
ldbblk->entries++;
}
static void linedebug_hunk(FILE *f,struct list *ldblist)
{
struct linedb_block *ldbblk;
while (ldbblk = (struct linedb_block *)remhead(ldblist)) {
char abspathbuf[MAXPATHLEN];
struct linedb_entry *ldbent;
uint32_t abspathlen;
/* get source file name as full absolute path */
if (!abs_path(ldbblk->filename)) {
char *cwd = append_path_delimiter(get_workdir());
snprintf(abspathbuf,MAXPATHLEN,"%s%s",cwd,ldbblk->filename);
myfree(cwd);
}
else
strcpy(abspathbuf,ldbblk->filename);
abspathlen = strlen32(abspathbuf);
/* write DEBUG-LINE hunk header */
fw32(f,HUNK_DEBUG,1);
fw32(f,abspathlen + ldbblk->entries*2 + 3,1);
fw32(f,ldbblk->offset,1);
fw32(f,0x4c494e45,1); /* "LINE" */
fw32(f,abspathlen,1);
fwname(f,abspathbuf);
/* writes and deallocate entries */
while (ldbent = (struct linedb_entry *)remhead(&ldbblk->lines)) {
fw32(f,ldbent->line,1);
fw32(f,ldbent->offset,1);
myfree(ldbent);
ldbblk->entries--;
}
/* deallocate debug block */
if (ldbblk->entries != 0)
ierror(0); /* shouldn't happen */
myfree(ldbblk);
}
}
static void extheader(FILE *f)
{
if (!exthunk) {
exthunk = 1;
fw32(f,HUNK_EXT,1);
}
}
static void exttrailer(FILE *f)
{
if (exthunk)
fw32(f,0,1);
}
static void ext_refs(FILE *f,struct list *xreflist)
/* write all external references from a section into a HUNK_EXT hunk */
{
while (xreflist->first->next) {
struct hunkxref *x,*next;
uint32_t n,type,size;
char *name;
extheader(f);
x = (struct hunkxref *)xreflist->first;
name = x->name;
type = x->type;
size = x->size;
for (n=0,x=(struct hunkxref *)xreflist->first;
x->n.next; x=(struct hunkxref *)x->n.next) {
if (!strcmp(x->name,name) && x->type==type)
n++;
}
fw32(f,(type<<24) | strlen32(name),1);
fwname(f,name);
if (type==EXT_ABSCOMMON || type==EXT_RELCOMMON)
fw32(f,size,1);
fw32(f,n,1);
x = (struct hunkxref *)xreflist->first;
while (next = (struct hunkxref *)x->n.next) {
if (!strcmp(x->name,name) && x->type==type) {
fw32(f,x->offset,1);
remnode(&x->n);
myfree(x);
}
x = next;
}
}
}
static void ext_defs(FILE *f,int symtype,int global,size_t idx,
uint32_t xtype)
{
int header = 0;
symbol *sym;
for (sym=secsyms[idx]; sym; sym=sym->next) {
if (sym->type==symtype && (sym->flags&global)==global) {
if (!header) {
header = 1;
if (xtype == EXT_SYMB)
fw32(f,HUNK_SYMBOL,1);
else
extheader(f);
}
fw32(f,(xtype<<24) | strlen32(sym->name),1);
fwname(f,sym->name);
fw32(f,(uint32_t)get_sym_value(sym),1);
}
}
if (header && xtype==EXT_SYMB)
fw32(f,0,1);
}
static void report_bad_relocs(struct list *reloclist)
{
struct hunkreloc *r;
/* report all remaining relocs in the list as unsupported */
for (r=(struct hunkreloc *)reloclist->first; r->n.next;
r=(struct hunkreloc *)r->n.next)
unsupp_reloc_error(r->rl); /* reloc not supported */
}
static void write_object(FILE *f,section *sec,symbol *sym)
{
int wrotesec = 0;
sec = prepare_sections(sec,sym);
/* write header */
fw32(f,HUNK_UNIT,1);
fw32(f,strlen32(filename),1);
fwname(f,filename);
if (sec) {
for (; sec; sec=sec->next) {
if (!(sec->flags & SEC_DELETED)) {
uint32_t type;
atom *a;
struct list reloclist,xreflist,linedblist;
wrotesec = 1;
initlist(&reloclist);
initlist(&xreflist);
initlist(&linedblist);
/* section name */
if (strlen(sec->name)) {
fw32(f,HUNK_NAME,1);
fw32(f,strlen32(sec->name),1);
fwname(f,sec->name);
}
/* section type */
if (!(type = scan_attr(sec))) {
output_error(3,sec->attr); /* section attributes not supported */
type = HUNK_DATA; /* default */
}
fwmemflags(f,sec,type);
fw32(f,(get_sec_size(sec)+3)>>2,1); /* size */
if (type != HUNK_BSS) {
/* write contents */
utaddr pc=0,npc;
for (a=sec->first; a; a=a->next) {
npc = fwpcalign(f,a,sec,pc);
if (genlinedebug && (a->type==DATA || a->type==SPACE))
add_linedebug(&linedblist,a->src,a->line,npc);
if (a->type == DATA)
fwdata(f,a->content.db->data,a->content.db->size);
else if (a->type == SPACE)
fwsblock(f,a->content.sb);
else if (a->type == LINE && !genlinedebug)
add_linedebug(&linedblist,NULL,a->content.srcline,npc);
process_relocs(a,&reloclist,&xreflist,sec,npc);
pc = npc + atom_size(a,sec,npc);
}
if (type == HUNK_CODE && (pc&1) == 0)
fwnopalign(f,pc);
else
fwalign(f,pc,4);
}
/* relocation hunks */
reloc_hunk(f,HUNK_ABSRELOC32,0,&reloclist);
reloc_hunk(f,HUNK_RELRELOC8,0,&reloclist);
reloc_hunk(f,HUNK_RELRELOC16,0,&reloclist);
reloc_hunk(f,HUNK_RELRELOC26,0,&reloclist);
reloc_hunk(f,HUNK_RELRELOC32,0,&reloclist);
reloc_hunk(f,HUNK_DREL16,0,&reloclist);
report_bad_relocs(&reloclist);
/* external references and global definitions */
exthunk = 0;
ext_refs(f,&xreflist);
if (sec->idx == 0) /* absolute definitions into first hunk */
ext_defs(f,EXPRESSION,EXPORT,0,EXT_ABS);
ext_defs(f,LABSYM,EXPORT,sec->idx,EXT_DEF);
exttrailer(f);
if (!no_symbols) {
/* symbol table */
if (!hunk_onlyglobal)
ext_defs(f,LABSYM,0,sec->idx,EXT_SYMB);
/* line-debug */
linedebug_hunk(f,&linedblist);
}
fw32(f,HUNK_END,1);
}
}
}
if (!wrotesec) {
/* there was no section at all - dummy section size 0 */
#if defined(VASM_CPU_PPC)
fw32(f,HUNK_PPC_CODE,1);
#else
fw32(f,HUNK_CODE,1);
#endif
fw32(f,0,1);
fw32(f,HUNK_END,1);
}
}
static void write_exec(FILE *f,section *sec,symbol *sym)
{
section *s;
sec = prepare_sections(sec,sym);
/* write header */
fw32(f,HUNK_HEADER,1);
fw32(f,0,1);
if (sec_cnt) {
fw32(f,sec_cnt,1); /* number of sections - no overlay support */
fw32(f,0,1); /* first section index: 0 */
fw32(f,sec_cnt-1,1); /* last section index: sec_cnt - 1 */
/* write section sizes and memory flags */
for (s=sec; s; s=s->next) {
if (!(s->flags & SEC_DELETED))
fwmemflags(f,s,(get_sec_size(s)+3)>>2);
}
/* section hunk loop */
for (; sec; sec=sec->next) {
if (!(sec->flags & SEC_DELETED)) {
uint32_t type;
atom *a;
struct list reloclist,linedblist;
initlist(&reloclist);
initlist(&linedblist);
/* write hunk-type and size */
if (!(type = scan_attr(sec))) {
output_error(3,sec->attr); /* section attributes not supported */
type = HUNK_DATA; /* default */
}
fw32(f,type,1);
if (type != HUNK_BSS) {
/* write contents */
utaddr pc,npc,size;
size = databss ? file_size(sec) : sect_size(sec);
fw32(f,(size+3)>>2,1);
for (a=sec->first,pc=0; a!=NULL&&pc<size; a=a->next) {
npc = fwpcalign(f,a,sec,pc);
if (genlinedebug && (a->type==DATA || a->type==SPACE))
add_linedebug(&linedblist,a->src,a->line,npc);
if (a->type == DATA)
fwdata(f,a->content.db->data,a->content.db->size);
else if (a->type == SPACE)
fwsblock(f,a->content.sb);
else if (a->type == LINE && !genlinedebug)
add_linedebug(&linedblist,NULL,a->content.srcline,npc);
process_relocs(a,&reloclist,NULL,sec,npc);
pc = npc + atom_size(a,sec,npc);
}
if (type == HUNK_CODE && (pc&1) == 0 && a == NULL)
fwnopalign(f,pc);
else
fwalign(f,pc,4);
}
else /* HUNK_BSS */
fw32(f,(get_sec_size(sec)+3)>>2,1);
if (!kick1)
reloc_hunk(f,HUNK_ABSRELOC32,1,&reloclist);
reloc_hunk(f,HUNK_ABSRELOC32,0,&reloclist);
if (!kick1) /* RELRELOC32 works with short 16-bit offsets only! */
reloc_hunk(f,HUNK_RELRELOC32,1,&reloclist);
report_bad_relocs(&reloclist);
if (!no_symbols) {
/* symbol table */
if (!hunk_onlyglobal)
ext_defs(f,LABSYM,0,sec->idx,EXT_SYMB);
/* line-debug */
linedebug_hunk(f,&linedblist);
}
fw32(f,HUNK_END,1);
}
}
}
else {
/* no sections: create single code hunk with size 0 */
fw32(f,1,1);
fw32(f,0,1);
fw32(f,0,1);
fw32(f,0,1);
fw32(f,HUNK_CODE,1);
fw32(f,0,1);
fw32(f,HUNK_END,1);
}
}
static void write_output(FILE *f,section *sec,symbol *sym)
{
if (exec_out)
write_exec(f,sec,sym);
else
write_object(f,sec,sym);
}