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