File: mac2elf.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 (1267 lines) | stat: -rw-r--r-- 56,027 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
/****************************  mac2elf.cpp   *********************************
* Author:        Agner Fog
* Date created:  2008-05-15
* Last modified: 2009-05-19
* Project:       objconv
* Module:        mac2elf.cpp
* Description:
* Module for converting Mach-O file to ELF file
*
* Copyright 2008 GNU General Public License http://www.gnu.org/licenses
*****************************************************************************/

#include "stdafx.h"


template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt,
          class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
CMAC2ELF<MACSTRUCTURES,ELFSTRUCTURES>::CMAC2ELF () {
   // Constructor
   memset(this, 0, sizeof(*this));                    // Reset everything
}


template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt,
          class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CMAC2ELF<MACSTRUCTURES,ELFSTRUCTURES>::Convert() {
   // Do the conversion
   // Some compilers require this-> for accessing members of template base class,
   // according to the so-called two-phase lookup rule.

   NumSectionsNew = 5;                                    // Number of sections generated so far

   // Allocate variable size buffers
   MaxSectionsNew = NumSectionsNew + 2 * this->NumSections + 2;// Max number of sections needed
   NewSections.SetNum(MaxSectionsNew+1);                  // Allocate buffers for each section
   NewSections.SetZero();                                 // Initialize
   NewSectionHeaders.SetNum(MaxSectionsNew+1);            // Allocate array for temporary section headers
   NewSectionHeaders.SetZero();                           // Initialize
   NewSectIndex.SetNum(this->NumSections+1);              // Array for translating old section index to new section index
   NewSectIndex.SetZero();                                // Initialize
   SectionSymbols.SetNum(this->MaxSectionsNew+1);         // Array of new symbol indices for sections
   SectionSymbols.SetZero();                              // Initialize
   NewSymbolIndex.SetNum(this->SymTabNumber);             // Array of new symbol indices
   NewSymbolIndex.SetZero();                              // Initialize

   // Call the subfunctions
   ToFile.SetFileType(FILETYPE_ELF);       // Set type of to file
   MakeSegments();                         // Make segment headers and code/data segments
   MakeSymbolTable();                      // Symbol table and string tables
   MakeRelocationTables(this->FileHeader); // Make relocation tables
   MakeImportTables();                     // Fill import tables
   MakeGOT();                              // Make fake Global Offset Table
   MakeBinaryFile();                       // Putting sections together
   *this << ToFile;                        // Take over new file buffer
}


