MCFragment: Remove clearContents and uses of non-streaming doneAppending Make the fixed-size part of MCFragment append-only to support allocating content as trailing data. The `doneAppending` API is reserved by MCStreamer API before finish and should not be used by the addrsig and call-graph-profile features.
diff --git a/llvm/include/llvm/MC/MCSection.h b/llvm/include/llvm/MC/MCSection.h index 953e6f7..87a8349 100644 --- a/llvm/include/llvm/MC/MCSection.h +++ b/llvm/include/llvm/MC/MCSection.h
@@ -343,9 +343,9 @@ bool getAllowAutoPadding() const { return AllowAutoPadding; } void setAllowAutoPadding(bool V) { AllowAutoPadding = V; } - // Content-related functions manage parent's storage using ContentStart and + //== Content-related functions manage parent's storage using ContentStart and // ContentSize. - void clearContents() { ContentEnd = ContentStart; } + // Get a SmallVector reference. The caller should call doneAppending to update // `ContentEnd`. SmallVectorImpl<char> &getContentsForAppending() {
diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp index 3298eef..48d2fc6 100644 --- a/llvm/lib/MC/MachObjectWriter.cpp +++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -797,11 +797,8 @@ UndefinedSymbolData); if (!CGProfile.empty()) { - MCSection *CGProfileSection = getContext().getMachOSection( - "__LLVM", "__cg_profile", 0, SectionKind::getMetadata()); - auto &Frag = *CGProfileSection->begin(); - Frag.clearContents(); - raw_svector_ostream OS(Frag.getContentsForAppending()); + SmallString<0> Content; + raw_svector_ostream OS(Content); for (const MCObjectWriter::CGProfileEntry &CGPE : CGProfile) { uint32_t FromIndex = CGPE.From->getSymbol().getIndex(); uint32_t ToIndex = CGPE.To->getSymbol().getIndex(); @@ -809,7 +806,9 @@ support::endian::write(OS, ToIndex, W.Endian); support::endian::write(OS, CGPE.Count, W.Endian); } - Frag.doneAppending(); + MCSection *Sec = getContext().getMachOSection("__LLVM", "__cg_profile", 0, + SectionKind::getMetadata()); + llvm::copy(OS.str(), Sec->curFragList()->Head->getContents().data()); } unsigned NumSections = Asm.end() - Asm.begin();
diff --git a/llvm/lib/MC/WinCOFFObjectWriter.cpp b/llvm/lib/MC/WinCOFFObjectWriter.cpp index c69b8d6..6ad4334 100644 --- a/llvm/lib/MC/WinCOFFObjectWriter.cpp +++ b/llvm/lib/MC/WinCOFFObjectWriter.cpp
@@ -1070,10 +1070,8 @@ // Create the contents of the .llvm_addrsig section. if (Mode != DwoOnly && OWriter.getEmitAddrsigSection()) { - auto *Sec = getContext().getCOFFSection(".llvm_addrsig", - COFF::IMAGE_SCN_LNK_REMOVE); - auto *Frag = Sec->curFragList()->Head; - raw_svector_ostream OS(Frag->getContentsForAppending()); + SmallString<0> Content; + raw_svector_ostream OS(Content); for (const MCSymbol *S : OWriter.AddrsigSyms) { if (!S->isRegistered()) continue; @@ -1088,15 +1086,15 @@ "executePostLayoutBinding!"); encodeULEB128(SectionMap[TargetSection]->Symbol->getIndex(), OS); } - Frag->doneAppending(); + auto *Sec = getContext().getCOFFSection(".llvm_addrsig", + COFF::IMAGE_SCN_LNK_REMOVE); + Sec->curFragList()->Tail->setVarContents(OS.str()); } // Create the contents of the .llvm.call-graph-profile section. if (Mode != DwoOnly && !OWriter.getCGProfile().empty()) { - auto *Sec = getContext().getCOFFSection(".llvm.call-graph-profile", - COFF::IMAGE_SCN_LNK_REMOVE); - auto *Frag = Sec->curFragList()->Head; - raw_svector_ostream OS(Frag->getContentsForAppending()); + SmallString<0> Content; + raw_svector_ostream OS(Content); for (const auto &CGPE : OWriter.getCGProfile()) { uint32_t FromIndex = CGPE.From->getSymbol().getIndex(); uint32_t ToIndex = CGPE.To->getSymbol().getIndex(); @@ -1104,7 +1102,9 @@ support::endian::write(OS, ToIndex, W.Endian); support::endian::write(OS, CGPE.Count, W.Endian); } - Frag->doneAppending(); + auto *Sec = getContext().getCOFFSection(".llvm.call-graph-profile", + COFF::IMAGE_SCN_LNK_REMOVE); + Sec->curFragList()->Tail->setVarContents(OS.str()); } assignFileOffsets();