@@ -118,7 +118,7 @@ class Writer {
118
118
void createExportTable ();
119
119
void assignAddresses ();
120
120
void removeEmptySections ();
121
- void createSymbolAndStringTable ();
121
+ void createStringTable ();
122
122
void openFile (StringRef OutputPath);
123
123
template <typename PEHeaderTy> void writeHeader ();
124
124
void createSEHTable (OutputSection *RData);
@@ -127,9 +127,6 @@ class Writer {
127
127
void writeBuildId ();
128
128
void sortExceptionTable ();
129
129
130
- llvm::Optional<coff_symbol16> createSymbol (Defined *D);
131
- size_t addEntryToStringTable (StringRef Str);
132
-
133
130
OutputSection *findSection (StringRef Name);
134
131
OutputSection *createSection (StringRef Name);
135
132
void addBaserels (OutputSection *Dest);
@@ -154,7 +151,7 @@ class Writer {
154
151
ArrayRef<uint8_t > SectionTable;
155
152
156
153
uint64_t FileSize;
157
- uint32_t PointerToSymbolTable = 0 ;
154
+ uint32_t PointerToStringTable = 0 ;
158
155
uint64_t SizeOfImage;
159
156
uint64_t SizeOfHeaders;
160
157
};
@@ -293,7 +290,7 @@ void Writer::run() {
293
290
assignAddresses ();
294
291
removeEmptySections ();
295
292
setSectionPermissions ();
296
- createSymbolAndStringTable ();
293
+ createStringTable ();
297
294
298
295
// We must do this before opening the output file, as it depends on being able
299
296
// to read the contents of the existing output file.
@@ -470,72 +467,7 @@ void Writer::removeEmptySections() {
470
467
Sec->SectionIndex = Idx++;
471
468
}
472
469
473
- size_t Writer::addEntryToStringTable (StringRef Str) {
474
- assert (Str.size () > COFF::NameSize);
475
- size_t OffsetOfEntry = Strtab.size () + 4 ; // +4 for the size field
476
- Strtab.insert (Strtab.end (), Str.begin (), Str.end ());
477
- Strtab.push_back (' \0 ' );
478
- return OffsetOfEntry;
479
- }
480
-
481
- Optional<coff_symbol16> Writer::createSymbol (Defined *Def) {
482
- // Relative symbols are unrepresentable in a COFF symbol table.
483
- if (isa<DefinedSynthetic>(Def))
484
- return None;
485
-
486
- // Don't write dead symbols or symbols in codeview sections to the symbol
487
- // table.
488
- if (!Def->isLive ())
489
- return None;
490
- if (auto *D = dyn_cast<DefinedRegular>(Def))
491
- if (D->getChunk ()->isCodeView ())
492
- return None;
493
-
494
- coff_symbol16 Sym;
495
- StringRef Name = Def->getName ();
496
- if (Name.size () > COFF::NameSize) {
497
- Sym.Name .Offset .Zeroes = 0 ;
498
- Sym.Name .Offset .Offset = addEntryToStringTable (Name);
499
- } else {
500
- memset (Sym.Name .ShortName , 0 , COFF::NameSize);
501
- memcpy (Sym.Name .ShortName , Name.data (), Name.size ());
502
- }
503
-
504
- if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
505
- COFFSymbolRef Ref = D->getCOFFSymbol ();
506
- Sym.Type = Ref.getType ();
507
- Sym.StorageClass = Ref.getStorageClass ();
508
- } else {
509
- Sym.Type = IMAGE_SYM_TYPE_NULL;
510
- Sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
511
- }
512
- Sym.NumberOfAuxSymbols = 0 ;
513
-
514
- switch (Def->kind ()) {
515
- case Symbol::DefinedAbsoluteKind:
516
- Sym.Value = Def->getRVA ();
517
- Sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
518
- break ;
519
- default : {
520
- uint64_t RVA = Def->getRVA ();
521
- OutputSection *Sec = nullptr ;
522
- for (OutputSection *S : OutputSections) {
523
- if (S->getRVA () > RVA)
524
- break ;
525
- Sec = S;
526
- }
527
- Sym.Value = RVA - Sec->getRVA ();
528
- Sym.SectionNumber = Sec->SectionIndex ;
529
- break ;
530
- }
531
- }
532
- return Sym;
533
- }
534
-
535
- void Writer::createSymbolAndStringTable () {
536
- if (!Config->Debug || !Config->WriteSymtab )
537
- return ;
538
-
470
+ void Writer::createStringTable () {
539
471
// Name field in the section table is 8 byte long. Longer names need
540
472
// to be written to the string table. First, construct string table.
541
473
for (OutputSection *Sec : OutputSections) {
@@ -549,31 +481,19 @@ void Writer::createSymbolAndStringTable() {
549
481
// to libunwind.
550
482
if ((Sec->getPermissions () & IMAGE_SCN_MEM_DISCARDABLE) == 0 )
551
483
continue ;
552
- Sec->setStringTableOff (addEntryToStringTable (Name));
484
+ Sec->setStringTableOff (Strtab.size () + 4 ); // +4 for the size field
485
+ Strtab.insert (Strtab.end (), Name.begin (), Name.end ());
486
+ Strtab.push_back (' \0 ' );
553
487
}
554
488
555
- for (ObjFile *File : ObjFile::Instances) {
556
- for (Symbol *B : File->getSymbols ()) {
557
- auto *D = dyn_cast<Defined>(B);
558
- if (!D || D->WrittenToSymtab )
559
- continue ;
560
- D->WrittenToSymtab = true ;
561
-
562
- if (Optional<coff_symbol16> Sym = createSymbol (D))
563
- OutputSymtab.push_back (*Sym);
564
- }
565
- }
489
+ if (Strtab.empty ())
490
+ return ;
566
491
567
492
OutputSection *LastSection = OutputSections.back ();
568
- // We position the symbol table to be adjacent to the end of the last section.
569
- uint64_t FileOff = LastSection->getFileOff () +
570
- alignTo (LastSection->getRawSize (), SectorSize);
571
- if (!OutputSymtab.empty ()) {
572
- PointerToSymbolTable = FileOff;
573
- FileOff += OutputSymtab.size () * sizeof (coff_symbol16);
574
- }
575
- FileOff += Strtab.size () + 4 ;
576
- FileSize = alignTo (FileOff, SectorSize);
493
+ // We position the string table to be adjacent to the end of the last section.
494
+ PointerToStringTable = LastSection->getFileOff () +
495
+ alignTo (LastSection->getRawSize (), SectorSize);
496
+ FileSize = alignTo (PointerToStringTable + Strtab.size () + 4 , SectorSize);
577
497
}
578
498
579
499
// Visits all sections to assign incremental, non-overlapping RVAs and
@@ -760,22 +680,18 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
760
680
SectionTable = ArrayRef<uint8_t >(
761
681
Buf - OutputSections.size () * sizeof (coff_section), Buf);
762
682
763
- if (OutputSymtab.empty ())
683
+ // The string table normally follows the symbol table, but because we always
684
+ // emit an empty symbol table, the string table appears at the location of the
685
+ // symbol table.
686
+ COFF->PointerToSymbolTable = PointerToStringTable;
687
+ COFF->NumberOfSymbols = 0 ;
688
+ if (Strtab.empty ())
764
689
return ;
765
690
766
- COFF->PointerToSymbolTable = PointerToSymbolTable;
767
- uint32_t NumberOfSymbols = OutputSymtab.size ();
768
- COFF->NumberOfSymbols = NumberOfSymbols;
769
- auto *SymbolTable = reinterpret_cast <coff_symbol16 *>(
770
- Buffer->getBufferStart () + COFF->PointerToSymbolTable );
771
- for (size_t I = 0 ; I != NumberOfSymbols; ++I)
772
- SymbolTable[I] = OutputSymtab[I];
773
- // Create the string table, it follows immediately after the symbol table.
774
- // The first 4 bytes is length including itself.
775
- Buf = reinterpret_cast <uint8_t *>(&SymbolTable[NumberOfSymbols]);
776
- write32le (Buf, Strtab.size () + 4 );
777
- if (!Strtab.empty ())
778
- memcpy (Buf + 4 , Strtab.data (), Strtab.size ());
691
+ auto *StringTable = Buffer->getBufferStart () + PointerToStringTable;
692
+ // Create the string table. The first 4 bytes is length including itself.
693
+ write32le (StringTable, Strtab.size () + 4 );
694
+ memcpy (StringTable + 4 , Strtab.data (), Strtab.size ());
779
695
}
780
696
781
697
void Writer::openFile (StringRef Path) {
0 commit comments