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