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