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