File: omf2asm.cpp

package info (click to toggle)
objconv 2.56%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,300 kB
  • sloc: cpp: 27,039; makefile: 4; sh: 2
file content (954 lines) | stat: -rw-r--r-- 37,699 bytes parent folder | download | duplicates (2)
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
/****************************  omf2asm.cpp   *********************************
* Author:        Agner Fog, modified by Don Clugston
* Date created:  2007-05-27
* Last modified: 2014-05-32
* Project:       objconv
* Module:        omf2asm.cpp
* Description:
* Module for disassembling OMF object files
*
* (c) 2007-2014 GNU General Public License www.gnu.org/copyleft/gpl.html
*****************************************************************************/
#include "stdafx.h"


// Constructor
COMF2ASM::COMF2ASM() {
}


// Convert
void COMF2ASM::Convert() {
   // Do the conversion
   
   // Tell disassembler
   Disasm.Init(0, 0);

   // Make temporary Segments table
   CountSegments();

   // Make external symbols in Disasm
   MakeExternalSymbolsTable();

   // Make public symbols in Disasm
   MakePublicSymbolsTable();

   // Make symbol table entries for communal symbols.
   MakeCommunalSymbolsTable();

   // Make Segment list and relocations list
   MakeSegmentList();

   // Make group definitions
   MakeGroupDefinitions();

   // Disassemble
   Disasm.Go();  

   // Take over output file from Disasm
   *this << Disasm.OutFile;
}

