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