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 NumberfOfDataDirectory = 16;
81 
82 namespace {
83 
84 class DebugDirectoryChunk : public Chunk {
85 public:
DebugDirectoryChunk(const std::vector<Chunk * > & R)86   DebugDirectoryChunk(const std::vector<Chunk *> &R) : Records(R) {}
87 
getSize() const88   size_t getSize() const override {
89     return Records.size() * sizeof(debug_directory);
90   }
91 
writeTo(uint8_t * B) const92   void writeTo(uint8_t *B) const override {
93     auto *D = reinterpret_cast<debug_directory *>(B + OutputSectionOff);
94 
95     for (const Chunk *Record : Records) {
96       D->Characteristics = 0;
97       D->TimeDateStamp = 0;
98       D->MajorVersion = 0;
99       D->MinorVersion = 0;
100       D->Type = COFF::IMAGE_DEBUG_TYPE_CODEVIEW;
101       D->SizeOfData = Record->getSize();
102       D->AddressOfRawData = Record->getRVA();
103       OutputSection *OS = Record->getOutputSection();
104       uint64_t Offs = OS->getFileOff() + (Record->getRVA() - OS->getRVA());
105       D->PointerToRawData = Offs;
106 
107       TimeDateStamps.push_back(&D->TimeDateStamp);
108       ++D;
109     }
110   }
111 
setTimeDateStamp(uint32_t TimeDateStamp)112   void setTimeDateStamp(uint32_t TimeDateStamp) {
113     for (support::ulittle32_t *TDS : TimeDateStamps)
114       *TDS = TimeDateStamp;
115   }
116 
117 private:
118   mutable std::vector<support::ulittle32_t *> TimeDateStamps;
119   const std::vector<Chunk *> &Records;
120 };
121 
122 class CVDebugRecordChunk : public Chunk {
123 public:
getSize() const124   size_t getSize() const override {
125     return sizeof(codeview::DebugInfo) + Config->PDBAltPath.size() + 1;
126   }
127 
writeTo(uint8_t * B) const128   void writeTo(uint8_t *B) const override {
129     // Save off the DebugInfo entry to backfill the file signature (build id)
130     // in Writer::writeBuildId
131     BuildId = reinterpret_cast<codeview::DebugInfo *>(B + OutputSectionOff);
132 
133     // variable sized field (PDB Path)
134     char *P = reinterpret_cast<char *>(B + OutputSectionOff + sizeof(*BuildId));
135     if (!Config->PDBAltPath.empty())
136       memcpy(P, Config->PDBAltPath.data(), Config->PDBAltPath.size());
137     P[Config->PDBAltPath.size()] = '\0';
138   }
139 
140   mutable codeview::DebugInfo *BuildId = nullptr;
141 };
142 
143 // The writer writes a SymbolTable result to a file.
144 class Writer {
145 public:
Writer()146   Writer() : Buffer(errorHandler().OutputBuffer) {}
147   void run();
148 
149 private:
150   void createSections();
151   void createMiscChunks();
152   void createImportTables();
153   void createExportTable();
154   void mergeSections();
155   void assignAddresses();
156   void removeEmptySections();
157   void createSymbolAndStringTable();
158   void openFile(StringRef OutputPath);
159   template <typename PEHeaderTy> void writeHeader();
160   void createSEHTable();
161   void createGuardCFTables();
162   void markSymbolsForRVATable(ObjFile *File,
163                               ArrayRef<SectionChunk *> SymIdxChunks,
164                               SymbolRVASet &TableSymbols);
165   void maybeAddRVATable(SymbolRVASet TableSymbols, StringRef TableSym,
166                         StringRef CountSym);
167   void setSectionPermissions();
168   void writeSections();
169   void writeBuildId();
170   void sortExceptionTable();
171 
172   llvm::Optional<coff_symbol16> createSymbol(Defined *D);
173   size_t addEntryToStringTable(StringRef Str);
174 
175   OutputSection *findSection(StringRef Name);
176   void addBaserels();
177   void addBaserelBlocks(std::vector<Baserel> &V);
178 
179   uint32_t getSizeOfInitializedData();
180   std::map<StringRef, std::vector<DefinedImportData *>> binImports();
181 
182   std::unique_ptr<FileOutputBuffer> &Buffer;
183   std::vector<OutputSection *> OutputSections;
184   std::vector<char> Strtab;
185   std::vector<llvm::object::coff_symbol16> OutputSymtab;
186   IdataContents Idata;
187   DelayLoadContents DelayIdata;
188   EdataContents Edata;
189   bool SetNoSEHCharacteristic = false;
190 
191   DebugDirectoryChunk *DebugDirectory = nullptr;
192   std::vector<Chunk *> DebugRecords;
193   CVDebugRecordChunk *BuildId = nullptr;
194   Optional<codeview::DebugInfo> PreviousBuildId;
195   ArrayRef<uint8_t> SectionTable;
196 
197   uint64_t FileSize;
198   uint32_t PointerToSymbolTable = 0;
199   uint64_t SizeOfImage;
200   uint64_t SizeOfHeaders;
201 
202   OutputSection *TextSec;
203   OutputSection *RdataSec;
204   OutputSection *BuildidSec;
205   OutputSection *DataSec;
206   OutputSection *PdataSec;
207   OutputSection *IdataSec;
208   OutputSection *EdataSec;
209   OutputSection *DidatSec;
210   OutputSection *RsrcSec;
211   OutputSection *RelocSec;
212 
213   // The first and last .pdata sections in the output file.
214   //
215   // We need to keep track of the location of .pdata in whichever section it
216   // gets merged into so that we can sort its contents and emit a correct data
217   // directory entry for the exception table. This is also the case for some
218   // other sections (such as .edata) but because the contents of those sections
219   // are entirely linker-generated we can keep track of their locations using
220   // the chunks that the linker creates. All .pdata chunks come from input
221   // files, so we need to keep track of them separately.
222   Chunk *FirstPdata = nullptr;
223   Chunk *LastPdata;
224 };
225 } // anonymous namespace
226 
227 namespace lld {
228 namespace coff {
229 
230 static Timer CodeLayoutTimer("Code Layout", Timer::root());
231 static Timer DiskCommitTimer("Commit Output File", Timer::root());
232 
writeResult()233 void writeResult() { Writer().run(); }
234 
addChunk(Chunk * C)235 void OutputSection::addChunk(Chunk *C) {
236   Chunks.push_back(C);
237   C->setOutputSection(this);
238 }
239 
setPermissions(uint32_t C)240 void OutputSection::setPermissions(uint32_t C) {
241   Header.Characteristics &= ~PermMask;
242   Header.Characteristics |= C;
243 }
244 
merge(OutputSection * Other)245 void OutputSection::merge(OutputSection *Other) {
246   for (Chunk *C : Other->Chunks)
247     C->setOutputSection(this);
248   Chunks.insert(Chunks.end(), Other->Chunks.begin(), Other->Chunks.end());
249   Other->Chunks.clear();
250 }
251 
252 // Write the section header to a given buffer.
writeHeaderTo(uint8_t * Buf)253 void OutputSection::writeHeaderTo(uint8_t *Buf) {
254   auto *Hdr = reinterpret_cast<coff_section *>(Buf);
255   *Hdr = Header;
256   if (StringTableOff) {
257     // If name is too long, write offset into the string table as a name.
258     sprintf(Hdr->Name, "/%d", StringTableOff);
259   } else {
260     assert(!Config->Debug || Name.size() <= COFF::NameSize ||
261            (Hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0);
262     strncpy(Hdr->Name, Name.data(),
263             std::min(Name.size(), (size_t)COFF::NameSize));
264   }
265 }
266 
267 } // namespace coff
268 } // namespace lld
269 
270 // PDBs are matched against executables using a build id which consists of three
271 // components:
272 //   1. A 16-bit GUID
273 //   2. An age
274 //   3. A time stamp.
275 //
276 // Debuggers and symbol servers match executables against debug info by checking
277 // each of these components of the EXE/DLL against the corresponding value in
278 // the PDB and failing a match if any of the components differ.  In the case of
279 // symbol servers, symbols are cached in a folder that is a function of the
280 // GUID.  As a result, in order to avoid symbol cache pollution where every
281 // incremental build copies a new PDB to the symbol cache, we must try to re-use
282 // the existing GUID if one exists, but bump the age.  This way the match will
283 // fail, so the symbol cache knows to use the new PDB, but the GUID matches, so
284 // it overwrites the existing item in the symbol cache rather than making a new
285 // one.
loadExistingBuildId(StringRef Path)286 static Optional<codeview::DebugInfo> loadExistingBuildId(StringRef Path) {
287   // We don't need to incrementally update a previous build id if we're not
288   // writing codeview debug info.
289   if (!Config->Debug)
290     return None;
291 
292   auto ExpectedBinary = llvm::object::createBinary(Path);
293   if (!ExpectedBinary) {
294     consumeError(ExpectedBinary.takeError());
295     return None;
296   }
297 
298   auto Binary = std::move(*ExpectedBinary);
299   if (!Binary.getBinary()->isCOFF())
300     return None;
301 
302   std::error_code EC;
303   COFFObjectFile File(Binary.getBinary()->getMemoryBufferRef(), EC);
304   if (EC)
305     return None;
306 
307   // If the machine of the binary we're outputting doesn't match the machine
308   // of the existing binary, don't try to re-use the build id.
309   if (File.is64() != Config->is64() || File.getMachine() != Config->Machine)
310     return None;
311 
312   for (const auto &DebugDir : File.debug_directories()) {
313     if (DebugDir.Type != IMAGE_DEBUG_TYPE_CODEVIEW)
314       continue;
315 
316     const codeview::DebugInfo *ExistingDI = nullptr;
317     StringRef PDBFileName;
318     if (auto EC = File.getDebugPDBInfo(ExistingDI, PDBFileName)) {
319       (void)EC;
320       return None;
321     }
322     // We only support writing PDBs in v70 format.  So if this is not a build
323     // id that we recognize / support, ignore it.
324     if (ExistingDI->Signature.CVSignature != OMF::Signature::PDB70)
325       return None;
326     return *ExistingDI;
327   }
328   return None;
329 }
330 
331 // The main function of the writer.
run()332 void Writer::run() {
333   ScopedTimer T1(CodeLayoutTimer);
334 
335   createSections();
336   createMiscChunks();
337   createImportTables();
338   createExportTable();
339   mergeSections();
340   assignAddresses();
341   removeEmptySections();
342   setSectionPermissions();
343   createSymbolAndStringTable();
344 
345   if (FileSize > UINT32_MAX)
346     fatal("image size (" + Twine(FileSize) + ") " +
347         "exceeds maximum allowable size (" + Twine(UINT32_MAX) + ")");
348 
349   // We must do this before opening the output file, as it depends on being able
350   // to read the contents of the existing output file.
351   PreviousBuildId = loadExistingBuildId(Config->OutputFile);
352   openFile(Config->OutputFile);
353   if (Config->is64()) {
354     writeHeader<pe32plus_header>();
355   } else {
356     writeHeader<pe32_header>();
357   }
358   writeSections();
359   sortExceptionTable();
360   writeBuildId();
361 
362   T1.stop();
363 
364   if (!Config->PDBPath.empty() && Config->Debug) {
365     assert(BuildId);
366     createPDB(Symtab, OutputSections, SectionTable, *BuildId->BuildId);
367   }
368 
369   writeMapFile(OutputSections);
370 
371   ScopedTimer T2(DiskCommitTimer);
372   if (auto E = Buffer->commit())
373     fatal("failed to write the output file: " + toString(std::move(E)));
374 }
375 
getOutputSectionName(StringRef Name)376 static StringRef getOutputSectionName(StringRef Name) {
377   StringRef S = Name.split('$').first;
378 
379   // Treat a later period as a separator for MinGW, for sections like
380   // ".ctors.01234".
381   return S.substr(0, S.find('.', 1));
382 }
383 
384 // For /order.
sortBySectionOrder(std::vector<Chunk * > & Chunks)385 static void sortBySectionOrder(std::vector<Chunk *> &Chunks) {
386   auto GetPriority = [](const Chunk *C) {
387     if (auto *Sec = dyn_cast<SectionChunk>(C))
388       if (Sec->Sym)
389         return Config->Order.lookup(Sec->Sym->getName());
390     return 0;
391   };
392 
393   std::stable_sort(Chunks.begin(), Chunks.end(),
394                    [=](const Chunk *A, const Chunk *B) {
395                      return GetPriority(A) < GetPriority(B);
396                    });
397 }
398 
399 // Create output section objects and add them to OutputSections.
createSections()400 void Writer::createSections() {
401   // First, create the builtin sections.
402   const uint32_t DATA = IMAGE_SCN_CNT_INITIALIZED_DATA;
403   const uint32_t BSS = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
404   const uint32_t CODE = IMAGE_SCN_CNT_CODE;
405   const uint32_t DISCARDABLE = IMAGE_SCN_MEM_DISCARDABLE;
406   const uint32_t R = IMAGE_SCN_MEM_READ;
407   const uint32_t W = IMAGE_SCN_MEM_WRITE;
408   const uint32_t X = IMAGE_SCN_MEM_EXECUTE;
409 
410   SmallDenseMap<std::pair<StringRef, uint32_t>, OutputSection *> Sections;
411   auto CreateSection = [&](StringRef Name, uint32_t OutChars) {
412     OutputSection *&Sec = Sections[{Name, OutChars}];
413     if (!Sec) {
414       Sec = make<OutputSection>(Name, OutChars);
415       OutputSections.push_back(Sec);
416     }
417     return Sec;
418   };
419 
420   // Try to match the section order used by link.exe.
421   TextSec = CreateSection(".text", CODE | R | X);
422   CreateSection(".bss", BSS | R | W);
423   RdataSec = CreateSection(".rdata", DATA | R);
424   BuildidSec = CreateSection(".buildid", DATA | R);
425   DataSec = CreateSection(".data", DATA | R | W);
426   PdataSec = CreateSection(".pdata", DATA | R);
427   IdataSec = CreateSection(".idata", DATA | R);
428   EdataSec = CreateSection(".edata", DATA | R);
429   DidatSec = CreateSection(".didat", DATA | R);
430   RsrcSec = CreateSection(".rsrc", DATA | R);
431   RelocSec = CreateSection(".reloc", DATA | DISCARDABLE | R);
432 
433   // Then bin chunks by name and output characteristics.
434   std::map<std::pair<StringRef, uint32_t>, std::vector<Chunk *>> Map;
435   for (Chunk *C : Symtab->getChunks()) {
436     auto *SC = dyn_cast<SectionChunk>(C);
437     if (SC && !SC->isLive()) {
438       if (Config->Verbose)
439         SC->printDiscardedMessage();
440       continue;
441     }
442     Map[{C->getSectionName(), C->getOutputCharacteristics()}].push_back(C);
443   }
444 
445   // Process an /order option.
446   if (!Config->Order.empty())
447     for (auto &Pair : Map)
448       sortBySectionOrder(Pair.second);
449 
450   // Then create an OutputSection for each section.
451   // '$' and all following characters in input section names are
452   // discarded when determining output section. So, .text$foo
453   // contributes to .text, for example. See PE/COFF spec 3.2.
454   for (auto Pair : Map) {
455     StringRef Name = getOutputSectionName(Pair.first.first);
456     uint32_t OutChars = Pair.first.second;
457 
458     // In link.exe, there is a special case for the I386 target where .CRT
459     // sections are treated as if they have output characteristics DATA | R if
460     // their characteristics are DATA | R | W. This implements the same special
461     // case for all architectures.
462     if (Name == ".CRT")
463       OutChars = DATA | R;
464 
465     OutputSection *Sec = CreateSection(Name, OutChars);
466     std::vector<Chunk *> &Chunks = Pair.second;
467     for (Chunk *C : Chunks)
468       Sec->addChunk(C);
469   }
470 
471   // Finally, move some output sections to the end.
472   auto SectionOrder = [&](OutputSection *S) {
473     // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because
474     // the loader cannot handle holes. Stripping can remove other discardable ones
475     // than .reloc, which is first of them (created early).
476     if (S->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
477       return 2;
478     // .rsrc should come at the end of the non-discardable sections because its
479     // size may change by the Win32 UpdateResources() function, causing
480     // subsequent sections to move (see https://crbug.com/827082).
481     if (S == RsrcSec)
482       return 1;
483     return 0;
484   };
485   std::stable_sort(OutputSections.begin(), OutputSections.end(),
486                    [&](OutputSection *S, OutputSection *T) {
487                      return SectionOrder(S) < SectionOrder(T);
488                    });
489 }
490 
createMiscChunks()491 void Writer::createMiscChunks() {
492   for (auto &P : MergeChunk::Instances)
493     RdataSec->addChunk(P.second);
494 
495   // Create thunks for locally-dllimported symbols.
496   if (!Symtab->LocalImportChunks.empty()) {
497     for (Chunk *C : Symtab->LocalImportChunks)
498       RdataSec->addChunk(C);
499   }
500 
501   // Create Debug Information Chunks
502   if (Config->Debug) {
503     DebugDirectory = make<DebugDirectoryChunk>(DebugRecords);
504 
505     OutputSection *DebugInfoSec = Config->MinGW ? BuildidSec : RdataSec;
506 
507     // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified.  We
508     // output a PDB no matter what, and this chunk provides the only means of
509     // allowing a debugger to match a PDB and an executable.  So we need it even
510     // if we're ultimately not going to write CodeView data to the PDB.
511     auto *CVChunk = make<CVDebugRecordChunk>();
512     BuildId = CVChunk;
513     DebugRecords.push_back(CVChunk);
514 
515     DebugInfoSec->addChunk(DebugDirectory);
516     for (Chunk *C : DebugRecords)
517       DebugInfoSec->addChunk(C);
518   }
519 
520   // Create SEH table. x86-only.
521   if (Config->Machine == I386)
522     createSEHTable();
523 
524   // Create /guard:cf tables if requested.
525   if (Config->GuardCF != GuardCFLevel::Off)
526     createGuardCFTables();
527 }
528 
529 // Create .idata section for the DLL-imported symbol table.
530 // The format of this section is inherently Windows-specific.
531 // IdataContents class abstracted away the details for us,
532 // so we just let it create chunks and add them to the section.
createImportTables()533 void Writer::createImportTables() {
534   if (ImportFile::Instances.empty())
535     return;
536 
537   // Initialize DLLOrder so that import entries are ordered in
538   // the same order as in the command line. (That affects DLL
539   // initialization order, and this ordering is MSVC-compatible.)
540   for (ImportFile *File : ImportFile::Instances) {
541     if (!File->Live)
542       continue;
543 
544     std::string DLL = StringRef(File->DLLName).lower();
545     if (Config->DLLOrder.count(DLL) == 0)
546       Config->DLLOrder[DLL] = Config->DLLOrder.size();
547 
548     if (File->ThunkSym) {
549       if (!isa<DefinedImportThunk>(File->ThunkSym))
550         fatal(toString(*File->ThunkSym) + " was replaced");
551       DefinedImportThunk *Thunk = cast<DefinedImportThunk>(File->ThunkSym);
552       if (File->ThunkLive)
553         TextSec->addChunk(Thunk->getChunk());
554     }
555 
556     if (File->ImpSym && !isa<DefinedImportData>(File->ImpSym))
557       fatal(toString(*File->ImpSym) + " was replaced");
558     DefinedImportData *ImpSym = cast_or_null<DefinedImportData>(File->ImpSym);
559     if (Config->DelayLoads.count(StringRef(File->DLLName).lower())) {
560       if (!File->ThunkSym)
561         fatal("cannot delay-load " + toString(File) +
562               " due to import of data: " + toString(*ImpSym));
563       DelayIdata.add(ImpSym);
564     } else {
565       Idata.add(ImpSym);
566     }
567   }
568 
569   if (!Idata.empty())
570     for (Chunk *C : Idata.getChunks())
571       IdataSec->addChunk(C);
572 
573   if (!DelayIdata.empty()) {
574     Defined *Helper = cast<Defined>(Config->DelayLoadHelper);
575     DelayIdata.create(Helper);
576     for (Chunk *C : DelayIdata.getChunks())
577       DidatSec->addChunk(C);
578     for (Chunk *C : DelayIdata.getDataChunks())
579       DataSec->addChunk(C);
580     for (Chunk *C : DelayIdata.getCodeChunks())
581       TextSec->addChunk(C);
582   }
583 }
584 
createExportTable()585 void Writer::createExportTable() {
586   if (Config->Exports.empty())
587     return;
588   for (Chunk *C : Edata.Chunks)
589     EdataSec->addChunk(C);
590 }
591 
592 // The Windows loader doesn't seem to like empty sections,
593 // so we remove them if any.
removeEmptySections()594 void Writer::removeEmptySections() {
595   auto IsEmpty = [](OutputSection *S) { return S->getVirtualSize() == 0; };
596   OutputSections.erase(
597       std::remove_if(OutputSections.begin(), OutputSections.end(), IsEmpty),
598       OutputSections.end());
599   uint32_t Idx = 1;
600   for (OutputSection *Sec : OutputSections)
601     Sec->SectionIndex = Idx++;
602 }
603 
addEntryToStringTable(StringRef Str)604 size_t Writer::addEntryToStringTable(StringRef Str) {
605   assert(Str.size() > COFF::NameSize);
606   size_t OffsetOfEntry = Strtab.size() + 4; // +4 for the size field
607   Strtab.insert(Strtab.end(), Str.begin(), Str.end());
608   Strtab.push_back('\0');
609   return OffsetOfEntry;
610 }
611 
createSymbol(Defined * Def)612 Optional<coff_symbol16> Writer::createSymbol(Defined *Def) {
613   coff_symbol16 Sym;
614   switch (Def->kind()) {
615   case Symbol::DefinedAbsoluteKind:
616     Sym.Value = Def->getRVA();
617     Sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
618     break;
619   case Symbol::DefinedSyntheticKind:
620     // Relative symbols are unrepresentable in a COFF symbol table.
621     return None;
622   default: {
623     // Don't write symbols that won't be written to the output to the symbol
624     // table.
625     Chunk *C = Def->getChunk();
626     if (!C)
627       return None;
628     OutputSection *OS = C->getOutputSection();
629     if (!OS)
630       return None;
631 
632     Sym.Value = Def->getRVA() - OS->getRVA();
633     Sym.SectionNumber = OS->SectionIndex;
634     break;
635   }
636   }
637 
638   StringRef Name = Def->getName();
639   if (Name.size() > COFF::NameSize) {
640     Sym.Name.Offset.Zeroes = 0;
641     Sym.Name.Offset.Offset = addEntryToStringTable(Name);
642   } else {
643     memset(Sym.Name.ShortName, 0, COFF::NameSize);
644     memcpy(Sym.Name.ShortName, Name.data(), Name.size());
645   }
646 
647   if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
648     COFFSymbolRef Ref = D->getCOFFSymbol();
649     Sym.Type = Ref.getType();
650     Sym.StorageClass = Ref.getStorageClass();
651   } else {
652     Sym.Type = IMAGE_SYM_TYPE_NULL;
653     Sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
654   }
655   Sym.NumberOfAuxSymbols = 0;
656   return Sym;
657 }
658 
createSymbolAndStringTable()659 void Writer::createSymbolAndStringTable() {
660   // PE/COFF images are limited to 8 byte section names. Longer names can be
661   // supported by writing a non-standard string table, but this string table is
662   // not mapped at runtime and the long names will therefore be inaccessible.
663   // link.exe always truncates section names to 8 bytes, whereas binutils always
664   // preserves long section names via the string table. LLD adopts a hybrid
665   // solution where discardable sections have long names preserved and
666   // non-discardable sections have their names truncated, to ensure that any
667   // section which is mapped at runtime also has its name mapped at runtime.
668   for (OutputSection *Sec : OutputSections) {
669     if (Sec->Name.size() <= COFF::NameSize)
670       continue;
671     if ((Sec->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0)
672       continue;
673     Sec->setStringTableOff(addEntryToStringTable(Sec->Name));
674   }
675 
676   if (Config->DebugDwarf || Config->DebugSymtab) {
677     for (ObjFile *File : ObjFile::Instances) {
678       for (Symbol *B : File->getSymbols()) {
679         auto *D = dyn_cast_or_null<Defined>(B);
680         if (!D || D->WrittenToSymtab)
681           continue;
682         D->WrittenToSymtab = true;
683 
684         if (Optional<coff_symbol16> Sym = createSymbol(D))
685           OutputSymtab.push_back(*Sym);
686       }
687     }
688   }
689 
690   if (OutputSymtab.empty() && Strtab.empty())
691     return;
692 
693   // We position the symbol table to be adjacent to the end of the last section.
694   uint64_t FileOff = FileSize;
695   PointerToSymbolTable = FileOff;
696   FileOff += OutputSymtab.size() * sizeof(coff_symbol16);
697   FileOff += 4 + Strtab.size();
698   FileSize = alignTo(FileOff, SectorSize);
699 }
700 
mergeSections()701 void Writer::mergeSections() {
702   if (!PdataSec->getChunks().empty()) {
703     FirstPdata = PdataSec->getChunks().front();
704     LastPdata = PdataSec->getChunks().back();
705   }
706 
707   for (auto &P : Config->Merge) {
708     StringRef ToName = P.second;
709     if (P.first == ToName)
710       continue;
711     StringSet<> Names;
712     while (1) {
713       if (!Names.insert(ToName).second)
714         fatal("/merge: cycle found for section '" + P.first + "'");
715       auto I = Config->Merge.find(ToName);
716       if (I == Config->Merge.end())
717         break;
718       ToName = I->second;
719     }
720     OutputSection *From = findSection(P.first);
721     OutputSection *To = findSection(ToName);
722     if (!From)
723       continue;
724     if (!To) {
725       From->Name = ToName;
726       continue;
727     }
728     To->merge(From);
729   }
730 }
731 
732 // Visits all sections to assign incremental, non-overlapping RVAs and
733 // file offsets.
assignAddresses()734 void Writer::assignAddresses() {
735   SizeOfHeaders = DOSStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
736                   sizeof(data_directory) * NumberfOfDataDirectory +
737                   sizeof(coff_section) * OutputSections.size();
738   SizeOfHeaders +=
739       Config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
740   SizeOfHeaders = alignTo(SizeOfHeaders, SectorSize);
741   uint64_t RVA = PageSize; // The first page is kept unmapped.
742   FileSize = SizeOfHeaders;
743 
744   for (OutputSection *Sec : OutputSections) {
745     if (Sec == RelocSec)
746       addBaserels();
747     uint64_t RawSize = 0, VirtualSize = 0;
748     Sec->Header.VirtualAddress = RVA;
749     for (Chunk *C : Sec->getChunks()) {
750       VirtualSize = alignTo(VirtualSize, C->Alignment);
751       C->setRVA(RVA + VirtualSize);
752       C->OutputSectionOff = VirtualSize;
753       C->finalizeContents();
754       VirtualSize += C->getSize();
755       if (C->hasData())
756         RawSize = alignTo(VirtualSize, SectorSize);
757     }
758     if (VirtualSize > UINT32_MAX)
759       error("section larger than 4 GiB: " + Sec->Name);
760     Sec->Header.VirtualSize = VirtualSize;
761     Sec->Header.SizeOfRawData = RawSize;
762     if (RawSize != 0)
763       Sec->Header.PointerToRawData = FileSize;
764     RVA += alignTo(VirtualSize, PageSize);
765     FileSize += alignTo(RawSize, SectorSize);
766   }
767   SizeOfImage = alignTo(RVA, PageSize);
768 }
769 
writeHeader()770 template <typename PEHeaderTy> void Writer::writeHeader() {
771   // Write DOS header. For backwards compatibility, the first part of a PE/COFF
772   // executable consists of an MS-DOS MZ executable. If the executable is run
773   // under DOS, that program gets run (usually to just print an error message).
774   // When run under Windows, the loader looks at AddressOfNewExeHeader and uses
775   // the PE header instead.
776   uint8_t *Buf = Buffer->getBufferStart();
777   auto *DOS = reinterpret_cast<dos_header *>(Buf);
778   Buf += sizeof(dos_header);
779   DOS->Magic[0] = 'M';
780   DOS->Magic[1] = 'Z';
781   DOS->UsedBytesInTheLastPage = DOSStubSize % 512;
782   DOS->FileSizeInPages = divideCeil(DOSStubSize, 512);
783   DOS->HeaderSizeInParagraphs = sizeof(dos_header) / 16;
784 
785   DOS->AddressOfRelocationTable = sizeof(dos_header);
786   DOS->AddressOfNewExeHeader = DOSStubSize;
787 
788   // Write DOS program.
789   memcpy(Buf, DOSProgram, sizeof(DOSProgram));
790   Buf += sizeof(DOSProgram);
791 
792   // Write PE magic
793   memcpy(Buf, PEMagic, sizeof(PEMagic));
794   Buf += sizeof(PEMagic);
795 
796   // Write COFF header
797   auto *COFF = reinterpret_cast<coff_file_header *>(Buf);
798   Buf += sizeof(*COFF);
799   COFF->Machine = Config->Machine;
800   COFF->NumberOfSections = OutputSections.size();
801   COFF->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
802   if (Config->LargeAddressAware)
803     COFF->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
804   if (!Config->is64())
805     COFF->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
806   if (Config->DLL)
807     COFF->Characteristics |= IMAGE_FILE_DLL;
808   if (!Config->Relocatable)
809     COFF->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
810   COFF->SizeOfOptionalHeader =
811       sizeof(PEHeaderTy) + sizeof(data_directory) * NumberfOfDataDirectory;
812 
813   // Write PE header
814   auto *PE = reinterpret_cast<PEHeaderTy *>(Buf);
815   Buf += sizeof(*PE);
816   PE->Magic = Config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
817 
818   // If {Major,Minor}LinkerVersion is left at 0.0, then for some
819   // reason signing the resulting PE file with Authenticode produces a
820   // signature that fails to validate on Windows 7 (but is OK on 10).
821   // Set it to 14.0, which is what VS2015 outputs, and which avoids
822   // that problem.
823   PE->MajorLinkerVersion = 14;
824   PE->MinorLinkerVersion = 0;
825 
826   PE->ImageBase = Config->ImageBase;
827   PE->SectionAlignment = PageSize;
828   PE->FileAlignment = SectorSize;
829   PE->MajorImageVersion = Config->MajorImageVersion;
830   PE->MinorImageVersion = Config->MinorImageVersion;
831   PE->MajorOperatingSystemVersion = Config->MajorOSVersion;
832   PE->MinorOperatingSystemVersion = Config->MinorOSVersion;
833   PE->MajorSubsystemVersion = Config->MajorOSVersion;
834   PE->MinorSubsystemVersion = Config->MinorOSVersion;
835   PE->Subsystem = Config->Subsystem;
836   PE->SizeOfImage = SizeOfImage;
837   PE->SizeOfHeaders = SizeOfHeaders;
838   if (!Config->NoEntry) {
839     Defined *Entry = cast<Defined>(Config->Entry);
840     PE->AddressOfEntryPoint = Entry->getRVA();
841     // Pointer to thumb code must have the LSB set, so adjust it.
842     if (Config->Machine == ARMNT)
843       PE->AddressOfEntryPoint |= 1;
844   }
845   PE->SizeOfStackReserve = Config->StackReserve;
846   PE->SizeOfStackCommit = Config->StackCommit;
847   PE->SizeOfHeapReserve = Config->HeapReserve;
848   PE->SizeOfHeapCommit = Config->HeapCommit;
849   if (Config->AppContainer)
850     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER;
851   if (Config->DynamicBase)
852     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
853   if (Config->HighEntropyVA)
854     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
855   if (!Config->AllowBind)
856     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND;
857   if (Config->NxCompat)
858     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
859   if (!Config->AllowIsolation)
860     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION;
861   if (Config->GuardCF != GuardCFLevel::Off)
862     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF;
863   if (Config->IntegrityCheck)
864     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
865   if (SetNoSEHCharacteristic)
866     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH;
867   if (Config->TerminalServerAware)
868     PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
869   PE->NumberOfRvaAndSize = NumberfOfDataDirectory;
870   if (TextSec->getVirtualSize()) {
871     PE->BaseOfCode = TextSec->getRVA();
872     PE->SizeOfCode = TextSec->getRawSize();
873   }
874   PE->SizeOfInitializedData = getSizeOfInitializedData();
875 
876   // Write data directory
877   auto *Dir = reinterpret_cast<data_directory *>(Buf);
878   Buf += sizeof(*Dir) * NumberfOfDataDirectory;
879   if (!Config->Exports.empty()) {
880     Dir[EXPORT_TABLE].RelativeVirtualAddress = Edata.getRVA();
881     Dir[EXPORT_TABLE].Size = Edata.getSize();
882   }
883   if (!Idata.empty()) {
884     Dir[IMPORT_TABLE].RelativeVirtualAddress = Idata.getDirRVA();
885     Dir[IMPORT_TABLE].Size = Idata.getDirSize();
886     Dir[IAT].RelativeVirtualAddress = Idata.getIATRVA();
887     Dir[IAT].Size = Idata.getIATSize();
888   }
889   if (RsrcSec->getVirtualSize()) {
890     Dir[RESOURCE_TABLE].RelativeVirtualAddress = RsrcSec->getRVA();
891     Dir[RESOURCE_TABLE].Size = RsrcSec->getVirtualSize();
892   }
893   if (FirstPdata) {
894     Dir[EXCEPTION_TABLE].RelativeVirtualAddress = FirstPdata->getRVA();
895     Dir[EXCEPTION_TABLE].Size =
896         LastPdata->getRVA() + LastPdata->getSize() - FirstPdata->getRVA();
897   }
898   if (RelocSec->getVirtualSize()) {
899     Dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = RelocSec->getRVA();
900     Dir[BASE_RELOCATION_TABLE].Size = RelocSec->getVirtualSize();
901   }
902   if (Symbol *Sym = Symtab->findUnderscore("_tls_used")) {
903     if (Defined *B = dyn_cast<Defined>(Sym)) {
904       Dir[TLS_TABLE].RelativeVirtualAddress = B->getRVA();
905       Dir[TLS_TABLE].Size = Config->is64()
906                                 ? sizeof(object::coff_tls_directory64)
907                                 : sizeof(object::coff_tls_directory32);
908     }
909   }
910   if (Config->Debug) {
911     Dir[DEBUG_DIRECTORY].RelativeVirtualAddress = DebugDirectory->getRVA();
912     Dir[DEBUG_DIRECTORY].Size = DebugDirectory->getSize();
913   }
914   if (Symbol *Sym = Symtab->findUnderscore("_load_config_used")) {
915     if (auto *B = dyn_cast<DefinedRegular>(Sym)) {
916       SectionChunk *SC = B->getChunk();
917       assert(B->getRVA() >= SC->getRVA());
918       uint64_t OffsetInChunk = B->getRVA() - SC->getRVA();
919       if (!SC->hasData() || OffsetInChunk + 4 > SC->getSize())
920         fatal("_load_config_used is malformed");
921 
922       ArrayRef<uint8_t> SecContents = SC->getContents();
923       uint32_t LoadConfigSize =
924           *reinterpret_cast<const ulittle32_t *>(&SecContents[OffsetInChunk]);
925       if (OffsetInChunk + LoadConfigSize > SC->getSize())
926         fatal("_load_config_used is too large");
927       Dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = B->getRVA();
928       Dir[LOAD_CONFIG_TABLE].Size = LoadConfigSize;
929     }
930   }
931   if (!DelayIdata.empty()) {
932     Dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
933         DelayIdata.getDirRVA();
934     Dir[DELAY_IMPORT_DESCRIPTOR].Size = DelayIdata.getDirSize();
935   }
936 
937   // Write section table
938   for (OutputSection *Sec : OutputSections) {
939     Sec->writeHeaderTo(Buf);
940     Buf += sizeof(coff_section);
941   }
942   SectionTable = ArrayRef<uint8_t>(
943       Buf - OutputSections.size() * sizeof(coff_section), Buf);
944 
945   if (OutputSymtab.empty() && Strtab.empty())
946     return;
947 
948   COFF->PointerToSymbolTable = PointerToSymbolTable;
949   uint32_t NumberOfSymbols = OutputSymtab.size();
950   COFF->NumberOfSymbols = NumberOfSymbols;
951   auto *SymbolTable = reinterpret_cast<coff_symbol16 *>(
952       Buffer->getBufferStart() + COFF->PointerToSymbolTable);
953   for (size_t I = 0; I != NumberOfSymbols; ++I)
954     SymbolTable[I] = OutputSymtab[I];
955   // Create the string table, it follows immediately after the symbol table.
956   // The first 4 bytes is length including itself.
957   Buf = reinterpret_cast<uint8_t *>(&SymbolTable[NumberOfSymbols]);
958   write32le(Buf, Strtab.size() + 4);
959   if (!Strtab.empty())
960     memcpy(Buf + 4, Strtab.data(), Strtab.size());
961 }
962 
openFile(StringRef Path)963 void Writer::openFile(StringRef Path) {
964   Buffer = CHECK(
965       FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable),
966       "failed to open " + Path);
967 }
968 
createSEHTable()969 void Writer::createSEHTable() {
970   // Set the no SEH characteristic on x86 binaries unless we find exception
971   // handlers.
972   SetNoSEHCharacteristic = true;
973 
974   SymbolRVASet Handlers;
975   for (ObjFile *File : ObjFile::Instances) {
976     // FIXME: We should error here instead of earlier unless /safeseh:no was
977     // passed.
978     if (!File->hasSafeSEH())
979       return;
980 
981     markSymbolsForRVATable(File, File->getSXDataChunks(), Handlers);
982   }
983 
984   // Remove the "no SEH" characteristic if all object files were built with
985   // safeseh, we found some exception handlers, and there is a load config in
986   // the object.
987   SetNoSEHCharacteristic =
988       Handlers.empty() || !Symtab->findUnderscore("_load_config_used");
989 
990   maybeAddRVATable(std::move(Handlers), "__safe_se_handler_table",
991                    "__safe_se_handler_count");
992 }
993 
994 // Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set
995 // cannot contain duplicates. Therefore, the set is uniqued by Chunk and the
996 // symbol's offset into that Chunk.
addSymbolToRVASet(SymbolRVASet & RVASet,Defined * S)997 static void addSymbolToRVASet(SymbolRVASet &RVASet, Defined *S) {
998   Chunk *C = S->getChunk();
999   if (auto *SC = dyn_cast<SectionChunk>(C))
1000     C = SC->Repl; // Look through ICF replacement.
1001   uint32_t Off = S->getRVA() - (C ? C->getRVA() : 0);
1002   RVASet.insert({C, Off});
1003 }
1004 
1005 // Visit all relocations from all section contributions of this object file and
1006 // mark the relocation target as address-taken.
markSymbolsWithRelocations(ObjFile * File,SymbolRVASet & UsedSymbols)1007 static void markSymbolsWithRelocations(ObjFile *File,
1008                                        SymbolRVASet &UsedSymbols) {
1009   for (Chunk *C : File->getChunks()) {
1010     // We only care about live section chunks. Common chunks and other chunks
1011     // don't generally contain relocations.
1012     SectionChunk *SC = dyn_cast<SectionChunk>(C);
1013     if (!SC || !SC->isLive())
1014       continue;
1015 
1016     // Look for relocations in this section against symbols in executable output
1017     // sections.
1018     for (Symbol *Ref : SC->symbols()) {
1019       // FIXME: Do further testing to see if the relocation type matters,
1020       // especially for 32-bit where taking the address of something usually
1021       // uses an absolute relocation instead of a relative one.
1022       if (auto *D = dyn_cast_or_null<Defined>(Ref)) {
1023         Chunk *RefChunk = D->getChunk();
1024         OutputSection *OS = RefChunk ? RefChunk->getOutputSection() : nullptr;
1025         if (OS && OS->Header.Characteristics & IMAGE_SCN_MEM_EXECUTE)
1026           addSymbolToRVASet(UsedSymbols, D);
1027       }
1028     }
1029   }
1030 }
1031 
1032 // Create the guard function id table. This is a table of RVAs of all
1033 // address-taken functions. It is sorted and uniqued, just like the safe SEH
1034 // table.
createGuardCFTables()1035 void Writer::createGuardCFTables() {
1036   SymbolRVASet AddressTakenSyms;
1037   SymbolRVASet LongJmpTargets;
1038   for (ObjFile *File : ObjFile::Instances) {
1039     // If the object was compiled with /guard:cf, the address taken symbols
1040     // are in .gfids$y sections, and the longjmp targets are in .gljmp$y
1041     // sections. If the object was not compiled with /guard:cf, we assume there
1042     // were no setjmp targets, and that all code symbols with relocations are
1043     // possibly address-taken.
1044     if (File->hasGuardCF()) {
1045       markSymbolsForRVATable(File, File->getGuardFidChunks(), AddressTakenSyms);
1046       markSymbolsForRVATable(File, File->getGuardLJmpChunks(), LongJmpTargets);
1047     } else {
1048       markSymbolsWithRelocations(File, AddressTakenSyms);
1049     }
1050   }
1051 
1052   // Mark the image entry as address-taken.
1053   if (Config->Entry)
1054     addSymbolToRVASet(AddressTakenSyms, cast<Defined>(Config->Entry));
1055 
1056   // Ensure sections referenced in the gfid table are 16-byte aligned.
1057   for (const ChunkAndOffset &C : AddressTakenSyms)
1058     if (C.InputChunk->Alignment < 16)
1059       C.InputChunk->Alignment = 16;
1060 
1061   maybeAddRVATable(std::move(AddressTakenSyms), "__guard_fids_table",
1062                    "__guard_fids_count");
1063 
1064   // Add the longjmp target table unless the user told us not to.
1065   if (Config->GuardCF == GuardCFLevel::Full)
1066     maybeAddRVATable(std::move(LongJmpTargets), "__guard_longjmp_table",
1067                      "__guard_longjmp_count");
1068 
1069   // Set __guard_flags, which will be used in the load config to indicate that
1070   // /guard:cf was enabled.
1071   uint32_t GuardFlags = uint32_t(coff_guard_flags::CFInstrumented) |
1072                         uint32_t(coff_guard_flags::HasFidTable);
1073   if (Config->GuardCF == GuardCFLevel::Full)
1074     GuardFlags |= uint32_t(coff_guard_flags::HasLongJmpTable);
1075   Symbol *FlagSym = Symtab->findUnderscore("__guard_flags");
1076   cast<DefinedAbsolute>(FlagSym)->setVA(GuardFlags);
1077 }
1078 
1079 // Take a list of input sections containing symbol table indices and add those
1080 // symbols to an RVA table. The challenge is that symbol RVAs are not known and
1081 // depend on the table size, so we can't directly build a set of integers.
markSymbolsForRVATable(ObjFile * File,ArrayRef<SectionChunk * > SymIdxChunks,SymbolRVASet & TableSymbols)1082 void Writer::markSymbolsForRVATable(ObjFile *File,
1083                                     ArrayRef<SectionChunk *> SymIdxChunks,
1084                                     SymbolRVASet &TableSymbols) {
1085   for (SectionChunk *C : SymIdxChunks) {
1086     // Skip sections discarded by linker GC. This comes up when a .gfids section
1087     // is associated with something like a vtable and the vtable is discarded.
1088     // In this case, the associated gfids section is discarded, and we don't
1089     // mark the virtual member functions as address-taken by the vtable.
1090     if (!C->isLive())
1091       continue;
1092 
1093     // Validate that the contents look like symbol table indices.
1094     ArrayRef<uint8_t> Data = C->getContents();
1095     if (Data.size() % 4 != 0) {
1096       warn("ignoring " + C->getSectionName() +
1097            " symbol table index section in object " + toString(File));
1098       continue;
1099     }
1100 
1101     // Read each symbol table index and check if that symbol was included in the
1102     // final link. If so, add it to the table symbol set.
1103     ArrayRef<ulittle32_t> SymIndices(
1104         reinterpret_cast<const ulittle32_t *>(Data.data()), Data.size() / 4);
1105     ArrayRef<Symbol *> ObjSymbols = File->getSymbols();
1106     for (uint32_t SymIndex : SymIndices) {
1107       if (SymIndex >= ObjSymbols.size()) {
1108         warn("ignoring invalid symbol table index in section " +
1109              C->getSectionName() + " in object " + toString(File));
1110         continue;
1111       }
1112       if (Symbol *S = ObjSymbols[SymIndex]) {
1113         if (S->isLive())
1114           addSymbolToRVASet(TableSymbols, cast<Defined>(S));
1115       }
1116     }
1117   }
1118 }
1119 
1120 // Replace the absolute table symbol with a synthetic symbol pointing to
1121 // TableChunk so that we can emit base relocations for it and resolve section
1122 // relative relocations.
maybeAddRVATable(SymbolRVASet TableSymbols,StringRef TableSym,StringRef CountSym)1123 void Writer::maybeAddRVATable(SymbolRVASet TableSymbols, StringRef TableSym,
1124                               StringRef CountSym) {
1125   if (TableSymbols.empty())
1126     return;
1127 
1128   RVATableChunk *TableChunk = make<RVATableChunk>(std::move(TableSymbols));
1129   RdataSec->addChunk(TableChunk);
1130 
1131   Symbol *T = Symtab->findUnderscore(TableSym);
1132   Symbol *C = Symtab->findUnderscore(CountSym);
1133   replaceSymbol<DefinedSynthetic>(T, T->getName(), TableChunk);
1134   cast<DefinedAbsolute>(C)->setVA(TableChunk->getSize() / 4);
1135 }
1136 
1137 // Handles /section options to allow users to overwrite
1138 // section attributes.
setSectionPermissions()1139 void Writer::setSectionPermissions() {
1140   for (auto &P : Config->Section) {
1141     StringRef Name = P.first;
1142     uint32_t Perm = P.second;
1143     for (OutputSection *Sec : OutputSections)
1144       if (Sec->Name == Name)
1145         Sec->setPermissions(Perm);
1146   }
1147 }
1148 
1149 // Write section contents to a mmap'ed file.
writeSections()1150 void Writer::writeSections() {
1151   // Record the number of sections to apply section index relocations
1152   // against absolute symbols. See applySecIdx in Chunks.cpp..
1153   DefinedAbsolute::NumOutputSections = OutputSections.size();
1154 
1155   uint8_t *Buf = Buffer->getBufferStart();
1156   for (OutputSection *Sec : OutputSections) {
1157     uint8_t *SecBuf = Buf + Sec->getFileOff();
1158     // Fill gaps between functions in .text with INT3 instructions
1159     // instead of leaving as NUL bytes (which can be interpreted as
1160     // ADD instructions).
1161     if (Sec->Header.Characteristics & IMAGE_SCN_CNT_CODE)
1162       memset(SecBuf, 0xCC, Sec->getRawSize());
1163     for_each(parallel::par, Sec->getChunks().begin(), Sec->getChunks().end(),
1164              [&](Chunk *C) { C->writeTo(SecBuf); });
1165   }
1166 }
1167 
writeBuildId()1168 void Writer::writeBuildId() {
1169   // There are two important parts to the build ID.
1170   // 1) If building with debug info, the COFF debug directory contains a
1171   //    timestamp as well as a Guid and Age of the PDB.
1172   // 2) In all cases, the PE COFF file header also contains a timestamp.
1173   // For reproducibility, instead of a timestamp we want to use a hash of the
1174   // binary, however when building with debug info the hash needs to take into
1175   // account the debug info, since it's possible to add blank lines to a file
1176   // which causes the debug info to change but not the generated code.
1177   //
1178   // To handle this, we first set the Guid and Age in the debug directory (but
1179   // only if we're doing a debug build).  Then, we hash the binary (thus causing
1180   // the hash to change if only the debug info changes, since the Age will be
1181   // different).  Finally, we write that hash into the debug directory (if
1182   // present) as well as the COFF file header (always).
1183   if (Config->Debug) {
1184     assert(BuildId && "BuildId is not set!");
1185     if (PreviousBuildId.hasValue()) {
1186       *BuildId->BuildId = *PreviousBuildId;
1187       BuildId->BuildId->PDB70.Age = BuildId->BuildId->PDB70.Age + 1;
1188     } else {
1189       BuildId->BuildId->Signature.CVSignature = OMF::Signature::PDB70;
1190       BuildId->BuildId->PDB70.Age = 1;
1191       llvm::getRandomBytes(BuildId->BuildId->PDB70.Signature, 16);
1192     }
1193   }
1194 
1195   // At this point the only fields in the COFF file which remain unset are the
1196   // "timestamp" in the COFF file header, and the ones in the coff debug
1197   // directory.  Now we can hash the file and write that hash to the various
1198   // timestamp fields in the file.
1199   StringRef OutputFileData(
1200       reinterpret_cast<const char *>(Buffer->getBufferStart()),
1201       Buffer->getBufferSize());
1202 
1203   uint32_t Timestamp = Config->Timestamp;
1204   if (Config->Repro)
1205     Timestamp = static_cast<uint32_t>(xxHash64(OutputFileData));
1206 
1207   if (DebugDirectory)
1208     DebugDirectory->setTimeDateStamp(Timestamp);
1209 
1210   uint8_t *Buf = Buffer->getBufferStart();
1211   Buf += DOSStubSize + sizeof(PEMagic);
1212   object::coff_file_header *CoffHeader =
1213       reinterpret_cast<coff_file_header *>(Buf);
1214   CoffHeader->TimeDateStamp = Timestamp;
1215 }
1216 
1217 // Sort .pdata section contents according to PE/COFF spec 5.5.
sortExceptionTable()1218 void Writer::sortExceptionTable() {
1219   if (!FirstPdata)
1220     return;
1221   // We assume .pdata contains function table entries only.
1222   auto BufAddr = [&](Chunk *C) {
1223     return Buffer->getBufferStart() + C->getOutputSection()->getFileOff() +
1224            C->getRVA() - C->getOutputSection()->getRVA();
1225   };
1226   uint8_t *Begin = BufAddr(FirstPdata);
1227   uint8_t *End = BufAddr(LastPdata) + LastPdata->getSize();
1228   if (Config->Machine == AMD64) {
1229     struct Entry { ulittle32_t Begin, End, Unwind; };
1230     sort(parallel::par, (Entry *)Begin, (Entry *)End,
1231          [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
1232     return;
1233   }
1234   if (Config->Machine == ARMNT || Config->Machine == ARM64) {
1235     struct Entry { ulittle32_t Begin, Unwind; };
1236     sort(parallel::par, (Entry *)Begin, (Entry *)End,
1237          [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
1238     return;
1239   }
1240   errs() << "warning: don't know how to handle .pdata.\n";
1241 }
1242 
findSection(StringRef Name)1243 OutputSection *Writer::findSection(StringRef Name) {
1244   for (OutputSection *Sec : OutputSections)
1245     if (Sec->Name == Name)
1246       return Sec;
1247   return nullptr;
1248 }
1249 
getSizeOfInitializedData()1250 uint32_t Writer::getSizeOfInitializedData() {
1251   uint32_t Res = 0;
1252   for (OutputSection *S : OutputSections)
1253     if (S->Header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
1254       Res += S->getRawSize();
1255   return Res;
1256 }
1257 
1258 // Add base relocations to .reloc section.
addBaserels()1259 void Writer::addBaserels() {
1260   if (!Config->Relocatable)
1261     return;
1262   std::vector<Baserel> V;
1263   for (OutputSection *Sec : OutputSections) {
1264     if (Sec->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
1265       continue;
1266     // Collect all locations for base relocations.
1267     for (Chunk *C : Sec->getChunks())
1268       C->getBaserels(&V);
1269     // Add the addresses to .reloc section.
1270     if (!V.empty())
1271       addBaserelBlocks(V);
1272     V.clear();
1273   }
1274 }
1275 
1276 // Add addresses to .reloc section. Note that addresses are grouped by page.
addBaserelBlocks(std::vector<Baserel> & V)1277 void Writer::addBaserelBlocks(std::vector<Baserel> &V) {
1278   const uint32_t Mask = ~uint32_t(PageSize - 1);
1279   uint32_t Page = V[0].RVA & Mask;
1280   size_t I = 0, J = 1;
1281   for (size_t E = V.size(); J < E; ++J) {
1282     uint32_t P = V[J].RVA & Mask;
1283     if (P == Page)
1284       continue;
1285     RelocSec->addChunk(make<BaserelChunk>(Page, &V[I], &V[0] + J));
1286     I = J;
1287     Page = P;
1288   }
1289   if (I == J)
1290     return;
1291   RelocSec->addChunk(make<BaserelChunk>(Page, &V[I], &V[0] + J));
1292 }
1293