@@ -123,6 +123,7 @@ class COFFSection {
123
123
class llvm ::WinCOFFWriter {
124
124
WinCOFFObjectWriter &OWriter;
125
125
support::endian::Writer W;
126
+ MCAssembler *Asm = nullptr ;
126
127
127
128
using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
128
129
using sections = std::vector<std::unique_ptr<COFFSection>>;
@@ -158,11 +159,11 @@ class llvm::WinCOFFWriter {
158
159
DwoMode Mode);
159
160
160
161
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 ();
166
167
int getSectionNumber (const MCSection &Section) const ;
167
168
168
169
private:
@@ -171,10 +172,10 @@ class llvm::WinCOFFWriter {
171
172
COFFSymbol *GetOrCreateCOFFSymbol (const MCSymbol *Symbol);
172
173
COFFSection *createSection (StringRef Name);
173
174
174
- void defineSection (const MCAssembler &Asm, MCSectionCOFF const &Sec);
175
+ void defineSection (MCSectionCOFF const &Sec);
175
176
176
177
COFFSymbol *getLinkedSymbol (const MCSymbol &Symbol);
177
- void defineSymbol (const MCAssembler &Asm, const MCSymbol &Symbol);
178
+ void defineSymbol (const MCSymbol &Symbol);
178
179
179
180
void SetSymbolName (COFFSymbol &S);
180
181
void SetSectionName (COFFSection &S);
@@ -187,13 +188,13 @@ class llvm::WinCOFFWriter {
187
188
void WriteAuxiliarySymbols (const COFFSymbol::AuxiliarySymbols &S);
188
189
void writeSectionHeaders ();
189
190
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);
192
193
193
- void createFileSymbols (MCAssembler &Asm );
194
+ void createFileSymbols ();
194
195
void setWeakDefaultNames ();
195
196
void assignSectionNumbers ();
196
- void assignFileOffsets (MCAssembler &Asm );
197
+ void assignFileOffsets ();
197
198
};
198
199
199
200
WinCOFFObjectWriter::WinCOFFObjectWriter (
@@ -292,8 +293,7 @@ static uint32_t getAlignment(const MCSectionCOFF &Sec) {
292
293
293
294
// / This function takes a section data object from the assembler
294
295
// / 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) {
297
297
COFFSection *Section = createSection (MCSec.getName ());
298
298
COFFSymbol *Symbol = createSymbol (MCSec.getName ());
299
299
Section->Symbol = Symbol;
@@ -328,8 +328,8 @@ void WinCOFFWriter::defineSection(const MCAssembler &Asm,
328
328
if (UseOffsetLabels) {
329
329
const uint32_t Interval = 1 << OffsetLabelIntervalBits;
330
330
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) {
333
333
auto Name = (" $L" + MCSec.getName () + " _" + Twine (N++)).str ();
334
334
COFFSymbol *Label = createSymbol (Name);
335
335
Label->Section = Section;
@@ -369,9 +369,8 @@ COFFSymbol *WinCOFFWriter::getLinkedSymbol(const MCSymbol &Symbol) {
369
369
370
370
// / This function takes a symbol data object from the assembler
371
371
// / 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);
375
374
COFFSection *Sec = nullptr ;
376
375
MCSectionCOFF *MCSec = nullptr ;
377
376
if (Base && Base->getFragment ()) {
@@ -418,7 +417,7 @@ void WinCOFFWriter::defineSymbol(const MCAssembler &Asm,
418
417
}
419
418
420
419
if (Local) {
421
- Local->Data .Value = getSymbolValue (MCSym, Asm);
420
+ Local->Data .Value = getSymbolValue (MCSym, * Asm);
422
421
423
422
const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
424
423
Local->Data .Type = SymbolCOFF.getType ();
@@ -575,13 +574,12 @@ void WinCOFFWriter::WriteRelocation(const COFF::relocation &R) {
575
574
// Write MCSec's contents. What this function does is essentially
576
575
// "Asm.writeSectionData(&MCSec)", but it's a bit complicated
577
576
// 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) {
580
578
// Save the contents of the section to a temporary buffer, we need this
581
579
// to CRC the data before we dump it into the object file.
582
580
SmallVector<char , 128 > Buf;
583
581
raw_svector_ostream VecOS (Buf);
584
- Asm. writeSectionData (VecOS, &MCSec);
582
+ Asm-> writeSectionData (VecOS, &MCSec);
585
583
586
584
// Write the section contents to the object file.
587
585
W.OS << Buf;
@@ -593,7 +591,7 @@ uint32_t WinCOFFWriter::writeSectionContents(MCAssembler &Asm,
593
591
return JC.getCRC ();
594
592
}
595
593
596
- void WinCOFFWriter::writeSection (MCAssembler &Asm, const COFFSection &Sec) {
594
+ void WinCOFFWriter::writeSection (const COFFSection &Sec) {
597
595
if (Sec.Number == -1 )
598
596
return ;
599
597
@@ -602,7 +600,7 @@ void WinCOFFWriter::writeSection(MCAssembler &Asm, const COFFSection &Sec) {
602
600
assert (W.OS .tell () == Sec.Header .PointerToRawData &&
603
601
" Section::PointerToRawData is insane!" );
604
602
605
- uint32_t CRC = writeSectionContents (Asm, *Sec.MCSection );
603
+ uint32_t CRC = writeSectionContents (*Sec.MCSection );
606
604
607
605
// Update the section definition auxiliary symbol to record the CRC.
608
606
COFFSymbol::AuxiliarySymbols &AuxSyms = Sec.Symbol ->Aux ;
@@ -636,7 +634,7 @@ void WinCOFFWriter::writeSection(MCAssembler &Asm, const COFFSection &Sec) {
636
634
}
637
635
638
636
// Create .file symbols.
639
- void WinCOFFWriter::createFileSymbols (MCAssembler &Asm ) {
637
+ void WinCOFFWriter::createFileSymbols () {
640
638
for (const std::pair<std::string, size_t > &It : OWriter.getFileNames ()) {
641
639
// round up to calculate the number of auxiliary symbols required
642
640
const std::string &Name = It.first ;
@@ -734,19 +732,19 @@ void WinCOFFWriter::assignSectionNumbers() {
734
732
}
735
733
736
734
// Assign file offsets to COFF object file structures.
737
- void WinCOFFWriter::assignFileOffsets (MCAssembler &Asm ) {
735
+ void WinCOFFWriter::assignFileOffsets () {
738
736
unsigned Offset = W.OS .tell ();
739
737
740
738
Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
741
739
Offset += COFF::SectionSize * Header.NumberOfSections ;
742
740
743
- for (const auto &Section : Asm) {
741
+ for (const auto &Section : * Asm) {
744
742
COFFSection *Sec = SectionMap[&Section];
745
743
746
744
if (!Sec || Sec->Number == -1 )
747
745
continue ;
748
746
749
- Sec->Header .SizeOfRawData = Asm. getSectionAddressSize (Section);
747
+ Sec->Header .SizeOfRawData = Asm-> getSectionAddressSize (Section);
750
748
751
749
if (IsPhysicalSection (Sec)) {
752
750
Sec->Header .PointerToRawData = Offset;
@@ -807,22 +805,22 @@ void WinCOFFWriter::reset() {
807
805
WeakDefaults.clear ();
808
806
}
809
807
810
- void WinCOFFWriter::executePostLayoutBinding (MCAssembler &Asm ) {
808
+ void WinCOFFWriter::executePostLayoutBinding () {
811
809
// "Define" each section & symbol. This creates section & symbol
812
810
// entries in the staging area.
813
- for (const auto &Section : Asm) {
811
+ for (const auto &Section : * Asm) {
814
812
if ((Mode == NonDwoOnly && isDwoSection (Section)) ||
815
813
(Mode == DwoOnly && !isDwoSection (Section)))
816
814
continue ;
817
- defineSection (Asm, static_cast <const MCSectionCOFF &>(Section));
815
+ defineSection (static_cast <const MCSectionCOFF &>(Section));
818
816
}
819
817
820
818
if (Mode != DwoOnly)
821
- for (const MCSymbol &Symbol : Asm. symbols ())
819
+ for (const MCSymbol &Symbol : Asm-> symbols ())
822
820
// Define non-temporary or temporary static (private-linkage) symbols
823
821
if (!Symbol.isTemporary () ||
824
822
cast<MCSymbolCOFF>(Symbol).getClass () == COFF::IMAGE_SYM_CLASS_STATIC)
825
- defineSymbol (Asm, Symbol);
823
+ defineSymbol (Symbol);
826
824
827
825
UseBigObj = Sections.size () > COFF::MaxNumberOfSections16;
828
826
Header.NumberOfSections = Sections.size ();
@@ -834,9 +832,8 @@ void WinCOFFWriter::executePostLayoutBinding(MCAssembler &Asm) {
834
832
assignSectionNumbers ();
835
833
}
836
834
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) {
840
837
assert (Target.getAddSym () && " Relocation must reference a symbol!" );
841
838
842
839
const MCSymbol &A = *Target.getAddSym ();
@@ -869,10 +866,10 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment &F,
869
866
}
870
867
871
868
// Offset of the symbol in the section
872
- int64_t OffsetOfB = Asm. getSymbolOffset (*B);
869
+ int64_t OffsetOfB = Asm-> getSymbolOffset (*B);
873
870
874
871
// 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 ();
876
873
877
874
FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant ();
878
875
} else {
@@ -882,7 +879,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment &F,
882
879
COFFRelocation Reloc;
883
880
884
881
Reloc.Data .SymbolTableIndex = 0 ;
885
- Reloc.Data .VirtualAddress = Asm. getFragmentOffset (F);
882
+ Reloc.Data .VirtualAddress = Asm-> getFragmentOffset (F);
886
883
887
884
// Turn relocations for temporary symbols into section relocations.
888
885
if (A.isTemporary () && !SymbolMap[&A]) {
@@ -892,7 +889,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment &F,
892
889
" Section must already have been defined in executePostLayoutBinding!" );
893
890
COFFSection *Section = SectionMap[TargetSection];
894
891
Reloc.Symb = Section->Symbol ;
895
- FixedValue += Asm. getSymbolOffset (A);
892
+ FixedValue += Asm-> getSymbolOffset (A);
896
893
// Technically, we should do the final adjustments of FixedValue (below)
897
894
// before picking an offset symbol, otherwise we might choose one which
898
895
// is slightly too far away. The relocations where it really matters
@@ -918,7 +915,7 @@ void WinCOFFWriter::recordRelocation(MCAssembler &Asm, const MCFragment &F,
918
915
919
916
Reloc.Data .VirtualAddress += Fixup.getOffset ();
920
917
Reloc.Data .Type = OWriter.TargetObjectWriter ->getRelocType (
921
- getContext (), Target, Fixup, Target.getSubSym (), Asm. getBackend ());
918
+ getContext (), Target, Fixup, Target.getSubSym (), Asm-> getBackend ());
922
919
923
920
// The *_REL32 relocations are relative to the end of the relocation,
924
921
// not to the start.
@@ -995,12 +992,12 @@ static std::time_t getTime() {
995
992
return Now;
996
993
}
997
994
998
- uint64_t WinCOFFWriter::writeObject (MCAssembler &Asm ) {
995
+ uint64_t WinCOFFWriter::writeObject () {
999
996
uint64_t StartOffset = W.OS .tell ();
1000
997
1001
998
setWeakDefaultNames ();
1002
999
if (Mode != DwoOnly)
1003
- createFileSymbols (Asm );
1000
+ createFileSymbols ();
1004
1001
1005
1002
for (auto &Symbol : Symbols) {
1006
1003
// Update section number & offset for symbols that have them.
@@ -1106,7 +1103,7 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
1106
1103
}
1107
1104
}
1108
1105
1109
- assignFileOffsets (Asm );
1106
+ assignFileOffsets ();
1110
1107
1111
1108
// MS LINK expects to be able to use this timestamp to implement their
1112
1109
// /INCREMENTAL feature.
@@ -1124,8 +1121,8 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
1124
1121
#ifndef NDEBUG
1125
1122
sections::iterator I = Sections.begin ();
1126
1123
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 ();
1129
1126
for (; I != IE && J != JE; ++I, ++J) {
1130
1127
while (J != JE && ((Mode == NonDwoOnly && isDwoSection (*J)) ||
1131
1128
(Mode == DwoOnly && !isDwoSection (*J))))
@@ -1136,7 +1133,7 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
1136
1133
1137
1134
// Write section contents.
1138
1135
for (std::unique_ptr<COFFSection> &Sec : Sections)
1139
- writeSection (Asm, *Sec);
1136
+ writeSection (*Sec);
1140
1137
1141
1138
assert (W.OS .tell () == Header.PointerToSymbolTable &&
1142
1139
" Header::PointerToSymbolTable is insane!" );
@@ -1170,6 +1167,13 @@ void WinCOFFObjectWriter::reset() {
1170
1167
MCObjectWriter::reset ();
1171
1168
}
1172
1169
1170
+ void WinCOFFObjectWriter::setAssembler (MCAssembler *Asm) {
1171
+ MCObjectWriter::setAssembler (Asm);
1172
+ ObjWriter->setAssembler (Asm);
1173
+ if (DwoWriter)
1174
+ DwoWriter->setAssembler (Asm);
1175
+ }
1176
+
1173
1177
bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl (
1174
1178
const MCSymbol &SymA, const MCFragment &FB, bool InSet,
1175
1179
bool IsPCRel) const {
@@ -1186,16 +1190,16 @@ bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1186
1190
}
1187
1191
1188
1192
void WinCOFFObjectWriter::executePostLayoutBinding () {
1189
- ObjWriter->executePostLayoutBinding (*Asm );
1193
+ ObjWriter->executePostLayoutBinding ();
1190
1194
if (DwoWriter)
1191
- DwoWriter->executePostLayoutBinding (*Asm );
1195
+ DwoWriter->executePostLayoutBinding ();
1192
1196
}
1193
1197
1194
1198
void WinCOFFObjectWriter::recordRelocation (const MCFragment &F,
1195
1199
const MCFixup &Fixup, MCValue Target,
1196
1200
uint64_t &FixedValue) {
1197
1201
assert (!isDwoSection (*F.getParent ()) && " No relocation in Dwo sections" );
1198
- ObjWriter->recordRelocation (*Asm, F, Fixup, Target, FixedValue);
1202
+ ObjWriter->recordRelocation (F, Fixup, Target, FixedValue);
1199
1203
}
1200
1204
1201
1205
uint64_t WinCOFFObjectWriter::writeObject () {
@@ -1204,9 +1208,9 @@ uint64_t WinCOFFObjectWriter::writeObject() {
1204
1208
if (getContext ().hadError ())
1205
1209
return 0 ;
1206
1210
1207
- uint64_t TotalSize = ObjWriter->writeObject (*Asm );
1211
+ uint64_t TotalSize = ObjWriter->writeObject ();
1208
1212
if (DwoWriter)
1209
- TotalSize += DwoWriter->writeObject (*Asm );
1213
+ TotalSize += DwoWriter->writeObject ();
1210
1214
return TotalSize;
1211
1215
}
1212
1216
0 commit comments