File: cof2elf.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 (826 lines) | stat: -rw-r--r-- 35,903 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
/****************************  cof2elf.cpp   ********************************
* Author:        Agner Fog
* Date created:  2006-07-20
* Last modified: 2022-05-21
* Project:       objconv
* Module:        cof2elf.cpp
* Description:
* Module for converting PE/COFF file to ELF file
*
* Copyright 2006-2022 GNU General Public License http://www.gnu.org/licenses
*****************************************************************************/
#include "stdafx.h"


template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
CCOF2ELF<ELFSTRUCTURES>::CCOF2ELF () {
   // Constructor
   memset(this, 0, sizeof(*this));
}


template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CCOF2ELF<ELFSTRUCTURES>::Convert() {
   // Do the conversion
   NumSectionsNew = 5;                                    // Number of sections generated so far

   // Allocate variable size buffers
   MaxSectionsNew    = NumSectionsNew + 2 * NSections;    // Max number of sections needed
   NewSections.SetNum(MaxSectionsNew);                    // Allocate buffers for each section
   NewSections.SetZero();                                 // Initialize
   NewSectionHeaders.SetNum(MaxSectionsNew);              // Allocate array for temporary section headers
   NewSectionHeaders.SetZero();                           // Initialize
   NewSectIndex.SetNum(NSections);                        // Array for translating old section index (0-based) to new section index
   NewSectIndex.SetZero();                                // Initialize
   NewSymbolIndex.SetNum(NumberOfSymbols);                // 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();             // Relocation tables
   MakeBinaryFile();                   // Putting sections together
   *this << ToFile;                    // Take over new file buffer
}


