@@ -116,8 +116,9 @@ struct ELFWriter {
116
116
// / Helper struct for containing some precomputed information on symbols.
117
117
struct ELFSymbolData {
118
118
const MCSymbolELF *Symbol;
119
- uint32_t SectionIndex;
120
119
StringRef Name;
120
+ uint32_t SectionIndex;
121
+ uint32_t Order;
121
122
122
123
// Support lexicographic sorting.
123
124
bool operator <(const ELFSymbolData &RHS) const {
@@ -626,11 +627,15 @@ void ELFWriter::computeSymbolTable(
626
627
627
628
std::vector<ELFSymbolData> LocalSymbolData;
628
629
std::vector<ELFSymbolData> ExternalSymbolData;
630
+ MutableArrayRef<std::pair<std::string, size_t >> FileNames =
631
+ Asm.getFileNames ();
632
+ for (const std::pair<std::string, size_t > &F : FileNames)
633
+ StrTabBuilder.add (F.first );
629
634
630
635
// Add the data for the symbols.
631
636
bool HasLargeSectionIndex = false ;
632
- for (const MCSymbol &S : Asm.symbols ()) {
633
- const auto &Symbol = cast<MCSymbolELF>(S );
637
+ for (auto It : llvm::enumerate ( Asm.symbols () )) {
638
+ const auto &Symbol = cast<MCSymbolELF>(It. value () );
634
639
bool Used = Symbol.isUsedInReloc ();
635
640
bool WeakrefUsed = Symbol.isWeakrefUsedInReloc ();
636
641
bool isSignature = Symbol.isSignature ();
@@ -646,6 +651,7 @@ void ELFWriter::computeSymbolTable(
646
651
647
652
ELFSymbolData MSD;
648
653
MSD.Symbol = cast<MCSymbolELF>(&Symbol);
654
+ MSD.Order = It.index ();
649
655
650
656
bool Local = Symbol.getBinding () == ELF::STB_LOCAL;
651
657
assert (Local || !Symbol.isTemporary ());
@@ -716,34 +722,40 @@ void ELFWriter::computeSymbolTable(
716
722
SymtabShndxSection->setAlignment (Align (4 ));
717
723
}
718
724
719
- ArrayRef<std::string> FileNames = Asm.getFileNames ();
720
- for (const std::string &Name : FileNames)
721
- StrTabBuilder.add (Name);
722
-
723
725
StrTabBuilder.finalize ();
724
726
725
- // File symbols are emitted first and handled separately from normal symbols,
726
- // i.e. a non-STT_FILE symbol with the same name may appear.
727
- for (const std::string &Name : FileNames)
728
- Writer.writeSymbol (StrTabBuilder.getOffset (Name),
729
- ELF::STT_FILE | ELF::STB_LOCAL, 0 , 0 , ELF::STV_DEFAULT,
730
- ELF::SHN_ABS, true );
731
-
732
727
// Symbols are required to be in lexicographic order.
733
728
// array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
734
729
array_pod_sort (ExternalSymbolData.begin (), ExternalSymbolData.end ());
735
730
736
- // Set the symbol indices. Local symbols must come before all other
737
- // symbols with non-local bindings.
738
- unsigned Index = FileNames.size () + 1 ;
731
+ // Make the first STT_FILE precede previous local symbols.
732
+ unsigned Index = 1 ;
733
+ auto FileNameIt = FileNames.begin ();
734
+ if (!FileNames.empty ())
735
+ FileNames[0 ].second = 0 ;
739
736
740
737
for (ELFSymbolData &MSD : LocalSymbolData) {
738
+ // Emit STT_FILE symbols before their associated local symbols.
739
+ for (; FileNameIt != FileNames.end () && FileNameIt->second <= MSD.Order ;
740
+ ++FileNameIt) {
741
+ Writer.writeSymbol (StrTabBuilder.getOffset (FileNameIt->first ),
742
+ ELF::STT_FILE | ELF::STB_LOCAL, 0 , 0 , ELF::STV_DEFAULT,
743
+ ELF::SHN_ABS, true );
744
+ ++Index;
745
+ }
746
+
741
747
unsigned StringIndex = MSD.Symbol ->getType () == ELF::STT_SECTION
742
748
? 0
743
749
: StrTabBuilder.getOffset (MSD.Name );
744
750
MSD.Symbol ->setIndex (Index++);
745
751
writeSymbol (Writer, StringIndex, MSD, Layout);
746
752
}
753
+ for (; FileNameIt != FileNames.end (); ++FileNameIt) {
754
+ Writer.writeSymbol (StrTabBuilder.getOffset (FileNameIt->first ),
755
+ ELF::STT_FILE | ELF::STB_LOCAL, 0 , 0 , ELF::STV_DEFAULT,
756
+ ELF::SHN_ABS, true );
757
+ ++Index;
758
+ }
747
759
748
760
// Write the symbol table entries.
749
761
LastLocalSymbolIndex = Index;
0 commit comments