-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathomfobjectfile.d
889 lines (877 loc) · 39.1 KB
/
omfobjectfile.d
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
import std.algorithm;
import std.exception;
import std.conv;
import std.path;
import std.stdio;
import datafile;
import modules;
import omfdef;
import objectfile;
import section;
import sectiontable;
import segment;
import symbol;
import symboltable;
import workqueue;
public:
final class OmfObjectFile : ObjectFile
{
private:
DataFile f;
immutable(ubyte)[] sourcefile;
immutable(ubyte)[][] names;
Section[] sections;
OmfGroup[] groups;
immutable(ubyte)[][] externs;
SymbolTable symtab;
public:
this(DataFile f)
{
super(f.filename);
this.f = f;
}
override void dump()
{
//writeln("OMF Object file: ", f.filename);
f.seek(0);
while (!f.empty)
{
auto r = loadRecord();
r.dump();
}
}
override void loadSymbols(SymbolTable xsymtab, SectionTable sectab, WorkQueue!string queue, WorkQueue!ObjectFile objects)
{
debug(OMFDATA) writeln("OMF Object file: ", f.filename);
symtab = new SymbolTable(xsymtab);
objects.append(this);
f.seek(0);
enforce(f.peekByte() == 0x80, "First record must be THEADR");
auto modend = false;
while (!f.empty() && !modend)
{
auto r = loadRecord();
switch(r.type)
{
case OmfRecordType.THEADR:
auto len = r.data[0];
enforce(len == r.data.length - 1, "Corrupt THEADR record");
sourcefile = r.data[1..$];
debug(OMFDATA) writeln("Module: ", cast(string)r.data[1..$]);
break;
case OmfRecordType.LLNAMES:
case OmfRecordType.LNAMES:
auto data = r.data;
enforce(data.length != 0, "Empty LNAMES record");
while (data.length != 0)
{
auto len = data[0];
enforce(len + 1 <= data.length, "Name length too long");
names ~= data[1..1+len];
data = data[1+len..$];
}
break;
case OmfRecordType.COMENT:
enforce(r.data.length >= 2, "Corrupt COMENT record");
auto data = r.data;
auto ctype = getByte(data);
auto cclass = getByte(data);
switch(cclass)
{
case 0x9D:
checkMemoryModel(getBytes(data, 2));
break;
case 0x9E:
// DOSSEG
break;
case 0x9F:
queue.append(defaultExtension(cast(string)data, "lib"));
break;
case 0xA0:
auto subtype = getByte(data);
switch(subtype)
{
case 0x01: // IMPDEF
auto isOrdinal = getByte(data);
auto intname = getString(data);
auto modname = getString(data);
ushort entryOrd;
immutable(ubyte)[] expname;
if (isOrdinal)
entryOrd = getWordLE(data);
else
{
expname = getString(data);
if (expname.length == 0)
expname = intname;
}
enforce(data.length == 0, "Corrupt IMPDEF record");
//writeln("IMPDEF int:", cast(string)intname, " mod:", cast(string)modname, " ent:", isOrdinal ? to!string(entryOrd) : cast(string)expname);
auto imp = new Import(modname, entryOrd, expname);
imp.thunk = new ImportThunkSymbol(intname, imp);
imp.address = new ImportAddressSymbol(cast(immutable(ubyte)[])"__imp_" ~ intname, imp);
symtab.add(imp);
symtab.add(imp.thunk);
symtab.add(imp.address);
break;
case 0x02: // EXPDEF
enforce(false, "COMENT EXPDEF not supported");
break;
default:
enforce(false, "COMENT A0 subtype " ~ to!string(subtype, 16) ~ " not supported");
break;
}
break;
case 0xA1:
enforce(data.length == 3 &&
getByte(data) == 0x01 &&
getByte(data) == 'C' &&
getByte(data) == 'V',
"Only codeview debugging information is supported");
break;
case 0xA2:
modend = true;
break;
default:
enforce(false, "COMENT type " ~ to!string(cclass, 16) ~ " not supported");
}
break;
case OmfRecordType.SEGDEF16:
case OmfRecordType.SEGDEF32:
auto data = r.data;
enforce(data.length >= 5 || data.length <= 14, "Corrupt SEGDEF record");
auto A = data[0] >> 5;
auto C = (data[0] >> 2) & 7;
auto B = (data[0] & 2) != 0;
auto P = (data[0] & 1) != 0;
SectionAlign secalign;
switch(A)
{
case 0: //alignment = SectionAlignment.absolute; break;
enforce(false, "Absolute sections are not supported");
break;
case 1: secalign = SectionAlign.align_1; break;
case 2: secalign = SectionAlign.align_2; break;
case 3: secalign = SectionAlign.align_16; break;
case 4: secalign = SectionAlign.align_page; break;
case 5: secalign = SectionAlign.align_4; break;
default:
enforce(false, "Invalid alignment value");
break;
}
switch(C)
{
case 5:
secalign = SectionAlign.align_1;
break;
case 2, 4, 7, 0:
break;
default:
enforce(false, "Section combination not supported: " ~ to!string(C));
break;
}
enforce(!B, "Big sections are not supported");
enforce(P, "Use16 sections are not supported");
data = data[1..$];
uint length;
if (r.type == OmfRecordType.SEGDEF16)
{
enforce(data.length >= 2, "Corrupt SEGDEF record");
length = getWordLE(data);
} else {
enforce(data.length >= 2, "Corrupt SEGDEF record");
length = getDwordLE(data);
}
auto name = getIndex(data);
enforce(name <= names.length, "Invalid section name index");
auto cname = getIndex(data);
enforce(cname <= names.length, "Invalid class name index");
auto secclass = getSectionClass(names[name-1], names[cname-1]);
auto overlayName = getIndex(data); // Discard
enforce(overlayName <= names.length, "Invalid overlay name index");
enforce(data.length == 0, "Corrupt SEGDEF record");
auto sec = new Section(names[name-1], secclass, secalign, length);
sections ~= sec;
sectab.add(sec);
debug(OMFDATA) writeln("SEGDEF (", sections.length, ") name:", cast(string)names[name-1], " class:", cast(string)names[cname-1], " length:", length, " align:", secalign);
break;
case OmfRecordType.GRPDEF:
OmfGroup group;
auto data = r.data;
group.name = getIndex(data);
enforce(group.name <= names.length, "Invalid group name index");
while (data.length)
{
auto type = getByte(data);
enforce(type == 0xFF, "Only type FFH group components are supported");
auto index = getIndex(data);
enforce(index <= sections.length, "Invalid group section index");
group.segs ~= index;
}
//enforce(cast(string)names[group.name-1] == "FLAT", "Only the FLAT group is supported, not " ~ cast(string)names[group.name-1]);
groups ~= group;
//writeln("GRPDEF name:", cast(string)names[group.name-1], " components:", group.segs);
enforce(data.length == 0, "Corrupt GRPDEF record");
break;
case OmfRecordType.PUBDEF16:
case OmfRecordType.PUBDEF32:
auto off16 = (r.type == OmfRecordType.PUBDEF16);
auto data = r.data;
auto baseGroup = getIndex(data);
enforce(baseGroup <= groups.length, "Invalid base group index");
auto baseSec = getIndex(data);
enforce(baseSec <= sections.length, "Invalid base section index");
if (baseSec == 0)
auto baseFrame = getWordLE(data);
while (data.length)
{
auto group = baseGroup;
auto sec = baseSec;
auto length = getByte(data);
auto name = getBytes(data, length);
auto offset = off16 ? getWordLE(data) : getDwordLE(data);
auto type = getIndex(data);
if (sec)
{
symtab.add(new PublicSymbol(sections[sec-1], name, offset));
} else {
symtab.add(new AbsoluteSymbol(name, offset));
}
//writeln("PUBDEF name:", cast(string)name, " ", sec ? cast(string)sections[sec-1].name : "__abs__", "+", offset);
}
enforce(data.length == 0, "Corrupt PUBDEF record");
break;
case OmfRecordType.COMDAT16:
case OmfRecordType.COMDAT32:
auto off16 = (r.type == OmfRecordType.COMDAT16);
auto data = r.data;
auto flags = getByte(data);
auto isIterated = (flags & 0x2) != 0;
auto isLocal = (flags & 0x4) != 0;
enforce(flags < 8, "COMDAT flag not supported: " ~ to!string(flags));
auto attributes = getByte(data);
Comdat comdat;
switch(attributes & 0xF0)
{
case 0x00: comdat = Comdat.Unique; break;
case 0x10: comdat = Comdat.Any; break;
default:
enforce(false, "COMDAT attribute not supported");
break;
}
auto alignment = getByte(data);
auto offset = off16 ? getWordLE(data) : getDwordLE(data);
auto type = getIndex(data);
ushort baseGroup;
ushort baseSec;
if ((attributes & 0x0F) == 0x00)
{
baseGroup = getIndex(data);
enforce(baseGroup <= groups.length, "Invalid base group index");
baseSec = getIndex(data);
enforce(baseSec <= sections.length, "Invalid base section index");
if (baseSec == 0)
auto baseFrame = getWordLE(data);
} else
assert(0);
auto name = getIndex(data);
auto sec = sections[baseSec-1];
SectionAlign secalign;
switch(alignment)
{
case 0: secalign = sec.secalign; break;
case 1: secalign = SectionAlign.align_1; break;
case 2: secalign = SectionAlign.align_2; break;
case 3: secalign = SectionAlign.align_16; break;
case 4: secalign = SectionAlign.align_page; break;
case 5: secalign = SectionAlign.align_4; break;
default:
enforce(false, "Invalid alignment value");
break;
}
//auto length = data.length;
//auto xsec = new Section(sec.name ~ '$' ~ names[name-1], sec.secclass, secalign, length);
//auto csec = sectab.add(xsec);
if (flags & 1)
{
//writeln("Continue comdat ", cast(string)(sec.name ~ '$' ~ names[name-1]), " length:", data.length);
/*foreach(v; data)
writef("%.2X ", v);
writeln();*/
// Modify previous section
auto sym = symtab.searchName(names[name-1]);
assert(sym);
assert(cast(ComdatSymbol)sym);
auto csec = (cast(ComdatSymbol)sym).sec;
enforce(!isIterated);
//writeln(offset, "..", offset + data.length, " ", csec.length);
//enforce(offset == csec.length, "Overlapping or gapped comdats are not supported");
auto p = csec.container;
//enforce(offset == p.length, "Overlapping or gapped comdats are not supported");
enforce(p.members.length == 1);
// writefln("prev length: %s offset: %s", csec.exactlength, offset);
// enforce(offset == csec.exactlength);
// enforce(p.length == csec.length);
// writefln("extend %s to %s", csec.exactlength, offset + data.length);
// Relocations that would touch two continued COMDAT data symbols have their own COMDAT symbol,
// so the comdat range may be inside the rest of the data
csec.exactlength = max(csec.exactlength, offset + data.length);
csec.alignedlength = csec.exactlength.alignTo(csec.secalign);
p.length = csec.alignedlength.alignTo(p.secalign);
}
else
{
enforce(offset == 0);
auto csec = new Section(sec.name ~ '$' ~ names[name-1], sec.secclass, sec.secalign, data.length);
sectab.add(csec);
auto sym = new ComdatSymbol(csec, names[name-1], offset, comdat, isLocal);
symtab.add(sym);
//writeln("Start comdat ", cast(string)(sec.name ~ '$' ~ names[name-1]), " length:", data.length);
/*foreach(v; data)
writef("%.2X ", v);
writeln();*/
}
//writeln(flags, ' ', attributes, ' ', comdat);
//writeln("COMDAT name:", cast(string)names[name-1], " ", cast(string)sections[baseSec-1].name, isLocal ? " local" : "");
break;
case OmfRecordType.EXTDEF:
auto data = r.data;
while (data.length)
{
auto length = getByte(data);
auto name = getBytes(data, length);
auto type = getIndex(data);
if (name.startsWith("__imp_"))
{
symtab.add(new ExternSymbol(name[6..$]));
//writeln("EXTDEF name:", cast(string)name[6..$]);
}
auto sym = new ExternSymbol(name);
symtab.add(sym);
externs ~= name;
//writeln("EXTDEF name:", cast(string)name);
}
enforce(data.length == 0, "Corrupt EXTDEF record");
break;
case OmfRecordType.CEXTDEF:
auto data = r.data;
while (data.length)
{
auto name = getIndex(data);
enforce(name <= names.length, "Invalid symbol name index");
auto type = getIndex(data);
auto sym = new ExternSymbol(names[name-1]);
symtab.add(sym);
externs ~= names[name-1];
//writeln("CEXTDEF name:", cast(string)names[name-1]);
}
enforce(data.length == 0, "Corrupt CEXTDEF record");
break;
case OmfRecordType.COMDEF:
auto data = r.data;
while (data.length)
{
auto name = getString(data);
auto type = getIndex(data);
auto dt = getByte(data);
enforce(dt == 0x61 || dt == 0x62);
auto length = getComLength(data);
if (dt == 0x61)
length *= getComLength(data);
auto sym = new ComdefSymbol(name, length);
symtab.add(sym);
externs ~= name;
//writeln("COMDEF name:", cast(string)name);
}
enforce(data.length == 0, "Corrupt COMDEF record");
break;
case OmfRecordType.ALIAS:
case OmfRecordType.LPUBDEF:
case OmfRecordType.LCOMDEF:
case OmfRecordType.LEXTDEF:
enforce(false, "Record type " ~ to!string(r.type) ~ " not implemented");
break;
case OmfRecordType.LEDATA16:
case OmfRecordType.LEDATA32:
case OmfRecordType.LIDATA16:
case OmfRecordType.LIDATA32:
case OmfRecordType.FIXUPP16:
case OmfRecordType.FIXUPP32:
case OmfRecordType.LINNUM16:
case OmfRecordType.LINNUM32:
case OmfRecordType.LINSYM16:
case OmfRecordType.LINSYM32:
// Data definitions are skipped in the first pass
break;
case OmfRecordType.MODEND16:
case OmfRecordType.MODEND32:
//enforce(f.empty(), "MODEND is not at the end of object file");
auto data = r.data;
auto type = getByte(data);
auto isMain = (type & 0x80) != 0;
auto hasStart = (type & 0x40) != 0;
auto relStart = (type & 0x01) != 0;
enforce(!hasStart || relStart, "Relocatable start address flag must be set");
if (hasStart)
{
auto fixdata = getByte(data);
auto F = (fixdata & 0x80) != 0;
auto frametype = (fixdata & 0x70) >> 4;
auto T = (fixdata & 0x08) != 0;
auto P = (fixdata & 0x04) != 0;
auto Targt = (fixdata & 0x03);
ushort frame;
if (!F)
frame = getIndex(data);
ushort target;
if (!T)
target = getIndex(data);
uint displacement;
if (!P)
displacement = getDwordLE(data);
enforce(!P, "Displacement must be present for start address");
enforce(!F);
enforce(!T);
enforce(frametype == 1);
enforce(Targt == 2);
enforce(frame == 1, "Only FLAT group is supported");
enforce(displacement == 0, "Displacement is not supported for the start address");
//writeln(target, ' ', displacement);
//writeln(cast(string)externs[target-1]);
symtab.setEntry(externs[target-1]);
}
enforce(data.length == 0, "Corrupt MODEND record");
modend = true;
break;
default:
enforce(false, "Unsupported record type: " ~ to!string(r.type));
break;
}
}
symtab.checkUnresolved();
symtab.merge();
}
override void loadData(uint tlsBase)
{
debug(OMFDATA) writeln("OMF Object file: ", f.filename);
f.seek(0);
auto modend = false;
uint defOffset;
Section defSection;
bool defIterate;
immutable(ubyte)[] defData;
bool defOff16;
while (!f.empty() && !modend)
{
auto r = loadRecord();
auto data = r.data;
switch(r.type)
{
case OmfRecordType.THEADR:
auto len = r.data[0];
enforce(len == r.data.length - 1, "Corrupt THEADR record");
sourcefile = r.data[1..$];
debug(OMFDATA) writeln("Module: ", cast(string)r.data[1..$]);
break;
case OmfRecordType.LLNAMES:
case OmfRecordType.LNAMES:
case OmfRecordType.COMENT:
case OmfRecordType.SEGDEF16:
case OmfRecordType.SEGDEF32:
case OmfRecordType.GRPDEF:
case OmfRecordType.PUBDEF16:
case OmfRecordType.PUBDEF32:
case OmfRecordType.EXTDEF:
case OmfRecordType.CEXTDEF:
case OmfRecordType.COMDEF:
// Pass 1 records are skipped
break;
case OmfRecordType.ALIAS:
case OmfRecordType.LPUBDEF:
case OmfRecordType.LCOMDEF:
case OmfRecordType.LEXTDEF:
enforce(false, "Record type " ~ to!string(r.type) ~ " not implemented");
break;
case OmfRecordType.COMDAT16:
case OmfRecordType.COMDAT32:
writeRecord(defOffset, defSection, defIterate, defData, defOff16);
auto off16 = (r.type == OmfRecordType.COMDAT16);
auto flags = getByte(data);
auto isLocal = (flags & 0x4) != 0;
auto isIterated = (flags & 0x2) != 0;
assert(flags < 8);
auto attributes = getByte(data);
assert((attributes & 0xF0) <= 0x10);
assert((attributes & 0x0F) == 0x00);
auto alignment = getByte(data);
auto offset = off16 ? getWordLE(data) : getDwordLE(data);
auto type = getIndex(data);
auto baseGroup = getIndex(data);
enforce(baseGroup <= groups.length, "Invalid base group index");
auto baseSec = getIndex(data);
enforce(baseSec <= sections.length, "Invalid base section index");
if (baseSec == 0)
auto baseFrame = getWordLE(data);
auto name = getIndex(data);
auto sym = symtab.deepSearch(names[name-1]);
assert(sym);
assert(cast(ComdatSymbol)sym);
auto sec = (cast(ComdatSymbol)sym).sec;
defOffset = offset;
defSection = sec;
defIterate = isIterated;
defData = data;
defOff16 = off16;
debug(OMFDATA) writeln("COMDAT ", cast(string)sec.fullname, '+', offset, " ", data.length, " bytes");
debug(OMFDATA) writeln(sec.base, ' ', sec.container.base, ' ', flags, ' ', sec.container.members.length);
debug(OMFDATA) writefln("[%(%.2X %)]", data);
break;
case OmfRecordType.LEDATA16:
case OmfRecordType.LEDATA32:
writeRecord(defOffset, defSection, defIterate, defData, defOff16);
auto off16 = (r.type == OmfRecordType.LEDATA16);
auto index = getIndex(data);
enforce(index <= sections.length, "Invalid section index");
defOffset = off16 ? getWordLE(data) : getDwordLE(data);
defSection = sections[index-1];
defIterate = false;
defData = data;
defOff16 = off16;
debug(OMFDATA) writeln("LEDATA ", cast(string)defSection.fullname, '+', defOffset, " ", data.length, " bytes");
debug(OMFDATA) writefln("[%(%.2X %)]", data);
break;
case OmfRecordType.LIDATA16:
case OmfRecordType.LIDATA32:
writeRecord(defOffset, defSection, defIterate, defData, defOff16);
auto off16 = (r.type == OmfRecordType.LIDATA16);
auto index = getIndex(data);
enforce(index <= sections.length, "Invalid section index");
defOffset = off16 ? getWordLE(data) : getDwordLE(data);
defSection = sections[index-1];
defIterate = true;
defData = data;
defOff16 = off16;
debug(OMFDATA) writeln("LIDATA ", cast(string)defSection.fullname, '+', defOffset, " ", data.length, " bytes");
debug(OMFDATA) writefln("[%(%.2X %)]", data);
break;
case OmfRecordType.FIXUPP16:
case OmfRecordType.FIXUPP32:
while (data.length)
{
if ((data[0] & 0x80) == 0)
{
writeln("THREAD");
auto head = getByte(data);
auto index = getIndex(data);
}
else
{
auto locat = getWordBE(data);
auto M = (locat & 0x4000) != 0;
auto location = (locat & 0x3C00) >> 10;
auto offset = (locat & 0x3FF);
auto fixdata = getByte(data);
auto F = (fixdata & 0x80) != 0;
auto frametype = (fixdata & 0x70) >> 4;
auto T = (fixdata & 0x08) != 0;
auto P = (fixdata & 0x04) != 0;
auto Targt = (fixdata & 0x03);
ushort frame;
if (!F && (frametype == 0 || frametype == 1 || frametype == 2))
frame = getIndex(data);
ushort target;
if (!T)
target = getIndex(data);
uint displacement;
if (P == 0)
displacement = getDwordLE(data);
//writeln("FIXUP M:", M, " location:", location, " offset:", offset, " F:", F, " frametype:", frametype);
//writeln(" T:", T, " P:", P, " Targt: ", Targt, " frame:", frame, " target:", target, " disp:", displacement);
enforce(!F, "Frame threads are not supported");
enforce(!T, "Target threads are not supported");
//writeln("F", frametype, " T", (P << 2) | Targt);
uint targetBase;
uint targetAddress;
uint targetSeg;
//writeln(location, ' ', displacement, ' ', offset);
//writeln("in ", cast(string)defSection.fullname);
switch(Targt)
{
case 0:
debug(fixup) writeln("\nFIXUP target Segment relative (", cast(string)sections[target-1].name, ")");
auto targetSec = sections[target-1];
targetBase = targetSec.base;
targetAddress = targetBase + displacement;
targetSeg = targetSec.container.seg.segid;
break;
case 2:
//writeln(externs.length, ' ', target);
//writeln(cast(string[])externs);
debug(fixup) writeln("\nFIXUP target Extern relative (", cast(string)externs[target-1], ")");
auto extname = externs[target-1];
auto sym = symtab.deepSearch(extname);
debug(fixup) sym.dump();
assert(sym, cast(string)extname);
targetBase = sym.getAddress();
targetAddress = targetBase + displacement;
targetSeg = sym.getSegment();
break;
default:
enforce(false, "Group-relative targets are not supported");
break;
}
uint baseAddress;
switch(frametype)
{
case 0:
debug(fixup) writeln("FIXUP frame Segment relative (", cast(string)sections[frame-1].name, ")");
baseAddress = sections[frame-1].base;
break;
case 1:
debug(fixup) writeln("FIXUP frame Group relative (", cast(string)names[groups[frame-1].name-1], ")");
enforce(names[groups[frame-1].name-1] == "FLAT");
baseAddress = 0;
break;
case 5: // used for eh tables
debug(fixup) writeln("FIXUP frame Target X relative");
baseAddress = targetBase;
break;
default:
assert(0);
}
if (!M)
{
// self-relative
//enforce(baseAddress == 0);
debug(fixup) writeln("Relative");
baseAddress = defSection.base + defOffset + offset + 4;
}
else
{
debug(fixup) writeln("Direct");
baseAddress = 0;
}
// Warning: incoming casts: we know defData is unique
auto xdata = cast()defData;
switch(location)
{
case 9: // 32-bit offset
debug(fixup) writefln("### FIXUP 32b (0x%.4X) 0x%.8X -> 0x%.8X + 0x%.8X", offset, baseAddress, targetBase, displacement);
enforce(offset + 4 <= xdata.length);
(cast(uint[])xdata[offset..offset+4])[0] += targetAddress - baseAddress;
break;
case 10: // tls offset?
enforce(tlsBase != -1);
debug(fixup) writefln("### FIXUP tls (0x%.4X) 0x%.8X -> 0x%.8X + 0x%.8X", offset, tlsBase, targetBase, displacement);
enforce(offset + 4 <= xdata.length);
(cast(uint[])xdata[offset..offset+4])[0] += targetAddress - tlsBase;
break;
case 11: // seg-offset
debug(fixup) writefln("### FIXUP deb (0x%.4X) 0x%.8X -> 0x%.8X + 0x%.8X", offset, baseAddress, targetBase, displacement);
enforce(offset + 6 <= xdata.length);
(cast(ushort[])xdata[offset..offset+2])[0] = cast(ushort)targetSeg;
(cast(uint[])xdata[offset+2..offset+6])[0] = targetAddress - baseAddress;
break;
default:
enforce(false, "Only some weirdly selected and undocumented offset fixups are supported");
break;
}
}
}
enforce(data.length == 0, "Corrupt FIXUPP record");
break;
case OmfRecordType.LINNUM16:
case OmfRecordType.LINNUM32:
auto off16 = (r.type == OmfRecordType.LINNUM16);
debug(OMFDEBUG) writeln("LINNUM");
auto baseGroup = getIndex(data);
enforce(baseGroup <= groups.length, "Invalid group index");
assert(baseGroup == 0);
auto baseSeg = getIndex(data);
enforce(baseSeg && baseSeg <= sections.length, "Invalid section index");
while (data.length)
{
auto linnum = getWordLE(data);
auto offset = off16 ? getWordLE(data) : getDwordLE(data);
debug(OMFDEBUG) writeln(cast(string)sourcefile, "(", linnum, "): ", cast(string)sections[baseSeg-1].name, "+", offset);
}
break;
case OmfRecordType.LINSYM16:
case OmfRecordType.LINSYM32:
auto off16 = (r.type == OmfRecordType.LINSYM16);
debug(OMFDEBUG) writeln("LINSYM");
auto flags = getByte(data);
auto name = getIndex(data);
enforce(name <= names.length, "Invalid name index");
while (data.length)
{
auto linnum = getWordLE(data);
auto offset = off16 ? getWordLE(data) : getDwordLE(data);
debug(OMFDEBUG) writeln(cast(string)sourcefile, "(", linnum, "): _TEXT$", cast(string)names[name-1], "+", offset);
}
break;
case OmfRecordType.MODEND16:
case OmfRecordType.MODEND32:
modend = true;
writeRecord(defOffset, defSection, defIterate, defData, defOff16);
break;
default:
enforce(false, "Unsupported record type: " ~ to!string(r.type));
break;
}
}
}
private:
void writeRecord(uint offset, Section sec, bool iterate, immutable(ubyte)[] data, bool off16)
{
//enforce(sec.secclass != SectionClass.BSS, "Error: Data defined for uninitialized block");
if (!data.length)
return;
assert(sec);
if (!sec.alignedlength)
return;
if (sec.secclass == SectionClass.BSS)
return;
if (sec.secclass == SectionClass.DEBSYM)
return;
if (iterate)
{
void readDataBlock(immutable(ubyte)[] data)
{
auto repCount = off16 ? getWordLE(data) : getDwordLE(data);
auto blockCount = getWordLE(data);
//writeln("rep: ", repCount);
//writeln("blk: ", blockCount);
immutable(ubyte)[] repdata;
if (!blockCount)
{
repdata = getString(data);
//writeln("***********************************");
//writeln(offset, ": ", repdata, " * ", repCount);
foreach(i; 0..repCount)
{
//writeln(cast(string)sec.fullname);
//writeln(offset, " + ", repdata.length, " <= ", sec.length);
//writeln(sec.data.length);
enforce(offset + repdata.length <= sec.exactlength, "Data is too big for section");
sec.data[offset..offset + repdata.length] = repdata;
offset += repdata.length;
}
//writeln("LIDATA ", repdata, " * ", repCount);
}
else
{
assert(blockCount == 1);
foreach(i; 0..repCount)
readDataBlock(data);
}
}
readDataBlock(data);
} else {
//writeln(cast(string)sec.fullname);
//writeln(offset, ' ', data.length, ' ', sec.length);
//writeBytes(data);
enforce(offset + data.length <= sec.exactlength, "Data is too big for section");
sec.data[offset..offset + data.length] = data[];
}
}
OmfRecord loadRecord()
{
OmfRecord r;
r.type = OmfRecord.recordType(f.readByte());
r.data = f.readBytes(f.readWordLE())[0..$-1];
return r;
}
void checkMemoryModel(in ubyte[] s)
{
foreach(c; s)
{
switch(c)
{
case 'A':
enforce(false, "68000 memory model is not supported");
break;
case 'B':
enforce(false, "68010 memory model is not supported");
break;
case 'C':
enforce(false, "68020 memory model is not supported");
break;
case 'D':
enforce(false, "68030 memory model is not supported");
break;
case '0':
enforce(false, "8086 memory model is not supported");
break;
case '1':
enforce(false, "80186 memory model is not supported");
break;
case '2':
enforce(false, "80286 memory model is not supported");
break;
case '3':
enforce(false, "80386 memory model is not supported");
break;
case '7':
// 786, apparently ok
break;
case 'O':
// We don't care about optimization
break;
case 's':
enforce(false, "small memory model is not supported");
break;
case 'm':
enforce(false, "medium memory model is not supported");
break;
case 'c':
enforce(false, "compact memory model is not supported");
break;
case 'l':
enforce(false, "large memory model is not supported");
break;
case 'h':
enforce(false, "huge memory model is not supported");
break;
case 'n':
// Windows NT memory model
break;
default:
enforce(false, "Corrupt COMENT record");
}
}
}
SectionClass getSectionClass(immutable(ubyte)[] name, immutable(ubyte)[] cname)
{
SectionClass secclass;
with(SectionClass)
switch(cast(string)cname)
{
case "CODE": secclass = Code; break;
case "DATA":
if (name.length >= 4 && cast(string)name[0..4] == ".CRT")
secclass = CRT;
else
secclass = Data;
break;
case "CONST": secclass = Const; break;
case "BSS": secclass = BSS; break;
case "tls": secclass = TLS; break;
case "ENDBSS": secclass = BSS; break;
case "STACK": secclass = STACK; break;
case "DEBSYM": secclass = DEBSYM; break;
case "DEBTYP": secclass = DEBSYM; break;
default:
enforce(false, "Unknown section class: " ~ cast(string)cname);
break;
}
return secclass;
}
}
void writeBytes(in ubyte[] data)
{
write("[");
foreach(v; data)
writef("%.2X ", v);
writeln("]");
}