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/MCAsmLayout.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCFixup.h"
26 #include "llvm/MC/MCFragment.h"
27 #include "llvm/MC/MCObjectWriter.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCSectionCOFF.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCSymbolCOFF.h"
32 #include "llvm/MC/MCValue.h"
33 #include "llvm/MC/MCWinCOFFObjectWriter.h"
34 #include "llvm/MC/StringTableBuilder.h"
35 #include "llvm/Support/CRC.h"
36 #include "llvm/Support/Casting.h"
37 #include "llvm/Support/EndianStream.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/LEB128.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/raw_ostream.h"
52 using llvm::support::endian::write32le
;
54 #define DEBUG_TYPE "WinCOFFObjectWriter"
58 constexpr int OffsetLabelIntervalBits
= 20;
60 using name
= SmallString
<COFF::NameSize
>;
62 enum AuxiliaryType
{ ATWeakExternal
, ATFile
, ATSectionDefinition
};
65 AuxiliaryType AuxType
;
73 COFF::symbol Data
= {};
75 using AuxiliarySymbols
= SmallVector
<AuxSymbol
, 1>;
80 COFFSymbol
*Other
= nullptr;
81 COFFSection
*Section
= nullptr;
83 const MCSymbol
*MC
= nullptr;
85 COFFSymbol(StringRef Name
) : Name(Name
) {}
87 void set_name_offset(uint32_t Offset
);
89 int64_t getIndex() const { return Index
; }
90 void setIndex(int Value
) {
93 MC
->setIndex(static_cast<uint32_t>(Value
));
97 // This class contains staging data for a COFF relocation entry.
98 struct COFFRelocation
{
99 COFF::relocation Data
;
100 COFFSymbol
*Symb
= nullptr;
102 COFFRelocation() = default;
104 static size_t size() { return COFF::RelocationSize
; }
107 using relocations
= std::vector
<COFFRelocation
>;
111 COFF::section Header
= {};
115 MCSectionCOFF
const *MCSection
= nullptr;
116 COFFSymbol
*Symbol
= nullptr;
117 relocations Relocations
;
119 COFFSection(StringRef Name
) : Name(std::string(Name
)) {}
121 SmallVector
<COFFSymbol
*, 1> OffsetSymbols
;
124 class WinCOFFObjectWriter
;
126 class WinCOFFWriter
{
127 WinCOFFObjectWriter
&OWriter
;
128 support::endian::Writer W
;
130 using symbols
= std::vector
<std::unique_ptr
<COFFSymbol
>>;
131 using sections
= std::vector
<std::unique_ptr
<COFFSection
>>;
133 using symbol_map
= DenseMap
<MCSymbol
const *, COFFSymbol
*>;
134 using section_map
= DenseMap
<MCSection
const *, COFFSection
*>;
136 using symbol_list
= DenseSet
<COFFSymbol
*>;
138 // Root level file contents.
139 COFF::header Header
= {};
142 StringTableBuilder Strings
{StringTableBuilder::WinCOFF
};
144 // Maps used during object file creation.
145 section_map SectionMap
;
146 symbol_map SymbolMap
;
148 symbol_list WeakDefaults
;
151 bool UseOffsetLabels
= false;
154 MCSectionCOFF
*AddrsigSection
= nullptr;
155 MCSectionCOFF
*CGProfileSection
= nullptr;
163 WinCOFFWriter(WinCOFFObjectWriter
&OWriter
, raw_pwrite_stream
&OS
,
167 void executePostLayoutBinding(MCAssembler
&Asm
, const MCAsmLayout
&Layout
);
168 void recordRelocation(MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
169 const MCFragment
*Fragment
, const MCFixup
&Fixup
,
170 MCValue Target
, uint64_t &FixedValue
);
171 uint64_t writeObject(MCAssembler
&Asm
, const MCAsmLayout
&Layout
);
174 COFFSymbol
*createSymbol(StringRef Name
);
175 COFFSymbol
*GetOrCreateCOFFSymbol(const MCSymbol
*Symbol
);
176 COFFSection
*createSection(StringRef Name
);
178 void defineSection(MCSectionCOFF
const &Sec
, const MCAsmLayout
&Layout
);
180 COFFSymbol
*getLinkedSymbol(const MCSymbol
&Symbol
);
181 void DefineSymbol(const MCSymbol
&Symbol
, MCAssembler
&Assembler
,
182 const MCAsmLayout
&Layout
);
184 void SetSymbolName(COFFSymbol
&S
);
185 void SetSectionName(COFFSection
&S
);
187 bool IsPhysicalSection(COFFSection
*S
);
189 // Entity writing methods.
190 void WriteFileHeader(const COFF::header
&Header
);
191 void WriteSymbol(const COFFSymbol
&S
);
192 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols
&S
);
193 void writeSectionHeaders();
194 void WriteRelocation(const COFF::relocation
&R
);
195 uint32_t writeSectionContents(MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
196 const MCSection
&MCSec
);
197 void writeSection(MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
198 const COFFSection
&Sec
);
200 void createFileSymbols(MCAssembler
&Asm
);
201 void setWeakDefaultNames();
202 void assignSectionNumbers();
203 void assignFileOffsets(MCAssembler
&Asm
, const MCAsmLayout
&Layout
);
206 class WinCOFFObjectWriter
: public MCObjectWriter
{
207 friend class WinCOFFWriter
;
209 std::unique_ptr
<MCWinCOFFObjectTargetWriter
> TargetObjectWriter
;
210 std::unique_ptr
<WinCOFFWriter
> ObjWriter
, DwoWriter
;
213 WinCOFFObjectWriter(std::unique_ptr
<MCWinCOFFObjectTargetWriter
> MOTW
,
214 raw_pwrite_stream
&OS
)
215 : TargetObjectWriter(std::move(MOTW
)),
216 ObjWriter(std::make_unique
<WinCOFFWriter
>(*this, OS
,
217 WinCOFFWriter::AllSections
)) {
219 WinCOFFObjectWriter(std::unique_ptr
<MCWinCOFFObjectTargetWriter
> MOTW
,
220 raw_pwrite_stream
&OS
, raw_pwrite_stream
&DwoOS
)
221 : TargetObjectWriter(std::move(MOTW
)),
222 ObjWriter(std::make_unique
<WinCOFFWriter
>(*this, OS
,
223 WinCOFFWriter::NonDwoOnly
)),
224 DwoWriter(std::make_unique
<WinCOFFWriter
>(*this, DwoOS
,
225 WinCOFFWriter::DwoOnly
)) {}
227 // MCObjectWriter interface implementation.
228 void reset() override
;
229 void executePostLayoutBinding(MCAssembler
&Asm
,
230 const MCAsmLayout
&Layout
) override
;
231 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler
&Asm
,
232 const MCSymbol
&SymA
,
233 const MCFragment
&FB
, bool InSet
,
234 bool IsPCRel
) const override
;
235 void recordRelocation(MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
236 const MCFragment
*Fragment
, const MCFixup
&Fixup
,
237 MCValue Target
, uint64_t &FixedValue
) override
;
238 uint64_t writeObject(MCAssembler
&Asm
, const MCAsmLayout
&Layout
) override
;
241 } // end anonymous namespace
243 static bool isDwoSection(const MCSection
&Sec
) {
244 return Sec
.getName().ends_with(".dwo");
247 //------------------------------------------------------------------------------
248 // Symbol class implementation
250 // In the case that the name does not fit within 8 bytes, the offset
251 // into the string table is stored in the last 4 bytes instead, leaving
252 // the first 4 bytes as 0.
253 void COFFSymbol::set_name_offset(uint32_t Offset
) {
254 write32le(Data
.Name
+ 0, 0);
255 write32le(Data
.Name
+ 4, Offset
);
258 //------------------------------------------------------------------------------
259 // WinCOFFWriter class implementation
261 WinCOFFWriter::WinCOFFWriter(WinCOFFObjectWriter
&OWriter
,
262 raw_pwrite_stream
&OS
, DwoMode Mode
)
263 : OWriter(OWriter
), W(OS
, llvm::endianness::little
), Mode(Mode
) {
264 Header
.Machine
= OWriter
.TargetObjectWriter
->getMachine();
265 // Some relocations on ARM64 (the 21 bit ADRP relocations) have a slightly
266 // limited range for the immediate offset (+/- 1 MB); create extra offset
267 // label symbols with regular intervals to allow referencing a
268 // non-temporary symbol that is close enough.
269 UseOffsetLabels
= Header
.Machine
== COFF::IMAGE_FILE_MACHINE_ARM64
;
272 COFFSymbol
*WinCOFFWriter::createSymbol(StringRef Name
) {
273 Symbols
.push_back(std::make_unique
<COFFSymbol
>(Name
));
274 return Symbols
.back().get();
277 COFFSymbol
*WinCOFFWriter::GetOrCreateCOFFSymbol(const MCSymbol
*Symbol
) {
278 COFFSymbol
*&Ret
= SymbolMap
[Symbol
];
280 Ret
= createSymbol(Symbol
->getName());
284 COFFSection
*WinCOFFWriter::createSection(StringRef Name
) {
285 Sections
.emplace_back(std::make_unique
<COFFSection
>(Name
));
286 return Sections
.back().get();
289 static uint32_t getAlignment(const MCSectionCOFF
&Sec
) {
290 switch (Sec
.getAlign().value()) {
292 return COFF::IMAGE_SCN_ALIGN_1BYTES
;
294 return COFF::IMAGE_SCN_ALIGN_2BYTES
;
296 return COFF::IMAGE_SCN_ALIGN_4BYTES
;
298 return COFF::IMAGE_SCN_ALIGN_8BYTES
;
300 return COFF::IMAGE_SCN_ALIGN_16BYTES
;
302 return COFF::IMAGE_SCN_ALIGN_32BYTES
;
304 return COFF::IMAGE_SCN_ALIGN_64BYTES
;
306 return COFF::IMAGE_SCN_ALIGN_128BYTES
;
308 return COFF::IMAGE_SCN_ALIGN_256BYTES
;
310 return COFF::IMAGE_SCN_ALIGN_512BYTES
;
312 return COFF::IMAGE_SCN_ALIGN_1024BYTES
;
314 return COFF::IMAGE_SCN_ALIGN_2048BYTES
;
316 return COFF::IMAGE_SCN_ALIGN_4096BYTES
;
318 return COFF::IMAGE_SCN_ALIGN_8192BYTES
;
320 llvm_unreachable("unsupported section alignment");
323 /// This function takes a section data object from the assembler
324 /// and creates the associated COFF section staging object.
325 void WinCOFFWriter::defineSection(const MCSectionCOFF
&MCSec
,
326 const MCAsmLayout
&Layout
) {
327 COFFSection
*Section
= createSection(MCSec
.getName());
328 COFFSymbol
*Symbol
= createSymbol(MCSec
.getName());
329 Section
->Symbol
= Symbol
;
330 Symbol
->Section
= Section
;
331 Symbol
->Data
.StorageClass
= COFF::IMAGE_SYM_CLASS_STATIC
;
333 // Create a COMDAT symbol if needed.
334 if (MCSec
.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
) {
335 if (const MCSymbol
*S
= MCSec
.getCOMDATSymbol()) {
336 COFFSymbol
*COMDATSymbol
= GetOrCreateCOFFSymbol(S
);
337 if (COMDATSymbol
->Section
)
338 report_fatal_error("two sections have the same comdat");
339 COMDATSymbol
->Section
= Section
;
343 // In this case the auxiliary symbol is a Section Definition.
344 Symbol
->Aux
.resize(1);
346 Symbol
->Aux
[0].AuxType
= ATSectionDefinition
;
347 Symbol
->Aux
[0].Aux
.SectionDefinition
.Selection
= MCSec
.getSelection();
349 // Set section alignment.
350 Section
->Header
.Characteristics
= MCSec
.getCharacteristics();
351 Section
->Header
.Characteristics
|= getAlignment(MCSec
);
353 // Bind internal COFF section to MC section.
354 Section
->MCSection
= &MCSec
;
355 SectionMap
[&MCSec
] = Section
;
357 if (UseOffsetLabels
&& !MCSec
.getFragmentList().empty()) {
358 const uint32_t Interval
= 1 << OffsetLabelIntervalBits
;
360 for (uint32_t Off
= Interval
, E
= Layout
.getSectionAddressSize(&MCSec
);
361 Off
< E
; Off
+= Interval
) {
362 auto Name
= ("$L" + MCSec
.getName() + "_" + Twine(N
++)).str();
363 COFFSymbol
*Label
= createSymbol(Name
);
364 Label
->Section
= Section
;
365 Label
->Data
.StorageClass
= COFF::IMAGE_SYM_CLASS_LABEL
;
366 Label
->Data
.Value
= Off
;
367 Section
->OffsetSymbols
.push_back(Label
);
372 static uint64_t getSymbolValue(const MCSymbol
&Symbol
,
373 const MCAsmLayout
&Layout
) {
374 if (Symbol
.isCommon() && Symbol
.isExternal())
375 return Symbol
.getCommonSize();
378 if (!Layout
.getSymbolOffset(Symbol
, Res
))
384 COFFSymbol
*WinCOFFWriter::getLinkedSymbol(const MCSymbol
&Symbol
) {
385 if (!Symbol
.isVariable())
388 const MCSymbolRefExpr
*SymRef
=
389 dyn_cast
<MCSymbolRefExpr
>(Symbol
.getVariableValue());
393 const MCSymbol
&Aliasee
= SymRef
->getSymbol();
394 if (Aliasee
.isUndefined() || Aliasee
.isExternal())
395 return GetOrCreateCOFFSymbol(&Aliasee
);
400 /// This function takes a symbol data object from the assembler
401 /// and creates the associated COFF symbol staging object.
402 void WinCOFFWriter::DefineSymbol(const MCSymbol
&MCSym
, MCAssembler
&Assembler
,
403 const MCAsmLayout
&Layout
) {
404 COFFSymbol
*Sym
= GetOrCreateCOFFSymbol(&MCSym
);
405 const MCSymbol
*Base
= Layout
.getBaseSymbol(MCSym
);
406 COFFSection
*Sec
= nullptr;
407 if (Base
&& Base
->getFragment()) {
408 Sec
= SectionMap
[Base
->getFragment()->getParent()];
409 if (Sym
->Section
&& Sym
->Section
!= Sec
)
410 report_fatal_error("conflicting sections for symbol");
413 COFFSymbol
*Local
= nullptr;
414 if (cast
<MCSymbolCOFF
>(MCSym
).getWeakExternalCharacteristics()) {
415 Sym
->Data
.StorageClass
= COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL
;
416 Sym
->Section
= nullptr;
418 COFFSymbol
*WeakDefault
= getLinkedSymbol(MCSym
);
420 std::string WeakName
= (".weak." + MCSym
.getName() + ".default").str();
421 WeakDefault
= createSymbol(WeakName
);
423 WeakDefault
->Data
.SectionNumber
= COFF::IMAGE_SYM_ABSOLUTE
;
425 WeakDefault
->Section
= Sec
;
426 WeakDefaults
.insert(WeakDefault
);
430 Sym
->Other
= WeakDefault
;
432 // Setup the Weak External auxiliary symbol.
434 memset(&Sym
->Aux
[0], 0, sizeof(Sym
->Aux
[0]));
435 Sym
->Aux
[0].AuxType
= ATWeakExternal
;
436 Sym
->Aux
[0].Aux
.WeakExternal
.TagIndex
= 0; // Filled in later
437 Sym
->Aux
[0].Aux
.WeakExternal
.Characteristics
=
438 cast
<MCSymbolCOFF
>(MCSym
).getWeakExternalCharacteristics();
441 Sym
->Data
.SectionNumber
= COFF::IMAGE_SYM_ABSOLUTE
;
448 Local
->Data
.Value
= getSymbolValue(MCSym
, Layout
);
450 const MCSymbolCOFF
&SymbolCOFF
= cast
<MCSymbolCOFF
>(MCSym
);
451 Local
->Data
.Type
= SymbolCOFF
.getType();
452 Local
->Data
.StorageClass
= SymbolCOFF
.getClass();
454 // If no storage class was specified in the streamer, define it here.
455 if (Local
->Data
.StorageClass
== COFF::IMAGE_SYM_CLASS_NULL
) {
457 MCSym
.isExternal() || (!MCSym
.getFragment() && !MCSym
.isVariable());
459 Local
->Data
.StorageClass
= IsExternal
? COFF::IMAGE_SYM_CLASS_EXTERNAL
460 : COFF::IMAGE_SYM_CLASS_STATIC
;
467 void WinCOFFWriter::SetSectionName(COFFSection
&S
) {
468 if (S
.Name
.size() <= COFF::NameSize
) {
469 std::memcpy(S
.Header
.Name
, S
.Name
.c_str(), S
.Name
.size());
473 uint64_t StringTableEntry
= Strings
.getOffset(S
.Name
);
474 if (!COFF::encodeSectionName(S
.Header
.Name
, StringTableEntry
))
475 report_fatal_error("COFF string table is greater than 64 GB.");
478 void WinCOFFWriter::SetSymbolName(COFFSymbol
&S
) {
479 if (S
.Name
.size() > COFF::NameSize
)
480 S
.set_name_offset(Strings
.getOffset(S
.Name
));
482 std::memcpy(S
.Data
.Name
, S
.Name
.c_str(), S
.Name
.size());
485 bool WinCOFFWriter::IsPhysicalSection(COFFSection
*S
) {
486 return (S
->Header
.Characteristics
& COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
) ==
490 //------------------------------------------------------------------------------
491 // entity writing methods
493 void WinCOFFWriter::WriteFileHeader(const COFF::header
&Header
) {
495 W
.write
<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN
);
496 W
.write
<uint16_t>(0xFFFF);
497 W
.write
<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion
);
498 W
.write
<uint16_t>(Header
.Machine
);
499 W
.write
<uint32_t>(Header
.TimeDateStamp
);
500 W
.OS
.write(COFF::BigObjMagic
, sizeof(COFF::BigObjMagic
));
501 W
.write
<uint32_t>(0);
502 W
.write
<uint32_t>(0);
503 W
.write
<uint32_t>(0);
504 W
.write
<uint32_t>(0);
505 W
.write
<uint32_t>(Header
.NumberOfSections
);
506 W
.write
<uint32_t>(Header
.PointerToSymbolTable
);
507 W
.write
<uint32_t>(Header
.NumberOfSymbols
);
509 W
.write
<uint16_t>(Header
.Machine
);
510 W
.write
<uint16_t>(static_cast<int16_t>(Header
.NumberOfSections
));
511 W
.write
<uint32_t>(Header
.TimeDateStamp
);
512 W
.write
<uint32_t>(Header
.PointerToSymbolTable
);
513 W
.write
<uint32_t>(Header
.NumberOfSymbols
);
514 W
.write
<uint16_t>(Header
.SizeOfOptionalHeader
);
515 W
.write
<uint16_t>(Header
.Characteristics
);
519 void WinCOFFWriter::WriteSymbol(const COFFSymbol
&S
) {
520 W
.OS
.write(S
.Data
.Name
, COFF::NameSize
);
521 W
.write
<uint32_t>(S
.Data
.Value
);
523 W
.write
<uint32_t>(S
.Data
.SectionNumber
);
525 W
.write
<uint16_t>(static_cast<int16_t>(S
.Data
.SectionNumber
));
526 W
.write
<uint16_t>(S
.Data
.Type
);
527 W
.OS
<< char(S
.Data
.StorageClass
);
528 W
.OS
<< char(S
.Data
.NumberOfAuxSymbols
);
529 WriteAuxiliarySymbols(S
.Aux
);
532 void WinCOFFWriter::WriteAuxiliarySymbols(
533 const COFFSymbol::AuxiliarySymbols
&S
) {
534 for (const AuxSymbol
&i
: S
) {
537 W
.write
<uint32_t>(i
.Aux
.WeakExternal
.TagIndex
);
538 W
.write
<uint32_t>(i
.Aux
.WeakExternal
.Characteristics
);
539 W
.OS
.write_zeros(sizeof(i
.Aux
.WeakExternal
.unused
));
541 W
.OS
.write_zeros(COFF::Symbol32Size
- COFF::Symbol16Size
);
544 W
.OS
.write(reinterpret_cast<const char *>(&i
.Aux
),
545 UseBigObj
? COFF::Symbol32Size
: COFF::Symbol16Size
);
547 case ATSectionDefinition
:
548 W
.write
<uint32_t>(i
.Aux
.SectionDefinition
.Length
);
549 W
.write
<uint16_t>(i
.Aux
.SectionDefinition
.NumberOfRelocations
);
550 W
.write
<uint16_t>(i
.Aux
.SectionDefinition
.NumberOfLinenumbers
);
551 W
.write
<uint32_t>(i
.Aux
.SectionDefinition
.CheckSum
);
552 W
.write
<uint16_t>(static_cast<int16_t>(i
.Aux
.SectionDefinition
.Number
));
553 W
.OS
<< char(i
.Aux
.SectionDefinition
.Selection
);
554 W
.OS
.write_zeros(sizeof(i
.Aux
.SectionDefinition
.unused
));
556 static_cast<int16_t>(i
.Aux
.SectionDefinition
.Number
>> 16));
558 W
.OS
.write_zeros(COFF::Symbol32Size
- COFF::Symbol16Size
);
564 // Write the section header.
565 void WinCOFFWriter::writeSectionHeaders() {
566 // Section numbers must be monotonically increasing in the section
567 // header, but our Sections array is not sorted by section number,
568 // so make a copy of Sections and sort it.
569 std::vector
<COFFSection
*> Arr
;
570 for (auto &Section
: Sections
)
571 Arr
.push_back(Section
.get());
572 llvm::sort(Arr
, [](const COFFSection
*A
, const COFFSection
*B
) {
573 return A
->Number
< B
->Number
;
576 for (auto &Section
: Arr
) {
577 if (Section
->Number
== -1)
580 COFF::section
&S
= Section
->Header
;
581 if (Section
->Relocations
.size() >= 0xffff)
582 S
.Characteristics
|= COFF::IMAGE_SCN_LNK_NRELOC_OVFL
;
583 W
.OS
.write(S
.Name
, COFF::NameSize
);
584 W
.write
<uint32_t>(S
.VirtualSize
);
585 W
.write
<uint32_t>(S
.VirtualAddress
);
586 W
.write
<uint32_t>(S
.SizeOfRawData
);
587 W
.write
<uint32_t>(S
.PointerToRawData
);
588 W
.write
<uint32_t>(S
.PointerToRelocations
);
589 W
.write
<uint32_t>(S
.PointerToLineNumbers
);
590 W
.write
<uint16_t>(S
.NumberOfRelocations
);
591 W
.write
<uint16_t>(S
.NumberOfLineNumbers
);
592 W
.write
<uint32_t>(S
.Characteristics
);
596 void WinCOFFWriter::WriteRelocation(const COFF::relocation
&R
) {
597 W
.write
<uint32_t>(R
.VirtualAddress
);
598 W
.write
<uint32_t>(R
.SymbolTableIndex
);
599 W
.write
<uint16_t>(R
.Type
);
602 // Write MCSec's contents. What this function does is essentially
603 // "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
604 // because it needs to compute a CRC.
605 uint32_t WinCOFFWriter::writeSectionContents(MCAssembler
&Asm
,
606 const MCAsmLayout
&Layout
,
607 const MCSection
&MCSec
) {
608 // Save the contents of the section to a temporary buffer, we need this
609 // to CRC the data before we dump it into the object file.
610 SmallVector
<char, 128> Buf
;
611 raw_svector_ostream
VecOS(Buf
);
612 Asm
.writeSectionData(VecOS
, &MCSec
, Layout
);
614 // Write the section contents to the object file.
617 // Calculate our CRC with an initial value of '0', this is not how
618 // JamCRC is specified but it aligns with the expected output.
619 JamCRC
JC(/*Init=*/0);
620 JC
.update(ArrayRef(reinterpret_cast<uint8_t *>(Buf
.data()), Buf
.size()));
624 void WinCOFFWriter::writeSection(MCAssembler
&Asm
, const MCAsmLayout
&Layout
,
625 const COFFSection
&Sec
) {
626 if (Sec
.Number
== -1)
629 // Write the section contents.
630 if (Sec
.Header
.PointerToRawData
!= 0) {
631 assert(W
.OS
.tell() == Sec
.Header
.PointerToRawData
&&
632 "Section::PointerToRawData is insane!");
634 uint32_t CRC
= writeSectionContents(Asm
, Layout
, *Sec
.MCSection
);
636 // Update the section definition auxiliary symbol to record the CRC.
637 COFFSymbol::AuxiliarySymbols
&AuxSyms
= Sec
.Symbol
->Aux
;
638 assert(AuxSyms
.size() == 1 && AuxSyms
[0].AuxType
== ATSectionDefinition
);
639 AuxSymbol
&SecDef
= AuxSyms
[0];
640 SecDef
.Aux
.SectionDefinition
.CheckSum
= CRC
;
643 // Write relocations for this section.
644 if (Sec
.Relocations
.empty()) {
645 assert(Sec
.Header
.PointerToRelocations
== 0 &&
646 "Section::PointerToRelocations is insane!");
650 assert(W
.OS
.tell() == Sec
.Header
.PointerToRelocations
&&
651 "Section::PointerToRelocations is insane!");
653 if (Sec
.Relocations
.size() >= 0xffff) {
654 // In case of overflow, write actual relocation count as first
655 // relocation. Including the synthetic reloc itself (+ 1).
657 R
.VirtualAddress
= Sec
.Relocations
.size() + 1;
658 R
.SymbolTableIndex
= 0;
663 for (const auto &Relocation
: Sec
.Relocations
)
664 WriteRelocation(Relocation
.Data
);
667 // Create .file symbols.
668 void WinCOFFWriter::createFileSymbols(MCAssembler
&Asm
) {
669 for (const std::pair
<std::string
, size_t> &It
: Asm
.getFileNames()) {
670 // round up to calculate the number of auxiliary symbols required
671 const std::string
&Name
= It
.first
;
672 unsigned SymbolSize
= UseBigObj
? COFF::Symbol32Size
: COFF::Symbol16Size
;
673 unsigned Count
= (Name
.size() + SymbolSize
- 1) / SymbolSize
;
675 COFFSymbol
*File
= createSymbol(".file");
676 File
->Data
.SectionNumber
= COFF::IMAGE_SYM_DEBUG
;
677 File
->Data
.StorageClass
= COFF::IMAGE_SYM_CLASS_FILE
;
678 File
->Aux
.resize(Count
);
681 unsigned Length
= Name
.size();
682 for (auto &Aux
: File
->Aux
) {
683 Aux
.AuxType
= ATFile
;
685 if (Length
> SymbolSize
) {
686 memcpy(&Aux
.Aux
, Name
.c_str() + Offset
, SymbolSize
);
687 Length
= Length
- SymbolSize
;
689 memcpy(&Aux
.Aux
, Name
.c_str() + Offset
, Length
);
690 memset((char *)&Aux
.Aux
+ Length
, 0, SymbolSize
- Length
);
694 Offset
+= SymbolSize
;
699 void WinCOFFWriter::setWeakDefaultNames() {
700 if (WeakDefaults
.empty())
703 // If multiple object files use a weak symbol (either with a regular
704 // defined default, or an absolute zero symbol as default), the defaults
705 // cause duplicate definitions unless their names are made unique. Look
706 // for a defined extern symbol, that isn't comdat - that should be unique
707 // unless there are other duplicate definitions. And if none is found,
708 // allow picking a comdat symbol, as that's still better than nothing.
710 COFFSymbol
*Unique
= nullptr;
711 for (bool AllowComdat
: {false, true}) {
712 for (auto &Sym
: Symbols
) {
713 // Don't include the names of the defaults themselves
714 if (WeakDefaults
.count(Sym
.get()))
716 // Only consider external symbols
717 if (Sym
->Data
.StorageClass
!= COFF::IMAGE_SYM_CLASS_EXTERNAL
)
719 // Only consider symbols defined in a section or that are absolute
720 if (!Sym
->Section
&& Sym
->Data
.SectionNumber
!= COFF::IMAGE_SYM_ABSOLUTE
)
722 if (!AllowComdat
&& Sym
->Section
&&
723 Sym
->Section
->Header
.Characteristics
& COFF::IMAGE_SCN_LNK_COMDAT
)
731 // If we didn't find any unique symbol to use for the names, just skip this.
734 for (auto *Sym
: WeakDefaults
) {
735 Sym
->Name
.append(".");
736 Sym
->Name
.append(Unique
->Name
);
740 static bool isAssociative(const COFFSection
&Section
) {
741 return Section
.Symbol
->Aux
[0].Aux
.SectionDefinition
.Selection
==
742 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
;
745 void WinCOFFWriter::assignSectionNumbers() {
747 auto Assign
= [&](COFFSection
&Section
) {
749 Section
.Symbol
->Data
.SectionNumber
= I
;
750 Section
.Symbol
->Aux
[0].Aux
.SectionDefinition
.Number
= I
;
754 // Although it is not explicitly requested by the Microsoft COFF spec,
755 // we should avoid emitting forward associative section references,
756 // because MSVC link.exe as of 2017 cannot handle that.
757 for (const std::unique_ptr
<COFFSection
> &Section
: Sections
)
758 if (!isAssociative(*Section
))
760 for (const std::unique_ptr
<COFFSection
> &Section
: Sections
)
761 if (isAssociative(*Section
))
765 // Assign file offsets to COFF object file structures.
766 void WinCOFFWriter::assignFileOffsets(MCAssembler
&Asm
,
767 const MCAsmLayout
&Layout
) {
768 unsigned Offset
= W
.OS
.tell();
770 Offset
+= UseBigObj
? COFF::Header32Size
: COFF::Header16Size
;
771 Offset
+= COFF::SectionSize
* Header
.NumberOfSections
;
773 for (const auto &Section
: Asm
) {
774 COFFSection
*Sec
= SectionMap
[&Section
];
776 if (!Sec
|| Sec
->Number
== -1)
779 Sec
->Header
.SizeOfRawData
= Layout
.getSectionAddressSize(&Section
);
781 if (IsPhysicalSection(Sec
)) {
782 Sec
->Header
.PointerToRawData
= Offset
;
783 Offset
+= Sec
->Header
.SizeOfRawData
;
786 if (!Sec
->Relocations
.empty()) {
787 bool RelocationsOverflow
= Sec
->Relocations
.size() >= 0xffff;
789 if (RelocationsOverflow
) {
790 // Signal overflow by setting NumberOfRelocations to max value. Actual
791 // size is found in reloc #0. Microsoft tools understand this.
792 Sec
->Header
.NumberOfRelocations
= 0xffff;
794 Sec
->Header
.NumberOfRelocations
= Sec
->Relocations
.size();
796 Sec
->Header
.PointerToRelocations
= Offset
;
798 if (RelocationsOverflow
) {
799 // Reloc #0 will contain actual count, so make room for it.
800 Offset
+= COFF::RelocationSize
;
803 Offset
+= COFF::RelocationSize
* Sec
->Relocations
.size();
805 for (auto &Relocation
: Sec
->Relocations
) {
806 assert(Relocation
.Symb
->getIndex() != -1);
807 Relocation
.Data
.SymbolTableIndex
= Relocation
.Symb
->getIndex();
811 assert(Sec
->Symbol
->Aux
.size() == 1 &&
812 "Section's symbol must have one aux!");
813 AuxSymbol
&Aux
= Sec
->Symbol
->Aux
[0];
814 assert(Aux
.AuxType
== ATSectionDefinition
&&
815 "Section's symbol's aux symbol must be a Section Definition!");
816 Aux
.Aux
.SectionDefinition
.Length
= Sec
->Header
.SizeOfRawData
;
817 Aux
.Aux
.SectionDefinition
.NumberOfRelocations
=
818 Sec
->Header
.NumberOfRelocations
;
819 Aux
.Aux
.SectionDefinition
.NumberOfLinenumbers
=
820 Sec
->Header
.NumberOfLineNumbers
;
823 Header
.PointerToSymbolTable
= Offset
;
826 void WinCOFFWriter::reset() {
827 memset(&Header
, 0, sizeof(Header
));
828 Header
.Machine
= OWriter
.TargetObjectWriter
->getMachine();
834 WeakDefaults
.clear();
837 void WinCOFFWriter::executePostLayoutBinding(MCAssembler
&Asm
,
838 const MCAsmLayout
&Layout
) {
839 // "Define" each section & symbol. This creates section & symbol
840 // entries in the staging area.
841 for (const auto &Section
: Asm
) {
842 if ((Mode
== NonDwoOnly
&& isDwoSection(Section
)) ||
843 (Mode
== DwoOnly
&& !isDwoSection(Section
)))
845 defineSection(static_cast<const MCSectionCOFF
&>(Section
), Layout
);
849 for (const MCSymbol
&Symbol
: Asm
.symbols())
850 // Define non-temporary or temporary static (private-linkage) symbols
851 if (!Symbol
.isTemporary() ||
852 cast
<MCSymbolCOFF
>(Symbol
).getClass() == COFF::IMAGE_SYM_CLASS_STATIC
)
853 DefineSymbol(Symbol
, Asm
, Layout
);
856 void WinCOFFWriter::recordRelocation(MCAssembler
&Asm
,
857 const MCAsmLayout
&Layout
,
858 const MCFragment
*Fragment
,
859 const MCFixup
&Fixup
, MCValue Target
,
860 uint64_t &FixedValue
) {
861 assert(Target
.getSymA() && "Relocation must reference a symbol!");
863 const MCSymbol
&A
= Target
.getSymA()->getSymbol();
864 if (!A
.isRegistered()) {
865 Asm
.getContext().reportError(Fixup
.getLoc(), Twine("symbol '") +
867 "' can not be undefined");
870 if (A
.isTemporary() && A
.isUndefined()) {
871 Asm
.getContext().reportError(Fixup
.getLoc(), Twine("assembler label '") +
873 "' can not be undefined");
877 MCSection
*MCSec
= Fragment
->getParent();
879 // Mark this symbol as requiring an entry in the symbol table.
880 assert(SectionMap
.contains(MCSec
) &&
881 "Section must already have been defined in executePostLayoutBinding!");
883 COFFSection
*Sec
= SectionMap
[MCSec
];
884 const MCSymbolRefExpr
*SymB
= Target
.getSymB();
887 const MCSymbol
*B
= &SymB
->getSymbol();
888 if (!B
->getFragment()) {
889 Asm
.getContext().reportError(
891 Twine("symbol '") + B
->getName() +
892 "' can not be undefined in a subtraction expression");
896 // Offset of the symbol in the section
897 int64_t OffsetOfB
= Layout
.getSymbolOffset(*B
);
899 // Offset of the relocation in the section
900 int64_t OffsetOfRelocation
=
901 Layout
.getFragmentOffset(Fragment
) + Fixup
.getOffset();
903 FixedValue
= (OffsetOfRelocation
- OffsetOfB
) + Target
.getConstant();
905 FixedValue
= Target
.getConstant();
908 COFFRelocation Reloc
;
910 Reloc
.Data
.SymbolTableIndex
= 0;
911 Reloc
.Data
.VirtualAddress
= Layout
.getFragmentOffset(Fragment
);
913 // Turn relocations for temporary symbols into section relocations.
914 if (A
.isTemporary() && !SymbolMap
[&A
]) {
915 MCSection
*TargetSection
= &A
.getSection();
917 SectionMap
.contains(TargetSection
) &&
918 "Section must already have been defined in executePostLayoutBinding!");
919 COFFSection
*Section
= SectionMap
[TargetSection
];
920 Reloc
.Symb
= Section
->Symbol
;
921 FixedValue
+= Layout
.getSymbolOffset(A
);
922 // Technically, we should do the final adjustments of FixedValue (below)
923 // before picking an offset symbol, otherwise we might choose one which
924 // is slightly too far away. The relocations where it really matters
925 // (arm64 adrp relocations) don't get any offset though.
926 if (UseOffsetLabels
&& !Section
->OffsetSymbols
.empty()) {
927 uint64_t LabelIndex
= FixedValue
>> OffsetLabelIntervalBits
;
928 if (LabelIndex
> 0) {
929 if (LabelIndex
<= Section
->OffsetSymbols
.size())
930 Reloc
.Symb
= Section
->OffsetSymbols
[LabelIndex
- 1];
932 Reloc
.Symb
= Section
->OffsetSymbols
.back();
933 FixedValue
-= Reloc
.Symb
->Data
.Value
;
938 SymbolMap
.contains(&A
) &&
939 "Symbol must already have been defined in executePostLayoutBinding!");
940 Reloc
.Symb
= SymbolMap
[&A
];
943 ++Reloc
.Symb
->Relocations
;
945 Reloc
.Data
.VirtualAddress
+= Fixup
.getOffset();
946 Reloc
.Data
.Type
= OWriter
.TargetObjectWriter
->getRelocType(
947 Asm
.getContext(), Target
, Fixup
, SymB
, Asm
.getBackend());
949 // The *_REL32 relocations are relative to the end of the relocation,
951 if ((Header
.Machine
== COFF::IMAGE_FILE_MACHINE_AMD64
&&
952 Reloc
.Data
.Type
== COFF::IMAGE_REL_AMD64_REL32
) ||
953 (Header
.Machine
== COFF::IMAGE_FILE_MACHINE_I386
&&
954 Reloc
.Data
.Type
== COFF::IMAGE_REL_I386_REL32
) ||
955 (Header
.Machine
== COFF::IMAGE_FILE_MACHINE_ARMNT
&&
956 Reloc
.Data
.Type
== COFF::IMAGE_REL_ARM_REL32
) ||
957 (Header
.Machine
== COFF::IMAGE_FILE_MACHINE_ARM64
&&
958 Reloc
.Data
.Type
== COFF::IMAGE_REL_ARM64_REL32
))
961 if (Header
.Machine
== COFF::IMAGE_FILE_MACHINE_ARMNT
) {
962 switch (Reloc
.Data
.Type
) {
963 case COFF::IMAGE_REL_ARM_ABSOLUTE
:
964 case COFF::IMAGE_REL_ARM_ADDR32
:
965 case COFF::IMAGE_REL_ARM_ADDR32NB
:
966 case COFF::IMAGE_REL_ARM_TOKEN
:
967 case COFF::IMAGE_REL_ARM_SECTION
:
968 case COFF::IMAGE_REL_ARM_SECREL
:
970 case COFF::IMAGE_REL_ARM_BRANCH11
:
971 case COFF::IMAGE_REL_ARM_BLX11
:
972 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
973 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
975 case COFF::IMAGE_REL_ARM_BRANCH24
:
976 case COFF::IMAGE_REL_ARM_BLX24
:
977 case COFF::IMAGE_REL_ARM_MOV32A
:
978 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
979 // only used for ARM mode code, which is documented as being unsupported
980 // by Windows on ARM. Empirical proof indicates that masm is able to
981 // generate the relocations however the rest of the MSVC toolchain is
982 // unable to handle it.
983 llvm_unreachable("unsupported relocation");
985 case COFF::IMAGE_REL_ARM_MOV32T
:
987 case COFF::IMAGE_REL_ARM_BRANCH20T
:
988 case COFF::IMAGE_REL_ARM_BRANCH24T
:
989 case COFF::IMAGE_REL_ARM_BLX23T
:
990 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
991 // perform a 4 byte adjustment to the relocation. Relative branches are
992 // offset by 4 on ARM, however, because there is no RELA relocations, all
993 // branches are offset by 4.
994 FixedValue
= FixedValue
+ 4;
999 // The fixed value never makes sense for section indices, ignore it.
1000 if (Fixup
.getKind() == FK_SecRel_2
)
1003 if (OWriter
.TargetObjectWriter
->recordRelocation(Fixup
))
1004 Sec
->Relocations
.push_back(Reloc
);
1007 static std::time_t getTime() {
1008 std::time_t Now
= time(nullptr);
1009 if (Now
< 0 || !isUInt
<32>(Now
))
1014 uint64_t WinCOFFWriter::writeObject(MCAssembler
&Asm
,
1015 const MCAsmLayout
&Layout
) {
1016 uint64_t StartOffset
= W
.OS
.tell();
1018 if (Sections
.size() > INT32_MAX
)
1020 "PE COFF object files can't have more than 2147483647 sections");
1022 UseBigObj
= Sections
.size() > COFF::MaxNumberOfSections16
;
1023 Header
.NumberOfSections
= Sections
.size();
1024 Header
.NumberOfSymbols
= 0;
1026 setWeakDefaultNames();
1027 assignSectionNumbers();
1028 if (Mode
!= DwoOnly
)
1029 createFileSymbols(Asm
);
1031 for (auto &Symbol
: Symbols
) {
1032 // Update section number & offset for symbols that have them.
1033 if (Symbol
->Section
)
1034 Symbol
->Data
.SectionNumber
= Symbol
->Section
->Number
;
1035 Symbol
->setIndex(Header
.NumberOfSymbols
++);
1036 // Update auxiliary symbol info.
1037 Symbol
->Data
.NumberOfAuxSymbols
= Symbol
->Aux
.size();
1038 Header
.NumberOfSymbols
+= Symbol
->Data
.NumberOfAuxSymbols
;
1041 // Build string table.
1042 for (const auto &S
: Sections
)
1043 if (S
->Name
.size() > COFF::NameSize
)
1044 Strings
.add(S
->Name
);
1045 for (const auto &S
: Symbols
)
1046 if (S
->Name
.size() > COFF::NameSize
)
1047 Strings
.add(S
->Name
);
1051 for (const auto &S
: Sections
)
1053 for (auto &S
: Symbols
)
1056 // Fixup weak external references.
1057 for (auto &Symbol
: Symbols
) {
1058 if (Symbol
->Other
) {
1059 assert(Symbol
->getIndex() != -1);
1060 assert(Symbol
->Aux
.size() == 1 && "Symbol must contain one aux symbol!");
1061 assert(Symbol
->Aux
[0].AuxType
== ATWeakExternal
&&
1062 "Symbol's aux symbol must be a Weak External!");
1063 Symbol
->Aux
[0].Aux
.WeakExternal
.TagIndex
= Symbol
->Other
->getIndex();
1067 // Fixup associative COMDAT sections.
1068 for (auto &Section
: Sections
) {
1069 if (Section
->Symbol
->Aux
[0].Aux
.SectionDefinition
.Selection
!=
1070 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
)
1073 const MCSectionCOFF
&MCSec
= *Section
->MCSection
;
1074 const MCSymbol
*AssocMCSym
= MCSec
.getCOMDATSymbol();
1077 // It's an error to try to associate with an undefined symbol or a symbol
1078 // without a section.
1079 if (!AssocMCSym
->isInSection()) {
1080 Asm
.getContext().reportError(
1081 SMLoc(), Twine("cannot make section ") + MCSec
.getName() +
1082 Twine(" associative with sectionless symbol ") +
1083 AssocMCSym
->getName());
1087 const auto *AssocMCSec
= cast
<MCSectionCOFF
>(&AssocMCSym
->getSection());
1088 assert(SectionMap
.count(AssocMCSec
));
1089 COFFSection
*AssocSec
= SectionMap
[AssocMCSec
];
1091 // Skip this section if the associated section is unused.
1092 if (AssocSec
->Number
== -1)
1095 Section
->Symbol
->Aux
[0].Aux
.SectionDefinition
.Number
= AssocSec
->Number
;
1098 // Create the contents of the .llvm_addrsig section.
1099 if (Mode
!= DwoOnly
&& OWriter
.EmitAddrsigSection
) {
1100 auto Frag
= new MCDataFragment(AddrsigSection
);
1101 Frag
->setLayoutOrder(0);
1102 raw_svector_ostream
OS(Frag
->getContents());
1103 for (const MCSymbol
*S
: OWriter
.AddrsigSyms
) {
1104 if (!S
->isRegistered())
1106 if (!S
->isTemporary()) {
1107 encodeULEB128(S
->getIndex(), OS
);
1111 MCSection
*TargetSection
= &S
->getSection();
1112 assert(SectionMap
.contains(TargetSection
) &&
1113 "Section must already have been defined in "
1114 "executePostLayoutBinding!");
1115 encodeULEB128(SectionMap
[TargetSection
]->Symbol
->getIndex(), OS
);
1119 // Create the contents of the .llvm.call-graph-profile section.
1120 if (Mode
!= DwoOnly
&& CGProfileSection
) {
1121 auto *Frag
= new MCDataFragment(CGProfileSection
);
1122 Frag
->setLayoutOrder(0);
1123 raw_svector_ostream
OS(Frag
->getContents());
1124 for (const MCAssembler::CGProfileEntry
&CGPE
: Asm
.CGProfile
) {
1125 uint32_t FromIndex
= CGPE
.From
->getSymbol().getIndex();
1126 uint32_t ToIndex
= CGPE
.To
->getSymbol().getIndex();
1127 support::endian::write(OS
, FromIndex
, W
.Endian
);
1128 support::endian::write(OS
, ToIndex
, W
.Endian
);
1129 support::endian::write(OS
, CGPE
.Count
, W
.Endian
);
1133 assignFileOffsets(Asm
, Layout
);
1135 // MS LINK expects to be able to use this timestamp to implement their
1136 // /INCREMENTAL feature.
1137 if (Asm
.isIncrementalLinkerCompatible()) {
1138 Header
.TimeDateStamp
= getTime();
1140 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
1141 Header
.TimeDateStamp
= 0;
1144 // Write it all to disk...
1145 WriteFileHeader(Header
);
1146 writeSectionHeaders();
1149 sections::iterator I
= Sections
.begin();
1150 sections::iterator IE
= Sections
.end();
1151 MCAssembler::iterator J
= Asm
.begin();
1152 MCAssembler::iterator JE
= Asm
.end();
1153 for (; I
!= IE
&& J
!= JE
; ++I
, ++J
) {
1154 while (J
!= JE
&& ((Mode
== NonDwoOnly
&& isDwoSection(*J
)) ||
1155 (Mode
== DwoOnly
&& !isDwoSection(*J
))))
1157 assert(J
!= JE
&& (**I
).MCSection
== &*J
&& "Wrong bound MCSection");
1161 // Write section contents.
1162 for (std::unique_ptr
<COFFSection
> &Sec
: Sections
)
1163 writeSection(Asm
, Layout
, *Sec
);
1165 assert(W
.OS
.tell() == Header
.PointerToSymbolTable
&&
1166 "Header::PointerToSymbolTable is insane!");
1168 // Write a symbol table.
1169 for (auto &Symbol
: Symbols
)
1170 if (Symbol
->getIndex() != -1)
1171 WriteSymbol(*Symbol
);
1173 // Write a string table, which completes the entire COFF file.
1174 Strings
.write(W
.OS
);
1176 return W
.OS
.tell() - StartOffset
;
1179 //------------------------------------------------------------------------------
1180 // WinCOFFObjectWriter class implementation
1182 ////////////////////////////////////////////////////////////////////////////////
1183 // MCObjectWriter interface implementations
1185 void WinCOFFObjectWriter::reset() {
1189 MCObjectWriter::reset();
1192 bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1193 const MCAssembler
&Asm
, const MCSymbol
&SymA
, const MCFragment
&FB
,
1194 bool InSet
, bool IsPCRel
) const {
1195 // Don't drop relocations between functions, even if they are in the same text
1196 // section. Multiple Visual C++ linker features depend on having the
1197 // relocations present. The /INCREMENTAL flag will cause these relocations to
1198 // point to thunks, and the /GUARD:CF flag assumes that it can use relocations
1199 // to approximate the set of all address taken functions. LLD's implementation
1200 // of /GUARD:CF also relies on the existance of these relocations.
1201 uint16_t Type
= cast
<MCSymbolCOFF
>(SymA
).getType();
1202 if ((Type
>> COFF::SCT_COMPLEX_TYPE_SHIFT
) == COFF::IMAGE_SYM_DTYPE_FUNCTION
)
1204 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm
, SymA
, FB
,
1208 void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler
&Asm
,
1209 const MCAsmLayout
&Layout
) {
1210 if (EmitAddrsigSection
) {
1211 ObjWriter
->AddrsigSection
= Asm
.getContext().getCOFFSection(
1212 ".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE
,
1213 SectionKind::getMetadata());
1214 Asm
.registerSection(*ObjWriter
->AddrsigSection
);
1217 if (!Asm
.CGProfile
.empty()) {
1218 ObjWriter
->CGProfileSection
= Asm
.getContext().getCOFFSection(
1219 ".llvm.call-graph-profile", COFF::IMAGE_SCN_LNK_REMOVE
,
1220 SectionKind::getMetadata());
1221 Asm
.registerSection(*ObjWriter
->CGProfileSection
);
1224 ObjWriter
->executePostLayoutBinding(Asm
, Layout
);
1226 DwoWriter
->executePostLayoutBinding(Asm
, Layout
);
1229 void WinCOFFObjectWriter::recordRelocation(MCAssembler
&Asm
,
1230 const MCAsmLayout
&Layout
,
1231 const MCFragment
*Fragment
,
1232 const MCFixup
&Fixup
, MCValue Target
,
1233 uint64_t &FixedValue
) {
1234 assert(!isDwoSection(*Fragment
->getParent()) &&
1235 "No relocation in Dwo sections");
1236 ObjWriter
->recordRelocation(Asm
, Layout
, Fragment
, Fixup
, Target
, FixedValue
);
1239 uint64_t WinCOFFObjectWriter::writeObject(MCAssembler
&Asm
,
1240 const MCAsmLayout
&Layout
) {
1241 uint64_t TotalSize
= ObjWriter
->writeObject(Asm
, Layout
);
1243 TotalSize
+= DwoWriter
->writeObject(Asm
, Layout
);
1247 MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_
)
1248 : Machine(Machine_
) {}
1250 // Pin the vtable to this file.
1251 void MCWinCOFFObjectTargetWriter::anchor() {}
1253 //------------------------------------------------------------------------------
1254 // WinCOFFObjectWriter factory function
1256 std::unique_ptr
<MCObjectWriter
> llvm::createWinCOFFObjectWriter(
1257 std::unique_ptr
<MCWinCOFFObjectTargetWriter
> MOTW
, raw_pwrite_stream
&OS
) {
1258 return std::make_unique
<WinCOFFObjectWriter
>(std::move(MOTW
), OS
);
1261 std::unique_ptr
<MCObjectWriter
> llvm::createWinCOFFDwoObjectWriter(
1262 std::unique_ptr
<MCWinCOFFObjectTargetWriter
> MOTW
, raw_pwrite_stream
&OS
,
1263 raw_pwrite_stream
&DwoOS
) {
1264 return std::make_unique
<WinCOFFObjectWriter
>(std::move(MOTW
), OS
, DwoOS
);