template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt,
          class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CMAC2ELF<MACSTRUCTURES,ELFSTRUCTURES>::MakeSegments() {
   // Convert subfunction: Make segment headers and code/data segments
   TELF_SectionHeader NewSecHeader; // New section header
   uint32_t oldsec;                   // Section index in old file
   uint32_t newsec;                   // Section index in new file
   uint32_t SecNameIndex;             // Section name index into shstrtab
   char const * SecName;            // Name of new section
   const int MAXSECTIONNAMELENGTH = 256;
   char RelocationSectionName[MAXSECTIONNAMELENGTH];
   const int WordSize = sizeof(MInt) * 8;

   // Special segment names
   static const char * SpecialSegmentNames[] = {
      "Null", ".symtab", ".shstrtab", ".strtab", ".stabstr"
   };
   // Indexes to these are:
   symtab      = 1;               // Symbol table section number
   shstrtab    = 2;               // Section name string table section number
   strtab      = 3;               // Object name string table section number
   stabstr     = 4;               // Debug string table section number

   // Number of special segments = number of names in SpecialSegmentNames:
   const uint32_t NumSpecialSegments = sizeof(SpecialSegmentNames)/sizeof(SpecialSegmentNames[0]);

   // Make first section header string table entry empty
   NewSections[shstrtab].PushString("");

   // Loop through special sections, except the first Null section:
   for (newsec = 0; newsec < NumSpecialSegments; newsec++) {
      // Put data into new section header:
      // Initialize to zero
      memset(&NewSecHeader, 0, sizeof(NewSecHeader));

      if (newsec > 0) {
         // Put name into section header string table
         SecName = SpecialSegmentNames[newsec];
         SecNameIndex = NewSections[shstrtab].PushString(SecName);

         // Put name into new section header
         NewSecHeader.sh_name = SecNameIndex;
      }

      // Put section header into temporary buffer
      NewSectionHeaders[newsec] = NewSecHeader;
   }

   // Put type, flags, etc. into special segments:
   NewSectionHeaders[symtab]  .sh_type  = SHT_SYMTAB;
   NewSectionHeaders[symtab]  .sh_entsize = sizeof(TELF_Symbol);
   NewSectionHeaders[symtab]  .sh_link  = strtab;
   NewSectionHeaders[shstrtab].sh_type  = SHT_STRTAB;
   NewSectionHeaders[shstrtab].sh_flags = SHF_STRINGS;
   NewSectionHeaders[shstrtab].sh_addralign = 1;
   NewSectionHeaders[strtab]  .sh_type  = SHT_STRTAB;
   NewSectionHeaders[strtab]  .sh_flags = SHF_STRINGS;
   NewSectionHeaders[strtab]  .sh_addralign = 1;
   NewSectionHeaders[stabstr] .sh_type  = SHT_STRTAB;
   NewSectionHeaders[stabstr] .sh_flags = SHF_STRINGS;
   NewSectionHeaders[stabstr] .sh_addralign = 1;

   if (newsec != NumSectionsNew) {
      // Check my program for internal consistency
      // If you get this error then change the value of NumSectionsNew in 
      // the constructor to equal the number of entries in 
      // SpecialSegmentNames, including the Null segment
      err.submit(9000);
   }

   // Find sections in old file
   uint32_t icmd;                        // Current load command
   uint32_t command;                     // Load command
   uint32_t cmdsize = 0;                 // Command size

   // Pointer to current position in old file
   uint8_t * currentp = (uint8_t*)(this->Buf() + sizeof(TMAC_header));

   // Loop through file commands
   for (icmd = 1; icmd <= this->FileHeader.ncmds; icmd++, currentp += cmdsize) {
      command = ((MAC_load_command*)currentp) -> cmd;
      cmdsize = ((MAC_load_command*)currentp) -> cmdsize;

      if (command == MAC_LC_SEGMENT || command == MAC_LC_SEGMENT_64) {
         // This is the segment command (there should be only one)
         if ((command == MAC_LC_SEGMENT) ^ (WordSize == 32)) {
            // 32-bit segment in 64-bit file or vice versa
            err.submit(2320);  return;
         }
         if (cmdsize < sizeof(TMAC_segment_command)) {
            // Zero cmdsize or too small
            err.submit(2321); return;
         }
         // Point to segment command
         TMAC_segment_command * sh = (TMAC_segment_command*)currentp;

         if (stricmp(sh->segname, MAC_SEG_OBJC) == 0) {
            // objective-C runtime segment
            err.submit(2021);  continue;
         }

         // Find first section header
         TMAC_section * sectp = (TMAC_section*)(currentp + sizeof(TMAC_segment_command));

         // Loop through section headers
         for (oldsec = 1; oldsec <= this->NumSections; oldsec++, sectp++) {

            // Get section name
            SecName = sectp->sectname;

            // Check for special section names
            if (stricmp(SecName,"__eh_frame") == 0) {
               // This is an exception handler section
               if (cmd.ExeptionInfo == CMDL_EXCEPTION_STRIP) {
                  // Remove exception handler section
                  cmd.CountExceptionRemoved();
                  continue;
               }
               else if (cmd.InputType != cmd.OutputType) {
                  err.submit(1030); // Warn that exception information is incompatible
               }
            }
            if (sectp->flags & MAC_S_ATTR_DEBUG) {
               // This section has debug information
               if (cmd.DebugInfo == CMDL_DEBUG_STRIP) {
                  // Remove debug info
                  cmd.CountDebugRemoved();
                  continue;
               }
               else if (cmd.InputType != cmd.OutputType) {
                  err.submit(1029); // Warn that debug information is incompatible
               }
            }

            // Store section index in index translation table
            NewSectIndex[oldsec] = newsec;

            // Store section data
            if (sectp->size > 0 && !((sectp->flags & MAC_SECTION_TYPE) == MAC_S_ZEROFILL || (sectp->flags & MAC_SECTION_TYPE)==MAC_S_GB_ZEROFILL)) {
               NewSections[newsec].Push(this->Buf()+sectp->offset, uint32_t(sectp->size));
            }

            // Put data into new section header:
            // Initialize to zero
            memset(&NewSecHeader, 0, sizeof(NewSecHeader));

            uint32_t type = sectp->flags & MAC_SECTION_TYPE;
            uint32_t attributes = sectp->flags & MAC_SECTION_ATTRIBUTES;

            // Section type
            if (type == MAC_S_ZEROFILL || type == MAC_S_GB_ZEROFILL) {
               // BSS section
               NewSecHeader.sh_type = SHT_NOBITS;    // BSS
            }
            else {
               // Normal code or data section
               NewSecHeader.sh_type = SHT_PROGBITS;  // Program code or data
            }

            // Section flags
            NewSecHeader.sh_flags |= SHF_ALLOC;      // Occupies memory during execution
            if (attributes & (MAC_S_ATTR_SOME_INSTRUCTIONS | MAC_S_ATTR_PURE_INSTRUCTIONS)) {
               // Executable
               NewSecHeader.sh_flags |= SHF_EXECINSTR;
            }
            else {
               switch (type) {
               case MAC_S_CSTRING_LITERALS:
               case MAC_S_4BYTE_LITERALS:
               case MAC_S_8BYTE_LITERALS:
               case MAC_S_16BYTE_LITERALS:
               case MAC_S_LITERAL_POINTERS:
                  // not writeable
                  break;
               default:
                  // writeable
                  NewSecHeader.sh_flags |= SHF_WRITE;
                  break;
               }
            }

            // Check for special sections
            if (strcmp(SecName, MAC_CONSTRUCTOR_NAME) == 0) {
               // Constructors segment
               SecName = ELF_CONSTRUCTOR_NAME;
               NewSecHeader.sh_flags = SHF_WRITE | SHF_ALLOC;
            }

            // Put name into section header string table
            SecNameIndex = NewSections[shstrtab].PushString(SecName);

            // Put name into new section header
            NewSecHeader.sh_name = SecNameIndex;

            // Section virtual memory address
            NewSecHeader.sh_addr = sectp->addr;

            // Section size in memory
            NewSecHeader.sh_size = sectp->size;

            // Section alignment
            NewSecHeader.sh_addralign = uint32_t(1 << sectp->align);

            // Put section header into temporary buffer
            NewSectionHeaders[newsec] = NewSecHeader;

            // Increment section number
            newsec++;

            // Check if section is import table
            int SectionType   = sectp->flags & MAC_SECTION_TYPE;
            int IsImportTable = SectionType >= MAC_S_NON_LAZY_SYMBOL_POINTERS && SectionType <= MAC_S_SYMBOL_STUBS;

            if (sectp->nreloc > 0 || IsImportTable) {
               // Source section has relocations. 
               // Make a relocation section in destination file

               // Put data into relocation section header:
               // Initialize to zero
               memset(&NewSecHeader, 0, sizeof(NewSecHeader));

               // Name for relocation section = ".rel" or ".rela" + name of section
               if (WordSize == 32) {
                  strcpy(RelocationSectionName, ".rel");
               }
               else {
                  strcpy(RelocationSectionName, ".rela");
               }
               strncat(RelocationSectionName, SecName, MAXSECTIONNAMELENGTH-5);
               RelocationSectionName[MAXSECTIONNAMELENGTH-1] = 0;

               // Put name into section header string table
               uint32_t SecNameIndex = NewSections[shstrtab].PushString(RelocationSectionName);

               // Put name into new section header
               NewSecHeader.sh_name = SecNameIndex;

               // Section type
               NewSecHeader.sh_type = (WordSize == 32) ? SHT_REL : SHT_RELA;  // Relocation section

               // Entry size
               NewSecHeader.sh_entsize = (WordSize == 32) ? sizeof(Elf32_Rel) : sizeof(Elf64_Rela);  // Relocation section

               // Section alignment
               NewSecHeader.sh_addralign = WordSize / 8;

               // Link to the section it relocates for
               NewSecHeader.sh_info = newsec - 1;

               // Put section header into temporary buffer
               NewSectionHeaders[newsec] = NewSecHeader;

               // Increment section number
               newsec++;

               // Check if there are any GOT relocations
               // Pointer to old relocation entry
               if (sectp->reloff >= this->GetDataSize()) {err.submit(2035); break;}
               MAC_relocation_info * relp = (MAC_relocation_info*)(this->Buf() + sectp->reloff);
               // Loop through old relocations
               for (uint32_t oldr = 1; oldr <= sectp->nreloc; oldr++, relp++) {
                  uint32_t RType = relp->r_type;         // relocation type
                  // No scattered relocation in 64-bit mode. GOT only in 64-bit mode
                  if (WordSize == 64 && (RType == MAC64_RELOC_GOT_LOAD || RType == MAC64_RELOC_GOT)) {
                     HasGOT++;
                  }
               }
            }
         }

         // Check if GOT needed
         if (HasGOT && WordSize == 64) {
            // Make a fake Global Offset Table
            FakeGOTSection = newsec;

            // Put name and data into section header
            memset(&NewSecHeader, 0, sizeof(NewSecHeader));
            SecNameIndex = NewSections[shstrtab].PushString("_fakeGOT");
            NewSecHeader.sh_name = SecNameIndex;
            NewSecHeader.sh_type = SHT_PROGBITS; // Type
            NewSecHeader.sh_flags = SHF_ALLOC;   // Flags
            NewSecHeader.sh_addralign = 8;       // Alignment

            // Put section header into temporary buffer
            NewSectionHeaders[newsec++] = NewSecHeader;

            // Make relocation section for fake GOT
            memset(&NewSecHeader, 0, sizeof(NewSecHeader));
            // Put name and data into section header
            SecNameIndex = NewSections[shstrtab].PushString("_rela.fakeGOT");
            NewSecHeader.sh_name = SecNameIndex;
            NewSecHeader.sh_type = SHT_RELA;     // Type
            NewSecHeader.sh_flags = 0;           // Flags
            NewSecHeader.sh_addralign = 8;       // Alignment
            NewSecHeader.sh_entsize = sizeof(Elf64_Rela);  // Entry size
            NewSecHeader.sh_info = newsec - 1;   // Link to the section it relocates for
            NewSecHeader.sh_link = symtab;       // Link to symbol table
            // Put section header into temporary buffer
            NewSectionHeaders[newsec++] = NewSecHeader;
         }
         // Number of sections generated
         NumSectionsNew = newsec;
      }
   }
}


