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