xref: /llvm-project-15.0.7/lld/COFF/Writer.cpp (revision ccbe567f)
1 //===- Writer.cpp ---------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Writer.h"
10 #include "Config.h"
11 #include "DLL.h"
12 #include "InputFiles.h"
13 #include "MapFile.h"
14 #include "PDB.h"
15 #include "SymbolTable.h"
16 #include "Symbols.h"
17 #include "lld/Common/ErrorHandler.h"
18 #include "lld/Common/Memory.h"
19 #include "lld/Common/Threads.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 "llvm/Support/xxhash.h"
32 #include <algorithm>
33 #include <cstdio>
34 #include <map>
35 #include <memory>
36 #include <utility>
37 
38 using namespace llvm;
39 using namespace llvm::COFF;
40 using namespace llvm::object;
41 using namespace llvm::support;
42 using namespace llvm::support::endian;
43 using namespace lld;
44 using namespace lld::coff;
45 
46 /* To re-generate DOSProgram:
47 $ cat > /tmp/DOSProgram.asm
48 org 0
49         ; Copy cs to ds.
50         push cs
51         pop ds
52         ; Point ds:dx at the $-terminated string.
53         mov dx, str
54         ; Int 21/AH=09h: Write string to standard output.
55         mov ah, 0x9
56         int 0x21
57         ; Int 21/AH=4Ch: Exit with return code (in AL).
58         mov ax, 0x4C01
59         int 0x21
60 str:
61         db 'This program cannot be run in DOS mode.$'
62 align 8, db 0
63 $ nasm -fbin /tmp/DOSProgram.asm -o /tmp/DOSProgram.bin
64 $ xxd -i /tmp/DOSProgram.bin
65 */
66 static unsigned char DOSProgram[] = {
67   0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8, 0x01, 0x4c,
68   0xcd, 0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72,
69   0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x62, 0x65,
70   0x20, 0x72, 0x75, 0x6e, 0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20,
71   0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x24, 0x00, 0x00
72 };
73 static_assert(sizeof(DOSProgram) % 8 == 0,
74               "DOSProgram size must be multiple of 8");
75 
76 static const int SectorSize = 512;
77 static const int DOSStubSize = sizeof(dos_header) + sizeof(DOSProgram);
78 static_assert(DOSStubSize % 8 == 0, "DOSStub size must be multiple of 8");
79 
80 static const int NumberOfDataDirectory = 16;
81 
82 namespace {
83 
84 class DebugDirectoryChunk : public Chunk {
85 public:
86   DebugDirectoryChunk(const std::vector<Chunk *> &R, bool WriteRepro)
87       : Records(R), WriteRepro(WriteRepro) {}
88 
89   size_t getSize() const override {
90     return (Records.size() + int(WriteRepro)) * sizeof(debug_directory);
91   }
92 
93   void writeTo(uint8_t *B) const override {
94     auto *D = reinterpret_cast<debug_directory *>(B + OutputSectionOff);
95 
96     for (const Chunk *Record : Records) {
97       OutputSection *OS = Record->getOutputSection();
98       uint64_t Offs = OS->getFileOff() + (Record->getRVA() - OS->getRVA());
99       fillEntry(D, COFF::IMAGE_DEBUG_TYPE_CODEVIEW, Record->getSize(),
100                 Record->getRVA(), Offs);
101       ++D;
102     }
103 
104     if (WriteRepro) {
105       // FIXME: The COFF spec allows either a 0-sized entry to just say
106       // "the timestamp field is really a hash", or a 4-byte size field
107       // followed by that many bytes containing a longer hash (with the
108       // lowest 4 bytes usually being the timestamp in little-endian order).
109       // Consider storing the full 8 bytes computed by xxHash64 here.
110       fillEntry(D, COFF::IMAGE_DEBUG_TYPE_REPRO, 0, 0, 0);
111     }
112   }
113 
114   void setTimeDateStamp(uint32_t TimeDateStamp) {
115     for (support::ulittle32_t *TDS : TimeDateStamps)
116       *TDS = TimeDateStamp;
117   }
118 
119 private:
120   void fillEntry(debug_directory *D, COFF::DebugType DebugType, size_t Size,
121                  uint64_t RVA, uint64_t Offs) const {
122     D->Characteristics = 0;
123     D->TimeDateStamp = 0;
124     D->MajorVersion = 0;
125     D->MinorVersion = 0;
126     D->Type = DebugType;
127     D->SizeOfData = Size;
128     D->AddressOfRawData = RVA;
129     D->PointerToRawData = Offs;
130 
131     TimeDateStamps.push_back(&D->TimeDateStamp);
132   }
133 
134   mutable std::vector<support::ulittle32_t *> TimeDateStamps;
135   const std::vector<Chunk *> &Records;
136   bool WriteRepro;
137 };
138 
139 class CVDebugRecordChunk : public Chunk {
140 public:
141   size_t getSize() const override {
142     return sizeof(codeview::DebugInfo) + Config->PDBAltPath.size() + 1;
143   }
144 
145   void writeTo(uint8_t *B) const override {
146     // Save off the DebugInfo entry to backfill the file signature (build id)
147     // in Writer::writeBuildId
148     BuildId = reinterpret_cast<codeview::DebugInfo *>(B + OutputSectionOff);
149 
150     // variable sized field (PDB Path)
151     char *P = reinterpret_cast<char *>(B + OutputSectionOff + sizeof(*BuildId));
152     if (!Config->PDBAltPath.empty())
153       memcpy(P, Config->PDBAltPath.data(), Config->PDBAltPath.size());
154     P[Config->PDBAltPath.size()] = '\0';
155   }
156 
157   mutable codeview::DebugInfo *BuildId = nullptr;
158 };
159 
160 // PartialSection represents a group of chunks that contribute to an
161 // OutputSection. Collating a collection of PartialSections of same name and
162 // characteristics constitutes the OutputSection.
163 class PartialSectionKey {
164 public:
165   StringRef Name;
166   unsigned Characteristics;
167 
168   bool operator<(const PartialSectionKey &Other) const {
169     int C = Name.compare(Other.Name);
170     if (C == 1)
171       return false;
172     if (C == 0)
173       return Characteristics < Other.Characteristics;
174     return true;
175   }
176 };
177 
178 // The writer writes a SymbolTable result to a file.
179 class Writer {
180 public:
181   Writer() : Buffer(errorHandler().OutputBuffer) {}
182   void run();
183 
184 private:
185   void createSections();
186   void createMiscChunks();
187   void createImportTables();
188   void appendImportThunks();
189   void locateImportTables();
190   void createExportTable();
191   void mergeSections();
192   void removeUnusedSections();
193   void assignAddresses();
194   void finalizeAddresses();
195   void removeEmptySections();
196   void createSymbolAndStringTable();
197   void openFile(StringRef OutputPath);
198   template <typename PEHeaderTy> void writeHeader();
199   void createSEHTable();
200   void createRuntimePseudoRelocs();
201   void insertCtorDtorSymbols();
202   void createGuardCFTables();
203   void markSymbolsForRVATable(ObjFile *File,
204                               ArrayRef<SectionChunk *> SymIdxChunks,
205                               SymbolRVASet &TableSymbols);
206   void maybeAddRVATable(SymbolRVASet TableSymbols, StringRef TableSym,
207                         StringRef CountSym);
208   void setSectionPermissions();
209   void writeSections();
210   void writeBuildId();
211   void sortExceptionTable();
212   void sortCRTSectionChunks(std::vector<Chunk *> &Chunks);
213   void addSyntheticIdata();
214   bool fixGnuImportChunks();
215   PartialSection *createPartialSection(StringRef Name, uint32_t OutChars);
216   PartialSection *findPartialSection(StringRef Name, uint32_t OutChars);
217 
218   llvm::Optional<coff_symbol16> createSymbol(Defined *D);
219   size_t addEntryToStringTable(StringRef Str);
220 
221   OutputSection *findSection(StringRef Name);
222   void addBaserels();
223   void addBaserelBlocks(std::vector<Baserel> &V);
224 
225   uint32_t getSizeOfInitializedData();
226 
227   std::unique_ptr<FileOutputBuffer> &Buffer;
228   std::map<PartialSectionKey, PartialSection *> PartialSections;
229   std::vector<OutputSection *> OutputSections;
230   std::vector<char> Strtab;
231   std::vector<llvm::object::coff_symbol16> OutputSymtab;
232   IdataContents Idata;
233   Chunk *ImportTableStart = nullptr;
234   uint64_t ImportTableSize = 0;
235   Chunk *IATStart = nullptr;
236   uint64_t IATSize = 0;
237   DelayLoadContents DelayIdata;
238   EdataContents Edata;
239   bool SetNoSEHCharacteristic = false;
240 
241   DebugDirectoryChunk *DebugDirectory = nullptr;
242   std::vector<Chunk *> DebugRecords;
243   CVDebugRecordChunk *BuildId = nullptr;
244   ArrayRef<uint8_t> SectionTable;
245 
246   uint64_t FileSize;
247   uint32_t PointerToSymbolTable = 0;
248   uint64_t SizeOfImage;
249   uint64_t SizeOfHeaders;
250 
251   OutputSection *TextSec;
252   OutputSection *RdataSec;
253   OutputSection *BuildidSec;
254   OutputSection *DataSec;
255   OutputSection *PdataSec;
256   OutputSection *IdataSec;
257   OutputSection *EdataSec;
258   OutputSection *DidatSec;
259   OutputSection *RsrcSec;
260   OutputSection *RelocSec;
261   OutputSection *CtorsSec;
262   OutputSection *DtorsSec;
263 
264   // The first and last .pdata sections in the output file.
265   //
266   // We need to keep track of the location of .pdata in whichever section it
267   // gets merged into so that we can sort its contents and emit a correct data
268   // directory entry for the exception table. This is also the case for some
269   // other sections (such as .edata) but because the contents of those sections
270   // are entirely linker-generated we can keep track of their locations using
271   // the chunks that the linker creates. All .pdata chunks come from input
272   // files, so we need to keep track of them separately.
273   Chunk *FirstPdata = nullptr;
274   Chunk *LastPdata;
275 };
276 } // anonymous namespace
277 
278 namespace lld {
279 namespace coff {
280 
281 static Timer CodeLayoutTimer("Code Layout", Timer::root());
282 static Timer DiskCommitTimer("Commit Output File", Timer::root());
283 
284 void writeResult() { Writer().run(); }
285 
286 void OutputSection::addChunk(Chunk *C) {
287   Chunks.push_back(C);
288   C->setOutputSection(this);
289 }
290 
291 void OutputSection::insertChunkAtStart(Chunk *C) {
292   Chunks.insert(Chunks.begin(), C);
293   C->setOutputSection(this);
294 }
295 
296 void OutputSection::setPermissions(uint32_t C) {
297   Header.Characteristics &= ~PermMask;
298   Header.Characteristics |= C;
299 }
300 
301 void OutputSection::merge(OutputSection *Other) {
302   for (Chunk *C : Other->Chunks)
303     C->setOutputSection(this);
304   Chunks.insert(Chunks.end(), Other->Chunks.begin(), Other->Chunks.end());
305   Other->Chunks.clear();
306   ContribSections.insert(ContribSections.end(), Other->ContribSections.begin(),
307                          Other->ContribSections.end());
308   Other->ContribSections.clear();
309 }
310 
311 // Write the section header to a given buffer.
312 void OutputSection::writeHeaderTo(uint8_t *Buf) {
313   auto *Hdr = reinterpret_cast<coff_section *>(Buf);
314   *Hdr = Header;
315   if (StringTableOff) {
316     // If name is too long, write offset into the string table as a name.
317     sprintf(Hdr->Name, "/%d", StringTableOff);
318   } else {
319     assert(!Config->Debug || Name.size() <= COFF::NameSize ||
320            (Hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0);
321     strncpy(Hdr->Name, Name.data(),
322             std::min(Name.size(), (size_t)COFF::NameSize));
323   }
324 }
325 
326 void OutputSection::addContributingPartialSection(PartialSection *Sec) {
327   ContribSections.push_back(Sec);
328 }
329 
330 } // namespace coff
331 } // namespace lld
332 
333 // Check whether the target address S is in range from a relocation
334 // of type RelType at address P.
335 static bool isInRange(uint16_t RelType, uint64_t S, uint64_t P, int Margin) {
336   if (Config->Machine == ARMNT) {
337     int64_t Diff = AbsoluteDifference(S, P + 4) + Margin;
338     switch (RelType) {
339     case IMAGE_REL_ARM_BRANCH20T:
340       return isInt<21>(Diff);
341     case IMAGE_REL_ARM_BRANCH24T:
342     case IMAGE_REL_ARM_BLX23T:
343       return isInt<25>(Diff);
344     default:
345       return true;
346     }
347   } else if (Config->Machine == ARM64) {
348     int64_t Diff = AbsoluteDifference(S, P) + Margin;
349     switch (RelType) {
350     case IMAGE_REL_ARM64_BRANCH26:
351       return isInt<28>(Diff);
352     case IMAGE_REL_ARM64_BRANCH19:
353       return isInt<21>(Diff);
354     case IMAGE_REL_ARM64_BRANCH14:
355       return isInt<16>(Diff);
356     default:
357       return true;
358     }
359   } else {
360     llvm_unreachable("Unexpected architecture");
361   }
362 }
363 
364 // Return the last thunk for the given target if it is in range,
365 // or create a new one.
366 static std::pair<Defined *, bool>
367 getThunk(DenseMap<uint64_t, Defined *> &LastThunks, Defined *Target, uint64_t P,
368          uint16_t Type, int Margin) {
369   Defined *&LastThunk = LastThunks[Target->getRVA()];
370   if (LastThunk && isInRange(Type, LastThunk->getRVA(), P, Margin))
371     return {LastThunk, false};
372   Chunk *C;
373   switch (Config->Machine) {
374   case ARMNT:
375     C = make<RangeExtensionThunkARM>(Target);
376     break;
377   case ARM64:
378     C = make<RangeExtensionThunkARM64>(Target);
379     break;
380   default:
381     llvm_unreachable("Unexpected architecture");
382   }
383   Defined *D = make<DefinedSynthetic>("", C);
384   LastThunk = D;
385   return {D, true};
386 }
387 
388 // This checks all relocations, and for any relocation which isn't in range
389 // it adds a thunk after the section chunk that contains the relocation.
390 // If the latest thunk for the specific target is in range, that is used
391 // instead of creating a new thunk. All range checks are done with the
392 // specified margin, to make sure that relocations that originally are in
393 // range, but only barely, also get thunks - in case other added thunks makes
394 // the target go out of range.
395 //
396 // After adding thunks, we verify that all relocations are in range (with
397 // no extra margin requirements). If this failed, we restart (throwing away
398 // the previously created thunks) and retry with a wider margin.
399 static bool createThunks(OutputSection *OS, int Margin) {
400   bool AddressesChanged = false;
401   DenseMap<uint64_t, Defined *> LastThunks;
402   DenseMap<std::pair<ObjFile *, Defined *>, uint32_t> ThunkSymtabIndices;
403   size_t ThunksSize = 0;
404   // Recheck Chunks.size() each iteration, since we can insert more
405   // elements into it.
406   for (size_t I = 0; I != OS->Chunks.size(); ++I) {
407     SectionChunk *SC = dyn_cast_or_null<SectionChunk>(OS->Chunks[I]);
408     if (!SC)
409       continue;
410     size_t ThunkInsertionSpot = I + 1;
411 
412     // Try to get a good enough estimate of where new thunks will be placed.
413     // Offset this by the size of the new thunks added so far, to make the
414     // estimate slightly better.
415     size_t ThunkInsertionRVA = SC->getRVA() + SC->getSize() + ThunksSize;
416     ObjFile *File = SC->File;
417     std::vector<std::pair<uint32_t, uint32_t>> RelocReplacements;
418     ArrayRef<coff_relocation> OriginalRelocs =
419         File->getCOFFObj()->getRelocations(SC->Header);
420     for (size_t J = 0, E = OriginalRelocs.size(); J < E; ++J) {
421       const coff_relocation &Rel = OriginalRelocs[J];
422       Symbol *RelocTarget = File->getSymbol(Rel.SymbolTableIndex);
423 
424       // The estimate of the source address P should be pretty accurate,
425       // but we don't know whether the target Symbol address should be
426       // offset by ThunkSize or not (or by some of ThunksSize but not all of
427       // it), giving us some uncertainty once we have added one thunk.
428       uint64_t P = SC->getRVA() + Rel.VirtualAddress + ThunksSize;
429 
430       Defined *Sym = dyn_cast_or_null<Defined>(RelocTarget);
431       if (!Sym)
432         continue;
433 
434       uint64_t S = Sym->getRVA();
435 
436       if (isInRange(Rel.Type, S, P, Margin))
437         continue;
438 
439       // If the target isn't in range, hook it up to an existing or new
440       // thunk.
441       Defined *Thunk;
442       bool WasNew;
443       std::tie(Thunk, WasNew) = getThunk(LastThunks, Sym, P, Rel.Type, Margin);
444       if (WasNew) {
445         Chunk *ThunkChunk = Thunk->getChunk();
446         ThunkChunk->setRVA(
447             ThunkInsertionRVA); // Estimate of where it will be located.
448         ThunkChunk->setOutputSection(OS);
449         OS->Chunks.insert(OS->Chunks.begin() + ThunkInsertionSpot, ThunkChunk);
450         ThunkInsertionSpot++;
451         ThunksSize += ThunkChunk->getSize();
452         ThunkInsertionRVA += ThunkChunk->getSize();
453         AddressesChanged = true;
454       }
455 
456       // To redirect the relocation, add a symbol to the parent object file's
457       // symbol table, and replace the relocation symbol table index with the
458       // new index.
459       auto Insertion = ThunkSymtabIndices.insert({{File, Thunk}, ~0U});
460       uint32_t &ThunkSymbolIndex = Insertion.first->second;
461       if (Insertion.second)
462         ThunkSymbolIndex = File->addRangeThunkSymbol(Thunk);
463       RelocReplacements.push_back({J, ThunkSymbolIndex});
464     }
465 
466     // Get a writable copy of this section's relocations so they can be
467     // modified. If the relocations point into the object file, allocate new
468     // memory. Otherwise, this must be previously allocated memory that can be
469     // modified in place.
470     MutableArrayRef<coff_relocation> NewRelocs;
471     if (OriginalRelocs.data() == SC->Relocs.data()) {
472       NewRelocs = makeMutableArrayRef(
473           BAlloc.Allocate<coff_relocation>(OriginalRelocs.size()),
474           OriginalRelocs.size());
475     } else {
476       NewRelocs = makeMutableArrayRef(
477           const_cast<coff_relocation *>(SC->Relocs.data()), SC->Relocs.size());
478     }
479 
480     // Copy each relocation, but replace the symbol table indices which need
481     // thunks.
482     auto NextReplacement = RelocReplacements.begin();
483     auto EndReplacement = RelocReplacements.end();
484     for (size_t I = 0, E = OriginalRelocs.size(); I != E; ++I) {
485       NewRelocs[I] = OriginalRelocs[I];
486       if (NextReplacement != EndReplacement && NextReplacement->first == I) {
487         NewRelocs[I].SymbolTableIndex = NextReplacement->second;
488         ++NextReplacement;
489       }
490     }
491 
492     SC->Relocs = makeArrayRef(NewRelocs.data(), NewRelocs.size());
493   }
494   return AddressesChanged;
495 }
496 
497 // Verify that all relocations are in range, with no extra margin requirements.
498 static bool verifyRanges(const std::vector<Chunk *> Chunks) {
499   for (Chunk *C : Chunks) {
500     SectionChunk *SC = dyn_cast_or_null<SectionChunk>(C);
501     if (!SC)
502       continue;
503 
504     for (size_t J = 0, E = SC->Relocs.size(); J < E; ++J) {
505       const coff_relocation &Rel = SC->Relocs[J];
506       Symbol *RelocTarget = SC->File->getSymbol(Rel.SymbolTableIndex);
507 
508       Defined *Sym = dyn_cast_or_null<Defined>(RelocTarget);
509       if (!Sym)
510         continue;
511 
512       uint64_t P = SC->getRVA() + Rel.VirtualAddress;
513       uint64_t S = Sym->getRVA();
514 
515       if (!isInRange(Rel.Type, S, P, 0))
516         return false;
517     }
518   }
519   return true;
520 }
521 
522 // Assign addresses and add thunks if necessary.
523 void Writer::finalizeAddresses() {
524   assignAddresses();
525   if (Config->Machine != ARMNT && Config->Machine != ARM64)
526     return;
527 
528   size_t OrigNumChunks = 0;
529   for (OutputSection *Sec : OutputSections) {
530     Sec->OrigChunks = Sec->Chunks;
531     OrigNumChunks += Sec->Chunks.size();
532   }
533 
534   int Pass = 0;
535   int Margin = 1024 * 100;
536   while (true) {
537     // First check whether we need thunks at all, or if the previous pass of
538     // adding them turned out ok.
539     bool RangesOk = true;
540     size_t NumChunks = 0;
541     for (OutputSection *Sec : OutputSections) {
542       if (!verifyRanges(Sec->Chunks)) {
543         RangesOk = false;
544         break;
545       }
546       NumChunks += Sec->Chunks.size();
547     }
548     if (RangesOk) {
549       if (Pass > 0)
550         log("Added " + Twine(NumChunks - OrigNumChunks) + " thunks with " +
551             "margin " + Twine(Margin) + " in " + Twine(Pass) + " passes");
552       return;
553     }
554 
555     if (Pass >= 10)
556       fatal("adding thunks hasn't converged after " + Twine(Pass) + " passes");
557 
558     if (Pass > 0) {
559       // If the previous pass didn't work out, reset everything back to the
560       // original conditions before retrying with a wider margin. This should
561       // ideally never happen under real circumstances.
562       for (OutputSection *Sec : OutputSections)
563         Sec->Chunks = Sec->OrigChunks;
564       Margin *= 2;
565     }
566 
567     // Try adding thunks everywhere where it is needed, with a margin
568     // to avoid things going out of range due to the added thunks.
569     bool AddressesChanged = false;
570     for (OutputSection *Sec : OutputSections)
571       AddressesChanged |= createThunks(Sec, Margin);
572     // If the verification above thought we needed thunks, we should have
573     // added some.
574     assert(AddressesChanged);
575 
576     // Recalculate the layout for the whole image (and verify the ranges at
577     // the start of the next round).
578     assignAddresses();
579 
580     Pass++;
581   }
582 }
583 
584 // The main function of the writer.
585 void Writer::run() {
586   ScopedTimer T1(CodeLayoutTimer);
587 
588   createImportTables();
589   createSections();
590   createMiscChunks();
591   appendImportThunks();
592   createExportTable();
593   mergeSections();
594   removeUnusedSections();
595   finalizeAddresses();
596   removeEmptySections();
597   setSectionPermissions();
598   createSymbolAndStringTable();
599 
600   if (FileSize > UINT32_MAX)
601     fatal("image size (" + Twine(FileSize) + ") " +
602         "exceeds maximum allowable size (" + Twine(UINT32_MAX) + ")");
603 
604   openFile(Config->OutputFile);
605   if (Config->is64()) {
606     writeHeader<pe32plus_header>();
607   } else {
608     writeHeader<pe32_header>();
609   }
610   writeSections();
611   sortExceptionTable();
612 
613   T1.stop();
614 
615   if (!Config->PDBPath.empty() && Config->Debug) {
616     assert(BuildId);
617     createPDB(Symtab, OutputSections, SectionTable, BuildId->BuildId);
618   }
619   writeBuildId();
620 
621   writeMapFile(OutputSections);
622 
623   ScopedTimer T2(DiskCommitTimer);
624   if (auto E = Buffer->commit())
625     fatal("failed to write the output file: " + toString(std::move(E)));
626 }
627 
628 static StringRef getOutputSectionName(StringRef Name) {
629   StringRef S = Name.split('$').first;
630 
631   // Treat a later period as a separator for MinGW, for sections like
632   // ".ctors.01234".
633   return S.substr(0, S.find('.', 1));
634 }
635 
636 // For /order.
637 static void sortBySectionOrder(std::vector<Chunk *> &Chunks) {
638   auto GetPriority = [](const Chunk *C) {
639     if (auto *Sec = dyn_cast<SectionChunk>(C))
640       if (Sec->Sym)
641         return Config->Order.lookup(Sec->Sym->getName());
642     return 0;
643   };
644 
645   llvm::stable_sort(Chunks, [=](const Chunk *A, const Chunk *B) {
646     return GetPriority(A) < GetPriority(B);
647   });
648 }
649 
650 // Sort concrete section chunks from GNU import libraries.
651 //
652 // GNU binutils doesn't use short import files, but instead produces import
653 // libraries that consist of object files, with section chunks for the .idata$*
654 // sections. These are linked just as regular static libraries. Each import
655 // library consists of one header object, one object file for every imported
656 // symbol, and one trailer object. In order for the .idata tables/lists to
657 // be formed correctly, the section chunks within each .idata$* section need
658 // to be grouped by library, and sorted alphabetically within each library
659 // (which makes sure the header comes first and the trailer last).
660 bool Writer::fixGnuImportChunks() {
661   uint32_t RDATA = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
662 
663   // Make sure all .idata$* section chunks are mapped as RDATA in order to
664   // be sorted into the same sections as our own synthesized .idata chunks.
665   for (auto It : PartialSections) {
666     PartialSection *PSec = It.second;
667     if (!PSec->Name.startswith(".idata"))
668       continue;
669     if (PSec->Characteristics == RDATA)
670       continue;
671     PartialSection *RDataSec = createPartialSection(PSec->Name, RDATA);
672     RDataSec->Chunks.insert(RDataSec->Chunks.end(), PSec->Chunks.begin(),
673                             PSec->Chunks.end());
674     PSec->Chunks.clear();
675   }
676 
677   bool HasIdata = false;
678   // Sort all .idata$* chunks, grouping chunks from the same library,
679   // with alphabetical ordering of the object fils within a library.
680   for (auto It : PartialSections) {
681     PartialSection *PSec = It.second;
682     if (!PSec->Name.startswith(".idata"))
683       continue;
684 
685     if (!PSec->Chunks.empty())
686       HasIdata = true;
687     llvm::stable_sort(PSec->Chunks, [&](Chunk *S, Chunk *T) {
688       SectionChunk *SC1 = dyn_cast_or_null<SectionChunk>(S);
689       SectionChunk *SC2 = dyn_cast_or_null<SectionChunk>(T);
690       if (!SC1 || !SC2) {
691         // if SC1, order them ascending. If SC2 or both null,
692         // S is not less than T.
693         return SC1 != nullptr;
694       }
695       // Make a string with "libraryname/objectfile" for sorting, achieving
696       // both grouping by library and sorting of objects within a library,
697       // at once.
698       std::string Key1 =
699           (SC1->File->ParentName + "/" + SC1->File->getName()).str();
700       std::string Key2 =
701           (SC2->File->ParentName + "/" + SC2->File->getName()).str();
702       return Key1 < Key2;
703     });
704   }
705   return HasIdata;
706 }
707 
708 // Add generated idata chunks, for imported symbols and DLLs, and a
709 // terminator in .idata$2.
710 void Writer::addSyntheticIdata() {
711   uint32_t RDATA = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
712   Idata.create();
713 
714   // Add the .idata content in the right section groups, to allow
715   // chunks from other linked in object files to be grouped together.
716   // See Microsoft PE/COFF spec 5.4 for details.
717   auto Add = [&](StringRef N, std::vector<Chunk *> &V) {
718     PartialSection *PSec = createPartialSection(N, RDATA);
719     PSec->Chunks.insert(PSec->Chunks.end(), V.begin(), V.end());
720   };
721 
722   // The loader assumes a specific order of data.
723   // Add each type in the correct order.
724   Add(".idata$2", Idata.Dirs);
725   Add(".idata$4", Idata.Lookups);
726   Add(".idata$5", Idata.Addresses);
727   Add(".idata$6", Idata.Hints);
728   Add(".idata$7", Idata.DLLNames);
729 }
730 
731 // Locate the first Chunk and size of the import directory list and the
732 // IAT.
733 void Writer::locateImportTables() {
734   uint32_t RDATA = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
735 
736   if (PartialSection *ImportDirs = findPartialSection(".idata$2", RDATA)) {
737     if (!ImportDirs->Chunks.empty())
738       ImportTableStart = ImportDirs->Chunks.front();
739     for (Chunk *C : ImportDirs->Chunks)
740       ImportTableSize += C->getSize();
741   }
742 
743   if (PartialSection *ImportAddresses = findPartialSection(".idata$5", RDATA)) {
744     if (!ImportAddresses->Chunks.empty())
745       IATStart = ImportAddresses->Chunks.front();
746     for (Chunk *C : ImportAddresses->Chunks)
747       IATSize += C->getSize();
748   }
749 }
750 
751 // Create output section objects and add them to OutputSections.
752 void Writer::createSections() {
753   // First, create the builtin sections.
754   const uint32_t DATA = IMAGE_SCN_CNT_INITIALIZED_DATA;
755   const uint32_t BSS = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
756   const uint32_t CODE = IMAGE_SCN_CNT_CODE;
757   const uint32_t DISCARDABLE = IMAGE_SCN_MEM_DISCARDABLE;
758   const uint32_t R = IMAGE_SCN_MEM_READ;
759   const uint32_t W = IMAGE_SCN_MEM_WRITE;
760   const uint32_t X = IMAGE_SCN_MEM_EXECUTE;
761 
762   SmallDenseMap<std::pair<StringRef, uint32_t>, OutputSection *> Sections;
763   auto CreateSection = [&](StringRef Name, uint32_t OutChars) {
764     OutputSection *&Sec = Sections[{Name, OutChars}];
765     if (!Sec) {
766       Sec = make<OutputSection>(Name, OutChars);
767       OutputSections.push_back(Sec);
768     }
769     return Sec;
770   };
771 
772   // Try to match the section order used by link.exe.
773   TextSec = CreateSection(".text", CODE | R | X);
774   CreateSection(".bss", BSS | R | W);
775   RdataSec = CreateSection(".rdata", DATA | R);
776   BuildidSec = CreateSection(".buildid", DATA | R);
777   DataSec = CreateSection(".data", DATA | R | W);
778   PdataSec = CreateSection(".pdata", DATA | R);
779   IdataSec = CreateSection(".idata", DATA | R);
780   EdataSec = CreateSection(".edata", DATA | R);
781   DidatSec = CreateSection(".didat", DATA | R);
782   RsrcSec = CreateSection(".rsrc", DATA | R);
783   RelocSec = CreateSection(".reloc", DATA | DISCARDABLE | R);
784   CtorsSec = CreateSection(".ctors", DATA | R | W);
785   DtorsSec = CreateSection(".dtors", DATA | R | W);
786 
787   // Then bin chunks by name and output characteristics.
788   for (Chunk *C : Symtab->getChunks()) {
789     auto *SC = dyn_cast<SectionChunk>(C);
790     if (SC && !SC->Live) {
791       if (Config->Verbose)
792         SC->printDiscardedMessage();
793       continue;
794     }
795     PartialSection *PSec = createPartialSection(C->getSectionName(),
796                                                 C->getOutputCharacteristics());
797     PSec->Chunks.push_back(C);
798   }
799 
800   // Even in non MinGW cases, we might need to link against GNU import
801   // libraries.
802   bool HasIdata = fixGnuImportChunks();
803   if (!Idata.empty())
804     HasIdata = true;
805 
806   if (HasIdata)
807     addSyntheticIdata();
808 
809   // Process an /order option.
810   if (!Config->Order.empty())
811     for (auto It : PartialSections)
812       sortBySectionOrder(It.second->Chunks);
813 
814   if (HasIdata)
815     locateImportTables();
816 
817   // Then create an OutputSection for each section.
818   // '$' and all following characters in input section names are
819   // discarded when determining output section. So, .text$foo
820   // contributes to .text, for example. See PE/COFF spec 3.2.
821   for (auto It : PartialSections) {
822     PartialSection *PSec = It.second;
823     StringRef Name = getOutputSectionName(PSec->Name);
824     uint32_t OutChars = PSec->Characteristics;
825 
826     if (Name == ".CRT") {
827       // In link.exe, there is a special case for the I386 target where .CRT
828       // sections are treated as if they have output characteristics DATA | R if
829       // their characteristics are DATA | R | W. This implements the same
830       // special case for all architectures.
831       OutChars = DATA | R;
832 
833       log("Processing section " + PSec->Name + " -> " + Name);
834 
835       sortCRTSectionChunks(PSec->Chunks);
836     }
837 
838     OutputSection *Sec = CreateSection(Name, OutChars);
839     for (Chunk *C : PSec->Chunks)
840       Sec->addChunk(C);
841 
842     Sec->addContributingPartialSection(PSec);
843   }
844 
845   // Finally, move some output sections to the end.
846   auto SectionOrder = [&](const OutputSection *S) {
847     // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because
848     // the loader cannot handle holes. Stripping can remove other discardable ones
849     // than .reloc, which is first of them (created early).
850     if (S->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
851       return 2;
852     // .rsrc should come at the end of the non-discardable sections because its
853     // size may change by the Win32 UpdateResources() function, causing
854     // subsequent sections to move (see https://crbug.com/827082).
855     if (S == RsrcSec)
856       return 1;
857     return 0;
858   };
859   llvm::stable_sort(OutputSections,
860                     [&](const OutputSection *S, const OutputSection *T) {
861                       return SectionOrder(S) < SectionOrder(T);
862                     });
863 }
864 
865 void Writer::createMiscChunks() {
866   for (auto &P : MergeChunk::Instances)
867     RdataSec->addChunk(P.second);
868 
869   // Create thunks for locally-dllimported symbols.
870   if (!Symtab->LocalImportChunks.empty()) {
871     for (Chunk *C : Symtab->LocalImportChunks)
872       RdataSec->addChunk(C);
873   }
874 
875   // Create Debug Information Chunks
876   OutputSection *DebugInfoSec = Config->MinGW ? BuildidSec : RdataSec;
877   if (Config->Debug || Config->Repro) {
878     DebugDirectory = make<DebugDirectoryChunk>(DebugRecords, Config->Repro);
879     DebugInfoSec->addChunk(DebugDirectory);
880   }
881 
882   if (Config->Debug) {
883     // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified.  We
884     // output a PDB no matter what, and this chunk provides the only means of
885     // allowing a debugger to match a PDB and an executable.  So we need it even
886     // if we're ultimately not going to write CodeView data to the PDB.
887     BuildId = make<CVDebugRecordChunk>();
888     DebugRecords.push_back(BuildId);
889 
890     for (Chunk *C : DebugRecords)
891       DebugInfoSec->addChunk(C);
892   }
893 
894   // Create SEH table. x86-only.
895   if (Config->Machine == I386)
896     createSEHTable();
897 
898   // Create /guard:cf tables if requested.
899   if (Config->GuardCF != GuardCFLevel::Off)
900     createGuardCFTables();
901 
902   if (Config->MinGW) {
903     createRuntimePseudoRelocs();
904 
905     insertCtorDtorSymbols();
906   }
907 }
908 
909 // Create .idata section for the DLL-imported symbol table.
910 // The format of this section is inherently Windows-specific.
911 // IdataContents class abstracted away the details for us,
912 // so we just let it create chunks and add them to the section.
913 void Writer::createImportTables() {
914   // Initialize DLLOrder so that import entries are ordered in
915   // the same order as in the command line. (That affects DLL
916   // initialization order, and this ordering is MSVC-compatible.)
917   for (ImportFile *File : ImportFile::Instances) {
918     if (!File->Live)
919       continue;
920 
921     std::string DLL = StringRef(File->DLLName).lower();
922     if (Config->DLLOrder.count(DLL) == 0)
923       Config->DLLOrder[DLL] = Config->DLLOrder.size();
924 
925     if (File->ImpSym && !isa<DefinedImportData>(File->ImpSym))
926       fatal(toString(*File->ImpSym) + " was replaced");
927     DefinedImportData *ImpSym = cast_or_null<DefinedImportData>(File->ImpSym);
928     if (Config->DelayLoads.count(StringRef(File->DLLName).lower())) {
929       if (!File->ThunkSym)
930         fatal("cannot delay-load " + toString(File) +
931               " due to import of data: " + toString(*ImpSym));
932       DelayIdata.add(ImpSym);
933     } else {
934       Idata.add(ImpSym);
935     }
936   }
937 }
938 
939 void Writer::appendImportThunks() {
940   if (ImportFile::Instances.empty())
941     return;
942 
943   for (ImportFile *File : ImportFile::Instances) {
944     if (!File->Live)
945       continue;
946 
947     if (!File->ThunkSym)
948       continue;
949 
950     if (!isa<DefinedImportThunk>(File->ThunkSym))
951       fatal(toString(*File->ThunkSym) + " was replaced");
952     DefinedImportThunk *Thunk = cast<DefinedImportThunk>(File->ThunkSym);
953     if (File->ThunkLive)
954       TextSec->addChunk(Thunk->getChunk());
955   }
956 
957   if (!DelayIdata.empty()) {
958     Defined *Helper = cast<Defined>(Config->DelayLoadHelper);
959     DelayIdata.create(Helper);
960     for (Chunk *C : DelayIdata.getChunks())
961       DidatSec->addChunk(C);
962     for (Chunk *C : DelayIdata.getDataChunks())
963       DataSec->addChunk(C);
964     for (Chunk *C : DelayIdata.getCodeChunks())
965       TextSec->addChunk(C);
966   }
967 }
968 
969 void Writer::createExportTable() {
970   if (Config->Exports.empty())
971     return;
972   for (Chunk *C : Edata.Chunks)
973     EdataSec->addChunk(C);
974 }
975 
976 void Writer::removeUnusedSections() {
977   // Remove sections that we can be sure won't get content, to avoid
978   // allocating space for their section headers.
979   auto IsUnused = [this](OutputSection *S) {
980     if (S == RelocSec)
981       return false; // This section is populated later.
982     // MergeChunks have zero size at this point, as their size is finalized
983     // later. Only remove sections that have no Chunks at all.
984     return S->Chunks.empty();
985   };
986   OutputSections.erase(
987       std::remove_if(OutputSections.begin(), OutputSections.end(), IsUnused),
988       OutputSections.end());
989 }
990 
991 // The Windows loader doesn't seem to like empty sections,
992 // so we remove them if any.
993 void Writer::removeEmptySections() {
994   auto IsEmpty = [](OutputSection *S) { return S->getVirtualSize() == 0; };
995   OutputSections.erase(
996       std::remove_if(OutputSections.begin(), OutputSections.end(), IsEmpty),
997       OutputSections.end());
998   uint32_t Idx = 1;
999   for (OutputSection *Sec : OutputSections)
1000     Sec->SectionIndex = Idx++;
1001 }
1002 
1003 size_t Writer::addEntryToStringTable(StringRef Str) {
1004   assert(Str.size() > COFF::NameSize);
1005   size_t OffsetOfEntry = Strtab.size() + 4; // +4 for the size field
1006   Strtab.insert(Strtab.end(), Str.begin(), Str.end());
1007   Strtab.push_back('\0');
1008   return OffsetOfEntry;
1009 }
1010 
1011 Optional<coff_symbol16> Writer::createSymbol(Defined *Def) {
1012   coff_symbol16 Sym;
1013   switch (Def->kind()) {
1014   case Symbol::DefinedAbsoluteKind:
1015     Sym.Value = Def->getRVA();
1016     Sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
1017     break;
1018   case Symbol::DefinedSyntheticKind:
1019     // Relative symbols are unrepresentable in a COFF symbol table.
1020     return None;
1021   default: {
1022     // Don't write symbols that won't be written to the output to the symbol
1023     // table.
1024     Chunk *C = Def->getChunk();
1025     if (!C)
1026       return None;
1027     OutputSection *OS = C->getOutputSection();
1028     if (!OS)
1029       return None;
1030 
1031     Sym.Value = Def->getRVA() - OS->getRVA();
1032     Sym.SectionNumber = OS->SectionIndex;
1033     break;
1034   }
1035   }
1036 
1037   StringRef Name = Def->getName();
1038   if (Name.size() > COFF::NameSize) {
1039     Sym.Name.Offset.Zeroes = 0;
1040     Sym.Name.Offset.Offset = addEntryToStringTable(Name);
1041   } else {
1042     memset(Sym.Name.ShortName, 0, COFF::NameSize);
1043     memcpy(Sym.Name.ShortName, Name.data(), Name.size());
1044   }
1045 
1046   if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
1047     COFFSymbolRef Ref = D->getCOFFSymbol();
1048     Sym.Type = Ref.getType();
1049     Sym.StorageClass = Ref.getStorageClass();
1050   } else {
1051     Sym.Type = IMAGE_SYM_TYPE_NULL;
1052     Sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
1053   }
1054   Sym.NumberOfAuxSymbols = 0;
1055   return Sym;
1056 }
1057 
1058 void Writer::createSymbolAndStringTable() {
1059   // PE/COFF images are limited to 8 byte section names. Longer names can be
1060   // supported by writing a non-standard string table, but this string table is
1061   // not mapped at runtime and the long names will therefore be inaccessible.
1062   // link.exe always truncates section names to 8 bytes, whereas binutils always
1063   // preserves long section names via the string table. LLD adopts a hybrid
1064   // solution where discardable sections have long names preserved and
1065   // non-discardable sections have their names truncated, to ensure that any
1066   // section which is mapped at runtime also has its name mapped at runtime.
1067   for (OutputSection *Sec : OutputSections) {
1068     if (Sec->Name.size() <= COFF::NameSize)
1069       continue;
1070     if ((Sec->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0)
1071       continue;
1072     Sec->setStringTableOff(addEntryToStringTable(Sec->Name));
1073   }
1074 
1075   if (Config->DebugDwarf || Config->DebugSymtab) {
1076     for (ObjFile *File : ObjFile::Instances) {
1077       for (Symbol *B : File->getSymbols()) {
1078         auto *D = dyn_cast_or_null<Defined>(B);
1079         if (!D || D->WrittenToSymtab)
1080           continue;
1081         D->WrittenToSymtab = true;
1082 
1083         if (Optional<coff_symbol16> Sym = createSymbol(D))
1084           OutputSymtab.push_back(*Sym);
1085       }
1086     }
1087   }
1088 
1089   if (OutputSymtab.empty() && Strtab.empty())
1090     return;
1091 
1092   // We position the symbol table to be adjacent to the end of the last section.
1093   uint64_t FileOff = FileSize;
1094   PointerToSymbolTable = FileOff;
1095   FileOff += OutputSymtab.size() * sizeof(coff_symbol16);
1096   FileOff += 4 + Strtab.size();
1097   FileSize = alignTo(FileOff, SectorSize);
1098 }
1099 
1100 void Writer::mergeSections() {
1101   if (!PdataSec->Chunks.empty()) {
1102     FirstPdata = PdataSec->Chunks.front();
1103     LastPdata = PdataSec->Chunks.back();
1104   }
1105 
1106   for (auto &P : Config->Merge) {
1107     StringRef ToName = P.second;
1108     if (P.first == ToName)
1109       continue;
1110     StringSet<> Names;
1111     while (1) {
1112       if (!Names.insert(ToName).second)
1113         fatal("/merge: cycle found for section '" + P.first + "'");
1114       auto I = Config->Merge.find(ToName);
1115       if (I == Config->Merge.end())
1116         break;
1117       ToName = I->second;
1118     }
1119     OutputSection *From = findSection(P.first);
1120     OutputSection *To = findSection(ToName);
1121     if (!From)
1122       continue;
1123     if (!To) {
1124       From->Name = ToName;
1125       continue;
1126     }
1127     To->merge(From);
1128   }
1129 }
1130 
1131 // Visits all sections to assign incremental, non-overlapping RVAs and
1132 // file offsets.
1133 void Writer::assignAddresses() {
1134   SizeOfHeaders = DOSStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
1135                   sizeof(data_directory) * NumberOfDataDirectory +
1136                   sizeof(coff_section) * OutputSections.size();
1137   SizeOfHeaders +=
1138       Config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
1139   SizeOfHeaders = alignTo(SizeOfHeaders, SectorSize);
1140   uint64_t RVA = PageSize; // The first page is kept unmapped.
1141   FileSize = SizeOfHeaders;
1142 
1143   for (OutputSection *Sec : OutputSections) {
1144     if (Sec == RelocSec)
1145       addBaserels();
1146     uint64_t RawSize = 0, VirtualSize = 0;
1147     Sec->Header.VirtualAddress = RVA;
1148 
1149     // If /FUNCTIONPADMIN is used, functions are padded in order to create a
1150     // hotpatchable image.
1151     const bool IsCodeSection =
1152         (Sec->Header.Characteristics & IMAGE_SCN_CNT_CODE) &&
1153         (Sec->Header.Characteristics & IMAGE_SCN_MEM_READ) &&
1154         (Sec->Header.Characteristics & IMAGE_SCN_MEM_EXECUTE);
1155     uint32_t Padding = IsCodeSection ? Config->FunctionPadMin : 0;
1156 
1157     for (Chunk *C : Sec->Chunks) {
1158       if (Padding && C->isHotPatchable())
1159         VirtualSize += Padding;
1160       VirtualSize = alignTo(VirtualSize, C->Alignment);
1161       C->setRVA(RVA + VirtualSize);
1162       C->OutputSectionOff = VirtualSize;
1163       C->finalizeContents();
1164       VirtualSize += C->getSize();
1165       if (C->hasData())
1166         RawSize = alignTo(VirtualSize, SectorSize);
1167     }
1168     if (VirtualSize > UINT32_MAX)
1169       error("section larger than 4 GiB: " + Sec->Name);
1170     Sec->Header.VirtualSize = VirtualSize;
1171     Sec->Header.SizeOfRawData = RawSize;
1172     if (RawSize != 0)
1173       Sec->Header.PointerToRawData = FileSize;
1174     RVA += alignTo(VirtualSize, PageSize);
1175     FileSize += alignTo(RawSize, SectorSize);
1176   }
1177   SizeOfImage = alignTo(RVA, PageSize);
1178 }
1179 
1180 template <typename PEHeaderTy> void Writer::writeHeader() {
1181   // Write DOS header. For backwards compatibility, the first part of a PE/COFF
1182   // executable consists of an MS-DOS MZ executable. If the executable is run
1183   // under DOS, that program gets run (usually to just print an error message).
1184   // When run under Windows, the loader looks at AddressOfNewExeHeader and uses
1185   // the PE header instead.
1186   uint8_t *Buf = Buffer->getBufferStart();
1187   auto *DOS = reinterpret_cast<dos_header *>(Buf);
1188   Buf += sizeof(dos_header);
1189   DOS->Magic[0] = 'M';
1190   DOS->Magic[1] = 'Z';
1191   DOS->UsedBytesInTheLastPage = DOSStubSize % 512;
1192   DOS->FileSizeInPages = divideCeil(DOSStubSize, 512);
1193   DOS->HeaderSizeInParagraphs = sizeof(dos_header) / 16;
1194 
1195   DOS->AddressOfRelocationTable = sizeof(dos_header);
1196   DOS->AddressOfNewExeHeader = DOSStubSize;
1197 
1198   // Write DOS program.
1199   memcpy(Buf, DOSProgram, sizeof(DOSProgram));
1200   Buf += sizeof(DOSProgram);
1201 
1202   // Write PE magic
1203   memcpy(Buf, PEMagic, sizeof(PEMagic));
1204   Buf += sizeof(PEMagic);
1205 
1206   // Write COFF header
1207   auto *COFF = reinterpret_cast<coff_file_header *>(Buf);
1208   Buf += sizeof(*COFF);
1209   COFF->Machine = Config->Machine;
1210   COFF->NumberOfSections = OutputSections.size();
1211   COFF->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
1212   if (Config->LargeAddressAware)
1213     COFF->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
1214   if (!Config->is64())
1215     COFF->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
1216   if (Config->DLL)
1217     COFF->Characteristics |= IMAGE_FILE_DLL;
1218   if (!Config->Relocatable)
1219     COFF->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
1220   if (Config->SwaprunCD)
1221     COFF->Characteristics |= IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP;
1222   if (Config->SwaprunNet)
1223     COFF->Characteristics |= IMAGE_FILE_NET_RUN_FROM_SWAP;
1224   COFF->SizeOfOptionalHeader =
1225       sizeof(PEHeaderTy) + sizeof(data_directory) * NumberOfDataDirectory;
1226 
1227   // Write PE header
1228   auto *PE = reinterpret_cast<PEHeaderTy *>(Buf);
1229   Buf += sizeof(*PE);
1230   PE->Magic = Config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
1231 
1232   // If {Major,Minor}LinkerVersion is left at 0.0, then for some
1233   // reason signing the resulting PE file with Authenticode produces a
1234   // signature that fails to validate on Windows 7 (but is OK on 10).
1235   // Set it to 14.0, which is what VS2015 outputs, and which avoids
1236   // that problem.
1237   PE->MajorLinkerVersion = 14;
1238   PE->MinorLinkerVersion = 0;
1239 
1240   PE->ImageBase = Config->ImageBase;
1241   PE->SectionAlignment = PageSize;
1242   PE->FileAlignment = SectorSize;
1243   PE->MajorImageVersion = Config->MajorImageVersion;
1244   PE->MinorImageVersion = Config->MinorImageVersion;
1245   PE->MajorOperatingSystemVersion = Config->MajorOSVersion;
1246   PE->MinorOperatingSystemVersion = Config->MinorOSVersion;
1247   PE->MajorSubsystemVersion = Config->MajorOSVersion;
1248   PE->MinorSubsystemVersion = Config->MinorOSVersion;
1249   PE->Subsystem = Config->Subsystem;
1250   PE->SizeOfImage = SizeOfImage;
1251   PE->SizeOfHeaders = SizeOfHeaders;
1252   if (!Config->NoEntry) {
1253     Defined *Entry = cast<Defined>(Config->Entry);
1254     PE->AddressOfEntryPoint = Entry->getRVA();
1255     // Pointer to thumb code must have the LSB set, so adjust it.
1256     if (Config->Machine == ARMNT)
1257       PE->AddressOfEntryPoint |= 1;
1258   }
1259   PE->SizeOfStackReserve = Config->StackReserve;
1260   PE->SizeOfStackCommit = Config->StackCommit;
1261   PE->SizeOfHeapReserve = Config->HeapReserve;
1262   PE->SizeOfHeapCommit = Config->HeapCommit;
1263   if (Config->AppContainer)
1264     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER;
1265   if (Config->DynamicBase)
1266     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
1267   if (Config->HighEntropyVA)
1268     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
1269   if (!Config->AllowBind)
1270     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND;
1271   if (Config->NxCompat)
1272     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
1273   if (!Config->AllowIsolation)
1274     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION;
1275   if (Config->GuardCF != GuardCFLevel::Off)
1276     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF;
1277   if (Config->IntegrityCheck)
1278     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
1279   if (SetNoSEHCharacteristic)
1280     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH;
1281   if (Config->TerminalServerAware)
1282     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
1283   PE->NumberOfRvaAndSize = NumberOfDataDirectory;
1284   if (TextSec->getVirtualSize()) {
1285     PE->BaseOfCode = TextSec->getRVA();
1286     PE->SizeOfCode = TextSec->getRawSize();
1287   }
1288   PE->SizeOfInitializedData = getSizeOfInitializedData();
1289 
1290   // Write data directory
1291   auto *Dir = reinterpret_cast<data_directory *>(Buf);
1292   Buf += sizeof(*Dir) * NumberOfDataDirectory;
1293   if (!Config->Exports.empty()) {
1294     Dir[EXPORT_TABLE].RelativeVirtualAddress = Edata.getRVA();
1295     Dir[EXPORT_TABLE].Size = Edata.getSize();
1296   }
1297   if (ImportTableStart) {
1298     Dir[IMPORT_TABLE].RelativeVirtualAddress = ImportTableStart->getRVA();
1299     Dir[IMPORT_TABLE].Size = ImportTableSize;
1300   }
1301   if (IATStart) {
1302     Dir[IAT].RelativeVirtualAddress = IATStart->getRVA();
1303     Dir[IAT].Size = IATSize;
1304   }
1305   if (RsrcSec->getVirtualSize()) {
1306     Dir[RESOURCE_TABLE].RelativeVirtualAddress = RsrcSec->getRVA();
1307     Dir[RESOURCE_TABLE].Size = RsrcSec->getVirtualSize();
1308   }
1309   if (FirstPdata) {
1310     Dir[EXCEPTION_TABLE].RelativeVirtualAddress = FirstPdata->getRVA();
1311     Dir[EXCEPTION_TABLE].Size =
1312         LastPdata->getRVA() + LastPdata->getSize() - FirstPdata->getRVA();
1313   }
1314   if (RelocSec->getVirtualSize()) {
1315     Dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = RelocSec->getRVA();
1316     Dir[BASE_RELOCATION_TABLE].Size = RelocSec->getVirtualSize();
1317   }
1318   if (Symbol *Sym = Symtab->findUnderscore("_tls_used")) {
1319     if (Defined *B = dyn_cast<Defined>(Sym)) {
1320       Dir[TLS_TABLE].RelativeVirtualAddress = B->getRVA();
1321       Dir[TLS_TABLE].Size = Config->is64()
1322                                 ? sizeof(object::coff_tls_directory64)
1323                                 : sizeof(object::coff_tls_directory32);
1324     }
1325   }
1326   if (DebugDirectory) {
1327     Dir[DEBUG_DIRECTORY].RelativeVirtualAddress = DebugDirectory->getRVA();
1328     Dir[DEBUG_DIRECTORY].Size = DebugDirectory->getSize();
1329   }
1330   if (Symbol *Sym = Symtab->findUnderscore("_load_config_used")) {
1331     if (auto *B = dyn_cast<DefinedRegular>(Sym)) {
1332       SectionChunk *SC = B->getChunk();
1333       assert(B->getRVA() >= SC->getRVA());
1334       uint64_t OffsetInChunk = B->getRVA() - SC->getRVA();
1335       if (!SC->hasData() || OffsetInChunk + 4 > SC->getSize())
1336         fatal("_load_config_used is malformed");
1337 
1338       ArrayRef<uint8_t> SecContents = SC->getContents();
1339       uint32_t LoadConfigSize =
1340           *reinterpret_cast<const ulittle32_t *>(&SecContents[OffsetInChunk]);
1341       if (OffsetInChunk + LoadConfigSize > SC->getSize())
1342         fatal("_load_config_used is too large");
1343       Dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = B->getRVA();
1344       Dir[LOAD_CONFIG_TABLE].Size = LoadConfigSize;
1345     }
1346   }
1347   if (!DelayIdata.empty()) {
1348     Dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
1349         DelayIdata.getDirRVA();
1350     Dir[DELAY_IMPORT_DESCRIPTOR].Size = DelayIdata.getDirSize();
1351   }
1352 
1353   // Write section table
1354   for (OutputSection *Sec : OutputSections) {
1355     Sec->writeHeaderTo(Buf);
1356     Buf += sizeof(coff_section);
1357   }
1358   SectionTable = ArrayRef<uint8_t>(
1359       Buf - OutputSections.size() * sizeof(coff_section), Buf);
1360 
1361   if (OutputSymtab.empty() && Strtab.empty())
1362     return;
1363 
1364   COFF->PointerToSymbolTable = PointerToSymbolTable;
1365   uint32_t NumberOfSymbols = OutputSymtab.size();
1366   COFF->NumberOfSymbols = NumberOfSymbols;
1367   auto *SymbolTable = reinterpret_cast<coff_symbol16 *>(
1368       Buffer->getBufferStart() + COFF->PointerToSymbolTable);
1369   for (size_t I = 0; I != NumberOfSymbols; ++I)
1370     SymbolTable[I] = OutputSymtab[I];
1371   // Create the string table, it follows immediately after the symbol table.
1372   // The first 4 bytes is length including itself.
1373   Buf = reinterpret_cast<uint8_t *>(&SymbolTable[NumberOfSymbols]);
1374   write32le(Buf, Strtab.size() + 4);
1375   if (!Strtab.empty())
1376     memcpy(Buf + 4, Strtab.data(), Strtab.size());
1377 }
1378 
1379 void Writer::openFile(StringRef Path) {
1380   Buffer = CHECK(
1381       FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable),
1382       "failed to open " + Path);
1383 }
1384 
1385 void Writer::createSEHTable() {
1386   // Set the no SEH characteristic on x86 binaries unless we find exception
1387   // handlers.
1388   SetNoSEHCharacteristic = true;
1389 
1390   SymbolRVASet Handlers;
1391   for (ObjFile *File : ObjFile::Instances) {
1392     // FIXME: We should error here instead of earlier unless /safeseh:no was
1393     // passed.
1394     if (!File->hasSafeSEH())
1395       return;
1396 
1397     markSymbolsForRVATable(File, File->getSXDataChunks(), Handlers);
1398   }
1399 
1400   // Remove the "no SEH" characteristic if all object files were built with
1401   // safeseh, we found some exception handlers, and there is a load config in
1402   // the object.
1403   SetNoSEHCharacteristic =
1404       Handlers.empty() || !Symtab->findUnderscore("_load_config_used");
1405 
1406   maybeAddRVATable(std::move(Handlers), "__safe_se_handler_table",
1407                    "__safe_se_handler_count");
1408 }
1409 
1410 // Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set
1411 // cannot contain duplicates. Therefore, the set is uniqued by Chunk and the
1412 // symbol's offset into that Chunk.
1413 static void addSymbolToRVASet(SymbolRVASet &RVASet, Defined *S) {
1414   Chunk *C = S->getChunk();
1415   if (auto *SC = dyn_cast<SectionChunk>(C))
1416     C = SC->Repl; // Look through ICF replacement.
1417   uint32_t Off = S->getRVA() - (C ? C->getRVA() : 0);
1418   RVASet.insert({C, Off});
1419 }
1420 
1421 // Given a symbol, add it to the GFIDs table if it is a live, defined, function
1422 // symbol in an executable section.
1423 static void maybeAddAddressTakenFunction(SymbolRVASet &AddressTakenSyms,
1424                                          Symbol *S) {
1425   if (!S)
1426     return;
1427 
1428   switch (S->kind()) {
1429   case Symbol::DefinedLocalImportKind:
1430   case Symbol::DefinedImportDataKind:
1431     // Defines an __imp_ pointer, so it is data, so it is ignored.
1432     break;
1433   case Symbol::DefinedCommonKind:
1434     // Common is always data, so it is ignored.
1435     break;
1436   case Symbol::DefinedAbsoluteKind:
1437   case Symbol::DefinedSyntheticKind:
1438     // Absolute is never code, synthetic generally isn't and usually isn't
1439     // determinable.
1440     break;
1441   case Symbol::LazyKind:
1442   case Symbol::UndefinedKind:
1443     // Undefined symbols resolve to zero, so they don't have an RVA. Lazy
1444     // symbols shouldn't have relocations.
1445     break;
1446 
1447   case Symbol::DefinedImportThunkKind:
1448     // Thunks are always code, include them.
1449     addSymbolToRVASet(AddressTakenSyms, cast<Defined>(S));
1450     break;
1451 
1452   case Symbol::DefinedRegularKind: {
1453     // This is a regular, defined, symbol from a COFF file. Mark the symbol as
1454     // address taken if the symbol type is function and it's in an executable
1455     // section.
1456     auto *D = cast<DefinedRegular>(S);
1457     if (D->getCOFFSymbol().getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
1458       Chunk *RefChunk = D->getChunk();
1459       OutputSection *OS = RefChunk ? RefChunk->getOutputSection() : nullptr;
1460       if (OS && OS->Header.Characteristics & IMAGE_SCN_MEM_EXECUTE)
1461         addSymbolToRVASet(AddressTakenSyms, D);
1462     }
1463     break;
1464   }
1465   }
1466 }
1467 
1468 // Visit all relocations from all section contributions of this object file and
1469 // mark the relocation target as address-taken.
1470 static void markSymbolsWithRelocations(ObjFile *File,
1471                                        SymbolRVASet &UsedSymbols) {
1472   for (Chunk *C : File->getChunks()) {
1473     // We only care about live section chunks. Common chunks and other chunks
1474     // don't generally contain relocations.
1475     SectionChunk *SC = dyn_cast<SectionChunk>(C);
1476     if (!SC || !SC->Live)
1477       continue;
1478 
1479     for (const coff_relocation &Reloc : SC->Relocs) {
1480       if (Config->Machine == I386 && Reloc.Type == COFF::IMAGE_REL_I386_REL32)
1481         // Ignore relative relocations on x86. On x86_64 they can't be ignored
1482         // since they're also used to compute absolute addresses.
1483         continue;
1484 
1485       Symbol *Ref = SC->File->getSymbol(Reloc.SymbolTableIndex);
1486       maybeAddAddressTakenFunction(UsedSymbols, Ref);
1487     }
1488   }
1489 }
1490 
1491 // Create the guard function id table. This is a table of RVAs of all
1492 // address-taken functions. It is sorted and uniqued, just like the safe SEH
1493 // table.
1494 void Writer::createGuardCFTables() {
1495   SymbolRVASet AddressTakenSyms;
1496   SymbolRVASet LongJmpTargets;
1497   for (ObjFile *File : ObjFile::Instances) {
1498     // If the object was compiled with /guard:cf, the address taken symbols
1499     // are in .gfids$y sections, and the longjmp targets are in .gljmp$y
1500     // sections. If the object was not compiled with /guard:cf, we assume there
1501     // were no setjmp targets, and that all code symbols with relocations are
1502     // possibly address-taken.
1503     if (File->hasGuardCF()) {
1504       markSymbolsForRVATable(File, File->getGuardFidChunks(), AddressTakenSyms);
1505       markSymbolsForRVATable(File, File->getGuardLJmpChunks(), LongJmpTargets);
1506     } else {
1507       markSymbolsWithRelocations(File, AddressTakenSyms);
1508     }
1509   }
1510 
1511   // Mark the image entry as address-taken.
1512   if (Config->Entry)
1513     maybeAddAddressTakenFunction(AddressTakenSyms, Config->Entry);
1514 
1515   // Mark exported symbols in executable sections as address-taken.
1516   for (Export &E : Config->Exports)
1517     maybeAddAddressTakenFunction(AddressTakenSyms, E.Sym);
1518 
1519   // Ensure sections referenced in the gfid table are 16-byte aligned.
1520   for (const ChunkAndOffset &C : AddressTakenSyms)
1521     if (C.InputChunk->Alignment < 16)
1522       C.InputChunk->Alignment = 16;
1523 
1524   maybeAddRVATable(std::move(AddressTakenSyms), "__guard_fids_table",
1525                    "__guard_fids_count");
1526 
1527   // Add the longjmp target table unless the user told us not to.
1528   if (Config->GuardCF == GuardCFLevel::Full)
1529     maybeAddRVATable(std::move(LongJmpTargets), "__guard_longjmp_table",
1530                      "__guard_longjmp_count");
1531 
1532   // Set __guard_flags, which will be used in the load config to indicate that
1533   // /guard:cf was enabled.
1534   uint32_t GuardFlags = uint32_t(coff_guard_flags::CFInstrumented) |
1535                         uint32_t(coff_guard_flags::HasFidTable);
1536   if (Config->GuardCF == GuardCFLevel::Full)
1537     GuardFlags |= uint32_t(coff_guard_flags::HasLongJmpTable);
1538   Symbol *FlagSym = Symtab->findUnderscore("__guard_flags");
1539   cast<DefinedAbsolute>(FlagSym)->setVA(GuardFlags);
1540 }
1541 
1542 // Take a list of input sections containing symbol table indices and add those
1543 // symbols to an RVA table. The challenge is that symbol RVAs are not known and
1544 // depend on the table size, so we can't directly build a set of integers.
1545 void Writer::markSymbolsForRVATable(ObjFile *File,
1546                                     ArrayRef<SectionChunk *> SymIdxChunks,
1547                                     SymbolRVASet &TableSymbols) {
1548   for (SectionChunk *C : SymIdxChunks) {
1549     // Skip sections discarded by linker GC. This comes up when a .gfids section
1550     // is associated with something like a vtable and the vtable is discarded.
1551     // In this case, the associated gfids section is discarded, and we don't
1552     // mark the virtual member functions as address-taken by the vtable.
1553     if (!C->Live)
1554       continue;
1555 
1556     // Validate that the contents look like symbol table indices.
1557     ArrayRef<uint8_t> Data = C->getContents();
1558     if (Data.size() % 4 != 0) {
1559       warn("ignoring " + C->getSectionName() +
1560            " symbol table index section in object " + toString(File));
1561       continue;
1562     }
1563 
1564     // Read each symbol table index and check if that symbol was included in the
1565     // final link. If so, add it to the table symbol set.
1566     ArrayRef<ulittle32_t> SymIndices(
1567         reinterpret_cast<const ulittle32_t *>(Data.data()), Data.size() / 4);
1568     ArrayRef<Symbol *> ObjSymbols = File->getSymbols();
1569     for (uint32_t SymIndex : SymIndices) {
1570       if (SymIndex >= ObjSymbols.size()) {
1571         warn("ignoring invalid symbol table index in section " +
1572              C->getSectionName() + " in object " + toString(File));
1573         continue;
1574       }
1575       if (Symbol *S = ObjSymbols[SymIndex]) {
1576         if (S->isLive())
1577           addSymbolToRVASet(TableSymbols, cast<Defined>(S));
1578       }
1579     }
1580   }
1581 }
1582 
1583 // Replace the absolute table symbol with a synthetic symbol pointing to
1584 // TableChunk so that we can emit base relocations for it and resolve section
1585 // relative relocations.
1586 void Writer::maybeAddRVATable(SymbolRVASet TableSymbols, StringRef TableSym,
1587                               StringRef CountSym) {
1588   if (TableSymbols.empty())
1589     return;
1590 
1591   RVATableChunk *TableChunk = make<RVATableChunk>(std::move(TableSymbols));
1592   RdataSec->addChunk(TableChunk);
1593 
1594   Symbol *T = Symtab->findUnderscore(TableSym);
1595   Symbol *C = Symtab->findUnderscore(CountSym);
1596   replaceSymbol<DefinedSynthetic>(T, T->getName(), TableChunk);
1597   cast<DefinedAbsolute>(C)->setVA(TableChunk->getSize() / 4);
1598 }
1599 
1600 // MinGW specific. Gather all relocations that are imported from a DLL even
1601 // though the code didn't expect it to, produce the table that the runtime
1602 // uses for fixing them up, and provide the synthetic symbols that the
1603 // runtime uses for finding the table.
1604 void Writer::createRuntimePseudoRelocs() {
1605   std::vector<RuntimePseudoReloc> Rels;
1606 
1607   for (Chunk *C : Symtab->getChunks()) {
1608     auto *SC = dyn_cast<SectionChunk>(C);
1609     if (!SC || !SC->Live)
1610       continue;
1611     SC->getRuntimePseudoRelocs(Rels);
1612   }
1613 
1614   if (!Rels.empty())
1615     log("Writing " + Twine(Rels.size()) + " runtime pseudo relocations");
1616   PseudoRelocTableChunk *Table = make<PseudoRelocTableChunk>(Rels);
1617   RdataSec->addChunk(Table);
1618   EmptyChunk *EndOfList = make<EmptyChunk>();
1619   RdataSec->addChunk(EndOfList);
1620 
1621   Symbol *HeadSym = Symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__");
1622   Symbol *EndSym = Symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__");
1623   replaceSymbol<DefinedSynthetic>(HeadSym, HeadSym->getName(), Table);
1624   replaceSymbol<DefinedSynthetic>(EndSym, EndSym->getName(), EndOfList);
1625 }
1626 
1627 // MinGW specific.
1628 // The MinGW .ctors and .dtors lists have sentinels at each end;
1629 // a (uintptr_t)-1 at the start and a (uintptr_t)0 at the end.
1630 // There's a symbol pointing to the start sentinel pointer, __CTOR_LIST__
1631 // and __DTOR_LIST__ respectively.
1632 void Writer::insertCtorDtorSymbols() {
1633   AbsolutePointerChunk *CtorListHead = make<AbsolutePointerChunk>(-1);
1634   AbsolutePointerChunk *CtorListEnd = make<AbsolutePointerChunk>(0);
1635   AbsolutePointerChunk *DtorListHead = make<AbsolutePointerChunk>(-1);
1636   AbsolutePointerChunk *DtorListEnd = make<AbsolutePointerChunk>(0);
1637   CtorsSec->insertChunkAtStart(CtorListHead);
1638   CtorsSec->addChunk(CtorListEnd);
1639   DtorsSec->insertChunkAtStart(DtorListHead);
1640   DtorsSec->addChunk(DtorListEnd);
1641 
1642   Symbol *CtorListSym = Symtab->findUnderscore("__CTOR_LIST__");
1643   Symbol *DtorListSym = Symtab->findUnderscore("__DTOR_LIST__");
1644   replaceSymbol<DefinedSynthetic>(CtorListSym, CtorListSym->getName(),
1645                                   CtorListHead);
1646   replaceSymbol<DefinedSynthetic>(DtorListSym, DtorListSym->getName(),
1647                                   DtorListHead);
1648 }
1649 
1650 // Handles /section options to allow users to overwrite
1651 // section attributes.
1652 void Writer::setSectionPermissions() {
1653   for (auto &P : Config->Section) {
1654     StringRef Name = P.first;
1655     uint32_t Perm = P.second;
1656     for (OutputSection *Sec : OutputSections)
1657       if (Sec->Name == Name)
1658         Sec->setPermissions(Perm);
1659   }
1660 }
1661 
1662 // Write section contents to a mmap'ed file.
1663 void Writer::writeSections() {
1664   // Record the number of sections to apply section index relocations
1665   // against absolute symbols. See applySecIdx in Chunks.cpp..
1666   DefinedAbsolute::NumOutputSections = OutputSections.size();
1667 
1668   uint8_t *Buf = Buffer->getBufferStart();
1669   for (OutputSection *Sec : OutputSections) {
1670     uint8_t *SecBuf = Buf + Sec->getFileOff();
1671     // Fill gaps between functions in .text with INT3 instructions
1672     // instead of leaving as NUL bytes (which can be interpreted as
1673     // ADD instructions).
1674     if (Sec->Header.Characteristics & IMAGE_SCN_CNT_CODE)
1675       memset(SecBuf, 0xCC, Sec->getRawSize());
1676     parallelForEach(Sec->Chunks, [&](Chunk *C) { C->writeTo(SecBuf); });
1677   }
1678 }
1679 
1680 void Writer::writeBuildId() {
1681   // There are two important parts to the build ID.
1682   // 1) If building with debug info, the COFF debug directory contains a
1683   //    timestamp as well as a Guid and Age of the PDB.
1684   // 2) In all cases, the PE COFF file header also contains a timestamp.
1685   // For reproducibility, instead of a timestamp we want to use a hash of the
1686   // PE contents.
1687   if (Config->Debug) {
1688     assert(BuildId && "BuildId is not set!");
1689     // BuildId->BuildId was filled in when the PDB was written.
1690   }
1691 
1692   // At this point the only fields in the COFF file which remain unset are the
1693   // "timestamp" in the COFF file header, and the ones in the coff debug
1694   // directory.  Now we can hash the file and write that hash to the various
1695   // timestamp fields in the file.
1696   StringRef OutputFileData(
1697       reinterpret_cast<const char *>(Buffer->getBufferStart()),
1698       Buffer->getBufferSize());
1699 
1700   uint32_t Timestamp = Config->Timestamp;
1701   uint64_t Hash = 0;
1702   bool GenerateSyntheticBuildId =
1703       Config->MinGW && Config->Debug && Config->PDBPath.empty();
1704 
1705   if (Config->Repro || GenerateSyntheticBuildId)
1706     Hash = xxHash64(OutputFileData);
1707 
1708   if (Config->Repro)
1709     Timestamp = static_cast<uint32_t>(Hash);
1710 
1711   if (GenerateSyntheticBuildId) {
1712     // For MinGW builds without a PDB file, we still generate a build id
1713     // to allow associating a crash dump to the executable.
1714     BuildId->BuildId->PDB70.CVSignature = OMF::Signature::PDB70;
1715     BuildId->BuildId->PDB70.Age = 1;
1716     memcpy(BuildId->BuildId->PDB70.Signature, &Hash, 8);
1717     // xxhash only gives us 8 bytes, so put some fixed data in the other half.
1718     memcpy(&BuildId->BuildId->PDB70.Signature[8], "LLD PDB.", 8);
1719   }
1720 
1721   if (DebugDirectory)
1722     DebugDirectory->setTimeDateStamp(Timestamp);
1723 
1724   uint8_t *Buf = Buffer->getBufferStart();
1725   Buf += DOSStubSize + sizeof(PEMagic);
1726   object::coff_file_header *CoffHeader =
1727       reinterpret_cast<coff_file_header *>(Buf);
1728   CoffHeader->TimeDateStamp = Timestamp;
1729 }
1730 
1731 // Sort .pdata section contents according to PE/COFF spec 5.5.
1732 void Writer::sortExceptionTable() {
1733   if (!FirstPdata)
1734     return;
1735   // We assume .pdata contains function table entries only.
1736   auto BufAddr = [&](Chunk *C) {
1737     return Buffer->getBufferStart() + C->getOutputSection()->getFileOff() +
1738            C->getRVA() - C->getOutputSection()->getRVA();
1739   };
1740   uint8_t *Begin = BufAddr(FirstPdata);
1741   uint8_t *End = BufAddr(LastPdata) + LastPdata->getSize();
1742   if (Config->Machine == AMD64) {
1743     struct Entry { ulittle32_t Begin, End, Unwind; };
1744     parallelSort(
1745         MutableArrayRef<Entry>((Entry *)Begin, (Entry *)End),
1746         [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
1747     return;
1748   }
1749   if (Config->Machine == ARMNT || Config->Machine == ARM64) {
1750     struct Entry { ulittle32_t Begin, Unwind; };
1751     parallelSort(
1752         MutableArrayRef<Entry>((Entry *)Begin, (Entry *)End),
1753         [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
1754     return;
1755   }
1756   errs() << "warning: don't know how to handle .pdata.\n";
1757 }
1758 
1759 // The CRT section contains, among other things, the array of function
1760 // pointers that initialize every global variable that is not trivially
1761 // constructed. The CRT calls them one after the other prior to invoking
1762 // main().
1763 //
1764 // As per C++ spec, 3.6.2/2.3,
1765 // "Variables with ordered initialization defined within a single
1766 // translation unit shall be initialized in the order of their definitions
1767 // in the translation unit"
1768 //
1769 // It is therefore critical to sort the chunks containing the function
1770 // pointers in the order that they are listed in the object file (top to
1771 // bottom), otherwise global objects might not be initialized in the
1772 // correct order.
1773 void Writer::sortCRTSectionChunks(std::vector<Chunk *> &Chunks) {
1774   auto SectionChunkOrder = [](const Chunk *A, const Chunk *B) {
1775     auto SA = dyn_cast<SectionChunk>(A);
1776     auto SB = dyn_cast<SectionChunk>(B);
1777     assert(SA && SB && "Non-section chunks in CRT section!");
1778 
1779     StringRef SAObj = SA->File->MB.getBufferIdentifier();
1780     StringRef SBObj = SB->File->MB.getBufferIdentifier();
1781 
1782     return SAObj == SBObj && SA->getSectionNumber() < SB->getSectionNumber();
1783   };
1784   llvm::stable_sort(Chunks, SectionChunkOrder);
1785 
1786   if (Config->Verbose) {
1787     for (auto &C : Chunks) {
1788       auto SC = dyn_cast<SectionChunk>(C);
1789       log("  " + SC->File->MB.getBufferIdentifier().str() +
1790           ", SectionID: " + Twine(SC->getSectionNumber()));
1791     }
1792   }
1793 }
1794 
1795 OutputSection *Writer::findSection(StringRef Name) {
1796   for (OutputSection *Sec : OutputSections)
1797     if (Sec->Name == Name)
1798       return Sec;
1799   return nullptr;
1800 }
1801 
1802 uint32_t Writer::getSizeOfInitializedData() {
1803   uint32_t Res = 0;
1804   for (OutputSection *S : OutputSections)
1805     if (S->Header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
1806       Res += S->getRawSize();
1807   return Res;
1808 }
1809 
1810 // Add base relocations to .reloc section.
1811 void Writer::addBaserels() {
1812   if (!Config->Relocatable)
1813     return;
1814   RelocSec->Chunks.clear();
1815   std::vector<Baserel> V;
1816   for (OutputSection *Sec : OutputSections) {
1817     if (Sec->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
1818       continue;
1819     // Collect all locations for base relocations.
1820     for (Chunk *C : Sec->Chunks)
1821       C->getBaserels(&V);
1822     // Add the addresses to .reloc section.
1823     if (!V.empty())
1824       addBaserelBlocks(V);
1825     V.clear();
1826   }
1827 }
1828 
1829 // Add addresses to .reloc section. Note that addresses are grouped by page.
1830 void Writer::addBaserelBlocks(std::vector<Baserel> &V) {
1831   const uint32_t Mask = ~uint32_t(PageSize - 1);
1832   uint32_t Page = V[0].RVA & Mask;
1833   size_t I = 0, J = 1;
1834   for (size_t E = V.size(); J < E; ++J) {
1835     uint32_t P = V[J].RVA & Mask;
1836     if (P == Page)
1837       continue;
1838     RelocSec->addChunk(make<BaserelChunk>(Page, &V[I], &V[0] + J));
1839     I = J;
1840     Page = P;
1841   }
1842   if (I == J)
1843     return;
1844   RelocSec->addChunk(make<BaserelChunk>(Page, &V[I], &V[0] + J));
1845 }
1846 
1847 PartialSection *Writer::createPartialSection(StringRef Name,
1848                                              uint32_t OutChars) {
1849   PartialSection *&PSec = PartialSections[{Name, OutChars}];
1850   if (PSec)
1851     return PSec;
1852   PSec = make<PartialSection>(Name, OutChars);
1853   return PSec;
1854 }
1855 
1856 PartialSection *Writer::findPartialSection(StringRef Name, uint32_t OutChars) {
1857   auto It = PartialSections.find({Name, OutChars});
1858   if (It != PartialSections.end())
1859     return It->second;
1860   return nullptr;
1861 }
1862