1 //===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains an implementation of a Win32 COFF object file writer.
10 //
11 //===----------------------------------------------------------------------===//
12 
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"
42 #include <algorithm>
43 #include <cassert>
44 #include <cstddef>
45 #include <cstdint>
46 #include <cstring>
47 #include <ctime>
48 #include <memory>
49 #include <string>
50 #include <vector>
51 
52 using namespace llvm;
53 using llvm::support::endian::write32le;
54 
55 #define DEBUG_TYPE "WinCOFFObjectWriter"
56 
57 namespace {
58 
59 constexpr int OffsetLabelIntervalBits = 20;
60 
61 using name = SmallString<COFF::NameSize>;
62 
63 enum AuxiliaryType {
64   ATWeakExternal,
65   ATFile,
66   ATSectionDefinition
67 };
68 
69 struct AuxSymbol {
70   AuxiliaryType AuxType;
71   COFF::Auxiliary Aux;
72 };
73 
74 class COFFSection;
75 
76 class COFFSymbol {
77 public:
78   COFF::symbol Data = {};
79 
80   using AuxiliarySymbols = SmallVector<AuxSymbol, 1>;
81 
82   name Name;
83   int Index;
84   AuxiliarySymbols Aux;
85   COFFSymbol *Other = nullptr;
86   COFFSection *Section = nullptr;
87   int Relocations = 0;
88   const MCSymbol *MC = nullptr;
89 
90   COFFSymbol(StringRef Name) : Name(Name) {}
91 
92   void set_name_offset(uint32_t Offset);
93 
94   int64_t getIndex() const { return Index; }
95   void setIndex(int Value) {
96     Index = Value;
97     if (MC)
98       MC->setIndex(static_cast<uint32_t>(Value));
99   }
100 };
101 
102 // This class contains staging data for a COFF relocation entry.
103 struct COFFRelocation {
104   COFF::relocation Data;
105   COFFSymbol *Symb = nullptr;
106 
107   COFFRelocation() = default;
108 
109   static size_t size() { return COFF::RelocationSize; }
110 };
111 
112 using relocations = std::vector<COFFRelocation>;
113 
114 class COFFSection {
115 public:
116   COFF::section Header = {};
117 
118   std::string Name;
119   int Number;
120   MCSectionCOFF const *MCSection = nullptr;
121   COFFSymbol *Symbol = nullptr;
122   relocations Relocations;
123 
124   COFFSection(StringRef Name) : Name(std::string(Name)) {}
125 
126   SmallVector<COFFSymbol *, 1> OffsetSymbols;
127 };
128 
129 class WinCOFFObjectWriter : public MCObjectWriter {
130 public:
131   support::endian::Writer W;
132 
133   using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
134   using sections = std::vector<std::unique_ptr<COFFSection>>;
135 
136   using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>;
137   using section_map = DenseMap<MCSection const *, COFFSection *>;
138 
139   using symbol_list = DenseSet<COFFSymbol *>;
140 
141   std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
142 
143   // Root level file contents.
144   COFF::header Header = {};
145   sections Sections;
146   symbols Symbols;
147   StringTableBuilder Strings{StringTableBuilder::WinCOFF};
148 
149   // Maps used during object file creation.
150   section_map SectionMap;
151   symbol_map SymbolMap;
152 
153   symbol_list WeakDefaults;
154 
155   bool UseBigObj;
156   bool UseOffsetLabels = false;
157 
158   bool EmitAddrsigSection = false;
159   MCSectionCOFF *AddrsigSection;
160   std::vector<const MCSymbol *> AddrsigSyms;
161 
162   MCSectionCOFF *CGProfileSection = nullptr;
163 
164   WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
165                       raw_pwrite_stream &OS);
166 
167   void reset() override {
168     memset(&Header, 0, sizeof(Header));
169     Header.Machine = TargetObjectWriter->getMachine();
170     Sections.clear();
171     Symbols.clear();
172     Strings.clear();
173     SectionMap.clear();
174     SymbolMap.clear();
175     MCObjectWriter::reset();
176   }
177 
178   COFFSymbol *createSymbol(StringRef Name);
179   COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
180   COFFSection *createSection(StringRef Name);
181 
182   void defineSection(MCSectionCOFF const &Sec, const MCAsmLayout &Layout);
183 
184   COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
185   void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
186                     const MCAsmLayout &Layout);
187 
188   void SetSymbolName(COFFSymbol &S);
189   void SetSectionName(COFFSection &S);
190 
191   bool IsPhysicalSection(COFFSection *S);
192 
193   // Entity writing methods.
194 
195   void WriteFileHeader(const COFF::header &Header);
196   void WriteSymbol(const COFFSymbol &S);
197   void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
198   void writeSectionHeaders();
199   void WriteRelocation(const COFF::relocation &R);
200   uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
201                                 const MCSection &MCSec);
202   void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
203                     const COFFSection &Sec, const MCSection &MCSec);
204 
205   // MCObjectWriter interface implementation.
206 
207   void executePostLayoutBinding(MCAssembler &Asm,
208                                 const MCAsmLayout &Layout) override;
209 
210   bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
211                                               const MCSymbol &SymA,
212                                               const MCFragment &FB, bool InSet,
213                                               bool IsPCRel) const override;
214 
215   void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
216                         const MCFragment *Fragment, const MCFixup &Fixup,
217                         MCValue Target, uint64_t &FixedValue) override;
218 
219   void createFileSymbols(MCAssembler &Asm);
220   void setWeakDefaultNames();
221   void assignSectionNumbers();
222   void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
223 
224   void emitAddrsigSection() override { EmitAddrsigSection = true; }
225   void addAddrsigSymbol(const MCSymbol *Sym) override {
226     AddrsigSyms.push_back(Sym);
227   }
228 
229   uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
230 };
231 
232 } // end anonymous namespace
233 
234 //------------------------------------------------------------------------------
235 // Symbol class implementation
236 
237 // In the case that the name does not fit within 8 bytes, the offset
238 // into the string table is stored in the last 4 bytes instead, leaving
239 // the first 4 bytes as 0.
240 void COFFSymbol::set_name_offset(uint32_t Offset) {
241   write32le(Data.Name + 0, 0);
242   write32le(Data.Name + 4, Offset);
243 }
244 
245 //------------------------------------------------------------------------------
246 // WinCOFFObjectWriter class implementation
247 
248 WinCOFFObjectWriter::WinCOFFObjectWriter(
249     std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
250     : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {
251   Header.Machine = TargetObjectWriter->getMachine();
252   // Some relocations on ARM64 (the 21 bit ADRP relocations) have a slightly
253   // limited range for the immediate offset (+/- 1 MB); create extra offset
254   // label symbols with regular intervals to allow referencing a
255   // non-temporary symbol that is close enough.
256   UseOffsetLabels = Header.Machine == COFF::IMAGE_FILE_MACHINE_ARM64;
257 }
258 
259 COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
260   Symbols.push_back(std::make_unique<COFFSymbol>(Name));
261   return Symbols.back().get();
262 }
263 
264 COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
265   COFFSymbol *&Ret = SymbolMap[Symbol];
266   if (!Ret)
267     Ret = createSymbol(Symbol->getName());
268   return Ret;
269 }
270 
271 COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
272   Sections.emplace_back(std::make_unique<COFFSection>(Name));
273   return Sections.back().get();
274 }
275 
276 static uint32_t getAlignment(const MCSectionCOFF &Sec) {
277   switch (Sec.getAlignment()) {
278   case 1:
279     return COFF::IMAGE_SCN_ALIGN_1BYTES;
280   case 2:
281     return COFF::IMAGE_SCN_ALIGN_2BYTES;
282   case 4:
283     return COFF::IMAGE_SCN_ALIGN_4BYTES;
284   case 8:
285     return COFF::IMAGE_SCN_ALIGN_8BYTES;
286   case 16:
287     return COFF::IMAGE_SCN_ALIGN_16BYTES;
288   case 32:
289     return COFF::IMAGE_SCN_ALIGN_32BYTES;
290   case 64:
291     return COFF::IMAGE_SCN_ALIGN_64BYTES;
292   case 128:
293     return COFF::IMAGE_SCN_ALIGN_128BYTES;
294   case 256:
295     return COFF::IMAGE_SCN_ALIGN_256BYTES;
296   case 512:
297     return COFF::IMAGE_SCN_ALIGN_512BYTES;
298   case 1024:
299     return COFF::IMAGE_SCN_ALIGN_1024BYTES;
300   case 2048:
301     return COFF::IMAGE_SCN_ALIGN_2048BYTES;
302   case 4096:
303     return COFF::IMAGE_SCN_ALIGN_4096BYTES;
304   case 8192:
305     return COFF::IMAGE_SCN_ALIGN_8192BYTES;
306   }
307   llvm_unreachable("unsupported section alignment");
308 }
309 
310 /// This function takes a section data object from the assembler
311 /// and creates the associated COFF section staging object.
312 void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &MCSec,
313                                         const MCAsmLayout &Layout) {
314   COFFSection *Section = createSection(MCSec.getName());
315   COFFSymbol *Symbol = createSymbol(MCSec.getName());
316   Section->Symbol = Symbol;
317   Symbol->Section = Section;
318   Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
319 
320   // Create a COMDAT symbol if needed.
321   if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
322     if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
323       COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
324       if (COMDATSymbol->Section)
325         report_fatal_error("two sections have the same comdat");
326       COMDATSymbol->Section = Section;
327     }
328   }
329 
330   // In this case the auxiliary symbol is a Section Definition.
331   Symbol->Aux.resize(1);
332   Symbol->Aux[0] = {};
333   Symbol->Aux[0].AuxType = ATSectionDefinition;
334   Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
335 
336   // Set section alignment.
337   Section->Header.Characteristics = MCSec.getCharacteristics();
338   Section->Header.Characteristics |= getAlignment(MCSec);
339 
340   // Bind internal COFF section to MC section.
341   Section->MCSection = &MCSec;
342   SectionMap[&MCSec] = Section;
343 
344   if (UseOffsetLabels && !MCSec.getFragmentList().empty()) {
345     const uint32_t Interval = 1 << OffsetLabelIntervalBits;
346     uint32_t N = 1;
347     for (uint32_t Off = Interval, E = Layout.getSectionAddressSize(&MCSec);
348          Off < E; Off += Interval) {
349       auto Name = ("$L" + MCSec.getName() + "_" + Twine(N++)).str();
350       COFFSymbol *Label = createSymbol(Name);
351       Label->Section = Section;
352       Label->Data.StorageClass = COFF::IMAGE_SYM_CLASS_LABEL;
353       Label->Data.Value = Off;
354       Section->OffsetSymbols.push_back(Label);
355     }
356   }
357 }
358 
359 static uint64_t getSymbolValue(const MCSymbol &Symbol,
360                                const MCAsmLayout &Layout) {
361   if (Symbol.isCommon() && Symbol.isExternal())
362     return Symbol.getCommonSize();
363 
364   uint64_t Res;
365   if (!Layout.getSymbolOffset(Symbol, Res))
366     return 0;
367 
368   return Res;
369 }
370 
371 COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
372   if (!Symbol.isVariable())
373     return nullptr;
374 
375   const MCSymbolRefExpr *SymRef =
376       dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
377   if (!SymRef)
378     return nullptr;
379 
380   const MCSymbol &Aliasee = SymRef->getSymbol();
381   if (Aliasee.isUndefined() || Aliasee.isExternal())
382     return GetOrCreateCOFFSymbol(&Aliasee);
383   else
384     return nullptr;
385 }
386 
387 /// This function takes a symbol data object from the assembler
388 /// and creates the associated COFF symbol staging object.
389 void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
390                                        MCAssembler &Assembler,
391                                        const MCAsmLayout &Layout) {
392   COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
393   const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
394   COFFSection *Sec = nullptr;
395   if (Base && Base->getFragment()) {
396     Sec = SectionMap[Base->getFragment()->getParent()];
397     if (Sym->Section && Sym->Section != Sec)
398       report_fatal_error("conflicting sections for symbol");
399   }
400 
401   COFFSymbol *Local = nullptr;
402   if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
403     Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
404     Sym->Section = nullptr;
405 
406     COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
407     if (!WeakDefault) {
408       std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
409       WeakDefault = createSymbol(WeakName);
410       if (!Sec)
411         WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
412       else
413         WeakDefault->Section = Sec;
414       WeakDefaults.insert(WeakDefault);
415       Local = WeakDefault;
416     }
417 
418     Sym->Other = WeakDefault;
419 
420     // Setup the Weak External auxiliary symbol.
421     Sym->Aux.resize(1);
422     memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0]));
423     Sym->Aux[0].AuxType = ATWeakExternal;
424     Sym->Aux[0].Aux.WeakExternal.TagIndex = 0;
425     Sym->Aux[0].Aux.WeakExternal.Characteristics =
426         COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS;
427   } else {
428     if (!Base)
429       Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
430     else
431       Sym->Section = Sec;
432     Local = Sym;
433   }
434 
435   if (Local) {
436     Local->Data.Value = getSymbolValue(MCSym, Layout);
437 
438     const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
439     Local->Data.Type = SymbolCOFF.getType();
440     Local->Data.StorageClass = SymbolCOFF.getClass();
441 
442     // If no storage class was specified in the streamer, define it here.
443     if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
444       bool IsExternal = MCSym.isExternal() ||
445                         (!MCSym.getFragment() && !MCSym.isVariable());
446 
447       Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
448                                             : COFF::IMAGE_SYM_CLASS_STATIC;
449     }
450   }
451 
452   Sym->MC = &MCSym;
453 }
454 
455 void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
456   if (S.Name.size() <= COFF::NameSize) {
457     std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
458     return;
459   }
460 
461   uint64_t StringTableEntry = Strings.getOffset(S.Name);
462   if (!COFF::encodeSectionName(S.Header.Name, StringTableEntry))
463     report_fatal_error("COFF string table is greater than 64 GB.");
464 }
465 
466 void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
467   if (S.Name.size() > COFF::NameSize)
468     S.set_name_offset(Strings.getOffset(S.Name));
469   else
470     std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
471 }
472 
473 bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
474   return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
475          0;
476 }
477 
478 //------------------------------------------------------------------------------
479 // entity writing methods
480 
481 void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
482   if (UseBigObj) {
483     W.write<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
484     W.write<uint16_t>(0xFFFF);
485     W.write<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion);
486     W.write<uint16_t>(Header.Machine);
487     W.write<uint32_t>(Header.TimeDateStamp);
488     W.OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
489     W.write<uint32_t>(0);
490     W.write<uint32_t>(0);
491     W.write<uint32_t>(0);
492     W.write<uint32_t>(0);
493     W.write<uint32_t>(Header.NumberOfSections);
494     W.write<uint32_t>(Header.PointerToSymbolTable);
495     W.write<uint32_t>(Header.NumberOfSymbols);
496   } else {
497     W.write<uint16_t>(Header.Machine);
498     W.write<uint16_t>(static_cast<int16_t>(Header.NumberOfSections));
499     W.write<uint32_t>(Header.TimeDateStamp);
500     W.write<uint32_t>(Header.PointerToSymbolTable);
501     W.write<uint32_t>(Header.NumberOfSymbols);
502     W.write<uint16_t>(Header.SizeOfOptionalHeader);
503     W.write<uint16_t>(Header.Characteristics);
504   }
505 }
506 
507 void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
508   W.OS.write(S.Data.Name, COFF::NameSize);
509   W.write<uint32_t>(S.Data.Value);
510   if (UseBigObj)
511     W.write<uint32_t>(S.Data.SectionNumber);
512   else
513     W.write<uint16_t>(static_cast<int16_t>(S.Data.SectionNumber));
514   W.write<uint16_t>(S.Data.Type);
515   W.OS << char(S.Data.StorageClass);
516   W.OS << char(S.Data.NumberOfAuxSymbols);
517   WriteAuxiliarySymbols(S.Aux);
518 }
519 
520 void WinCOFFObjectWriter::WriteAuxiliarySymbols(
521     const COFFSymbol::AuxiliarySymbols &S) {
522   for (const AuxSymbol &i : S) {
523     switch (i.AuxType) {
524     case ATWeakExternal:
525       W.write<uint32_t>(i.Aux.WeakExternal.TagIndex);
526       W.write<uint32_t>(i.Aux.WeakExternal.Characteristics);
527       W.OS.write_zeros(sizeof(i.Aux.WeakExternal.unused));
528       if (UseBigObj)
529         W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
530       break;
531     case ATFile:
532       W.OS.write(reinterpret_cast<const char *>(&i.Aux),
533                         UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size);
534       break;
535     case ATSectionDefinition:
536       W.write<uint32_t>(i.Aux.SectionDefinition.Length);
537       W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfRelocations);
538       W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfLinenumbers);
539       W.write<uint32_t>(i.Aux.SectionDefinition.CheckSum);
540       W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
541       W.OS << char(i.Aux.SectionDefinition.Selection);
542       W.OS.write_zeros(sizeof(i.Aux.SectionDefinition.unused));
543       W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
544       if (UseBigObj)
545         W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
546       break;
547     }
548   }
549 }
550 
551 // Write the section header.
552 void WinCOFFObjectWriter::writeSectionHeaders() {
553   // Section numbers must be monotonically increasing in the section
554   // header, but our Sections array is not sorted by section number,
555   // so make a copy of Sections and sort it.
556   std::vector<COFFSection *> Arr;
557   for (auto &Section : Sections)
558     Arr.push_back(Section.get());
559   llvm::sort(Arr, [](const COFFSection *A, const COFFSection *B) {
560     return A->Number < B->Number;
561   });
562 
563   for (auto &Section : Arr) {
564     if (Section->Number == -1)
565       continue;
566 
567     COFF::section &S = Section->Header;
568     if (Section->Relocations.size() >= 0xffff)
569       S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
570     W.OS.write(S.Name, COFF::NameSize);
571     W.write<uint32_t>(S.VirtualSize);
572     W.write<uint32_t>(S.VirtualAddress);
573     W.write<uint32_t>(S.SizeOfRawData);
574     W.write<uint32_t>(S.PointerToRawData);
575     W.write<uint32_t>(S.PointerToRelocations);
576     W.write<uint32_t>(S.PointerToLineNumbers);
577     W.write<uint16_t>(S.NumberOfRelocations);
578     W.write<uint16_t>(S.NumberOfLineNumbers);
579     W.write<uint32_t>(S.Characteristics);
580   }
581 }
582 
583 void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
584   W.write<uint32_t>(R.VirtualAddress);
585   W.write<uint32_t>(R.SymbolTableIndex);
586   W.write<uint16_t>(R.Type);
587 }
588 
589 // Write MCSec's contents. What this function does is essentially
590 // "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
591 // because it needs to compute a CRC.
592 uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm,
593                                                    const MCAsmLayout &Layout,
594                                                    const MCSection &MCSec) {
595   // Save the contents of the section to a temporary buffer, we need this
596   // to CRC the data before we dump it into the object file.
597   SmallVector<char, 128> Buf;
598   raw_svector_ostream VecOS(Buf);
599   Asm.writeSectionData(VecOS, &MCSec, Layout);
600 
601   // Write the section contents to the object file.
602   W.OS << Buf;
603 
604   // Calculate our CRC with an initial value of '0', this is not how
605   // JamCRC is specified but it aligns with the expected output.
606   JamCRC JC(/*Init=*/0);
607   JC.update(makeArrayRef(reinterpret_cast<uint8_t*>(Buf.data()), Buf.size()));
608   return JC.getCRC();
609 }
610 
611 void WinCOFFObjectWriter::writeSection(MCAssembler &Asm,
612                                        const MCAsmLayout &Layout,
613                                        const COFFSection &Sec,
614                                        const MCSection &MCSec) {
615   if (Sec.Number == -1)
616     return;
617 
618   // Write the section contents.
619   if (Sec.Header.PointerToRawData != 0) {
620     assert(W.OS.tell() == Sec.Header.PointerToRawData &&
621            "Section::PointerToRawData is insane!");
622 
623     uint32_t CRC = writeSectionContents(Asm, Layout, MCSec);
624 
625     // Update the section definition auxiliary symbol to record the CRC.
626     COFFSection *Sec = SectionMap[&MCSec];
627     COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
628     assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
629     AuxSymbol &SecDef = AuxSyms[0];
630     SecDef.Aux.SectionDefinition.CheckSum = CRC;
631   }
632 
633   // Write relocations for this section.
634   if (Sec.Relocations.empty()) {
635     assert(Sec.Header.PointerToRelocations == 0 &&
636            "Section::PointerToRelocations is insane!");
637     return;
638   }
639 
640   assert(W.OS.tell() == Sec.Header.PointerToRelocations &&
641          "Section::PointerToRelocations is insane!");
642 
643   if (Sec.Relocations.size() >= 0xffff) {
644     // In case of overflow, write actual relocation count as first
645     // relocation. Including the synthetic reloc itself (+ 1).
646     COFF::relocation R;
647     R.VirtualAddress = Sec.Relocations.size() + 1;
648     R.SymbolTableIndex = 0;
649     R.Type = 0;
650     WriteRelocation(R);
651   }
652 
653   for (const auto &Relocation : Sec.Relocations)
654     WriteRelocation(Relocation.Data);
655 }
656 
657 ////////////////////////////////////////////////////////////////////////////////
658 // MCObjectWriter interface implementations
659 
660 void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
661                                                    const MCAsmLayout &Layout) {
662   if (EmitAddrsigSection) {
663     AddrsigSection = Asm.getContext().getCOFFSection(
664         ".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE,
665         SectionKind::getMetadata());
666     Asm.registerSection(*AddrsigSection);
667   }
668 
669   if (!Asm.CGProfile.empty()) {
670     CGProfileSection = Asm.getContext().getCOFFSection(
671         ".llvm.call-graph-profile", COFF::IMAGE_SCN_LNK_REMOVE,
672         SectionKind::getMetadata());
673     Asm.registerSection(*CGProfileSection);
674   }
675 
676   // "Define" each section & symbol. This creates section & symbol
677   // entries in the staging area.
678   for (const auto &Section : Asm)
679     defineSection(static_cast<const MCSectionCOFF &>(Section), Layout);
680 
681   for (const MCSymbol &Symbol : Asm.symbols())
682     if (!Symbol.isTemporary())
683       DefineSymbol(Symbol, Asm, Layout);
684 }
685 
686 bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
687     const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
688     bool InSet, bool IsPCRel) const {
689   // Don't drop relocations between functions, even if they are in the same text
690   // section. Multiple Visual C++ linker features depend on having the
691   // relocations present. The /INCREMENTAL flag will cause these relocations to
692   // point to thunks, and the /GUARD:CF flag assumes that it can use relocations
693   // to approximate the set of all address taken functions. LLD's implementation
694   // of /GUARD:CF also relies on the existance of these relocations.
695   uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
696   if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
697     return false;
698   return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
699                                                                 InSet, IsPCRel);
700 }
701 
702 void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
703                                            const MCAsmLayout &Layout,
704                                            const MCFragment *Fragment,
705                                            const MCFixup &Fixup, MCValue Target,
706                                            uint64_t &FixedValue) {
707   assert(Target.getSymA() && "Relocation must reference a symbol!");
708 
709   const MCSymbol &A = Target.getSymA()->getSymbol();
710   if (!A.isRegistered()) {
711     Asm.getContext().reportError(Fixup.getLoc(),
712                                       Twine("symbol '") + A.getName() +
713                                           "' can not be undefined");
714     return;
715   }
716   if (A.isTemporary() && A.isUndefined()) {
717     Asm.getContext().reportError(Fixup.getLoc(),
718                                       Twine("assembler label '") + A.getName() +
719                                           "' can not be undefined");
720     return;
721   }
722 
723   MCSection *MCSec = Fragment->getParent();
724 
725   // Mark this symbol as requiring an entry in the symbol table.
726   assert(SectionMap.find(MCSec) != SectionMap.end() &&
727          "Section must already have been defined in executePostLayoutBinding!");
728 
729   COFFSection *Sec = SectionMap[MCSec];
730   const MCSymbolRefExpr *SymB = Target.getSymB();
731 
732   if (SymB) {
733     const MCSymbol *B = &SymB->getSymbol();
734     if (!B->getFragment()) {
735       Asm.getContext().reportError(
736           Fixup.getLoc(),
737           Twine("symbol '") + B->getName() +
738               "' can not be undefined in a subtraction expression");
739       return;
740     }
741 
742     // Offset of the symbol in the section
743     int64_t OffsetOfB = Layout.getSymbolOffset(*B);
744 
745     // Offset of the relocation in the section
746     int64_t OffsetOfRelocation =
747         Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
748 
749     FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
750   } else {
751     FixedValue = Target.getConstant();
752   }
753 
754   COFFRelocation Reloc;
755 
756   Reloc.Data.SymbolTableIndex = 0;
757   Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
758 
759   // Turn relocations for temporary symbols into section relocations.
760   if (A.isTemporary()) {
761     MCSection *TargetSection = &A.getSection();
762     assert(
763         SectionMap.find(TargetSection) != SectionMap.end() &&
764         "Section must already have been defined in executePostLayoutBinding!");
765     COFFSection *Section = SectionMap[TargetSection];
766     Reloc.Symb = Section->Symbol;
767     FixedValue += Layout.getSymbolOffset(A);
768     // Technically, we should do the final adjustments of FixedValue (below)
769     // before picking an offset symbol, otherwise we might choose one which
770     // is slightly too far away. The relocations where it really matters
771     // (arm64 adrp relocations) don't get any offset though.
772     if (UseOffsetLabels && !Section->OffsetSymbols.empty()) {
773       uint64_t LabelIndex = FixedValue >> OffsetLabelIntervalBits;
774       if (LabelIndex > 0) {
775         if (LabelIndex <= Section->OffsetSymbols.size())
776           Reloc.Symb = Section->OffsetSymbols[LabelIndex - 1];
777         else
778           Reloc.Symb = Section->OffsetSymbols.back();
779         FixedValue -= Reloc.Symb->Data.Value;
780       }
781     }
782   } else {
783     assert(
784         SymbolMap.find(&A) != SymbolMap.end() &&
785         "Symbol must already have been defined in executePostLayoutBinding!");
786     Reloc.Symb = SymbolMap[&A];
787   }
788 
789   ++Reloc.Symb->Relocations;
790 
791   Reloc.Data.VirtualAddress += Fixup.getOffset();
792   Reloc.Data.Type = TargetObjectWriter->getRelocType(
793       Asm.getContext(), Target, Fixup, SymB, Asm.getBackend());
794 
795   // The *_REL32 relocations are relative to the end of the relocation,
796   // not to the start.
797   if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
798        Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
799       (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
800        Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32) ||
801       (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT &&
802        Reloc.Data.Type == COFF::IMAGE_REL_ARM_REL32) ||
803       (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARM64 &&
804        Reloc.Data.Type == COFF::IMAGE_REL_ARM64_REL32))
805     FixedValue += 4;
806 
807   if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
808     switch (Reloc.Data.Type) {
809     case COFF::IMAGE_REL_ARM_ABSOLUTE:
810     case COFF::IMAGE_REL_ARM_ADDR32:
811     case COFF::IMAGE_REL_ARM_ADDR32NB:
812     case COFF::IMAGE_REL_ARM_TOKEN:
813     case COFF::IMAGE_REL_ARM_SECTION:
814     case COFF::IMAGE_REL_ARM_SECREL:
815       break;
816     case COFF::IMAGE_REL_ARM_BRANCH11:
817     case COFF::IMAGE_REL_ARM_BLX11:
818     // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
819     // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
820     // for Windows CE).
821     case COFF::IMAGE_REL_ARM_BRANCH24:
822     case COFF::IMAGE_REL_ARM_BLX24:
823     case COFF::IMAGE_REL_ARM_MOV32A:
824       // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
825       // only used for ARM mode code, which is documented as being unsupported
826       // by Windows on ARM.  Empirical proof indicates that masm is able to
827       // generate the relocations however the rest of the MSVC toolchain is
828       // unable to handle it.
829       llvm_unreachable("unsupported relocation");
830       break;
831     case COFF::IMAGE_REL_ARM_MOV32T:
832       break;
833     case COFF::IMAGE_REL_ARM_BRANCH20T:
834     case COFF::IMAGE_REL_ARM_BRANCH24T:
835     case COFF::IMAGE_REL_ARM_BLX23T:
836       // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
837       // perform a 4 byte adjustment to the relocation.  Relative branches are
838       // offset by 4 on ARM, however, because there is no RELA relocations, all
839       // branches are offset by 4.
840       FixedValue = FixedValue + 4;
841       break;
842     }
843   }
844 
845   // The fixed value never makes sense for section indices, ignore it.
846   if (Fixup.getKind() == FK_SecRel_2)
847     FixedValue = 0;
848 
849   if (TargetObjectWriter->recordRelocation(Fixup))
850     Sec->Relocations.push_back(Reloc);
851 }
852 
853 static std::time_t getTime() {
854   std::time_t Now = time(nullptr);
855   if (Now < 0 || !isUInt<32>(Now))
856     return UINT32_MAX;
857   return Now;
858 }
859 
860 // Create .file symbols.
861 void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) {
862   for (const std::pair<std::string, size_t> &It : Asm.getFileNames()) {
863     // round up to calculate the number of auxiliary symbols required
864     const std::string &Name = It.first;
865     unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
866     unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
867 
868     COFFSymbol *File = createSymbol(".file");
869     File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
870     File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
871     File->Aux.resize(Count);
872 
873     unsigned Offset = 0;
874     unsigned Length = Name.size();
875     for (auto &Aux : File->Aux) {
876       Aux.AuxType = ATFile;
877 
878       if (Length > SymbolSize) {
879         memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
880         Length = Length - SymbolSize;
881       } else {
882         memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
883         memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
884         break;
885       }
886 
887       Offset += SymbolSize;
888     }
889   }
890 }
891 
892 void WinCOFFObjectWriter::setWeakDefaultNames() {
893   if (WeakDefaults.empty())
894     return;
895 
896   // If multiple object files use a weak symbol (either with a regular
897   // defined default, or an absolute zero symbol as default), the defaults
898   // cause duplicate definitions unless their names are made unique. Look
899   // for a defined extern symbol, that isn't comdat - that should be unique
900   // unless there are other duplicate definitions. And if none is found,
901   // allow picking a comdat symbol, as that's still better than nothing.
902 
903   COFFSymbol *Unique = nullptr;
904   for (bool AllowComdat : {false, true}) {
905     for (auto &Sym : Symbols) {
906       // Don't include the names of the defaults themselves
907       if (WeakDefaults.count(Sym.get()))
908         continue;
909       // Only consider external symbols
910       if (Sym->Data.StorageClass != COFF::IMAGE_SYM_CLASS_EXTERNAL)
911         continue;
912       // Only consider symbols defined in a section or that are absolute
913       if (!Sym->Section && Sym->Data.SectionNumber != COFF::IMAGE_SYM_ABSOLUTE)
914         continue;
915       if (!AllowComdat && Sym->Section &&
916           Sym->Section->Header.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
917         continue;
918       Unique = Sym.get();
919       break;
920     }
921     if (Unique)
922       break;
923   }
924   // If we didn't find any unique symbol to use for the names, just skip this.
925   if (!Unique)
926     return;
927   for (auto *Sym : WeakDefaults) {
928     Sym->Name.append(".");
929     Sym->Name.append(Unique->Name);
930   }
931 }
932 
933 static bool isAssociative(const COFFSection &Section) {
934   return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
935          COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
936 }
937 
938 void WinCOFFObjectWriter::assignSectionNumbers() {
939   size_t I = 1;
940   auto Assign = [&](COFFSection &Section) {
941     Section.Number = I;
942     Section.Symbol->Data.SectionNumber = I;
943     Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
944     ++I;
945   };
946 
947   // Although it is not explicitly requested by the Microsoft COFF spec,
948   // we should avoid emitting forward associative section references,
949   // because MSVC link.exe as of 2017 cannot handle that.
950   for (const std::unique_ptr<COFFSection> &Section : Sections)
951     if (!isAssociative(*Section))
952       Assign(*Section);
953   for (const std::unique_ptr<COFFSection> &Section : Sections)
954     if (isAssociative(*Section))
955       Assign(*Section);
956 }
957 
958 // Assign file offsets to COFF object file structures.
959 void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm,
960                                             const MCAsmLayout &Layout) {
961   unsigned Offset = W.OS.tell();
962 
963   Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
964   Offset += COFF::SectionSize * Header.NumberOfSections;
965 
966   for (const auto &Section : Asm) {
967     COFFSection *Sec = SectionMap[&Section];
968 
969     if (Sec->Number == -1)
970       continue;
971 
972     Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
973 
974     if (IsPhysicalSection(Sec)) {
975       Sec->Header.PointerToRawData = Offset;
976       Offset += Sec->Header.SizeOfRawData;
977     }
978 
979     if (!Sec->Relocations.empty()) {
980       bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
981 
982       if (RelocationsOverflow) {
983         // Signal overflow by setting NumberOfRelocations to max value. Actual
984         // size is found in reloc #0. Microsoft tools understand this.
985         Sec->Header.NumberOfRelocations = 0xffff;
986       } else {
987         Sec->Header.NumberOfRelocations = Sec->Relocations.size();
988       }
989       Sec->Header.PointerToRelocations = Offset;
990 
991       if (RelocationsOverflow) {
992         // Reloc #0 will contain actual count, so make room for it.
993         Offset += COFF::RelocationSize;
994       }
995 
996       Offset += COFF::RelocationSize * Sec->Relocations.size();
997 
998       for (auto &Relocation : Sec->Relocations) {
999         assert(Relocation.Symb->getIndex() != -1);
1000         Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
1001       }
1002     }
1003 
1004     assert(Sec->Symbol->Aux.size() == 1 &&
1005            "Section's symbol must have one aux!");
1006     AuxSymbol &Aux = Sec->Symbol->Aux[0];
1007     assert(Aux.AuxType == ATSectionDefinition &&
1008            "Section's symbol's aux symbol must be a Section Definition!");
1009     Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
1010     Aux.Aux.SectionDefinition.NumberOfRelocations =
1011         Sec->Header.NumberOfRelocations;
1012     Aux.Aux.SectionDefinition.NumberOfLinenumbers =
1013         Sec->Header.NumberOfLineNumbers;
1014   }
1015 
1016   Header.PointerToSymbolTable = Offset;
1017 }
1018 
1019 uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
1020                                           const MCAsmLayout &Layout) {
1021   uint64_t StartOffset = W.OS.tell();
1022 
1023   if (Sections.size() > INT32_MAX)
1024     report_fatal_error(
1025         "PE COFF object files can't have more than 2147483647 sections");
1026 
1027   UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
1028   Header.NumberOfSections = Sections.size();
1029   Header.NumberOfSymbols = 0;
1030 
1031   setWeakDefaultNames();
1032   assignSectionNumbers();
1033   createFileSymbols(Asm);
1034 
1035   for (auto &Symbol : Symbols) {
1036     // Update section number & offset for symbols that have them.
1037     if (Symbol->Section)
1038       Symbol->Data.SectionNumber = Symbol->Section->Number;
1039     Symbol->setIndex(Header.NumberOfSymbols++);
1040     // Update auxiliary symbol info.
1041     Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
1042     Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
1043   }
1044 
1045   // Build string table.
1046   for (const auto &S : Sections)
1047     if (S->Name.size() > COFF::NameSize)
1048       Strings.add(S->Name);
1049   for (const auto &S : Symbols)
1050     if (S->Name.size() > COFF::NameSize)
1051       Strings.add(S->Name);
1052   Strings.finalize();
1053 
1054   // Set names.
1055   for (const auto &S : Sections)
1056     SetSectionName(*S);
1057   for (auto &S : Symbols)
1058     SetSymbolName(*S);
1059 
1060   // Fixup weak external references.
1061   for (auto &Symbol : Symbols) {
1062     if (Symbol->Other) {
1063       assert(Symbol->getIndex() != -1);
1064       assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
1065       assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
1066              "Symbol's aux symbol must be a Weak External!");
1067       Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
1068     }
1069   }
1070 
1071   // Fixup associative COMDAT sections.
1072   for (auto &Section : Sections) {
1073     if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
1074         COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
1075       continue;
1076 
1077     const MCSectionCOFF &MCSec = *Section->MCSection;
1078     const MCSymbol *AssocMCSym = MCSec.getCOMDATSymbol();
1079     assert(AssocMCSym);
1080 
1081     // It's an error to try to associate with an undefined symbol or a symbol
1082     // without a section.
1083     if (!AssocMCSym->isInSection()) {
1084       Asm.getContext().reportError(
1085           SMLoc(), Twine("cannot make section ") + MCSec.getName() +
1086                        Twine(" associative with sectionless symbol ") +
1087                        AssocMCSym->getName());
1088       continue;
1089     }
1090 
1091     const auto *AssocMCSec = cast<MCSectionCOFF>(&AssocMCSym->getSection());
1092     assert(SectionMap.count(AssocMCSec));
1093     COFFSection *AssocSec = SectionMap[AssocMCSec];
1094 
1095     // Skip this section if the associated section is unused.
1096     if (AssocSec->Number == -1)
1097       continue;
1098 
1099     Section->Symbol->Aux[0].Aux.SectionDefinition.Number = AssocSec->Number;
1100   }
1101 
1102   // Create the contents of the .llvm_addrsig section.
1103   if (EmitAddrsigSection) {
1104     auto Frag = new MCDataFragment(AddrsigSection);
1105     Frag->setLayoutOrder(0);
1106     raw_svector_ostream OS(Frag->getContents());
1107     for (const MCSymbol *S : AddrsigSyms) {
1108       if (!S->isTemporary()) {
1109         encodeULEB128(S->getIndex(), OS);
1110         continue;
1111       }
1112 
1113       MCSection *TargetSection = &S->getSection();
1114       assert(SectionMap.find(TargetSection) != SectionMap.end() &&
1115              "Section must already have been defined in "
1116              "executePostLayoutBinding!");
1117       encodeULEB128(SectionMap[TargetSection]->Symbol->getIndex(), OS);
1118     }
1119   }
1120 
1121   // Create the contents of the .llvm.call-graph-profile section.
1122   if (CGProfileSection) {
1123     auto *Frag = new MCDataFragment(CGProfileSection);
1124     Frag->setLayoutOrder(0);
1125     raw_svector_ostream OS(Frag->getContents());
1126     for (const MCAssembler::CGProfileEntry &CGPE : Asm.CGProfile) {
1127       uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
1128       uint32_t ToIndex = CGPE.To->getSymbol().getIndex();
1129       support::endian::write(OS, FromIndex, W.Endian);
1130       support::endian::write(OS, ToIndex, W.Endian);
1131       support::endian::write(OS, CGPE.Count, W.Endian);
1132     }
1133   }
1134 
1135   assignFileOffsets(Asm, Layout);
1136 
1137   // MS LINK expects to be able to use this timestamp to implement their
1138   // /INCREMENTAL feature.
1139   if (Asm.isIncrementalLinkerCompatible()) {
1140     Header.TimeDateStamp = getTime();
1141   } else {
1142     // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
1143     Header.TimeDateStamp = 0;
1144   }
1145 
1146   // Write it all to disk...
1147   WriteFileHeader(Header);
1148   writeSectionHeaders();
1149 
1150   // Write section contents.
1151   sections::iterator I = Sections.begin();
1152   sections::iterator IE = Sections.end();
1153   MCAssembler::iterator J = Asm.begin();
1154   MCAssembler::iterator JE = Asm.end();
1155   for (; I != IE && J != JE; ++I, ++J)
1156     writeSection(Asm, Layout, **I, *J);
1157 
1158   assert(W.OS.tell() == Header.PointerToSymbolTable &&
1159          "Header::PointerToSymbolTable is insane!");
1160 
1161   // Write a symbol table.
1162   for (auto &Symbol : Symbols)
1163     if (Symbol->getIndex() != -1)
1164       WriteSymbol(*Symbol);
1165 
1166   // Write a string table, which completes the entire COFF file.
1167   Strings.write(W.OS);
1168 
1169   return W.OS.tell() - StartOffset;
1170 }
1171 
1172 MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1173     : Machine(Machine_) {}
1174 
1175 // Pin the vtable to this file.
1176 void MCWinCOFFObjectTargetWriter::anchor() {}
1177 
1178 //------------------------------------------------------------------------------
1179 // WinCOFFObjectWriter factory function
1180 
1181 std::unique_ptr<MCObjectWriter> llvm::createWinCOFFObjectWriter(
1182     std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) {
1183   return std::make_unique<WinCOFFObjectWriter>(std::move(MOTW), OS);
1184 }
1185