template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt,
          class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CMAC2ELF<MACSTRUCTURES,ELFSTRUCTURES>::MakeSymbolTable() {
   // Convert subfunction: Make symbol table and string tables
   uint32_t isym;            // current old symbol table entry
   uint32_t OldSectionIndex; // Index into old section table. 1-based
   uint32_t NewSectionIndex; // Index into new section table. 0-based
   const char * name1;     // Name of symbol
   TELF_Symbol sym;        // Temporary symbol table record
   uint32_t DebugRemoved = 0;// Debug symbols removed

   // pointer to old string table
   char * oldstringtab = (char*)(this->Buf() + this->StringTabOffset); 

   // pointer to old symbol table
   TMAC_nlist * symp0, *symp;
   symp0 = (TMAC_nlist*)(this->Buf() + this->SymTabOffset);

   // Check within range
   if (this->SymTabOffset + this->SymTabNumber * sizeof(TMAC_nlist) > this->DataSize) {
      err.submit(2040);  return;
   }

   // Make the first symbol record empty
   NewSections[symtab].Push(0, sizeof(TELF_Symbol));

   // Make first string table entries empty
   NewSections[strtab] .PushString("");
   NewSections[stabstr].PushString("");

   // Make symbol records for the start of each section in case they are needed
   // by section-relative relocations (r_extern = 0 in MAC_relocation_info)
   for (uint32_t sec = 1; sec < NumSectionsNew; sec++) {
      uint32_t type = NewSectionHeaders[sec].sh_type;
      if (type == SHT_PROGBITS || type == SHT_NOBITS) {
         // Make unnamed symbol table entry for this section
         memset(&sym, 0, sizeof(sym));
         sym.st_shndx = sec;
         sym.st_type = STT_SECTION;
         // Put record into new symbol table
         NewSections[symtab].Push(&sym, sizeof(sym));
         // Insert into section symbol translation table
         SectionSymbols[sec] = NewSections[symtab].GetLastIndex();
      }
   }

   // Loop through old symbol table. Local symbols first, global symbols last
   for (isym = 0, symp = symp0; isym < this->SymTabNumber; isym++, symp++) {

      if ((symp->n_type & MAC_N_STAB) && (cmd.DebugInfo & CMDL_DEBUG_STRIP)) {
         // Debug symbol should be removed
         DebugRemoved++;  continue;
      }

      // Reset destination entry
      memset(&sym, 0, sizeof(sym));

      // Get binding
      if (isym < this->iextdefsym) {
         // Local
         sym.st_bind = STB_LOCAL;
      }
      else if (symp->n_desc & (MAC_N_WEAK_REF | MAC_N_WEAK_DEF)) {
         // Weak public or weak external
         sym.st_bind = STB_WEAK;
      }
      else {
         // Global (public or external)
         sym.st_bind = STB_GLOBAL;
      }

      // Symbol name
      if (symp->n_strx < this->StringTabSize) {
         name1 = oldstringtab + symp->n_strx;
      }
      else {
         err.submit(2112);  break;
      }
         
      // Symbol value
      sym.st_value = symp->n_value;
         
      // Get section
      OldSectionIndex = symp->n_sect;
      if (OldSectionIndex > this->NumSections) {
         err.submit(2016); break;
      }
      // Get new section index
      NewSectionIndex = 0;
      if (OldSectionIndex > 0) {
         // Get new section index from translation table
         NewSectionIndex = NewSectIndex[OldSectionIndex];
         // Change symbol address to section-relative
         // (Also in 64-bit mode)
         sym.st_value -= NewSectionHeaders[NewSectionIndex].sh_addr;
      }
      sym.st_shndx = (uint16_t)NewSectionIndex;

      if (OldSectionIndex && !NewSectionIndex) {
         // Section has been removed. Remove symbol also
         continue;
      }

      // Check symbol type
      int32_t RefType = symp->n_desc & MAC_REF_TYPE;
      if (RefType == MAC_REF_FLAG_UNDEFINED_LAZY || RefType == MAC_REF_FLAG_PRIVATE_UNDEFINED_LAZY) {
         // Lazy binding
         err.submit(1061, name1);
      }
      else if ((symp->n_type & MAC_N_TYPE) == MAC_N_ABS) {
         // Absolute symbol
         sym.st_type  = STT_NOTYPE;
         sym.st_shndx = (uint16_t)SHN_ABS;
         if (sym.st_bind == STB_LOCAL) {
            continue; // Remove absolute local symbol (not allowed in COFF)
         }
      }
      else if (sym.st_shndx == 0) {  // added by Vladimir 'phcoder' Serbinenko:
         // This is an external
         sym.st_type = STT_NOTYPE;
      }		  
      else {
         // This is a data definition record
         if (NewSectionHeaders[NewSectionIndex].sh_flags & SHF_EXECINSTR) {
            // Code section, assume this is a function
            sym.st_type = STT_FUNC;
         }
         else {
            // This is a data object
            sym.st_type = STT_OBJECT;
         }
         if (sym.st_bind == STB_GLOBAL && NewSectionIndex) {
            // Symbol is public
            // The size is not specified in Mac record,
            // so we may give it an arbitrary size:
            sym.st_size = 4;
         }
      }

      // Put symbol name into string table
      if (name1 && *name1) {
         sym.st_name = NewSections[strtab].PushString(name1);
      }

      // Put record into new symbol table
      NewSections[symtab].Push(&sym, sizeof(sym));

      // Insert into symbol translation table
      NewSymbolIndex[isym] = NewSections[symtab].GetLastIndex();

      // Make index to first global symbol
      if (isym >= this->iextdefsym && !NewSectionHeaders[symtab].sh_info) {
         // This is the first global symbol
         NewSectionHeaders[symtab].sh_info = NewSymbolIndex[isym];
      }
   }
}


