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

source code of lld/COFF/Writer.cpp