xref: /llvm-project-15.0.7/lld/COFF/Writer.cpp (revision 9284931e)
1 //===- Writer.cpp ---------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Writer.h"
11 #include "Config.h"
12 #include "DLL.h"
13 #include "InputFiles.h"
14 #include "MapFile.h"
15 #include "PDB.h"
16 #include "SymbolTable.h"
17 #include "Symbols.h"
18 #include "lld/Common/ErrorHandler.h"
19 #include "lld/Common/Memory.h"
20 #include "lld/Common/Timer.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/StringSwitch.h"
24 #include "llvm/Support/BinaryStreamReader.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/FileOutputBuffer.h"
28 #include "llvm/Support/Parallel.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/RandomNumberGenerator.h"
31 #include <algorithm>
32 #include <cstdio>
33 #include <map>
34 #include <memory>
35 #include <utility>
36 
37 using namespace llvm;
38 using namespace llvm::COFF;
39 using namespace llvm::object;
40 using namespace llvm::support;
41 using namespace llvm::support::endian;
42 using namespace lld;
43 using namespace lld::coff;
44 
45 static const int SectorSize = 512;
46 static const int DOSStubSize = 64;
47 static const int NumberfOfDataDirectory = 16;
48 
49 namespace {
50 
51 class DebugDirectoryChunk : public Chunk {
52 public:
53   DebugDirectoryChunk(const std::vector<Chunk *> &R) : Records(R) {}
54 
55   size_t getSize() const override {
56     return Records.size() * sizeof(debug_directory);
57   }
58 
59   void writeTo(uint8_t *B) const override {
60     auto *D = reinterpret_cast<debug_directory *>(B + OutputSectionOff);
61 
62     for (const Chunk *Record : Records) {
63       D->Characteristics = 0;
64       D->TimeDateStamp = 0;
65       D->MajorVersion = 0;
66       D->MinorVersion = 0;
67       D->Type = COFF::IMAGE_DEBUG_TYPE_CODEVIEW;
68       D->SizeOfData = Record->getSize();
69       D->AddressOfRawData = Record->getRVA();
70       OutputSection *OS = Record->getOutputSection();
71       uint64_t Offs = OS->getFileOff() + (Record->getRVA() - OS->getRVA());
72       D->PointerToRawData = Offs;
73 
74       ++D;
75     }
76   }
77 
78 private:
79   const std::vector<Chunk *> &Records;
80 };
81 
82 class CVDebugRecordChunk : public Chunk {
83 public:
84   CVDebugRecordChunk() {
85     PDBAbsPath = Config->PDBPath;
86     if (!PDBAbsPath.empty())
87       llvm::sys::fs::make_absolute(PDBAbsPath);
88   }
89 
90   size_t getSize() const override {
91     return sizeof(codeview::DebugInfo) + PDBAbsPath.size() + 1;
92   }
93 
94   void writeTo(uint8_t *B) const override {
95     // Save off the DebugInfo entry to backfill the file signature (build id)
96     // in Writer::writeBuildId
97     BuildId = reinterpret_cast<codeview::DebugInfo *>(B + OutputSectionOff);
98 
99     // variable sized field (PDB Path)
100     char *P = reinterpret_cast<char *>(B + OutputSectionOff + sizeof(*BuildId));
101     if (!PDBAbsPath.empty())
102       memcpy(P, PDBAbsPath.data(), PDBAbsPath.size());
103     P[PDBAbsPath.size()] = '\0';
104   }
105 
106   SmallString<128> PDBAbsPath;
107   mutable codeview::DebugInfo *BuildId = nullptr;
108 };
109 
110 // The writer writes a SymbolTable result to a file.
111 class Writer {
112 public:
113   Writer() : Buffer(errorHandler().OutputBuffer) {}
114   void run();
115 
116 private:
117   void createSections();
118   void createMiscChunks();
119   void createImportTables();
120   void createExportTable();
121   void assignAddresses();
122   void removeEmptySections();
123   void createSymbolAndStringTable();
124   void openFile(StringRef OutputPath);
125   template <typename PEHeaderTy> void writeHeader();
126   void createSEHTable(OutputSection *RData);
127   void createGFIDTable(OutputSection *RData);
128   void markSymbolsForRVATable(ObjFile *File,
129                               ArrayRef<SectionChunk *> SymIdxChunks,
130                               SymbolRVASet &TableSymbols);
131   void maybeAddRVATable(OutputSection *RData, SymbolRVASet TableSymbols,
132                         StringRef TableSym, StringRef CountSym);
133   void setSectionPermissions();
134   void writeSections();
135   void writeBuildId();
136   void sortExceptionTable();
137 
138   llvm::Optional<coff_symbol16> createSymbol(Defined *D);
139   size_t addEntryToStringTable(StringRef Str);
140 
141   OutputSection *findSection(StringRef Name);
142   OutputSection *createSection(StringRef Name);
143   void addBaserels(OutputSection *Dest);
144   void addBaserelBlocks(OutputSection *Dest, std::vector<Baserel> &V);
145 
146   uint32_t getSizeOfInitializedData();
147   std::map<StringRef, std::vector<DefinedImportData *>> binImports();
148 
149   std::unique_ptr<FileOutputBuffer> &Buffer;
150   std::vector<OutputSection *> OutputSections;
151   std::vector<char> Strtab;
152   std::vector<llvm::object::coff_symbol16> OutputSymtab;
153   IdataContents Idata;
154   DelayLoadContents DelayIdata;
155   EdataContents Edata;
156   RVATableChunk *GuardFidsTable = nullptr;
157   RVATableChunk *SEHTable = nullptr;
158 
159   Chunk *DebugDirectory = nullptr;
160   std::vector<Chunk *> DebugRecords;
161   CVDebugRecordChunk *BuildId = nullptr;
162   Optional<codeview::DebugInfo> PreviousBuildId;
163   ArrayRef<uint8_t> SectionTable;
164 
165   uint64_t FileSize;
166   uint32_t PointerToSymbolTable = 0;
167   uint64_t SizeOfImage;
168   uint64_t SizeOfHeaders;
169 };
170 } // anonymous namespace
171 
172 namespace lld {
173 namespace coff {
174 
175 static Timer CodeLayoutTimer("Code Layout", Timer::root());
176 static Timer DiskCommitTimer("Commit Output File", Timer::root());
177 
178 void writeResult() { Writer().run(); }
179 
180 void OutputSection::setRVA(uint64_t RVA) {
181   Header.VirtualAddress = RVA;
182   for (Chunk *C : Chunks)
183     C->setRVA(C->getRVA() + RVA);
184 }
185 
186 void OutputSection::setFileOffset(uint64_t Off) {
187   // If a section has no actual data (i.e. BSS section), we want to
188   // set 0 to its PointerToRawData. Otherwise the output is rejected
189   // by the loader.
190   if (Header.SizeOfRawData == 0)
191     return;
192 
193   // It is possible that this assignment could cause an overflow of the u32,
194   // but that should be caught by the FileSize check in OutputSection::run().
195   Header.PointerToRawData = Off;
196 }
197 
198 void OutputSection::addChunk(Chunk *C) {
199   Chunks.push_back(C);
200   C->setOutputSection(this);
201   uint64_t Off = Header.VirtualSize;
202   Off = alignTo(Off, C->Alignment);
203   C->setRVA(Off);
204   C->OutputSectionOff = Off;
205   Off += C->getSize();
206   if (Off > UINT32_MAX)
207     error("section larger than 4 GiB: " + Name);
208   Header.VirtualSize = Off;
209   if (C->hasData())
210     Header.SizeOfRawData = alignTo(Off, SectorSize);
211 }
212 
213 void OutputSection::addPermissions(uint32_t C) {
214   Header.Characteristics |= C & PermMask;
215 }
216 
217 void OutputSection::setPermissions(uint32_t C) {
218   Header.Characteristics = C & PermMask;
219 }
220 
221 // Write the section header to a given buffer.
222 void OutputSection::writeHeaderTo(uint8_t *Buf) {
223   auto *Hdr = reinterpret_cast<coff_section *>(Buf);
224   *Hdr = Header;
225   if (StringTableOff) {
226     // If name is too long, write offset into the string table as a name.
227     sprintf(Hdr->Name, "/%d", StringTableOff);
228   } else {
229     assert(!Config->Debug || Name.size() <= COFF::NameSize ||
230            (Hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0);
231     strncpy(Hdr->Name, Name.data(),
232             std::min(Name.size(), (size_t)COFF::NameSize));
233   }
234 }
235 
236 } // namespace coff
237 } // namespace lld
238 
239 // PDBs are matched against executables using a build id which consists of three
240 // components:
241 //   1. A 16-bit GUID
242 //   2. An age
243 //   3. A time stamp.
244 //
245 // Debuggers and symbol servers match executables against debug info by checking
246 // each of these components of the EXE/DLL against the corresponding value in
247 // the PDB and failing a match if any of the components differ.  In the case of
248 // symbol servers, symbols are cached in a folder that is a function of the
249 // GUID.  As a result, in order to avoid symbol cache pollution where every
250 // incremental build copies a new PDB to the symbol cache, we must try to re-use
251 // the existing GUID if one exists, but bump the age.  This way the match will
252 // fail, so the symbol cache knows to use the new PDB, but the GUID matches, so
253 // it overwrites the existing item in the symbol cache rather than making a new
254 // one.
255 static Optional<codeview::DebugInfo> loadExistingBuildId(StringRef Path) {
256   // We don't need to incrementally update a previous build id if we're not
257   // writing codeview debug info.
258   if (!Config->Debug)
259     return None;
260 
261   auto ExpectedBinary = llvm::object::createBinary(Path);
262   if (!ExpectedBinary) {
263     consumeError(ExpectedBinary.takeError());
264     return None;
265   }
266 
267   auto Binary = std::move(*ExpectedBinary);
268   if (!Binary.getBinary()->isCOFF())
269     return None;
270 
271   std::error_code EC;
272   COFFObjectFile File(Binary.getBinary()->getMemoryBufferRef(), EC);
273   if (EC)
274     return None;
275 
276   // If the machine of the binary we're outputting doesn't match the machine
277   // of the existing binary, don't try to re-use the build id.
278   if (File.is64() != Config->is64() || File.getMachine() != Config->Machine)
279     return None;
280 
281   for (const auto &DebugDir : File.debug_directories()) {
282     if (DebugDir.Type != IMAGE_DEBUG_TYPE_CODEVIEW)
283       continue;
284 
285     const codeview::DebugInfo *ExistingDI = nullptr;
286     StringRef PDBFileName;
287     if (auto EC = File.getDebugPDBInfo(ExistingDI, PDBFileName)) {
288       (void)EC;
289       return None;
290     }
291     // We only support writing PDBs in v70 format.  So if this is not a build
292     // id that we recognize / support, ignore it.
293     if (ExistingDI->Signature.CVSignature != OMF::Signature::PDB70)
294       return None;
295     return *ExistingDI;
296   }
297   return None;
298 }
299 
300 // The main function of the writer.
301 void Writer::run() {
302   ScopedTimer T1(CodeLayoutTimer);
303 
304   createSections();
305   createMiscChunks();
306   createImportTables();
307   createExportTable();
308   if (Config->Relocatable)
309     createSection(".reloc");
310   assignAddresses();
311   removeEmptySections();
312   setSectionPermissions();
313   createSymbolAndStringTable();
314 
315   if (FileSize > UINT32_MAX)
316     fatal("image size (" + Twine(FileSize) + ") " +
317         "exceeds maximum allowable size (" + Twine(UINT32_MAX) + ")");
318 
319   // We must do this before opening the output file, as it depends on being able
320   // to read the contents of the existing output file.
321   PreviousBuildId = loadExistingBuildId(Config->OutputFile);
322   openFile(Config->OutputFile);
323   if (Config->is64()) {
324     writeHeader<pe32plus_header>();
325   } else {
326     writeHeader<pe32_header>();
327   }
328   writeSections();
329   sortExceptionTable();
330   writeBuildId();
331 
332   T1.stop();
333 
334   if (!Config->PDBPath.empty() && Config->Debug) {
335     assert(BuildId);
336     createPDB(Symtab, OutputSections, SectionTable, *BuildId->BuildId);
337   }
338 
339   writeMapFile(OutputSections);
340 
341   ScopedTimer T2(DiskCommitTimer);
342   if (auto E = Buffer->commit())
343     fatal("failed to write the output file: " + toString(std::move(E)));
344 }
345 
346 static StringRef getOutputSection(StringRef Name) {
347   StringRef S = Name.split('$').first;
348 
349   // Treat a later period as a separator for MinGW, for sections like
350   // ".ctors.01234".
351   S = S.substr(0, S.find('.', 1));
352 
353   auto It = Config->Merge.find(S);
354   if (It == Config->Merge.end())
355     return S;
356   return It->second;
357 }
358 
359 // For /order.
360 static void sortBySectionOrder(std::vector<Chunk *> &Chunks) {
361   auto GetPriority = [](const Chunk *C) {
362     if (auto *Sec = dyn_cast<SectionChunk>(C))
363       if (Sec->Sym)
364         return Config->Order.lookup(Sec->Sym->getName());
365     return 0;
366   };
367 
368   std::stable_sort(Chunks.begin(), Chunks.end(),
369                    [=](const Chunk *A, const Chunk *B) {
370                      return GetPriority(A) < GetPriority(B);
371                    });
372 }
373 
374 // Create output section objects and add them to OutputSections.
375 void Writer::createSections() {
376   // First, bin chunks by name.
377   std::map<StringRef, std::vector<Chunk *>> Map;
378   for (Chunk *C : Symtab->getChunks()) {
379     auto *SC = dyn_cast<SectionChunk>(C);
380     if (SC && !SC->isLive()) {
381       if (Config->Verbose)
382         SC->printDiscardedMessage();
383       continue;
384     }
385     Map[C->getSectionName()].push_back(C);
386   }
387 
388   // Process an /order option.
389   if (!Config->Order.empty())
390     for (auto &Pair : Map)
391       sortBySectionOrder(Pair.second);
392 
393   // Then create an OutputSection for each section.
394   // '$' and all following characters in input section names are
395   // discarded when determining output section. So, .text$foo
396   // contributes to .text, for example. See PE/COFF spec 3.2.
397   SmallDenseMap<StringRef, OutputSection *> Sections;
398   for (auto Pair : Map) {
399     StringRef Name = getOutputSection(Pair.first);
400     OutputSection *&Sec = Sections[Name];
401     if (!Sec) {
402       Sec = make<OutputSection>(Name);
403       OutputSections.push_back(Sec);
404     }
405     std::vector<Chunk *> &Chunks = Pair.second;
406     for (Chunk *C : Chunks) {
407       Sec->addChunk(C);
408       Sec->addPermissions(C->getPermissions());
409     }
410   }
411 }
412 
413 void Writer::createMiscChunks() {
414   OutputSection *RData = createSection(".rdata");
415 
416   // Create thunks for locally-dllimported symbols.
417   if (!Symtab->LocalImportChunks.empty()) {
418     for (Chunk *C : Symtab->LocalImportChunks)
419       RData->addChunk(C);
420   }
421 
422   // Create Debug Information Chunks
423   if (Config->Debug) {
424     DebugDirectory = make<DebugDirectoryChunk>(DebugRecords);
425 
426     // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified.  We
427     // output a PDB no matter what, and this chunk provides the only means of
428     // allowing a debugger to match a PDB and an executable.  So we need it even
429     // if we're ultimately not going to write CodeView data to the PDB.
430     auto *CVChunk = make<CVDebugRecordChunk>();
431     BuildId = CVChunk;
432     DebugRecords.push_back(CVChunk);
433 
434     RData->addChunk(DebugDirectory);
435     for (Chunk *C : DebugRecords)
436       RData->addChunk(C);
437   }
438 
439   // Create SEH table. x86-only.
440   if (Config->Machine == I386)
441     createSEHTable(RData);
442 
443   // Create the guard function id table if requested.
444   if (Config->GuardCF)
445     createGFIDTable(RData);
446 }
447 
448 // Create .idata section for the DLL-imported symbol table.
449 // The format of this section is inherently Windows-specific.
450 // IdataContents class abstracted away the details for us,
451 // so we just let it create chunks and add them to the section.
452 void Writer::createImportTables() {
453   if (ImportFile::Instances.empty())
454     return;
455 
456   // Initialize DLLOrder so that import entries are ordered in
457   // the same order as in the command line. (That affects DLL
458   // initialization order, and this ordering is MSVC-compatible.)
459   for (ImportFile *File : ImportFile::Instances) {
460     if (!File->Live)
461       continue;
462 
463     std::string DLL = StringRef(File->DLLName).lower();
464     if (Config->DLLOrder.count(DLL) == 0)
465       Config->DLLOrder[DLL] = Config->DLLOrder.size();
466   }
467 
468   OutputSection *Text = createSection(".text");
469   for (ImportFile *File : ImportFile::Instances) {
470     if (!File->Live)
471       continue;
472 
473     if (DefinedImportThunk *Thunk = File->ThunkSym)
474       Text->addChunk(Thunk->getChunk());
475 
476     if (Config->DelayLoads.count(StringRef(File->DLLName).lower())) {
477       if (!File->ThunkSym)
478         fatal("cannot delay-load " + toString(File) +
479               " due to import of data: " + toString(*File->ImpSym));
480       DelayIdata.add(File->ImpSym);
481     } else {
482       Idata.add(File->ImpSym);
483     }
484   }
485 
486   if (!Idata.empty()) {
487     OutputSection *Sec = createSection(".idata");
488     for (Chunk *C : Idata.getChunks())
489       Sec->addChunk(C);
490   }
491 
492   if (!DelayIdata.empty()) {
493     Defined *Helper = cast<Defined>(Config->DelayLoadHelper);
494     DelayIdata.create(Helper);
495     OutputSection *Sec = createSection(".didat");
496     for (Chunk *C : DelayIdata.getChunks())
497       Sec->addChunk(C);
498     Sec = createSection(".data");
499     for (Chunk *C : DelayIdata.getDataChunks())
500       Sec->addChunk(C);
501     Sec = createSection(".text");
502     for (Chunk *C : DelayIdata.getCodeChunks())
503       Sec->addChunk(C);
504   }
505 }
506 
507 void Writer::createExportTable() {
508   if (Config->Exports.empty())
509     return;
510   OutputSection *Sec = createSection(".edata");
511   for (Chunk *C : Edata.Chunks)
512     Sec->addChunk(C);
513 }
514 
515 // The Windows loader doesn't seem to like empty sections,
516 // so we remove them if any.
517 void Writer::removeEmptySections() {
518   auto IsEmpty = [](OutputSection *S) { return S->getVirtualSize() == 0; };
519   OutputSections.erase(
520       std::remove_if(OutputSections.begin(), OutputSections.end(), IsEmpty),
521       OutputSections.end());
522   uint32_t Idx = 1;
523   for (OutputSection *Sec : OutputSections)
524     Sec->SectionIndex = Idx++;
525 }
526 
527 size_t Writer::addEntryToStringTable(StringRef Str) {
528   assert(Str.size() > COFF::NameSize);
529   size_t OffsetOfEntry = Strtab.size() + 4; // +4 for the size field
530   Strtab.insert(Strtab.end(), Str.begin(), Str.end());
531   Strtab.push_back('\0');
532   return OffsetOfEntry;
533 }
534 
535 Optional<coff_symbol16> Writer::createSymbol(Defined *Def) {
536   // Relative symbols are unrepresentable in a COFF symbol table.
537   if (isa<DefinedSynthetic>(Def))
538     return None;
539 
540   // Don't write dead symbols or symbols in codeview sections to the symbol
541   // table.
542   if (!Def->isLive())
543     return None;
544   if (auto *D = dyn_cast<DefinedRegular>(Def))
545     if (D->getChunk()->isCodeView())
546       return None;
547 
548   coff_symbol16 Sym;
549   StringRef Name = Def->getName();
550   if (Name.size() > COFF::NameSize) {
551     Sym.Name.Offset.Zeroes = 0;
552     Sym.Name.Offset.Offset = addEntryToStringTable(Name);
553   } else {
554     memset(Sym.Name.ShortName, 0, COFF::NameSize);
555     memcpy(Sym.Name.ShortName, Name.data(), Name.size());
556   }
557 
558   if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
559     COFFSymbolRef Ref = D->getCOFFSymbol();
560     Sym.Type = Ref.getType();
561     Sym.StorageClass = Ref.getStorageClass();
562   } else {
563     Sym.Type = IMAGE_SYM_TYPE_NULL;
564     Sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
565   }
566   Sym.NumberOfAuxSymbols = 0;
567 
568   switch (Def->kind()) {
569   case Symbol::DefinedAbsoluteKind:
570     Sym.Value = Def->getRVA();
571     Sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
572     break;
573   default: {
574     uint64_t RVA = Def->getRVA();
575     OutputSection *Sec = nullptr;
576     for (OutputSection *S : OutputSections) {
577       if (S->getRVA() > RVA)
578         break;
579       Sec = S;
580     }
581     Sym.Value = RVA - Sec->getRVA();
582     Sym.SectionNumber = Sec->SectionIndex;
583     break;
584   }
585   }
586   return Sym;
587 }
588 
589 void Writer::createSymbolAndStringTable() {
590   // Name field in the section table is 8 byte long. Longer names need
591   // to be written to the string table. First, construct string table.
592   for (OutputSection *Sec : OutputSections) {
593     StringRef Name = Sec->getName();
594     if (Name.size() <= COFF::NameSize)
595       continue;
596     // If a section isn't discardable (i.e. will be mapped at runtime),
597     // prefer a truncated section name over a long section name in
598     // the string table that is unavailable at runtime. Note that link.exe
599     // always truncates, even for discardable sections.
600     if ((Sec->getPermissions() & IMAGE_SCN_MEM_DISCARDABLE) == 0)
601       continue;
602     Sec->setStringTableOff(addEntryToStringTable(Name));
603   }
604 
605   if (Config->DebugDwarf) {
606     for (ObjFile *File : ObjFile::Instances) {
607       for (Symbol *B : File->getSymbols()) {
608         auto *D = dyn_cast_or_null<Defined>(B);
609         if (!D || D->WrittenToSymtab)
610           continue;
611         D->WrittenToSymtab = true;
612 
613         if (Optional<coff_symbol16> Sym = createSymbol(D))
614           OutputSymtab.push_back(*Sym);
615       }
616     }
617   }
618 
619   if (OutputSymtab.empty() && Strtab.empty())
620     return;
621 
622   // We position the symbol table to be adjacent to the end of the last section.
623   uint64_t FileOff = FileSize;
624   PointerToSymbolTable = FileOff;
625   FileOff += OutputSymtab.size() * sizeof(coff_symbol16);
626   FileOff += 4 + Strtab.size();
627   FileSize = alignTo(FileOff, SectorSize);
628 }
629 
630 // Visits all sections to assign incremental, non-overlapping RVAs and
631 // file offsets.
632 void Writer::assignAddresses() {
633   SizeOfHeaders = DOSStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
634                   sizeof(data_directory) * NumberfOfDataDirectory +
635                   sizeof(coff_section) * OutputSections.size();
636   SizeOfHeaders +=
637       Config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
638   SizeOfHeaders = alignTo(SizeOfHeaders, SectorSize);
639   uint64_t RVA = PageSize; // The first page is kept unmapped.
640   FileSize = SizeOfHeaders;
641   // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because
642   // the loader cannot handle holes.
643   std::stable_partition(
644       OutputSections.begin(), OutputSections.end(), [](OutputSection *S) {
645         return (S->getPermissions() & IMAGE_SCN_MEM_DISCARDABLE) == 0;
646       });
647   for (OutputSection *Sec : OutputSections) {
648     if (Sec->getName() == ".reloc")
649       addBaserels(Sec);
650     Sec->setRVA(RVA);
651     Sec->setFileOffset(FileSize);
652     RVA += alignTo(Sec->getVirtualSize(), PageSize);
653     FileSize += alignTo(Sec->getRawSize(), SectorSize);
654   }
655   SizeOfImage = alignTo(RVA, PageSize);
656 }
657 
658 template <typename PEHeaderTy> void Writer::writeHeader() {
659   // Write DOS stub
660   uint8_t *Buf = Buffer->getBufferStart();
661   auto *DOS = reinterpret_cast<dos_header *>(Buf);
662   Buf += DOSStubSize;
663   DOS->Magic[0] = 'M';
664   DOS->Magic[1] = 'Z';
665   DOS->AddressOfRelocationTable = sizeof(dos_header);
666   DOS->AddressOfNewExeHeader = DOSStubSize;
667 
668   // Write PE magic
669   memcpy(Buf, PEMagic, sizeof(PEMagic));
670   Buf += sizeof(PEMagic);
671 
672   // Write COFF header
673   auto *COFF = reinterpret_cast<coff_file_header *>(Buf);
674   Buf += sizeof(*COFF);
675   COFF->Machine = Config->Machine;
676   COFF->NumberOfSections = OutputSections.size();
677   COFF->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
678   if (Config->LargeAddressAware)
679     COFF->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
680   if (!Config->is64())
681     COFF->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
682   if (Config->DLL)
683     COFF->Characteristics |= IMAGE_FILE_DLL;
684   if (!Config->Relocatable)
685     COFF->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
686   COFF->SizeOfOptionalHeader =
687       sizeof(PEHeaderTy) + sizeof(data_directory) * NumberfOfDataDirectory;
688 
689   // Write PE header
690   auto *PE = reinterpret_cast<PEHeaderTy *>(Buf);
691   Buf += sizeof(*PE);
692   PE->Magic = Config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
693 
694   // If {Major,Minor}LinkerVersion is left at 0.0, then for some
695   // reason signing the resulting PE file with Authenticode produces a
696   // signature that fails to validate on Windows 7 (but is OK on 10).
697   // Set it to 14.0, which is what VS2015 outputs, and which avoids
698   // that problem.
699   PE->MajorLinkerVersion = 14;
700   PE->MinorLinkerVersion = 0;
701 
702   PE->ImageBase = Config->ImageBase;
703   PE->SectionAlignment = PageSize;
704   PE->FileAlignment = SectorSize;
705   PE->MajorImageVersion = Config->MajorImageVersion;
706   PE->MinorImageVersion = Config->MinorImageVersion;
707   PE->MajorOperatingSystemVersion = Config->MajorOSVersion;
708   PE->MinorOperatingSystemVersion = Config->MinorOSVersion;
709   PE->MajorSubsystemVersion = Config->MajorOSVersion;
710   PE->MinorSubsystemVersion = Config->MinorOSVersion;
711   PE->Subsystem = Config->Subsystem;
712   PE->SizeOfImage = SizeOfImage;
713   PE->SizeOfHeaders = SizeOfHeaders;
714   if (!Config->NoEntry) {
715     Defined *Entry = cast<Defined>(Config->Entry);
716     PE->AddressOfEntryPoint = Entry->getRVA();
717     // Pointer to thumb code must have the LSB set, so adjust it.
718     if (Config->Machine == ARMNT)
719       PE->AddressOfEntryPoint |= 1;
720   }
721   PE->SizeOfStackReserve = Config->StackReserve;
722   PE->SizeOfStackCommit = Config->StackCommit;
723   PE->SizeOfHeapReserve = Config->HeapReserve;
724   PE->SizeOfHeapCommit = Config->HeapCommit;
725   if (Config->AppContainer)
726     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER;
727   if (Config->DynamicBase)
728     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
729   if (Config->HighEntropyVA)
730     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
731   if (!Config->AllowBind)
732     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND;
733   if (Config->NxCompat)
734     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
735   if (!Config->AllowIsolation)
736     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION;
737   if (Config->GuardCF)
738     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF;
739   if (Config->Machine == I386 && !SEHTable &&
740       !Symtab->findUnderscore("_load_config_used"))
741     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH;
742   if (Config->TerminalServerAware)
743     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
744   PE->NumberOfRvaAndSize = NumberfOfDataDirectory;
745   if (OutputSection *Text = findSection(".text")) {
746     PE->BaseOfCode = Text->getRVA();
747     PE->SizeOfCode = Text->getRawSize();
748   }
749   PE->SizeOfInitializedData = getSizeOfInitializedData();
750 
751   // Write data directory
752   auto *Dir = reinterpret_cast<data_directory *>(Buf);
753   Buf += sizeof(*Dir) * NumberfOfDataDirectory;
754   if (OutputSection *Sec = findSection(".edata")) {
755     Dir[EXPORT_TABLE].RelativeVirtualAddress = Sec->getRVA();
756     Dir[EXPORT_TABLE].Size = Sec->getVirtualSize();
757   }
758   if (!Idata.empty()) {
759     Dir[IMPORT_TABLE].RelativeVirtualAddress = Idata.getDirRVA();
760     Dir[IMPORT_TABLE].Size = Idata.getDirSize();
761     Dir[IAT].RelativeVirtualAddress = Idata.getIATRVA();
762     Dir[IAT].Size = Idata.getIATSize();
763   }
764   if (OutputSection *Sec = findSection(".rsrc")) {
765     Dir[RESOURCE_TABLE].RelativeVirtualAddress = Sec->getRVA();
766     Dir[RESOURCE_TABLE].Size = Sec->getVirtualSize();
767   }
768   if (OutputSection *Sec = findSection(".pdata")) {
769     Dir[EXCEPTION_TABLE].RelativeVirtualAddress = Sec->getRVA();
770     Dir[EXCEPTION_TABLE].Size = Sec->getVirtualSize();
771   }
772   if (OutputSection *Sec = findSection(".reloc")) {
773     Dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = Sec->getRVA();
774     Dir[BASE_RELOCATION_TABLE].Size = Sec->getVirtualSize();
775   }
776   if (Symbol *Sym = Symtab->findUnderscore("_tls_used")) {
777     if (Defined *B = dyn_cast<Defined>(Sym)) {
778       Dir[TLS_TABLE].RelativeVirtualAddress = B->getRVA();
779       Dir[TLS_TABLE].Size = Config->is64()
780                                 ? sizeof(object::coff_tls_directory64)
781                                 : sizeof(object::coff_tls_directory32);
782     }
783   }
784   if (Config->Debug) {
785     Dir[DEBUG_DIRECTORY].RelativeVirtualAddress = DebugDirectory->getRVA();
786     Dir[DEBUG_DIRECTORY].Size = DebugDirectory->getSize();
787   }
788   if (Symbol *Sym = Symtab->findUnderscore("_load_config_used")) {
789     if (auto *B = dyn_cast<DefinedRegular>(Sym)) {
790       SectionChunk *SC = B->getChunk();
791       assert(B->getRVA() >= SC->getRVA());
792       uint64_t OffsetInChunk = B->getRVA() - SC->getRVA();
793       if (!SC->hasData() || OffsetInChunk + 4 > SC->getSize())
794         fatal("_load_config_used is malformed");
795 
796       ArrayRef<uint8_t> SecContents = SC->getContents();
797       uint32_t LoadConfigSize =
798           *reinterpret_cast<const ulittle32_t *>(&SecContents[OffsetInChunk]);
799       if (OffsetInChunk + LoadConfigSize > SC->getSize())
800         fatal("_load_config_used is too large");
801       Dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = B->getRVA();
802       Dir[LOAD_CONFIG_TABLE].Size = LoadConfigSize;
803     }
804   }
805   if (!DelayIdata.empty()) {
806     Dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
807         DelayIdata.getDirRVA();
808     Dir[DELAY_IMPORT_DESCRIPTOR].Size = DelayIdata.getDirSize();
809   }
810 
811   // Write section table
812   for (OutputSection *Sec : OutputSections) {
813     Sec->writeHeaderTo(Buf);
814     Buf += sizeof(coff_section);
815   }
816   SectionTable = ArrayRef<uint8_t>(
817       Buf - OutputSections.size() * sizeof(coff_section), Buf);
818 
819   if (OutputSymtab.empty() && Strtab.empty())
820     return;
821 
822   COFF->PointerToSymbolTable = PointerToSymbolTable;
823   uint32_t NumberOfSymbols = OutputSymtab.size();
824   COFF->NumberOfSymbols = NumberOfSymbols;
825   auto *SymbolTable = reinterpret_cast<coff_symbol16 *>(
826       Buffer->getBufferStart() + COFF->PointerToSymbolTable);
827   for (size_t I = 0; I != NumberOfSymbols; ++I)
828     SymbolTable[I] = OutputSymtab[I];
829   // Create the string table, it follows immediately after the symbol table.
830   // The first 4 bytes is length including itself.
831   Buf = reinterpret_cast<uint8_t *>(&SymbolTable[NumberOfSymbols]);
832   write32le(Buf, Strtab.size() + 4);
833   if (!Strtab.empty())
834     memcpy(Buf + 4, Strtab.data(), Strtab.size());
835 }
836 
837 void Writer::openFile(StringRef Path) {
838   Buffer = CHECK(
839       FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable),
840       "failed to open " + Path);
841 }
842 
843 void Writer::createSEHTable(OutputSection *RData) {
844   SymbolRVASet Handlers;
845   for (ObjFile *File : ObjFile::Instances) {
846     // FIXME: We should error here instead of earlier unless /safeseh:no was
847     // passed.
848     if (!File->hasSafeSEH())
849       return;
850 
851     markSymbolsForRVATable(File, File->getSXDataChunks(), Handlers);
852   }
853 
854   maybeAddRVATable(RData, std::move(Handlers), "__safe_se_handler_table",
855                    "__safe_se_handler_count");
856 }
857 
858 // Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set
859 // cannot contain duplicates. Therefore, the set is uniqued by Chunk and the
860 // symbol's offset into that Chunk.
861 static void addSymbolToRVASet(SymbolRVASet &RVASet, Defined *S) {
862   Chunk *C = S->getChunk();
863   if (auto *SC = dyn_cast<SectionChunk>(C))
864     C = SC->Repl; // Look through ICF replacement.
865   uint32_t Off = S->getRVA() - (C ? C->getRVA() : 0);
866   RVASet.insert({C, Off});
867 }
868 
869 // Visit all relocations from all section contributions of this object file and
870 // mark the relocation target as address-taken.
871 static void markSymbolsWithRelocations(ObjFile *File,
872                                        SymbolRVASet &UsedSymbols) {
873   for (Chunk *C : File->getChunks()) {
874     // We only care about live section chunks. Common chunks and other chunks
875     // don't generally contain relocations.
876     SectionChunk *SC = dyn_cast<SectionChunk>(C);
877     if (!SC || !SC->isLive())
878       continue;
879 
880     // Look for relocations in this section against symbols in executable output
881     // sections.
882     for (Symbol *Ref : SC->symbols()) {
883       // FIXME: Do further testing to see if the relocation type matters,
884       // especially for 32-bit where taking the address of something usually
885       // uses an absolute relocation instead of a relative one.
886       if (auto *D = dyn_cast_or_null<Defined>(Ref)) {
887         Chunk *RefChunk = D->getChunk();
888         OutputSection *OS = RefChunk ? RefChunk->getOutputSection() : nullptr;
889         if (OS && OS->getPermissions() & IMAGE_SCN_MEM_EXECUTE)
890           addSymbolToRVASet(UsedSymbols, D);
891       }
892     }
893   }
894 }
895 
896 // Create the guard function id table. This is a table of RVAs of all
897 // address-taken functions. It is sorted and uniqued, just like the safe SEH
898 // table.
899 void Writer::createGFIDTable(OutputSection *RData) {
900   SymbolRVASet AddressTakenSyms;
901   for (ObjFile *File : ObjFile::Instances) {
902     // If the object was compiled with /guard:cf, the address taken symbols are
903     // in the .gfids$y sections. Otherwise, we approximate the set of address
904     // taken symbols by checking which symbols were used by relocations in live
905     // sections.
906     if (File->hasGuardCF())
907       markSymbolsForRVATable(File, File->getGuardFidChunks(), AddressTakenSyms);
908     else
909       markSymbolsWithRelocations(File, AddressTakenSyms);
910   }
911 
912   // Mark the image entry as address-taken.
913   if (Config->Entry)
914     addSymbolToRVASet(AddressTakenSyms, cast<Defined>(Config->Entry));
915 
916   maybeAddRVATable(RData, std::move(AddressTakenSyms), "__guard_fids_table",
917                    "__guard_fids_count");
918 
919   // Set __guard_flags, which will be used in the load config to indicate that
920   // /guard:cf was enabled.
921   uint32_t GuardFlags = uint32_t(coff_guard_flags::CFInstrumented) |
922                         uint32_t(coff_guard_flags::HasFidTable);
923   Symbol *FlagSym = Symtab->findUnderscore("__guard_flags");
924   cast<DefinedAbsolute>(FlagSym)->setVA(GuardFlags);
925 }
926 
927 // Take a list of input sections containing symbol table indices and add those
928 // symbols to an RVA table. The challenge is that symbol RVAs are not known and
929 // depend on the table size, so we can't directly build a set of integers.
930 void Writer::markSymbolsForRVATable(ObjFile *File,
931                                     ArrayRef<SectionChunk *> SymIdxChunks,
932                                     SymbolRVASet &TableSymbols) {
933   for (SectionChunk *C : SymIdxChunks) {
934     // Skip sections discarded by linker GC. This comes up when a .gfids section
935     // is associated with something like a vtable and the vtable is discarded.
936     // In this case, the associated gfids section is discarded, and we don't
937     // mark the virtual member functions as address-taken by the vtable.
938     if (!C->isLive())
939       continue;
940 
941     // Validate that the contents look like symbol table indices.
942     ArrayRef<uint8_t> Data = C->getContents();
943     if (Data.size() % 4 != 0) {
944       warn("ignoring " + C->getSectionName() +
945            " symbol table index section in object " + toString(File));
946       continue;
947     }
948 
949     // Read each symbol table index and check if that symbol was included in the
950     // final link. If so, add it to the table symbol set.
951     ArrayRef<ulittle32_t> SymIndices(
952         reinterpret_cast<const ulittle32_t *>(Data.data()), Data.size() / 4);
953     ArrayRef<Symbol *> ObjSymbols = File->getSymbols();
954     for (uint32_t SymIndex : SymIndices) {
955       if (SymIndex >= ObjSymbols.size()) {
956         warn("ignoring invalid symbol table index in section " +
957              C->getSectionName() + " in object " + toString(File));
958         continue;
959       }
960       if (Symbol *S = ObjSymbols[SymIndex]) {
961         if (S->isLive())
962           addSymbolToRVASet(TableSymbols, cast<Defined>(S));
963       }
964     }
965   }
966 }
967 
968 // Replace the absolute table symbol with a synthetic symbol pointing to
969 // TableChunk so that we can emit base relocations for it and resolve section
970 // relative relocations.
971 void Writer::maybeAddRVATable(OutputSection *RData,
972                               SymbolRVASet TableSymbols,
973                               StringRef TableSym, StringRef CountSym) {
974   if (TableSymbols.empty())
975     return;
976 
977   RVATableChunk *TableChunk = make<RVATableChunk>(std::move(TableSymbols));
978   RData->addChunk(TableChunk);
979 
980   Symbol *T = Symtab->findUnderscore(TableSym);
981   Symbol *C = Symtab->findUnderscore(CountSym);
982   replaceSymbol<DefinedSynthetic>(T, T->getName(), TableChunk);
983   cast<DefinedAbsolute>(C)->setVA(TableChunk->getSize() / 4);
984 }
985 
986 // Handles /section options to allow users to overwrite
987 // section attributes.
988 void Writer::setSectionPermissions() {
989   for (auto &P : Config->Section) {
990     StringRef Name = P.first;
991     uint32_t Perm = P.second;
992     if (auto *Sec = findSection(Name))
993       Sec->setPermissions(Perm);
994   }
995 }
996 
997 // Write section contents to a mmap'ed file.
998 void Writer::writeSections() {
999   // Record the section index that should be used when resolving a section
1000   // relocation against an absolute symbol.
1001   DefinedAbsolute::OutputSectionIndex = OutputSections.size() + 1;
1002 
1003   uint8_t *Buf = Buffer->getBufferStart();
1004   for (OutputSection *Sec : OutputSections) {
1005     uint8_t *SecBuf = Buf + Sec->getFileOff();
1006     // Fill gaps between functions in .text with INT3 instructions
1007     // instead of leaving as NUL bytes (which can be interpreted as
1008     // ADD instructions).
1009     if (Sec->getPermissions() & IMAGE_SCN_CNT_CODE)
1010       memset(SecBuf, 0xCC, Sec->getRawSize());
1011     for_each(parallel::par, Sec->getChunks().begin(), Sec->getChunks().end(),
1012              [&](Chunk *C) { C->writeTo(SecBuf); });
1013   }
1014 }
1015 
1016 void Writer::writeBuildId() {
1017   // If we're not writing a build id (e.g. because /debug is not specified),
1018   // then just return;
1019   if (!Config->Debug)
1020     return;
1021 
1022   assert(BuildId && "BuildId is not set!");
1023 
1024   if (PreviousBuildId.hasValue()) {
1025     *BuildId->BuildId = *PreviousBuildId;
1026     BuildId->BuildId->PDB70.Age = BuildId->BuildId->PDB70.Age + 1;
1027     return;
1028   }
1029 
1030   BuildId->BuildId->Signature.CVSignature = OMF::Signature::PDB70;
1031   BuildId->BuildId->PDB70.Age = 1;
1032   llvm::getRandomBytes(BuildId->BuildId->PDB70.Signature, 16);
1033 }
1034 
1035 // Sort .pdata section contents according to PE/COFF spec 5.5.
1036 void Writer::sortExceptionTable() {
1037   OutputSection *Sec = findSection(".pdata");
1038   if (!Sec)
1039     return;
1040   // We assume .pdata contains function table entries only.
1041   uint8_t *Begin = Buffer->getBufferStart() + Sec->getFileOff();
1042   uint8_t *End = Begin + Sec->getVirtualSize();
1043   if (Config->Machine == AMD64) {
1044     struct Entry { ulittle32_t Begin, End, Unwind; };
1045     sort(parallel::par, (Entry *)Begin, (Entry *)End,
1046          [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
1047     return;
1048   }
1049   if (Config->Machine == ARMNT || Config->Machine == ARM64) {
1050     struct Entry { ulittle32_t Begin, Unwind; };
1051     sort(parallel::par, (Entry *)Begin, (Entry *)End,
1052          [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
1053     return;
1054   }
1055   errs() << "warning: don't know how to handle .pdata.\n";
1056 }
1057 
1058 OutputSection *Writer::findSection(StringRef Name) {
1059   for (OutputSection *Sec : OutputSections)
1060     if (Sec->getName() == Name)
1061       return Sec;
1062   return nullptr;
1063 }
1064 
1065 uint32_t Writer::getSizeOfInitializedData() {
1066   uint32_t Res = 0;
1067   for (OutputSection *S : OutputSections)
1068     if (S->getPermissions() & IMAGE_SCN_CNT_INITIALIZED_DATA)
1069       Res += S->getRawSize();
1070   return Res;
1071 }
1072 
1073 // Returns an existing section or create a new one if not found.
1074 OutputSection *Writer::createSection(StringRef Name) {
1075   if (auto *Sec = findSection(Name))
1076     return Sec;
1077   const auto DATA = IMAGE_SCN_CNT_INITIALIZED_DATA;
1078   const auto BSS = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
1079   const auto CODE = IMAGE_SCN_CNT_CODE;
1080   const auto DISCARDABLE = IMAGE_SCN_MEM_DISCARDABLE;
1081   const auto R = IMAGE_SCN_MEM_READ;
1082   const auto W = IMAGE_SCN_MEM_WRITE;
1083   const auto X = IMAGE_SCN_MEM_EXECUTE;
1084   uint32_t Perms = StringSwitch<uint32_t>(Name)
1085                        .Case(".bss", BSS | R | W)
1086                        .Case(".data", DATA | R | W)
1087                        .Cases(".didat", ".edata", ".idata", ".rdata", DATA | R)
1088                        .Case(".reloc", DATA | DISCARDABLE | R)
1089                        .Case(".text", CODE | R | X)
1090                        .Default(0);
1091   if (!Perms)
1092     llvm_unreachable("unknown section name");
1093   auto Sec = make<OutputSection>(Name);
1094   Sec->addPermissions(Perms);
1095   OutputSections.push_back(Sec);
1096   return Sec;
1097 }
1098 
1099 // Dest is .reloc section. Add contents to that section.
1100 void Writer::addBaserels(OutputSection *Dest) {
1101   std::vector<Baserel> V;
1102   for (OutputSection *Sec : OutputSections) {
1103     if (Sec == Dest)
1104       continue;
1105     // Collect all locations for base relocations.
1106     for (Chunk *C : Sec->getChunks())
1107       C->getBaserels(&V);
1108     // Add the addresses to .reloc section.
1109     if (!V.empty())
1110       addBaserelBlocks(Dest, V);
1111     V.clear();
1112   }
1113 }
1114 
1115 // Add addresses to .reloc section. Note that addresses are grouped by page.
1116 void Writer::addBaserelBlocks(OutputSection *Dest, std::vector<Baserel> &V) {
1117   const uint32_t Mask = ~uint32_t(PageSize - 1);
1118   uint32_t Page = V[0].RVA & Mask;
1119   size_t I = 0, J = 1;
1120   for (size_t E = V.size(); J < E; ++J) {
1121     uint32_t P = V[J].RVA & Mask;
1122     if (P == Page)
1123       continue;
1124     Dest->addChunk(make<BaserelChunk>(Page, &V[I], &V[0] + J));
1125     I = J;
1126     Page = P;
1127   }
1128   if (I == J)
1129     return;
1130   Dest->addChunk(make<BaserelChunk>(Page, &V[I], &V[0] + J));
1131 }
1132