Skip to content

Commit 7840c00

Browse files
committed
[MC] Move MCAsmLayout::SectionOrder to MachObjectWriter::SectionOrder
Follow-up to 2c1fb41. SectionOrder is Mach-O specific to place zerofill sections after non-zerofill sections in the object writer.
1 parent 81f4fb6 commit 7840c00

File tree

5 files changed

+39
-49
lines changed

5 files changed

+39
-49
lines changed

llvm/include/llvm/MC/MCAsmLayout.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,14 @@ namespace llvm {
1616
class MCAssembler;
1717
class MCSection;
1818

19-
/// Encapsulates the layout of an assembly file at a particular point in time.
20-
///
21-
/// Assembly may require computing multiple layouts for a particular assembly
22-
/// file as part of the relaxation process. This class encapsulates the layout
23-
/// at a single point in time in such a way that it is always possible to
24-
/// efficiently compute the exact address of any symbol in the assembly file,
25-
/// even during the relaxation process.
2619
class MCAsmLayout {
2720
MCAssembler &Assembler;
2821

29-
/// List of sections in layout order.
30-
llvm::SmallVector<MCSection *, 16> SectionOrder;
31-
3222
public:
3323
MCAsmLayout(MCAssembler &Assembler);
3424

3525
/// Get the assembler object this is a layout for.
3626
MCAssembler &getAssembler() const { return Assembler; }
37-
38-
39-
/// \name Section Access (in layout order)
40-
/// @{
41-
42-
llvm::SmallVectorImpl<MCSection *> &getSectionOrder() { return SectionOrder; }
43-
const llvm::SmallVectorImpl<MCSection *> &getSectionOrder() const {
44-
return SectionOrder;
45-
}
4627
};
4728

4829
} // end namespace llvm

