@@ -443,24 +443,28 @@ MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
443443 return getOrCreateDirectionalLocalSymbol (LocalLabelVal, Instance);
444444}
445445
446+ // Create a section symbol, with a distinct one for each section of the same.
447+ // The first symbol is used for assembly code references.
446448template <typename Symbol>
447449Symbol *MCContext::getOrCreateSectionSymbol (StringRef Section) {
448450 Symbol *R;
449451 auto &SymEntry = getSymbolTableEntry (Section);
450452 MCSymbol *Sym = SymEntry.second .Symbol ;
451- // A section symbol can not redefine regular symbols. There may be multiple
452- // sections with the same name, in which case the first such section wins.
453453 if (Sym && Sym->isDefined () &&
454454 (!Sym->isInSection () || Sym->getSection ().getBeginSymbol () != Sym))
455455 reportError (SMLoc (), " invalid symbol redefinition" );
456- if (Sym && Sym->isUndefined ()) {
456+ // Use the symbol's index to track if it has been used as a section symbol.
457+ // Set to -1 to catch potential bugs if misused as a symbol index.
458+ if (Sym && Sym->getIndex () != -1u ) {
457459 R = cast<Symbol>(Sym);
458460 } else {
459461 SymEntry.second .Used = true ;
460462 R = new (&SymEntry, *this ) Symbol (&SymEntry, /* isTemporary=*/ false );
461463 if (!Sym)
462464 SymEntry.second .Symbol = R;
463465 }
466+ // Mark as section symbol.
467+ R->setIndex (-1u );
464468 return R;
465469}
466470
@@ -568,7 +572,6 @@ MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
568572 MCSectionMachO (Segment, Name.substr (Name.size () - Section.size ()),
569573 TypeAndAttributes, Reserved2, Kind, Begin);
570574 R.first ->second = Ret;
571- allocInitialFragment (*Ret);
572575 return Ret;
573576}
574577
@@ -579,15 +582,8 @@ MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
579582 bool Comdat, unsigned UniqueID,
580583 const MCSymbolELF *LinkedToSym) {
581584 auto *R = getOrCreateSectionSymbol<MCSymbolELF>(Section);
582- R->setBinding (ELF::STB_LOCAL);
583- R->setType (ELF::STT_SECTION);
584-
585- auto *Ret = new (ELFAllocator.Allocate ()) MCSectionELF (
585+ return new (ELFAllocator.Allocate ()) MCSectionELF (
586586 Section, Type, Flags, EntrySize, Group, Comdat, UniqueID, R, LinkedToSym);
587-
588- auto *F = allocInitialFragment (*Ret);
589- R->setFragment (F);
590- return Ret;
591587}
592588
593589MCSectionELF *
@@ -743,7 +739,6 @@ MCSectionGOFF *MCContext::getGOFFSection(SectionKind Kind, StringRef Name,
743739 MCSectionGOFF (CachedName, Kind, IsVirtual, Attributes,
744740 static_cast <MCSectionGOFF *>(Parent));
745741 Iter->second = GOFFSection;
746- allocInitialFragment (*GOFFSection);
747742 return GOFFSection;
748743}
749744
@@ -798,8 +793,7 @@ MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
798793 MCSectionCOFF *Result = new (COFFAllocator.Allocate ()) MCSectionCOFF (
799794 CachedName, Characteristics, COMDATSymbol, Selection, UniqueID, Begin);
800795 Iter->second = Result;
801- auto *F = allocInitialFragment (*Result);
802- Begin->setFragment (F);
796+ Begin->setFragment (&Result->getDummyFragment ());
803797 return Result;
804798}
805799
@@ -870,8 +864,6 @@ MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
870864 MCSectionWasm (CachedName, Kind, Flags, GroupSym, UniqueID, Begin);
871865 Entry.second = Result;
872866
873- auto *F = allocInitialFragment (*Result);
874- Begin->setFragment (F);
875867 return Result;
876868}
877869
@@ -927,24 +919,11 @@ MCSectionXCOFF *MCContext::getXCOFFSection(
927919 MultiSymbolsAllowed);
928920
929921 Entry.second = Result;
930-
931- auto *F = allocInitialFragment (*Result);
932-
933- // We might miss calculating the symbols difference as absolute value before
934- // adding fixups when symbol_A without the fragment set is the csect itself
935- // and symbol_B is in it.
936- // TODO: Currently we only set the fragment for XMC_PR csects and DWARF
937- // sections because we don't have other cases that hit this problem yet.
938- if (IsDwarfSec || CsectProp->MappingClass == XCOFF::XMC_PR)
939- QualName->setFragment (F);
940-
941922 return Result;
942923}
943924
944925MCSectionSPIRV *MCContext::getSPIRVSection () {
945926 MCSectionSPIRV *Result = new (SPIRVAllocator.Allocate ()) MCSectionSPIRV ();
946-
947- allocInitialFragment (*Result);
948927 return Result;
949928}
950929
@@ -964,7 +943,6 @@ MCSectionDXContainer *MCContext::getDXContainerSection(StringRef Section,
964943 new (DXCAllocator.Allocate ()) MCSectionDXContainer (Name, K, nullptr );
965944
966945 // The first fragment will store the header
967- allocInitialFragment (*MapIt->second );
968946 return MapIt->second ;
969947}
970948
0 commit comments