/
link.c
1281 lines (1105 loc) · 35.6 KB
/
link.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
#include <stdlib.h>
#include <string.h>
#include "link.h"
#include "logger.h"
#include "compiler.h"
/*
* Amiga Hunk file format related definitions
*
* see http://amiga-dev.wikidot.com/file-format:hunk
*/
#define HUNK_TYPE_UNIT 0x03E7
#define HUNK_TYPE_NAME 0x03E8
#define HUNK_TYPE_CODE 0x03E9
#define HUNK_TYPE_DATA 0x03EA
#define HUNK_TYPE_BSS 0x03EB
#define HUNK_TYPE_RELOC32 0x03EC
#define HUNK_TYPE_EXT 0x03EF
#define HUNK_TYPE_SYMBOL 0x03F0
#define HUNK_TYPE_DEBUG 0x03F1
#define HUNK_TYPE_END 0x03F2
#define HUNK_TYPE_HEADER 0x03F3
#define EXT_TYPE_DEF 1
#define EXT_TYPE_ABS 2
#define EXT_TYPE_REF32 129
#define EXT_TYPE_COMMON 130
#define EXT_TYPE_ABSREF16 138
#define MAX_BUF 1024
#define MAX_NUM_HUNKS 64
#define ENABLE_SYMBOL_HUNK
#define DEBUG_MAGIC 0x44425141 // AQBD - marks beginning of debug hunk
#define DEBUG_VERSION 1
#define DEBUG_INFO_LINE 1
#define DEBUG_INFO_END 0xFEDC
static uint8_t g_buf[MAX_BUF]; // scratch buffer
static char g_name[MAX_BUF]; // current hunk name
static AS_segment g_hunk_table[MAX_NUM_HUNKS]; // used during hunk object loading
static uint32_t g_hunk_sizes[MAX_NUM_HUNKS]; // used during load file loading
static int g_hunk_id_cnt;
static AS_segment g_hunk_cur; // last code/data/bss hunk read
static FILE *g_fObjFile=NULL; // object file being written
static FILE *g_fLoadFile=NULL; // load file being written
static void link_fail (string msg)
{
LOG_printf (LOG_ERROR, "*** linker error: %s\n", msg);
//assert(FALSE);
if (g_fLoadFile)
{
fclose (g_fLoadFile);
g_fLoadFile = NULL;
}
if (g_fObjFile)
{
fclose (g_fObjFile);
g_fObjFile = NULL;
}
CO_exit(127);
}
#if LOG_LEVEL == LOG_DEBUG
//static void hexdump (uint8_t *mem, uint32_t offset, uint32_t num_bytes)
//{
// LOG_printf (LOG_DEBUG, "HEX: 0x%08x ", offset);
// uint32_t cnt=0;
// uint32_t num_longs = num_bytes >> 2;
// while (cnt<num_longs)
// {
// uint32_t w = *( (uint32_t *) (mem+offset+cnt*4) );
// LOG_printf (LOG_DEBUG, " 0x%08x", ENDIAN_SWAP_32(w));
// cnt++;
// }
// LOG_printf (LOG_DEBUG, "\n");
//}
#endif
LI_segmentList LI_SegmentList(void)
{
LI_segmentList sl = U_poolAlloc (UP_link, sizeof(*sl));
sl->first = NULL;
sl->last = NULL;
return sl;
}
void LI_segmentListAppend (LI_segmentList sl, AS_segment seg)
{
LI_segmentListNode node = U_poolAlloc (UP_link, sizeof(*node));
node->seg = seg;
node->next = NULL;
if (sl->first)
sl->last = sl->last->next = node;
else
sl->first = sl->last = node;
}
static bool fread_u4(FILE *f, uint32_t *u)
{
if (fread (u, 4, 1, f) != 1)
return FALSE;
*u = ENDIAN_SWAP_32 (*u);
return TRUE;
}
static bool fread_u2(FILE *f, uint16_t *u)
{
if (fread (u, 2, 1, f) != 1)
return FALSE;
*u = ENDIAN_SWAP_16 (*u);
return TRUE;
}
static bool load_hunk_unit(FILE *f)
{
for (int i=0; i<MAX_NUM_HUNKS; i++)
g_hunk_table[i] = NULL;
g_hunk_id_cnt = 0;
g_hunk_cur = NULL;
uint32_t name_len;
if (!fread_u4 (f, &name_len))
{
LOG_printf (LOG_ERROR, "link: read error #1.\n");
return FALSE;
}
name_len *=4;
if (name_len>=MAX_BUF)
{
LOG_printf (LOG_ERROR, "link: unit name too long.\n");
return FALSE;
}
if (fread (g_buf, name_len, 1, f) != 1)
{
LOG_printf (LOG_ERROR, "link: read error #2.\n");
return FALSE;
}
g_buf[name_len] = 0;
LOG_printf (LOG_DEBUG, "link: unit name: %s\n", g_buf);
return TRUE;
}
static bool load_hunk_name(string sourcefn, FILE *f)
{
uint32_t name_len;
if (!fread_u4 (f, &name_len))
{
LOG_printf (LOG_ERROR, "link: read error #3.\n");
return FALSE;
}
name_len *=4;
if (name_len>=MAX_BUF)
{
LOG_printf (LOG_ERROR, "link: hunk name too long.\n");
return FALSE;
}
if (fread (g_name, name_len, 1, f) != 1)
{
LOG_printf (LOG_ERROR, "link: read error #4.\n");
return FALSE;
}
g_name[name_len] = 0;
LOG_printf (LOG_DEBUG, "link: %s: hunk name: %s\n", sourcefn, g_name);
return TRUE;
}
static AS_segment getOrCreateSegment (U_poolId pid, string sourcefn, string name, int id, AS_segKind kind, size_t min_size)
{
if (id >= MAX_NUM_HUNKS)
{
LOG_printf (LOG_ERROR, "link: hunk table overflow\n");
return FALSE;
}
AS_segment seg = g_hunk_table[id];
if (!seg)
{
seg = AS_Segment(pid, sourcefn, name, kind, min_size);
g_hunk_table[id] = seg;
return seg;
}
if (kind != AS_unknownSeg)
{
if (seg->kind == AS_unknownSeg)
seg->kind = kind;
if (kind != seg->kind)
assert(FALSE);
}
if (!seg->name && name)
seg->name = name;
AS_ensureSegmentSize (pid, seg, min_size);
return seg;
}
static bool load_hunk_code(U_poolId pid, string sourcefn, FILE *f)
{
uint32_t code_len;
if (!fread_u4 (f, &code_len))
{
LOG_printf (LOG_ERROR, "link: read error #5.\n");
return FALSE;
}
code_len *=4;
LOG_printf (LOG_DEBUG, "link: %s: code hunk size: %d bytes.\n", sourcefn, code_len);
g_hunk_cur = getOrCreateSegment (pid, sourcefn, String(UP_link, g_name), g_hunk_id_cnt++, AS_codeSeg, code_len);
strcpy (g_name, "unnamed");
if (code_len>0)
{
if (fread (g_hunk_cur->mem, code_len, 1, f) != 1)
{
LOG_printf (LOG_ERROR, "link: read error #6.\n");
return FALSE;
}
}
g_hunk_cur->mem_pos = code_len;
return TRUE;
}
static bool load_hunk_data(U_poolId pid, string sourcefn, FILE *f)
{
uint32_t data_len;
if (!fread_u4 (f, &data_len))
{
LOG_printf (LOG_ERROR, "link: read error #7.\n");
return FALSE;
}
data_len *=4;
LOG_printf (LOG_DEBUG, "link: %s: data hunk size: %d bytes.\n", sourcefn, data_len);
g_hunk_cur = getOrCreateSegment (pid, sourcefn, String(UP_link, g_name), g_hunk_id_cnt++, AS_dataSeg, data_len);
strcpy (g_name, "unnamed");
if (fread (g_hunk_cur->mem, data_len, 1, f) != 1)
{
LOG_printf (LOG_ERROR, "link: read error #8.\n");
return FALSE;
}
//hexdump (g_hunk_cur->mem, 0, data_len);
g_hunk_cur->mem_pos = data_len;
return TRUE;
}
static bool load_hunk_bss(U_poolId pid, string sourcefn, FILE *f)
{
uint32_t bss_len;
if (!fread_u4 (f, &bss_len))
{
LOG_printf (LOG_ERROR, "link: read error #9.\n");
return FALSE;
}
bss_len *=4;
LOG_printf (LOG_DEBUG, "link: %s: bss hunk size: %d bytes (hdr: %d bytes).\n", sourcefn, bss_len, g_hunk_sizes[g_hunk_id_cnt]*4);
g_hunk_cur = getOrCreateSegment (pid, sourcefn, String(UP_link, g_name), g_hunk_id_cnt, AS_bssSeg, g_hunk_sizes[g_hunk_id_cnt]*4);
g_hunk_id_cnt++;
strcpy (g_name, "unnamed");
g_hunk_cur->mem_size = bss_len;
g_hunk_cur->mem_pos = bss_len;
return TRUE;
}
static bool load_hunk_reloc32(U_poolId pid, string sourcefn, FILE *f)
{
uint32_t num_offs, hunk_id;
while (TRUE)
{
if (!fread_u4 (f, &num_offs))
{
LOG_printf (LOG_ERROR, "link: read error #10.\n");
return FALSE;
}
if (!num_offs)
return TRUE; // finished
if (!fread_u4 (f, &hunk_id))
{
LOG_printf (LOG_ERROR, "link: read error #11.\n");
return FALSE;
}
LOG_printf (LOG_DEBUG, "link: %s: reloc32: %d offsets in hunk #%d.\n", sourcefn, num_offs, hunk_id);
AS_segment seg = getOrCreateSegment (pid, sourcefn, NULL, hunk_id, AS_unknownSeg, /*min_size=*/0);
for (uint32_t i=0; i<num_offs; i++)
{
uint32_t off;
if (!fread_u4 (f, &off))
{
LOG_printf (LOG_ERROR, "link: read error #12.\n");
return FALSE;
}
AS_segmentAddReloc32 (pid, g_hunk_cur, seg, off);
}
}
return TRUE;
}
static bool load_hunk_ext(U_poolId pid, string sourcefn, FILE *f)
{
if (!g_hunk_cur)
{
LOG_printf (LOG_ERROR, "link: ext hunk detected when no segment is defined yet.\n");
assert(FALSE);
return FALSE;
}
while (TRUE)
{
uint32_t c;
if (!fread_u4 (f, &c))
{
LOG_printf (LOG_ERROR, "link: read error #13.\n");
return FALSE;
}
if (!c)
return TRUE;
uint32_t ext_type = c >>24;
uint32_t name_len = c & 0x00FFFFFF;
name_len *=4;
if (name_len>=MAX_BUF)
{
LOG_printf (LOG_ERROR, "link: hunk name too long.\n");
return FALSE;
}
if (fread (g_buf, name_len, 1, f) != 1)
{
LOG_printf (LOG_ERROR, "link: read error #14.\n");
return FALSE;
}
g_buf[name_len] = 0;
LOG_printf (LOG_DEBUG, "link: %s: hunk_ext: ext_type=%d, name_len=%d, name=%s\n", sourcefn, ext_type, name_len, g_buf);
S_symbol sym = S_Symbol ((string) g_buf, /*case_sensitive=*/FALSE);
switch (ext_type)
{
case EXT_TYPE_REF32:
{
uint32_t num_refs;
if (!fread_u4 (f, &num_refs))
{
LOG_printf (LOG_ERROR, "link: read error #15.\n");
return FALSE;
}
for (uint32_t i=0; i<num_refs; i++)
{
uint32_t offset;
if (!fread_u4 (f, &offset))
{
LOG_printf (LOG_ERROR, "link: read error #16.\n");
return FALSE;
}
AS_segmentAddRef (pid, g_hunk_cur, sym, offset, Temp_w_L, /*common_size=*/0);
}
break;
}
case EXT_TYPE_COMMON:
{
uint32_t common_size;
if (!fread_u4 (f, &common_size))
{
LOG_printf (LOG_ERROR, "link: read error #17.\n");
return FALSE;
}
uint32_t num_refs;
if (!fread_u4 (f, &num_refs))
{
LOG_printf (LOG_ERROR, "link: read error #18.\n");
return FALSE;
}
for (uint32_t i=0; i<num_refs; i++)
{
uint32_t offset;
if (!fread_u4 (f, &offset))
{
LOG_printf (LOG_ERROR, "link: read error #19.\n");
return FALSE;
}
AS_segmentAddRef (pid, g_hunk_cur, sym, offset, Temp_w_L, /*common_size=*/common_size);
}
break;
}
case EXT_TYPE_DEF:
{
uint32_t offset;
if (!fread_u4 (f, &offset))
{
LOG_printf (LOG_ERROR, "link: read error #20.\n");
return FALSE;
}
AS_segmentAddDef (pid, g_hunk_cur, sym, offset);
LOG_printf (LOG_DEBUG, "link: %s: -> ext_def, offset=0x%08x\n", sourcefn, offset);
break;
}
case EXT_TYPE_ABS:
{
uint32_t v;
if (!fread_u4 (f, &v))
{
LOG_printf (LOG_ERROR, "link: read error #21.\n");
return FALSE;
}
// FIXME AS_segmentAddDef (g_hunk_cur, sym, v);
LOG_printf (LOG_DEBUG, "link: %s: -> ext_abs, v=0x%08x\n", sourcefn, v);
break;
}
case EXT_TYPE_ABSREF16:
{
uint32_t num_refs;
if (!fread_u4 (f, &num_refs))
{
LOG_printf (LOG_ERROR, "link: read error #22.\n");
return FALSE;
}
for (uint32_t i=0; i<num_refs; i++)
{
uint32_t v;
if (!fread_u4 (f, &v))
{
LOG_printf (LOG_ERROR, "link: read error #23.\n");
return FALSE;
}
// FIXME AS_segmentAddRef (g_hunk_cur, sym, v, Temp_w_L, /*common_size=*/0);
LOG_printf (LOG_DEBUG, "link: %s: -> ext_absref16, v=0x%08x\n", sourcefn, v);
}
break;
}
default:
LOG_printf (LOG_ERROR, "link: FIXME: ext type %d not implemented yet.\n", ext_type);
assert(FALSE);
}
}
return TRUE;
}
static bool load_hunk_symbol(string sourcefn, FILE *f)
{
if (!g_hunk_cur)
{
LOG_printf (LOG_ERROR, "link: symbol hunk detected when so segment is defined yet.\n");
assert(FALSE);
return FALSE;
}
while (TRUE)
{
uint32_t num_longs;
if (!fread_u4 (f, &num_longs))
{
LOG_printf (LOG_ERROR, "link: read error #32.\n");
return FALSE;
}
if (!num_longs)
return TRUE;
uint32_t name_len = num_longs * 4;
if (name_len>=MAX_BUF)
{
LOG_printf (LOG_ERROR, "link: symbol name too long.\n");
return FALSE;
}
if (fread (g_buf, name_len, 1, f) != 1)
{
LOG_printf (LOG_ERROR, "link: read error #33.\n");
return FALSE;
}
g_buf[name_len] = 0;
uint32_t offset;
if (!fread_u4 (f, &offset))
{
LOG_printf (LOG_ERROR, "link: read error #34.\n");
return FALSE;
}
LOG_printf (LOG_DEBUG, "link: hunk_symbol: name=%s(len=%d) offset=0x%08lx\n", g_buf, name_len, offset);
}
return TRUE;
}
static bool load_hunk_debug(U_poolId pid, string sourcefn, FILE *f)
{
if (!g_hunk_cur)
{
LOG_printf (LOG_ERROR, "link: debug hunk detected when so segment is defined yet.\n");
assert(FALSE);
return FALSE;
}
uint32_t num_longs;
if (!fread_u4 (f, &num_longs))
{
LOG_printf (LOG_ERROR, "link: read error #35.\n");
return FALSE;
}
if (!num_longs)
return TRUE;
if (num_longs < 2)
{
LOG_printf (LOG_ERROR, "link: debug hunk too short.\n");
return FALSE;
}
uint32_t magic, version;
if (!fread_u4 (f, &magic))
{
LOG_printf (LOG_ERROR, "link: read error #37.\n");
return FALSE;
}
if (!fread_u4 (f, &version))
{
LOG_printf (LOG_ERROR, "link: read error #38.\n");
return FALSE;
}
if ((magic != DEBUG_MAGIC) || (version != DEBUG_VERSION))
{
LOG_printf (LOG_ERROR, "link: debug hunk format not recognized.\n");
return FALSE;
}
num_longs -= 2;
while (num_longs>0)
{
uint16_t cmd;
if (!fread_u2 (f, &cmd))
{
LOG_printf (LOG_ERROR, "link: read error #36.\n");
return FALSE;
}
switch (cmd)
{
case DEBUG_INFO_LINE:
{
uint16_t line;
uint32_t offset;
if (!fread_u2 (f, &line))
{
LOG_printf (LOG_ERROR, "link: read error #37.\n");
return FALSE;
}
if (!fread_u4 (f, &offset))
{
LOG_printf (LOG_ERROR, "link: read error #38.\n");
return FALSE;
}
LOG_printf (LOG_DEBUG, "link: hunk_debug: INFO_LINE line=%4d offset=0x%08lx\n", line, offset);
AS_segmentAddSrcMap (pid, g_hunk_cur, line, offset);
num_longs -= 2;
break;
}
case DEBUG_INFO_END:
num_longs = 0;
break;
default:
LOG_printf (LOG_ERROR, "link: debug hunk: unknown entry 0x%04x\n", cmd);
return FALSE;
}
}
return TRUE;
}
bool LI_segmentListReadObjectFile (U_poolId pid, LI_segmentList sl, string sourcefn, FILE *f)
{
uint32_t ht;
if (!fread_u4 (f, &ht))
{
LOG_printf (LOG_ERROR, "link: read error #24.\n");
return FALSE;
}
LOG_printf (LOG_DEBUG, "link: %s: hunk type: %08x\n", sourcefn, ht);
if (ht != HUNK_TYPE_UNIT)
{
LOG_printf (LOG_ERROR, "link: %s: this is not an object file, header mismatch: found 0x%08x, expected %08x\n", sourcefn, ht, HUNK_TYPE_UNIT);
return FALSE;
}
for (uint32_t i = 0; i<MAX_NUM_HUNKS; i++)
g_hunk_sizes[i] = 0;
if (!load_hunk_unit(f))
return FALSE;
strcpy (g_name, "unnamed");
while (TRUE)
{
if (!fread_u4 (f, &ht))
break;
LOG_printf (LOG_DEBUG, "link: %s: hunk type: %08x\n", sourcefn, ht);
switch (ht)
{
case HUNK_TYPE_UNIT:
if (!load_hunk_unit(f))
return FALSE;
break;
case HUNK_TYPE_NAME:
if (!load_hunk_name(sourcefn, f))
return FALSE;
break;
case HUNK_TYPE_CODE:
if (!load_hunk_code(pid, sourcefn, f))
return FALSE;
break;
case HUNK_TYPE_DATA:
if (!load_hunk_data(pid, sourcefn, f))
return FALSE;
break;
case HUNK_TYPE_BSS:
if (!load_hunk_bss(pid, sourcefn, f))
return FALSE;
break;
case HUNK_TYPE_RELOC32:
if (!load_hunk_reloc32(pid, sourcefn, f))
return FALSE;
break;
case HUNK_TYPE_EXT:
if (!load_hunk_ext(pid, sourcefn, f))
return FALSE;
break;
case HUNK_TYPE_END:
if (!g_hunk_cur)
{
LOG_printf (LOG_ERROR, "link: hunk_end detected when no hunk was defined.\n");
return FALSE;
}
LI_segmentListAppend (sl, g_hunk_cur);
g_hunk_cur = NULL;
break;
default:
LOG_printf (LOG_ERROR, "link: unknown hunk type 0x%08x.\n", ht);
assert(FALSE);
return FALSE;
}
}
return TRUE;
}
typedef struct symInfo_ *symInfo;
struct symInfo_
{
AS_segment seg;
uint32_t offset;
};
bool LI_link (U_poolId pid, LI_segmentList sl)
{
TAB_table symTable = TAB_empty(UP_link); // S_symbol -> symInfo
// pass 1: collect all symbol definitions from all segments,
// assign unique hunk_ids
uint32_t hunk_id = 0;
for (LI_segmentListNode node = sl->first; node; node=node->next)
{
for (AS_segmentDef def = node->seg->defs; def; def=def->next)
{
LOG_printf (LOG_DEBUG, "link: pass1: found definition for symbol %-20s (%p): offset=0x%08x at hunk #%02d (%s)\n", S_name (def->sym), def->sym, def->offset, node->seg->hunk_id, node->seg->sourcefn);
symInfo si = (symInfo) TAB_look (symTable, def->sym);
if (si)
{
char msg[256];
snprintf(msg, 256, "symbol %s defined more than once!", S_name(def->sym));
link_fail (msg);
}
si = U_poolAlloc (UP_link, sizeof(*si));
si->seg = node->seg;
si->offset = def->offset;
TAB_enter (symTable, def->sym, si);
}
node->seg->hunk_id = hunk_id++;
}
// pass 2: resolve external references
AS_segment commonSeg = NULL;
for (LI_segmentListNode node = sl->first; node; node=node->next)
{
if (!node->seg->refs)
continue;
TAB_iter i = TAB_Iter (node->seg->refs);
S_symbol sym;
AS_segmentRef sr;
while (TAB_next (i, (void **)&sym, (void**) &sr))
{
symInfo si = TAB_look (symTable, sym);
if (!si)
{
if (!sr->common_size)
{
LOG_printf (LOG_ERROR, "link: *** ERROR: unresolved symbol %s (%p)\n\n", S_name(sym), sym);
return FALSE;
}
if (!commonSeg)
{
commonSeg = AS_Segment (pid, "common", "common", AS_dataSeg, 0);
commonSeg->hunk_id = hunk_id++;
LI_segmentListAppend (sl, commonSeg);
}
si = U_poolAlloc (UP_link, sizeof(*si));
si->seg = commonSeg;
si->offset = commonSeg->mem_pos;
LOG_printf (LOG_DEBUG, "link: symbol %s allocated in common segment at offset 0x%08x, common_size=%zd\n", S_name(sym), si->offset, sr->common_size);
TAB_enter (symTable, sym, si);
AS_assembleDataFill (commonSeg, sr->common_size);
}
assert (sr->w == Temp_w_L); // FIXME
while (sr)
{
LOG_printf (LOG_DEBUG, "link: pass2: adding reloc32 for symbol %-20s in hunk #%02d at offset 0x%08x -> hunk #%02d, offset 0x%08x\n",
S_name (sym), node->seg->hunk_id, sr->offset, si->seg->hunk_id, si->offset);
uint32_t *p = (uint32_t *) (node->seg->mem+sr->offset);
*p = ENDIAN_SWAP_32(si->offset);
AS_segmentAddReloc32 (pid, node->seg, si->seg, sr->offset);
#if LOG_LEVEL == LOG_DEBUG
//hexdump (node->seg->mem, sr->offset-4, 16);
#endif
sr = sr->next;
}
}
}
return TRUE;
}
static void fwrite_u2(FILE *f, uint16_t u)
{
u = ENDIAN_SWAP_16 (u);
if (fwrite (&u, 2, 1, f) != 1)
link_fail ("write error");
}
static void fwrite_u4(FILE *f, uint32_t u)
{
u = ENDIAN_SWAP_32 (u);
if (fwrite (&u, 4, 1, f) != 1)
link_fail ("write error");
}
static void write_hunk_unit (string name, FILE *f)
{
fwrite_u4 (f, HUNK_TYPE_UNIT);
uint32_t l = strlen(name);
uint32_t n = roundUp(l, 4) / 4;
LOG_printf (LOG_DEBUG, "link: write hunk unit %s, n=%d\n", name, n);
fwrite_u4 (f, n);
if (fwrite (name, n*4, 1, f) != 1)
link_fail ("write error");
}
static void write_hunk_header (LI_segmentList sl, FILE *f)
{
fwrite_u4 (f, HUNK_TYPE_HEADER);
fwrite_u4 (f, 0); // no hunk names
uint32_t hunkCnt = 0;
for (LI_segmentListNode n = sl->first; n; n=n->next)
hunkCnt++;
fwrite_u4 (f, hunkCnt);
fwrite_u4 (f, 0);
fwrite_u4 (f, hunkCnt-1);
for (LI_segmentListNode n = sl->first; n; n=n->next)
{
uint32_t nw = roundUp(n->seg->mem_pos, 4) / 4;
fwrite_u4 (f, nw);
}
}
static void write_hunk_name (AS_segment seg, FILE *f)
{
fwrite_u4 (f, HUNK_TYPE_NAME);
uint32_t l = strlen(seg->name);
uint32_t n = roundUp(l, 4) / 4;
LOG_printf (LOG_DEBUG, "link: write hunk name %s, n=%d\n", seg->name, n);
fwrite_u4 (f, n);
if (fwrite (seg->name, n*4, 1, f) != 1)
link_fail ("write error");
}
static void write_hunk_code (AS_segment seg, FILE *f)
{
fwrite_u4 (f, HUNK_TYPE_CODE);
uint32_t n = roundUp(seg->mem_pos, 4) / 4;
LOG_printf (LOG_DEBUG, "link: code section, size=%zd bytes\n", seg->mem_pos);
fwrite_u4 (f, n);
if (n>0)
{
if (fwrite (seg->mem, n*4, 1, f) != 1)
link_fail ("write error");
}
}
static void write_hunk_data (AS_segment seg, FILE *f)
{
fwrite_u4 (f, HUNK_TYPE_DATA);
uint32_t n = roundUp(seg->mem_pos, 4) / 4;
LOG_printf (LOG_DEBUG, "link: data section, size=%zd bytes\n", seg->mem_pos);
fwrite_u4 (f, n);
if (n>0)
{
if (fwrite (seg->mem, n*4, 1, f) != 1)
link_fail ("write error");
}
}
static void write_hunk_bss (AS_segment seg, FILE *f)
{
fwrite_u4 (f, HUNK_TYPE_BSS);
uint32_t n = roundUp(seg->mem_pos, 4) / 4;
fwrite_u4 (f, n);
}
static void write_hunk_reloc32 (AS_segment seg, FILE *f)
{
if (!seg->relocs)
return;
fwrite_u4 (f, HUNK_TYPE_RELOC32);
TAB_iter segIter = TAB_Iter (seg->relocs);
AS_segment seg_to;
AS_segmentReloc32 relocs;
while (TAB_next (segIter, (void **)&seg_to, (void **)&relocs))
{
uint32_t cnt = 0;
for (AS_segmentReloc32 r = relocs; r; r=r->next)
cnt++;
fwrite_u4 (f, cnt);
fwrite_u4 (f, seg_to->hunk_id);
uint32_t i = 0;
for (AS_segmentReloc32 r = relocs; r; r=r->next)
{
fwrite_u4 (f, r->offset);
i++;
}
}
fwrite_u4 (f, 0); // end marker
}
#ifdef ENABLE_SYMBOL_HUNK
static void write_hunk_symbol (AS_segment seg, FILE *f)
{
if (!seg->defs)
return;
fwrite_u4 (f, HUNK_TYPE_SYMBOL);
for (AS_segmentDef def = seg->defs; def; def=def->next)
{
string name = S_name (def->sym);
uint32_t l = strlen(name);
uint32_t n = roundUp(l,4)/4;
fwrite_u4 (f, n);
if (fwrite (name, n*4, 1, f) != 1)
link_fail ("write error");
fwrite_u4 (f, def->offset);
}
fwrite_u4 (f, 0);
}
#endif
static void write_hunk_ext (AS_segment seg, FILE *f)
{
if (!seg->defs && !seg->refs)
return;
fwrite_u4 (f, HUNK_TYPE_EXT);
if (seg->refs)
{
TAB_iter i = TAB_Iter(seg->refs);
S_symbol sym;
AS_segmentRef ref;
while (TAB_next(i, (void **) &sym, (void **)&ref))
{
string name = S_name (sym);
uint32_t l = strlen(name);
uint32_t n = roundUp(l,4)/4;
uint32_t c = (EXT_TYPE_REF32<<24) | n;
fwrite_u4 (f, c);
if (n>0)
{
if (fwrite (name, n*4, 1, f) != 1)
link_fail ("write error");
}
uint32_t cnt=0;
for (AS_segmentRef r=ref; r; r=r->next)
cnt++;
fwrite_u4 (f, cnt);
for (AS_segmentRef r=ref; r; r=r->next)
fwrite_u4 (f, r->offset);
}
}
for (AS_segmentDef def = seg->defs; def; def=def->next)
{
string name = S_name (def->sym);
uint32_t l = strlen(name);
uint32_t n = roundUp(l,4)/4;
uint32_t c = (EXT_TYPE_DEF<<24) | n;
fwrite_u4 (f, c);
if (n>0)
{
if (fwrite (name, n*4, 1, f) != 1)
link_fail ("write error");
}
fwrite_u4 (f, def->offset);
}
fwrite_u4 (f, 0);
}
static void write_hunk_debug (AS_segment seg, FILE *f)
{
if (!seg->srcMap)
return;
int numSrcMappings = 0;
for (AS_srcMapNode n = seg->srcMap; n; n=n->next)
numSrcMappings++;
fwrite_u4 (f, HUNK_TYPE_DEBUG);
uint32_t hunk_size = 2 + numSrcMappings*2;
LOG_printf (LOG_DEBUG, "link: debug section, size=%zd bytes\n", hunk_size*4);
fwrite_u4 (f, hunk_size);
fwrite_u4 (f, DEBUG_MAGIC);
fwrite_u4 (f, DEBUG_VERSION);