template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CCOF2ELF<ELFSTRUCTURES>::MakeSegments() {
   // Convert subfunction: Make segment headers and code/data segments
   TELF_SectionHeader NewSecHeader;    // New section header
   int oldsec;                         // Section index in old file
   int newsec;                         // Section index in new file
   uint32_t SecNameIndex;                // Section name index into shstrtab
   char const * SecName;               // Name of new section
   const int WordSize = sizeof(NewFileHeader.e_entry) * 8; // word size 32 or 64 bits

   // 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 int 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 CCOF2ELF::CCOF2ELF to equal the number of entries in 
      // SpecialSegmentNames, including the Null segment
      err.submit(9000);
   }

   // Loop through source file sections
   for (oldsec = 0; oldsec < this->NSections; oldsec++) {

      // Pointer to old section header
      SCOFF_SectionHeader * SectionHeader = &this->SectionHeaders[oldsec];

      // Get section name
      SecName = this->GetSectionName(SectionHeader->Name);
      if (strnicmp(SecName,"debug",5) == 0 || strnicmp(SecName+1,"debug",5) == 0) {
         // This is a debug section
         if (cmd.DebugInfo == CMDL_DEBUG_STRIP) {
            // Remove debug info
            NewSectIndex[oldsec] = COFF_SECTION_REMOVE_ME;  // Remember that this section is removed
            cmd.CountDebugRemoved();
            continue;
         }
         else if (cmd.InputType != cmd.OutputType) {
            err.submit(1029); // Warn that debug information is incompatible
         }
      }
      if (strnicmp(SecName,".drectve",8) == 0 || (SectionHeader->Flags & (PE_SCN_LNK_INFO | PE_SCN_LNK_REMOVE))) {
         // This is a directive section
         if (cmd.ExeptionInfo) {
            // Remove directive section
            NewSectIndex[oldsec] = COFF_SECTION_REMOVE_ME;  // Remember that this section is removed
            cmd.CountExceptionRemoved();
            continue;
         }
      }
      if (strnicmp(SecName,".pdata", 6) == 0) {
         // This section has exception information
         if (cmd.ExeptionInfo == CMDL_EXCEPTION_STRIP) {
            // Remove exception info
            NewSectIndex[oldsec] = COFF_SECTION_REMOVE_ME;  // Remember that this section is removed
            cmd.CountExceptionRemoved();
            continue;
         }
         else if (cmd.InputType != cmd.OutputType) {
            err.submit(1030); // Warn that exception information is incompatible
         }
      }
      
      if (strnicmp(SecName,".cormeta", 8) == 0) {
         // This is a .NET Common Language Runtime section
         err.submit(2014);
      }
      if (strnicmp(SecName,".rsrc", 5) == 0) {
         // This section has Windows resource information
         err.submit(1031);
      }

      // Store section index in index translation table (zero-based index)
      NewSectIndex[oldsec] = newsec;

      // Store section data
      if (SectionHeader->SizeOfRawData > 0) {
         NewSections[newsec].Push(Buf()+SectionHeader->PRawData, SectionHeader->SizeOfRawData);
      }

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

      // Section type
      if (!(SectionHeader->Flags & PE_SCN_LNK_REMOVE)) {
         NewSecHeader.sh_type = SHT_PROGBITS;  // Program code or data
         NewSecHeader.sh_flags |= SHF_ALLOC;   // Occupies memory during execution
      }
      if (SectionHeader->Flags & PE_SCN_CNT_UNINIT_DATA) {
         NewSecHeader.sh_type = SHT_NOBITS;    // BSS
      }

      // Section flags
      if (SectionHeader->Flags & PE_SCN_MEM_WRITE) {
         NewSecHeader.sh_flags |= SHF_WRITE;
      }
      if (SectionHeader->Flags & PE_SCN_MEM_EXECUTE) {
         NewSecHeader.sh_flags |= SHF_EXECINSTR;
      }

      // Check for special sections
      if (strcmp(SecName, COFF_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 = SectionHeader->VirtualAddress;

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

      // Section alignment
      if (SectionHeader->Flags & PE_SCN_ALIGN_MASK) {
         NewSecHeader.sh_addralign = uint32_t(1 << (((SectionHeader->Flags & PE_SCN_ALIGN_MASK) / PE_SCN_ALIGN_1) - 1));
      }

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

      // Increment section number
      newsec++;

      if (SectionHeader->NRelocations > 0) {
         // 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
         const int MAXSECTIONNAMELENGTH = 256;
         char RelocationSectionName[MAXSECTIONNAMELENGTH] = ".rel";
         if (WordSize == 64) strcat(RelocationSectionName, "a"); // 32-bit: .rel, 64-bit: .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

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

         // Increment section number
         newsec++;
      }
   }
   // Number of sections generated
   NumSectionsNew = newsec;
}


template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CCOF2ELF<ELFSTRUCTURES>::MakeSymbolTable() {
   // Convert subfunction: Make symbol table and string tables
   int isym;                           // current symbol table entry
   int numaux;                         // Number of auxiliary entries in source record
   int OldSectionIndex;                // Index into old section table. 1-based
   int NewSectionIndex;                // Index into new section table. 0-based
   //const int WordSize = sizeof(NewFileHeader.e_entry) * 8; // word size 32 or 64 bits

   TELF_Symbol sym;                    // Temporary symbol table record
   const char * name1;                 // Name of section or main record

   // Pointer to old symbol table
   union {
      SCOFF_SymTableEntry * p;         // Symtab entry pointer
      int8_t * b;                        // Used for increment
   } OldSymtab;

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

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

   // Loop twice through source symbol table to get local symbols first, global symbols last
   // Loop 1: Look for local symbols only
   OldSymtab.p = SymbolTable; // Pointer to source symbol table
   for (isym = 0; isym < this->NumberOfSymbols; isym += numaux+1, OldSymtab.b += SIZE_SCOFF_SymTableEntry*(numaux+1)) {

      if (OldSymtab.b >= Buf() + DataSize) {
         err.submit(2040);
         break;
      }

      // Number of auxiliary records belonging to same symbol
      numaux = OldSymtab.p->s.NumAuxSymbols;  if (numaux < 0) numaux = 0;

      if (OldSymtab.p->s.StorageClass != COFF_CLASS_EXTERNAL && OldSymtab.p->s.StorageClass != COFF_CLASS_WEAK_EXTERNAL) {
         // Symbol is local

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

         // Binding
         sym.st_bind = STB_LOCAL;

         // Get first aux record if numaux > 0
         //SCOFF_SymTableEntryAux * sa = (SCOFF_SymTableEntryAux *)(OldSymtab.b + SIZE_SCOFF_SymTableEntry);

         // Symbol name
         name1 = this->GetSymbolName(OldSymtab.p->s.Name);

         // Symbol value
         sym.st_value = OldSymtab.p->s.Value;

         // Get section
         OldSectionIndex = OldSymtab.p->s.SectionNumber;  // 1-based index into old section table
         NewSectionIndex = 0;                 // 0-based index into old section table
         if (OldSectionIndex > 0 && OldSectionIndex <= this->NSections) {
            // Subtract 1 from OldSectionIndex because NewSectIndex[] is zero-based while OldSectionIndex is 1-based
            // Get new section index from translation table
            NewSectionIndex = NewSectIndex[OldSectionIndex-1]; 
         }
         if (NewSectionIndex == COFF_SECTION_REMOVE_ME) {
            continue; // Section has been removed. Remove symbol too
         }

         sym.st_shndx = (uint16_t)NewSectionIndex;

         // Check symbol type
         if (OldSymtab.p->s.StorageClass == COFF_CLASS_FILE) {
            // This is a filename record
            if (numaux > 0 && numaux < 20) {
               // Get filename from subsequent Aux records.
               // Remove path from filename because the path makes no sense on a different platform.
               const char * filename = GetShortFileName(OldSymtab.p);
               // Put file name into string table and debug string table
               sym.st_name = NewSections[strtab].PushString(filename);
               NewSections[stabstr].PushString(filename);
            }
            // Attributes for filename record
            sym.st_shndx  = (uint16_t)SHN_ABS;
            sym.st_type   = STT_FILE;
            sym.st_bind   = STB_LOCAL;
            sym.st_value  = 0;
         }
         else if (numaux && OldSymtab.p->s.StorageClass == COFF_CLASS_STATIC
         && OldSymtab.p->s.Value == 0 && OldSymtab.p->s.Type != 0x20) {
            // This is a section definition record
            sym.st_name  = 0;  name1 = 0;
            sym.st_type  = STT_SECTION;
            sym.st_bind  = STB_LOCAL;
            sym.st_value = 0;
            // aux record contains length and number of relocations. Ignore aux record
         }
         else if (OldSymtab.p->s.SectionNumber < 0) {
            // This is an absolute or debug symbol
            sym.st_type  = STT_NOTYPE;
            sym.st_shndx = (uint16_t)SHN_ABS;
         }
         else if (OldSymtab.p->s.Type == 0 && OldSymtab.p->s.StorageClass == COFF_CLASS_FUNCTION) {
            // This is a .bf, .lf, or .ef record following a function record
            // Contains line number information etc. Ignore this record
            continue;
         }
         else if (OldSymtab.p->s.SectionNumber <= 0) {
            // Unknown
            sym.st_type = STT_NOTYPE;
         }
         else {
            // This is a local data definition record
            sym.st_type = STT_OBJECT;
            // The size is not specified in COFF record,
            // so we may give it an arbitrary size:
            // sym.size = 4;
         }

         // Put symbol name into string table if we have not already done so
         if (sym.st_name == 0 && 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();

      } // End if not external
   }  // End loop 1

   // Finished with local symbols
   // Make index to first global symbol
   NewSectionHeaders[symtab].sh_info = NewSections[symtab].GetLastIndex() + 1;

   // Loop 2: Look for global symbols only
   OldSymtab.p = SymbolTable; // Pointer to source symbol table
   for (isym = 0; isym < NumberOfSymbols; isym += numaux+1, OldSymtab.b += SIZE_SCOFF_SymTableEntry*(numaux+1)) {

      // Number of auxiliary records belonging to same symbol
      numaux = OldSymtab.p->s.NumAuxSymbols;  if (numaux < 0) numaux = 0;

      if (OldSymtab.p->s.StorageClass == COFF_CLASS_EXTERNAL || OldSymtab.p->s.StorageClass == COFF_CLASS_WEAK_EXTERNAL) {
         // Symbol is global (public or external)

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

         // Binding
         sym.st_bind = STB_GLOBAL;
         if (OldSymtab.p->s.StorageClass == COFF_CLASS_WEAK_EXTERNAL) sym.st_bind = STB_WEAK;

         // Get first aux record if numaux > 0
         SCOFF_SymTableEntry * sa = (SCOFF_SymTableEntry*)(OldSymtab.b + SIZE_SCOFF_SymTableEntry);

         // Symbol name
         name1 = GetSymbolName(OldSymtab.p->s.Name);

         // Symbol value
         sym.st_value = OldSymtab.p->s.Value;

         // Get section
         OldSectionIndex = OldSymtab.p->s.SectionNumber; // 1-based index into old section table
         NewSectionIndex = 0;                          // 0-based index into old section table
         if (OldSectionIndex > 0 && OldSectionIndex <= NSections) {
            // Subtract 1 from OldSectionIndex because NewSectIndex[] is zero-based while OldSectionIndex is 1-based
            // Get new section index from translation table
            NewSectionIndex = NewSectIndex[OldSectionIndex-1]; 
         }
         if (NewSectionIndex == COFF_SECTION_REMOVE_ME) {
            continue; // Section has been removed. Remove symbol too
         }
         if ((int16_t)OldSectionIndex == COFF_SECTION_ABSOLUTE) {
            NewSectionIndex = SHN_ABS;
         }

         sym.st_shndx = (uint16_t)NewSectionIndex;

         // Check symbol type
         if (OldSymtab.p->s.SectionNumber < 0) {
            // This is an absolute or debug symbol
            sym.st_type = STT_NOTYPE;
         }
         else if (OldSymtab.p->s.Type == COFF_TYPE_FUNCTION && OldSymtab.p->s.SectionNumber > 0) {
            // This is a function definition record
            sym.st_type = STT_FUNC;
            if (numaux) {
               // Get size from aux record
               sym.st_size = sa->func.TotalSize;
            }
            if (sym.st_size == 0) {
               // The size is not specified in the COFF file. 
               // We may give it an arbitrary size:
               // sym.size = 1;
            }
         }
         else if (OldSymtab.p->s.SectionNumber <= 0) {
            // This is an external symbol
            sym.st_type = STT_NOTYPE;
         }
         else {
            // This is a data definition record
            sym.st_type = STT_OBJECT;
            // Symbol must have a size. The size is not specified in COFF record,
            // so we just give it an arbitrary size
            sym.st_size = 4;
         }

         // Put symbol name into string table if we have not already done so
         if (sym.st_name == 0 && 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();

      } // End if external
   }  // End loop 2
}


template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CCOF2ELF<ELFSTRUCTURES>::MakeRelocationTables() {
   // Convert subfunction: Relocation tables
   int32_t oldsec;                                 // Relocated section number in source file
   int32_t newsec;                                 // Relocated section number in destination file
   int32_t newsecr;                                // Relocation table section number in destination file
   TELF_SectionHeader * NewRelTableSecHeader;    // Section header for new relocation table
   char TempText[32];                            // Temporary text buffer
   const int WordSize = sizeof(NewFileHeader.e_entry) * 8; // word size 32 or 64 bits

   // Loop through source file sections
   for (oldsec = 0; oldsec < NSections; oldsec++) {

      // New section index
      newsec = NewSectIndex[oldsec];
      if (newsec == COFF_SECTION_REMOVE_ME) {
         continue;   // This is a debug or exception handler section which has been removed
      }

      // Pointer to old section header
      SCOFF_SectionHeader * SectionHeader = &this->SectionHeaders[oldsec];

      if (SectionHeader->NRelocations > 0) {
         // This section has relocations

         // Finc new relocation table section
         newsecr = newsec + 1;

         // Check that we have allocated a relocation section
         if (oldsec+1 < this->NSections && NewSectIndex[oldsec+1] == newsecr) err.submit(9000);
         if (newsecr >= NumSectionsNew) err.submit(9000);

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

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

         // Pointer to old relocation entry
         union {
            SCOFF_Relocation * p;  // pointer to record
            int8_t * b;              // used for address calculation and incrementing
         } OldReloc;

         // Loop through relocations

         OldReloc.b = Buf() + SectionHeader->PRelocations;
         for (int i = 0; i < SectionHeader->NRelocations; i++, OldReloc.b += SIZE_SCOFF_Relocation) {

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

            // Section offset of relocated address
            NewRelocEntry.r_offset = OldReloc.p->VirtualAddress;

            // Target symbol
            uint32_t TargetSymbol = OldReloc.p->SymbolTableIndex;
            if (TargetSymbol >= (uint32_t)NumberOfSymbols) {
               err.submit(2031);  // Symbol not in table
            }
            else {  // Translate symbol number
               NewRelocEntry.r_sym = NewSymbolIndex[TargetSymbol];
            }

            if (WordSize == 32) {
               // Interpret 32-bit COFF relocation types
               switch (OldReloc.p->Type) {
               case COFF32_RELOC_ABS:     // Ignored
                  NewRelocEntry.r_type = R_386_NONE;  break;

               case COFF32_RELOC_TOKEN:   // .NET common language runtime token
                  err.submit(2014);       // Error message
                  // Continue in next case and insert absolute address as token:
               case COFF32_RELOC_DIR32:   // 32-bit absolute virtual address
                  NewRelocEntry.r_type = R_386_32;  break;

               case COFF32_RELOC_IMGREL:  // 32-bit image relative address
                  // Image-relative relocation not supported in ELF
                  if (cmd.OutputType == FILETYPE_MACHO_LE) {
                     // Intermediate during conversion to MachO
                     NewRelocEntry.r_type = R_UNSUPPORTED_IMAGEREL;
                     break;
                  }
                  // Work-around unsupported image-relative relocation
                  // Convert to absolute
                  NewRelocEntry.r_type = R_386_32; // Absolute relocation
                  if (cmd.ImageBase == 0) {
                     // Default image base for 32-bit Linux
                     cmd.ImageBase = 0x8048000; // 0x400000 ?
                  }
                  NewRelocEntry.r_addend -= cmd.ImageBase;
                  // Warn that image base must be set to the specified value
                  sprintf(TempText, "%X", cmd.ImageBase); // write value as hexadecimal
                  err.submit(1301, TempText);  err.ClearError(1301);
                  break;

               case COFF32_RELOC_REL32:   // 32-bit self-relative
                  NewRelocEntry.r_type = R_386_PC32;
                  // Difference between EIP-relative and self-relative relocation = size of address field
                  NewRelocEntry.r_addend = -4;  break; 
                  /* !! error  if self-relative relocation with offset
                   !! test data that fails = testpic32.obj */

               case COFF32_RELOC_SECTION:   // 16-bit section index in file
               case COFF32_RELOC_SECREL:    // 32-bit section-relative
               case COFF32_RELOC_SECREL7:   //  8-bit section-relative
                  // These fixup types are not supported in ELF files
                  if (cmd.DebugInfo != CMDL_DEBUG_STRIP) {
                     // Issue warning. Ignore if stripping debug info
                     err.submit(1010);
                  }
                  break;
                  
               default:
                  err.submit(2030, OldReloc.p->Type);  break; // Error: Unknown relocation type (%i) ignored
               }
            }
            else {
               // Interpret 64-bit COFF relocation types
               switch (OldReloc.p->Type) {
               case COFF64_RELOC_ABS:        // Ignored
                  NewRelocEntry.r_type = R_X86_64_NONE;  break;

               case COFF64_RELOC_TOKEN:      // .NET common language runtime token
                  err.submit(2014);          // Error message
                  // Continue in next case and insert absolute address as token:

               case COFF64_RELOC_ABS64:      // 64 bit absolute virtual address
                  NewRelocEntry.r_type = R_X86_64_64;  break;

               case COFF64_RELOC_PPC_TOKEN:
                  err.submit(2014);          // Error message
                  // Continue in next case and insert absolute address as token:

               case COFF64_RELOC_ABS32:      // 32 bit absolute address
                  NewRelocEntry.r_type = R_X86_64_32S;  break;

               case COFF64_RELOC_IMGREL:     // 32 bit image-relative
                  // Image-relative relocation not supported in ELF
                  if (cmd.OutputType == FILETYPE_MACHO_LE) {
                     // Intermediate during conversion to MachO
                     NewRelocEntry.r_type = R_UNSUPPORTED_IMAGEREL;
                     break;
                  }
                  // Work-around unsupported image-relative relocation
                  // Convert to absolute
                  NewRelocEntry.r_type = R_X86_64_32S; // Absolute 32-bit relocation
                  if (cmd.ImageBase == 0) {
                     // Default image base for 64-bit Linux
                     cmd.ImageBase = 0x400000;
                  }
                  NewRelocEntry.r_addend -= cmd.ImageBase;
                  // Warn that image base must be set to the specified value
                  sprintf(TempText, "%X", cmd.ImageBase); // write value as hexadecimal
                  err.submit(1301, TempText);  err.ClearError(1301);
                  break;

               case COFF64_RELOC_REL32:      // 32 bit, RIP-relative
               case COFF64_RELOC_REL32_1:    // 32 bit, relative to RIP - 1. For instruction with immediate byte operand
               case COFF64_RELOC_REL32_2:    // 32 bit, relative to RIP - 2. For instruction with immediate word operand
               case COFF64_RELOC_REL32_3:    // 32 bit, relative to RIP - 3. (useless)
               case COFF64_RELOC_REL32_4:    // 32 bit, relative to RIP - 4. For instruction with immediate dword operand
               case COFF64_RELOC_REL32_5:    // 32 bit, relative to RIP - 5. (useless)
                  NewRelocEntry.r_type = R_X86_64_PC32;
                  // Note:
                  // The microprocessor calculates RIP-relative addresses 
                  // relative to the value of the instruction pointer AFTER 
                  // the instruction. This is equal to the address of the 
                  // relocated field plus the size of the relocated field 
                  // itself plus the size of any immediate operand coming 
                  // after the relocated field.
                  // The COFF format makes the correction for this offset in 
                  // the linker by using a differet relocation type for 
                  // immediate operand size = 0, 1, 2 or 4.
                  // The ELF format makes the same correction by an explicit 
                  // addend, which is -4, -5, -6 or -8, respectively.
                  // The difference between RIP-relative and self-relative 
                  // relocation is equal to the size of the address field plus
                  // the size of any immediate operand:
                  NewRelocEntry.r_addend = -(4 + OldReloc.p->Type - COFF64_RELOC_REL32);                  
                  break;

               case COFF64_RELOC_SECTION:   // 16-bit section index in file
               case COFF64_RELOC_SECREL:    // 32-bit section-relative
               case COFF64_RELOC_SECREL7:   //  8-bit section-relative

                  // These fixup types are not supported in ELF files
                  if (cmd.DebugInfo != CMDL_DEBUG_STRIP) {
                     // Issue warning. Ignore if stripping debug info
                     err.submit(1010);
                  }
                  break; 

               default:
                  err.submit(2030, OldReloc.p->Type);  break; // Error: Unknown relocation type (%i) ignored
               }
            }

            // Find inline addend
            int32_t * paddend = 0;
            if (OldReloc.p->VirtualAddress + 4 > NewSections[newsec].GetDataSize()
               || NewSectionHeaders[newsec].sh_type == SHT_NOBITS) {
                  // Address of relocation is invalid
                  err.submit(2032);
               }
            else {
               // Make pointer to inline addend
               paddend = (int32_t*)(NewSections[newsec].Buf() 
                  + NewSectionHeaders[newsec].sh_offset + OldReloc.p->VirtualAddress);
            } 

            // Put relocation record into table
            if (WordSize == 32) {
               if (NewRelocEntry.r_addend != 0) {
                  // Use inline addends in 32 bit ELF (SHT_REL)
                  // Put addend inline
                  if (paddend) *paddend += uint32_t(NewRelocEntry.r_addend);
                  NewRelocEntry.r_addend = 0;
               }

               // Save 32-bit relocation record Elf32_Rel, not Elf32_Rela
               if (NewRelocEntry.r_addend) err.submit(9000);
               NewSections[newsecr].Push(&NewRelocEntry, sizeof(Elf32_Rel));
            }
            else {
               // 64 bit
               /*
               if (*paddend != 0) {
                  // Use explicit addend in 64 bit ELF (SHT_RELA)
                  // Explicit addend may cause link error if it appears to point outside section
                  NewRelocEntry.r_addend += *paddend;
                  *paddend = 0;
               }*/

               // Save 64-bit relocation record. Must be Elf64_Rela
               NewSections[newsecr].Push(&NewRelocEntry, sizeof(Elf64_Rela));
            }
         }
      }
   }
}


template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
void CCOF2ELF<ELFSTRUCTURES>::MakeBinaryFile() {
   // Convert subfunction: Make section headers and file header,
   // and combine everything into a single memory buffer.
   int32_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

   // 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;
      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));
}


// Make template instances for 32 and 64 bits
template class CCOF2ELF<ELF32STRUCTURES>;
template class CCOF2ELF<ELF64STRUCTURES>;