@@ -200,6 +200,16 @@ MCInst *MCContext::createMCInst() {
200200 return new (MCInstAllocator.Allocate ()) MCInst;
201201}
202202
203+ // Allocate the initial MCFragment for the begin symbol.
204+ MCFragment *MCContext::allocInitialFragment (MCSection &Sec) {
205+ assert (!Sec.curFragList ()->Head );
206+ auto *F = allocFragment<MCFragment>();
207+ F->setParent (&Sec);
208+ Sec.curFragList ()->Head = F;
209+ Sec.curFragList ()->Tail = F;
210+ return F;
211+ }
212+
203213// ===----------------------------------------------------------------------===//
204214// Symbol Manipulation
205215// ===----------------------------------------------------------------------===//
@@ -433,28 +443,24 @@ MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
433443 return getOrCreateDirectionalLocalSymbol (LocalLabelVal, Instance);
434444}
435445
436- // Create a section symbol, with a distinct one for each section of the same.
437- // The first symbol is used for assembly code references.
438446template <typename Symbol>
439447Symbol *MCContext::getOrCreateSectionSymbol (StringRef Section) {
440448 Symbol *R;
441449 auto &SymEntry = getSymbolTableEntry (Section);
442450 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.
443453 if (Sym && Sym->isDefined () &&
444454 (!Sym->isInSection () || Sym->getSection ().getBeginSymbol () != Sym))
445455 reportError (SMLoc (), " invalid symbol redefinition" );
446- // Use the symbol's index to track if it has been used as a section symbol.
447- // Set to -1 to catch potential bugs if misused as a symbol index.
448- if (Sym && Sym->getIndex () != -1u ) {
456+ if (Sym && Sym->isUndefined ()) {
449457 R = cast<Symbol>(Sym);
450458 } else {
451459 SymEntry.second .Used = true ;
452460 R = new (&SymEntry, *this ) Symbol (&SymEntry, /* isTemporary=*/ false );
453461 if (!Sym)
454462 SymEntry.second .Symbol = R;
455463 }
456- // Mark as section symbol.
457- R->setIndex (-1u );
458464 return R;
459465}
460466
@@ -562,6 +568,7 @@ MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
562568 MCSectionMachO (Segment, Name.substr (Name.size () - Section.size ()),
563569 TypeAndAttributes, Reserved2, Kind, Begin);
564570 R.first ->second = Ret;
571+ allocInitialFragment (*Ret);
565572 return Ret;
566573}
567574
@@ -572,8 +579,15 @@ MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
572579 bool Comdat, unsigned UniqueID,
573580 const MCSymbolELF *LinkedToSym) {
574581 auto *R = getOrCreateSectionSymbol<MCSymbolELF>(Section);
575- return new (ELFAllocator.Allocate ()) MCSectionELF (
582+ R->setBinding (ELF::STB_LOCAL);
583+ R->setType (ELF::STT_SECTION);
584+
585+ auto *Ret = new (ELFAllocator.Allocate ()) MCSectionELF (
576586 Section, Type, Flags, EntrySize, Group, Comdat, UniqueID, R, LinkedToSym);
587+
588+ auto *F = allocInitialFragment (*Ret);
589+ R->setFragment (F);
590+ return Ret;
577591}
578592
579593MCSectionELF *
@@ -729,6 +743,7 @@ MCSectionGOFF *MCContext::getGOFFSection(SectionKind Kind, StringRef Name,
729743 MCSectionGOFF (CachedName, Kind, IsVirtual, Attributes,
730744 static_cast <MCSectionGOFF *>(Parent));
731745 Iter->second = GOFFSection;
746+ allocInitialFragment (*GOFFSection);
732747 return GOFFSection;
733748}
734749
@@ -783,7 +798,8 @@ MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
783798 MCSectionCOFF *Result = new (COFFAllocator.Allocate ()) MCSectionCOFF (
784799 CachedName, Characteristics, COMDATSymbol, Selection, UniqueID, Begin);
785800 Iter->second = Result;
786- Begin->setFragment (&Result->getDummyFragment ());
801+ auto *F = allocInitialFragment (*Result);
802+ Begin->setFragment (F);
787803 return Result;
788804}
789805
@@ -854,6 +870,8 @@ MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
854870 MCSectionWasm (CachedName, Kind, Flags, GroupSym, UniqueID, Begin);
855871 Entry.second = Result;
856872
873+ auto *F = allocInitialFragment (*Result);
874+ Begin->setFragment (F);
857875 return Result;
858876}
859877
@@ -909,11 +927,24 @@ MCSectionXCOFF *MCContext::getXCOFFSection(
909927 MultiSymbolsAllowed);
910928
911929 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+
912941 return Result;
913942}
914943
915944MCSectionSPIRV *MCContext::getSPIRVSection () {
916945 MCSectionSPIRV *Result = new (SPIRVAllocator.Allocate ()) MCSectionSPIRV ();
946+
947+ allocInitialFragment (*Result);
917948 return Result;
918949}
919950
@@ -933,6 +964,7 @@ MCSectionDXContainer *MCContext::getDXContainerSection(StringRef Section,
933964 new (DXCAllocator.Allocate ()) MCSectionDXContainer (Name, K, nullptr );
934965
935966 // The first fragment will store the header
967+ allocInitialFragment (*MapIt->second );
936968 return MapIt->second ;
937969}
938970
0 commit comments