1 //===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains an implementation of a Win32 COFF object file writer.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/DenseSet.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/BinaryFormat/COFF.h"
21 #include "llvm/MC/MCAssembler.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCFixup.h"
25 #include "llvm/MC/MCFragment.h"
26 #include "llvm/MC/MCObjectWriter.h"
27 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCSectionCOFF.h"
29 #include "llvm/MC/MCSymbol.h"
30 #include "llvm/MC/MCSymbolCOFF.h"
31 #include "llvm/MC/MCValue.h"
32 #include "llvm/MC/MCWinCOFFObjectWriter.h"
33 #include "llvm/MC/StringTableBuilder.h"
34 #include "llvm/Support/CRC.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/EndianStream.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/LEB128.h"
39 #include "llvm/Support/MathExtras.h"
40 #include "llvm/Support/raw_ostream.h"
51 using llvm::support::endian::write32le
;
53 #define DEBUG_TYPE "WinCOFFObjectWriter"
57 constexpr int OffsetLabelIntervalBits
= 20;
59 using name
= SmallString
<COFF::NameSize
>;
61 enum AuxiliaryType
{ ATWeakExternal
, ATFile
, ATSectionDefinition
};
64 AuxiliaryType AuxType
;
72 COFF::symbol Data
= {};
74 using AuxiliarySymbols
= SmallVector
<AuxSymbol
, 1>;
79 COFFSymbol
*Other
= nullptr;
80 COFFSection
*Section
= nullptr;
82 const MCSymbol
*MC
= nullptr;
84 COFFSymbol(StringRef Name
) : Name(Name
) {}
86 void set_name_offset(uint32_t Offset
);
88 int64_t getIndex() const { return Index
; }
89 void setIndex(int Value
) {
92 MC
->setIndex(static_cast<uint32_t>(Value
));
96 // This class contains staging data for a COFF relocation entry.
97 struct COFFRelocation
{
98 COFF::relocation Data
;
99 COFFSymbol
*Symb
= nullptr;
101 COFFRelocation() = default;
103 static size_t size() { return COFF::RelocationSize
; }
106 using relocations
= std::vector
<COFFRelocation
>;
110 COFF::section Header
= {};
114 MCSectionCOFF
const *MCSection
= nullptr;
115 COFFSymbol
*Symbol
= nullptr;
116 relocations Relocations
;
118 COFFSection(StringRef Name
) : Name(std::string(Name
)) {}
120 SmallVector
<COFFSymbol
*, 1> OffsetSymbols
;
124 class llvm::WinCOFFWriter
{
125 WinCOFFObjectWriter
&OWriter
;
126 support::endian::Writer W
;
128 using symbols
= std::vector
<std::unique_ptr
<COFFSymbol
>>;
129 using sections
= std::vector
<std::unique_ptr
<COFFSection
>>;
131 using symbol_map
= DenseMap
<MCSymbol
const *, COFFSymbol
*>;
132 using section_map
= DenseMap
<MCSection
const *, COFFSection
*>;
134 using symbol_list
= DenseSet
<COFFSymbol
*>;
136 // Root level file contents.
137 COFF::header Header
= {};
140 StringTableBuilder Strings
{StringTableBuilder::WinCOFF
};
142 // Maps used during object file creation.
143 section_map SectionMap
;
144 symbol_map SymbolMap
;
146 symbol_list WeakDefaults
;
149 bool UseOffsetLabels
= false;
158 WinCOFFWriter(WinCOFFObjectWriter
&OWriter
, raw_pwrite_stream
&OS
,
162 void executePostLayoutBinding(MCAssembler
&Asm
);
163 void recordRelocation(MCAssembler
&Asm
, const MCFragment
*Fragment
,
164 const MCFixup
&Fixup
, MCValue Target
,
165 uint64_t &FixedValue
);
166 uint64_t writeObject(MCAssembler
&Asm
);
169 COFFSymbol
*createSymbol(StringRef Name
);
170 COFFSymbol
*GetOrCreateCOFFSymbol(const MCSymbol
*Symbol
);
171 COFFSection
*createSection(StringRef Name
);
173 void defineSection(const MCAssembler
&Asm
, MCSectionCOFF
const &Sec
);
175 COFFSymbol
*getLinkedSymbol(const MCSymbol
&Symbol
);
176 void defineSymbol(const MCAssembler
&Asm
, const MCSymbol
&Symbol
);
178 void SetSymbolName(COFFSymbol
&S
);
179 void SetSectionName(COFFSection
&S
);
181 bool IsPhysicalSection(COFFSection
*S
);
183 // Entity writing methods.
184 void WriteFileHeader(const COFF::header
&Header
);
185 void WriteSymbol(const COFFSymbol
&S
);
186 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols
&S
);
187 void writeSectionHeaders();
188 void WriteRelocation(const COFF::relocation
&R
);
189 uint32_t writeSectionContents(MCAssembler
&Asm
, const MCSection
&MCSec
);
190 void writeSection(MCAssembler
&Asm
, const COFFSection
&Sec
);
192 void createFileSymbols(MCAssembler
&Asm
);
193 void setWeakDefaultNames();
194 void assignSectionNumbers();
195 void assignFileOffsets(MCAssembler
&Asm
);
198 WinCOFFObjectWriter::WinCOFFObjectWriter(
199 std::unique_ptr
<MCWinCOFFObjectTargetWriter
> MOTW
, raw_pwrite_stream
&OS
)
200 : TargetObjectWriter(std::move(MOTW
)),
201 ObjWriter(std::make_unique
<WinCOFFWriter
>(*this, OS
,
202 WinCOFFWriter::AllSections
)) {}
203 WinCOFFObjectWriter::WinCOFFObjectWriter(
204 std::unique_ptr
<MCWinCOFFObjectTargetWriter
> MOTW
, raw_pwrite_stream
&OS
,
205 raw_pwrite_stream
&DwoOS
)
206 : TargetObjectWriter(std::move(MOTW
)),
207 ObjWriter(std::make_unique
<WinCOFFWriter
>(*this, OS
,
208 WinCOFFWriter::NonDwoOnly
)),
209 DwoWriter(std::make_unique
<WinCOFFWriter
>(*this, DwoOS
,
210 WinCOFFWriter::DwoOnly
)) {}
212 static bool isDwoSection(const MCSection
&Sec
) {
213 return Sec
.getName().ends_with(".dwo");
216 //------------------------------------------------------------------------------
217 // Symbol class implementation
219 // In the case that the name does not fit within 8 bytes, the offset
220 // into the string table is stored in the last 4 bytes instead, leaving
221 // the first 4 bytes as 0.
222 void COFFSymbol::set_name_offset(uint32_t Offset
) {
223 write32le(Data
.Name
+ 0, 0);
224 write32le(Data
.Name
+ 4, Offset
);
227 //------------------------------------------------------------------------------
228 // WinCOFFWriter class implementation
230 WinCOFFWriter::WinCOFFWriter(WinCOFFObjectWriter
&OWriter
,
231 raw_pwrite_stream
&OS
, DwoMode Mode
)
232 : OWriter(OWriter
), W(OS
, llvm::endianness::little
), Mode(Mode
) {
233 Header
.Machine
= OWriter
.TargetObjectWriter
->getMachine();
234 // Some relocations on ARM64 (the 21 bit ADRP relocations) have a slightly
235 // limited range for the immediate offset (+/- 1 MB); create extra offset
236 // label symbols with regular intervals to allow referencing a
237 // non-temporary symbol that is close enough.
238 UseOffsetLabels
= COFF::isAnyArm64(Header
.Machine
);
241 COFFSymbol
*WinCOFFWriter::createSymbol(StringRef Name
) {
242 Symbols
.push_back(std::make_unique
<COFFSymbol
>(Name
));
243 return Symbols
.back().get();
246 COFFSymbol
*WinCOFFWriter::GetOrCreateCOFFSymbol(const MCSymbol
*Symbol
) {
247 COFFSymbol
*&Ret
= SymbolMap
[Symbol
];
249 Ret
= createSymbol(Symbol
->getName());
253 COFFSection
*WinCOFFWriter::createSection(StringRef Name
) {
254 Sections
.emplace_back(std::make_unique
<COFFSection
>(Name
));
255 return Sections
.back().get();
258 static uint32_t getAlignment(const MCSectionCOFF
&Sec
) {
259 switch (Sec
.getAlign().value()) {
261 return COFF::IMAGE_SCN_ALIGN_1BYTES
;
263 return COFF::IMAGE_SCN_ALIGN_2BYTES
;
265 return COFF::IMAGE_SCN_ALIGN_4BYTES
;
267 return COFF::IMAGE_SCN_ALIGN_8BYTES
;
269 return COFF::IMAGE_SCN_ALIGN_16BYTES
;
271 return COFF::IMAGE_SCN_ALIGN_32BYTES
;
273 return COFF::IMAGE_SCN_ALIGN_64BYTES
;
275 return COFF::IMAGE_SCN_ALIGN_128BYTES
;
277 return COFF::IMAGE_SCN_ALIGN_256BYTES
;
279 return COFF::IMAGE_SCN_ALIGN_512BYTES
;
281 return COFF::IMAGE_SCN_ALIGN_1024BYTES
;
283 return COFF::IMAGE_SCN_ALIGN_2048BYTES
;
285 return COFF::IMAGE_SCN_ALIGN_4096BYTES
;
287 return COFF::IMAGE_SCN_ALIGN_8192BYTES
;
289 llvm_unreachable("unsupported section alignment");
292 /// This function takes a section data object from the assembler
293 /// and creates the associated COFF section staging object.
294 void WinCOFFWriter::defineSection(const MCAssembler
&Asm
,
295 const MCSectionCOFF
&MCSec
) {
296 COFFSection
*Section
= createSection(MCSec
.getName());
297 COFFSymbol
*Symbol
= createSymbol(MCSec
.getName());
298 Section
->Symbol
= Symbol
;
299 SymbolMap
[MCSec
.getBeginSymbol()] = Symbol
;
300 Symbol
->Section
= Section
;
301 Symbol
->Data
.StorageClass
= COFF::IMAGE_SYM_CLASS_STATIC
;
303 // Create a COMDAT symbol if needed.
304 if (MCSec
.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
) {
305 if (const MCSymbol
*S
= MCSec
.getCOMDATSymbol()) {
306 COFFSymbol
*COMDATSymbol
= GetOrCreateCOFFSymbol(S
);
307 if (COMDATSymbol
->Section
)
308 report_fatal_error("two sections have the same comdat");
309 COMDATSymbol
->Section
= Section
;
313 // In this case the auxiliary symbol is a Section Definition.
314 Symbol
->Aux
.resize(1);
316 Symbol
->Aux
[0].AuxType
= ATSectionDefinition
;
317 Symbol
->Aux
[0].Aux
.SectionDefinition
.Selection
= MCSec
.getSelection();
319 // Set section alignment.
320 Section
->Header
.Characteristics
= MCSec
.getCharacteristics();
321 Section
->Header
.Characteristics
|= getAlignment(MCSec
);
323 // Bind internal COFF section to MC section.
324 Section
->MCSection
= &MCSec
;
325 SectionMap
[&MCSec
] = Section
;
327 if (UseOffsetLabels
&& !MCSec
.empty()) {
328 const uint32_t Interval
= 1 << OffsetLabelIntervalBits
;
330 for (uint32_t Off
= Interval
, E
= Asm
.getSectionAddressSize(MCSec
); Off
< E
;
332 auto Name
= ("$L" + MCSec
.getName() + "_" + Twine(N
++)).str();
333 COFFSymbol
*Label
= createSymbol(Name
);
334 Label
->Section
= Section
;
335 Label
->Data
.StorageClass
= COFF::IMAGE_SYM_CLASS_LABEL
;
336 Label
->Data
.Value
= Off
;
337 Section
->OffsetSymbols
.push_back(Label
);
342 static uint64_t getSymbolValue(const MCSymbol
&Symbol
, const MCAssembler
&Asm
) {
343 if (Symbol
.isCommon() && Symbol
.isExternal())
344 return Symbol
.getCommonSize();
347 if (!Asm
.getSymbolOffset(Symbol
, Res
))
353 COFFSymbol
*WinCOFFWriter::getLinkedSymbol(const MCSymbol
&Symbol
) {
354 if (!Symbol
.isVariable())
357 const MCSymbolRefExpr
*SymRef
=
358 dyn_cast
<MCSymbolRefExpr
>(Symbol
.getVariableValue());
362 const MCSymbol
&Aliasee
= SymRef
->getSymbol();
363 if (Aliasee
.isUndefined() || Aliasee
.isExternal())
364 return GetOrCreateCOFFSymbol(&Aliasee
);
369 /// This function takes a symbol data object from the assembler
370 /// and creates the associated COFF symbol staging object.
371 void WinCOFFWriter::defineSymbol(const MCAssembler
&Asm
,
372 const MCSymbol
&MCSym
) {
373 const MCSymbol
*Base
= Asm
.getBaseSymbol(MCSym
);
374 COFFSection
*Sec
= nullptr;
375 MCSectionCOFF
*MCSec
= nullptr;
376 if (Base
&& Base
->getFragment()) {
377 MCSec
= cast
<MCSectionCOFF
>(Base
->getFragment()->getParent());
378 Sec
= SectionMap
[MCSec
];
381 if (Mode
== NonDwoOnly
&& MCSec
&& isDwoSection(*MCSec
))
384 COFFSymbol
*Sym
= GetOrCreateCOFFSymbol(&MCSym
);
385 COFFSymbol
*Local
= nullptr;
386 if (cast
<MCSymbolCOFF
>(MCSym
).getWeakExternalCharacteristics()) {
387 Sym
->Data
.StorageClass
= COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL
;
388 Sym
->Section
= nullptr;
390 COFFSymbol
*WeakDefault
= getLinkedSymbol(MCSym
);
392 std::string WeakName
= (".weak." + MCSym
.getName() + ".default").str();
393 WeakDefault
= createSymbol(WeakName
);
395 WeakDefault
->Data
.SectionNumber
= COFF::IMAGE_SYM_ABSOLUTE
;
397 WeakDefault
->Section
= Sec
;
398 WeakDefaults
.insert(WeakDefault
);
402 Sym
->Other
= WeakDefault
;
404 // Setup the Weak External auxiliary symbol.
406 memset(&Sym
->Aux
[0], 0, sizeof(Sym
->Aux
[0]));
407 Sym
->Aux
[0].AuxType
= ATWeakExternal
;
408 Sym
->Aux
[0].Aux
.WeakExternal
.TagIndex
= 0; // Filled in later
409 Sym
->Aux
[0].Aux
.WeakExternal
.Characteristics
=
410 cast
<MCSymbolCOFF
>(MCSym
).getWeakExternalCharacteristics();
413 Sym
->Data
.SectionNumber
= COFF::IMAGE_SYM_ABSOLUTE
;
420 Local
->Data
.Value
= getSymbolValue(MCSym
, Asm
);
422 const MCSymbolCOFF
&SymbolCOFF
= cast
<MCSymbolCOFF
>(MCSym
);
423 Local
->Data
.Type
= SymbolCOFF
.getType();
424 Local
->Data
.StorageClass
= SymbolCOFF
.getClass();
426 // If no storage class was specified in the streamer, define it here.
427 if (Local
->Data
.StorageClass
== COFF::IMAGE_SYM_CLASS_NULL
) {
429 MCSym
.isExternal() || (!MCSym
.getFragment() && !MCSym
.isVariable());
431 Local
->Data
.StorageClass
= IsExternal
? COFF::IMAGE_SYM_CLASS_EXTERNAL
432 : COFF::IMAGE_SYM_CLASS_STATIC
;
439 void WinCOFFWriter::SetSectionName(COFFSection
&S
) {
440 if (S
.Name
.size() <= COFF::NameSize
) {
441 std::memcpy(S
.Header
.Name
, S
.Name
.c_str(), S
.Name
.size());
445 uint64_t StringTableEntry
= Strings
.getOffset(S
.Name
);
446 if (!COFF::encodeSectionName(S
.Header
.Name
, StringTableEntry
))
447 report_fatal_error("COFF string table is greater than 64 GB.");
450 void WinCOFFWriter::SetSymbolName(COFFSymbol
&S
) {
451 if (S
.Name
.size() > COFF::NameSize
)
452 S
.set_name_offset(Strings
.getOffset(S
.Name
));
454 std::memcpy(S
.Data
.Name
, S
.Name
.c_str(), S
.Name
.size());
457 bool WinCOFFWriter::IsPhysicalSection(COFFSection
*S
) {
458 return (S
->Header
.Characteristics
& COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
) ==
462 //------------------------------------------------------------------------------
463 // entity writing methods
465 void WinCOFFWriter::WriteFileHeader(const COFF::header
&Header
) {
467 W
.write
<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN
);
468 W
.write
<uint16_t>(0xFFFF);
469 W
.write
<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion
);
470 W
.write
<uint16_t>(Header
.Machine
);
471 W
.write
<uint32_t>(Header
.TimeDateStamp
);
472 W
.OS
.write(COFF::BigObjMagic
, sizeof(COFF::BigObjMagic
));
473 W
.write
<uint32_t>(0);
474 W
.write
<uint32_t>(0);
475 W
.write
<uint32_t>(0);
476 W
.write
<uint32_t>(0);
477 W
.write
<uint32_t>(Header
.NumberOfSections
);
478 W
.write
<uint32_t>(Header
.PointerToSymbolTable
);
479 W
.write
<uint32_t>(Header
.NumberOfSymbols
);
481 W
.write
<uint16_t>(Header
.Machine
);
482 W
.write
<uint16_t>(static_cast<int16_t>(Header
.NumberOfSections
));
483 W
.write
<uint32_t>(Header
.TimeDateStamp
);
484 W
.write
<uint32_t>(Header
.PointerToSymbolTable
);
485 W
.write
<uint32_t>(Header
.NumberOfSymbols
);
486 W
.write
<uint16_t>(Header
.SizeOfOptionalHeader
);
487 W
.write
<uint16_t>(Header
.Characteristics
);
491 void WinCOFFWriter::WriteSymbol(const COFFSymbol
&S
) {
492 W
.OS
.write(S
.Data
.Name
, COFF::NameSize
);
493 W
.write
<uint32_t>(S
.Data
.Value
);
495 W
.write
<uint32_t>(S
.Data
.SectionNumber
);
497 W
.write
<uint16_t>(static_cast<int16_t>(S
.Data
.SectionNumber
));
498 W
.write
<uint16_t>(S
.Data
.Type
);
499 W
.OS
<< char(S
.Data
.StorageClass
);
500 W
.OS
<< char(S
.Data
.NumberOfAuxSymbols
);
501 WriteAuxiliarySymbols(S
.Aux
);
504 void WinCOFFWriter::WriteAuxiliarySymbols(
505 const COFFSymbol::AuxiliarySymbols
&S
) {
506 for (const AuxSymbol
&i
: S
) {
509 W
.write
<uint32_t>(i
.Aux
.WeakExternal
.TagIndex
);
510 W
.write
<uint32_t>(i
.Aux
.WeakExternal
.Characteristics
);
511 W
.OS
.write_zeros(sizeof(i
.Aux
.WeakExternal
.unused
));
513 W
.OS
.write_zeros(COFF::Symbol32Size
- COFF::Symbol16Size
);
516 W
.OS
.write(reinterpret_cast<const char *>(&i
.Aux
),
517 UseBigObj
? COFF::Symbol32Size
: COFF::Symbol16Size
);
519 case ATSectionDefinition
:
520 W
.write
<uint32_t>(i
.Aux
.SectionDefinition
.Length
);
521 W
.write
<uint16_t>(i
.Aux
.SectionDefinition
.NumberOfRelocations
);
522 W
.write
<uint16_t>(i
.Aux
.SectionDefinition
.NumberOfLinenumbers
);
523 W
.write
<uint32_t>(i
.Aux
.SectionDefinition
.CheckSum
);
524 W
.write
<uint16_t>(static_cast<int16_t>(i
.Aux
.SectionDefinition
.Number
));
525 W
.OS
<< char(i
.Aux
.SectionDefinition
.Selection
);
526 W
.OS
.write_zeros(sizeof(i
.Aux
.SectionDefinition
.unused
));
528 static_cast<int16_t>(i
.Aux
.SectionDefinition
.Number
>> 16));
530 W
.OS
.write_zeros(COFF::Symbol32Size
- COFF::Symbol16Size
);
536 // Write the section header.
537 void WinCOFFWriter::writeSectionHeaders() {
538 // Section numbers must be monotonically increasing in the section
539 // header, but our Sections array is not sorted by section number,
540 // so make a copy of Sections and sort it.
541 std::vector
<COFFSection
*> Arr
;
542 for (auto &Section
: Sections
)
543 Arr
.push_back(Section
.get());
544 llvm::sort(Arr
, [](const COFFSection
*A
, const COFFSection
*B
) {
545 return A
->Number
< B
->Number
;
548 for (auto &Section
: Arr
) {
549 if (Section
->Number
== -1)
552 COFF::section
&S
= Section
->Header
;
553 if (Section
->Relocations
.size() >= 0xffff)
554 S
.Characteristics
|= COFF::IMAGE_SCN_LNK_NRELOC_OVFL
;
555 W
.OS
.write(S
.Name
, COFF::NameSize
);
556 W
.write
<uint32_t>(S
.VirtualSize
);
557 W
.write
<uint32_t>(S
.VirtualAddress
);
558 W
.write
<uint32_t>(S
.SizeOfRawData
);
559 W
.write
<uint32_t>(S
.PointerToRawData
);
560 W
.write
<uint32_t>(S
.PointerToRelocations
);
561 W
.write
<uint32_t>(S
.PointerToLineNumbers
);
562 W
.write
<uint16_t>(S
.NumberOfRelocations
);
563 W
.write
<uint16_t>(S
.NumberOfLineNumbers
);
564 W
.write
<uint32_t>(S
.Characteristics
);
568 void WinCOFFWriter::WriteRelocation(const COFF::relocation
&R
) {
569 W
.write
<uint32_t>(R
.VirtualAddress
);
570 W
.write
<uint32_t>(R
.SymbolTableIndex
);
571 W
.write
<uint16_t>(R
.Type
);
574 // Write MCSec's contents. What this function does is essentially
575 // "Asm.writeSectionData(&MCSec)", but it's a bit complicated
576 // because it needs to compute a CRC.
577 uint32_t WinCOFFWriter::writeSectionContents(MCAssembler
&Asm
,
578 const MCSection
&MCSec
) {
579 // Save the contents of the section to a temporary buffer, we need this
580 // to CRC the data before we dump it into the object file.
581 SmallVector
<char, 128> Buf
;
582 raw_svector_ostream
VecOS(Buf
);
583 Asm
.writeSectionData(VecOS
, &MCSec
);
585 // Write the section contents to the object file.
588 // Calculate our CRC with an initial value of '0', this is not how
589 // JamCRC is specified but it aligns with the expected output.
590 JamCRC
JC(/*Init=*/0);
591 JC
.update(ArrayRef(reinterpret_cast<uint8_t *>(Buf
.data()), Buf
.size()));
595 void WinCOFFWriter::writeSection(MCAssembler
&Asm
, const COFFSection
&Sec
) {
596 if (Sec
.Number
== -1)
599 // Write the section contents.
600 if (Sec
.Header
.PointerToRawData
!= 0) {
601 assert(W
.OS
.tell() == Sec
.Header
.PointerToRawData
&&
602 "Section::PointerToRawData is insane!");
604 uint32_t CRC
= writeSectionContents(Asm
, *Sec
.MCSection
);
606 // Update the section definition auxiliary symbol to record the CRC.
607 COFFSymbol::AuxiliarySymbols
&AuxSyms
= Sec
.Symbol
->Aux
;
608 assert(AuxSyms
.size() == 1 && AuxSyms
[0].AuxType
== ATSectionDefinition
);
609 AuxSymbol
&SecDef
= AuxSyms
[0];
610 SecDef
.Aux
.SectionDefinition
.CheckSum
= CRC
;
613 // Write relocations for this section.
614 if (Sec
.Relocations
.empty()) {
615 assert(Sec
.Header
.PointerToRelocations
== 0 &&
616 "Section::PointerToRelocations is insane!");
620 assert(W
.OS
.tell() == Sec
.Header
.PointerToRelocations
&&
621 "Section::PointerToRelocations is insane!");
623 if (Sec
.Relocations
.size() >= 0xffff) {
624 // In case of overflow, write actual relocation count as first
625 // relocation. Including the synthetic reloc itself (+ 1).
627 R
.VirtualAddress
= Sec
.Relocations
.size() + 1;
628 R
.SymbolTableIndex
= 0;
633 for (const auto &Relocation
: Sec
.Relocations
)
634 WriteRelocation(Relocation
.Data
);
637 // Create .file symbols.
638 void WinCOFFWriter::createFileSymbols(MCAssembler
&Asm
) {
639 for (const std::pair
<std::string
, size_t> &It
: OWriter
.getFileNames()) {
640 // round up to calculate the number of auxiliary symbols required
641 const std::string
&Name
= It
.first
;
642 unsigned SymbolSize
= UseBigObj
? COFF::Symbol32Size
: COFF::Symbol16Size
;
643 unsigned Count
= (Name
.size() + SymbolSize
- 1) / SymbolSize
;
645 COFFSymbol
*File
= createSymbol(".file");
646 File
->Data
.SectionNumber
= COFF::IMAGE_SYM_DEBUG
;
647 File
->Data
.StorageClass
= COFF::IMAGE_SYM_CLASS_FILE
;
648 File
->Aux
.resize(Count
);
651 unsigned Length
= Name
.size();
652 for (auto &Aux
: File
->Aux
) {
653 Aux
.AuxType
= ATFile
;
655 if (Length
> SymbolSize
) {
656 memcpy(&Aux
.Aux
, Name
.c_str() + Offset
, SymbolSize
);
657 Length
= Length
- SymbolSize
;
659 memcpy(&Aux
.Aux
, Name
.c_str() + Offset
, Length
);
660 memset((char *)&Aux
.Aux
+ Length
, 0, SymbolSize
- Length
);
664 Offset
+= SymbolSize
;
669 void WinCOFFWriter::setWeakDefaultNames() {
670 if (WeakDefaults
.empty())
673 // If multiple object files use a weak symbol (either with a regular
674 // defined default, or an absolute zero symbol as default), the defaults
675 // cause duplicate definitions unless their names are made unique. Look
676 // for a defined extern symbol, that isn't comdat - that should be unique
677 // unless there are other duplicate definitions. And if none is found,
678 // allow picking a comdat symbol, as that's still better than nothing.
680 COFFSymbol
*Unique
= nullptr;
681 for (bool AllowComdat
: {false, true}) {
682 for (auto &Sym
: Symbols
) {
683 // Don't include the names of the defaults themselves
684 if (WeakDefaults
.count(Sym
.get()))
686 // Only consider external symbols
687 if (Sym
->Data
.StorageClass
!= COFF::IMAGE_SYM_CLASS_EXTERNAL
)
689 // Only consider symbols defined in a section or that are absolute
690 if (!Sym
->Section
&& Sym
->Data
.SectionNumber
!= COFF::IMAGE_SYM_ABSOLUTE
)
692 if (!AllowComdat
&& Sym
->Section
&&
693 Sym
->Section
->Header
.Characteristics
& COFF::IMAGE_SCN_LNK_COMDAT
)
701 // If we didn't find any unique symbol to use for the names, just skip this.
704 for (auto *Sym
: WeakDefaults
) {
705 Sym
->Name
.append(".");
706 Sym
->Name
.append(Unique
->Name
);
710 static bool isAssociative(const COFFSection
&Section
) {
711 return Section
.Symbol
->Aux
[0].Aux
.SectionDefinition
.Selection
==
712 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
;
715 void WinCOFFWriter::assignSectionNumbers() {
717 auto Assign
= [&](COFFSection
&Section
) {
719 Section
.Symbol
->Data
.SectionNumber
= I
;
720 Section
.Symbol
->Aux
[0].Aux
.SectionDefinition
.Number
= I
;
724 // Although it is not explicitly requested by the Microsoft COFF spec,
725 // we should avoid emitting forward associative section references,
726 // because MSVC link.exe as of 2017 cannot handle that.
727 for (const std::unique_ptr
<COFFSection
> &Section
: Sections
)
728 if (!isAssociative(*Section
))
730 for (const std::unique_ptr
<COFFSection
> &Section
: Sections
)
731 if (isAssociative(*Section
))
735 // Assign file offsets to COFF object file structures.
736 void WinCOFFWriter::assignFileOffsets(MCAssembler
&Asm
) {
737 unsigned Offset
= W
.OS
.tell();
739 Offset
+= UseBigObj
? COFF::Header32Size
: COFF::Header16Size
;
740 Offset
+= COFF::SectionSize
* Header
.NumberOfSections
;
742 for (const auto &Section
: Asm
) {
743 COFFSection
*Sec
= SectionMap
[&Section
];
745 if (!Sec
|| Sec
->Number
== -1)
748 Sec
->Header
.SizeOfRawData
= Asm
.getSectionAddressSize(Section
);
750 if (IsPhysicalSection(Sec
)) {
751 Sec
->Header
.PointerToRawData
= Offset
;
752 Offset
+= Sec
->Header
.SizeOfRawData
;
755 if (!Sec
->Relocations
.empty()) {
756 bool RelocationsOverflow
= Sec
->Relocations
.size() >= 0xffff;
758 if (RelocationsOverflow
) {
759 // Signal overflow by setting NumberOfRelocations to max value. Actual
760 // size is found in reloc #0. Microsoft tools understand this.
761 Sec
->Header
.NumberOfRelocations
= 0xffff;
763 Sec
->Header
.NumberOfRelocations
= Sec
->Relocations
.size();
765 Sec
->Header
.PointerToRelocations
= Offset
;
767 if (RelocationsOverflow
) {
768 // Reloc #0 will contain actual count, so make room for it.
769 Offset
+= COFF::RelocationSize
;
772 Offset
+= COFF::RelocationSize
* Sec
->Relocations
.size();
774 for (auto &Relocation
: Sec
->Relocations
) {
775 assert(Relocation
.Symb
->getIndex() != -1);
776 Relocation
.Data
.SymbolTableIndex
= Relocation
.Symb
->getIndex();
780 assert(Sec
->Symbol
->Aux
.size() == 1 &&
781 "Section's symbol must have one aux!");
782 AuxSymbol
&Aux
= Sec
->Symbol
->Aux
[0];
783 assert(Aux
.AuxType
== ATSectionDefinition
&&
784 "Section's symbol's aux symbol must be a Section Definition!");
785 Aux
.Aux
.SectionDefinition
.Length
= Sec
->Header
.SizeOfRawData
;
786 Aux
.Aux
.SectionDefinition
.NumberOfRelocations
=
787 Sec
->Header
.NumberOfRelocations
;
788 Aux
.Aux
.SectionDefinition
.NumberOfLinenumbers
=
789 Sec
->Header
.NumberOfLineNumbers
;
792 Header
.PointerToSymbolTable
= Offset
;
795 void WinCOFFWriter::reset() {
796 memset(&Header
, 0, sizeof(Header
));
797 Header
.Machine
= OWriter
.TargetObjectWriter
->getMachine();
803 WeakDefaults
.clear();
806 void WinCOFFWriter::executePostLayoutBinding(MCAssembler
&Asm
) {
807 // "Define" each section & symbol. This creates section & symbol
808 // entries in the staging area.
809 for (const auto &Section
: Asm
) {
810 if ((Mode
== NonDwoOnly
&& isDwoSection(Section
)) ||
811 (Mode
== DwoOnly
&& !isDwoSection(Section
)))
813 defineSection(Asm
, static_cast<const MCSectionCOFF
&>(Section
));
817 for (const MCSymbol
&Symbol
: Asm
.symbols())
818 // Define non-temporary or temporary static (private-linkage) symbols
819 if (!Symbol
.isTemporary() ||
820 cast
<MCSymbolCOFF
>(Symbol
).getClass() == COFF::IMAGE_SYM_CLASS_STATIC
)
821 defineSymbol(Asm
, Symbol
);
824 void WinCOFFWriter::recordRelocation(MCAssembler
&Asm
,
825 const MCFragment
*Fragment
,
826 const MCFixup
&Fixup
, MCValue Target
,
827 uint64_t &FixedValue
) {
828 assert(Target
.getSymA() && "Relocation must reference a symbol!");
830 const MCSymbol
&A
= Target
.getSymA()->getSymbol();
831 if (!A
.isRegistered()) {
832 Asm
.getContext().reportError(Fixup
.getLoc(), Twine("symbol '") +
834 "' can not be undefined");
837 if (A
.isTemporary() && A
.isUndefined()) {
838 Asm
.getContext().reportError(Fixup
.getLoc(), Twine("assembler label '") +
840 "' can not be undefined");
844 MCSection
*MCSec
= Fragment
->getParent();
846 // Mark this symbol as requiring an entry in the symbol table.
847 assert(SectionMap
.contains(MCSec
) &&
848 "Section must already have been defined in executePostLayoutBinding!");
850 COFFSection
*Sec
= SectionMap
[MCSec
];
851 const MCSymbolRefExpr
*SymB
= Target
.getSymB();
854 const MCSymbol
*B
= &SymB
->getSymbol();
855 if (!B
->getFragment()) {
856 Asm
.getContext().reportError(
858 Twine("symbol '") + B
->getName() +
859 "' can not be undefined in a subtraction expression");
863 // Offset of the symbol in the section
864 int64_t OffsetOfB
= Asm
.getSymbolOffset(*B
);
866 // Offset of the relocation in the section
867 int64_t OffsetOfRelocation
=
868 Asm
.getFragmentOffset(*Fragment
) + Fixup
.getOffset();
870 FixedValue
= (OffsetOfRelocation
- OffsetOfB
) + Target
.getConstant();
872 FixedValue
= Target
.getConstant();
875 COFFRelocation Reloc
;
877 Reloc
.Data
.SymbolTableIndex
= 0;
878 Reloc
.Data
.VirtualAddress
= Asm
.getFragmentOffset(*Fragment
);
880 // Turn relocations for temporary symbols into section relocations.
881 if (A
.isTemporary() && !SymbolMap
[&A
]) {
882 MCSection
*TargetSection
= &A
.getSection();
884 SectionMap
.contains(TargetSection
) &&
885 "Section must already have been defined in executePostLayoutBinding!");
886 COFFSection
*Section
= SectionMap
[TargetSection
];
887 Reloc
.Symb
= Section
->Symbol
;
888 FixedValue
+= Asm
.getSymbolOffset(A
);
889 // Technically, we should do the final adjustments of FixedValue (below)
890 // before picking an offset symbol, otherwise we might choose one which
891 // is slightly too far away. The relocations where it really matters
892 // (arm64 adrp relocations) don't get any offset though.
893 if (UseOffsetLabels
&& !Section
->OffsetSymbols
.empty()) {
894 uint64_t LabelIndex
= FixedValue
>> OffsetLabelIntervalBits
;
895 if (LabelIndex
> 0) {
896 if (LabelIndex
<= Section
->OffsetSymbols
.size())
897 Reloc
.Symb
= Section
->OffsetSymbols
[LabelIndex
- 1];
899 Reloc
.Symb
= Section
->OffsetSymbols
.back();
900 FixedValue
-= Reloc
.Symb
->Data
.Value
;
905 SymbolMap
.contains(&A
) &&
906 "Symbol must already have been defined in executePostLayoutBinding!");
907 Reloc
.Symb
= SymbolMap
[&A
];
910 ++Reloc
.Symb
->Relocations
;
912 Reloc
.Data
.VirtualAddress
+= Fixup
.getOffset();
913 Reloc
.Data
.Type
= OWriter
.TargetObjectWriter
->getRelocType(
914 Asm
.getContext(), Target
, Fixup
, SymB
, Asm
.getBackend());
916 // The *_REL32 relocations are relative to the end of the relocation,
918 if ((Header
.Machine
== COFF::IMAGE_FILE_MACHINE_AMD64
&&
919 Reloc
.Data
.Type
== COFF::IMAGE_REL_AMD64_REL32
) ||
920 (Header
.Machine
== COFF::IMAGE_FILE_MACHINE_I386
&&
921 Reloc
.Data
.Type
== COFF::IMAGE_REL_I386_REL32
) ||
922 (Header
.Machine
== COFF::IMAGE_FILE_MACHINE_ARMNT
&&
923 Reloc
.Data
.Type
== COFF::IMAGE_REL_ARM_REL32
) ||
924 (COFF::isAnyArm64(Header
.Machine
) &&
925 Reloc
.Data
.Type
== COFF::IMAGE_REL_ARM64_REL32
))
928 if (Header
.Machine
== COFF::IMAGE_FILE_MACHINE_ARMNT
) {
929 switch (Reloc
.Data
.Type
) {
930 case COFF::IMAGE_REL_ARM_ABSOLUTE
:
931 case COFF::IMAGE_REL_ARM_ADDR32
:
932 case COFF::IMAGE_REL_ARM_ADDR32NB
:
933 case COFF::IMAGE_REL_ARM_TOKEN
:
934 case COFF::IMAGE_REL_ARM_SECTION
:
935 case COFF::IMAGE_REL_ARM_SECREL
:
937 case COFF::IMAGE_REL_ARM_BRANCH11
:
938 case COFF::IMAGE_REL_ARM_BLX11
:
939 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
940 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
942 case COFF::IMAGE_REL_ARM_BRANCH24
:
943 case COFF::IMAGE_REL_ARM_BLX24
:
944 case COFF::IMAGE_REL_ARM_MOV32A
:
945 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
946 // only used for ARM mode code, which is documented as being unsupported
947 // by Windows on ARM. Empirical proof indicates that masm is able to
948 // generate the relocations however the rest of the MSVC toolchain is
949 // unable to handle it.
950 llvm_unreachable("unsupported relocation");
952 case COFF::IMAGE_REL_ARM_MOV32T
:
954 case COFF::IMAGE_REL_ARM_BRANCH20T
:
955 case COFF::IMAGE_REL_ARM_BRANCH24T
:
956 case COFF::IMAGE_REL_ARM_BLX23T
:
957 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
958 // perform a 4 byte adjustment to the relocation. Relative branches are
959 // offset by 4 on ARM, however, because there is no RELA relocations, all
960 // branches are offset by 4.
961 FixedValue
= FixedValue
+ 4;
966 // The fixed value never makes sense for section indices, ignore it.
967 if (Fixup
.getKind() == FK_SecRel_2
)
970 if (OWriter
.TargetObjectWriter
->recordRelocation(Fixup
))
971 Sec
->Relocations
.push_back(Reloc
);
974 static std::time_t getTime() {
975 std::time_t Now
= time(nullptr);
976 if (Now
< 0 || !isUInt
<32>(Now
))
981 uint64_t WinCOFFWriter::writeObject(MCAssembler
&Asm
) {
982 uint64_t StartOffset
= W
.OS
.tell();
984 if (Sections
.size() > INT32_MAX
)
986 "PE COFF object files can't have more than 2147483647 sections");
988 UseBigObj
= Sections
.size() > COFF::MaxNumberOfSections16
;
989 Header
.NumberOfSections
= Sections
.size();
990 Header
.NumberOfSymbols
= 0;
992 setWeakDefaultNames();
993 assignSectionNumbers();
995 createFileSymbols(Asm
);
997 for (auto &Symbol
: Symbols
) {
998 // Update section number & offset for symbols that have them.
1000 Symbol
->Data
.SectionNumber
= Symbol
->Section
->Number
;
1001 Symbol
->setIndex(Header
.NumberOfSymbols
++);
1002 // Update auxiliary symbol info.
1003 Symbol
->Data
.NumberOfAuxSymbols
= Symbol
->Aux
.size();
1004 Header
.NumberOfSymbols
+= Symbol
->Data
.NumberOfAuxSymbols
;
1007 // Build string table.
1008 for (const auto &S
: Sections
)
1009 if (S
->Name
.size() > COFF::NameSize
)
1010 Strings
.add(S
->Name
);
1011 for (const auto &S
: Symbols
)
1012 if (S
->Name
.size() > COFF::NameSize
)
1013 Strings
.add(S
->Name
);
1017 for (const auto &S
: Sections
)
1019 for (auto &S
: Symbols
)
1022 // Fixup weak external references.
1023 for (auto &Symbol
: Symbols
) {
1024 if (Symbol
->Other
) {
1025 assert(Symbol
->getIndex() != -1);
1026 assert(Symbol
->Aux
.size() == 1 && "Symbol must contain one aux symbol!");
1027 assert(Symbol
->Aux
[0].AuxType
== ATWeakExternal
&&
1028 "Symbol's aux symbol must be a Weak External!");
1029 Symbol
->Aux
[0].Aux
.WeakExternal
.TagIndex
= Symbol
->Other
->getIndex();
1033 // Fixup associative COMDAT sections.
1034 for (auto &Section
: Sections
) {
1035 if (Section
->Symbol
->Aux
[0].Aux
.SectionDefinition
.Selection
!=
1036 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
)
1039 const MCSectionCOFF
&MCSec
= *Section
->MCSection
;
1040 const MCSymbol
*AssocMCSym
= MCSec
.getCOMDATSymbol();
1043 // It's an error to try to associate with an undefined symbol or a symbol
1044 // without a section.
1045 if (!AssocMCSym
->isInSection()) {
1046 Asm
.getContext().reportError(
1047 SMLoc(), Twine("cannot make section ") + MCSec
.getName() +
1048 Twine(" associative with sectionless symbol ") +
1049 AssocMCSym
->getName());
1053 const auto *AssocMCSec
= cast
<MCSectionCOFF
>(&AssocMCSym
->getSection());
1054 assert(SectionMap
.count(AssocMCSec
));
1055 COFFSection
*AssocSec
= SectionMap
[AssocMCSec
];
1057 // Skip this section if the associated section is unused.
1058 if (AssocSec
->Number
== -1)
1061 Section
->Symbol
->Aux
[0].Aux
.SectionDefinition
.Number
= AssocSec
->Number
;
1064 // Create the contents of the .llvm_addrsig section.
1065 if (Mode
!= DwoOnly
&& OWriter
.getEmitAddrsigSection()) {
1066 auto *Sec
= Asm
.getContext().getCOFFSection(
1067 ".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE
);
1068 auto *Frag
= cast
<MCDataFragment
>(Sec
->curFragList()->Head
);
1069 raw_svector_ostream
OS(Frag
->getContents());
1070 for (const MCSymbol
*S
: OWriter
.AddrsigSyms
) {
1071 if (!S
->isRegistered())
1073 if (!S
->isTemporary()) {
1074 encodeULEB128(S
->getIndex(), OS
);
1078 MCSection
*TargetSection
= &S
->getSection();
1079 assert(SectionMap
.contains(TargetSection
) &&
1080 "Section must already have been defined in "
1081 "executePostLayoutBinding!");
1082 encodeULEB128(SectionMap
[TargetSection
]->Symbol
->getIndex(), OS
);
1086 // Create the contents of the .llvm.call-graph-profile section.
1087 if (Mode
!= DwoOnly
&& !OWriter
.getCGProfile().empty()) {
1088 auto *Sec
= Asm
.getContext().getCOFFSection(
1089 ".llvm.call-graph-profile", COFF::IMAGE_SCN_LNK_REMOVE
);
1090 auto *Frag
= cast
<MCDataFragment
>(Sec
->curFragList()->Head
);
1091 raw_svector_ostream
OS(Frag
->getContents());
1092 for (const auto &CGPE
: OWriter
.getCGProfile()) {
1093 uint32_t FromIndex
= CGPE
.From
->getSymbol().getIndex();
1094 uint32_t ToIndex
= CGPE
.To
->getSymbol().getIndex();
1095 support::endian::write(OS
, FromIndex
, W
.Endian
);
1096 support::endian::write(OS
, ToIndex
, W
.Endian
);
1097 support::endian::write(OS
, CGPE
.Count
, W
.Endian
);
1101 assignFileOffsets(Asm
);
1103 // MS LINK expects to be able to use this timestamp to implement their
1104 // /INCREMENTAL feature.
1105 if (OWriter
.IncrementalLinkerCompatible
) {
1106 Header
.TimeDateStamp
= getTime();
1108 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
1109 Header
.TimeDateStamp
= 0;
1112 // Write it all to disk...
1113 WriteFileHeader(Header
);
1114 writeSectionHeaders();
1117 sections::iterator I
= Sections
.begin();
1118 sections::iterator IE
= Sections
.end();
1119 auto J
= Asm
.begin();
1120 auto JE
= Asm
.end();
1121 for (; I
!= IE
&& J
!= JE
; ++I
, ++J
) {
1122 while (J
!= JE
&& ((Mode
== NonDwoOnly
&& isDwoSection(*J
)) ||
1123 (Mode
== DwoOnly
&& !isDwoSection(*J
))))
1125 assert(J
!= JE
&& (**I
).MCSection
== &*J
&& "Wrong bound MCSection");
1129 // Write section contents.
1130 for (std::unique_ptr
<COFFSection
> &Sec
: Sections
)
1131 writeSection(Asm
, *Sec
);
1133 assert(W
.OS
.tell() == Header
.PointerToSymbolTable
&&
1134 "Header::PointerToSymbolTable is insane!");
1136 // Write a symbol table.
1137 for (auto &Symbol
: Symbols
)
1138 if (Symbol
->getIndex() != -1)
1139 WriteSymbol(*Symbol
);
1141 // Write a string table, which completes the entire COFF file.
1142 Strings
.write(W
.OS
);
1144 return W
.OS
.tell() - StartOffset
;
1147 //------------------------------------------------------------------------------
1148 // WinCOFFObjectWriter class implementation
1150 ////////////////////////////////////////////////////////////////////////////////
1151 // MCObjectWriter interface implementations
1153 void WinCOFFObjectWriter::reset() {
1154 IncrementalLinkerCompatible
= false;
1158 MCObjectWriter::reset();
1161 bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1162 const MCAssembler
&Asm
, const MCSymbol
&SymA
, const MCFragment
&FB
,
1163 bool InSet
, bool IsPCRel
) const {
1164 // Don't drop relocations between functions, even if they are in the same text
1165 // section. Multiple Visual C++ linker features depend on having the
1166 // relocations present. The /INCREMENTAL flag will cause these relocations to
1167 // point to thunks, and the /GUARD:CF flag assumes that it can use relocations
1168 // to approximate the set of all address taken functions. LLD's implementation
1169 // of /GUARD:CF also relies on the existance of these relocations.
1170 uint16_t Type
= cast
<MCSymbolCOFF
>(SymA
).getType();
1171 if ((Type
>> COFF::SCT_COMPLEX_TYPE_SHIFT
) == COFF::IMAGE_SYM_DTYPE_FUNCTION
)
1173 return &SymA
.getSection() == FB
.getParent();
1176 void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler
&Asm
) {
1177 ObjWriter
->executePostLayoutBinding(Asm
);
1179 DwoWriter
->executePostLayoutBinding(Asm
);
1182 void WinCOFFObjectWriter::recordRelocation(MCAssembler
&Asm
,
1183 const MCFragment
*Fragment
,
1184 const MCFixup
&Fixup
, MCValue Target
,
1185 uint64_t &FixedValue
) {
1186 assert(!isDwoSection(*Fragment
->getParent()) &&
1187 "No relocation in Dwo sections");
1188 ObjWriter
->recordRelocation(Asm
, Fragment
, Fixup
, Target
, FixedValue
);
1191 uint64_t WinCOFFObjectWriter::writeObject(MCAssembler
&Asm
) {
1192 uint64_t TotalSize
= ObjWriter
->writeObject(Asm
);
1194 TotalSize
+= DwoWriter
->writeObject(Asm
);
1198 MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_
)
1199 : Machine(Machine_
) {}
1201 // Pin the vtable to this file.
1202 void MCWinCOFFObjectTargetWriter::anchor() {}
1204 //------------------------------------------------------------------------------
1205 // WinCOFFObjectWriter factory function
1207 std::unique_ptr
<MCObjectWriter
> llvm::createWinCOFFObjectWriter(
1208 std::unique_ptr
<MCWinCOFFObjectTargetWriter
> MOTW
, raw_pwrite_stream
&OS
) {
1209 return std::make_unique
<WinCOFFObjectWriter
>(std::move(MOTW
), OS
);
1212 std::unique_ptr
<MCObjectWriter
> llvm::createWinCOFFDwoObjectWriter(
1213 std::unique_ptr
<MCWinCOFFObjectTargetWriter
> MOTW
, raw_pwrite_stream
&OS
,
1214 raw_pwrite_stream
&DwoOS
) {
1215 return std::make_unique
<WinCOFFObjectWriter
>(std::move(MOTW
), OS
, DwoOS
);