void COMF2ASM::CountSegments() {
   // Make temporary Segments table
   uint32_t i;                                     // Record number
   uint32_t NameIndex;                             // Name index
   uint32_t ClassIndex;                            // Class name index
   SOMFSegment SegRecord;                        // Segment record

   // Define structure of attributes
   OMF_SAttrib Attributes;

   // Initialize temporary list of segments. Entry 0 is blank
   Segments.PushZero();
   
   // Search for SEGDEF records
   for (i = 0; i < NumRecords; i++) {
      if (Records[i].Type2 == OMF_SEGDEF) {
         // SEGDEF record
         Records[i].Index = 3;
         // Loop through entries in record. There should be only 1
         while (Records[i].Index < Records[i].End) {
            // Read segment attributes
            Attributes.b = Records[i].GetByte();
            if (Attributes.u.A == 0) {
               // Frame and Offset only included if A = 0
               Records[i].GetWord();             // Frame ignored
               SegRecord.Offset = Records[i].GetByte();
            }
            else SegRecord.Offset = 0;

            SegRecord.Size = Records[i].GetNumeric();
            NameIndex  = Records[i].GetIndex();
            ClassIndex = Records[i].GetIndex();  // Class index
            Records[i].GetIndex();               // Overlay index ignored
            SegRecord.NameO = GetLocalNameO(NameIndex);     // Segment name

            if (Attributes.u.B) {
               // Segment is big
               if (Attributes.u.P) {
                  // 32 bit segment. Big means 2^32 bytes!
                  err.submit(2306);
               }
               else {
                  // 16 bit segment. Big means 2^16 bytes
                  SegRecord.Size = 0x10000;
               }
            }

            // Get word size
            SegRecord.WordSize = Attributes.u.P ? 32 : 16;

            // Get alignment
            switch (Attributes.u.A) {
            case 0:  // Absolute segment
            case 1:  // Byte alignment
               SegRecord.Align = 0;
               break;

            case 2:  // Word alignment
               SegRecord.Align = 1;
               break;

            case 3:  // Paragraph alignment
               SegRecord.Align = 4;
               break;

            case 4:  // Page alignment
               SegRecord.Align = 16;
               break;

            case 5:  // DWord alignment
               SegRecord.Align = 2;
               break;

            default: // Unknown
               SegRecord.Align = 3; // Arbitrary value
               break;
            }

            // Get further attributes from class name
            char * ClassName = GetLocalName(ClassIndex);

            // Convert class name to upper case
            uint32_t n = (uint32_t)strlen(ClassName);
            for (uint32_t j = 0; j < n; j++) ClassName[j] &= ~0x20;

            // Search for known class names.
            // Standard names are CODE, DATA, BSS, CONST, STACK
            if (strstr(ClassName, "CODE") || strstr(ClassName, "TEXT")) {
               // Code segment
               SegRecord.Type = 1;
            }
            else if (strstr(ClassName, "DATA")) {
               // Data segment
               SegRecord.Type = 2;
            }
            else if (strstr(ClassName, "BSS")) {
               // Unitialized data segment
               SegRecord.Type = 3;
            }
            else if (strstr(ClassName, "CONST")) {
               // Constant data segment
               SegRecord.Type = 4;
            }
            else if (strstr(ClassName, "STACK")) {
               // Stack segment.
               SegRecord.Type = 0;
            }
            else {
               // Unknown/user defined class. Assume data segment
               SegRecord.Type = 2;
            }

            // Store temporary segment record
            Segments.Push(SegRecord);
         }
         if (Records[i].Index != Records[i].End) err.submit(1203);   // Check for consistency
      }
   }

   FirstComDatSection = Segments.GetNumEntries();
   // Communal sections (as used by Digital Mars):
   // This part by Don Clugston
   for (i = 0; i < NumRecords; i++) {
      if (Records[i].Type2 == OMF_COMDAT) {
         Records[i].Index = 3;

         uint8_t flags = Records[i].GetByte();
         if ((flags & 2) != 0) {
            // don't support iterated data yet
            err.submit(2318);           // Error message: not supported
            continue;
         }
         uint8_t attribs = Records[i].GetByte();
         uint8_t align = Records[i].GetByte();
         uint32_t ofs  = Records[i].GetNumeric();
         Records[i].GetIndex(); // type (ignore)
         //uint16_t publicBase = 0;
         uint16_t publicSegment = 0;
         // From the OMF Spec 1.1: "If alloc type is EXPLICIT, public base is present and is
         // identical to public base fields BaseGroup, Base Segment & BaseFrame in the PUBDEF."
         // BUT: In the diagram in the spec it is described as 1-2 bytes (ie, an Index field).
         // but in PUBDEF, those fields are Index, Index, or Index, zero, Index. (2-5 bytes)
         // The diagram appears to be erroneous.
         if ((attribs & 0xF) == 0){
            //publicBase = Records[i].GetIndex();
            publicSegment = Records[i].GetIndex();
            if (publicSegment == 0) {
                //Records[i].GetIndex(); // skip frame in this case
                // I don't have the Digital Mars obj spec, but this seems to help ??
                publicSegment = Records[i].GetIndex(); // ??
            }
         }
         uint16_t publicName = Records[i].GetIndex();
         uint32_t RecSize = Records[i].End - Records[i].Index; // Calculate size of data
         if (attribs & 0xF) {
            SegRecord.Type = 0x1000  | (attribs & 0xFF);
            SegRecord.WordSize = (attribs & 0x2) ? 32 : 16;
         }
         else {			 
            // use value from segdef
            SegRecord.Type = 0x1000 | Segments[publicSegment].Type;
            SegRecord.WordSize = Segments[publicSegment].WordSize;
         }

         //SegRecord.Type |= 1;//!!

         if (align != 0) {
            // alignment: (none), byte, word, paragraph, page, dword, arbitrary, arbitrary.
            static const int alignvalues[] = {0, 0, 1, 4, 16, 2, 3, 3};
            SegRecord.Align = alignvalues[align & 0x7];
         }
         else { // use value from segdef
            SegRecord.Align = Segments[publicSegment].Align;
         }
         SegRecord.Size = RecSize;

         // Get function name
         const char * name = GetLocalName(publicName);

         // Make a section name by putting _text$ before function name
         uint32_t ComdatSectionNameIndex = NameBuffer.Push("_text$", 6);
         NameBuffer.PushString(name); // append function name
         SegRecord.NameO = ComdatSectionNameIndex;
         SegRecord.NameIndex = publicName;

         if (flags & 1) {
            // continuation.
            // Add to the length to the previous entry.
            Segments[Segments.GetNumEntries()-1].Size += RecSize;
         } 
         else {
            SegRecord.Offset = ofs;
            Segments.Push(SegRecord);
         }
      }
   }

   // Communal sections (as used by Borland):
   for (i = 0; i < NumRecords; i++) {
      if (Records[i].Type2 == OMF_COMDEF) {
         uint32_t DType, DSize = 0, DNum;
         uint16_t Segment = 0;
         const char * FuncName = 0;

         // Loop through possibly multiple entries in record
         while (Records[i].Index < Records[i].End) {
            // Get function name
            FuncName = Records[i].GetString();
            Records[i].GetByte(); // Type index, should be 0, ignored
            DType = Records[i].GetByte(); // Data type
            switch (DType) {
            case 0x61:
               DNum  = Records[i].GetLength();
               DSize = Records[i].GetLength() * DNum;
               break;
            case 0x62:
               DSize = Records[i].GetLength();
               break;
            default:
               DSize = Records[i].GetLength();
               if (DType < 0x60) { // Borland segment index
                  Segment = DType;
                  break;
               }
               err.submit(2016); // unknown type
               break;
            }
         }
         if (Segment >= Segments.GetNumEntries()) {err.submit(2016); return;}

         // Copy segment record
         SegRecord = Segments[Segment];

         // Make a section name as SEGMENTNAME$FUNCTIONNAME
         const char * SegmentName = (char*)NameBuffer.Buf() + SegRecord.NameO;
         uint32_t ComdatSectionNameIndex = NameBuffer.Push(SegmentName, (uint32_t)strlen(SegmentName));
         NameBuffer.Push("$", 1);
         NameBuffer.PushString(FuncName); // append function name
         SegRecord.NameO = ComdatSectionNameIndex;
         SegRecord.Size = DSize;
         SegRecord.Type |= 0x1000;
         //SegRecord.BufOffset = ??

         // Store segment
         Segments.Push(SegRecord);

         if (Records[i].Index != Records[i].End) err.submit(1203);   // Check for consistency
      }
   }
   // Number of segments, not including blank zero entry
   NumSegments = Segments.GetNumEntries() - 1;
}