llvm/include/llvm/MC/MCMachObjectWriter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ class MachObjectWriter : public MCObjectWriter {
109109

110110
SectionAddrMap SectionAddress;
111111

112+
// List of sections in layout order. Virtual sections are after non-virtual
113+
// sections.
114+
SmallVector<MCSection *, 0> SectionOrder;
115+
112116
/// @}
113117
/// \name Symbol Table Data
114118
/// @{
@@ -149,6 +153,9 @@ class MachObjectWriter : public MCObjectWriter {
149153

150154
bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
151155

156+
const llvm::SmallVectorImpl<MCSection *> &getSectionOrder() const {
157+
return SectionOrder;
158+
}
152159
SectionAddrMap &getSectionAddressMap() { return SectionAddress; }
153160

154161
uint64_t getSectionAddress(const MCSection *Sec) const {

llvm/lib/MC/MCAssembler.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,7 @@ uint64_t MCAssembler::computeFragmentSize(const MCFragment &F) const {
381381
llvm_unreachable("invalid fragment kind");
382382
}
383383

384-
MCAsmLayout::MCAsmLayout(MCAssembler &Asm) : Assembler(Asm) {
385-
// Compute the section layout order. Virtual sections must go last.
386-
for (MCSection &Sec : Asm)
387-
if (!Sec.isVirtualSection())
388-
SectionOrder.push_back(&Sec);
389-
for (MCSection &Sec : Asm)
390-
if (Sec.isVirtualSection())
391-
SectionOrder.push_back(&Sec);
392-
}
384+
MCAsmLayout::MCAsmLayout(MCAssembler &Asm) : Assembler(Asm) {}
393385

394386
// Compute the amount of padding required before the fragment \p F to
395387
// obey bundling restrictions, where \p FOffset is the fragment's offset in
@@ -951,31 +943,26 @@ void MCAssembler::layout(MCAsmLayout &Layout) {
951943
errs() << "assembler backend - pre-layout\n--\n";
952944
dump(); });
953945

954-
// Create dummy fragments and assign section ordinals.
946+
// Assign section ordinals.
955947
unsigned SectionIndex = 0;
956-
for (MCSection &Sec : *this)
948+
for (MCSection &Sec : *this) {
957949
Sec.setOrdinal(SectionIndex++);
958950

959-
// Assign layout order indices to sections and fragments.
960-
for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
961-
MCSection *Sec = Layout.getSectionOrder()[i];
962-
Sec->setLayoutOrder(i);
963-
964951
// Chain together fragments from all subsections.
965-
if (Sec->Subsections.size() > 1) {
952+
if (Sec.Subsections.size() > 1) {
966953
MCDummyFragment Dummy;
967954
MCFragment *Tail = &Dummy;
968-
for (auto &[_, List] : Sec->Subsections) {
955+
for (auto &[_, List] : Sec.Subsections) {
969956
assert(List.Head);
970957
Tail->Next = List.Head;
971958
Tail = List.Tail;
972959
}
973-
Sec->Subsections.clear();
974-
Sec->Subsections.push_back({0u, {Dummy.getNext(), Tail}});
975-
Sec->CurFragList = &Sec->Subsections[0].second;
960+
Sec.Subsections.clear();
961+
Sec.Subsections.push_back({0u, {Dummy.getNext(), Tail}});
962+
Sec.CurFragList = &Sec.Subsections[0].second;
976963

977964
unsigned FragmentIndex = 0;
978-
for (MCFragment &Frag : *Sec)
965+
for (MCFragment &Frag : Sec)
979966
Frag.setLayoutOrder(FragmentIndex++);
980967
}
981968
}

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ uint64_t MachObjectWriter::getPaddingSize(const MCAssembler &Asm,
125125
const MCSection *Sec) const {
126126
uint64_t EndAddr = getSectionAddress(Sec) + Asm.getSectionAddressSize(*Sec);
127127
unsigned Next = Sec->getLayoutOrder() + 1;
128-
if (Next >= Asm.getLayout()->getSectionOrder().size())
128+
if (Next >= SectionOrder.size())
129129
return 0;
130130

131-
const MCSection &NextSec = *Asm.getLayout()->getSectionOrder()[Next];
131+
const MCSection &NextSec = *SectionOrder[Next];
132132
if (NextSec.isVirtualSection())
133133
return 0;
134134
return offsetToAlignment(EndAddr, NextSec.getAlign());
@@ -670,8 +670,24 @@ void MachObjectWriter::computeSymbolTable(
670670
}
671671

672672
void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm) {
673+
// Assign layout order indices to sections.
674+
unsigned i = 0;
675+
// Compute the section layout order. Virtual sections must go last.
676+
for (MCSection &Sec : Asm) {
677+
if (!Sec.isVirtualSection()) {
678+
SectionOrder.push_back(&Sec);
679+
Sec.setLayoutOrder(i++);
680+
}
681+
}
682+
for (MCSection &Sec : Asm) {
683+
if (Sec.isVirtualSection()) {
684+
SectionOrder.push_back(&Sec);
685+
Sec.setLayoutOrder(i++);
686+
}
687+
}
688+
673689
uint64_t StartAddress = 0;
674-
for (const MCSection *Sec : Asm.getLayout()->getSectionOrder()) {
690+
for (const MCSection *Sec : SectionOrder) {
675691
StartAddress = alignTo(StartAddress, Sec->getAlign());
676692
SectionAddress[Sec] = StartAddress;
677693
StartAddress += Asm.getSectionAddressSize(*Sec);

llvm/tools/dsymutil/MachOUtils.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,13 @@ static void transferSegmentAndSections(
326326
static bool createDwarfSegment(const MCAssembler& Asm,uint64_t VMAddr, uint64_t FileOffset,
327327
uint64_t FileSize, unsigned NumSections,
328328
MachObjectWriter &Writer) {
329-
auto &Layout = *Asm.getLayout();
330329
Writer.writeSegmentLoadCommand("__DWARF", NumSections, VMAddr,
331330
alignTo(FileSize, 0x1000), FileOffset,
332331
FileSize, /* MaxProt */ 7,
333332
/* InitProt =*/3);
334333

335-
for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
336-
MCSection *Sec = Layout.getSectionOrder()[i];
334+
for (unsigned int i = 0, n = Writer.getSectionOrder().size(); i != n; ++i) {
335+
MCSection *Sec = Writer.getSectionOrder()[i];
337336
if (!Asm.getSectionFileSize(*Sec))
338337
continue;
339338

@@ -491,8 +490,8 @@ bool generateDsymCompanion(
491490
unsigned NumDwarfSections = 0;
492491
uint64_t DwarfSegmentSize = 0;
493492

494-
for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
495-
MCSection *Sec = Layout.getSectionOrder()[i];
493+
for (unsigned int i = 0, n = Writer.getSectionOrder().size(); i != n; ++i) {
494+
MCSection *Sec = Writer.getSectionOrder()[i];
496495
if (Sec->begin() == Sec->end())
497496
continue;
498497

0 commit comments

Comments
 (0)