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