template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt,
          class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CMAC2ELF<MACSTRUCTURES,ELFSTRUCTURES>::MakeRelocationTables(MAC_header_32&) {
   // Convert subfunction: Relocation tables, 32-bit version

   uint32_t oldsec;        // Relocated section number in source file
   uint32_t newsec;        // Relocated section number in destination file
   uint32_t newsecr;       // Relocation table section number in destination file
   MInt   SectAddr;      // Section address of relocation source
   MInt   SourceAddress; // Address of relocation source including section address
   MInt   TargetAddress; // Target address including section address
   uint32_t TargetSection; // New section index of relocation target
   uint32_t TargetOffset;  // Section-relative offset of relocation target
   uint32_t RefAddress;    // Reference point address including section address
   uint32_t RefSection;    // New section index of reference point
   uint32_t RefOffset;     // Section-relative offset of reference point
   int32_t * inlinep = 0;  // Pointer to inline addend
   //const int WordSize = sizeof(MInt) * 8;

   TELF_SectionHeader * NewRelTableSecHeader;  // Section header for new relocation table

   // Number of symbols
   uint32_t NumSymbols = NewSections[symtab].GetNumEntries();

   // New symbol table
   //Elf32_Sym * NewSymbolTable = (Elf32_Sym *)(NewSections[symtab].Buf());

   // Find first section header
   MAC_section_32 * sectp = (MAC_section_32*)(this->Buf() + this->SectionHeaderOffset);

   // Loop through section headers
   for (oldsec = 1; oldsec <= this->NumSections; oldsec++, sectp++) {

      if (sectp->nreloc > 0) {
         // Source section has relocations

         // New section index
         newsec = NewSectIndex[oldsec];

         // Check that section has not been deleted
         if (newsec > 0) {

            // Section address
            SectAddr = NewSectionHeaders[newsec].sh_addr;

            // Finc new relocation table section
            newsecr = newsec + 1;
            if (newsecr >= NewSectionHeaders.GetNumEntries()) {
               err.submit(9000); return;}

            // New relocation table section header
            NewRelTableSecHeader = &NewSectionHeaders[newsecr];

            // Check that we have allocated this as a relocation section
            if (NewRelTableSecHeader->sh_info != newsec) {
               err.submit(9000); return;
            }

            // Insert header info
            NewRelTableSecHeader->sh_type  = SHT_REL;
            NewRelTableSecHeader->sh_flags = 0;
            NewRelTableSecHeader->sh_addralign = 4;
            NewRelTableSecHeader->sh_link = symtab; // Point to symbol table
            NewRelTableSecHeader->sh_info = newsec; // Point to relocated section
            NewRelTableSecHeader->sh_entsize = sizeof(Elf32_Rel); // Entry size:

            // Pointer to old relocation entry
            if (sectp->reloff >= this->GetDataSize()) {err.submit(2035); break;}
            MAC_relocation_info * relp = (MAC_relocation_info*)(this->Buf() + sectp->reloff);

            // Loop through old relocations
            for (uint32_t oldr = 1; oldr <= sectp->nreloc; oldr++, relp++) {

               // Make new relocation entry and set to zero
               Elf32_Rel NewRelocEntry;

               memset(&NewRelocEntry, 0, sizeof(NewRelocEntry));

               if (relp->r_address & R_SCATTERED) {
                  // scattered relocation into
                  MAC_scattered_relocation_info * scatp = (MAC_scattered_relocation_info*)relp;

                  // Address of source
                  NewRelocEntry.r_offset = scatp->r_address;
                  if (NewRelocEntry.r_offset >= NewSections[newsec].GetDataSize()) {
                     err.submit(2035); continue; // Out of range
                  }
                  // Pointer to inline addend
                  inlinep = (int32_t*)(NewSections[newsec].Buf() + NewRelocEntry.r_offset);
                  if (scatp->r_pcrel) {
                     // Self-relative scattered
                     if (scatp->r_type != MAC32_RELOC_VANILLA) {
                        err.submit(2030, scatp->r_type); continue; // Unexpected type
                     }
                     // Scattered, self-relative, vanilla
                     // Note: I have never seen this relocation method, so I have not
                     // been able to test it. I don't know for sure how it works and 
                     // the documentation is poor.
                     SourceAddress = SectAddr + scatp->r_address;

                     // Target address
                     TargetAddress = SourceAddress + *inlinep;
                     TranslateAddress(TargetAddress, TargetSection, TargetOffset);
                     if (TargetSection == 0) {err.submit(2031); continue;} // not found
                     NewRelocEntry.r_sym = SectionSymbols[TargetSection];
                     if (NewRelocEntry.r_sym == 0) {
                        err.submit(2031); continue; // refers to non-program section
                     }

                     // inline contains full relative address
                     // compensate by subtracting relative address to target section
                     *inlinep -= int32_t(NewSectionHeaders[TargetSection].sh_addr - SourceAddress);
                     // Relocation type
                     NewRelocEntry.r_type = R_386_PC32;
                  }
                  else if (scatp->r_type == MAC32_RELOC_VANILLA) {
                     // Scattered, absolute
                     TargetAddress = *inlinep;
                     TranslateAddress(TargetAddress, TargetSection, TargetOffset);
                     if (TargetSection == 0) {
                        err.submit(2031); continue;} // Target not found
                     NewRelocEntry.r_sym = SectionSymbols[TargetSection];
                     *inlinep = TargetOffset;
                     NewRelocEntry.r_type = R_386_32;
                     if (scatp->r_length != 2) {
                        err.submit(2030, scatp->r_type); continue; // Only 32-bit supported
                     }
                  }
                  else if (scatp->r_type == MAC32_RELOC_SECTDIFF || scatp->r_type == MAC32_RELOC_LOCAL_SECTDIFF) {
                     // relative to arbitrary reference point
                     // check that next record is MAC32_RELOC_PAIR
                     if (oldr == sectp->nreloc || (scatp+1)->r_type != MAC32_RELOC_PAIR || scatp->r_length != 2) {                              
                        err.submit(2050); continue;
                     }
                     // Find target address and reference point
                     RefAddress = (scatp+1)->r_value;
                     TranslateAddress(RefAddress, RefSection, RefOffset);
                     TargetAddress = RefAddress + *inlinep;
                     TranslateAddress(TargetAddress, TargetSection, TargetOffset);
                     // Check that both points are found
                     if (RefSection == 0 || TargetSection == 0) {
                        err.submit(2031); oldr++; relp++; continue;
                     }
                     // Address relative to arbitrary reference point can be translated
                     // to self-relative address if reference point is in same section as source
                     if (RefSection != newsec) {
                        err.submit(2044); oldr++; relp++; continue;
                     }
                     // Translation is possible
                     // Get symbol for target section
                     NewRelocEntry.r_sym = SectionSymbols[TargetSection];
                     // Make self-relative relocation
                     NewRelocEntry.r_type = R_386_PC32;
                     // Calculate compensating addend
                     *inlinep = TargetOffset + scatp->r_address - RefOffset;
                     // Linker will add (target section) - (source full address) to *inlinep, which gives
                     // (target full address) - (reference point full address)
                     // Advance pointers because we have used two records
                     oldr++; relp++; 
                  }
                  else if (scatp->r_type == MAC32_RELOC_PB_LA_PTR) {
                     // procedure linkage table. Not supported
                     NewRelocEntry.r_type = R_386_PLT32;
                     err.submit(2043);
                  }
                  else {
                     // unknown scattered relocation type
                     err.submit(2030, scatp->r_type); continue;
                  }
               }
               else {
                  // Non scattered relocation info
                  // Section offset of relocated address
                  NewRelocEntry.r_offset = relp->r_address;
                  if (NewRelocEntry.r_offset >= NewSections[newsec].GetDataSize()) {
                     err.submit(2035); continue; // Out of range
                  }
                  // Pointer to inline addend
                  inlinep = (int32_t*)(NewSections[newsec].Buf() + NewRelocEntry.r_offset);

                  if (relp->r_extern) {
                     // r_extern = 1: target indicated by symbol index
                     uint32_t symold = relp->r_symbolnum;
                     if (symold >= this->SymTabNumber) {
                        err.submit(2031); continue; // index out of range
                     }
                     NewRelocEntry.r_sym = NewSymbolIndex[symold];
                     if (relp->r_pcrel) {
                        // Self-relative. 
                        // Inline contains -(source address)
                        // Add (source address) to compensate
                        *inlinep += int32_t(SectAddr + relp->r_address);
                     }
                  }
                  else {
                     // r_extern = 0. Target indicated by section + offset
                     // Old section number
                     uint32_t secold = relp->r_symbolnum;
                     if (secold > this->NumSections) {
                        err.submit(2031); continue; // index out of range
                     }
                     TargetSection = NewSectIndex[secold];
                     NewRelocEntry.r_sym = SectionSymbols[TargetSection];
                     if (NewRelocEntry.r_sym == 0 || NewRelocEntry.r_sym > NumSymbols) {
                        err.submit(2031); continue; // refers to non-program section
                     }
                     if (relp->r_pcrel) {
                        // Self-relative. 
                        // Inline contains (target address)-(source address)
                        // Subtract this to compensate

                        // Target section address
                        TargetOffset = uint32_t(NewSectionHeaders[TargetSection].sh_addr);
                        SourceAddress = SectAddr + relp->r_address;
                        *inlinep -= int32_t(TargetOffset - SourceAddress);
                     }
                     else {
                        // Absolute reference
                        // Inline contains target address, convert to section:offset address
                        TranslateAddress(*inlinep, TargetSection, TargetOffset);
                        if (TargetSection == 0) { // Target not found
                           err.submit(2035); continue;
                        }
                        // Translate to section-relative address by subtracting target section address
                        *inlinep -= int32_t(NewSectionHeaders[TargetSection].sh_addr);
                     }
                  }
                  // relocation type (32-bit non-scattered)
                  switch (relp->r_type) {
                  case MAC32_RELOC_VANILLA:
                     // Normal relocation
                     if (relp->r_pcrel) { // self relative
                        NewRelocEntry.r_type = R_386_PC32;
                     }
                     else { // direct
                        NewRelocEntry.r_type = R_386_32;
                     }
                     break;
                  default:
                     err.submit(2030, relp->r_type); // unknown type
                     continue;
                  }
                  // size
                  if (relp->r_length != 2) { // wrong size
                     err.submit(2030,relp->r_type);
                  }
               }
               // Put relocation record into table
               NewSections[newsecr].Push(&NewRelocEntry, sizeof(NewRelocEntry));
            }
         }
      }
   }
}