void COMF2ASM::MakeExternalSymbolsTable() {
   // Make symbol table and string table entries for external symbols
   uint32_t iextsym;                               // External symbol index
   uint32_t isymo;                                 // Symbol index in disassembler
   uint32_t NumExtSym = SymbolNameOffset.GetNumEntries(); // Number of external symbols
   ExtdefTranslation.SetNum(NumExtSym+1);        // Allocate space in symbol index translation table

   // Loop through external symbol names
   for (iextsym = 1; iextsym < NumExtSym; iextsym++) {

      // Get name
      const char * Name = GetSymbolName(iextsym);

      // Define symbol
      isymo = Disasm.AddSymbol(0, 0, 0, 0, 0x20, 0, Name);

      // Update table for translating old EXTDEF number to disassembler symbol index
      ExtdefTranslation[iextsym] = isymo;
   }
}


void COMF2ASM::MakePublicSymbolsTable() {
   // Make symbol table entries for public symbols
   uint32_t i;                                     // Record index
   char * string;                                // Symbol name
   uint32_t Segment;                               // Segment
   uint32_t Offset;                                // Offset
   uint32_t isymo;                                 // Symbol number in disasm
   uint32_t CommunalSection = FirstComDatSection;  // Index to communal section

   PubdefTranslation.Push(0);                    // Make index 0 = 0

   // Search for PUBDEF records
   for (i = 0; i < NumRecords; i++) {
      if (Records[i].Type2 == OMF_PUBDEF) {
         // PUBDEF record

         Records[i].Index = 3;
         Records[i].GetIndex();                  // Group. Ignore
         Segment = Records[i].GetIndex();        // Segment
         if (Segment == 0) Records[i].GetWord(); // Base frame. Ignore

         // Loop through strings in record
         while (Records[i].Index < Records[i].End) {
            string = Records[i].GetString();     // Symbol name
            Offset = Records[i].GetNumeric();    // Offset to segment
            Records[i].GetIndex();               // Type index. Ignore

            // Define symbol
            isymo = Disasm.AddSymbol(Segment, Offset, 0, 0, 4, 0, string);

            // Update table for translating old PUBDEF number to disassembler symbol index
            PubdefTranslation.Push(isymo);
         }
         if (Records[i].Index != Records[i].End) err.submit(1203);   // Check for consistency
      }
   }

   // Search for OMF_COMDEF records
   for (i = 0; i < NumRecords; i++) {
      if (Records[i].Type2 == OMF_COMDEF) {
         // COMDEF record, Borland communal name
         uint32_t DType;
         //uint32_t DSize;
         //uint32_t DNum;
         Records[i].Index = 3;

         // Loop through possibly multiple entries in record
         while (Records[i].Index < Records[i].End) {
            string = Records[i].GetString();
            Records[i].GetByte(); // Type index, should be 0, ignore
            DType = Records[i].GetByte(); // Data type            
            switch (DType) {
            case 0x61:
               //DNum  = Records[i].GetLength();
               //DSize = Records[i].GetLength();
               continue; // Don't know what to do with this type. Ignore
            case 0x62:
               //DSize = Records[i].GetLength();
               continue; // Don't know what to do with this type. Ignore
            default:
               //DSize = Records[i].GetLength();
               if (DType < 0x60) { // Borland segment index
                  break;
               }
               continue; // Unknown type. Ignore
            }
            // Define symbol
            Segment = CommunalSection;
            isymo = Disasm.AddSymbol(Segment, 0, 0, 0, 0x10, 0, string);

            // Update table for translating old PUBDEF number to disassembler symbol index
            PubdefTranslation.Push(isymo);
         }
         CommunalSection++;

         if (Records[i].Index != Records[i].End) err.submit(1203);   // Check for consistency
      }
   }
}


