Skip to content

Commit 21fac2d

Browse files
committed
[MC] Ensure all new sections have a MCDataFragment
MCAssembler::layout ensures that every section has at least one fragment, which simplifies MCAsmLayout::getSectionAddressSize (see e73353c from 2010). It's better to ensure the condition is satisfied at create time (COFF, GOFF, Mach-O) to simplify more fragment processing.
1 parent 6dc8de7 commit 21fac2d

File tree

4 files changed

+11
-21
lines changed

4 files changed

+11
-21
lines changed

llvm/lib/MC/MCAssembler.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ void MCAssembler::reset() {
126126
bool MCAssembler::registerSection(MCSection &Section) {
127127
if (Section.isRegistered())
128128
return false;
129+
assert(Section.curFragList()->Head && "allocInitialFragment not called");
129130
Sections.push_back(&Section);
130131
Section.setIsRegistered(true);
131132
return true;
@@ -856,17 +857,8 @@ void MCAssembler::layout(MCAsmLayout &Layout) {
856857

857858
// Create dummy fragments and assign section ordinals.
858859
unsigned SectionIndex = 0;
859-
for (MCSection &Sec : *this) {
860-
// Create dummy fragments to eliminate any empty sections, this simplifies
861-
// layout.
862-
if (Sec.empty()) {
863-
auto *F = getContext().allocFragment<MCDataFragment>();
864-
F->setParent(&Sec);
865-
Sec.addFragment(*F);
866-
}
867-
860+
for (MCSection &Sec : *this)
868861
Sec.setOrdinal(SectionIndex++);
869-
}
870862

871863
// Assign layout order indices to sections and fragments.
872864
for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {

llvm/lib/MC/MCContext.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,12 @@ MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
483483

484484
// Otherwise, return a new section.
485485
StringRef Name = R.first->first();
486-
R.first->second = new (MachOAllocator.Allocate())
486+
auto *Ret = new (MachOAllocator.Allocate())
487487
MCSectionMachO(Segment, Name.substr(Name.size() - Section.size()),
488488
TypeAndAttributes, Reserved2, Kind, Begin);
489-
return R.first->second;
489+
R.first->second = Ret;
490+
allocInitialFragment(*Ret);
491+
return Ret;
490492
}
491493

492494
MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
@@ -672,7 +674,7 @@ MCSectionGOFF *MCContext::getGOFFSection(StringRef Section, SectionKind Kind,
672674
MCSectionGOFF *GOFFSection = new (GOFFAllocator.Allocate())
673675
MCSectionGOFF(CachedName, Kind, Parent, Subsection);
674676
Iter->second = GOFFSection;
675-
677+
allocInitialFragment(*GOFFSection);
676678
return GOFFSection;
677679
}
678680

@@ -701,8 +703,8 @@ MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
701703
StringRef CachedName = Iter->first.SectionName;
702704
MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
703705
CachedName, Characteristics, COMDATSymbol, Selection, Begin);
704-
705706
Iter->second = Result;
707+
allocInitialFragment(*Result);
706708
return Result;
707709
}
708710

llvm/lib/MC/MCMachOStreamer.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,12 @@ void MCMachOStreamer::finalizeCGProfile() {
554554
MCSection *CGProfileSection = Asm.getContext().getMachOSection(
555555
"__LLVM", "__cg_profile", 0, SectionKind::getMetadata());
556556
Asm.registerSection(*CGProfileSection);
557-
auto *Frag = getContext().allocFragment<MCDataFragment>();
558-
Frag->setParent(CGProfileSection);
559-
CGProfileSection->addFragment(*Frag);
560557
// For each entry, reserve space for 2 32-bit indices and a 64-bit count.
561558
size_t SectionBytes =
562559
Asm.CGProfile.size() * (2 * sizeof(uint32_t) + sizeof(uint64_t));
563-
Frag->getContents().resize(SectionBytes);
560+
cast<MCDataFragment>(*CGProfileSection->begin())
561+
.getContents()
562+
.resize(SectionBytes);
564563
}
565564

566565
MCStreamer *llvm::createMachOStreamer(MCContext &Context,

llvm/tools/dsymutil/MachOUtils.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,6 @@ bool generateDsymCompanion(
629629

630630
// Emit the Dwarf sections contents.
631631
for (const MCSection &Sec : MCAsm) {
632-
if (Sec.empty())
633-
continue;
634-
635632
uint64_t Pos = OutFile.tell();
636633
OutFile.write_zeros(alignTo(Pos, Sec.getAlign()) - Pos);
637634
MCAsm.writeSectionData(OutFile, &Sec, Layout);

0 commit comments

Comments
 (0)