template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt,
          class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CMAC2ELF<MACSTRUCTURES,ELFSTRUCTURES>::MakeRelocationTables(MAC_header_64&) {
   // Convert subfunction: Relocation tables, 64-bit version

   uint32_t oldsec;        // Relocated section number in source file
   uint32_t newsec;        // Relocated section number in destination file
   uint32_t newsecr;       // Relocation table section number in destination file
   uint32_t symold;        // Old index of symbol
   uint32_t TargetSym;     // Target symbol
   uint32_t TargetSection; // New section index of relocation target
   uint32_t RefSym;        // Reference symbol
   uint32_t RefSection;    // New section index of reference point
   int64_t  RefOffset;     // Section-relative offset of reference point
   int64_t  SectAddr;      // Address of current section
   //const int WordSize = sizeof(MInt) * 8;  // Word size, 32 or 64 bits

   TELF_SectionHeader * NewRelTableSecHeader;  // Section header for new relocation table

   // Number of symbols
   //uint32_t NumSymbols = NewSections[symtab].GetNumEntries();

   // New symbol table
   Elf64_Sym * NewSymbolTable = (Elf64_Sym *)(NewSections[symtab].Buf());

   // Find first section header
   MAC_section_64 * sectp = (MAC_section_64*)(this->Buf() + this->SectionHeaderOffset);

   // Loop through section headers
   for (oldsec = 1; oldsec <= this->NumSections; oldsec++, sectp++) {

      if (sectp->nreloc > 0) {
         // Source section has relocations

         // New section index
         newsec = NewSectIndex[oldsec];

         // Check that section has not been deleted
         if (newsec > 0) {

            // Section address
            SectAddr = NewSectionHeaders[newsec].sh_addr;

            // Finc new relocation table section
            newsecr = newsec + 1;
            if (newsecr > NewSectionHeaders.GetNumEntries()) {
               err.submit(9000); return;
            }

            // New relocation table section header
            NewRelTableSecHeader = &NewSectionHeaders[newsecr];

            // Check that we have allocated this as a relocation section
            if (NewRelTableSecHeader->sh_info != newsec) {
               err.submit(9000); return;
            }

            // Insert header info
            NewRelTableSecHeader->sh_type  = SHT_RELA;
            NewRelTableSecHeader->sh_flags = 0;
            NewRelTableSecHeader->sh_addralign = 8;
            NewRelTableSecHeader->sh_link = symtab; // Point to symbol table
            NewRelTableSecHeader->sh_info = newsec; // Point to relocated section
            // Entry size:
            NewRelTableSecHeader->sh_entsize = sizeof(Elf64_Rela);

            // Pointer to old relocation entry
            if (sectp->reloff >= this->GetDataSize()) {err.submit(2035); break;}
            MAC_relocation_info * relp = (MAC_relocation_info*)(this->Buf() + sectp->reloff);

            // Loop through old relocations
            for (uint32_t oldr = 1; oldr <= sectp->nreloc; oldr++, relp++) {

               // Make new relocation entry and set to zero
               Elf64_Rela NewRelocEntry;
               memset(&NewRelocEntry, 0, sizeof(NewRelocEntry));

               // Pointer to inline addend
               int32_t * inlinep = 0;

               if (relp->r_address & R_SCATTERED) {
                  // scattered not allowed in 64-bit
                  err.submit(2030, ((MAC_scattered_relocation_info*)relp)->r_type); continue;
               }
               else {
                  // Non scattered relocation info
                  // Section offset of relocated address
                  NewRelocEntry.r_offset = relp->r_address;
                  if (NewRelocEntry.r_offset >= NewSections[newsec].GetDataSize()) {
                     err.submit(2035); continue; // Out of range
                  }
                  // Pointer to inline addend
                  inlinep = (int32_t*)(NewSections[newsec].Buf() + NewRelocEntry.r_offset);

                  // Symbol index of target
                  symold = relp->r_symbolnum;
                  if (relp->r_extern) {
                     if (symold >= this->SymTabNumber) {
                        err.submit(2031); continue; // index out of range
                     }
                     NewRelocEntry.r_sym = NewSymbolIndex[symold];
                  }
                  else {
                     // r_extern = 0, r_symbolnum = section
                     if (symold > NumSectionsNew) {err.submit(2031); continue;}
                     TargetSection = NewSectIndex[symold];
                     NewRelocEntry.r_sym = SectionSymbols[TargetSection];
                     if (relp->r_pcrel) {
                        // Self-relative. 
                        // Inline contains (target address)-(source address)
                        // Subtract this to compensate
                        // Target section address
                        uint64_t TargetSectAddr = NewSectionHeaders[TargetSection].sh_addr;
                        uint64_t SourceAddress = SectAddr + relp->r_address;
                        *inlinep -= int32_t(TargetSectAddr - SourceAddress);
                        *inlinep += 4; // Compensate for subtracting 4 below
                     }
                  }

                  // Find relocation type
                  switch (relp->r_type) {
                  case MAC64_RELOC_UNSIGNED: // absolute address, 32 or 64 bits
                     if (relp->r_length == 2) {
                        NewRelocEntry.r_type = R_X86_64_32S; // 32 bit signed
                     }
                     else if (relp->r_length == 3) {
                        NewRelocEntry.r_type = R_X86_64_64;  // 64 bit
                     }
                     else {
                        err.submit(2030,relp->r_type); continue;
                     }
                     break;
                  case MAC64_RELOC_SIGNED:   // rip-relative, implicit addend = -4
                  case MAC64_RELOC_BRANCH:   // rip-relative, implicit addend = -4
                  case MAC64_RELOC_SIGNED_1: // implicit addend = -4, not -5
                  case MAC64_RELOC_SIGNED_2: // implicit addend = -4, not -6
                  case MAC64_RELOC_SIGNED_4: // implicit addend = -4, not -8
                     // These are all the same:
                     // signed 32-bit rip-relative with implicit -4 addend
                     if (relp->r_length != 2) { // wrong size
                        err.submit(2030,relp->r_type); continue;
                     }
                     NewRelocEntry.r_type = R_X86_64_PC32;
                     // ELF = self-relative, Mac64 = rip-relative. Compensate for difference
                     *inlinep -= 4;
                     break;
                  case MAC64_RELOC_SUBTRACTOR:
                     // relative to arbitrary reference point
                     // must be followed by a X86_64_RELOC_UNSIGNED
                     // check that next record is MAC64_RELOC_UNSIGNED
                     if (oldr == sectp->nreloc || (relp+1)->r_type != MAC64_RELOC_UNSIGNED) {                              
                        err.submit(2050); continue;
                     }
                     // Reference symbol
                     RefSym     = NewRelocEntry.r_sym;
                     RefSection = NewSymbolTable[RefSym].st_shndx;
                     RefOffset  = NewSymbolTable[RefSym].st_value;

                     // Target symbol
                     symold = (relp+1)->r_symbolnum;
                     if (symold >= this->SymTabNumber) {
                        err.submit(2031); continue; // index out of range
                     }
                     TargetSym = NewSymbolIndex[symold];
                     NewRelocEntry.r_sym = TargetSym;

                     // Address relative to arbitrary reference point can be translated
                     // to self-relative address if reference point is in same section as source
                     if (RefSection != newsec) {
                        err.submit(2044); oldr++; relp++; continue;
                     }
                     if (relp->r_length == 2) {
                        *inlinep += int32_t(NewRelocEntry.r_offset) - int32_t(RefOffset);
                     }
                     else if (relp->r_length == 3) {
                        *(int64_t*)inlinep += NewRelocEntry.r_offset - RefOffset;
                        // there is no 64-bit self-relative relocation in ELF,
                        // use 32-bit self-relative and hope there is no carry
                        err.submit(1302); // Warn. This will fail if inline value changes sign
                     }
                     else {                              
                        err.submit(2044);  // wrong size
                     }
                     // self-relative type
                     NewRelocEntry.r_type = R_X86_64_PC32;

                     // increment counters because we used two records
                     relp++; oldr++;
                     break;

                  case MAC64_RELOC_GOT_LOAD: // a rip-relative load of a GOT entry
                     *inlinep = -4;  
                     // Continue into next case
                  case MAC64_RELOC_GOT:      // other GOT references
                     // Make fake GOT entry
                     //NewRelocEntry.r_addend = MakeGOTEntry(NewRelocEntry.r_sym) - 4;
                     *inlinep += MakeGOTEntry(NewRelocEntry.r_sym);
                     NewRelocEntry.r_sym = FakeGOTSymbol;
                     NewRelocEntry.r_type = R_X86_64_PC32;
                     break;
                  }
               }

               // Put relocation record into table
               NewSections[newsecr].Push(&NewRelocEntry, sizeof(NewRelocEntry));
            }
         }
      }
   }
}