void COMF2ASM::MakeCommunalSymbolsTable() {
   // Make symbol table entries for communal symbols
   char * string;                                // Symbol name

   // Search for communal records
   for (uint32_t i = 0; i < NumRecords; i++) {
      // Count communal records
      if (Records[i].Type2 == OMF_CEXTDEF) {
         Records[i].Index = 3;
         // Loop through strings in record
         while (Records[i].Index < Records[i].End) {			 
            uint32_t LIndex = Records[i].GetIndex();
            Records[i].GetIndex(); // Group. Ignore
            string = GetLocalName(LIndex);

            // find section with same name
            int32_t section = 0;
            for (uint32_t j = 0; j < Segments.GetNumEntries(); j++) {
               if (Segments[j].NameIndex == LIndex) {
                  section = (int32_t)j; break;
               }
            }

            // Define symbol
            Disasm.AddSymbol(section, 0, 0, 0, 0x10, 0, string);
         }
      }
   }
}


void COMF2ASM::MakeGroupDefinitions() {
   // Make segment group definitions
   uint32_t i;                                     // Record index

   // Search for group records
   for (i = 0; i < NumRecords; i++) {
      if (Records[i].Type2 == OMF_GRPDEF) {
         // GRPDEF record
         Records[i].Index = 3;
         // Get group name
         uint32_t ClassIndex = Records[i].GetIndex();
         char * GroupName = GetLocalName(ClassIndex);

         // Define group
         Disasm.AddSectionGroup(GroupName, 0);

         // Loop through remaining entries in record
         while (Records[i].Index < Records[i].End) {
            // Entry type should be 0xFF
            uint8_t Type = Records[i].GetByte();
            // Get member name
            int32_t NameIndex = Records[i].GetIndex();
            // Check if type valid
            if (Type == 0xFF && NameIndex > 0) {
               // A group member is found. Add member to group
               Disasm.AddSectionGroup(GroupName, NameIndex);
            }
         }
         if (Records[i].Index != Records[i].End) err.submit(1203);   // Check for consistency
      }
   }
}


