Skip to content

Commit ca006ee

Browse files
committed
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.
1 parent ef4e4a0 commit ca006ee

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

llvm/include/llvm/MC/MCSection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,9 @@ class MCFragment {
343343
bool getAllowAutoPadding() const { return AllowAutoPadding; }
344344
void setAllowAutoPadding(bool V) { AllowAutoPadding = V; }
345345

346-
// Content-related functions manage parent's storage using ContentStart and
346+
//== Content-related functions manage parent's storage using ContentStart and
347347
// ContentSize.
348-
void clearContents() { ContentEnd = ContentStart; }
348+
349349
// Get a SmallVector reference. The caller should call doneAppending to update
350350
// `ContentEnd`.
351351
SmallVectorImpl<char> &getContentsForAppending() {

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -797,19 +797,18 @@ uint64_t MachObjectWriter::writeObject() {
797797
UndefinedSymbolData);
798798

799799
if (!CGProfile.empty()) {
800-
MCSection *CGProfileSection = getContext().getMachOSection(
801-
"__LLVM", "__cg_profile", 0, SectionKind::getMetadata());
802-
auto &Frag = *CGProfileSection->begin();
803-
Frag.clearContents();
804-
raw_svector_ostream OS(Frag.getContentsForAppending());
800+
SmallString<0> Content;
801+
raw_svector_ostream OS(Content);
805802
for (const MCObjectWriter::CGProfileEntry &CGPE : CGProfile) {
806803
uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
807804
uint32_t ToIndex = CGPE.To->getSymbol().getIndex();
808805
support::endian::write(OS, FromIndex, W.Endian);
809806
support::endian::write(OS, ToIndex, W.Endian);
810807
support::endian::write(OS, CGPE.Count, W.Endian);
811808
}
812-
Frag.doneAppending();
809+
MCSection *Sec = getContext().getMachOSection("__LLVM", "__cg_profile", 0,
810+
SectionKind::getMetadata());
811+
llvm::copy(OS.str(), Sec->curFragList()->Head->getContents().data());
813812
}
814813

815814
unsigned NumSections = Asm.end() - Asm.begin();

llvm/lib/MC/WinCOFFObjectWriter.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,10 +1070,8 @@ uint64_t WinCOFFWriter::writeObject() {
10701070

10711071
// Create the contents of the .llvm_addrsig section.
10721072
if (Mode != DwoOnly && OWriter.getEmitAddrsigSection()) {
1073-
auto *Sec = getContext().getCOFFSection(".llvm_addrsig",
1074-
COFF::IMAGE_SCN_LNK_REMOVE);
1075-
auto *Frag = Sec->curFragList()->Head;
1076-
raw_svector_ostream OS(Frag->getContentsForAppending());
1073+
SmallString<0> Content;
1074+
raw_svector_ostream OS(Content);
10771075
for (const MCSymbol *S : OWriter.AddrsigSyms) {
10781076
if (!S->isRegistered())
10791077
continue;
@@ -1088,23 +1086,25 @@ uint64_t WinCOFFWriter::writeObject() {
10881086
"executePostLayoutBinding!");
10891087
encodeULEB128(SectionMap[TargetSection]->Symbol->getIndex(), OS);
10901088
}
1091-
Frag->doneAppending();
1089+
auto *Sec = getContext().getCOFFSection(".llvm_addrsig",
1090+
COFF::IMAGE_SCN_LNK_REMOVE);
1091+
Sec->curFragList()->Tail->setVarContents(OS.str());
10921092
}
10931093

10941094
// Create the contents of the .llvm.call-graph-profile section.
10951095
if (Mode != DwoOnly && !OWriter.getCGProfile().empty()) {
1096-
auto *Sec = getContext().getCOFFSection(".llvm.call-graph-profile",
1097-
COFF::IMAGE_SCN_LNK_REMOVE);
1098-
auto *Frag = Sec->curFragList()->Head;
1099-
raw_svector_ostream OS(Frag->getContentsForAppending());
1096+
SmallString<0> Content;
1097+
raw_svector_ostream OS(Content);
11001098
for (const auto &CGPE : OWriter.getCGProfile()) {
11011099
uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
11021100
uint32_t ToIndex = CGPE.To->getSymbol().getIndex();
11031101
support::endian::write(OS, FromIndex, W.Endian);
11041102
support::endian::write(OS, ToIndex, W.Endian);
11051103
support::endian::write(OS, CGPE.Count, W.Endian);
11061104
}
1107-
Frag->doneAppending();
1105+
auto *Sec = getContext().getCOFFSection(".llvm.call-graph-profile",
1106+
COFF::IMAGE_SCN_LNK_REMOVE);
1107+
Sec->curFragList()->Tail->setVarContents(OS.str());
11081108
}
11091109

11101110
assignFileOffsets();

0 commit comments

Comments
 (0)