template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt,
          class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CMAC2ELF<MACSTRUCTURES,ELFSTRUCTURES>::MakeBinaryFile() {
   // Convert subfunction: Make section headers and file header,
   // and combine everything into a single memory buffer.
   uint32_t newsec;              // Section index
   uint32_t SecOffset;           // Section offset in file
   uint32_t SecSize;             // Section size in file
   uint32_t SectionHeaderOffset; // File offset to section headers
   const int WordSize = sizeof(MInt) * 8;

   // Set file type in ToFile
   ToFile.SetFileType(FILETYPE_ELF);
   
   // Make space for file header in ToFile, but don't fill data into it yet
   ToFile.Push(0, sizeof(TELF_Header));

   // Loop through new section buffers
   for (newsec = 0; newsec < NumSectionsNew; newsec++) {

      // Size of section
      SecSize = NewSections[newsec].GetDataSize();

      // Put section into ToFile
      SecOffset = ToFile.Push(NewSections[newsec].Buf(), SecSize);

      // Put size and offset into section header
      NewSectionHeaders[newsec].sh_offset = SecOffset;
      if (SecSize) { // Don't set size of BSS sections to zero
         NewSectionHeaders[newsec].sh_size = SecSize;
      }

      // Align before next entry
      ToFile.Align(16);
   }

   // Start offset of section headers
   SectionHeaderOffset = ToFile.GetDataSize();

   // Loop through new section headers
   for (newsec = 0; newsec < NumSectionsNew; newsec++) {

      // Put section header into ToFile
      ToFile.Push(&NewSectionHeaders[newsec], sizeof(TELF_SectionHeader));
   }

   // Make file header
   TELF_Header FileHeader;
   memset(&FileHeader, 0, sizeof(FileHeader)); // Initialize to 0

   // Put file type magic number in
   strcpy((char*)(FileHeader.e_ident), ELFMAG);
   // File class
   FileHeader.e_ident[EI_CLASS] = (WordSize == 32) ? ELFCLASS32 : ELFCLASS64;
   // Data Endian-ness
   FileHeader.e_ident[EI_DATA] = ELFDATA2LSB;
   // ELF version
   FileHeader.e_ident[EI_VERSION] = EV_CURRENT;
   // ABI
   FileHeader.e_ident[EI_OSABI] = ELFOSABI_SYSV;
   // ABI version
   FileHeader.e_ident[EI_ABIVERSION] = 0;
   // File type
   FileHeader.e_type = ET_REL;
   // Machine architecture
   FileHeader.e_machine = (WordSize == 32) ? EM_386 : EM_X86_64;
   // Version
   FileHeader.e_version = EV_CURRENT;
   // Flags
   FileHeader.e_flags = 0;

   // Section header table offset
   FileHeader.e_shoff = SectionHeaderOffset;

   // File header size
   FileHeader.e_ehsize = sizeof(TELF_Header);

   // Section header size
   FileHeader.e_shentsize = sizeof(TELF_SectionHeader);

   // Number of section headers
   FileHeader.e_shnum = (uint16_t)NumSectionsNew;

   // Section header string table index
   FileHeader.e_shstrndx = (uint16_t)shstrtab;

   // Put file header into beginning of ToFile where we made space for it
   memcpy(ToFile.Buf(), &FileHeader, sizeof(FileHeader));
}