// MakeSegmentList
void COMF2ASM::MakeSegmentList() {
   // Make Sections list in Disasm
   int32_t  SegNum;                                // Segment number
   int32_t  Segment = 0;                           // Segment number in OMF record
   uint32_t RecNum;                                // OMF record number
   uint32_t LastDataRecord;                        // OMF record number of last LEDATA record
   uint32_t RecOffset;                             // Segment offset of LEDATA, LIDATA record
   uint32_t RecSize;                               // Data size of LEDATA, LIDATA record
   uint32_t LastDataRecordSize;                    // Last RecSize
   uint32_t LastOffset;                            // Last RecOffset
   int8_t * LastDataRecordPointer;                 // Point to last raw data
   uint32_t BufOffset;                             // Offset of segment into SegmentData buffer
   CMemoryBuffer TempBuf;                        // Temporary buffer for building raw data

   // Loop through segments
   for (SegNum = 1; SegNum <= NumSegments; SegNum++) {

      // Get size
      uint32_t SegmentSize = Segments[SegNum].Size;
      if (SegmentSize == 0) continue;            // Empty segment

      // Allocate temporary data buffer and reset it
      TempBuf.SetSize(SegmentSize + 16);
      int FillByte = 0;                          // Byte to fill memory with
      if (Segments[SegNum].Type == 1) {
         // Code segment. Fill any unused bytes with NOP opcode = 0x90
         FillByte = 0x90;
      }
      memset(TempBuf.Buf(), FillByte, SegmentSize + 16);// Reset to all 0 or NOP

      LastDataRecord = 0;
      LastDataRecordSize = 0;
      LastDataRecordPointer = 0;
      LastOffset = 0;
      int comdatsSoFar = 0;

      // Search for LEDATA, LIDATA and FIXUPP records for this segment
      for (RecNum = 0; RecNum < NumRecords; RecNum++) {

         if (Records[RecNum].Type2 == OMF_LEDATA) {

            // LEDATA record
            Records[RecNum].Index = 3;           // Initialize record reading
            Segment = Records[RecNum].GetIndex();// Read segment number

            if ((Segment & 0xC000) == 0x4000) {
               // Refers to Borland communal section
               Segment = (Segment & ~0x4000) + FirstComDatSection - 1;
            }

            if (Segment != SegNum) continue; // Does not refer to this segment

            RecOffset = Records[RecNum].GetNumeric();// Read offset of this record
            RecSize = Records[RecNum].End - Records[RecNum].Index; // Calculate size of data
            LastDataRecord = RecNum;             // Save for later FIXUPP that refers to this record

            if (RecOffset < LastOffset + LastDataRecordSize && LastOffset < RecOffset + RecSize) {
               // Overlapping data records
               if (RecOffset + 8 < LastOffset + LastDataRecordSize || Segments[SegNum].Type != 1) {
                  // Overlapping data by more than 7 bytes or not executable code
                  err.submit(1207);
               }
               else {
                  // Possibly backpatched code
                  err.submit(1208);              // Warning
                  err.ClearError(1208);          // Report only once
               }
            }

            LastDataRecordSize = RecSize;
            LastDataRecordPointer = Records[RecNum].buffer + Records[RecNum].FileOffset + Records[RecNum].Index;
            LastOffset = RecOffset;                 // Save offset for subsequent FIXUPP records

            // Check if data within segment
            if (RecOffset + RecSize > SegmentSize) {
               err.submit(2309, GetSegmentName(Segment));
               continue;
            }

            // Put raw data into temporary buffer
            memcpy(TempBuf.Buf() + RecOffset, LastDataRecordPointer, RecSize);

         } // Finished with LEDATA record

         if (Records[RecNum].Type2 == OMF_LIDATA) {
            // LIDATA record
            Records[RecNum].Index = 3;           // Initialize record reading
            Segment = Records[RecNum].GetIndex();

            if (Segment != SegNum) continue; // Does not refer to this segment

            LastDataRecord = RecNum;             // Save for later FIXUPP that refers to this record

            RecOffset = Records[RecNum].GetNumeric();// Read offset

            if (RecOffset > SegmentSize) {
               err.submit(2310); continue;       // Error: outside bounds
            }

            // Unpack LIDATA blocks recursively
            RecSize = Records[RecNum].UnpackLIDATABlock(TempBuf.Buf() + RecOffset, SegmentSize - RecOffset);

            if (RecOffset < LastOffset + LastDataRecordSize && LastOffset < RecOffset + RecSize) {
               // Overlapping data records
               err.submit(1207);                 // Warning
            }
            LastDataRecordSize = RecSize;           // Save data size
            LastOffset = RecOffset;                 // Save offset for subsequent FIXUPP records

         } // Finished with LIDATA record

         if (Records[RecNum].Type2 == OMF_COMDAT) {
            // COMDAT record.

            Records[RecNum].Index = 3;           // Initialize record reading
            uint16_t flags = Records[RecNum].GetByte();
            if ((flags&1)==0) { // not a continuation
               ++comdatsSoFar;
               LastDataRecord = RecNum;             // Save for later FIXUPP that refers to this record
            }
            Segment = FirstComDatSection + comdatsSoFar-1;
            if (SegNum != Segment) continue;

            uint16_t attribs = Records[RecNum].GetByte();
            Records[RecNum].GetByte(); // align (ignore)
            RecOffset = Records[RecNum].GetNumeric();
            Records[RecNum].GetIndex(); // type (ignore)
            if ((attribs&0xF)==0) {
               Records[RecNum].GetIndex(); // public base
               uint16_t publicSegment = Records[RecNum].GetIndex();
               if (publicSegment==0) Records[RecNum].GetIndex(); // public frame (ignore)
            }
            Records[RecNum].GetIndex(); // public name (ignore)
            RecSize = Records[RecNum].End - Records[RecNum].Index; // Calculate size of data

            LastDataRecord = RecNum;             // Save for later FIXUPP that refers to this record
            LastDataRecordSize = RecSize;
            LastDataRecordPointer = Records[RecNum].buffer + Records[RecNum].Index+Records[RecNum].FileOffset;
            LastOffset = RecOffset;
            // Put raw data into temporary buffer
            memcpy(TempBuf.Buf() + RecOffset, LastDataRecordPointer, RecSize);
         } // Finished with COMDAT record

         if (Records[RecNum].Type2 == OMF_FIXUPP) {
            // FIXUPP record
            if (Segment != SegNum) continue; // Does not refer to this segment
            Records[RecNum].Index = 3;

            if (Records[LastDataRecord].Type2 == OMF_LEDATA) {
               // FIXUPP for last LEDATA record
               // Make relocation records
               MakeRelocations(Segment, RecNum, LastOffset, LastDataRecordSize, (uint8_t*)TempBuf.Buf());
            } 
            else if (Records[RecNum].Index < Records[RecNum].End) {
               // Non-empty FIXUPP record does not refer to LEDATA record
               if (Records[LastDataRecord].Type2 == OMF_COMDAT) {
                  // FIXUPP for last COMDAT record
                  // Make relocation records
                  MakeRelocations(Segment, RecNum, LastOffset, LastDataRecordSize, (uint8_t*)TempBuf.Buf());
               }
               else if (Records[LastDataRecord].Type2 == OMF_LIDATA) {
                  err.submit(2311);              // Error: Relocation of iterated data not supported
               }
               else {
                  err.submit(2312);              // Does not refer to data record
               }
            }
         }
      } // End of loop to search for LEDATA, LIDATA and FIXUPP records for this segment

      // Transfer raw data from TempBuf to SegmentData buffer
      BufOffset = SegmentData.Push(TempBuf.Buf(), SegmentSize);

      // Remember offset into SegmentData
      Segments[SegNum].BufOffset = BufOffset;

   } // End of first loop through segments

   // We must put all segments into SegmentData buffer before we assign pointers to
   // the raw data because otherwise the SegmentData buffer might me reallocated
   // when it grows and the pointers become invalid. This is the reasons why we
   // have two loops through the segments here.

   // Second loop through segments
   int totalcodesize=0;
   for (SegNum = 1; SegNum <= NumSegments; SegNum++) {

      // Pointer to merged raw data
      uint8_t * RawDatap = (uint8_t*)SegmentData.Buf() + Segments[SegNum].BufOffset;

      // Size of raw data
      uint32_t InitSize = (Segments[SegNum].Type == 3) ? 0 : Segments[SegNum].Size;

      // Define segment
      const char * SegmentName = (char*)NameBuffer.Buf() + Segments[SegNum].NameO;
      Disasm.AddSection(RawDatap, InitSize, Segments[SegNum].Size, Segments[SegNum].Offset,
         Segments[SegNum].Type, Segments[SegNum].Align, Segments[SegNum].WordSize, SegmentName);
      if (Segments[SegNum].Type == 1 || Segments[SegNum].Type == 0x1001) {
         totalcodesize += Segments[SegNum].Size;
      }
   }
}


