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