template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt,
          class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CMAC2ELF<MACSTRUCTURES,ELFSTRUCTURES>::MakeImportTables() {
   // Convert subfunction: Fill import tables
   uint32_t oldsec;            // Old section number
   uint32_t Type;              // Old section type
   uint32_t NumEntries;        // Number of entries in import table
   uint32_t EntrySize;         // Entry size of import table
   uint32_t NewSec1;           // New section number of import table
   uint32_t NewSec2;           // New section number of relocation for import table
   uint32_t Offset;            // Offset of relocation source
   uint32_t OldSymbol;         // Old symbol number of import
   uint32_t i;                 // Loop counter
   uint32_t * IndSymTab;       // Pointer to indirect symbol table
   uint32_t IndSymi;           // Index into indirect symbol table
   uint32_t IndSymNum;         // Number of entries in indirect symbol table
   TELF_Relocation NewRelocEntry; // New relocation entry
   const int WordSize = sizeof(MInt) * 8;

   // Machine code of jmp instruction
   static const int8_t JmpInstruction[5] = {int8_t(0xE9), int8_t(0xFC), int8_t(0xFF), int8_t(0xFF), int8_t(0xFF)};

   // Number of indirect symbols
   IndSymNum = this->IndirectSymTabNumber;
   if (IndSymNum == 0) {
      return; // No indirect symbols
   }
   // Find indirect symbol table
   IndSymTab = (uint32_t*)(this->Buf() + this->IndirectSymTabOffset);

   // Find first section header
   TMAC_section * sectp = (TMAC_section*)(this->Buf() + this->SectionHeaderOffset);

   // Loop through section headers
   for (oldsec = 1; oldsec <= this->NumSections; oldsec++, sectp++) {
      // Search for import tables
      Type = sectp->flags & MAC_SECTION_TYPE;
      if (Type >= MAC_S_NON_LAZY_SYMBOL_POINTERS && Type <= MAC_S_SYMBOL_STUBS) {
         // This is an import table

         // Indirect symbol table first entry
         IndSymi = sectp->reserved1;

         // Entry size:
         EntrySize = sectp->reserved2;
         if (EntrySize == 0) EntrySize = WordSize / 8;

         // Find new section
         NewSec1 = NewSectIndex[oldsec];
         NumEntries = uint32_t(NewSectionHeaders[NewSec1].sh_size) / EntrySize;

         // Find new relocation section
         NewSec2 = NewSec1 + 1;
         if (NewSectionHeaders[NewSec2].sh_type != SHT_REL && NewSectionHeaders[NewSec2].sh_type != SHT_RELA) {
            err.submit(9000); // This should be a relocation section
         }
         NewSectionHeaders[NewSec2].sh_link = symtab; // Point to symbol table

         // Offset of first relocation
         Offset = EntrySize & 1; // 1 if EntrySize = 5, otherwise 0

         // Loop through entries
         for (i = 0; i < NumEntries; i++, Offset += EntrySize) {
            // Find symbol
            if (IndSymi >= IndSymNum) {
               err.submit(1303); break; // Import symbol table exhausted
            }
            OldSymbol = IndSymTab[IndSymi];
            if (OldSymbol >= this->SymTabNumber) {
               err.submit(1052); break;
            }
            // Increment pointer to import symbol table
            IndSymi++;

            // Make relocation record
            memset(&NewRelocEntry, 0, sizeof(NewRelocEntry));
            NewRelocEntry.r_offset = Offset;
            if (WordSize == 32) {
               if (EntrySize == 4) {
                  NewRelocEntry.r_type = R_386_32;
               }
               else if (EntrySize == 5) {
                  NewRelocEntry.r_type = R_386_PC32;
               }
               else {
                  err.submit(2045);
               }
            }
            else { // 64 bit
               if (EntrySize == 8) {
                  NewRelocEntry.r_type = R_X86_64_64;
               }
               else if (EntrySize == 5) {
                  NewRelocEntry.r_type = R_X86_64_PC32;
               }
               else {
                  err.submit(2045);
               }
            }
            NewRelocEntry.r_sym = NewSymbolIndex[OldSymbol];

            // Store relocation record
            NewSections[NewSec2].Push(&NewRelocEntry, (WordSize==32) ? sizeof(Elf32_Rel) : sizeof(Elf64_Rela));

            // Insert jmp instruction if EntrySize = 5
            if (EntrySize == 5) {
               if (Offset -1 + EntrySize > NewSections[NewSec1].GetDataSize()) {
                  err.submit(9000); // Outside section
               }
               memcpy(NewSections[NewSec1].Buf()+Offset-1, JmpInstruction, 5);
            }
         }
      }
   }
}


