Skip to content

Commit 9513284

Browse files
committed
WinCOFFObjectWriter: Simplify code with member MCAssembler *
Similar to b65760b for ELF.
1 parent 403c722 commit 9513284

File tree

2 files changed

+57
-52
lines changed

2 files changed

+57
-52
lines changed

llvm/include/llvm/MC/MCWinCOFFObjectWriter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class WinCOFFObjectWriter final : public MCObjectWriter {
6060

6161
// MCObjectWriter interface implementation.
6262
void reset() override;
63+
void setAssembler(MCAssembler *Asm) override;
6364
void setIncrementalLinkerCompatible(bool Value) {
6465
IncrementalLinkerCompatible = Value;
6566
}

llvm/lib/MC/WinCOFFObjectWriter.cpp

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class COFFSection {
123123
class llvm::WinCOFFWriter {
124124
WinCOFFObjectWriter &OWriter;
125125
support::endian::Writer W;
126+
MCAssembler *Asm = nullptr;
126127

127128
using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
128129
using sections = std::vector<std::unique_ptr<COFFSection>>;
@@ -158,11 +159,11 @@ class llvm::WinCOFFWriter {
158159
DwoMode Mode);
159160

160161
void reset();
161-
void executePostLayoutBinding(MCAssembler &Asm);
162-
void recordRelocation(MCAssembler &Asm, const MCFragment &F,
163-
const MCFixup &Fixup, MCValue Target,
164-
uint64_t &FixedValue);
165-
uint64_t writeObject(MCAssembler &Asm);
162+
void setAssembler(MCAssembler *A) { Asm = A; }
163+
void executePostLayoutBinding();
164+
void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
165+
MCValue Target, uint64_t &FixedValue);
166+
uint64_t writeObject();
166167
int getSectionNumber(const MCSection &Section) const;
167168

168169
private:
@@ -171,10 +172,10 @@ class llvm::WinCOFFWriter {
171172
COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
172173
COFFSection *createSection(StringRef Name);
173174

174-
void defineSection(const MCAssembler &Asm, MCSectionCOFF const &Sec);
175+
void defineSection(MCSectionCOFF const &Sec);
175176

176177
COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
177-
void defineSymbol(const MCAssembler &Asm, const MCSymbol &Symbol);
178+
void defineSymbol(const MCSymbol &Symbol);
178179

179180
void SetSymbolName(COFFSymbol &S);
180181
void SetSectionName(COFFSection &S);
@@ -187,13 +188,13 @@ class llvm::WinCOFFWriter {
187188
void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
188189
void writeSectionHeaders();
189190
void WriteRelocation(const COFF::relocation &R);
190-
uint32_t writeSectionContents(MCAssembler &Asm, const MCSection &MCSec);
191-
void writeSection(MCAssembler &Asm, const COFFSection &Sec);
191+
uint32_t writeSectionContents(const MCSection &MCSec);
192+
void writeSection(const COFFSection &Sec);
192193

193-
void createFileSymbols(MCAssembler &Asm);
194+
void createFileSymbols();
194195
void setWeakDefaultNames();
195196
void assignSectionNumbers();
196-
void assignFileOffsets(MCAssembler &Asm);
197+
void assignFileOffsets();
197198
};
198199

199200
WinCOFFObjectWriter::WinCOFFObjectWriter(
@@ -292,8 +293,7 @@ static uint32_t getAlignment(const MCSectionCOFF &Sec) {
292293

293294
/// This function takes a section data object from the assembler
294295
/// and creates the associated COFF section staging object.
295-
void WinCOFFWriter::defineSection(const MCAssembler &Asm,
296-
const MCSectionCOFF &MCSec) {
296+
void WinCOFFWriter::defineSection(const MCSectionCOFF &MCSec) {
297297
COFFSection *Section = createSection(MCSec.getName());
298298
COFFSymbol *Symbol = createSymbol(MCSec.getName());
299299
Section->Symbol = Symbol;
@@ -328,8 +328,8 @@ void WinCOFFWriter::defineSection(const MCAssembler &Asm,
328328
if (UseOffsetLabels) {
329329
const uint32_t Interval = 1 << OffsetLabelIntervalBits;
330330
uint32_t N = 1;
331-
for (uint32_t Off = Interval, E = Asm.getSectionAddressSize(MCSec); Off < E;
332-
Off += Interval) {
331+
for (uint32_t Off = Interval, E = Asm->getSectionAddressSize(MCSec);
332+
Off < E; Off += Interval) {
333333
auto Name = ("$L" + MCSec.getName() + "_" + Twine(N++)).str();
334334
COFFSymbol *Label = createSymbol(Name);
335335
Label->Section = Section;
@@ -369,9 +369,8 @@ COFFSymbol *WinCOFFWriter::getLinkedSymbol(const MCSymbol &Symbol) {
369369

370370
/// This function takes a symbol data object from the assembler
371371
/// and creates the associated COFF symbol staging object.
372-
void WinCOFFWriter::defineSymbol(const MCAssembler &Asm,
373-
const MCSymbol &MCSym) {
374-
const MCSymbol *Base = Asm.getBaseSymbol(MCSym);
372+
void WinCOFFWriter::defineSymbol(const MCSymbol &MCSym) {
373+
const MCSymbol *Base = Asm->getBaseSymbol(MCSym);
375374
COFFSection *Sec = nullptr;
376375
MCSectionCOFF *MCSec = nullptr;
377376
if (Base && Base->getFragment()) {
@@ -418,7 +417,7 @@ void WinCOFFWriter::defineSymbol(const MCAssembler &Asm,
418417
}
419418

420419
if (Local) {
421-
Local->Data.Value = getSymbolValue(MCSym, Asm);
420+
Local->Data.Value = getSymbolValue(MCSym, *Asm);
422421

423422
const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
424423
Local->Data.Type = SymbolCOFF.getType();
@@ -575,13 +574,12 @@ void WinCOFFWriter::WriteRelocation(const COFF::relocation &R) {
575574
// Write MCSec's contents. What this function does is essentially
576575
// "Asm.writeSectionData(&MCSec)", but it's a bit complicated
577576
// because it needs to compute a CRC.
578-
uint32_t WinCOFFWriter::writeSectionContents(MCAssembler &Asm,
579-
const MCSection &MCSec) {
577+
uint32_t WinCOFFWriter::writeSectionContents(const MCSection &MCSec) {
580578
// Save the contents of the section to a temporary buffer, we need this
581579
// to CRC the data before we dump it into the object file.
582580
SmallVector<char, 128> Buf;
583581
raw_svector_ostream VecOS(Buf);
584-
Asm.writeSectionData(VecOS, &MCSec);
582+
Asm->writeSectionData(VecOS, &MCSec);
585583

586584
// Write the section contents to the object file.
587585
W.OS << Buf;
@@ -593,7 +591,7 @@ uint32_t WinCOFFWriter::writeSectionContents(MCAssembler &Asm,
593591
return JC.getCRC();
594592
}
595593

596-
void WinCOFFWriter::writeSection(MCAssembler &Asm, const COFFSection &Sec) {
594+
void WinCOFFWriter::writeSection(const COFFSection &Sec) {
597595
if (Sec.Number == -1)
598596
return;
599597

@@ -602,7 +600,7 @@ void WinCOFFWriter::writeSection(MCAssembler &Asm, const COFFSection &Sec) {
602600
assert(W.OS.tell() == Sec.Header.PointerToRawData &&
603601
"Section::PointerToRawData is insane!");
604602

605-
uint32_t CRC = writeSectionContents(Asm, *Sec.MCSection);
603+
uint32_t CRC = writeSectionContents(*Sec.MCSection);
606604

607605
// Update the section definition auxiliary symbol to record the CRC.
608606
COFFSymbol::AuxiliarySymbols &AuxSyms = Sec.Symbol->Aux;
@@ -636,7 +634,7 @@ void WinCOFFWriter::writeSection(MCAssembler &Asm, const COFFSection &Sec) {
636634
}
637635

638636
// Create .file symbols.
639-
void WinCOFFWriter::createFileSymbols(MCAssembler &Asm) {
637+
void WinCOFFWriter::createFileSymbols() {
640638
for (const std::pair<std::string, size_t> &It : OWriter.getFileNames()) {
641639
// round up to calculate the number of auxiliary symbols required
642640
const std::string &Name = It.first;
@@ -734,19 +732,19 @@ void WinCOFFWriter::assignSectionNumbers() {
734732
}
735733

736734
// Assign file offsets to COFF object file structures.
737-
void WinCOFFWriter::assignFileOffsets(MCAssembler &Asm) {
735+
void WinCOFFWriter::assignFileOffsets() {
738736
unsigned Offset = W.OS.tell();
739737

740738
Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
741739
Offset += COFF::SectionSize * Header.NumberOfSections;
742740

743-
for (const auto &Section : Asm) {
741+
for (const auto &Section : *Asm) {
744742
COFFSection *Sec = SectionMap[&Section];
745743

746744
if (!Sec || Sec->Number == -1)
747745
continue;
748746

749-
Sec->Header.SizeOfRawData = Asm.getSectionAddressSize(Section);
747+
Sec->Header.SizeOfRawData = Asm->getSectionAddressSize(Section);
750748

751749
if (IsPhysicalSection(Sec)) {
752750
Sec->Header.PointerToRawData = Offset;
@@ -807,22 +805,22 @@ void WinCOFFWriter::reset() {
807805
WeakDefaults.clear();
808806
}
809807

810-
void WinCOFFWriter::executePostLayoutBinding(MCAssembler &Asm) {
808+
void WinCOFFWriter::executePostLayoutBinding() {
811809
// "Define" each section & symbol. This creates section & symbol
812810
// entries in the staging area.
813-
for (const auto &Section : Asm) {
811+
for (const auto &Section : *Asm) {
814812
if ((Mode == NonDwoOnly && isDwoSection(Section)) ||
815813
(Mode == DwoOnly && !isDwoSection(Section)))
816814
continue;
817-
defineSection(Asm, static_cast<const MCSectionCOFF &>(Section));
815+
defineSection(static_cast<const MCSectionCOFF &>(Section));
818816
}
819817

820818
if (Mode != DwoOnly)
821-
for (const MCSymbol &Symbol : Asm.symbols())
819+
for (const MCSymbol &Symbol : Asm->symbols())
822820
// Define non-temporary or temporary static (private-linkage) symbols
823821
if (!Symbol.isTemporary() ||
824822
cast<MCSymbolCOFF>(Symbol).getClass() == COFF::IMAGE_SYM_CLASS_STATIC)
825-
defineSymbol(Asm, Symbol);
823+
defineSymbol(Symbol);
826824

827825
UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
828826
Header.NumberOfSections = Sections.size();
@@ -834,9 +832,8 @@ void WinCOFFWriter::executePostLayoutBinding(MCAssembler &Asm) {
834832
assignSectionNumbers();
835833
}
836834

837-
void WinCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment &F,
838-
const MCFixup &Fixup, MCValue Target,
839-
uint64_t &FixedValue) {
835+
void WinCOFFWriter::recordRelocation(const MCFragment &F, const MCFixup &Fixup,
836+
MCValue Target, uint64_t &FixedValue) {
840837
assert(Target.getAddSym() && "Relocation must reference a symbol!");
841838

842839
const MCSymbol &A = *Target.getAddSym();
@@ -869,10 +866,10 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment &F,
869866
}
870867

871868
// Offset of the symbol in the section
872-
int64_t OffsetOfB = Asm.getSymbolOffset(*B);
869+
int64_t OffsetOfB = Asm->getSymbolOffset(*B);
873870

874871
// Offset of the relocation in the section
875-
int64_t OffsetOfRelocation = Asm.getFragmentOffset(F) + Fixup.getOffset();
872+
int64_t OffsetOfRelocation = Asm->getFragmentOffset(F) + Fixup.getOffset();
876873

877874
FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
878875
} else {
@@ -882,7 +879,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment &F,
882879
COFFRelocation Reloc;
883880

884881
Reloc.Data.SymbolTableIndex = 0;
885-
Reloc.Data.VirtualAddress = Asm.getFragmentOffset(F);
882+
Reloc.Data.VirtualAddress = Asm->getFragmentOffset(F);
886883

887884
// Turn relocations for temporary symbols into section relocations.
888885
if (A.isTemporary() && !SymbolMap[&A]) {
@@ -892,7 +889,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment &F,
892889
"Section must already have been defined in executePostLayoutBinding!");
893890
COFFSection *Section = SectionMap[TargetSection];
894891
Reloc.Symb = Section->Symbol;
895-
FixedValue += Asm.getSymbolOffset(A);
892+
FixedValue += Asm->getSymbolOffset(A);
896893
// Technically, we should do the final adjustments of FixedValue (below)
897894
// before picking an offset symbol, otherwise we might choose one which
898895
// is slightly too far away. The relocations where it really matters
@@ -918,7 +915,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment &F,
918915

919916
Reloc.Data.VirtualAddress += Fixup.getOffset();
920917
Reloc.Data.Type = OWriter.TargetObjectWriter->getRelocType(
921-
getContext(), Target, Fixup, Target.getSubSym(), Asm.getBackend());
918+
getContext(), Target, Fixup, Target.getSubSym(), Asm->getBackend());
922919

923920
// The *_REL32 relocations are relative to the end of the relocation,
924921
// not to the start.
@@ -995,12 +992,12 @@ static std::time_t getTime() {
995992
return Now;
996993
}
997994

998-
uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
995+
uint64_t WinCOFFWriter::writeObject() {
999996
uint64_t StartOffset = W.OS.tell();
1000997

1001998
setWeakDefaultNames();
1002999
if (Mode != DwoOnly)
1003-
createFileSymbols(Asm);
1000+
createFileSymbols();
10041001

10051002
for (auto &Symbol : Symbols) {
10061003
// Update section number & offset for symbols that have them.
@@ -1106,7 +1103,7 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
11061103
}
11071104
}
11081105

1109-
assignFileOffsets(Asm);
1106+
assignFileOffsets();
11101107

11111108
// MS LINK expects to be able to use this timestamp to implement their
11121109
// /INCREMENTAL feature.
@@ -1124,8 +1121,8 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
11241121
#ifndef NDEBUG
11251122
sections::iterator I = Sections.begin();
11261123
sections::iterator IE = Sections.end();
1127-
auto J = Asm.begin();
1128-
auto JE = Asm.end();
1124+
auto J = Asm->begin();
1125+
auto JE = Asm->end();
11291126
for (; I != IE && J != JE; ++I, ++J) {
11301127
while (J != JE && ((Mode == NonDwoOnly && isDwoSection(*J)) ||
11311128
(Mode == DwoOnly && !isDwoSection(*J))))
@@ -1136,7 +1133,7 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
11361133

11371134
// Write section contents.
11381135
for (std::unique_ptr<COFFSection> &Sec : Sections)
1139-
writeSection(Asm, *Sec);
1136+
writeSection(*Sec);
11401137

11411138
assert(W.OS.tell() == Header.PointerToSymbolTable &&
11421139
"Header::PointerToSymbolTable is insane!");
@@ -1170,6 +1167,13 @@ void WinCOFFObjectWriter::reset() {
11701167
MCObjectWriter::reset();
11711168
}
11721169

1170+
void WinCOFFObjectWriter::setAssembler(MCAssembler *Asm) {
1171+
MCObjectWriter::setAssembler(Asm);
1172+
ObjWriter->setAssembler(Asm);
1173+
if (DwoWriter)
1174+
DwoWriter->setAssembler(Asm);
1175+
}
1176+
11731177
bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
11741178
const MCSymbol &SymA, const MCFragment &FB, bool InSet,
11751179
bool IsPCRel) const {
@@ -1186,16 +1190,16 @@ bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
11861190
}
11871191

11881192
void WinCOFFObjectWriter::executePostLayoutBinding() {
1189-
ObjWriter->executePostLayoutBinding(*Asm);
1193+
ObjWriter->executePostLayoutBinding();
11901194
if (DwoWriter)
1191-
DwoWriter->executePostLayoutBinding(*Asm);
1195+
DwoWriter->executePostLayoutBinding();
11921196
}
11931197

11941198
void WinCOFFObjectWriter::recordRelocation(const MCFragment &F,
11951199
const MCFixup &Fixup, MCValue Target,
11961200
uint64_t &FixedValue) {
11971201
assert(!isDwoSection(*F.getParent()) && "No relocation in Dwo sections");
1198-
ObjWriter->recordRelocation(*Asm, F, Fixup, Target, FixedValue);
1202+
ObjWriter->recordRelocation(F, Fixup, Target, FixedValue);
11991203
}
12001204

12011205
uint64_t WinCOFFObjectWriter::writeObject() {
@@ -1204,9 +1208,9 @@ uint64_t WinCOFFObjectWriter::writeObject() {
12041208
if (getContext().hadError())
12051209
return 0;
12061210

1207-
uint64_t TotalSize = ObjWriter->writeObject(*Asm);
1211+
uint64_t TotalSize = ObjWriter->writeObject();
12081212
if (DwoWriter)
1209-
TotalSize += DwoWriter->writeObject(*Asm);
1213+
TotalSize += DwoWriter->writeObject();
12101214
return TotalSize;
12111215
}
12121216

0 commit comments

Comments
 (0)