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