diff --git a/llvm/include/llvm/MC/MCSection.h b/llvm/include/llvm/MC/MCSection.h --- a/llvm/include/llvm/MC/MCSection.h +++ b/llvm/include/llvm/MC/MCSection.h @@ -100,16 +100,19 @@ SmallVector PendingLabels; protected: + // TODO Make Name private when possible. + StringRef Name; SectionVariant Variant; SectionKind Kind; - MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin); + MCSection(SectionVariant V, StringRef Name, SectionKind K, MCSymbol *Begin); ~MCSection(); public: MCSection(const MCSection &) = delete; MCSection &operator=(const MCSection &) = delete; + StringRef getName() const { return Name; } SectionKind getKind() const { return Kind; } SectionVariant getVariant() const { return Variant; } diff --git a/llvm/include/llvm/MC/MCSectionCOFF.h b/llvm/include/llvm/MC/MCSectionCOFF.h --- a/llvm/include/llvm/MC/MCSectionCOFF.h +++ b/llvm/include/llvm/MC/MCSectionCOFF.h @@ -24,9 +24,6 @@ /// This represents a section on Windows class MCSectionCOFF final : public MCSection { - // The memory for this string is stored in the same MCContext as *this. - StringRef SectionName; - // FIXME: The following fields should not be mutable, but are for now so the // asm parser can honor the .linkonce directive. @@ -51,12 +48,12 @@ private: friend class MCContext; - MCSectionCOFF(StringRef Section, unsigned Characteristics, + // The storage of Name is owned by MCContext's COFFUniquingMap. + MCSectionCOFF(StringRef Name, unsigned Characteristics, MCSymbol *COMDATSymbol, int Selection, SectionKind K, MCSymbol *Begin) - : MCSection(SV_COFF, K, Begin), SectionName(Section), - Characteristics(Characteristics), COMDATSymbol(COMDATSymbol), - Selection(Selection) { + : MCSection(SV_COFF, Name, K, Begin), Characteristics(Characteristics), + COMDATSymbol(COMDATSymbol), Selection(Selection) { assert((Characteristics & 0x00F00000) == 0 && "alignment must not be set upon section creation"); } @@ -66,7 +63,6 @@ /// section name bool ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const; - StringRef getName() const { return SectionName; } unsigned getCharacteristics() const { return Characteristics; } MCSymbol *getCOMDATSymbol() const { return COMDATSymbol; } int getSelection() const { return Selection; } diff --git a/llvm/include/llvm/MC/MCSectionELF.h b/llvm/include/llvm/MC/MCSectionELF.h --- a/llvm/include/llvm/MC/MCSectionELF.h +++ b/llvm/include/llvm/MC/MCSectionELF.h @@ -25,10 +25,6 @@ /// This represents a section on linux, lots of unix variants and some bare /// metal systems. class MCSectionELF final : public MCSection { - /// This is the name of the section. The referenced memory is owned by - /// TargetLoweringObjectFileELF's ELFUniqueMap. - StringRef SectionName; - /// This is the sh_type field of a section, drawn from the enums below. unsigned Type; @@ -51,24 +47,26 @@ private: friend class MCContext; - MCSectionELF(StringRef Section, unsigned type, unsigned flags, SectionKind K, + // The storage of Name is owned by MCContext's ELFUniquingMap. + MCSectionELF(StringRef Name, unsigned type, unsigned flags, SectionKind K, unsigned entrySize, const MCSymbolELF *group, unsigned UniqueID, MCSymbol *Begin, const MCSymbolELF *LinkedToSym) - : MCSection(SV_ELF, K, Begin), SectionName(Section), Type(type), - Flags(flags), UniqueID(UniqueID), EntrySize(entrySize), Group(group), + : MCSection(SV_ELF, Name, K, Begin), Type(type), Flags(flags), + UniqueID(UniqueID), EntrySize(entrySize), Group(group), LinkedToSym(LinkedToSym) { if (Group) Group->setIsSignature(); } - void setSectionName(StringRef Name) { SectionName = Name; } + // TODO Delete after we stop supporting generation of GNU-style .zdebug_* + // sections. + void setSectionName(StringRef Name) { this->Name = Name; } public: /// Decides whether a '.section' directive should be printed before the /// section name bool ShouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const; - StringRef getName() const { return SectionName; } unsigned getType() const { return Type; } unsigned getFlags() const { return Flags; } unsigned getEntrySize() const { return EntrySize; } diff --git a/llvm/include/llvm/MC/MCSectionMachO.h b/llvm/include/llvm/MC/MCSectionMachO.h --- a/llvm/include/llvm/MC/MCSectionMachO.h +++ b/llvm/include/llvm/MC/MCSectionMachO.h @@ -23,7 +23,6 @@ /// system, these are also described in /usr/include/mach-o/loader.h. class MCSectionMachO final : public MCSection { char SegmentName[16]; // Not necessarily null terminated! - char SectionName[16]; // Not necessarily null terminated! /// This is the SECTION_TYPE and SECTION_ATTRIBUTES field of a section, drawn /// from the enums below. @@ -44,12 +43,6 @@ return StringRef(SegmentName, 16); return StringRef(SegmentName); } - StringRef getName() const { - // SectionName is not necessarily null terminated! - if (SectionName[15]) - return StringRef(SectionName, 16); - return StringRef(SectionName); - } unsigned getTypeAndAttributes() const { return TypeAndAttributes; } unsigned getStubSize() const { return Reserved2; } diff --git a/llvm/include/llvm/MC/MCSectionWasm.h b/llvm/include/llvm/MC/MCSectionWasm.h --- a/llvm/include/llvm/MC/MCSectionWasm.h +++ b/llvm/include/llvm/MC/MCSectionWasm.h @@ -25,10 +25,6 @@ /// This represents a section on wasm. class MCSectionWasm final : public MCSection { - /// This is the name of the section. The referenced memory is owned by - /// TargetLoweringObjectFileWasm's WasmUniqueMap. - StringRef SectionName; - unsigned UniqueID; const MCSymbolWasm *Group; @@ -45,18 +41,17 @@ // Whether this data segment is passive bool IsPassive = false; + // The storage of Name is owned by MCContext's WasmUniquingMap. friend class MCContext; - MCSectionWasm(StringRef Section, SectionKind K, const MCSymbolWasm *group, + MCSectionWasm(StringRef Name, SectionKind K, const MCSymbolWasm *group, unsigned UniqueID, MCSymbol *Begin) - : MCSection(SV_Wasm, K, Begin), SectionName(Section), UniqueID(UniqueID), - Group(group) {} + : MCSection(SV_Wasm, Name, K, Begin), UniqueID(UniqueID), Group(group) {} public: /// Decides whether a '.section' directive should be printed before the /// section name bool shouldOmitSectionDirective(StringRef Name, const MCAsmInfo &MAI) const; - StringRef getName() const { return SectionName; } const MCSymbolWasm *getGroup() const { return Group; } void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, diff --git a/llvm/include/llvm/MC/MCSectionXCOFF.h b/llvm/include/llvm/MC/MCSectionXCOFF.h --- a/llvm/include/llvm/MC/MCSectionXCOFF.h +++ b/llvm/include/llvm/MC/MCSectionXCOFF.h @@ -33,17 +33,16 @@ class MCSectionXCOFF final : public MCSection { friend class MCContext; - StringRef Name; XCOFF::StorageMappingClass MappingClass; XCOFF::SymbolType Type; XCOFF::StorageClass StorageClass; MCSymbolXCOFF *const QualName; - MCSectionXCOFF(StringRef Section, XCOFF::StorageMappingClass SMC, + MCSectionXCOFF(StringRef Name, XCOFF::StorageMappingClass SMC, XCOFF::SymbolType ST, XCOFF::StorageClass SC, SectionKind K, MCSymbolXCOFF *QualName, MCSymbol *Begin) - : MCSection(SV_XCOFF, K, Begin), Name(Section), MappingClass(SMC), - Type(ST), StorageClass(SC), QualName(QualName) { + : MCSection(SV_XCOFF, Name, K, Begin), MappingClass(SMC), Type(ST), + StorageClass(SC), QualName(QualName) { assert((ST == XCOFF::XTY_SD || ST == XCOFF::XTY_CM || ST == XCOFF::XTY_ER) && "Invalid or unhandled type for csect."); assert(QualName != nullptr && "QualName is needed."); @@ -58,7 +57,6 @@ return S->getVariant() == SV_XCOFF; } - StringRef getName() const { return Name; } XCOFF::StorageMappingClass getMappingClass() const { return MappingClass; } XCOFF::StorageClass getStorageClass() const { return StorageClass; } XCOFF::SymbolType getCSectType() const { return Type; } diff --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp --- a/llvm/lib/MC/MCAssembler.cpp +++ b/llvm/lib/MC/MCAssembler.cpp @@ -686,11 +686,8 @@ report_fatal_error("cannot have fixups in virtual section!"); for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i) if (DF.getContents()[i]) { - if (auto *ELFSec = dyn_cast(Sec)) - report_fatal_error("non-zero initializer found in section '" + - ELFSec->getName() + "'"); - else - report_fatal_error("non-zero initializer found in virtual section"); + report_fatal_error("non-zero initializer found in section '" + + Sec->getName() + "'"); } break; } diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp --- a/llvm/lib/MC/MCContext.cpp +++ b/llvm/lib/MC/MCContext.cpp @@ -302,23 +302,25 @@ // diagnosed by the client as an error. // Form the name to look up. - SmallString<64> Name; - Name += Segment; - Name.push_back(','); - Name += Section; + assert(Section.size() <= 16 && "section name is too long"); + assert(!memchr(Section.data(), '\0', Section.size()) && + "section name cannot contain NUL"); // Do the lookup, if we have a hit, return it. - MCSectionMachO *&Entry = MachOUniquingMap[Name]; - if (Entry) - return Entry; + auto R = MachOUniquingMap.try_emplace((Segment + Twine(',') + Section).str()); + if (!R.second) + return R.first->second; MCSymbol *Begin = nullptr; if (BeginSymName) Begin = createTempSymbol(BeginSymName, false); // Otherwise, return a new section. - return Entry = new (MachOAllocator.Allocate()) MCSectionMachO( - Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin); + StringRef Name = R.first->first(); + R.first->second = new (MachOAllocator.Allocate()) + MCSectionMachO(Segment, Name.substr(Name.size() - Section.size()), + TypeAndAttributes, Reserved2, Kind, Begin); + return R.first->second; } void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) { diff --git a/llvm/lib/MC/MCSection.cpp b/llvm/lib/MC/MCSection.cpp --- a/llvm/lib/MC/MCSection.cpp +++ b/llvm/lib/MC/MCSection.cpp @@ -20,9 +20,11 @@ using namespace llvm; -MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin) +MCSection::MCSection(SectionVariant V, StringRef Name, SectionKind K, + MCSymbol *Begin) : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false), - IsRegistered(false), DummyFragment(this), Variant(V), Kind(K) {} + IsRegistered(false), DummyFragment(this), Name(Name), Variant(V), + Kind(K) {} MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) { if (!End) diff --git a/llvm/lib/MC/MCSectionCOFF.cpp b/llvm/lib/MC/MCSectionCOFF.cpp --- a/llvm/lib/MC/MCSectionCOFF.cpp +++ b/llvm/lib/MC/MCSectionCOFF.cpp @@ -38,7 +38,7 @@ raw_ostream &OS, const MCExpr *Subsection) const { // standard sections don't require the '.section' - if (ShouldOmitSectionDirective(SectionName, MAI)) { + if (ShouldOmitSectionDirective(getName(), MAI)) { OS << '\t' << getName() << '\n'; return; } @@ -61,7 +61,7 @@ if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED) OS << 's'; if ((getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE) && - !isImplicitlyDiscardable(SectionName)) + !isImplicitlyDiscardable(getName())) OS << 'D'; OS << '"'; diff --git a/llvm/lib/MC/MCSectionELF.cpp b/llvm/lib/MC/MCSectionELF.cpp --- a/llvm/lib/MC/MCSectionELF.cpp +++ b/llvm/lib/MC/MCSectionELF.cpp @@ -53,7 +53,7 @@ void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, raw_ostream &OS, const MCExpr *Subsection) const { - if (ShouldOmitSectionDirective(SectionName, MAI)) { + if (ShouldOmitSectionDirective(getName(), MAI)) { OS << '\t' << getName(); if (Subsection) { OS << '\t'; diff --git a/llvm/lib/MC/MCSectionMachO.cpp b/llvm/lib/MC/MCSectionMachO.cpp --- a/llvm/lib/MC/MCSectionMachO.cpp +++ b/llvm/lib/MC/MCSectionMachO.cpp @@ -83,7 +83,7 @@ MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section, unsigned TAA, unsigned reserved2, SectionKind K, MCSymbol *Begin) - : MCSection(SV_MachO, K, Begin), TypeAndAttributes(TAA), + : MCSection(SV_MachO, Section, K, Begin), TypeAndAttributes(TAA), Reserved2(reserved2) { assert(Segment.size() <= 16 && Section.size() <= 16 && "Segment or section string too long"); @@ -92,11 +92,6 @@ SegmentName[i] = Segment[i]; else SegmentName[i] = 0; - - if (i < Section.size()) - SectionName[i] = Section[i]; - else - SectionName[i] = 0; } } diff --git a/llvm/lib/MC/MCSectionWasm.cpp b/llvm/lib/MC/MCSectionWasm.cpp --- a/llvm/lib/MC/MCSectionWasm.cpp +++ b/llvm/lib/MC/MCSectionWasm.cpp @@ -48,7 +48,7 @@ raw_ostream &OS, const MCExpr *Subsection) const { - if (shouldOmitSectionDirective(SectionName, MAI)) { + if (shouldOmitSectionDirective(getName(), MAI)) { OS << '\t' << getName(); if (Subsection) { OS << '\t';