template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt,
          class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CMAC2ELF<MACSTRUCTURES,ELFSTRUCTURES>::TranslateAddress(MInt addr, uint32_t & section, uint32_t & offset) {
   // Translate 32-bit address to section + offset
   // (Sections are not necessarily ordered by address)
   uint32_t sec;
   MInt secstart;
   for (sec = 1; sec < NumSectionsNew; sec++) {
      secstart = NewSectionHeaders[sec].sh_addr;
      if (addr >= secstart && addr < secstart + MInt(NewSectionHeaders[sec].sh_size)) {
         // Section found
         section = sec;
         offset = uint32_t(addr - secstart);
         return;
      }
   }
   // Not found
   section = offset = 0; 
}

template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt,
          class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
uint32_t CMAC2ELF<MACSTRUCTURES,ELFSTRUCTURES>::MakeGOTEntry(int symbol) {
   // Make entry in fake GOT for symbol
   uint32_t NumGOTEntries = GOTSymbols.GetNumEntries();
   uint32_t symi;  // Symbol index
   const int WordSize = sizeof(MInt) * 8;

   // Get symbol for start of GOT
   FakeGOTSymbol = SectionSymbols[FakeGOTSection];
   
   // Search for symbol in previous entries
   for (symi = 0; symi < NumGOTEntries; symi++) {
      if (GOTSymbols[symi] == symbol) break;
   }
   if (symi == NumGOTEntries) {
      // Not found. Make new entry
      GOTSymbols.Push(symbol);
   }
   return symi * (WordSize / 8);
}


template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt,
          class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CMAC2ELF<MACSTRUCTURES,ELFSTRUCTURES>::MakeGOT() {
   // Make fake Global Offset Table
   const int WordSize = sizeof(MInt) * 8;
   if (!HasGOT) return;

   uint32_t NumEntries = GOTSymbols.GetNumEntries();
   NewSections[FakeGOTSection].Push(0, NumEntries*(WordSize/8)); 

   // Make relocations for GOT
   Elf64_Rela NewRelocEntry;
   memset(&NewRelocEntry, 0, sizeof(NewRelocEntry));

   for (uint32_t i = 0; i < NumEntries; i++) {
      NewRelocEntry.r_offset = i * (WordSize/8);
      NewRelocEntry.r_sym = GOTSymbols[i];
      NewRelocEntry.r_type = R_X86_64_64;
      NewSections[FakeGOTSection+1].Push(&NewRelocEntry, sizeof(NewRelocEntry));
   }
}


// Make template instances for 32 and 64 bits
template class CMAC2ELF<MAC32STRUCTURES,ELF32STRUCTURES>;
template class CMAC2ELF<MAC64STRUCTURES,ELF64STRUCTURES>;