I plan to use MCSection::getName() in D78138. Having the function in the base class is also convenient for debugging.
Reviewed By: rnk
Differential Revision: https://reviews.llvm.org/D78251
SmallVector<PendingLabel, 2> 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; }
/// 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.
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");
}
/// 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; }
/// 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;
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; }
/// 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.
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; }
/// 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;
// 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,
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.");
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; }
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<const MCSectionELF>(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;
}
// 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) {
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)
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;
}
if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED)
OS << 's';
if ((getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE) &&
- !isImplicitlyDiscardable(SectionName))
+ !isImplicitlyDiscardable(getName()))
OS << 'D';
OS << '"';
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';
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");
SegmentName[i] = Segment[i];
else
SegmentName[i] = 0;
-
- if (i < Section.size())
- SectionName[i] = Section[i];
- else
- SectionName[i] = 0;
}
}
raw_ostream &OS,
const MCExpr *Subsection) const {
- if (shouldOmitSectionDirective(SectionName, MAI)) {
+ if (shouldOmitSectionDirective(getName(), MAI)) {
OS << '\t' << getName();
if (Subsection) {
OS << '\t';