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/MC/StringTableBuilder.h"
28#include "llvm/Support/Endian.h"
29#include "llvm/Support/FileOutputBuffer.h"
30#include "llvm/Support/FormatAdapters.h"
31#include "llvm/Support/FormatVariadic.h"
32#include "llvm/Support/Parallel.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");
79static_assert((sizeof(dos_header) + sizeof(dosProgram)) % 8 == 0,
80 "DOSStub size must be multiple of 8");
81
82static const int numberOfDataDirectory = 16;
83
84namespace {
85
86class DebugDirectoryChunk : public NonSectionChunk {
87public:
88 DebugDirectoryChunk(const COFFLinkerContext &c,
89 const std::vector<std::pair<COFF::DebugType, Chunk *>> &r,
90 bool writeRepro)
91 : records(r), writeRepro(writeRepro), ctx(c) {}
92
93 size_t getSize() const override {
94 return (records.size() + int(writeRepro)) * sizeof(debug_directory);
95 }
96
97 void writeTo(uint8_t *b) const override {
98 auto *d = reinterpret_cast<debug_directory *>(b);
99
100 for (const std::pair<COFF::DebugType, Chunk *>& record : records) {
101 Chunk *c = record.second;
102 const OutputSection *os = ctx.getOutputSection(c);
103 uint64_t offs = os->getFileOff() + (c->getRVA() - os->getRVA());
104 fillEntry(d, debugType: record.first, size: c->getSize(), rva: c->getRVA(), offs);
105 ++d;
106 }
107
108 if (writeRepro) {
109 // FIXME: The COFF spec allows either a 0-sized entry to just say
110 // "the timestamp field is really a hash", or a 4-byte size field
111 // followed by that many bytes containing a longer hash (with the
112 // lowest 4 bytes usually being the timestamp in little-endian order).
113 // Consider storing the full 8 bytes computed by xxh3_64bits here.
114 fillEntry(d, debugType: COFF::IMAGE_DEBUG_TYPE_REPRO, size: 0, rva: 0, offs: 0);
115 }
116 }
117
118 void setTimeDateStamp(uint32_t timeDateStamp) {
119 for (support::ulittle32_t *tds : timeDateStamps)
120 *tds = timeDateStamp;
121 }
122
123private:
124 void fillEntry(debug_directory *d, COFF::DebugType debugType, size_t size,
125 uint64_t rva, uint64_t offs) const {
126 d->Characteristics = 0;
127 d->TimeDateStamp = 0;
128 d->MajorVersion = 0;
129 d->MinorVersion = 0;
130 d->Type = debugType;
131 d->SizeOfData = size;
132 d->AddressOfRawData = rva;
133 d->PointerToRawData = offs;
134
135 timeDateStamps.push_back(x: &d->TimeDateStamp);
136 }
137
138 mutable std::vector<support::ulittle32_t *> timeDateStamps;
139 const std::vector<std::pair<COFF::DebugType, Chunk *>> &records;
140 bool writeRepro;
141 const COFFLinkerContext &ctx;
142};
143
144class CVDebugRecordChunk : public NonSectionChunk {
145public:
146 CVDebugRecordChunk(const COFFLinkerContext &c) : ctx(c) {}
147
148 size_t getSize() const override {
149 return sizeof(codeview::DebugInfo) + ctx.config.pdbAltPath.size() + 1;
150 }
151
152 void writeTo(uint8_t *b) const override {
153 // Save off the DebugInfo entry to backfill the file signature (build id)
154 // in Writer::writeBuildId
155 buildId = reinterpret_cast<codeview::DebugInfo *>(b);
156
157 // variable sized field (PDB Path)
158 char *p = reinterpret_cast<char *>(b + sizeof(*buildId));
159 if (!ctx.config.pdbAltPath.empty())
160 memcpy(dest: p, src: ctx.config.pdbAltPath.data(), n: ctx.config.pdbAltPath.size());
161 p[ctx.config.pdbAltPath.size()] = '\0';
162 }
163
164 mutable codeview::DebugInfo *buildId = nullptr;
165
166private:
167 const COFFLinkerContext &ctx;
168};
169
170class ExtendedDllCharacteristicsChunk : public NonSectionChunk {
171public:
172 ExtendedDllCharacteristicsChunk(uint32_t c) : characteristics(c) {}
173
174 size_t getSize() const override { return 4; }
175
176 void writeTo(uint8_t *buf) const override { write32le(P: buf, V: characteristics); }
177
178 uint32_t characteristics = 0;
179};
180
181// PartialSection represents a group of chunks that contribute to an
182// OutputSection. Collating a collection of PartialSections of same name and
183// characteristics constitutes the OutputSection.
184class PartialSectionKey {
185public:
186 StringRef name;
187 unsigned characteristics;
188
189 bool operator<(const PartialSectionKey &other) const {
190 int c = name.compare(RHS: other.name);
191 if (c > 0)
192 return false;
193 if (c == 0)
194 return characteristics < other.characteristics;
195 return true;
196 }
197};
198
199struct ChunkRange {
200 Chunk *first = nullptr, *last;
201};
202
203// The writer writes a SymbolTable result to a file.
204class Writer {
205public:
206 Writer(COFFLinkerContext &c)
207 : buffer(c.e.outputBuffer), strtab(StringTableBuilder::WinCOFF),
208 delayIdata(c), ctx(c) {}
209 void run();
210
211private:
212 void calculateStubDependentSizes();
213 void createSections();
214 void createMiscChunks();
215 void createImportTables();
216 void appendImportThunks();
217 void locateImportTables();
218 void createExportTable();
219 StringRef getMergeDestination(StringRef fromSection, StringRef toSection);
220 void mergeSection(const std::map<StringRef, StringRef>::value_type &p);
221 void mergeSections();
222 void sortECChunks();
223 void appendECImportTables();
224 void removeUnusedSections();
225 void layoutSections();
226 void assignAddresses();
227 bool isInRange(uint16_t relType, uint64_t s, uint64_t p, int margin,
228 MachineTypes machine);
229 std::pair<Defined *, bool> getThunk(DenseMap<uint64_t, Defined *> &lastThunks,
230 Defined *target, uint64_t p,
231 uint16_t type, int margin,
232 MachineTypes machine);
233 bool createThunks(OutputSection *os, int margin);
234 bool verifyRanges(const std::vector<Chunk *> chunks);
235 void createECCodeMap();
236 void finalizeAddresses();
237 void removeEmptySections();
238 void assignOutputSectionIndices();
239 void createSymbolAndStringTable();
240 void openFile(StringRef outputPath);
241 template <typename PEHeaderTy> void writeHeader();
242 void createSEHTable();
243 void createRuntimePseudoRelocs();
244 void createECChunks();
245 void insertCtorDtorSymbols();
246 void insertBssDataStartEndSymbols();
247 void markSymbolsWithRelocations(ObjFile *file, SymbolRVASet &usedSymbols);
248 void createGuardCFTables();
249 void markSymbolsForRVATable(ObjFile *file,
250 ArrayRef<SectionChunk *> symIdxChunks,
251 SymbolRVASet &tableSymbols);
252 void getSymbolsFromSections(ObjFile *file,
253 ArrayRef<SectionChunk *> symIdxChunks,
254 std::vector<Symbol *> &symbols);
255 void maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
256 StringRef countSym, bool hasFlag=false);
257 void setSectionPermissions();
258 void setECSymbols();
259 void writeSections();
260 void writeBuildId();
261 void writePEChecksum();
262 void sortSections();
263 template <typename T> void sortExceptionTable(ChunkRange &exceptionTable);
264 void sortExceptionTables();
265 void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
266 void addSyntheticIdata();
267 void sortBySectionOrder(std::vector<Chunk *> &chunks);
268 void fixPartialSectionChars(StringRef name, uint32_t chars);
269 bool fixGnuImportChunks();
270 void fixTlsAlignment(SymbolTable &symtab);
271 PartialSection *createPartialSection(StringRef name, uint32_t outChars);
272 PartialSection *findPartialSection(StringRef name, uint32_t outChars);
273
274 std::optional<coff_symbol16> createSymbol(Defined *d);
275 size_t addEntryToStringTable(StringRef str);
276
277 OutputSection *findSection(StringRef name);
278 void addBaserels();
279 void addBaserelBlocks(std::vector<Baserel> &v);
280 void createDynamicRelocs();
281
282 uint32_t getSizeOfInitializedData();
283
284 void prepareLoadConfig();
285 template <typename T>
286 void prepareLoadConfig(SymbolTable &symtab, T *loadConfig);
287
288 void printSummary();
289
290 std::unique_ptr<FileOutputBuffer> &buffer;
291 std::map<PartialSectionKey, PartialSection *> partialSections;
292 StringTableBuilder strtab;
293 std::vector<llvm::object::coff_symbol16> outputSymtab;
294 std::vector<ECCodeMapEntry> codeMap;
295 IdataContents idata;
296 Chunk *importTableStart = nullptr;
297 uint64_t importTableSize = 0;
298 Chunk *iatStart = nullptr;
299 uint64_t iatSize = 0;
300 DelayLoadContents delayIdata;
301 bool setNoSEHCharacteristic = false;
302 uint32_t tlsAlignment = 0;
303
304 DebugDirectoryChunk *debugDirectory = nullptr;
305 std::vector<std::pair<COFF::DebugType, Chunk *>> debugRecords;
306 CVDebugRecordChunk *buildId = nullptr;
307 ArrayRef<uint8_t> sectionTable;
308
309 // List of Arm64EC export thunks.
310 std::vector<std::pair<Chunk *, Defined *>> exportThunks;
311
312 uint64_t fileSize;
313 uint32_t pointerToSymbolTable = 0;
314 uint64_t sizeOfImage;
315 uint64_t sizeOfHeaders;
316
317 uint32_t dosStubSize;
318 uint32_t coffHeaderOffset;
319 uint32_t peHeaderOffset;
320 uint32_t dataDirOffset64;
321
322 OutputSection *textSec;
323 OutputSection *wowthkSec;
324 OutputSection *hexpthkSec;
325 OutputSection *bssSec;
326 OutputSection *rdataSec;
327 OutputSection *buildidSec;
328 OutputSection *cvinfoSec;
329 OutputSection *dataSec;
330 OutputSection *pdataSec;
331 OutputSection *idataSec;
332 OutputSection *edataSec;
333 OutputSection *didatSec;
334 OutputSection *a64xrmSec;
335 OutputSection *rsrcSec;
336 OutputSection *relocSec;
337 OutputSection *ctorsSec;
338 OutputSection *dtorsSec;
339 // Either .rdata section or .buildid section.
340 OutputSection *debugInfoSec;
341
342 // The range of .pdata sections in the output file.
343 //
344 // We need to keep track of the location of .pdata in whichever section it
345 // gets merged into so that we can sort its contents and emit a correct data
346 // directory entry for the exception table. This is also the case for some
347 // other sections (such as .edata) but because the contents of those sections
348 // are entirely linker-generated we can keep track of their locations using
349 // the chunks that the linker creates. All .pdata chunks come from input
350 // files, so we need to keep track of them separately.
351 ChunkRange pdata;
352
353 // x86_64 .pdata sections on ARM64EC/ARM64X targets.
354 ChunkRange hybridPdata;
355
356 // CHPE metadata symbol on ARM64C target.
357 DefinedRegular *chpeSym = nullptr;
358
359 COFFLinkerContext &ctx;
360};
361} // anonymous namespace
362
363void lld::coff::writeResult(COFFLinkerContext &ctx) {
364 llvm::TimeTraceScope timeScope("Write output(s)");
365 Writer(ctx).run();
366}
367
368void OutputSection::addChunk(Chunk *c) {
369 chunks.push_back(x: c);
370}
371
372void OutputSection::insertChunkAtStart(Chunk *c) {
373 chunks.insert(position: chunks.begin(), x: c);
374}
375
376void OutputSection::setPermissions(uint32_t c) {
377 header.Characteristics &= ~permMask;
378 header.Characteristics |= c;
379}
380
381void OutputSection::merge(OutputSection *other) {
382 chunks.insert(position: chunks.end(), first: other->chunks.begin(), last: other->chunks.end());
383 other->chunks.clear();
384 contribSections.insert(position: contribSections.end(), first: other->contribSections.begin(),
385 last: other->contribSections.end());
386 other->contribSections.clear();
387
388 // MS link.exe compatibility: when merging a code section into a data section,
389 // mark the target section as a code section.
390 if (other->header.Characteristics & IMAGE_SCN_CNT_CODE) {
391 header.Characteristics |= IMAGE_SCN_CNT_CODE;
392 header.Characteristics &=
393 ~(IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_CNT_UNINITIALIZED_DATA);
394 }
395}
396
397// Write the section header to a given buffer.
398void OutputSection::writeHeaderTo(uint8_t *buf, bool isDebug) {
399 auto *hdr = reinterpret_cast<coff_section *>(buf);
400 *hdr = header;
401 if (stringTableOff) {
402 // If name is too long, write offset into the string table as a name.
403 encodeSectionName(Out: hdr->Name, Offset: stringTableOff);
404 } else {
405 assert(!isDebug || name.size() <= COFF::NameSize ||
406 (hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0);
407 strncpy(dest: hdr->Name, src: name.data(),
408 n: std::min(a: name.size(), b: (size_t)COFF::NameSize));
409 }
410}
411
412void OutputSection::addContributingPartialSection(PartialSection *sec) {
413 contribSections.push_back(x: sec);
414}
415
416void OutputSection::splitECChunks() {
417 llvm::stable_sort(Range&: chunks, C: [=](const Chunk *a, const Chunk *b) {
418 return (a->getMachine() != ARM64) < (b->getMachine() != ARM64);
419 });
420}
421
422// Check whether the target address S is in range from a relocation
423// of type relType at address P.
424bool Writer::isInRange(uint16_t relType, uint64_t s, uint64_t p, int margin,
425 MachineTypes machine) {
426 if (machine == ARMNT) {
427 int64_t diff = AbsoluteDifference(X: s, Y: p + 4) + margin;
428 switch (relType) {
429 case IMAGE_REL_ARM_BRANCH20T:
430 return isInt<21>(x: diff);
431 case IMAGE_REL_ARM_BRANCH24T:
432 case IMAGE_REL_ARM_BLX23T:
433 return isInt<25>(x: diff);
434 default:
435 return true;
436 }
437 } else if (isAnyArm64(Machine: machine)) {
438 int64_t diff = AbsoluteDifference(X: s, Y: p) + margin;
439 switch (relType) {
440 case IMAGE_REL_ARM64_BRANCH26:
441 return isInt<28>(x: diff);
442 case IMAGE_REL_ARM64_BRANCH19:
443 return isInt<21>(x: diff);
444 case IMAGE_REL_ARM64_BRANCH14:
445 return isInt<16>(x: diff);
446 default:
447 return true;
448 }
449 } else {
450 return true;
451 }
452}
453
454// Return the last thunk for the given target if it is in range,
455// or create a new one.
456std::pair<Defined *, bool>
457Writer::getThunk(DenseMap<uint64_t, Defined *> &lastThunks, Defined *target,
458 uint64_t p, uint16_t type, int margin, MachineTypes machine) {
459 Defined *&lastThunk = lastThunks[target->getRVA()];
460 if (lastThunk && isInRange(relType: type, s: lastThunk->getRVA(), p, margin, machine))
461 return {lastThunk, false};
462 Chunk *c;
463 switch (getMachineArchType(machine)) {
464 case Triple::thumb:
465 c = make<RangeExtensionThunkARM>(args&: ctx, args&: target);
466 break;
467 case Triple::aarch64:
468 c = make<RangeExtensionThunkARM64>(args&: machine, args&: target);
469 break;
470 default:
471 llvm_unreachable("Unexpected architecture");
472 }
473 Defined *d = make<DefinedSynthetic>(args: "range_extension_thunk", args&: c);
474 lastThunk = d;
475 return {d, true};
476}
477
478// This checks all relocations, and for any relocation which isn't in range
479// it adds a thunk after the section chunk that contains the relocation.
480// If the latest thunk for the specific target is in range, that is used
481// instead of creating a new thunk. All range checks are done with the
482// specified margin, to make sure that relocations that originally are in
483// range, but only barely, also get thunks - in case other added thunks makes
484// the target go out of range.
485//
486// After adding thunks, we verify that all relocations are in range (with
487// no extra margin requirements). If this failed, we restart (throwing away
488// the previously created thunks) and retry with a wider margin.
489bool Writer::createThunks(OutputSection *os, int margin) {
490 bool addressesChanged = false;
491 DenseMap<uint64_t, Defined *> lastThunks;
492 DenseMap<std::pair<ObjFile *, Defined *>, uint32_t> thunkSymtabIndices;
493 size_t thunksSize = 0;
494 // Append thunks without repeatedly shifting the remaining chunks.
495 std::vector<Chunk *> newChunks;
496 newChunks.reserve(n: os->chunks.size());
497 for (Chunk *c : os->chunks) {
498 newChunks.push_back(x: c);
499 SectionChunk *sc = dyn_cast<SectionChunk>(Val: c);
500 if (!sc) {
501 auto chunk = cast<NonSectionChunk>(Val: c);
502 if (uint32_t size = chunk->extendRanges()) {
503 thunksSize += size;
504 addressesChanged = true;
505 }
506 continue;
507 }
508 MachineTypes machine = sc->getMachine();
509
510 // Try to get a good enough estimate of where new thunks will be placed.
511 // Offset this by the size of the new thunks added so far, to make the
512 // estimate slightly better.
513 size_t thunkInsertionRVA = sc->getRVA() + sc->getSize() + thunksSize;
514 ObjFile *file = sc->file;
515 std::vector<std::pair<uint32_t, uint32_t>> relocReplacements;
516 ArrayRef<coff_relocation> originalRelocs =
517 file->getCOFFObj()->getRelocations(Sec: sc->header);
518 for (size_t j = 0, e = originalRelocs.size(); j < e; ++j) {
519 const coff_relocation &rel = originalRelocs[j];
520 Symbol *relocTarget = file->getSymbol(symbolIndex: rel.SymbolTableIndex);
521
522 // The estimate of the source address P should be pretty accurate,
523 // but we don't know whether the target Symbol address should be
524 // offset by thunksSize or not (or by some of thunksSize but not all of
525 // it), giving us some uncertainty once we have added one thunk.
526 uint64_t p = sc->getRVA() + rel.VirtualAddress + thunksSize;
527
528 Defined *sym = dyn_cast_or_null<Defined>(Val: relocTarget);
529 if (!sym)
530 continue;
531
532 uint64_t s = sym->getRVA();
533
534 if (isInRange(relType: rel.Type, s, p, margin, machine))
535 continue;
536
537 // If the target isn't in range, hook it up to an existing or new thunk.
538 auto [thunk, wasNew] =
539 getThunk(lastThunks, target: sym, p, type: rel.Type, margin, machine);
540 if (wasNew) {
541 Chunk *thunkChunk = thunk->getChunk();
542 thunkChunk->setRVA(
543 thunkInsertionRVA); // Estimate of where it will be located.
544 newChunks.push_back(x: thunkChunk);
545 thunksSize += thunkChunk->getSize();
546 thunkInsertionRVA += thunkChunk->getSize();
547 addressesChanged = true;
548 }
549
550 // To redirect the relocation, add a symbol to the parent object file's
551 // symbol table, and replace the relocation symbol table index with the
552 // new index.
553 auto insertion = thunkSymtabIndices.insert(KV: {{file, thunk}, ~0U});
554 uint32_t &thunkSymbolIndex = insertion.first->second;
555 if (insertion.second)
556 thunkSymbolIndex = file->addRangeThunkSymbol(thunk);
557 relocReplacements.emplace_back(args&: j, args&: thunkSymbolIndex);
558 }
559
560 // Get a writable copy of this section's relocations so they can be
561 // modified. If the relocations point into the object file, allocate new
562 // memory. Otherwise, this must be previously allocated memory that can be
563 // modified in place.
564 ArrayRef<coff_relocation> curRelocs = sc->getRelocs();
565 MutableArrayRef<coff_relocation> newRelocs;
566 if (originalRelocs.data() == curRelocs.data()) {
567 newRelocs = MutableArrayRef(
568 bAlloc().Allocate<coff_relocation>(Num: originalRelocs.size()),
569 originalRelocs.size());
570 } else {
571 newRelocs = MutableArrayRef(
572 const_cast<coff_relocation *>(curRelocs.data()), curRelocs.size());
573 }
574
575 // Copy each relocation, but replace the symbol table indices which need
576 // thunks.
577 auto nextReplacement = relocReplacements.begin();
578 auto endReplacement = relocReplacements.end();
579 for (size_t i = 0, e = originalRelocs.size(); i != e; ++i) {
580 newRelocs[i] = originalRelocs[i];
581 if (nextReplacement != endReplacement && nextReplacement->first == i) {
582 newRelocs[i].SymbolTableIndex = nextReplacement->second;
583 ++nextReplacement;
584 }
585 }
586
587 sc->setRelocs(newRelocs);
588 }
589 os->chunks.swap(x&: newChunks);
590 return addressesChanged;
591}
592
593// Create a code map for CHPE metadata.
594void Writer::createECCodeMap() {
595 if (!ctx.symtab.isEC())
596 return;
597
598 // Clear the map in case we were're recomputing the map after adding
599 // a range extension thunk.
600 codeMap.clear();
601
602 std::optional<chpe_range_type> lastType;
603 Chunk *first, *last;
604
605 auto closeRange = [&]() {
606 if (lastType) {
607 codeMap.push_back(x: {first, last, *lastType});
608 lastType.reset();
609 }
610 };
611
612 for (OutputSection *sec : ctx.outputSections) {
613 for (Chunk *c : sec->chunks) {
614 // Skip empty section chunks. MS link.exe does not seem to do that and
615 // generates empty code ranges in some cases.
616 if (isa<SectionChunk>(Val: c) && !c->getSize())
617 continue;
618
619 std::optional<chpe_range_type> chunkType = c->getArm64ECRangeType();
620 if (chunkType != lastType) {
621 closeRange();
622 first = c;
623 lastType = chunkType;
624 }
625 last = c;
626 }
627 }
628
629 closeRange();
630
631 Symbol *tableCountSym = ctx.symtab.findUnderscore(name: "__hybrid_code_map_count");
632 cast<DefinedAbsolute>(Val: tableCountSym)->setVA(codeMap.size());
633}
634
635// Verify that all relocations are in range, with no extra margin requirements.
636bool Writer::verifyRanges(const std::vector<Chunk *> chunks) {
637 for (Chunk *c : chunks) {
638 SectionChunk *sc = dyn_cast<SectionChunk>(Val: c);
639 if (!sc) {
640 if (!cast<NonSectionChunk>(Val: c)->verifyRanges())
641 return false;
642 continue;
643 }
644 MachineTypes machine = sc->getMachine();
645
646 ArrayRef<coff_relocation> relocs = sc->getRelocs();
647 for (const coff_relocation &rel : relocs) {
648 Symbol *relocTarget = sc->file->getSymbol(symbolIndex: rel.SymbolTableIndex);
649
650 Defined *sym = dyn_cast_or_null<Defined>(Val: relocTarget);
651 if (!sym)
652 continue;
653
654 uint64_t p = sc->getRVA() + rel.VirtualAddress;
655 uint64_t s = sym->getRVA();
656
657 if (!isInRange(relType: rel.Type, s, p, margin: 0, machine))
658 return false;
659 }
660 }
661 return true;
662}
663
664// Assign addresses and add thunks if necessary.
665void Writer::finalizeAddresses() {
666 assignAddresses();
667 if (ctx.config.machine != ARMNT && !isAnyArm64(Machine: ctx.config.machine))
668 return;
669
670 size_t origNumChunks = 0;
671 for (OutputSection *sec : ctx.outputSections) {
672 sec->origChunks = sec->chunks;
673 origNumChunks += sec->chunks.size();
674 }
675
676 int pass = 0;
677 int margin = 1024 * 100;
678 while (true) {
679 llvm::TimeTraceScope timeScope2("Add thunks pass");
680
681 // First check whether we need thunks at all, or if the previous pass of
682 // adding them turned out ok.
683 bool rangesOk = true;
684 size_t numChunks = 0;
685 {
686 llvm::TimeTraceScope timeScope3("Verify ranges");
687 for (OutputSection *sec : ctx.outputSections) {
688 if (!verifyRanges(chunks: sec->chunks)) {
689 rangesOk = false;
690 break;
691 }
692 numChunks += sec->chunks.size();
693 }
694 }
695 if (rangesOk) {
696 if (pass > 0)
697 Log(ctx) << "Added " << (numChunks - origNumChunks) << " thunks with "
698 << "margin " << margin << " in " << pass << " passes";
699 return;
700 }
701
702 if (pass >= 10)
703 Fatal(ctx) << "adding thunks hasn't converged after " << pass
704 << " passes";
705
706 if (pass > 0) {
707 // If the previous pass didn't work out, reset everything back to the
708 // original conditions before retrying with a wider margin. This should
709 // ideally never happen under real circumstances.
710 for (OutputSection *sec : ctx.outputSections)
711 sec->chunks = sec->origChunks;
712 margin *= 2;
713 }
714
715 // Try adding thunks everywhere where it is needed, with a margin
716 // to avoid things going out of range due to the added thunks.
717 bool addressesChanged = false;
718 {
719 llvm::TimeTraceScope timeScope3("Create thunks");
720 for (OutputSection *sec : ctx.outputSections)
721 addressesChanged |= createThunks(os: sec, margin);
722 }
723 // If the verification above thought we needed thunks, we should have
724 // added some.
725 assert(addressesChanged);
726 (void)addressesChanged;
727
728 // Recalculate the layout for the whole image (and verify the ranges at
729 // the start of the next round).
730 assignAddresses();
731
732 pass++;
733 }
734}
735
736void Writer::writePEChecksum() {
737 if (!ctx.config.writeCheckSum) {
738 return;
739 }
740
741 llvm::TimeTraceScope timeScope("PE checksum");
742
743 // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#checksum
744 uint32_t *buf = (uint32_t *)buffer->getBufferStart();
745 uint32_t size = (uint32_t)(buffer->getBufferSize());
746
747 pe32_header *peHeader = (pe32_header *)((uint8_t *)buf + coffHeaderOffset +
748 sizeof(coff_file_header));
749
750 uint64_t sum = 0;
751 uint32_t count = size;
752 ulittle16_t *addr = (ulittle16_t *)buf;
753
754 // The PE checksum algorithm, implemented as suggested in RFC1071
755 while (count > 1) {
756 sum += *addr++;
757 count -= 2;
758 }
759
760 // Add left-over byte, if any
761 if (count > 0)
762 sum += *(unsigned char *)addr;
763
764 // Fold 32-bit sum to 16 bits
765 while (sum >> 16) {
766 sum = (sum & 0xffff) + (sum >> 16);
767 }
768
769 sum += size;
770 peHeader->CheckSum = sum;
771}
772
773// The main function of the writer.
774void Writer::run() {
775 {
776 llvm::TimeTraceScope timeScope("Write PE");
777 ScopedTimer t1(ctx.codeLayoutTimer);
778
779 calculateStubDependentSizes();
780 if (ctx.config.machine == ARM64X)
781 ctx.dynamicRelocs = make<DynamicRelocsChunk>();
782 createImportTables();
783 createSections();
784 appendImportThunks();
785 // Import thunks must be added before the Control Flow Guard tables are
786 // added.
787 createMiscChunks();
788 createExportTable();
789 mergeSections();
790 sortECChunks();
791 appendECImportTables();
792 createDynamicRelocs();
793 removeUnusedSections();
794 layoutSections();
795 finalizeAddresses();
796 removeEmptySections();
797 assignOutputSectionIndices();
798 setSectionPermissions();
799 setECSymbols();
800 createSymbolAndStringTable();
801
802 if (fileSize > UINT32_MAX)
803 Fatal(ctx) << "image size (" << fileSize << ") "
804 << "exceeds maximum allowable size (" << UINT32_MAX << ")";
805
806 openFile(outputPath: ctx.config.outputFile);
807 if (ctx.config.is64()) {
808 writeHeader<pe32plus_header>();
809 } else {
810 writeHeader<pe32_header>();
811 }
812 writeSections();
813 prepareLoadConfig();
814 sortExceptionTables();
815
816 // Fix up the alignment in the TLS Directory's characteristic field,
817 // if a specific alignment value is needed
818 if (tlsAlignment)
819 ctx.forEachSymtab(f: [&](SymbolTable &symtab) { fixTlsAlignment(symtab); });
820 }
821
822 if (!ctx.config.pdbPath.empty() && ctx.config.debug) {
823 assert(buildId);
824 createPDB(ctx, sectionTable, buildId: buildId->buildId);
825 }
826 writeBuildId();
827
828 writeLLDMapFile(ctx);
829 writeMapFile(ctx);
830
831 writePEChecksum();
832
833 printSummary();
834
835 if (errorCount())
836 return;
837
838 llvm::TimeTraceScope timeScope("Commit PE to disk");
839 ScopedTimer t2(ctx.outputCommitTimer);
840 if (auto e = buffer->commit())
841 Fatal(ctx) << "failed to write output '" << buffer->getPath()
842 << "': " << toString(E: std::move(e));
843}
844
845static StringRef getOutputSectionName(StringRef name, bool isMinGW) {
846 StringRef s = name.split(Separator: '$').first;
847 if (!isMinGW)
848 return s;
849
850 // Treat a later period as a separator for MinGW, for sections like
851 // ".ctors.01234".
852 return s.substr(Start: 0, N: s.find(C: '.', From: 1));
853}
854
855// For /order.
856void Writer::sortBySectionOrder(std::vector<Chunk *> &chunks) {
857 auto getPriority = [&ctx = ctx](const Chunk *c) {
858 if (auto *sec = dyn_cast<SectionChunk>(Val: c))
859 if (sec->sym)
860 return ctx.config.order.lookup(Key: sec->sym->getName());
861 return 0;
862 };
863
864 llvm::stable_sort(Range&: chunks, C: [=](const Chunk *a, const Chunk *b) {
865 return getPriority(a) < getPriority(b);
866 });
867}
868
869// Change the characteristics of existing PartialSections that belong to the
870// section Name to Chars.
871void Writer::fixPartialSectionChars(StringRef name, uint32_t chars) {
872 for (auto it : partialSections) {
873 PartialSection *pSec = it.second;
874 StringRef curName = pSec->name;
875 if (!curName.consume_front(Prefix: name) ||
876 (!curName.empty() && !curName.starts_with(Prefix: "$")))
877 continue;
878 if (pSec->characteristics == chars)
879 continue;
880 PartialSection *destSec = createPartialSection(name: pSec->name, outChars: chars);
881 destSec->chunks.insert(position: destSec->chunks.end(), first: pSec->chunks.begin(),
882 last: pSec->chunks.end());
883 pSec->chunks.clear();
884 }
885}
886
887// Sort concrete section chunks from GNU import libraries.
888//
889// GNU binutils doesn't use short import files, but instead produces import
890// libraries that consist of object files, with section chunks for the .idata$*
891// sections. These are linked just as regular static libraries. Each import
892// library consists of one header object, one object file for every imported
893// symbol, and one trailer object. In order for the .idata tables/lists to
894// be formed correctly, the section chunks within each .idata$* section need
895// to be grouped by library, and sorted alphabetically within each library
896// (which makes sure the header comes first and the trailer last).
897bool Writer::fixGnuImportChunks() {
898 uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
899
900 // Make sure all .idata$* section chunks are mapped as RDATA in order to
901 // be sorted into the same sections as our own synthesized .idata chunks.
902 fixPartialSectionChars(name: ".idata", chars: rdata);
903
904 bool hasIdata = false;
905 // Sort all .idata$* chunks, grouping chunks from the same library,
906 // with alphabetical ordering of the object files within a library.
907 for (auto it : partialSections) {
908 PartialSection *pSec = it.second;
909 if (!pSec->name.starts_with(Prefix: ".idata"))
910 continue;
911
912 if (!pSec->chunks.empty())
913 hasIdata = true;
914 llvm::stable_sort(Range&: pSec->chunks, C: [&](Chunk *s, Chunk *t) {
915 SectionChunk *sc1 = dyn_cast<SectionChunk>(Val: s);
916 SectionChunk *sc2 = dyn_cast<SectionChunk>(Val: t);
917 if (!sc1 || !sc2) {
918 // if SC1, order them ascending. If SC2 or both null,
919 // S is not less than T.
920 return sc1 != nullptr;
921 }
922 // Make a string with "libraryname/objectfile" for sorting, achieving
923 // both grouping by library and sorting of objects within a library,
924 // at once.
925 std::string key1 =
926 (sc1->file->parentName + "/" + sc1->file->getName()).str();
927 std::string key2 =
928 (sc2->file->parentName + "/" + sc2->file->getName()).str();
929 return key1 < key2;
930 });
931 }
932 return hasIdata;
933}
934
935// Add generated idata chunks, for imported symbols and DLLs, and a
936// terminator in .idata$2.
937void Writer::addSyntheticIdata() {
938 uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
939 idata.create(ctx);
940
941 // Add the .idata content in the right section groups, to allow
942 // chunks from other linked in object files to be grouped together.
943 // See Microsoft PE/COFF spec 5.4 for details.
944 auto add = [&](StringRef n, std::vector<Chunk *> &v) {
945 PartialSection *pSec = createPartialSection(name: n, outChars: rdata);
946 pSec->chunks.insert(position: pSec->chunks.end(), first: v.begin(), last: v.end());
947 };
948
949 // The loader assumes a specific order of data.
950 // Add each type in the correct order.
951 add(".idata$2", idata.dirs);
952 add(".idata$4", idata.lookups);
953 add(".idata$5", idata.addresses);
954 if (!idata.hints.empty())
955 add(".idata$6", idata.hints);
956 add(".idata$7", idata.dllNames);
957 if (!idata.auxIat.empty())
958 add(".idata$9", idata.auxIat);
959 if (!idata.auxIatCopy.empty())
960 add(".idata$a", idata.auxIatCopy);
961}
962
963void Writer::appendECImportTables() {
964 if (!isArm64EC(Machine: ctx.config.machine))
965 return;
966
967 const uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
968
969 // IAT is always placed at the beginning of .rdata section and its size
970 // is aligned to 4KB. Insert it here, after all merges all done.
971 if (PartialSection *importAddresses = findPartialSection(name: ".idata$5", outChars: rdata)) {
972 if (!rdataSec->chunks.empty())
973 rdataSec->chunks.front()->setAlignment(
974 std::max(a: 0x1000u, b: rdataSec->chunks.front()->getAlignment()));
975 iatSize = alignTo(Value: iatSize, Align: 0x1000);
976
977 rdataSec->chunks.insert(position: rdataSec->chunks.begin(),
978 first: importAddresses->chunks.begin(),
979 last: importAddresses->chunks.end());
980 rdataSec->contribSections.insert(position: rdataSec->contribSections.begin(),
981 x: importAddresses);
982 }
983
984 // The auxiliary IAT is always placed at the end of the .rdata section
985 // and is aligned to 4KB.
986 if (PartialSection *auxIat = findPartialSection(name: ".idata$9", outChars: rdata)) {
987 auxIat->chunks.front()->setAlignment(0x1000);
988 rdataSec->chunks.insert(position: rdataSec->chunks.end(), first: auxIat->chunks.begin(),
989 last: auxIat->chunks.end());
990 rdataSec->addContributingPartialSection(sec: auxIat);
991 }
992
993 if (!delayIdata.getAuxIat().empty()) {
994 delayIdata.getAuxIat().front()->setAlignment(0x1000);
995 rdataSec->chunks.insert(position: rdataSec->chunks.end(),
996 first: delayIdata.getAuxIat().begin(),
997 last: delayIdata.getAuxIat().end());
998 }
999}
1000
1001// Locate the first Chunk and size of the import directory list and the
1002// IAT.
1003void Writer::locateImportTables() {
1004 uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
1005
1006 if (PartialSection *importDirs = findPartialSection(name: ".idata$2", outChars: rdata)) {
1007 if (!importDirs->chunks.empty())
1008 importTableStart = importDirs->chunks.front();
1009 for (Chunk *c : importDirs->chunks)
1010 importTableSize += c->getSize();
1011 }
1012
1013 if (PartialSection *importAddresses = findPartialSection(name: ".idata$5", outChars: rdata)) {
1014 if (!importAddresses->chunks.empty())
1015 iatStart = importAddresses->chunks.front();
1016 for (Chunk *c : importAddresses->chunks)
1017 iatSize += c->getSize();
1018 }
1019}
1020
1021// Return whether a SectionChunk's suffix (the dollar and any trailing
1022// suffix) should be removed and sorted into the main suffixless
1023// PartialSection.
1024static bool shouldStripSectionSuffix(SectionChunk *sc, StringRef name,
1025 bool isMinGW) {
1026 // On MinGW, comdat groups are formed by putting the comdat group name
1027 // after the '$' in the section name. For .eh_frame$<symbol>, that must
1028 // still be sorted before the .eh_frame trailer from crtend.o, thus just
1029 // strip the section name trailer. For other sections, such as
1030 // .tls$$<symbol> (where non-comdat .tls symbols are otherwise stored in
1031 // ".tls$"), they must be strictly sorted after .tls. And for the
1032 // hypothetical case of comdat .CRT$XCU, we definitely need to keep the
1033 // suffix for sorting. Thus, to play it safe, only strip the suffix for
1034 // the standard sections.
1035 if (!isMinGW)
1036 return false;
1037 if (!sc || !sc->isCOMDAT())
1038 return false;
1039 return name.starts_with(Prefix: ".text$") || name.starts_with(Prefix: ".data$") ||
1040 name.starts_with(Prefix: ".rdata$") || name.starts_with(Prefix: ".pdata$") ||
1041 name.starts_with(Prefix: ".xdata$") || name.starts_with(Prefix: ".eh_frame$");
1042}
1043
1044void Writer::sortSections() {
1045 if (!ctx.config.callGraphProfile.empty()) {
1046 DenseMap<const SectionChunk *, int> order =
1047 computeCallGraphProfileOrder(ctx);
1048 for (auto it : order) {
1049 if (DefinedRegular *sym = it.first->sym)
1050 ctx.config.order[sym->getName()] = it.second;
1051 }
1052 }
1053 if (!ctx.config.order.empty())
1054 for (auto it : partialSections)
1055 sortBySectionOrder(chunks&: it.second->chunks);
1056}
1057
1058void Writer::calculateStubDependentSizes() {
1059 if (ctx.config.dosStub)
1060 dosStubSize = alignTo(Value: ctx.config.dosStub->getBufferSize(), Align: 8);
1061 else
1062 dosStubSize = sizeof(dos_header) + sizeof(dosProgram);
1063
1064 coffHeaderOffset = dosStubSize + sizeof(PEMagic);
1065 peHeaderOffset = coffHeaderOffset + sizeof(coff_file_header);
1066 dataDirOffset64 = peHeaderOffset + sizeof(pe32plus_header);
1067}
1068
1069// Create output section objects and add them to OutputSections.
1070void Writer::createSections() {
1071 llvm::TimeTraceScope timeScope("Output sections");
1072 // First, create the builtin sections.
1073 const uint32_t data = IMAGE_SCN_CNT_INITIALIZED_DATA;
1074 const uint32_t bss = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
1075 const uint32_t code = IMAGE_SCN_CNT_CODE;
1076 const uint32_t discardable = IMAGE_SCN_MEM_DISCARDABLE;
1077 const uint32_t r = IMAGE_SCN_MEM_READ;
1078 const uint32_t w = IMAGE_SCN_MEM_WRITE;
1079 const uint32_t x = IMAGE_SCN_MEM_EXECUTE;
1080
1081 SmallDenseMap<std::pair<StringRef, uint32_t>, OutputSection *> sections;
1082 auto createSection = [&](StringRef name, uint32_t outChars) {
1083 OutputSection *&sec = sections[{name, outChars}];
1084 if (!sec) {
1085 sec = make<OutputSection>(args&: name, args&: outChars);
1086 ctx.outputSections.push_back(x: sec);
1087 }
1088 return sec;
1089 };
1090
1091 // Try to match the section order used by link.exe.
1092 textSec = createSection(".text", code | r | x);
1093 if (isArm64EC(Machine: ctx.config.machine)) {
1094 wowthkSec = createSection(".wowthk", code | r | x);
1095 hexpthkSec = createSection(".hexpthk", code | r | x);
1096 }
1097 bssSec = createSection(".bss", bss | r | w);
1098 rdataSec = createSection(".rdata", data | r);
1099 buildidSec = createSection(".buildid", data | r);
1100 cvinfoSec = createSection(".cvinfo", data | r);
1101 dataSec = createSection(".data", data | r | w);
1102 pdataSec = createSection(".pdata", data | r);
1103 idataSec = createSection(".idata", data | r);
1104 edataSec = createSection(".edata", data | r);
1105 didatSec = createSection(".didat", data | r);
1106 if (isArm64EC(Machine: ctx.config.machine))
1107 a64xrmSec = createSection(".a64xrm", data | r);
1108 rsrcSec = createSection(".rsrc", data | r);
1109 relocSec = createSection(".reloc", data | discardable | r);
1110 ctorsSec = createSection(".ctors", data | r | w);
1111 dtorsSec = createSection(".dtors", data | r | w);
1112
1113 // Then bin chunks by name and output characteristics.
1114 for (Chunk *c : ctx.driver.getChunks()) {
1115 auto *sc = dyn_cast<SectionChunk>(Val: c);
1116 if (sc && !sc->live) {
1117 if (ctx.config.verbose)
1118 sc->printDiscardedMessage();
1119 continue;
1120 }
1121 if (auto *cc = dyn_cast<CommonChunk>(Val: c)) {
1122 if (!cc->live)
1123 continue;
1124 }
1125 StringRef name = c->getSectionName();
1126 if (shouldStripSectionSuffix(sc, name, isMinGW: ctx.config.mingw))
1127 name = name.split(Separator: '$').first;
1128
1129 if (name.starts_with(Prefix: ".tls"))
1130 tlsAlignment = std::max(a: tlsAlignment, b: c->getAlignment());
1131
1132 PartialSection *pSec = createPartialSection(name,
1133 outChars: c->getOutputCharacteristics());
1134 pSec->chunks.push_back(x: c);
1135 }
1136
1137 fixPartialSectionChars(name: ".rsrc", chars: data | r);
1138 fixPartialSectionChars(name: ".edata", chars: data | r);
1139 // Even in non MinGW cases, we might need to link against GNU import
1140 // libraries.
1141 bool hasIdata = fixGnuImportChunks();
1142 if (!idata.empty())
1143 hasIdata = true;
1144
1145 if (hasIdata)
1146 addSyntheticIdata();
1147
1148 sortSections();
1149
1150 if (hasIdata)
1151 locateImportTables();
1152
1153 for (auto thunk : ctx.symtab.sameAddressThunks)
1154 wowthkSec->addChunk(c: thunk);
1155
1156 // Then create an OutputSection for each section.
1157 // '$' and all following characters in input section names are
1158 // discarded when determining output section. So, .text$foo
1159 // contributes to .text, for example. See PE/COFF spec 3.2.
1160 for (auto it : partialSections) {
1161 PartialSection *pSec = it.second;
1162 StringRef name = getOutputSectionName(name: pSec->name, isMinGW: ctx.config.mingw);
1163 uint32_t outChars = pSec->characteristics;
1164
1165 if (name == ".CRT") {
1166 // In link.exe, there is a special case for the I386 target where .CRT
1167 // sections are treated as if they have output characteristics DATA | R if
1168 // their characteristics are DATA | R | W. This implements the same
1169 // special case for all architectures.
1170 outChars = data | r;
1171
1172 Log(ctx) << "Processing section " << pSec->name << " -> " << name;
1173
1174 sortCRTSectionChunks(chunks&: pSec->chunks);
1175 }
1176
1177 // ARM64EC has specific placement and alignment requirements for the IAT.
1178 // Delay adding its chunks until appendECImportTables.
1179 if (isArm64EC(Machine: ctx.config.machine) &&
1180 (pSec->name == ".idata$5" || pSec->name == ".idata$9"))
1181 continue;
1182
1183 OutputSection *sec = createSection(name, outChars);
1184 for (Chunk *c : pSec->chunks)
1185 sec->addChunk(c);
1186
1187 sec->addContributingPartialSection(sec: pSec);
1188 }
1189
1190 if (ctx.hybridSymtab) {
1191 if (OutputSection *sec = findSection(name: ".CRT"))
1192 sec->splitECChunks();
1193 }
1194
1195 // Finally, move some output sections to the end.
1196 auto sectionOrder = [&](const OutputSection *s) {
1197 // Move DISCARDABLE (or non-memory-mapped) sections to the end of file
1198 // because the loader cannot handle holes. Stripping can remove other
1199 // discardable ones than .reloc, which is first of them (created early).
1200 if (s->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) {
1201 // Move discardable sections named .debug_ to the end, after other
1202 // discardable sections. Stripping only removes the sections named
1203 // .debug_* - thus try to avoid leaving holes after stripping.
1204 if (s->name.starts_with(Prefix: ".debug_"))
1205 return 3;
1206 return 2;
1207 }
1208 // .rsrc should come at the end of the non-discardable sections because its
1209 // size may change by the Win32 UpdateResources() function, causing
1210 // subsequent sections to move (see https://crbug.com/827082).
1211 if (s == rsrcSec)
1212 return 1;
1213 return 0;
1214 };
1215 llvm::stable_sort(Range&: ctx.outputSections,
1216 C: [&](const OutputSection *s, const OutputSection *t) {
1217 return sectionOrder(s) < sectionOrder(t);
1218 });
1219}
1220
1221void Writer::createMiscChunks() {
1222 llvm::TimeTraceScope timeScope("Misc chunks");
1223 Configuration *config = &ctx.config;
1224
1225 for (MergeChunk *p : ctx.mergeChunkInstances) {
1226 if (p) {
1227 p->finalizeContents();
1228 rdataSec->addChunk(c: p);
1229 }
1230 }
1231
1232 // Create thunks for locally-dllimported symbols.
1233 ctx.forEachSymtab(f: [&](SymbolTable &symtab) {
1234 if (!symtab.localImportChunks.empty()) {
1235 for (Chunk *c : symtab.localImportChunks)
1236 rdataSec->addChunk(c);
1237 }
1238 });
1239
1240 // Create Debug Information Chunks
1241 if (config->mingw) {
1242 debugInfoSec = buildidSec;
1243 } else if (!config->mergeDebugDirectory) {
1244 debugInfoSec = cvinfoSec;
1245 } else {
1246 debugInfoSec = rdataSec;
1247 }
1248 if (config->buildIDHash != BuildIDHash::None || config->debug ||
1249 config->repro || config->cetCompat || config->cetCompatStrict ||
1250 config->cetCompatIpValidationRelaxed ||
1251 config->cetCompatDynamicApisInProcOnly || config->hotpatchCompat) {
1252 debugDirectory =
1253 make<DebugDirectoryChunk>(args&: ctx, args&: debugRecords, args&: config->repro);
1254 debugDirectory->setAlignment(4);
1255 debugInfoSec->addChunk(c: debugDirectory);
1256 }
1257
1258 if (config->debug || config->buildIDHash != BuildIDHash::None) {
1259 // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified. We
1260 // output a PDB no matter what, and this chunk provides the only means of
1261 // allowing a debugger to match a PDB and an executable. So we need it even
1262 // if we're ultimately not going to write CodeView data to the PDB.
1263 buildId = make<CVDebugRecordChunk>(args&: ctx);
1264 debugRecords.emplace_back(args: COFF::IMAGE_DEBUG_TYPE_CODEVIEW, args&: buildId);
1265 ctx.forEachSymtab(f: [&](SymbolTable &symtab) {
1266 if (Symbol *buildidSym = symtab.findUnderscore(name: "__buildid"))
1267 replaceSymbol<DefinedSynthetic>(s: buildidSym, arg: buildidSym->getName(),
1268 arg&: buildId, arg: 4);
1269 });
1270 }
1271
1272 uint16_t ex_characteristics_flags = 0;
1273 if (config->cetCompat)
1274 ex_characteristics_flags |= IMAGE_DLL_CHARACTERISTICS_EX_CET_COMPAT;
1275 if (config->cetCompatStrict)
1276 ex_characteristics_flags |=
1277 IMAGE_DLL_CHARACTERISTICS_EX_CET_COMPAT_STRICT_MODE;
1278 if (config->cetCompatIpValidationRelaxed)
1279 ex_characteristics_flags |=
1280 IMAGE_DLL_CHARACTERISTICS_EX_CET_SET_CONTEXT_IP_VALIDATION_RELAXED_MODE;
1281 if (config->cetCompatDynamicApisInProcOnly)
1282 ex_characteristics_flags |=
1283 IMAGE_DLL_CHARACTERISTICS_EX_CET_DYNAMIC_APIS_ALLOW_IN_PROC_ONLY;
1284 if (config->hotpatchCompat)
1285 ex_characteristics_flags |=
1286 IMAGE_DLL_CHARACTERISTICS_EX_HOTPATCH_COMPATIBLE;
1287
1288 if (ex_characteristics_flags) {
1289 debugRecords.emplace_back(
1290 args: COFF::IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS,
1291 args: make<ExtendedDllCharacteristicsChunk>(args&: ex_characteristics_flags));
1292 }
1293
1294 // Align and add each chunk referenced by the debug data directory.
1295 for (std::pair<COFF::DebugType, Chunk *> r : debugRecords) {
1296 r.second->setAlignment(4);
1297 debugInfoSec->addChunk(c: r.second);
1298 }
1299
1300 // Create SEH table. x86-only.
1301 if (config->safeSEH)
1302 createSEHTable();
1303
1304 // Create /guard:cf tables if requested.
1305 createGuardCFTables();
1306
1307 createECChunks();
1308
1309 if (config->autoImport)
1310 createRuntimePseudoRelocs();
1311
1312 if (config->mingw) {
1313 insertCtorDtorSymbols();
1314 insertBssDataStartEndSymbols();
1315 }
1316}
1317
1318// Create .idata section for the DLL-imported symbol table.
1319// The format of this section is inherently Windows-specific.
1320// IdataContents class abstracted away the details for us,
1321// so we just let it create chunks and add them to the section.
1322void Writer::createImportTables() {
1323 llvm::TimeTraceScope timeScope("Import tables");
1324 // Initialize DLLOrder so that import entries are ordered in
1325 // the same order as in the command line. (That affects DLL
1326 // initialization order, and this ordering is MSVC-compatible.)
1327 for (ImportFile *file : ctx.importFileInstances) {
1328 if (!file->live)
1329 continue;
1330
1331 std::string dll = StringRef(file->dllName).lower();
1332 ctx.config.dllOrder.try_emplace(k: dll, args: ctx.config.dllOrder.size());
1333
1334 if (file->impSym && !isa<DefinedImportData>(Val: file->impSym))
1335 Fatal(ctx) << file->symtab.printSymbol(sym: file->impSym) << " was replaced";
1336 DefinedImportData *impSym = cast_or_null<DefinedImportData>(Val: file->impSym);
1337 if (ctx.config.delayLoads.contains(key: StringRef(file->dllName).lower())) {
1338 if (!file->thunkSym)
1339 Fatal(ctx) << "cannot delay-load " << toString(file)
1340 << " due to import of data: "
1341 << file->symtab.printSymbol(sym: impSym);
1342 delayIdata.add(sym: impSym);
1343 } else {
1344 idata.add(sym: impSym);
1345 }
1346 }
1347}
1348
1349void Writer::appendImportThunks() {
1350 if (ctx.importFileInstances.empty())
1351 return;
1352
1353 llvm::TimeTraceScope timeScope("Import thunks");
1354 for (ImportFile *file : ctx.importFileInstances) {
1355 if (!file->live)
1356 continue;
1357
1358 if (file->thunkSym) {
1359 if (!isa<DefinedImportThunk>(Val: file->thunkSym))
1360 Fatal(ctx) << file->symtab.printSymbol(sym: file->thunkSym)
1361 << " was replaced";
1362 auto *chunk = cast<DefinedImportThunk>(Val: file->thunkSym)->getChunk();
1363 if (chunk->live)
1364 textSec->addChunk(c: chunk);
1365 }
1366
1367 if (file->auxThunkSym) {
1368 if (!isa<DefinedImportThunk>(Val: file->auxThunkSym))
1369 Fatal(ctx) << file->symtab.printSymbol(sym: file->auxThunkSym)
1370 << " was replaced";
1371 auto *chunk = cast<DefinedImportThunk>(Val: file->auxThunkSym)->getChunk();
1372 if (chunk->live)
1373 textSec->addChunk(c: chunk);
1374 }
1375
1376 if (file->impchkThunk)
1377 textSec->addChunk(c: file->impchkThunk);
1378 }
1379
1380 if (!delayIdata.empty()) {
1381 delayIdata.create();
1382 for (Chunk *c : delayIdata.getChunks())
1383 didatSec->addChunk(c);
1384 for (Chunk *c : delayIdata.getDataChunks())
1385 dataSec->addChunk(c);
1386 for (Chunk *c : delayIdata.getCodeChunks())
1387 textSec->addChunk(c);
1388 for (Chunk *c : delayIdata.getCodePData())
1389 pdataSec->addChunk(c);
1390 for (Chunk *c : delayIdata.getAuxIatCopy())
1391 rdataSec->addChunk(c);
1392 for (Chunk *c : delayIdata.getCodeUnwindInfo())
1393 rdataSec->addChunk(c);
1394 }
1395}
1396
1397void Writer::createExportTable() {
1398 llvm::TimeTraceScope timeScope("Export table");
1399 if (!edataSec->chunks.empty()) {
1400 // Allow using a custom built export table from input object files, instead
1401 // of having the linker synthesize the tables.
1402 if (!ctx.hybridSymtab) {
1403 ctx.symtab.edataStart = edataSec->chunks.front();
1404 ctx.symtab.edataEnd = edataSec->chunks.back();
1405 } else {
1406 // On hybrid target, split EC and native chunks.
1407 llvm::stable_sort(Range&: edataSec->chunks, C: [=](const Chunk *a, const Chunk *b) {
1408 return (a->getMachine() != ARM64) < (b->getMachine() != ARM64);
1409 });
1410
1411 for (auto chunk : edataSec->chunks) {
1412 if (chunk->getMachine() != ARM64) {
1413 ctx.symtab.edataStart = chunk;
1414 ctx.symtab.edataEnd = edataSec->chunks.back();
1415 break;
1416 }
1417
1418 if (!ctx.hybridSymtab->edataStart)
1419 ctx.hybridSymtab->edataStart = chunk;
1420 ctx.hybridSymtab->edataEnd = chunk;
1421 }
1422 }
1423 }
1424 ctx.forEachActiveSymtab(f: [&](SymbolTable &symtab) {
1425 if (symtab.edataStart) {
1426 if (symtab.hadExplicitExports)
1427 Warn(ctx) << "literal .edata sections override exports";
1428 } else if (!symtab.exports.empty()) {
1429 std::vector<Chunk *> edataChunks;
1430 createEdataChunks(symtab, chunks&: edataChunks);
1431 for (Chunk *c : edataChunks)
1432 edataSec->addChunk(c);
1433 symtab.edataStart = edataChunks.front();
1434 symtab.edataEnd = edataChunks.back();
1435 }
1436
1437 // Warn on exported deleting destructor.
1438 for (auto e : symtab.exports)
1439 if (e.sym && e.sym->getName().starts_with(Prefix: "??_G"))
1440 Warn(ctx) << "export of deleting dtor: " << toString(ctx, b&: *e.sym);
1441 });
1442}
1443
1444void Writer::removeUnusedSections() {
1445 llvm::TimeTraceScope timeScope("Remove unused sections");
1446 // Remove sections that we can be sure won't get content, to avoid
1447 // allocating space for their section headers.
1448 auto isUnused = [this](OutputSection *s) {
1449 if (s == relocSec)
1450 return false; // This section is populated later.
1451 // MergeChunks have zero size at this point, as their size is finalized
1452 // later. Only remove sections that have no Chunks at all.
1453 return s->chunks.empty();
1454 };
1455 llvm::erase_if(C&: ctx.outputSections, P: isUnused);
1456}
1457
1458void Writer::layoutSections() {
1459 llvm::TimeTraceScope timeScope("Layout sections");
1460 if (ctx.config.sectionOrder.empty())
1461 return;
1462
1463 llvm::stable_sort(Range&: ctx.outputSections,
1464 C: [this](const OutputSection *a, const OutputSection *b) {
1465 auto itA = ctx.config.sectionOrder.find(x: a->name.str());
1466 auto itB = ctx.config.sectionOrder.find(x: b->name.str());
1467 bool aInOrder = itA != ctx.config.sectionOrder.end();
1468 bool bInOrder = itB != ctx.config.sectionOrder.end();
1469
1470 // Put unspecified sections after all specified sections
1471 if (aInOrder && bInOrder) {
1472 return itA->second < itB->second;
1473 } else if (aInOrder && !bInOrder) {
1474 return true; // ordered sections come before unordered
1475 } else {
1476 // (!aInOrder && bInOrder): unordered comes after
1477 // ordered
1478 // (!aInOrder && !bInOrder): both unspecified, preserve
1479 // the original order
1480 return false;
1481 }
1482 });
1483}
1484
1485// The Windows loader doesn't seem to like empty sections,
1486// so we remove them if any.
1487void Writer::removeEmptySections() {
1488 llvm::TimeTraceScope timeScope("Remove empty sections");
1489 auto isEmpty = [](OutputSection *s) { return s->getVirtualSize() == 0; };
1490 llvm::erase_if(C&: ctx.outputSections, P: isEmpty);
1491}
1492
1493void Writer::assignOutputSectionIndices() {
1494 llvm::TimeTraceScope timeScope("Output sections indices");
1495 // Assign final output section indices, and assign each chunk to its output
1496 // section.
1497 uint32_t idx = 1;
1498 for (OutputSection *os : ctx.outputSections) {
1499 os->sectionIndex = idx;
1500 for (Chunk *c : os->chunks)
1501 c->setOutputSectionIdx(idx);
1502 ++idx;
1503 }
1504
1505 // Merge chunks are containers of chunks, so assign those an output section
1506 // too.
1507 for (MergeChunk *mc : ctx.mergeChunkInstances)
1508 if (mc)
1509 for (SectionChunk *sc : mc->sections)
1510 if (sc && sc->live)
1511 sc->setOutputSectionIdx(mc->getOutputSectionIdx());
1512}
1513
1514std::optional<coff_symbol16> Writer::createSymbol(Defined *def) {
1515 coff_symbol16 sym;
1516 switch (def->kind()) {
1517 case Symbol::DefinedAbsoluteKind: {
1518 auto *da = dyn_cast<DefinedAbsolute>(Val: def);
1519 // Note: COFF symbol can only store 32-bit values, so 64-bit absolute
1520 // values will be truncated.
1521 sym.Value = da->getVA();
1522 sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
1523 break;
1524 }
1525 default: {
1526 // Don't write symbols that won't be written to the output to the symbol
1527 // table.
1528 // We also try to write DefinedSynthetic as a normal symbol. Some of these
1529 // symbols do point to an actual chunk, like __safe_se_handler_table. Others
1530 // like __ImageBase are outside of sections and thus cannot be represented.
1531 Chunk *c = def->getChunk();
1532 if (!c)
1533 return std::nullopt;
1534 OutputSection *os = ctx.getOutputSection(c);
1535 if (!os)
1536 return std::nullopt;
1537
1538 sym.Value = def->getRVA() - os->getRVA();
1539 sym.SectionNumber = os->sectionIndex;
1540 break;
1541 }
1542 }
1543
1544 // Symbols that are runtime pseudo relocations don't point to the actual
1545 // symbol data itself (as they are imported), but points to the IAT entry
1546 // instead. Avoid emitting them to the symbol table, as they can confuse
1547 // debuggers.
1548 if (def->isRuntimePseudoReloc)
1549 return std::nullopt;
1550
1551 StringRef name = def->getName();
1552 if (name.size() > COFF::NameSize) {
1553 sym.Name.Offset.Zeroes = 0;
1554 sym.Name.Offset.Offset = 0; // Filled in later.
1555 strtab.add(S: name);
1556 } else {
1557 memset(s: sym.Name.ShortName, c: 0, n: COFF::NameSize);
1558 memcpy(dest: sym.Name.ShortName, src: name.data(), n: name.size());
1559 }
1560
1561 if (auto *d = dyn_cast<DefinedCOFF>(Val: def)) {
1562 COFFSymbolRef ref = d->getCOFFSymbol();
1563 sym.Type = ref.getType();
1564 sym.StorageClass = ref.getStorageClass();
1565 } else if (def->kind() == Symbol::DefinedImportThunkKind) {
1566 sym.Type = (IMAGE_SYM_DTYPE_FUNCTION << SCT_COMPLEX_TYPE_SHIFT) |
1567 IMAGE_SYM_TYPE_NULL;
1568 sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
1569 } else {
1570 sym.Type = IMAGE_SYM_TYPE_NULL;
1571 sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
1572 }
1573 sym.NumberOfAuxSymbols = 0;
1574 return sym;
1575}
1576
1577void Writer::createSymbolAndStringTable() {
1578 llvm::TimeTraceScope timeScope("Symbol and string table");
1579 // PE/COFF images are limited to 8 byte section names. Longer names can be
1580 // supported by writing a non-standard string table, but this string table is
1581 // not mapped at runtime and the long names will therefore be inaccessible.
1582 // link.exe always truncates section names to 8 bytes, whereas binutils always
1583 // preserves long section names via the string table. LLD adopts a hybrid
1584 // solution where discardable sections have long names preserved and
1585 // non-discardable sections have their names truncated, to ensure that any
1586 // section which is mapped at runtime also has its name mapped at runtime.
1587 SmallVector<OutputSection *> longNameSections;
1588 for (OutputSection *sec : ctx.outputSections) {
1589 if (sec->name.size() <= COFF::NameSize)
1590 continue;
1591 if ((sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0)
1592 continue;
1593 if (ctx.config.warnLongSectionNames) {
1594 Warn(ctx)
1595 << "section name " << sec->name
1596 << " is longer than 8 characters and will use a non-standard string "
1597 "table";
1598 }
1599 // Put the section name in the begin of strtab so that its offset is less
1600 // than Max7DecimalOffset otherwise lldb/gdb will not read it.
1601 strtab.add(S: sec->name, /*Priority=*/UINT8_MAX);
1602 longNameSections.push_back(Elt: sec);
1603 }
1604
1605 std::vector<std::pair<size_t, StringRef>> longNameSymbols;
1606 if (ctx.config.writeSymtab) {
1607 for (ObjFile *file : ctx.objFileInstances) {
1608 for (Symbol *b : file->getSymbols()) {
1609 auto *d = dyn_cast_or_null<Defined>(Val: b);
1610 if (!d || d->writtenToSymtab)
1611 continue;
1612 d->writtenToSymtab = true;
1613 if (auto *dc = dyn_cast_or_null<DefinedCOFF>(Val: d)) {
1614 COFFSymbolRef symRef = dc->getCOFFSymbol();
1615 if (symRef.isSectionDefinition() ||
1616 symRef.getStorageClass() == COFF::IMAGE_SYM_CLASS_LABEL)
1617 continue;
1618 }
1619
1620 if (std::optional<coff_symbol16> sym = createSymbol(def: d)) {
1621 if (d->getName().size() > COFF::NameSize)
1622 longNameSymbols.emplace_back(args: outputSymtab.size(), args: d->getName());
1623 outputSymtab.push_back(x: *sym);
1624 }
1625
1626 if (auto *dthunk = dyn_cast<DefinedImportThunk>(Val: d)) {
1627 if (!dthunk->wrappedSym->writtenToSymtab) {
1628 dthunk->wrappedSym->writtenToSymtab = true;
1629 if (std::optional<coff_symbol16> sym =
1630 createSymbol(def: dthunk->wrappedSym)) {
1631 if (dthunk->wrappedSym->getName().size() > COFF::NameSize)
1632 longNameSymbols.emplace_back(args: outputSymtab.size(),
1633 args: dthunk->wrappedSym->getName());
1634 outputSymtab.push_back(x: *sym);
1635 }
1636 }
1637 }
1638 }
1639 }
1640 }
1641
1642 if (outputSymtab.empty() && strtab.empty())
1643 return;
1644
1645 strtab.finalize();
1646 for (OutputSection *sec : longNameSections)
1647 sec->setStringTableOff(strtab.getOffset(S: sec->name));
1648 for (auto P : longNameSymbols) {
1649 coff_symbol16 &sym = outputSymtab[P.first];
1650 sym.Name.Offset.Offset = strtab.getOffset(S: P.second);
1651 }
1652
1653 // We position the symbol table to be adjacent to the end of the last section.
1654 uint64_t fileOff = fileSize;
1655 pointerToSymbolTable = fileOff;
1656 fileOff += outputSymtab.size() * sizeof(coff_symbol16);
1657 fileOff += strtab.getSize();
1658 fileSize = alignTo(Value: fileOff, Align: ctx.config.fileAlign);
1659}
1660
1661StringRef Writer::getMergeDestination(StringRef fromSection,
1662 StringRef toSection) {
1663 StringSet<> names;
1664 while (true) {
1665 if (!names.insert(key: toSection).second)
1666 Fatal(ctx) << "/merge: cycle found for section '" << fromSection << "'";
1667 auto i = ctx.config.merge.find(x: toSection);
1668 if (i == ctx.config.merge.end())
1669 break;
1670 toSection = i->second;
1671 }
1672 return toSection;
1673}
1674
1675void Writer::mergeSection(const std::map<StringRef, StringRef>::value_type &p) {
1676 if (p.first == p.second)
1677 return;
1678
1679 StringRef toSection = getMergeDestination(fromSection: p.first, toSection: p.second);
1680
1681 OutputSection *from = findSection(name: p.first);
1682 OutputSection *to = findSection(name: toSection);
1683 if (!from)
1684 return;
1685 if (!to) {
1686 from->name = toSection;
1687 return;
1688 }
1689 to->merge(other: from);
1690}
1691
1692void Writer::mergeSections() {
1693 llvm::TimeTraceScope timeScope("Merge sections");
1694 if (!pdataSec->chunks.empty()) {
1695 if (isArm64EC(Machine: ctx.config.machine)) {
1696 // On ARM64EC .pdata may contain both ARM64 and X64 data. Split them by
1697 // sorting and store their regions separately.
1698 llvm::stable_sort(Range&: pdataSec->chunks, C: [=](const Chunk *a, const Chunk *b) {
1699 return (a->getMachine() == AMD64) < (b->getMachine() == AMD64);
1700 });
1701
1702 for (auto chunk : pdataSec->chunks) {
1703 if (chunk->getMachine() == AMD64) {
1704 hybridPdata.first = chunk;
1705 hybridPdata.last = pdataSec->chunks.back();
1706 break;
1707 }
1708
1709 if (!pdata.first)
1710 pdata.first = chunk;
1711 pdata.last = chunk;
1712 }
1713 } else {
1714 pdata.first = pdataSec->chunks.front();
1715 pdata.last = pdataSec->chunks.back();
1716 }
1717 }
1718
1719 for (auto &p : ctx.config.merge) {
1720 if (p.first != ".bss")
1721 mergeSection(p);
1722 }
1723
1724 // Because .bss contains all zeros, it should be merged at the end of
1725 // whatever section it is being merged into (usually .data) so that the image
1726 // need not actually contain all of the zeros.
1727 auto it = ctx.config.merge.find(x: ".bss");
1728 if (it != ctx.config.merge.end()) {
1729 // Resolve the final merge target name following the chain.
1730 StringRef toSection = getMergeDestination(fromSection: it->first, toSection: it->second);
1731 // Don't merge .bss into a shared section. MSVC link.exe keeps .bss
1732 // separate when the target has IMAGE_SCN_MEM_SHARED, preventing unexpected
1733 // sharing across processes.
1734 auto secIt = ctx.config.section.find(x: toSection);
1735 if (secIt == ctx.config.section.end() ||
1736 !(secIt->second & IMAGE_SCN_MEM_SHARED))
1737 mergeSection(p: {it->first, toSection});
1738 }
1739}
1740
1741// EC targets may have chunks of various architectures mixed together at this
1742// point. Group code chunks of the same architecture together by sorting chunks
1743// by their EC range type.
1744void Writer::sortECChunks() {
1745 if (!isArm64EC(Machine: ctx.config.machine))
1746 return;
1747
1748 for (OutputSection *sec : ctx.outputSections) {
1749 if (sec->isCodeSection())
1750 llvm::stable_sort(Range&: sec->chunks, C: [=](const Chunk *a, const Chunk *b) {
1751 std::optional<chpe_range_type> aType = a->getArm64ECRangeType(),
1752 bType = b->getArm64ECRangeType();
1753 return bType && (!aType || *aType < *bType);
1754 });
1755 }
1756}
1757
1758// Visits all sections to assign incremental, non-overlapping RVAs and
1759// file offsets.
1760void Writer::assignAddresses() {
1761 llvm::TimeTraceScope timeScope("Assign addresses");
1762 Configuration *config = &ctx.config;
1763
1764 // We need to create EC code map so that ECCodeMapChunk knows its size.
1765 // We do it here to make sure that we account for range extension chunks.
1766 createECCodeMap();
1767
1768 sizeOfHeaders = dosStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
1769 sizeof(data_directory) * numberOfDataDirectory +
1770 sizeof(coff_section) * ctx.outputSections.size();
1771 sizeOfHeaders +=
1772 config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
1773 sizeOfHeaders = alignTo(Value: sizeOfHeaders, Align: config->fileAlign);
1774 fileSize = sizeOfHeaders;
1775
1776 // The first page is kept unmapped.
1777 uint64_t rva = alignTo(Value: sizeOfHeaders, Align: config->align);
1778
1779 for (OutputSection *sec : ctx.outputSections) {
1780 llvm::TimeTraceScope timeScope("Section: ", sec->name);
1781 if (sec == relocSec) {
1782 sec->chunks.clear();
1783 addBaserels();
1784 if (ctx.dynamicRelocs) {
1785 ctx.dynamicRelocs->finalize();
1786 relocSec->addChunk(c: ctx.dynamicRelocs);
1787 }
1788 }
1789 uint64_t rawSize = 0, virtualSize = 0;
1790 sec->header.VirtualAddress = rva;
1791
1792 // If /FUNCTIONPADMIN is used, functions are padded in order to create a
1793 // hotpatchable image.
1794 uint32_t padding = sec->isCodeSection() ? config->functionPadMin : 0;
1795 std::optional<chpe_range_type> prevECRange;
1796
1797 for (Chunk *c : sec->chunks) {
1798 // Alignment EC code range baudaries.
1799 if (isArm64EC(Machine: ctx.config.machine) && sec->isCodeSection()) {
1800 std::optional<chpe_range_type> rangeType = c->getArm64ECRangeType();
1801 if (rangeType != prevECRange) {
1802 virtualSize = alignTo(Value: virtualSize, Align: 4096);
1803 prevECRange = rangeType;
1804 }
1805 }
1806 if (padding && c->isHotPatchable())
1807 virtualSize += padding;
1808 // If chunk has EC entry thunk, reserve a space for an offset to the
1809 // thunk.
1810 if (c->getEntryThunk())
1811 virtualSize += sizeof(uint32_t);
1812 virtualSize = alignTo(Value: virtualSize, Align: c->getAlignment());
1813 c->setRVA(rva + virtualSize);
1814 virtualSize += c->getSize();
1815 if (c->hasData)
1816 rawSize = alignTo(Value: virtualSize, Align: config->fileAlign);
1817 }
1818 if (virtualSize > UINT32_MAX)
1819 Err(ctx) << "section larger than 4 GiB: " << sec->name;
1820 sec->header.VirtualSize = virtualSize;
1821 sec->header.SizeOfRawData = rawSize;
1822 if (rawSize != 0)
1823 sec->header.PointerToRawData = fileSize;
1824 rva += alignTo(Value: virtualSize, Align: config->align);
1825 fileSize += alignTo(Value: rawSize, Align: config->fileAlign);
1826 }
1827 sizeOfImage = alignTo(Value: rva, Align: config->align);
1828
1829 // Assign addresses to sections in MergeChunks.
1830 for (MergeChunk *mc : ctx.mergeChunkInstances)
1831 if (mc)
1832 mc->assignSubsectionRVAs();
1833}
1834
1835template <typename PEHeaderTy> void Writer::writeHeader() {
1836 // Write DOS header. For backwards compatibility, the first part of a PE/COFF
1837 // executable consists of an MS-DOS MZ executable. If the executable is run
1838 // under DOS, that program gets run (usually to just print an error message).
1839 // When run under Windows, the loader looks at AddressOfNewExeHeader and uses
1840 // the PE header instead.
1841 Configuration *config = &ctx.config;
1842
1843 uint8_t *buf = buffer->getBufferStart();
1844 auto *dos = reinterpret_cast<dos_header *>(buf);
1845
1846 // Write DOS program.
1847 if (config->dosStub) {
1848 memcpy(dest: buf, src: config->dosStub->getBufferStart(),
1849 n: config->dosStub->getBufferSize());
1850 // MS link.exe accepts an invalid `e_lfanew` (AddressOfNewExeHeader) and
1851 // updates it automatically. Replicate the same behaviour.
1852 dos->AddressOfNewExeHeader = alignTo(Value: config->dosStub->getBufferSize(), Align: 8);
1853 // Unlike MS link.exe, LLD accepts non-8-byte-aligned stubs.
1854 // In that case, we add zero paddings ourselves.
1855 buf += alignTo(Value: config->dosStub->getBufferSize(), Align: 8);
1856 } else {
1857 buf += sizeof(dos_header);
1858 dos->Magic[0] = 'M';
1859 dos->Magic[1] = 'Z';
1860 dos->UsedBytesInTheLastPage = dosStubSize % 512;
1861 dos->FileSizeInPages = divideCeil(Numerator: dosStubSize, Denominator: 512);
1862 dos->HeaderSizeInParagraphs = sizeof(dos_header) / 16;
1863
1864 dos->AddressOfRelocationTable = sizeof(dos_header);
1865 dos->AddressOfNewExeHeader = dosStubSize;
1866
1867 memcpy(dest: buf, src: dosProgram, n: sizeof(dosProgram));
1868 buf += sizeof(dosProgram);
1869 }
1870
1871 // Make sure DOS stub is aligned to 8 bytes at this point
1872 assert((buf - buffer->getBufferStart()) % 8 == 0);
1873
1874 // Write PE magic
1875 memcpy(dest: buf, src: PEMagic, n: sizeof(PEMagic));
1876 buf += sizeof(PEMagic);
1877
1878 // Write COFF header
1879 assert(coffHeaderOffset ==
1880 static_cast<size_t>(buf - buffer->getBufferStart()));
1881 auto *coff = reinterpret_cast<coff_file_header *>(buf);
1882 buf += sizeof(*coff);
1883 SymbolTable &symtab =
1884 ctx.config.machine == ARM64X ? *ctx.hybridSymtab : ctx.symtab;
1885 coff->Machine = symtab.isEC() ? AMD64 : symtab.machine;
1886 coff->NumberOfSections = ctx.outputSections.size();
1887 coff->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
1888 if (config->largeAddressAware)
1889 coff->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
1890 if (!config->is64())
1891 coff->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
1892 if (config->dll)
1893 coff->Characteristics |= IMAGE_FILE_DLL;
1894 if (config->driverUponly)
1895 coff->Characteristics |= IMAGE_FILE_UP_SYSTEM_ONLY;
1896 if (!config->relocatable)
1897 coff->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
1898 if (config->swaprunCD)
1899 coff->Characteristics |= IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP;
1900 if (config->swaprunNet)
1901 coff->Characteristics |= IMAGE_FILE_NET_RUN_FROM_SWAP;
1902 coff->SizeOfOptionalHeader =
1903 sizeof(PEHeaderTy) + sizeof(data_directory) * numberOfDataDirectory;
1904
1905 // Write PE header
1906 assert(peHeaderOffset == static_cast<size_t>(buf - buffer->getBufferStart()));
1907 auto *pe = reinterpret_cast<PEHeaderTy *>(buf);
1908 buf += sizeof(*pe);
1909 pe->Magic = config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
1910
1911 // If {Major,Minor}LinkerVersion is left at 0.0, then for some
1912 // reason signing the resulting PE file with Authenticode produces a
1913 // signature that fails to validate on Windows 7 (but is OK on 10).
1914 // Set it to 14.0, which is what VS2015 outputs, and which avoids
1915 // that problem.
1916 pe->MajorLinkerVersion = 14;
1917 pe->MinorLinkerVersion = 0;
1918
1919 pe->ImageBase = config->imageBase;
1920 pe->SectionAlignment = config->align;
1921 pe->FileAlignment = config->fileAlign;
1922 pe->MajorImageVersion = config->majorImageVersion;
1923 pe->MinorImageVersion = config->minorImageVersion;
1924 pe->MajorOperatingSystemVersion = config->majorOSVersion;
1925 pe->MinorOperatingSystemVersion = config->minorOSVersion;
1926 pe->MajorSubsystemVersion = config->majorSubsystemVersion;
1927 pe->MinorSubsystemVersion = config->minorSubsystemVersion;
1928 pe->Subsystem = config->subsystem;
1929 pe->SizeOfImage = sizeOfImage;
1930 pe->SizeOfHeaders = sizeOfHeaders;
1931 if (!config->noEntry) {
1932 Defined *entry = cast<Defined>(Val: symtab.entry);
1933 pe->AddressOfEntryPoint = entry->getRVA();
1934 // Pointer to thumb code must have the LSB set, so adjust it.
1935 if (config->machine == ARMNT)
1936 pe->AddressOfEntryPoint |= 1;
1937 }
1938 pe->SizeOfStackReserve = config->stackReserve;
1939 pe->SizeOfStackCommit = config->stackCommit;
1940 pe->SizeOfHeapReserve = config->heapReserve;
1941 pe->SizeOfHeapCommit = config->heapCommit;
1942 if (config->appContainer)
1943 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER;
1944 if (config->driverWdm)
1945 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER;
1946 if (config->dynamicBase)
1947 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
1948 if (config->highEntropyVA)
1949 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
1950 if (!config->allowBind)
1951 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND;
1952 if (config->nxCompat)
1953 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
1954 if (!config->allowIsolation)
1955 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION;
1956 if (config->guardCF != GuardCFLevel::Off)
1957 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF;
1958 if (config->integrityCheck)
1959 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
1960 if (setNoSEHCharacteristic || config->noSEH)
1961 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH;
1962 if (config->terminalServerAware)
1963 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
1964 pe->NumberOfRvaAndSize = numberOfDataDirectory;
1965 if (textSec->getVirtualSize()) {
1966 pe->BaseOfCode = textSec->getRVA();
1967 pe->SizeOfCode = textSec->getRawSize();
1968 }
1969 pe->SizeOfInitializedData = getSizeOfInitializedData();
1970
1971 // Write data directory
1972 assert(!ctx.config.is64() ||
1973 dataDirOffset64 ==
1974 static_cast<size_t>(buf - buffer->getBufferStart()));
1975 auto *dir = reinterpret_cast<data_directory *>(buf);
1976 buf += sizeof(*dir) * numberOfDataDirectory;
1977 if (symtab.edataStart) {
1978 dir[EXPORT_TABLE].RelativeVirtualAddress = symtab.edataStart->getRVA();
1979 dir[EXPORT_TABLE].Size = symtab.edataEnd->getRVA() +
1980 symtab.edataEnd->getSize() -
1981 symtab.edataStart->getRVA();
1982 }
1983 if (importTableStart) {
1984 dir[IMPORT_TABLE].RelativeVirtualAddress = importTableStart->getRVA();
1985 dir[IMPORT_TABLE].Size = importTableSize;
1986 }
1987 if (iatStart) {
1988 dir[IAT].RelativeVirtualAddress = iatStart->getRVA();
1989 dir[IAT].Size = iatSize;
1990 }
1991 if (rsrcSec->getVirtualSize()) {
1992 dir[RESOURCE_TABLE].RelativeVirtualAddress = rsrcSec->getRVA();
1993 dir[RESOURCE_TABLE].Size = rsrcSec->getVirtualSize();
1994 }
1995 // ARM64EC (but not ARM64X) contains x86_64 exception table in data directory.
1996 ChunkRange &exceptionTable =
1997 ctx.config.machine == ARM64EC ? hybridPdata : pdata;
1998 if (exceptionTable.first) {
1999 dir[EXCEPTION_TABLE].RelativeVirtualAddress =
2000 exceptionTable.first->getRVA();
2001 dir[EXCEPTION_TABLE].Size = exceptionTable.last->getRVA() +
2002 exceptionTable.last->getSize() -
2003 exceptionTable.first->getRVA();
2004 }
2005 size_t relocSize = relocSec->getVirtualSize();
2006 if (ctx.dynamicRelocs)
2007 relocSize -= ctx.dynamicRelocs->getSize();
2008 if (relocSize) {
2009 dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = relocSec->getRVA();
2010 dir[BASE_RELOCATION_TABLE].Size = relocSize;
2011 }
2012 if (Symbol *sym = symtab.findUnderscore(name: "_tls_used")) {
2013 if (Defined *b = dyn_cast<Defined>(Val: sym)) {
2014 dir[TLS_TABLE].RelativeVirtualAddress = b->getRVA();
2015 dir[TLS_TABLE].Size = config->is64()
2016 ? sizeof(object::coff_tls_directory64)
2017 : sizeof(object::coff_tls_directory32);
2018 }
2019 }
2020 if (debugDirectory) {
2021 dir[DEBUG_DIRECTORY].RelativeVirtualAddress = debugDirectory->getRVA();
2022 dir[DEBUG_DIRECTORY].Size = debugDirectory->getSize();
2023 }
2024 if (symtab.loadConfigSym) {
2025 dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress =
2026 symtab.loadConfigSym->getRVA();
2027 dir[LOAD_CONFIG_TABLE].Size = symtab.loadConfigSize;
2028 }
2029 if (!delayIdata.empty()) {
2030 dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
2031 delayIdata.getDirRVA();
2032 dir[DELAY_IMPORT_DESCRIPTOR].Size = delayIdata.getDirSize();
2033 }
2034
2035 // Write section table
2036 for (OutputSection *sec : ctx.outputSections) {
2037 sec->writeHeaderTo(buf, isDebug: config->debug);
2038 buf += sizeof(coff_section);
2039 }
2040 sectionTable = ArrayRef<uint8_t>(
2041 buf - ctx.outputSections.size() * sizeof(coff_section), buf);
2042
2043 if (outputSymtab.empty() && strtab.empty())
2044 return;
2045
2046 coff->PointerToSymbolTable = pointerToSymbolTable;
2047 uint32_t numberOfSymbols = outputSymtab.size();
2048 coff->NumberOfSymbols = numberOfSymbols;
2049 auto *symbolTable = reinterpret_cast<coff_symbol16 *>(
2050 buffer->getBufferStart() + coff->PointerToSymbolTable);
2051 for (size_t i = 0; i != numberOfSymbols; ++i)
2052 symbolTable[i] = outputSymtab[i];
2053 // Create the string table, it follows immediately after the symbol table.
2054 // The first 4 bytes is length including itself.
2055 buf = reinterpret_cast<uint8_t *>(&symbolTable[numberOfSymbols]);
2056 strtab.write(Buf: buf);
2057}
2058
2059void Writer::openFile(StringRef path) {
2060 buffer = CHECK(
2061 FileOutputBuffer::create(path, fileSize, FileOutputBuffer::F_executable),
2062 "failed to open " + path);
2063}
2064
2065void Writer::createSEHTable() {
2066 SymbolRVASet handlers;
2067 for (ObjFile *file : ctx.objFileInstances) {
2068 if (!file->hasSafeSEH())
2069 Err(ctx) << "/safeseh: " << file->getName()
2070 << " is not compatible with SEH";
2071 markSymbolsForRVATable(file, symIdxChunks: file->getSXDataChunks(), tableSymbols&: handlers);
2072 }
2073
2074 // Set the "no SEH" characteristic if there really were no handlers, or if
2075 // there is no load config object to point to the table of handlers.
2076 setNoSEHCharacteristic =
2077 handlers.empty() || !ctx.symtab.findUnderscore(name: "_load_config_used");
2078
2079 maybeAddRVATable(tableSymbols: std::move(handlers), tableSym: "__safe_se_handler_table",
2080 countSym: "__safe_se_handler_count");
2081}
2082
2083// Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set
2084// cannot contain duplicates. Therefore, the set is uniqued by Chunk and the
2085// symbol's offset into that Chunk.
2086static void addSymbolToRVASet(SymbolRVASet &rvaSet, Defined *s) {
2087 Chunk *c = s->getChunk();
2088 if (!c)
2089 return;
2090 if (auto *sc = dyn_cast<SectionChunk>(Val: c))
2091 c = sc->repl; // Look through ICF replacement.
2092 uint32_t off = s->getRVA() - (c ? c->getRVA() : 0);
2093 rvaSet.insert(V: {.inputChunk: c, .offset: off});
2094}
2095
2096// Given a symbol, add it to the GFIDs table if it is a live, defined, function
2097// symbol in an executable section.
2098static void maybeAddAddressTakenFunction(SymbolRVASet &addressTakenSyms,
2099 Symbol *s) {
2100 if (!s)
2101 return;
2102
2103 switch (s->kind()) {
2104 case Symbol::DefinedLocalImportKind:
2105 case Symbol::DefinedImportDataKind:
2106 // Defines an __imp_ pointer, so it is data, so it is ignored.
2107 break;
2108 case Symbol::DefinedCommonKind:
2109 // Common is always data, so it is ignored.
2110 break;
2111 case Symbol::DefinedAbsoluteKind:
2112 // Absolute is never code, synthetic generally isn't and usually isn't
2113 // determinable.
2114 break;
2115 case Symbol::DefinedSyntheticKind:
2116 // For EC export thunks, mark both the thunk itself and its target.
2117 if (auto expChunk = dyn_cast_or_null<ECExportThunkChunk>(
2118 Val: cast<Defined>(Val: s)->getChunk())) {
2119 addSymbolToRVASet(rvaSet&: addressTakenSyms, s: cast<Defined>(Val: s));
2120 addSymbolToRVASet(rvaSet&: addressTakenSyms, s: expChunk->target);
2121 }
2122 break;
2123 case Symbol::LazyArchiveKind:
2124 case Symbol::LazyObjectKind:
2125 case Symbol::LazyDLLSymbolKind:
2126 case Symbol::UndefinedKind:
2127 // Undefined symbols resolve to zero, so they don't have an RVA. Lazy
2128 // symbols shouldn't have relocations.
2129 break;
2130
2131 case Symbol::DefinedImportThunkKind:
2132 // Thunks are always code, include them.
2133 addSymbolToRVASet(rvaSet&: addressTakenSyms, s: cast<Defined>(Val: s));
2134 break;
2135
2136 case Symbol::DefinedRegularKind: {
2137 // This is a regular, defined, symbol from a COFF file. Mark the symbol as
2138 // address taken if the symbol type is function and it's in an executable
2139 // section.
2140 auto *d = cast<DefinedRegular>(Val: s);
2141 if (d->getCOFFSymbol().getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
2142 SectionChunk *sc = dyn_cast<SectionChunk>(Val: d->getChunk());
2143 if (sc && sc->live &&
2144 sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE)
2145 addSymbolToRVASet(rvaSet&: addressTakenSyms, s: d);
2146 }
2147 break;
2148 }
2149 }
2150}
2151
2152// Visit all relocations from all section contributions of this object file and
2153// mark the relocation target as address-taken.
2154void Writer::markSymbolsWithRelocations(ObjFile *file,
2155 SymbolRVASet &usedSymbols) {
2156 for (Chunk *c : file->getChunks()) {
2157 // We only care about live section chunks. Common chunks and other chunks
2158 // don't generally contain relocations.
2159 SectionChunk *sc = dyn_cast<SectionChunk>(Val: c);
2160 if (!sc || !sc->live)
2161 continue;
2162
2163 for (const coff_relocation &reloc : sc->getRelocs()) {
2164 if (ctx.config.machine == I386 &&
2165 reloc.Type == COFF::IMAGE_REL_I386_REL32)
2166 // Ignore relative relocations on x86. On x86_64 they can't be ignored
2167 // since they're also used to compute absolute addresses.
2168 continue;
2169
2170 Symbol *ref = sc->file->getSymbol(symbolIndex: reloc.SymbolTableIndex);
2171 maybeAddAddressTakenFunction(addressTakenSyms&: usedSymbols, s: ref);
2172 }
2173 }
2174}
2175
2176// Create the guard function id table. This is a table of RVAs of all
2177// address-taken functions. It is sorted and uniqued, just like the safe SEH
2178// table.
2179void Writer::createGuardCFTables() {
2180 Configuration *config = &ctx.config;
2181
2182 if (config->guardCF == GuardCFLevel::Off) {
2183 // MSVC marks the entire image as instrumented if any input object was built
2184 // with /guard:cf.
2185 for (ObjFile *file : ctx.objFileInstances) {
2186 if (file->hasGuardCF()) {
2187 ctx.forEachSymtab(f: [&](SymbolTable &symtab) {
2188 Symbol *flagSym = symtab.findUnderscore(name: "__guard_flags");
2189 cast<DefinedAbsolute>(Val: flagSym)->setVA(
2190 uint32_t(GuardFlags::CF_INSTRUMENTED));
2191 });
2192 break;
2193 }
2194 }
2195 return;
2196 }
2197
2198 SymbolRVASet addressTakenSyms;
2199 SymbolRVASet giatsRVASet;
2200 std::vector<Symbol *> giatsSymbols;
2201 SymbolRVASet longJmpTargets;
2202 SymbolRVASet ehContTargets;
2203 for (ObjFile *file : ctx.objFileInstances) {
2204 // If the object was compiled with /guard:cf, the address taken symbols
2205 // are in .gfids$y sections, and the longjmp targets are in .gljmp$y
2206 // sections. If the object was not compiled with /guard:cf, we assume there
2207 // were no setjmp targets, and that all code symbols with relocations are
2208 // possibly address-taken.
2209 if (file->hasGuardCF()) {
2210 markSymbolsForRVATable(file, symIdxChunks: file->getGuardFidChunks(), tableSymbols&: addressTakenSyms);
2211 markSymbolsForRVATable(file, symIdxChunks: file->getGuardIATChunks(), tableSymbols&: giatsRVASet);
2212 getSymbolsFromSections(file, symIdxChunks: file->getGuardIATChunks(), symbols&: giatsSymbols);
2213 markSymbolsForRVATable(file, symIdxChunks: file->getGuardLJmpChunks(), tableSymbols&: longJmpTargets);
2214 } else {
2215 markSymbolsWithRelocations(file, usedSymbols&: addressTakenSyms);
2216 }
2217 // If the object was compiled with /guard:ehcont, the ehcont targets are in
2218 // .gehcont$y sections.
2219 if (file->hasGuardEHCont())
2220 markSymbolsForRVATable(file, symIdxChunks: file->getGuardEHContChunks(), tableSymbols&: ehContTargets);
2221 }
2222
2223 // Mark the image entry as address-taken.
2224 ctx.forEachSymtab(f: [&](SymbolTable &symtab) {
2225 if (symtab.entry)
2226 maybeAddAddressTakenFunction(addressTakenSyms, s: symtab.entry);
2227
2228 // Mark exported symbols in executable sections as address-taken.
2229 for (Export &e : symtab.exports)
2230 maybeAddAddressTakenFunction(addressTakenSyms, s: e.sym);
2231 });
2232
2233 // For each entry in the .giats table, check if it has a corresponding load
2234 // thunk (e.g. because the DLL that defines it will be delay-loaded) and, if
2235 // so, add the load thunk to the address taken (.gfids) table.
2236 for (Symbol *s : giatsSymbols) {
2237 if (auto *di = dyn_cast<DefinedImportData>(Val: s)) {
2238 if (di->loadThunkSym)
2239 addSymbolToRVASet(rvaSet&: addressTakenSyms, s: di->loadThunkSym);
2240 }
2241 }
2242
2243 // Ensure sections referenced in the gfid table are 16-byte aligned.
2244 for (const ChunkAndOffset &c : addressTakenSyms)
2245 if (c.inputChunk->getAlignment() < 16)
2246 c.inputChunk->setAlignment(16);
2247
2248 maybeAddRVATable(tableSymbols: std::move(addressTakenSyms), tableSym: "__guard_fids_table",
2249 countSym: "__guard_fids_count");
2250
2251 // Add the Guard Address Taken IAT Entry Table (.giats).
2252 maybeAddRVATable(tableSymbols: std::move(giatsRVASet), tableSym: "__guard_iat_table",
2253 countSym: "__guard_iat_count");
2254
2255 // Add the longjmp target table unless the user told us not to.
2256 if (config->guardCF & GuardCFLevel::LongJmp)
2257 maybeAddRVATable(tableSymbols: std::move(longJmpTargets), tableSym: "__guard_longjmp_table",
2258 countSym: "__guard_longjmp_count");
2259
2260 // Add the ehcont target table unless the user told us not to.
2261 if (config->guardCF & GuardCFLevel::EHCont)
2262 maybeAddRVATable(tableSymbols: std::move(ehContTargets), tableSym: "__guard_eh_cont_table",
2263 countSym: "__guard_eh_cont_count");
2264
2265 // Set __guard_flags, which will be used in the load config to indicate that
2266 // /guard:cf was enabled.
2267 uint32_t guardFlags = uint32_t(GuardFlags::CF_INSTRUMENTED) |
2268 uint32_t(GuardFlags::CF_FUNCTION_TABLE_PRESENT);
2269 if (config->guardCF & GuardCFLevel::LongJmp)
2270 guardFlags |= uint32_t(GuardFlags::CF_LONGJUMP_TABLE_PRESENT);
2271 if (config->guardCF & GuardCFLevel::EHCont)
2272 guardFlags |= uint32_t(GuardFlags::EH_CONTINUATION_TABLE_PRESENT);
2273 ctx.forEachSymtab(f: [guardFlags](SymbolTable &symtab) {
2274 Symbol *flagSym = symtab.findUnderscore(name: "__guard_flags");
2275 cast<DefinedAbsolute>(Val: flagSym)->setVA(guardFlags);
2276 });
2277}
2278
2279// Take a list of input sections containing symbol table indices and add those
2280// symbols to a vector. The challenge is that symbol RVAs are not known and
2281// depend on the table size, so we can't directly build a set of integers.
2282void Writer::getSymbolsFromSections(ObjFile *file,
2283 ArrayRef<SectionChunk *> symIdxChunks,
2284 std::vector<Symbol *> &symbols) {
2285 for (SectionChunk *c : symIdxChunks) {
2286 // Skip sections discarded by linker GC. This comes up when a .gfids section
2287 // is associated with something like a vtable and the vtable is discarded.
2288 // In this case, the associated gfids section is discarded, and we don't
2289 // mark the virtual member functions as address-taken by the vtable.
2290 if (!c->live)
2291 continue;
2292
2293 // Validate that the contents look like symbol table indices.
2294 ArrayRef<uint8_t> data = c->getContents();
2295 if (data.size() % 4 != 0) {
2296 Warn(ctx) << "ignoring " << c->getSectionName()
2297 << " symbol table index section in object " << file;
2298 continue;
2299 }
2300
2301 // Read each symbol table index and check if that symbol was included in the
2302 // final link. If so, add it to the vector of symbols.
2303 ArrayRef<ulittle32_t> symIndices(
2304 reinterpret_cast<const ulittle32_t *>(data.data()), data.size() / 4);
2305 ArrayRef<Symbol *> objSymbols = file->getSymbols();
2306 for (uint32_t symIndex : symIndices) {
2307 if (symIndex >= objSymbols.size()) {
2308 Warn(ctx) << "ignoring invalid symbol table index in section "
2309 << c->getSectionName() << " in object " << file;
2310 continue;
2311 }
2312 if (Symbol *s = objSymbols[symIndex]) {
2313 if (s->isLive())
2314 symbols.push_back(x: cast<Symbol>(Val: s));
2315 }
2316 }
2317 }
2318}
2319
2320// Take a list of input sections containing symbol table indices and add those
2321// symbols to an RVA table.
2322void Writer::markSymbolsForRVATable(ObjFile *file,
2323 ArrayRef<SectionChunk *> symIdxChunks,
2324 SymbolRVASet &tableSymbols) {
2325 std::vector<Symbol *> syms;
2326 getSymbolsFromSections(file, symIdxChunks, symbols&: syms);
2327
2328 for (Symbol *s : syms)
2329 addSymbolToRVASet(rvaSet&: tableSymbols, s: cast<Defined>(Val: s));
2330}
2331
2332// Replace the absolute table symbol with a synthetic symbol pointing to
2333// tableChunk so that we can emit base relocations for it and resolve section
2334// relative relocations.
2335void Writer::maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
2336 StringRef countSym, bool hasFlag) {
2337 if (tableSymbols.empty())
2338 return;
2339
2340 NonSectionChunk *tableChunk;
2341 if (hasFlag)
2342 tableChunk = make<RVAFlagTableChunk>(args: std::move(tableSymbols));
2343 else
2344 tableChunk = make<RVATableChunk>(args: std::move(tableSymbols));
2345 rdataSec->addChunk(c: tableChunk);
2346
2347 ctx.forEachSymtab(f: [&](SymbolTable &symtab) {
2348 Symbol *t = symtab.findUnderscore(name: tableSym);
2349 Symbol *c = symtab.findUnderscore(name: countSym);
2350 replaceSymbol<DefinedSynthetic>(s: t, arg: t->getName(), arg&: tableChunk);
2351 cast<DefinedAbsolute>(Val: c)->setVA(tableChunk->getSize() / (hasFlag ? 5 : 4));
2352 });
2353}
2354
2355// Create CHPE metadata chunks.
2356void Writer::createECChunks() {
2357 if (!ctx.symtab.isEC())
2358 return;
2359
2360 for (Symbol *s : ctx.symtab.expSymbols) {
2361 auto sym = dyn_cast<Defined>(Val: s);
2362 if (!sym || !sym->getChunk())
2363 continue;
2364 if (auto thunk = dyn_cast<ECExportThunkChunk>(Val: sym->getChunk())) {
2365 hexpthkSec->addChunk(c: thunk);
2366 exportThunks.push_back(x: {thunk, thunk->target});
2367 } else if (auto def = dyn_cast<DefinedRegular>(Val: sym)) {
2368 // Allow section chunk to be treated as an export thunk if it looks like
2369 // one.
2370 SectionChunk *chunk = def->getChunk();
2371 if (!chunk->live || chunk->getMachine() != AMD64)
2372 continue;
2373 assert(sym->getName().starts_with("EXP+"));
2374 StringRef targetName = sym->getName().substr(Start: strlen(s: "EXP+"));
2375 // If EXP+#foo is an export thunk of a hybrid patchable function,
2376 // we should use the #foo$hp_target symbol as the redirection target.
2377 // First, try to look up the $hp_target symbol. If it can't be found,
2378 // assume it's a regular function and look for #foo instead.
2379 Symbol *targetSym = ctx.symtab.find(name: (targetName + "$hp_target").str());
2380 if (!targetSym)
2381 targetSym = ctx.symtab.find(name: targetName);
2382 Defined *t = dyn_cast_or_null<Defined>(Val: targetSym);
2383 if (t && isArm64EC(Machine: t->getChunk()->getMachine()))
2384 exportThunks.push_back(x: {chunk, t});
2385 }
2386 }
2387
2388 auto codeMapChunk = make<ECCodeMapChunk>(args&: codeMap);
2389 rdataSec->addChunk(c: codeMapChunk);
2390 Symbol *codeMapSym = ctx.symtab.findUnderscore(name: "__hybrid_code_map");
2391 replaceSymbol<DefinedSynthetic>(s: codeMapSym, arg: codeMapSym->getName(),
2392 arg&: codeMapChunk);
2393
2394 CHPECodeRangesChunk *ranges = make<CHPECodeRangesChunk>(args&: exportThunks);
2395 rdataSec->addChunk(c: ranges);
2396 Symbol *rangesSym =
2397 ctx.symtab.findUnderscore(name: "__x64_code_ranges_to_entry_points");
2398 replaceSymbol<DefinedSynthetic>(s: rangesSym, arg: rangesSym->getName(), arg&: ranges);
2399
2400 CHPERedirectionChunk *entryPoints = make<CHPERedirectionChunk>(args&: exportThunks);
2401 a64xrmSec->addChunk(c: entryPoints);
2402 Symbol *entryPointsSym =
2403 ctx.symtab.findUnderscore(name: "__arm64x_redirection_metadata");
2404 replaceSymbol<DefinedSynthetic>(s: entryPointsSym, arg: entryPointsSym->getName(),
2405 arg&: entryPoints);
2406
2407 for (auto thunk : ctx.symtab.sameAddressThunks) {
2408 // Relocation values are set later in setECSymbols.
2409 ctx.dynamicRelocs->add(type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2410 offset: thunk);
2411 ctx.dynamicRelocs->add(type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2412 offset: Arm64XRelocVal(thunk, sizeof(uint32_t)));
2413 }
2414}
2415
2416// MinGW specific. Gather all relocations that are imported from a DLL even
2417// though the code didn't expect it to, produce the table that the runtime
2418// uses for fixing them up, and provide the synthetic symbols that the
2419// runtime uses for finding the table.
2420void Writer::createRuntimePseudoRelocs() {
2421 ctx.forEachSymtab(f: [&](SymbolTable &symtab) {
2422 std::vector<RuntimePseudoReloc> rels;
2423
2424 for (Chunk *c : ctx.driver.getChunks()) {
2425 auto *sc = dyn_cast<SectionChunk>(Val: c);
2426 if (!sc || !sc->live || &sc->file->symtab != &symtab)
2427 continue;
2428 // Don't create pseudo relocations for sections that won't be
2429 // mapped at runtime.
2430 if (sc->header->Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
2431 continue;
2432 sc->getRuntimePseudoRelocs(res&: rels);
2433 }
2434
2435 if (!ctx.config.pseudoRelocs) {
2436 // Not writing any pseudo relocs; if some were needed, error out and
2437 // indicate what required them.
2438 for (const RuntimePseudoReloc &rpr : rels)
2439 Err(ctx) << "automatic dllimport of " << rpr.sym->getName() << " in "
2440 << toString(file: rpr.target->file)
2441 << " requires pseudo relocations";
2442 return;
2443 }
2444
2445 if (!rels.empty()) {
2446 Log(ctx) << "Writing " << Twine(rels.size())
2447 << " runtime pseudo relocations";
2448 const char *symbolName = "_pei386_runtime_relocator";
2449 Symbol *relocator = symtab.findUnderscore(name: symbolName);
2450 if (!relocator)
2451 Err(ctx)
2452 << "output image has runtime pseudo relocations, but the function "
2453 << symbolName
2454 << " is missing; it is needed for fixing the relocations at "
2455 "runtime";
2456 }
2457
2458 PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(args&: rels);
2459 rdataSec->addChunk(c: table);
2460 EmptyChunk *endOfList = make<EmptyChunk>();
2461 rdataSec->addChunk(c: endOfList);
2462
2463 Symbol *headSym = symtab.findUnderscore(name: "__RUNTIME_PSEUDO_RELOC_LIST__");
2464 Symbol *endSym = symtab.findUnderscore(name: "__RUNTIME_PSEUDO_RELOC_LIST_END__");
2465 replaceSymbol<DefinedSynthetic>(s: headSym, arg: headSym->getName(), arg&: table);
2466 replaceSymbol<DefinedSynthetic>(s: endSym, arg: endSym->getName(), arg&: endOfList);
2467 });
2468}
2469
2470// MinGW specific.
2471// The MinGW .ctors and .dtors lists have sentinels at each end;
2472// a (uintptr_t)-1 at the start and a (uintptr_t)0 at the end.
2473// There's a symbol pointing to the start sentinel pointer, __CTOR_LIST__
2474// and __DTOR_LIST__ respectively.
2475void Writer::insertCtorDtorSymbols() {
2476 ctx.forEachSymtab(f: [&](SymbolTable &symtab) {
2477 AbsolutePointerChunk *ctorListHead = make<AbsolutePointerChunk>(args&: symtab, args: -1);
2478 AbsolutePointerChunk *ctorListEnd = make<AbsolutePointerChunk>(args&: symtab, args: 0);
2479 AbsolutePointerChunk *dtorListHead = make<AbsolutePointerChunk>(args&: symtab, args: -1);
2480 AbsolutePointerChunk *dtorListEnd = make<AbsolutePointerChunk>(args&: symtab, args: 0);
2481 ctorsSec->insertChunkAtStart(c: ctorListHead);
2482 ctorsSec->addChunk(c: ctorListEnd);
2483 dtorsSec->insertChunkAtStart(c: dtorListHead);
2484 dtorsSec->addChunk(c: dtorListEnd);
2485
2486 Symbol *ctorListSym = symtab.findUnderscore(name: "__CTOR_LIST__");
2487 Symbol *dtorListSym = symtab.findUnderscore(name: "__DTOR_LIST__");
2488 replaceSymbol<DefinedSynthetic>(s: ctorListSym, arg: ctorListSym->getName(),
2489 arg&: ctorListHead);
2490 replaceSymbol<DefinedSynthetic>(s: dtorListSym, arg: dtorListSym->getName(),
2491 arg&: dtorListHead);
2492 });
2493
2494 if (ctx.hybridSymtab) {
2495 ctorsSec->splitECChunks();
2496 dtorsSec->splitECChunks();
2497 }
2498}
2499
2500// MinGW (really, Cygwin) specific.
2501// The Cygwin startup code uses __data_start__ __data_end__ __bss_start__
2502// and __bss_end__ to know what to copy during fork emulation.
2503void Writer::insertBssDataStartEndSymbols() {
2504 if (!dataSec->chunks.empty()) {
2505 Symbol *dataStartSym = ctx.symtab.find(name: "__data_start__");
2506 Symbol *dataEndSym = ctx.symtab.find(name: "__data_end__");
2507 Chunk *endChunk = dataSec->chunks.back();
2508 replaceSymbol<DefinedSynthetic>(s: dataStartSym, arg: dataStartSym->getName(),
2509 arg&: dataSec->chunks.front());
2510 replaceSymbol<DefinedSynthetic>(s: dataEndSym, arg: dataEndSym->getName(), arg&: endChunk,
2511 arg: endChunk->getSize());
2512 }
2513
2514 if (!bssSec->chunks.empty()) {
2515 Symbol *bssStartSym = ctx.symtab.find(name: "__bss_start__");
2516 Symbol *bssEndSym = ctx.symtab.find(name: "__bss_end__");
2517 Chunk *endChunk = bssSec->chunks.back();
2518 replaceSymbol<DefinedSynthetic>(s: bssStartSym, arg: bssStartSym->getName(),
2519 arg&: bssSec->chunks.front());
2520 replaceSymbol<DefinedSynthetic>(s: bssEndSym, arg: bssEndSym->getName(), arg&: endChunk,
2521 arg: endChunk->getSize());
2522 }
2523}
2524
2525// Handles /section options to allow users to overwrite
2526// section attributes.
2527void Writer::setSectionPermissions() {
2528 llvm::TimeTraceScope timeScope("Sections permissions");
2529 for (auto &p : ctx.config.section) {
2530 StringRef name = p.first;
2531 uint32_t perm = p.second;
2532 for (OutputSection *sec : ctx.outputSections)
2533 if (sec->name == name)
2534 sec->setPermissions(perm);
2535 }
2536}
2537
2538// Set symbols used by ARM64EC metadata.
2539void Writer::setECSymbols() {
2540 if (!ctx.symtab.isEC())
2541 return;
2542
2543 llvm::stable_sort(Range&: exportThunks, C: [](const std::pair<Chunk *, Defined *> &a,
2544 const std::pair<Chunk *, Defined *> &b) {
2545 return a.first->getRVA() < b.first->getRVA();
2546 });
2547
2548 ChunkRange &chpePdata = ctx.config.machine == ARM64X ? hybridPdata : pdata;
2549 Symbol *rfeTableSym = ctx.symtab.findUnderscore(name: "__arm64x_extra_rfe_table");
2550 replaceSymbol<DefinedSynthetic>(s: rfeTableSym, arg: "__arm64x_extra_rfe_table",
2551 arg&: chpePdata.first);
2552
2553 if (chpePdata.first) {
2554 Symbol *rfeSizeSym =
2555 ctx.symtab.findUnderscore(name: "__arm64x_extra_rfe_table_size");
2556 cast<DefinedAbsolute>(Val: rfeSizeSym)
2557 ->setVA(chpePdata.last->getRVA() + chpePdata.last->getSize() -
2558 chpePdata.first->getRVA());
2559 }
2560
2561 Symbol *rangesCountSym =
2562 ctx.symtab.findUnderscore(name: "__x64_code_ranges_to_entry_points_count");
2563 cast<DefinedAbsolute>(Val: rangesCountSym)->setVA(exportThunks.size());
2564
2565 Symbol *entryPointCountSym =
2566 ctx.symtab.findUnderscore(name: "__arm64x_redirection_metadata_count");
2567 cast<DefinedAbsolute>(Val: entryPointCountSym)->setVA(exportThunks.size());
2568
2569 Symbol *iatSym = ctx.symtab.findUnderscore(name: "__hybrid_auxiliary_iat");
2570 replaceSymbol<DefinedSynthetic>(s: iatSym, arg: "__hybrid_auxiliary_iat",
2571 arg: idata.auxIat.empty() ? nullptr
2572 : idata.auxIat.front());
2573
2574 Symbol *iatCopySym = ctx.symtab.findUnderscore(name: "__hybrid_auxiliary_iat_copy");
2575 replaceSymbol<DefinedSynthetic>(
2576 s: iatCopySym, arg: "__hybrid_auxiliary_iat_copy",
2577 arg: idata.auxIatCopy.empty() ? nullptr : idata.auxIatCopy.front());
2578
2579 Symbol *delayIatSym =
2580 ctx.symtab.findUnderscore(name: "__hybrid_auxiliary_delayload_iat");
2581 replaceSymbol<DefinedSynthetic>(
2582 s: delayIatSym, arg: "__hybrid_auxiliary_delayload_iat",
2583 arg: delayIdata.getAuxIat().empty() ? nullptr
2584 : delayIdata.getAuxIat().front());
2585
2586 Symbol *delayIatCopySym =
2587 ctx.symtab.findUnderscore(name: "__hybrid_auxiliary_delayload_iat_copy");
2588 replaceSymbol<DefinedSynthetic>(
2589 s: delayIatCopySym, arg: "__hybrid_auxiliary_delayload_iat_copy",
2590 arg: delayIdata.getAuxIatCopy().empty() ? nullptr
2591 : delayIdata.getAuxIatCopy().front());
2592
2593 if (ctx.config.machine == ARM64X) {
2594 // For the hybrid image, set the alternate entry point to the EC entry
2595 // point. In the hybrid view, it is swapped to the native entry point
2596 // using ARM64X relocations.
2597 if (auto altEntrySym = cast_or_null<Defined>(Val: ctx.symtab.entry)) {
2598 // If the entry is an EC export thunk, use its target instead.
2599 if (auto thunkChunk =
2600 dyn_cast<ECExportThunkChunk>(Val: altEntrySym->getChunk()))
2601 altEntrySym = thunkChunk->target;
2602 ctx.symtab.findUnderscore(name: "__arm64x_native_entrypoint")
2603 ->replaceKeepingName(other: altEntrySym, size: sizeof(SymbolUnion));
2604 }
2605
2606 if (ctx.symtab.edataStart)
2607 ctx.dynamicRelocs->set(
2608 offset: dataDirOffset64 + EXPORT_TABLE * sizeof(data_directory) +
2609 offsetof(data_directory, Size),
2610 value: ctx.symtab.edataEnd->getRVA() - ctx.symtab.edataStart->getRVA() +
2611 ctx.symtab.edataEnd->getSize());
2612 if (hybridPdata.first)
2613 ctx.dynamicRelocs->set(
2614 offset: dataDirOffset64 + EXCEPTION_TABLE * sizeof(data_directory) +
2615 offsetof(data_directory, Size),
2616 value: hybridPdata.last->getRVA() - hybridPdata.first->getRVA() +
2617 hybridPdata.last->getSize());
2618 if (chpeSym && pdata.first)
2619 ctx.dynamicRelocs->set(
2620 offset: chpeSym->getRVA() + offsetof(chpe_metadata, ExtraRFETableSize),
2621 value: pdata.last->getRVA() + pdata.last->getSize() - pdata.first->getRVA());
2622 }
2623
2624 for (SameAddressThunkARM64EC *thunk : ctx.symtab.sameAddressThunks)
2625 thunk->setDynamicRelocs(ctx);
2626}
2627
2628// Write section contents to a mmap'ed file.
2629void Writer::writeSections() {
2630 llvm::TimeTraceScope timeScope("Write sections");
2631 uint8_t *buf = buffer->getBufferStart();
2632 for (OutputSection *sec : ctx.outputSections) {
2633 uint8_t *secBuf = buf + sec->getFileOff();
2634 // Fill gaps between functions in .text with INT3 instructions
2635 // instead of leaving as NUL bytes (which can be interpreted as
2636 // ADD instructions). Only fill the gaps between chunks. Most
2637 // chunks overwrite it anyway, but uninitialized data chunks
2638 // merged into a code section don't.
2639 if ((sec->header.Characteristics & IMAGE_SCN_CNT_CODE) &&
2640 (ctx.config.machine == AMD64 || ctx.config.machine == I386)) {
2641 uint32_t prevEnd = 0;
2642 uint32_t rawSize = sec->getRawSize();
2643 for (Chunk *c : sec->chunks) {
2644 uint32_t off = c->getRVA() - sec->getRVA();
2645 // Chunks without data (e.g., .bss) have virtual addresses beyond
2646 // rawSize; stop filling when we reach the end of raw data.
2647 if (off >= rawSize)
2648 break;
2649 memset(s: secBuf + prevEnd, c: 0xCC, n: off - prevEnd);
2650 prevEnd = std::min(a: off + static_cast<uint32_t>(c->getSize()), b: rawSize);
2651 }
2652 memset(s: secBuf + prevEnd, c: 0xCC, n: rawSize - prevEnd);
2653 }
2654
2655 parallelForEach(R&: sec->chunks, Fn: [&](Chunk *c) {
2656 uint8_t *buf = secBuf + c->getRVA() - sec->getRVA();
2657 c->writeTo(buf);
2658
2659 // Write the offset to EC entry thunk preceding section contents. The low
2660 // bit is always set, so it's effectively an offset from the last byte of
2661 // the offset.
2662 if (Defined *entryThunk = c->getEntryThunk())
2663 write32le(P: buf - sizeof(uint32_t),
2664 V: entryThunk->getRVA() - c->getRVA() + 1);
2665 });
2666 }
2667}
2668
2669void Writer::writeBuildId() {
2670 llvm::TimeTraceScope timeScope("Write build ID");
2671
2672 // There are two important parts to the build ID.
2673 // 1) If building with debug info, the COFF debug directory contains a
2674 // timestamp as well as a Guid and Age of the PDB.
2675 // 2) In all cases, the PE COFF file header also contains a timestamp.
2676 // For reproducibility, instead of a timestamp we want to use a hash of the
2677 // PE contents.
2678 Configuration *config = &ctx.config;
2679 bool generateSyntheticBuildId = config->buildIDHash == BuildIDHash::Binary;
2680 if (generateSyntheticBuildId) {
2681 assert(buildId && "BuildId is not set!");
2682 // BuildId->BuildId was filled in when the PDB was written.
2683 }
2684
2685 // At this point the only fields in the COFF file which remain unset are the
2686 // "timestamp" in the COFF file header, and the ones in the coff debug
2687 // directory. Now we can hash the file and write that hash to the various
2688 // timestamp fields in the file.
2689 StringRef outputFileData(
2690 reinterpret_cast<const char *>(buffer->getBufferStart()),
2691 buffer->getBufferSize());
2692
2693 uint32_t timestamp = config->timestamp;
2694 uint64_t hash = 0;
2695
2696 if (config->repro || generateSyntheticBuildId)
2697 hash = xxh3_64bits(data: outputFileData);
2698
2699 if (config->repro)
2700 timestamp = static_cast<uint32_t>(hash);
2701
2702 if (generateSyntheticBuildId) {
2703 buildId->buildId->PDB70.CVSignature = OMF::Signature::PDB70;
2704 buildId->buildId->PDB70.Age = 1;
2705 memcpy(dest: buildId->buildId->PDB70.Signature, src: &hash, n: 8);
2706 // xxhash only gives us 8 bytes, so put some fixed data in the other half.
2707 memcpy(dest: &buildId->buildId->PDB70.Signature[8], src: "LLD PDB.", n: 8);
2708 }
2709
2710 if (debugDirectory)
2711 debugDirectory->setTimeDateStamp(timestamp);
2712
2713 uint8_t *buf = buffer->getBufferStart();
2714 buf += dosStubSize + sizeof(PEMagic);
2715 object::coff_file_header *coffHeader =
2716 reinterpret_cast<coff_file_header *>(buf);
2717 coffHeader->TimeDateStamp = timestamp;
2718}
2719
2720// Sort .pdata section contents according to PE/COFF spec 5.5.
2721template <typename T>
2722void Writer::sortExceptionTable(ChunkRange &exceptionTable) {
2723 if (!exceptionTable.first)
2724 return;
2725
2726 // We assume .pdata contains function table entries only.
2727 auto bufAddr = [&](Chunk *c) {
2728 OutputSection *os = ctx.getOutputSection(c);
2729 return buffer->getBufferStart() + os->getFileOff() + c->getRVA() -
2730 os->getRVA();
2731 };
2732 uint8_t *begin = bufAddr(exceptionTable.first);
2733 uint8_t *end = bufAddr(exceptionTable.last) + exceptionTable.last->getSize();
2734 if ((end - begin) % sizeof(T) != 0) {
2735 Fatal(ctx) << "unexpected .pdata size: " << (end - begin)
2736 << " is not a multiple of " << sizeof(T);
2737 }
2738
2739 parallelSort(MutableArrayRef<T>(reinterpret_cast<T *>(begin),
2740 reinterpret_cast<T *>(end)),
2741 [](const T &a, const T &b) { return a.begin < b.begin; });
2742}
2743
2744// Sort .pdata section contents according to PE/COFF spec 5.5.
2745void Writer::sortExceptionTables() {
2746 llvm::TimeTraceScope timeScope("Sort exception table");
2747
2748 struct EntryX64 {
2749 ulittle32_t begin, end, unwind;
2750 };
2751 struct EntryArm {
2752 ulittle32_t begin, unwind;
2753 };
2754
2755 switch (ctx.config.machine) {
2756 case AMD64:
2757 sortExceptionTable<EntryX64>(exceptionTable&: pdata);
2758 break;
2759 case ARM64EC:
2760 case ARM64X:
2761 sortExceptionTable<EntryX64>(exceptionTable&: hybridPdata);
2762 [[fallthrough]];
2763 case ARMNT:
2764 case ARM64:
2765 sortExceptionTable<EntryArm>(exceptionTable&: pdata);
2766 break;
2767 default:
2768 if (pdata.first)
2769 ctx.e.errs() << "warning: don't know how to handle .pdata\n";
2770 break;
2771 }
2772}
2773
2774// The CRT section contains, among other things, the array of function
2775// pointers that initialize every global variable that is not trivially
2776// constructed. The CRT calls them one after the other prior to invoking
2777// main().
2778//
2779// As per C++ spec, 3.6.2/2.3,
2780// "Variables with ordered initialization defined within a single
2781// translation unit shall be initialized in the order of their definitions
2782// in the translation unit"
2783//
2784// It is therefore critical to sort the chunks containing the function
2785// pointers in the order that they are listed in the object file (top to
2786// bottom), otherwise global objects might not be initialized in the
2787// correct order.
2788void Writer::sortCRTSectionChunks(std::vector<Chunk *> &chunks) {
2789 auto sectionChunkOrder = [](const Chunk *a, const Chunk *b) {
2790 auto sa = dyn_cast<SectionChunk>(Val: a);
2791 auto sb = dyn_cast<SectionChunk>(Val: b);
2792 assert(sa && sb && "Non-section chunks in CRT section!");
2793
2794 StringRef sAObj = sa->file->mb.getBufferIdentifier();
2795 StringRef sBObj = sb->file->mb.getBufferIdentifier();
2796
2797 return sAObj == sBObj && sa->getSectionNumber() < sb->getSectionNumber();
2798 };
2799 llvm::stable_sort(Range&: chunks, C: sectionChunkOrder);
2800
2801 if (ctx.config.verbose) {
2802 for (auto &c : chunks) {
2803 auto sc = dyn_cast<SectionChunk>(Val: c);
2804 Log(ctx) << " " << sc->file->mb.getBufferIdentifier().str()
2805 << ", SectionID: " << sc->getSectionNumber();
2806 }
2807 }
2808}
2809
2810OutputSection *Writer::findSection(StringRef name) {
2811 for (OutputSection *sec : ctx.outputSections)
2812 if (sec->name == name)
2813 return sec;
2814 return nullptr;
2815}
2816
2817uint32_t Writer::getSizeOfInitializedData() {
2818 uint32_t res = 0;
2819 for (OutputSection *s : ctx.outputSections)
2820 if (s->header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
2821 res += s->getRawSize();
2822 return res;
2823}
2824
2825// Add base relocations to .reloc section.
2826void Writer::addBaserels() {
2827 if (!ctx.config.relocatable)
2828 return;
2829 std::vector<Baserel> v;
2830 for (OutputSection *sec : ctx.outputSections) {
2831 if (sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
2832 continue;
2833 llvm::TimeTraceScope timeScope("Base relocations: ", sec->name);
2834 // Collect all locations for base relocations.
2835 for (Chunk *c : sec->chunks)
2836 c->getBaserels(res: &v);
2837 // Add the addresses to .reloc section.
2838 if (!v.empty())
2839 addBaserelBlocks(v);
2840 v.clear();
2841 }
2842}
2843
2844// Add addresses to .reloc section. Note that addresses are grouped by page.
2845void Writer::addBaserelBlocks(std::vector<Baserel> &v) {
2846 const uint32_t mask = ~uint32_t(pageSize - 1);
2847 uint32_t page = v[0].rva & mask;
2848 size_t i = 0, j = 1;
2849 llvm::sort(C&: v,
2850 Comp: [](const Baserel &x, const Baserel &y) { return x.rva < y.rva; });
2851 for (size_t e = v.size(); j < e; ++j) {
2852 uint32_t p = v[j].rva & mask;
2853 if (p == page)
2854 continue;
2855 relocSec->addChunk(c: make<BaserelChunk>(args&: page, args: &v[i], args: &v[0] + j));
2856 i = j;
2857 page = p;
2858 }
2859 if (i == j)
2860 return;
2861 relocSec->addChunk(c: make<BaserelChunk>(args&: page, args: &v[i], args: &v[0] + j));
2862}
2863
2864void Writer::createDynamicRelocs() {
2865 if (!ctx.dynamicRelocs)
2866 return;
2867
2868 // Adjust the Machine field in the COFF header to AMD64.
2869 ctx.dynamicRelocs->add(type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint16_t),
2870 offset: coffHeaderOffset + offsetof(coff_file_header, Machine),
2871 value: AMD64);
2872
2873 if (ctx.symtab.entry != ctx.hybridSymtab->entry ||
2874 pdata.first != hybridPdata.first) {
2875 chpeSym = cast_or_null<DefinedRegular>(
2876 Val: ctx.symtab.findUnderscore(name: "__chpe_metadata"));
2877 if (!chpeSym)
2878 Warn(ctx) << "'__chpe_metadata' is missing for ARM64X target";
2879 }
2880
2881 if (ctx.symtab.entry != ctx.hybridSymtab->entry) {
2882 ctx.dynamicRelocs->add(type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2883 offset: peHeaderOffset +
2884 offsetof(pe32plus_header, AddressOfEntryPoint),
2885 value: cast_or_null<Defined>(Val: ctx.symtab.entry));
2886
2887 // Swap the alternate entry point in the CHPE metadata.
2888 if (chpeSym)
2889 ctx.dynamicRelocs->add(
2890 type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2891 offset: Arm64XRelocVal(chpeSym, offsetof(chpe_metadata, AlternateEntryPoint)),
2892 value: cast_or_null<Defined>(Val: ctx.hybridSymtab->entry));
2893 }
2894
2895 if (ctx.symtab.edataStart != ctx.hybridSymtab->edataStart) {
2896 ctx.dynamicRelocs->add(type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2897 offset: dataDirOffset64 +
2898 EXPORT_TABLE * sizeof(data_directory) +
2899 offsetof(data_directory, RelativeVirtualAddress),
2900 value: ctx.symtab.edataStart);
2901 // The Size value is assigned after addresses are finalized.
2902 ctx.dynamicRelocs->add(type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2903 offset: dataDirOffset64 +
2904 EXPORT_TABLE * sizeof(data_directory) +
2905 offsetof(data_directory, Size));
2906 }
2907
2908 if (pdata.first != hybridPdata.first) {
2909 ctx.dynamicRelocs->add(type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2910 offset: dataDirOffset64 +
2911 EXCEPTION_TABLE * sizeof(data_directory) +
2912 offsetof(data_directory, RelativeVirtualAddress),
2913 value: hybridPdata.first);
2914 // The Size value is assigned after addresses are finalized.
2915 ctx.dynamicRelocs->add(type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2916 offset: dataDirOffset64 +
2917 EXCEPTION_TABLE * sizeof(data_directory) +
2918 offsetof(data_directory, Size));
2919
2920 // Swap ExtraRFETable in the CHPE metadata.
2921 if (chpeSym) {
2922 ctx.dynamicRelocs->add(
2923 type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2924 offset: Arm64XRelocVal(chpeSym, offsetof(chpe_metadata, ExtraRFETable)),
2925 value: pdata.first);
2926 // The Size value is assigned after addresses are finalized.
2927 ctx.dynamicRelocs->add(
2928 type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2929 offset: Arm64XRelocVal(chpeSym, offsetof(chpe_metadata, ExtraRFETableSize)));
2930 }
2931 }
2932
2933 // Set the hybrid load config to the EC load config.
2934 ctx.dynamicRelocs->add(type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2935 offset: dataDirOffset64 +
2936 LOAD_CONFIG_TABLE * sizeof(data_directory) +
2937 offsetof(data_directory, RelativeVirtualAddress),
2938 value: ctx.symtab.loadConfigSym);
2939 ctx.dynamicRelocs->add(type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2940 offset: dataDirOffset64 +
2941 LOAD_CONFIG_TABLE * sizeof(data_directory) +
2942 offsetof(data_directory, Size),
2943 value: ctx.symtab.loadConfigSize);
2944
2945 auto nativeTlsUsed =
2946 dyn_cast_or_null<Defined>(Val: ctx.hybridSymtab->findUnderscore(name: "_tls_used"));
2947 auto ecTlsUsed =
2948 dyn_cast_or_null<Defined>(Val: ctx.symtab.findUnderscore(name: "_tls_used"));
2949 if (nativeTlsUsed || ecTlsUsed) {
2950 ctx.dynamicRelocs->add(type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2951 offset: dataDirOffset64 +
2952 TLS_TABLE * sizeof(data_directory) +
2953 offsetof(data_directory, RelativeVirtualAddress),
2954 value: Arm64XRelocVal(ecTlsUsed));
2955 if (!nativeTlsUsed || !ecTlsUsed)
2956 ctx.dynamicRelocs->add(
2957 type: IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, size: sizeof(uint32_t),
2958 offset: dataDirOffset64 + TLS_TABLE * sizeof(data_directory) +
2959 offsetof(data_directory, Size),
2960 value: ecTlsUsed ? sizeof(coff_tls_directory64) : 0);
2961 }
2962}
2963
2964PartialSection *Writer::createPartialSection(StringRef name,
2965 uint32_t outChars) {
2966 PartialSection *&pSec = partialSections[{.name: name, .characteristics: outChars}];
2967 if (pSec)
2968 return pSec;
2969 pSec = make<PartialSection>(args&: name, args&: outChars);
2970 return pSec;
2971}
2972
2973PartialSection *Writer::findPartialSection(StringRef name, uint32_t outChars) {
2974 auto it = partialSections.find(x: {.name: name, .characteristics: outChars});
2975 if (it != partialSections.end())
2976 return it->second;
2977 return nullptr;
2978}
2979
2980void Writer::fixTlsAlignment(SymbolTable &symtab) {
2981 Defined *tlsSym =
2982 dyn_cast_or_null<Defined>(Val: symtab.findUnderscore(name: "_tls_used"));
2983 if (!tlsSym)
2984 return;
2985
2986 OutputSection *sec = ctx.getOutputSection(c: tlsSym->getChunk());
2987 assert(sec && tlsSym->getRVA() >= sec->getRVA() &&
2988 "no output section for _tls_used");
2989
2990 uint8_t *secBuf = buffer->getBufferStart() + sec->getFileOff();
2991 uint64_t tlsOffset = tlsSym->getRVA() - sec->getRVA();
2992 uint64_t directorySize = ctx.config.is64()
2993 ? sizeof(object::coff_tls_directory64)
2994 : sizeof(object::coff_tls_directory32);
2995
2996 if (tlsOffset + directorySize > sec->getRawSize())
2997 Fatal(ctx) << "_tls_used sym is malformed";
2998
2999 if (ctx.config.is64()) {
3000 object::coff_tls_directory64 *tlsDir =
3001 reinterpret_cast<object::coff_tls_directory64 *>(&secBuf[tlsOffset]);
3002 tlsDir->setAlignment(tlsAlignment);
3003 } else {
3004 object::coff_tls_directory32 *tlsDir =
3005 reinterpret_cast<object::coff_tls_directory32 *>(&secBuf[tlsOffset]);
3006 tlsDir->setAlignment(tlsAlignment);
3007 }
3008}
3009
3010void Writer::prepareLoadConfig() {
3011 ctx.forEachActiveSymtab(f: [&](SymbolTable &symtab) {
3012 if (!symtab.loadConfigSym)
3013 return;
3014
3015 OutputSection *sec = ctx.getOutputSection(c: symtab.loadConfigSym->getChunk());
3016 uint8_t *secBuf = buffer->getBufferStart() + sec->getFileOff();
3017 uint8_t *symBuf = secBuf + (symtab.loadConfigSym->getRVA() - sec->getRVA());
3018
3019 if (ctx.config.is64())
3020 prepareLoadConfig(symtab,
3021 loadConfig: reinterpret_cast<coff_load_configuration64 *>(symBuf));
3022 else
3023 prepareLoadConfig(symtab,
3024 loadConfig: reinterpret_cast<coff_load_configuration32 *>(symBuf));
3025 });
3026}
3027
3028template <typename T>
3029void Writer::prepareLoadConfig(SymbolTable &symtab, T *loadConfig) {
3030 size_t loadConfigSize = loadConfig->Size;
3031
3032#define RETURN_IF_NOT_CONTAINS(field) \
3033 if (loadConfigSize < offsetof(T, field) + sizeof(T::field)) { \
3034 Warn(ctx) << "'_load_config_used' structure too small to include " #field; \
3035 return; \
3036 }
3037
3038#define IF_CONTAINS(field) \
3039 if (loadConfigSize >= offsetof(T, field) + sizeof(T::field))
3040
3041#define CHECK_VA(field, sym) \
3042 if (auto *s = dyn_cast<DefinedSynthetic>(symtab.findUnderscore(sym))) \
3043 if (loadConfig->field != ctx.config.imageBase + s->getRVA()) \
3044 Warn(ctx) << #field " not set correctly in '_load_config_used'";
3045
3046#define CHECK_ABSOLUTE(field, sym) \
3047 if (auto *s = dyn_cast<DefinedAbsolute>(symtab.findUnderscore(sym))) \
3048 if (loadConfig->field != s->getVA()) \
3049 Warn(ctx) << #field " not set correctly in '_load_config_used'";
3050
3051 if (ctx.config.dependentLoadFlags) {
3052 RETURN_IF_NOT_CONTAINS(DependentLoadFlags)
3053 loadConfig->DependentLoadFlags = ctx.config.dependentLoadFlags;
3054 }
3055
3056 if (ctx.dynamicRelocs) {
3057 IF_CONTAINS(DynamicValueRelocTableSection) {
3058 loadConfig->DynamicValueRelocTableSection = relocSec->sectionIndex;
3059 loadConfig->DynamicValueRelocTableOffset =
3060 ctx.dynamicRelocs->getRVA() - relocSec->getRVA();
3061 }
3062 else {
3063 Warn(ctx) << "'_load_config_used' structure too small to include dynamic "
3064 "relocations";
3065 }
3066 }
3067
3068 IF_CONTAINS(CHPEMetadataPointer) {
3069 // On ARM64X, only the EC version of the load config contains
3070 // CHPEMetadataPointer. Copy its value to the native load config.
3071 if (ctx.config.machine == ARM64X && !symtab.isEC() &&
3072 ctx.symtab.loadConfigSize >=
3073 offsetof(T, CHPEMetadataPointer) + sizeof(T::CHPEMetadataPointer)) {
3074 OutputSection *sec =
3075 ctx.getOutputSection(c: ctx.symtab.loadConfigSym->getChunk());
3076 uint8_t *secBuf = buffer->getBufferStart() + sec->getFileOff();
3077 auto hybridLoadConfig =
3078 reinterpret_cast<const coff_load_configuration64 *>(
3079 secBuf + (ctx.symtab.loadConfigSym->getRVA() - sec->getRVA()));
3080 loadConfig->CHPEMetadataPointer = hybridLoadConfig->CHPEMetadataPointer;
3081 }
3082 }
3083
3084 if (ctx.config.guardCF == GuardCFLevel::Off)
3085 return;
3086 RETURN_IF_NOT_CONTAINS(GuardFlags)
3087 CHECK_VA(GuardCFFunctionTable, "__guard_fids_table")
3088 CHECK_ABSOLUTE(GuardCFFunctionCount, "__guard_fids_count")
3089 CHECK_ABSOLUTE(GuardFlags, "__guard_flags")
3090 IF_CONTAINS(GuardAddressTakenIatEntryCount) {
3091 CHECK_VA(GuardAddressTakenIatEntryTable, "__guard_iat_table")
3092 CHECK_ABSOLUTE(GuardAddressTakenIatEntryCount, "__guard_iat_count")
3093 }
3094
3095 if (!(ctx.config.guardCF & GuardCFLevel::LongJmp))
3096 return;
3097 RETURN_IF_NOT_CONTAINS(GuardLongJumpTargetCount)
3098 CHECK_VA(GuardLongJumpTargetTable, "__guard_longjmp_table")
3099 CHECK_ABSOLUTE(GuardLongJumpTargetCount, "__guard_longjmp_count")
3100
3101 if (!(ctx.config.guardCF & GuardCFLevel::EHCont))
3102 return;
3103 RETURN_IF_NOT_CONTAINS(GuardEHContinuationCount)
3104 CHECK_VA(GuardEHContinuationTable, "__guard_eh_cont_table")
3105 CHECK_ABSOLUTE(GuardEHContinuationCount, "__guard_eh_cont_count")
3106
3107#undef RETURN_IF_NOT_CONTAINS
3108#undef IF_CONTAINS
3109#undef CHECK_VA
3110#undef CHECK_ABSOLUTE
3111}
3112
3113void Writer::printSummary() {
3114 if (!ctx.config.showSummary)
3115 return;
3116
3117 SmallString<256> buffer;
3118 raw_svector_ostream stream(buffer);
3119
3120 stream << center_justify(Str: "Summary", Width: 80) << '\n'
3121 << std::string(80, '-') << '\n';
3122
3123 auto print = [&](uint64_t v, StringRef s) {
3124 stream << formatv(Fmt: "{0}",
3125 Vals: fmt_align(Item: formatv(Fmt: "{0:N}", Vals&: v), Where: AlignStyle::Right, Amount: 20))
3126 << " " << s << '\n';
3127 };
3128
3129 bool hasStats = ctx.pdbStats.has_value();
3130
3131 print(ctx.objFileInstances.size(),
3132 "Input OBJ files (expanded from all cmd-line inputs)");
3133 print(ctx.consumedInputsSize,
3134 "Size of all consumed OBJ files (non-lazy), in bytes");
3135 print(ctx.typeServerSourceMappings.size(), "PDB type server dependencies");
3136 print(ctx.precompSourceMappings.size(), "Precomp OBJ dependencies");
3137 print(hasStats ? ctx.pdbStats->nbTypeRecords : 0, "Input debug type records");
3138 print(hasStats ? ctx.pdbStats->nbTypeRecordsBytes : 0,
3139 "Size of all input debug type records, in bytes");
3140 print(hasStats ? ctx.pdbStats->nbTPIrecords : 0, "Merged TPI records");
3141 print(hasStats ? ctx.pdbStats->nbIPIrecords : 0, "Merged IPI records");
3142 print(hasStats ? ctx.pdbStats->strTabSize : 0, "Output PDB strings");
3143 print(hasStats ? ctx.pdbStats->globalSymbols : 0, "Global symbol records");
3144 print(hasStats ? ctx.pdbStats->moduleSymbols : 0, "Module symbol records");
3145 print(hasStats ? ctx.pdbStats->publicSymbols : 0, "Public symbol records");
3146
3147 if (hasStats)
3148 stream << ctx.pdbStats->largeInputTypeRecs;
3149
3150 Msg(ctx) << buffer;
3151}
3152