// MakeRelocations
void COMF2ASM::MakeRelocations(int32_t Segment, uint32_t RecNum, uint32_t SOffset, uint32_t RSize, uint8_t * SData) {
   // Make relocations for object and executable files
   // Parameters:
   // Segment = segment index of last LEDATA record
   // RecNum = FIXUPP record number
   // SOffset = segment relative offset of last LEDATA record
   // RSize = Size of last LEDATA record
   // SData = pointer to raw segment data

   uint32_t Frame, Target, TargetDisplacement; // Contents of FIXUPP record
   uint8_t  byte1, byte2;                // First two bytes of subrecord
   int32_t  Inline;                      // Inline address or addend in relocation source
   //int16_t  InlineSeg;                   // Segment address stored in relocation source
   int32_t  Addend;                      // Correction to add to target address
   int32_t  SourceSize;                  // Size of relocation source
   uint32_t RelType;                     // Relocation type, as defined in disasm.h
   int32_t  TargetSegment;               // Target segment or group
   uint32_t TargetOffset;                // Target offset
   uint32_t TargetSymbol;                // Symbol index of target
   uint32_t ReferenceIndex;              // Segment/group index of reference frame

   // Bitfields in subrecords
   OMF_SLocat Locat;         // Structure of first two bytes of FIXUP subrecord swapped = Locat field
   OMF_SFixData FixData;     // Structure of FixData field in FIXUP subrecord of FIXUPP record
   OMF_STrdDat TrdDat;       // Structure of Thread Data field in THREAD subrecord of FIXUPP record

   Records[RecNum].Index = 3;

   // Loop through entries in record
   while (Records[RecNum].Index < Records[RecNum].End) {

      // Read first byte
      byte1 = Records[RecNum].GetByte();
      if (byte1 & 0x80) {

         // This is a FIXUP subrecord
         Frame = 0; Target = 0; TargetDisplacement = 0;  Addend = 0;  ReferenceIndex = 0;

         // read second byte
         byte2 = Records[RecNum].GetByte();
         // swap bytes and put into byte12 bitfield
         Locat.bytes[1] = byte1;
         Locat.bytes[0] = byte2;

         // Read FixData
         FixData.b = Records[RecNum].GetByte();

         // Read conditional fields
         if (FixData.s.F) {
            // Frame specified by previously define thread
            // Does anybody still use compression of repeated fixup targets?
            // I don't care to support this if it is never used
            err.submit(2313);           // Error message: not supported
            continue;
         }
         else {
            if (FixData.s.Frame < 4) {
               // Frame datum field present
               Frame = Records[RecNum].GetIndex();
            }
            else Frame = 0;

            switch (FixData.s.Frame) { // Frame method
            case 0:  // F0: segment
               ReferenceIndex = Frame;
               break;

            case 1:  // F1: group
               // Groups defined after segments. Add number of segments to get group index
               ReferenceIndex = Frame + NumSegments;
               break;

            default:
            case 2:  // F2: external symbol
               ReferenceIndex = 0;
               break;

            case 4:  // F4: traget frame = source frame
               Frame = Segment;
               break;

            case 5:  // F5: target frame = target segment
               Frame = 0;
               break;
            }
         }

         if (FixData.s.T == 0) {
            // Target specified
            Target = Records[RecNum].GetIndex();
            if ((Target & 0xC000) == 0x4000) {
               // Refers to Borland communal section
               Target = (Target & ~0x4000) + FirstComDatSection - 1;
            }
            //uint32_t TargetMethod = FixData.s.Target + FixData.s.P * 4;
         }
         else {
            // Target specified in previous thread
            // Does anybody still use compression of repeated fixup targets?
            // I don't care to support this if it is never used
            err.submit(2313);           // Error message: not supported
            continue;
         }

         if (FixData.s.P == 0) {
            TargetDisplacement = Records[RecNum].GetNumeric();
         }

         if (!SData || Locat.s.Offset > RSize) {
            err.submit(2032); // Relocation points outside segment
            return;
         }
         // Get inline addend and check relocation method

         // Pointer to relocation source inline in raw data:
         uint8_t * inlinep = SData + SOffset + Locat.s.Offset;
         Inline = 0;  SourceSize = 0; 
         //InlineSeg = 0;  
         TargetSegment = 0;  TargetOffset = 0;  TargetSymbol = 0;

         // Relocation type
         if (Locat.s.M) {
            // Segment relative
            RelType = 8;
         }
         else {
            // (E)IP relative
            RelType = 2;
         }

         switch (Locat.s.Location) {// Relocation method
         case OMF_Fixup_8bit:       // 8 bit
            SourceSize = 1;
            Inline = *(int8_t*)inlinep;
            break;

         case OMF_Fixup_16bit:      // 16 bit 
            SourceSize = 2;
            Inline = *(int16_t*)inlinep;
            break;

         case OMF_Fixup_32bit:      // 32 bit
            SourceSize = 4;
            Inline = *(int32_t*)inlinep;
            break;

         case OMF_Fixup_Far:        // far 16+16 bit
            RelType = 0x400;
            SourceSize = 4;
            Inline = *(int16_t*)inlinep;
            break;

         case OMF_Fixup_Farword:    // far 32+16 bit
         case OMF_Fixup_Pharlab48:
            RelType = 0x400;
            SourceSize = 6;
            Inline = *(int32_t*)inlinep;
            break;

         case OMF_Fixup_Segment:    // segment selector
            if (TargetDisplacement || FixData.s.Target == 2) {
               // An offset is specified or an external symbol.
               // Segment of symbol is required (seg xxx)
               RelType = 0x200;
            }
            else {
               // A segment name or group name is required
               RelType = 0x100;
            };
            SourceSize = 2;
            Inline = *(int16_t*)inlinep;
            break;

         case OMF_Fixup_16bitLoader: // 16-bit loader resolved
            RelType = 0x21;
            SourceSize = 2;
            Inline = *(int16_t*)inlinep;
            break;

         case OMF_Fixup_32bitLoader: // 32-bit loader resolved
            RelType = 0x21;
            SourceSize = 4;
            Inline = *(int32_t*)inlinep;
            break;

         default:                   // unknown or not supported
            RelType = 0;
            SourceSize = 0;
            Inline = 0;
         } // end switch


         // Offset of relocation source
         uint32_t SourceOffset = SOffset + Locat.s.Offset;

         // Relocation type: direct or (E)IP-relative
         if (RelType == 2) {
            // (E)IP-relative
            // Correct for difference between source address and end of instruction
            Addend = -SourceSize;
         }

         // Check target method
         switch (FixData.s.Target) {             // = Target method modulo 4
         case 0: // T0 and T4: Target = segment
            // Local or public symbol
            TargetSegment = Target;              // Target segment
            TargetOffset = TargetDisplacement;   // Target offset 
            if (RelType != 0x100) {
               // Add inline to target address, except if target is a segment only
               TargetOffset += Inline;
               Addend -= Inline;                 // Avoid adding Inline twice
            }
            break;

         case 1: // T1 and T5: Target = segment group
            // Warning: this method has not occurred. Not tested!
            // Groups are numbered in sequence after segments in Disasm. Add number of segments to group index
            TargetSegment = Target + NumSegments;// Target group
            TargetOffset = TargetDisplacement;   // Target offset 
            if (RelType != 0x100) {
               // Add inline to target address, except if target is a segment only
               TargetOffset += Inline;
               Addend -= Inline;                 // Avoid adding Inline twice
            }
            break;

         case 2: // T2 and T6: Target = external symbol
            // Translate old EXTDEF index to new symbol table index
            if (Target < ExtdefTranslation.GetNumEntries()) {
               TargetSymbol = ExtdefTranslation[Target];
            }
            break;

         default: // Unknown method
            err.submit(2314, FixData.s.Target + FixData.s.P * 4);
         }

         if (TargetSymbol == 0) {
            // Make symbol record for target
            TargetSymbol = Disasm.AddSymbol(TargetSegment, TargetOffset, 0, 0, 2, 0, 0);
         }

         if (FixData.s.Frame == 4 && FixData.s.Target + FixData.s.P*4 == 6) {
            // Note:
            // Frame method F4 is apparently used by 16-bit Borland compiler for
            // indicating floating point instructions that can be emulated if no
            // 8087 processor is present. I can't find this documented anywhere.
            // I don't know what the exact criterion is for indicating that a FIXUP
            // subrecord is not a relocation record but a f.p. emulating record.
            // I have chosen to consider all subrecords with frame method F4 and 
            // target method T6 to be ignored.
            ;
         }
         else {
            // This is a proper relocation subrecord
            Disasm.AddRelocation(Segment, SourceOffset, Addend, RelType, SourceSize, TargetSymbol, ReferenceIndex); 
         }
      }
      else {
         // This is a THREAD subrecord.
         // I don't think this feature for compressing fixup data is
         // used any more, if it ever was. I am not supporting it here.
         // Frame threads can be safely ignored. A target thread cannot
         // be ignored if there is any reference to it. The error is 
         // reported above at the reference to a target thread, not here.
         TrdDat.b = byte1;              // Put byte into bitfield
         if (TrdDat.s.Method < 4) {     // Make sure we read this correctly, even if ignored
            Records[RecNum].GetIndex(); // has index field if method < 4 ?
         }
      }
   } // Finished loop through subrecords

   if (Records[RecNum].Index != Records[RecNum].End) err.submit(1203);   // Check for consistency
}