[lld][WebAssembly] Add `--table-base` setting
[llvm-project.git] / lld / COFF / Writer.cpp
blobc9b6318fe5212b044c258c011f39cc24861e44e3
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 //===----------------------------------------------------------------------===//
9 #include "Writer.h"
10 #include "COFFLinkerContext.h"
11 #include "CallGraphSort.h"
12 #include "Config.h"
13 #include "DLL.h"
14 #include "InputFiles.h"
15 #include "LLDMapFile.h"
16 #include "MapFile.h"
17 #include "PDB.h"
18 #include "SymbolTable.h"
19 #include "Symbols.h"
20 #include "lld/Common/ErrorHandler.h"
21 #include "lld/Common/Memory.h"
22 #include "lld/Common/Timer.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/StringSet.h"
26 #include "llvm/BinaryFormat/COFF.h"
27 #include "llvm/Support/BinaryStreamReader.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Endian.h"
30 #include "llvm/Support/FileOutputBuffer.h"
31 #include "llvm/Support/Parallel.h"
32 #include "llvm/Support/Path.h"
33 #include "llvm/Support/RandomNumberGenerator.h"
34 #include "llvm/Support/xxhash.h"
35 #include <algorithm>
36 #include <cstdio>
37 #include <map>
38 #include <memory>
39 #include <utility>
41 using namespace llvm;
42 using namespace llvm::COFF;
43 using namespace llvm::object;
44 using namespace llvm::support;
45 using namespace llvm::support::endian;
46 using namespace lld;
47 using namespace lld::coff;
49 /* To re-generate DOSProgram:
50 $ cat > /tmp/DOSProgram.asm
51 org 0
52 ; Copy cs to ds.
53 push cs
54 pop ds
55 ; Point ds:dx at the $-terminated string.
56 mov dx, str
57 ; Int 21/AH=09h: Write string to standard output.
58 mov ah, 0x9
59 int 0x21
60 ; Int 21/AH=4Ch: Exit with return code (in AL).
61 mov ax, 0x4C01
62 int 0x21
63 str:
64 db 'This program cannot be run in DOS mode.$'
65 align 8, db 0
66 $ nasm -fbin /tmp/DOSProgram.asm -o /tmp/DOSProgram.bin
67 $ xxd -i /tmp/DOSProgram.bin
69 static unsigned char dosProgram[] = {
70 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8, 0x01, 0x4c,
71 0xcd, 0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72,
72 0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x62, 0x65,
73 0x20, 0x72, 0x75, 0x6e, 0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20,
74 0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x24, 0x00, 0x00
76 static_assert(sizeof(dosProgram) % 8 == 0,
77 "DOSProgram size must be multiple of 8");
79 static const int dosStubSize = sizeof(dos_header) + sizeof(dosProgram);
80 static_assert(dosStubSize % 8 == 0, "DOSStub size must be multiple of 8");
82 static const int numberOfDataDirectory = 16;
84 namespace {
86 class DebugDirectoryChunk : public NonSectionChunk {
87 public:
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) {}
93 size_t getSize() const override {
94 return (records.size() + int(writeRepro)) * sizeof(debug_directory);
97 void writeTo(uint8_t *b) const override {
98 auto *d = reinterpret_cast<debug_directory *>(b);
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, record.first, c->getSize(), c->getRVA(), offs);
105 ++d;
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, COFF::IMAGE_DEBUG_TYPE_REPRO, 0, 0, 0);
118 void setTimeDateStamp(uint32_t timeDateStamp) {
119 for (support::ulittle32_t *tds : timeDateStamps)
120 *tds = timeDateStamp;
123 private:
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;
135 timeDateStamps.push_back(&d->TimeDateStamp);
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;
144 class CVDebugRecordChunk : public NonSectionChunk {
145 public:
146 CVDebugRecordChunk(const COFFLinkerContext &c) : ctx(c) {}
148 size_t getSize() const override {
149 return sizeof(codeview::DebugInfo) + ctx.config.pdbAltPath.size() + 1;
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);
157 // variable sized field (PDB Path)
158 char *p = reinterpret_cast<char *>(b + sizeof(*buildId));
159 if (!ctx.config.pdbAltPath.empty())
160 memcpy(p, ctx.config.pdbAltPath.data(), ctx.config.pdbAltPath.size());
161 p[ctx.config.pdbAltPath.size()] = '\0';
164 mutable codeview::DebugInfo *buildId = nullptr;
166 private:
167 const COFFLinkerContext &ctx;
170 class ExtendedDllCharacteristicsChunk : public NonSectionChunk {
171 public:
172 ExtendedDllCharacteristicsChunk(uint32_t c) : characteristics(c) {}
174 size_t getSize() const override { return 4; }
176 void writeTo(uint8_t *buf) const override { write32le(buf, characteristics); }
178 uint32_t characteristics = 0;
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.
184 class PartialSectionKey {
185 public:
186 StringRef name;
187 unsigned characteristics;
189 bool operator<(const PartialSectionKey &other) const {
190 int c = name.compare(other.name);
191 if (c > 0)
192 return false;
193 if (c == 0)
194 return characteristics < other.characteristics;
195 return true;
199 // The writer writes a SymbolTable result to a file.
200 class Writer {
201 public:
202 Writer(COFFLinkerContext &c)
203 : buffer(errorHandler().outputBuffer), delayIdata(c), edata(c), ctx(c) {}
204 void run();
206 private:
207 void createSections();
208 void createMiscChunks();
209 void createImportTables();
210 void appendImportThunks();
211 void locateImportTables();
212 void createExportTable();
213 void mergeSections();
214 void removeUnusedSections();
215 void assignAddresses();
216 bool isInRange(uint16_t relType, uint64_t s, uint64_t p, int margin);
217 std::pair<Defined *, bool> getThunk(DenseMap<uint64_t, Defined *> &lastThunks,
218 Defined *target, uint64_t p,
219 uint16_t type, int margin);
220 bool createThunks(OutputSection *os, int margin);
221 bool verifyRanges(const std::vector<Chunk *> chunks);
222 void finalizeAddresses();
223 void removeEmptySections();
224 void assignOutputSectionIndices();
225 void createSymbolAndStringTable();
226 void openFile(StringRef outputPath);
227 template <typename PEHeaderTy> void writeHeader();
228 void createSEHTable();
229 void createRuntimePseudoRelocs();
230 void insertCtorDtorSymbols();
231 void markSymbolsWithRelocations(ObjFile *file, SymbolRVASet &usedSymbols);
232 void createGuardCFTables();
233 void markSymbolsForRVATable(ObjFile *file,
234 ArrayRef<SectionChunk *> symIdxChunks,
235 SymbolRVASet &tableSymbols);
236 void getSymbolsFromSections(ObjFile *file,
237 ArrayRef<SectionChunk *> symIdxChunks,
238 std::vector<Symbol *> &symbols);
239 void maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
240 StringRef countSym, bool hasFlag=false);
241 void setSectionPermissions();
242 void writeSections();
243 void writeBuildId();
244 void writePEChecksum();
245 void sortSections();
246 void sortExceptionTable();
247 void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
248 void addSyntheticIdata();
249 void sortBySectionOrder(std::vector<Chunk *> &chunks);
250 void fixPartialSectionChars(StringRef name, uint32_t chars);
251 bool fixGnuImportChunks();
252 void fixTlsAlignment();
253 PartialSection *createPartialSection(StringRef name, uint32_t outChars);
254 PartialSection *findPartialSection(StringRef name, uint32_t outChars);
256 std::optional<coff_symbol16> createSymbol(Defined *d);
257 size_t addEntryToStringTable(StringRef str);
259 OutputSection *findSection(StringRef name);
260 void addBaserels();
261 void addBaserelBlocks(std::vector<Baserel> &v);
263 uint32_t getSizeOfInitializedData();
265 void checkLoadConfig();
266 template <typename T> void checkLoadConfigGuardData(const T *loadConfig);
268 std::unique_ptr<FileOutputBuffer> &buffer;
269 std::map<PartialSectionKey, PartialSection *> partialSections;
270 std::vector<char> strtab;
271 std::vector<llvm::object::coff_symbol16> outputSymtab;
272 IdataContents idata;
273 Chunk *importTableStart = nullptr;
274 uint64_t importTableSize = 0;
275 Chunk *edataStart = nullptr;
276 Chunk *edataEnd = nullptr;
277 Chunk *iatStart = nullptr;
278 uint64_t iatSize = 0;
279 DelayLoadContents delayIdata;
280 EdataContents edata;
281 bool setNoSEHCharacteristic = false;
282 uint32_t tlsAlignment = 0;
284 DebugDirectoryChunk *debugDirectory = nullptr;
285 std::vector<std::pair<COFF::DebugType, Chunk *>> debugRecords;
286 CVDebugRecordChunk *buildId = nullptr;
287 ArrayRef<uint8_t> sectionTable;
289 uint64_t fileSize;
290 uint32_t pointerToSymbolTable = 0;
291 uint64_t sizeOfImage;
292 uint64_t sizeOfHeaders;
294 OutputSection *textSec;
295 OutputSection *rdataSec;
296 OutputSection *buildidSec;
297 OutputSection *dataSec;
298 OutputSection *pdataSec;
299 OutputSection *idataSec;
300 OutputSection *edataSec;
301 OutputSection *didatSec;
302 OutputSection *rsrcSec;
303 OutputSection *relocSec;
304 OutputSection *ctorsSec;
305 OutputSection *dtorsSec;
307 // The first and last .pdata sections in the output file.
309 // We need to keep track of the location of .pdata in whichever section it
310 // gets merged into so that we can sort its contents and emit a correct data
311 // directory entry for the exception table. This is also the case for some
312 // other sections (such as .edata) but because the contents of those sections
313 // are entirely linker-generated we can keep track of their locations using
314 // the chunks that the linker creates. All .pdata chunks come from input
315 // files, so we need to keep track of them separately.
316 Chunk *firstPdata = nullptr;
317 Chunk *lastPdata;
319 COFFLinkerContext &ctx;
321 } // anonymous namespace
323 void lld::coff::writeResult(COFFLinkerContext &ctx) { Writer(ctx).run(); }
325 void OutputSection::addChunk(Chunk *c) {
326 chunks.push_back(c);
329 void OutputSection::insertChunkAtStart(Chunk *c) {
330 chunks.insert(chunks.begin(), c);
333 void OutputSection::setPermissions(uint32_t c) {
334 header.Characteristics &= ~permMask;
335 header.Characteristics |= c;
338 void OutputSection::merge(OutputSection *other) {
339 chunks.insert(chunks.end(), other->chunks.begin(), other->chunks.end());
340 other->chunks.clear();
341 contribSections.insert(contribSections.end(), other->contribSections.begin(),
342 other->contribSections.end());
343 other->contribSections.clear();
346 // Write the section header to a given buffer.
347 void OutputSection::writeHeaderTo(uint8_t *buf, bool isDebug) {
348 auto *hdr = reinterpret_cast<coff_section *>(buf);
349 *hdr = header;
350 if (stringTableOff) {
351 // If name is too long, write offset into the string table as a name.
352 encodeSectionName(hdr->Name, stringTableOff);
353 } else {
354 assert(!isDebug || name.size() <= COFF::NameSize ||
355 (hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0);
356 strncpy(hdr->Name, name.data(),
357 std::min(name.size(), (size_t)COFF::NameSize));
361 void OutputSection::addContributingPartialSection(PartialSection *sec) {
362 contribSections.push_back(sec);
365 // Check whether the target address S is in range from a relocation
366 // of type relType at address P.
367 bool Writer::isInRange(uint16_t relType, uint64_t s, uint64_t p, int margin) {
368 if (ctx.config.machine == ARMNT) {
369 int64_t diff = AbsoluteDifference(s, p + 4) + margin;
370 switch (relType) {
371 case IMAGE_REL_ARM_BRANCH20T:
372 return isInt<21>(diff);
373 case IMAGE_REL_ARM_BRANCH24T:
374 case IMAGE_REL_ARM_BLX23T:
375 return isInt<25>(diff);
376 default:
377 return true;
379 } else if (ctx.config.machine == ARM64) {
380 int64_t diff = AbsoluteDifference(s, p) + margin;
381 switch (relType) {
382 case IMAGE_REL_ARM64_BRANCH26:
383 return isInt<28>(diff);
384 case IMAGE_REL_ARM64_BRANCH19:
385 return isInt<21>(diff);
386 case IMAGE_REL_ARM64_BRANCH14:
387 return isInt<16>(diff);
388 default:
389 return true;
391 } else {
392 llvm_unreachable("Unexpected architecture");
396 // Return the last thunk for the given target if it is in range,
397 // or create a new one.
398 std::pair<Defined *, bool>
399 Writer::getThunk(DenseMap<uint64_t, Defined *> &lastThunks, Defined *target,
400 uint64_t p, uint16_t type, int margin) {
401 Defined *&lastThunk = lastThunks[target->getRVA()];
402 if (lastThunk && isInRange(type, lastThunk->getRVA(), p, margin))
403 return {lastThunk, false};
404 Chunk *c;
405 switch (ctx.config.machine) {
406 case ARMNT:
407 c = make<RangeExtensionThunkARM>(ctx, target);
408 break;
409 case ARM64:
410 c = make<RangeExtensionThunkARM64>(ctx, target);
411 break;
412 default:
413 llvm_unreachable("Unexpected architecture");
415 Defined *d = make<DefinedSynthetic>("range_extension_thunk", c);
416 lastThunk = d;
417 return {d, true};
420 // This checks all relocations, and for any relocation which isn't in range
421 // it adds a thunk after the section chunk that contains the relocation.
422 // If the latest thunk for the specific target is in range, that is used
423 // instead of creating a new thunk. All range checks are done with the
424 // specified margin, to make sure that relocations that originally are in
425 // range, but only barely, also get thunks - in case other added thunks makes
426 // the target go out of range.
428 // After adding thunks, we verify that all relocations are in range (with
429 // no extra margin requirements). If this failed, we restart (throwing away
430 // the previously created thunks) and retry with a wider margin.
431 bool Writer::createThunks(OutputSection *os, int margin) {
432 bool addressesChanged = false;
433 DenseMap<uint64_t, Defined *> lastThunks;
434 DenseMap<std::pair<ObjFile *, Defined *>, uint32_t> thunkSymtabIndices;
435 size_t thunksSize = 0;
436 // Recheck Chunks.size() each iteration, since we can insert more
437 // elements into it.
438 for (size_t i = 0; i != os->chunks.size(); ++i) {
439 SectionChunk *sc = dyn_cast_or_null<SectionChunk>(os->chunks[i]);
440 if (!sc)
441 continue;
442 size_t thunkInsertionSpot = i + 1;
444 // Try to get a good enough estimate of where new thunks will be placed.
445 // Offset this by the size of the new thunks added so far, to make the
446 // estimate slightly better.
447 size_t thunkInsertionRVA = sc->getRVA() + sc->getSize() + thunksSize;
448 ObjFile *file = sc->file;
449 std::vector<std::pair<uint32_t, uint32_t>> relocReplacements;
450 ArrayRef<coff_relocation> originalRelocs =
451 file->getCOFFObj()->getRelocations(sc->header);
452 for (size_t j = 0, e = originalRelocs.size(); j < e; ++j) {
453 const coff_relocation &rel = originalRelocs[j];
454 Symbol *relocTarget = file->getSymbol(rel.SymbolTableIndex);
456 // The estimate of the source address P should be pretty accurate,
457 // but we don't know whether the target Symbol address should be
458 // offset by thunksSize or not (or by some of thunksSize but not all of
459 // it), giving us some uncertainty once we have added one thunk.
460 uint64_t p = sc->getRVA() + rel.VirtualAddress + thunksSize;
462 Defined *sym = dyn_cast_or_null<Defined>(relocTarget);
463 if (!sym)
464 continue;
466 uint64_t s = sym->getRVA();
468 if (isInRange(rel.Type, s, p, margin))
469 continue;
471 // If the target isn't in range, hook it up to an existing or new thunk.
472 auto [thunk, wasNew] = getThunk(lastThunks, sym, p, rel.Type, margin);
473 if (wasNew) {
474 Chunk *thunkChunk = thunk->getChunk();
475 thunkChunk->setRVA(
476 thunkInsertionRVA); // Estimate of where it will be located.
477 os->chunks.insert(os->chunks.begin() + thunkInsertionSpot, thunkChunk);
478 thunkInsertionSpot++;
479 thunksSize += thunkChunk->getSize();
480 thunkInsertionRVA += thunkChunk->getSize();
481 addressesChanged = true;
484 // To redirect the relocation, add a symbol to the parent object file's
485 // symbol table, and replace the relocation symbol table index with the
486 // new index.
487 auto insertion = thunkSymtabIndices.insert({{file, thunk}, ~0U});
488 uint32_t &thunkSymbolIndex = insertion.first->second;
489 if (insertion.second)
490 thunkSymbolIndex = file->addRangeThunkSymbol(thunk);
491 relocReplacements.emplace_back(j, thunkSymbolIndex);
494 // Get a writable copy of this section's relocations so they can be
495 // modified. If the relocations point into the object file, allocate new
496 // memory. Otherwise, this must be previously allocated memory that can be
497 // modified in place.
498 ArrayRef<coff_relocation> curRelocs = sc->getRelocs();
499 MutableArrayRef<coff_relocation> newRelocs;
500 if (originalRelocs.data() == curRelocs.data()) {
501 newRelocs = MutableArrayRef(
502 bAlloc().Allocate<coff_relocation>(originalRelocs.size()),
503 originalRelocs.size());
504 } else {
505 newRelocs = MutableArrayRef(
506 const_cast<coff_relocation *>(curRelocs.data()), curRelocs.size());
509 // Copy each relocation, but replace the symbol table indices which need
510 // thunks.
511 auto nextReplacement = relocReplacements.begin();
512 auto endReplacement = relocReplacements.end();
513 for (size_t i = 0, e = originalRelocs.size(); i != e; ++i) {
514 newRelocs[i] = originalRelocs[i];
515 if (nextReplacement != endReplacement && nextReplacement->first == i) {
516 newRelocs[i].SymbolTableIndex = nextReplacement->second;
517 ++nextReplacement;
521 sc->setRelocs(newRelocs);
523 return addressesChanged;
526 // Verify that all relocations are in range, with no extra margin requirements.
527 bool Writer::verifyRanges(const std::vector<Chunk *> chunks) {
528 for (Chunk *c : chunks) {
529 SectionChunk *sc = dyn_cast_or_null<SectionChunk>(c);
530 if (!sc)
531 continue;
533 ArrayRef<coff_relocation> relocs = sc->getRelocs();
534 for (const coff_relocation &rel : relocs) {
535 Symbol *relocTarget = sc->file->getSymbol(rel.SymbolTableIndex);
537 Defined *sym = dyn_cast_or_null<Defined>(relocTarget);
538 if (!sym)
539 continue;
541 uint64_t p = sc->getRVA() + rel.VirtualAddress;
542 uint64_t s = sym->getRVA();
544 if (!isInRange(rel.Type, s, p, 0))
545 return false;
548 return true;
551 // Assign addresses and add thunks if necessary.
552 void Writer::finalizeAddresses() {
553 assignAddresses();
554 if (ctx.config.machine != ARMNT && ctx.config.machine != ARM64)
555 return;
557 size_t origNumChunks = 0;
558 for (OutputSection *sec : ctx.outputSections) {
559 sec->origChunks = sec->chunks;
560 origNumChunks += sec->chunks.size();
563 int pass = 0;
564 int margin = 1024 * 100;
565 while (true) {
566 // First check whether we need thunks at all, or if the previous pass of
567 // adding them turned out ok.
568 bool rangesOk = true;
569 size_t numChunks = 0;
570 for (OutputSection *sec : ctx.outputSections) {
571 if (!verifyRanges(sec->chunks)) {
572 rangesOk = false;
573 break;
575 numChunks += sec->chunks.size();
577 if (rangesOk) {
578 if (pass > 0)
579 log("Added " + Twine(numChunks - origNumChunks) + " thunks with " +
580 "margin " + Twine(margin) + " in " + Twine(pass) + " passes");
581 return;
584 if (pass >= 10)
585 fatal("adding thunks hasn't converged after " + Twine(pass) + " passes");
587 if (pass > 0) {
588 // If the previous pass didn't work out, reset everything back to the
589 // original conditions before retrying with a wider margin. This should
590 // ideally never happen under real circumstances.
591 for (OutputSection *sec : ctx.outputSections)
592 sec->chunks = sec->origChunks;
593 margin *= 2;
596 // Try adding thunks everywhere where it is needed, with a margin
597 // to avoid things going out of range due to the added thunks.
598 bool addressesChanged = false;
599 for (OutputSection *sec : ctx.outputSections)
600 addressesChanged |= createThunks(sec, margin);
601 // If the verification above thought we needed thunks, we should have
602 // added some.
603 assert(addressesChanged);
604 (void)addressesChanged;
606 // Recalculate the layout for the whole image (and verify the ranges at
607 // the start of the next round).
608 assignAddresses();
610 pass++;
614 void Writer::writePEChecksum() {
615 if (!ctx.config.writeCheckSum) {
616 return;
619 // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#checksum
620 uint32_t *buf = (uint32_t *)buffer->getBufferStart();
621 uint32_t size = (uint32_t)(buffer->getBufferSize());
623 coff_file_header *coffHeader =
624 (coff_file_header *)((uint8_t *)buf + dosStubSize + sizeof(PEMagic));
625 pe32_header *peHeader =
626 (pe32_header *)((uint8_t *)coffHeader + sizeof(coff_file_header));
628 uint64_t sum = 0;
629 uint32_t count = size;
630 ulittle16_t *addr = (ulittle16_t *)buf;
632 // The PE checksum algorithm, implemented as suggested in RFC1071
633 while (count > 1) {
634 sum += *addr++;
635 count -= 2;
638 // Add left-over byte, if any
639 if (count > 0)
640 sum += *(unsigned char *)addr;
642 // Fold 32-bit sum to 16 bits
643 while (sum >> 16) {
644 sum = (sum & 0xffff) + (sum >> 16);
647 sum += size;
648 peHeader->CheckSum = sum;
651 // The main function of the writer.
652 void Writer::run() {
653 ScopedTimer t1(ctx.codeLayoutTimer);
655 createImportTables();
656 createSections();
657 appendImportThunks();
658 // Import thunks must be added before the Control Flow Guard tables are added.
659 createMiscChunks();
660 createExportTable();
661 mergeSections();
662 removeUnusedSections();
663 finalizeAddresses();
664 removeEmptySections();
665 assignOutputSectionIndices();
666 setSectionPermissions();
667 createSymbolAndStringTable();
669 if (fileSize > UINT32_MAX)
670 fatal("image size (" + Twine(fileSize) + ") " +
671 "exceeds maximum allowable size (" + Twine(UINT32_MAX) + ")");
673 openFile(ctx.config.outputFile);
674 if (ctx.config.is64()) {
675 writeHeader<pe32plus_header>();
676 } else {
677 writeHeader<pe32_header>();
679 writeSections();
680 checkLoadConfig();
681 sortExceptionTable();
683 // Fix up the alignment in the TLS Directory's characteristic field,
684 // if a specific alignment value is needed
685 if (tlsAlignment)
686 fixTlsAlignment();
688 t1.stop();
690 if (!ctx.config.pdbPath.empty() && ctx.config.debug) {
691 assert(buildId);
692 createPDB(ctx, sectionTable, buildId->buildId);
694 writeBuildId();
696 writeLLDMapFile(ctx);
697 writeMapFile(ctx);
699 writePEChecksum();
701 if (errorCount())
702 return;
704 ScopedTimer t2(ctx.outputCommitTimer);
705 if (auto e = buffer->commit())
706 fatal("failed to write output '" + buffer->getPath() +
707 "': " + toString(std::move(e)));
710 static StringRef getOutputSectionName(StringRef name) {
711 StringRef s = name.split('$').first;
713 // Treat a later period as a separator for MinGW, for sections like
714 // ".ctors.01234".
715 return s.substr(0, s.find('.', 1));
718 // For /order.
719 void Writer::sortBySectionOrder(std::vector<Chunk *> &chunks) {
720 auto getPriority = [&ctx = ctx](const Chunk *c) {
721 if (auto *sec = dyn_cast<SectionChunk>(c))
722 if (sec->sym)
723 return ctx.config.order.lookup(sec->sym->getName());
724 return 0;
727 llvm::stable_sort(chunks, [=](const Chunk *a, const Chunk *b) {
728 return getPriority(a) < getPriority(b);
732 // Change the characteristics of existing PartialSections that belong to the
733 // section Name to Chars.
734 void Writer::fixPartialSectionChars(StringRef name, uint32_t chars) {
735 for (auto it : partialSections) {
736 PartialSection *pSec = it.second;
737 StringRef curName = pSec->name;
738 if (!curName.consume_front(name) ||
739 (!curName.empty() && !curName.starts_with("$")))
740 continue;
741 if (pSec->characteristics == chars)
742 continue;
743 PartialSection *destSec = createPartialSection(pSec->name, chars);
744 destSec->chunks.insert(destSec->chunks.end(), pSec->chunks.begin(),
745 pSec->chunks.end());
746 pSec->chunks.clear();
750 // Sort concrete section chunks from GNU import libraries.
752 // GNU binutils doesn't use short import files, but instead produces import
753 // libraries that consist of object files, with section chunks for the .idata$*
754 // sections. These are linked just as regular static libraries. Each import
755 // library consists of one header object, one object file for every imported
756 // symbol, and one trailer object. In order for the .idata tables/lists to
757 // be formed correctly, the section chunks within each .idata$* section need
758 // to be grouped by library, and sorted alphabetically within each library
759 // (which makes sure the header comes first and the trailer last).
760 bool Writer::fixGnuImportChunks() {
761 uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
763 // Make sure all .idata$* section chunks are mapped as RDATA in order to
764 // be sorted into the same sections as our own synthesized .idata chunks.
765 fixPartialSectionChars(".idata", rdata);
767 bool hasIdata = false;
768 // Sort all .idata$* chunks, grouping chunks from the same library,
769 // with alphabetical ordering of the object files within a library.
770 for (auto it : partialSections) {
771 PartialSection *pSec = it.second;
772 if (!pSec->name.starts_with(".idata"))
773 continue;
775 if (!pSec->chunks.empty())
776 hasIdata = true;
777 llvm::stable_sort(pSec->chunks, [&](Chunk *s, Chunk *t) {
778 SectionChunk *sc1 = dyn_cast_or_null<SectionChunk>(s);
779 SectionChunk *sc2 = dyn_cast_or_null<SectionChunk>(t);
780 if (!sc1 || !sc2) {
781 // if SC1, order them ascending. If SC2 or both null,
782 // S is not less than T.
783 return sc1 != nullptr;
785 // Make a string with "libraryname/objectfile" for sorting, achieving
786 // both grouping by library and sorting of objects within a library,
787 // at once.
788 std::string key1 =
789 (sc1->file->parentName + "/" + sc1->file->getName()).str();
790 std::string key2 =
791 (sc2->file->parentName + "/" + sc2->file->getName()).str();
792 return key1 < key2;
795 return hasIdata;
798 // Add generated idata chunks, for imported symbols and DLLs, and a
799 // terminator in .idata$2.
800 void Writer::addSyntheticIdata() {
801 uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
802 idata.create(ctx);
804 // Add the .idata content in the right section groups, to allow
805 // chunks from other linked in object files to be grouped together.
806 // See Microsoft PE/COFF spec 5.4 for details.
807 auto add = [&](StringRef n, std::vector<Chunk *> &v) {
808 PartialSection *pSec = createPartialSection(n, rdata);
809 pSec->chunks.insert(pSec->chunks.end(), v.begin(), v.end());
812 // The loader assumes a specific order of data.
813 // Add each type in the correct order.
814 add(".idata$2", idata.dirs);
815 add(".idata$4", idata.lookups);
816 add(".idata$5", idata.addresses);
817 if (!idata.hints.empty())
818 add(".idata$6", idata.hints);
819 add(".idata$7", idata.dllNames);
822 // Locate the first Chunk and size of the import directory list and the
823 // IAT.
824 void Writer::locateImportTables() {
825 uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
827 if (PartialSection *importDirs = findPartialSection(".idata$2", rdata)) {
828 if (!importDirs->chunks.empty())
829 importTableStart = importDirs->chunks.front();
830 for (Chunk *c : importDirs->chunks)
831 importTableSize += c->getSize();
834 if (PartialSection *importAddresses = findPartialSection(".idata$5", rdata)) {
835 if (!importAddresses->chunks.empty())
836 iatStart = importAddresses->chunks.front();
837 for (Chunk *c : importAddresses->chunks)
838 iatSize += c->getSize();
842 // Return whether a SectionChunk's suffix (the dollar and any trailing
843 // suffix) should be removed and sorted into the main suffixless
844 // PartialSection.
845 static bool shouldStripSectionSuffix(SectionChunk *sc, StringRef name,
846 bool isMinGW) {
847 // On MinGW, comdat groups are formed by putting the comdat group name
848 // after the '$' in the section name. For .eh_frame$<symbol>, that must
849 // still be sorted before the .eh_frame trailer from crtend.o, thus just
850 // strip the section name trailer. For other sections, such as
851 // .tls$$<symbol> (where non-comdat .tls symbols are otherwise stored in
852 // ".tls$"), they must be strictly sorted after .tls. And for the
853 // hypothetical case of comdat .CRT$XCU, we definitely need to keep the
854 // suffix for sorting. Thus, to play it safe, only strip the suffix for
855 // the standard sections.
856 if (!isMinGW)
857 return false;
858 if (!sc || !sc->isCOMDAT())
859 return false;
860 return name.starts_with(".text$") || name.starts_with(".data$") ||
861 name.starts_with(".rdata$") || name.starts_with(".pdata$") ||
862 name.starts_with(".xdata$") || name.starts_with(".eh_frame$");
865 void Writer::sortSections() {
866 if (!ctx.config.callGraphProfile.empty()) {
867 DenseMap<const SectionChunk *, int> order =
868 computeCallGraphProfileOrder(ctx);
869 for (auto it : order) {
870 if (DefinedRegular *sym = it.first->sym)
871 ctx.config.order[sym->getName()] = it.second;
874 if (!ctx.config.order.empty())
875 for (auto it : partialSections)
876 sortBySectionOrder(it.second->chunks);
879 // Create output section objects and add them to OutputSections.
880 void Writer::createSections() {
881 // First, create the builtin sections.
882 const uint32_t data = IMAGE_SCN_CNT_INITIALIZED_DATA;
883 const uint32_t bss = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
884 const uint32_t code = IMAGE_SCN_CNT_CODE;
885 const uint32_t discardable = IMAGE_SCN_MEM_DISCARDABLE;
886 const uint32_t r = IMAGE_SCN_MEM_READ;
887 const uint32_t w = IMAGE_SCN_MEM_WRITE;
888 const uint32_t x = IMAGE_SCN_MEM_EXECUTE;
890 SmallDenseMap<std::pair<StringRef, uint32_t>, OutputSection *> sections;
891 auto createSection = [&](StringRef name, uint32_t outChars) {
892 OutputSection *&sec = sections[{name, outChars}];
893 if (!sec) {
894 sec = make<OutputSection>(name, outChars);
895 ctx.outputSections.push_back(sec);
897 return sec;
900 // Try to match the section order used by link.exe.
901 textSec = createSection(".text", code | r | x);
902 createSection(".bss", bss | r | w);
903 rdataSec = createSection(".rdata", data | r);
904 buildidSec = createSection(".buildid", data | r);
905 dataSec = createSection(".data", data | r | w);
906 pdataSec = createSection(".pdata", data | r);
907 idataSec = createSection(".idata", data | r);
908 edataSec = createSection(".edata", data | r);
909 didatSec = createSection(".didat", data | r);
910 rsrcSec = createSection(".rsrc", data | r);
911 relocSec = createSection(".reloc", data | discardable | r);
912 ctorsSec = createSection(".ctors", data | r | w);
913 dtorsSec = createSection(".dtors", data | r | w);
915 // Then bin chunks by name and output characteristics.
916 for (Chunk *c : ctx.symtab.getChunks()) {
917 auto *sc = dyn_cast<SectionChunk>(c);
918 if (sc && !sc->live) {
919 if (ctx.config.verbose)
920 sc->printDiscardedMessage();
921 continue;
923 StringRef name = c->getSectionName();
924 if (shouldStripSectionSuffix(sc, name, ctx.config.mingw))
925 name = name.split('$').first;
927 if (name.starts_with(".tls"))
928 tlsAlignment = std::max(tlsAlignment, c->getAlignment());
930 PartialSection *pSec = createPartialSection(name,
931 c->getOutputCharacteristics());
932 pSec->chunks.push_back(c);
935 fixPartialSectionChars(".rsrc", data | r);
936 fixPartialSectionChars(".edata", data | r);
937 // Even in non MinGW cases, we might need to link against GNU import
938 // libraries.
939 bool hasIdata = fixGnuImportChunks();
940 if (!idata.empty())
941 hasIdata = true;
943 if (hasIdata)
944 addSyntheticIdata();
946 sortSections();
948 if (hasIdata)
949 locateImportTables();
951 // Then create an OutputSection for each section.
952 // '$' and all following characters in input section names are
953 // discarded when determining output section. So, .text$foo
954 // contributes to .text, for example. See PE/COFF spec 3.2.
955 for (auto it : partialSections) {
956 PartialSection *pSec = it.second;
957 StringRef name = getOutputSectionName(pSec->name);
958 uint32_t outChars = pSec->characteristics;
960 if (name == ".CRT") {
961 // In link.exe, there is a special case for the I386 target where .CRT
962 // sections are treated as if they have output characteristics DATA | R if
963 // their characteristics are DATA | R | W. This implements the same
964 // special case for all architectures.
965 outChars = data | r;
967 log("Processing section " + pSec->name + " -> " + name);
969 sortCRTSectionChunks(pSec->chunks);
972 OutputSection *sec = createSection(name, outChars);
973 for (Chunk *c : pSec->chunks)
974 sec->addChunk(c);
976 sec->addContributingPartialSection(pSec);
979 // Finally, move some output sections to the end.
980 auto sectionOrder = [&](const OutputSection *s) {
981 // Move DISCARDABLE (or non-memory-mapped) sections to the end of file
982 // because the loader cannot handle holes. Stripping can remove other
983 // discardable ones than .reloc, which is first of them (created early).
984 if (s->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) {
985 // Move discardable sections named .debug_ to the end, after other
986 // discardable sections. Stripping only removes the sections named
987 // .debug_* - thus try to avoid leaving holes after stripping.
988 if (s->name.starts_with(".debug_"))
989 return 3;
990 return 2;
992 // .rsrc should come at the end of the non-discardable sections because its
993 // size may change by the Win32 UpdateResources() function, causing
994 // subsequent sections to move (see https://crbug.com/827082).
995 if (s == rsrcSec)
996 return 1;
997 return 0;
999 llvm::stable_sort(ctx.outputSections,
1000 [&](const OutputSection *s, const OutputSection *t) {
1001 return sectionOrder(s) < sectionOrder(t);
1005 void Writer::createMiscChunks() {
1006 Configuration *config = &ctx.config;
1008 for (MergeChunk *p : ctx.mergeChunkInstances) {
1009 if (p) {
1010 p->finalizeContents();
1011 rdataSec->addChunk(p);
1015 // Create thunks for locally-dllimported symbols.
1016 if (!ctx.symtab.localImportChunks.empty()) {
1017 for (Chunk *c : ctx.symtab.localImportChunks)
1018 rdataSec->addChunk(c);
1021 // Create Debug Information Chunks
1022 OutputSection *debugInfoSec = config->mingw ? buildidSec : rdataSec;
1023 if (config->debug || config->repro || config->cetCompat) {
1024 debugDirectory =
1025 make<DebugDirectoryChunk>(ctx, debugRecords, config->repro);
1026 debugDirectory->setAlignment(4);
1027 debugInfoSec->addChunk(debugDirectory);
1030 if (config->debug) {
1031 // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified. We
1032 // output a PDB no matter what, and this chunk provides the only means of
1033 // allowing a debugger to match a PDB and an executable. So we need it even
1034 // if we're ultimately not going to write CodeView data to the PDB.
1035 buildId = make<CVDebugRecordChunk>(ctx);
1036 debugRecords.emplace_back(COFF::IMAGE_DEBUG_TYPE_CODEVIEW, buildId);
1039 if (config->cetCompat) {
1040 debugRecords.emplace_back(COFF::IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS,
1041 make<ExtendedDllCharacteristicsChunk>(
1042 IMAGE_DLL_CHARACTERISTICS_EX_CET_COMPAT));
1045 // Align and add each chunk referenced by the debug data directory.
1046 for (std::pair<COFF::DebugType, Chunk *> r : debugRecords) {
1047 r.second->setAlignment(4);
1048 debugInfoSec->addChunk(r.second);
1051 // Create SEH table. x86-only.
1052 if (config->safeSEH)
1053 createSEHTable();
1055 // Create /guard:cf tables if requested.
1056 if (config->guardCF != GuardCFLevel::Off)
1057 createGuardCFTables();
1059 if (config->autoImport)
1060 createRuntimePseudoRelocs();
1062 if (config->mingw)
1063 insertCtorDtorSymbols();
1066 // Create .idata section for the DLL-imported symbol table.
1067 // The format of this section is inherently Windows-specific.
1068 // IdataContents class abstracted away the details for us,
1069 // so we just let it create chunks and add them to the section.
1070 void Writer::createImportTables() {
1071 // Initialize DLLOrder so that import entries are ordered in
1072 // the same order as in the command line. (That affects DLL
1073 // initialization order, and this ordering is MSVC-compatible.)
1074 for (ImportFile *file : ctx.importFileInstances) {
1075 if (!file->live)
1076 continue;
1078 std::string dll = StringRef(file->dllName).lower();
1079 if (ctx.config.dllOrder.count(dll) == 0)
1080 ctx.config.dllOrder[dll] = ctx.config.dllOrder.size();
1082 if (file->impSym && !isa<DefinedImportData>(file->impSym))
1083 fatal(toString(ctx, *file->impSym) + " was replaced");
1084 DefinedImportData *impSym = cast_or_null<DefinedImportData>(file->impSym);
1085 if (ctx.config.delayLoads.count(StringRef(file->dllName).lower())) {
1086 if (!file->thunkSym)
1087 fatal("cannot delay-load " + toString(file) +
1088 " due to import of data: " + toString(ctx, *impSym));
1089 delayIdata.add(impSym);
1090 } else {
1091 idata.add(impSym);
1096 void Writer::appendImportThunks() {
1097 if (ctx.importFileInstances.empty())
1098 return;
1100 for (ImportFile *file : ctx.importFileInstances) {
1101 if (!file->live)
1102 continue;
1104 if (!file->thunkSym)
1105 continue;
1107 if (!isa<DefinedImportThunk>(file->thunkSym))
1108 fatal(toString(ctx, *file->thunkSym) + " was replaced");
1109 DefinedImportThunk *thunk = cast<DefinedImportThunk>(file->thunkSym);
1110 if (file->thunkLive)
1111 textSec->addChunk(thunk->getChunk());
1114 if (!delayIdata.empty()) {
1115 Defined *helper = cast<Defined>(ctx.config.delayLoadHelper);
1116 delayIdata.create(helper);
1117 for (Chunk *c : delayIdata.getChunks())
1118 didatSec->addChunk(c);
1119 for (Chunk *c : delayIdata.getDataChunks())
1120 dataSec->addChunk(c);
1121 for (Chunk *c : delayIdata.getCodeChunks())
1122 textSec->addChunk(c);
1123 for (Chunk *c : delayIdata.getCodePData())
1124 pdataSec->addChunk(c);
1125 for (Chunk *c : delayIdata.getCodeUnwindInfo())
1126 rdataSec->addChunk(c);
1130 void Writer::createExportTable() {
1131 if (!edataSec->chunks.empty()) {
1132 // Allow using a custom built export table from input object files, instead
1133 // of having the linker synthesize the tables.
1134 if (ctx.config.hadExplicitExports)
1135 warn("literal .edata sections override exports");
1136 } else if (!ctx.config.exports.empty()) {
1137 for (Chunk *c : edata.chunks)
1138 edataSec->addChunk(c);
1140 if (!edataSec->chunks.empty()) {
1141 edataStart = edataSec->chunks.front();
1142 edataEnd = edataSec->chunks.back();
1144 // Warn on exported deleting destructor.
1145 for (auto e : ctx.config.exports)
1146 if (e.sym && e.sym->getName().starts_with("??_G"))
1147 warn("export of deleting dtor: " + toString(ctx, *e.sym));
1150 void Writer::removeUnusedSections() {
1151 // Remove sections that we can be sure won't get content, to avoid
1152 // allocating space for their section headers.
1153 auto isUnused = [this](OutputSection *s) {
1154 if (s == relocSec)
1155 return false; // This section is populated later.
1156 // MergeChunks have zero size at this point, as their size is finalized
1157 // later. Only remove sections that have no Chunks at all.
1158 return s->chunks.empty();
1160 llvm::erase_if(ctx.outputSections, isUnused);
1163 // The Windows loader doesn't seem to like empty sections,
1164 // so we remove them if any.
1165 void Writer::removeEmptySections() {
1166 auto isEmpty = [](OutputSection *s) { return s->getVirtualSize() == 0; };
1167 llvm::erase_if(ctx.outputSections, isEmpty);
1170 void Writer::assignOutputSectionIndices() {
1171 // Assign final output section indices, and assign each chunk to its output
1172 // section.
1173 uint32_t idx = 1;
1174 for (OutputSection *os : ctx.outputSections) {
1175 os->sectionIndex = idx;
1176 for (Chunk *c : os->chunks)
1177 c->setOutputSectionIdx(idx);
1178 ++idx;
1181 // Merge chunks are containers of chunks, so assign those an output section
1182 // too.
1183 for (MergeChunk *mc : ctx.mergeChunkInstances)
1184 if (mc)
1185 for (SectionChunk *sc : mc->sections)
1186 if (sc && sc->live)
1187 sc->setOutputSectionIdx(mc->getOutputSectionIdx());
1190 size_t Writer::addEntryToStringTable(StringRef str) {
1191 assert(str.size() > COFF::NameSize);
1192 size_t offsetOfEntry = strtab.size() + 4; // +4 for the size field
1193 strtab.insert(strtab.end(), str.begin(), str.end());
1194 strtab.push_back('\0');
1195 return offsetOfEntry;
1198 std::optional<coff_symbol16> Writer::createSymbol(Defined *def) {
1199 coff_symbol16 sym;
1200 switch (def->kind()) {
1201 case Symbol::DefinedAbsoluteKind: {
1202 auto *da = dyn_cast<DefinedAbsolute>(def);
1203 // Note: COFF symbol can only store 32-bit values, so 64-bit absolute
1204 // values will be truncated.
1205 sym.Value = da->getVA();
1206 sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
1207 break;
1209 default: {
1210 // Don't write symbols that won't be written to the output to the symbol
1211 // table.
1212 // We also try to write DefinedSynthetic as a normal symbol. Some of these
1213 // symbols do point to an actual chunk, like __safe_se_handler_table. Others
1214 // like __ImageBase are outside of sections and thus cannot be represented.
1215 Chunk *c = def->getChunk();
1216 if (!c)
1217 return std::nullopt;
1218 OutputSection *os = ctx.getOutputSection(c);
1219 if (!os)
1220 return std::nullopt;
1222 sym.Value = def->getRVA() - os->getRVA();
1223 sym.SectionNumber = os->sectionIndex;
1224 break;
1228 // Symbols that are runtime pseudo relocations don't point to the actual
1229 // symbol data itself (as they are imported), but points to the IAT entry
1230 // instead. Avoid emitting them to the symbol table, as they can confuse
1231 // debuggers.
1232 if (def->isRuntimePseudoReloc)
1233 return std::nullopt;
1235 StringRef name = def->getName();
1236 if (name.size() > COFF::NameSize) {
1237 sym.Name.Offset.Zeroes = 0;
1238 sym.Name.Offset.Offset = addEntryToStringTable(name);
1239 } else {
1240 memset(sym.Name.ShortName, 0, COFF::NameSize);
1241 memcpy(sym.Name.ShortName, name.data(), name.size());
1244 if (auto *d = dyn_cast<DefinedCOFF>(def)) {
1245 COFFSymbolRef ref = d->getCOFFSymbol();
1246 sym.Type = ref.getType();
1247 sym.StorageClass = ref.getStorageClass();
1248 } else if (def->kind() == Symbol::DefinedImportThunkKind) {
1249 sym.Type = (IMAGE_SYM_DTYPE_FUNCTION << SCT_COMPLEX_TYPE_SHIFT) |
1250 IMAGE_SYM_TYPE_NULL;
1251 sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
1252 } else {
1253 sym.Type = IMAGE_SYM_TYPE_NULL;
1254 sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
1256 sym.NumberOfAuxSymbols = 0;
1257 return sym;
1260 void Writer::createSymbolAndStringTable() {
1261 // PE/COFF images are limited to 8 byte section names. Longer names can be
1262 // supported by writing a non-standard string table, but this string table is
1263 // not mapped at runtime and the long names will therefore be inaccessible.
1264 // link.exe always truncates section names to 8 bytes, whereas binutils always
1265 // preserves long section names via the string table. LLD adopts a hybrid
1266 // solution where discardable sections have long names preserved and
1267 // non-discardable sections have their names truncated, to ensure that any
1268 // section which is mapped at runtime also has its name mapped at runtime.
1269 for (OutputSection *sec : ctx.outputSections) {
1270 if (sec->name.size() <= COFF::NameSize)
1271 continue;
1272 if ((sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0)
1273 continue;
1274 if (ctx.config.warnLongSectionNames) {
1275 warn("section name " + sec->name +
1276 " is longer than 8 characters and will use a non-standard string "
1277 "table");
1279 sec->setStringTableOff(addEntryToStringTable(sec->name));
1282 if (ctx.config.debugDwarf || ctx.config.debugSymtab) {
1283 for (ObjFile *file : ctx.objFileInstances) {
1284 for (Symbol *b : file->getSymbols()) {
1285 auto *d = dyn_cast_or_null<Defined>(b);
1286 if (!d || d->writtenToSymtab)
1287 continue;
1288 d->writtenToSymtab = true;
1289 if (auto *dc = dyn_cast_or_null<DefinedCOFF>(d)) {
1290 COFFSymbolRef symRef = dc->getCOFFSymbol();
1291 if (symRef.isSectionDefinition() ||
1292 symRef.getStorageClass() == COFF::IMAGE_SYM_CLASS_LABEL)
1293 continue;
1296 if (std::optional<coff_symbol16> sym = createSymbol(d))
1297 outputSymtab.push_back(*sym);
1299 if (auto *dthunk = dyn_cast<DefinedImportThunk>(d)) {
1300 if (!dthunk->wrappedSym->writtenToSymtab) {
1301 dthunk->wrappedSym->writtenToSymtab = true;
1302 if (std::optional<coff_symbol16> sym =
1303 createSymbol(dthunk->wrappedSym))
1304 outputSymtab.push_back(*sym);
1311 if (outputSymtab.empty() && strtab.empty())
1312 return;
1314 // We position the symbol table to be adjacent to the end of the last section.
1315 uint64_t fileOff = fileSize;
1316 pointerToSymbolTable = fileOff;
1317 fileOff += outputSymtab.size() * sizeof(coff_symbol16);
1318 fileOff += 4 + strtab.size();
1319 fileSize = alignTo(fileOff, ctx.config.fileAlign);
1322 void Writer::mergeSections() {
1323 if (!pdataSec->chunks.empty()) {
1324 firstPdata = pdataSec->chunks.front();
1325 lastPdata = pdataSec->chunks.back();
1328 for (auto &p : ctx.config.merge) {
1329 StringRef toName = p.second;
1330 if (p.first == toName)
1331 continue;
1332 StringSet<> names;
1333 while (true) {
1334 if (!names.insert(toName).second)
1335 fatal("/merge: cycle found for section '" + p.first + "'");
1336 auto i = ctx.config.merge.find(toName);
1337 if (i == ctx.config.merge.end())
1338 break;
1339 toName = i->second;
1341 OutputSection *from = findSection(p.first);
1342 OutputSection *to = findSection(toName);
1343 if (!from)
1344 continue;
1345 if (!to) {
1346 from->name = toName;
1347 continue;
1349 to->merge(from);
1353 // Visits all sections to assign incremental, non-overlapping RVAs and
1354 // file offsets.
1355 void Writer::assignAddresses() {
1356 Configuration *config = &ctx.config;
1358 sizeOfHeaders = dosStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
1359 sizeof(data_directory) * numberOfDataDirectory +
1360 sizeof(coff_section) * ctx.outputSections.size();
1361 sizeOfHeaders +=
1362 config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
1363 sizeOfHeaders = alignTo(sizeOfHeaders, config->fileAlign);
1364 fileSize = sizeOfHeaders;
1366 // The first page is kept unmapped.
1367 uint64_t rva = alignTo(sizeOfHeaders, config->align);
1369 for (OutputSection *sec : ctx.outputSections) {
1370 if (sec == relocSec)
1371 addBaserels();
1372 uint64_t rawSize = 0, virtualSize = 0;
1373 sec->header.VirtualAddress = rva;
1375 // If /FUNCTIONPADMIN is used, functions are padded in order to create a
1376 // hotpatchable image.
1377 const bool isCodeSection =
1378 (sec->header.Characteristics & IMAGE_SCN_CNT_CODE) &&
1379 (sec->header.Characteristics & IMAGE_SCN_MEM_READ) &&
1380 (sec->header.Characteristics & IMAGE_SCN_MEM_EXECUTE);
1381 uint32_t padding = isCodeSection ? config->functionPadMin : 0;
1383 for (Chunk *c : sec->chunks) {
1384 if (padding && c->isHotPatchable())
1385 virtualSize += padding;
1386 virtualSize = alignTo(virtualSize, c->getAlignment());
1387 c->setRVA(rva + virtualSize);
1388 virtualSize += c->getSize();
1389 if (c->hasData)
1390 rawSize = alignTo(virtualSize, config->fileAlign);
1392 if (virtualSize > UINT32_MAX)
1393 error("section larger than 4 GiB: " + sec->name);
1394 sec->header.VirtualSize = virtualSize;
1395 sec->header.SizeOfRawData = rawSize;
1396 if (rawSize != 0)
1397 sec->header.PointerToRawData = fileSize;
1398 rva += alignTo(virtualSize, config->align);
1399 fileSize += alignTo(rawSize, config->fileAlign);
1401 sizeOfImage = alignTo(rva, config->align);
1403 // Assign addresses to sections in MergeChunks.
1404 for (MergeChunk *mc : ctx.mergeChunkInstances)
1405 if (mc)
1406 mc->assignSubsectionRVAs();
1409 template <typename PEHeaderTy> void Writer::writeHeader() {
1410 // Write DOS header. For backwards compatibility, the first part of a PE/COFF
1411 // executable consists of an MS-DOS MZ executable. If the executable is run
1412 // under DOS, that program gets run (usually to just print an error message).
1413 // When run under Windows, the loader looks at AddressOfNewExeHeader and uses
1414 // the PE header instead.
1415 Configuration *config = &ctx.config;
1416 uint8_t *buf = buffer->getBufferStart();
1417 auto *dos = reinterpret_cast<dos_header *>(buf);
1418 buf += sizeof(dos_header);
1419 dos->Magic[0] = 'M';
1420 dos->Magic[1] = 'Z';
1421 dos->UsedBytesInTheLastPage = dosStubSize % 512;
1422 dos->FileSizeInPages = divideCeil(dosStubSize, 512);
1423 dos->HeaderSizeInParagraphs = sizeof(dos_header) / 16;
1425 dos->AddressOfRelocationTable = sizeof(dos_header);
1426 dos->AddressOfNewExeHeader = dosStubSize;
1428 // Write DOS program.
1429 memcpy(buf, dosProgram, sizeof(dosProgram));
1430 buf += sizeof(dosProgram);
1432 // Write PE magic
1433 memcpy(buf, PEMagic, sizeof(PEMagic));
1434 buf += sizeof(PEMagic);
1436 // Write COFF header
1437 auto *coff = reinterpret_cast<coff_file_header *>(buf);
1438 buf += sizeof(*coff);
1439 switch (config->machine) {
1440 case ARM64EC:
1441 coff->Machine = AMD64;
1442 break;
1443 case ARM64X:
1444 coff->Machine = ARM64;
1445 break;
1446 default:
1447 coff->Machine = config->machine;
1449 coff->NumberOfSections = ctx.outputSections.size();
1450 coff->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
1451 if (config->largeAddressAware)
1452 coff->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
1453 if (!config->is64())
1454 coff->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
1455 if (config->dll)
1456 coff->Characteristics |= IMAGE_FILE_DLL;
1457 if (config->driverUponly)
1458 coff->Characteristics |= IMAGE_FILE_UP_SYSTEM_ONLY;
1459 if (!config->relocatable)
1460 coff->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
1461 if (config->swaprunCD)
1462 coff->Characteristics |= IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP;
1463 if (config->swaprunNet)
1464 coff->Characteristics |= IMAGE_FILE_NET_RUN_FROM_SWAP;
1465 coff->SizeOfOptionalHeader =
1466 sizeof(PEHeaderTy) + sizeof(data_directory) * numberOfDataDirectory;
1468 // Write PE header
1469 auto *pe = reinterpret_cast<PEHeaderTy *>(buf);
1470 buf += sizeof(*pe);
1471 pe->Magic = config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
1473 // If {Major,Minor}LinkerVersion is left at 0.0, then for some
1474 // reason signing the resulting PE file with Authenticode produces a
1475 // signature that fails to validate on Windows 7 (but is OK on 10).
1476 // Set it to 14.0, which is what VS2015 outputs, and which avoids
1477 // that problem.
1478 pe->MajorLinkerVersion = 14;
1479 pe->MinorLinkerVersion = 0;
1481 pe->ImageBase = config->imageBase;
1482 pe->SectionAlignment = config->align;
1483 pe->FileAlignment = config->fileAlign;
1484 pe->MajorImageVersion = config->majorImageVersion;
1485 pe->MinorImageVersion = config->minorImageVersion;
1486 pe->MajorOperatingSystemVersion = config->majorOSVersion;
1487 pe->MinorOperatingSystemVersion = config->minorOSVersion;
1488 pe->MajorSubsystemVersion = config->majorSubsystemVersion;
1489 pe->MinorSubsystemVersion = config->minorSubsystemVersion;
1490 pe->Subsystem = config->subsystem;
1491 pe->SizeOfImage = sizeOfImage;
1492 pe->SizeOfHeaders = sizeOfHeaders;
1493 if (!config->noEntry) {
1494 Defined *entry = cast<Defined>(config->entry);
1495 pe->AddressOfEntryPoint = entry->getRVA();
1496 // Pointer to thumb code must have the LSB set, so adjust it.
1497 if (config->machine == ARMNT)
1498 pe->AddressOfEntryPoint |= 1;
1500 pe->SizeOfStackReserve = config->stackReserve;
1501 pe->SizeOfStackCommit = config->stackCommit;
1502 pe->SizeOfHeapReserve = config->heapReserve;
1503 pe->SizeOfHeapCommit = config->heapCommit;
1504 if (config->appContainer)
1505 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER;
1506 if (config->driverWdm)
1507 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER;
1508 if (config->dynamicBase)
1509 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
1510 if (config->highEntropyVA)
1511 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
1512 if (!config->allowBind)
1513 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND;
1514 if (config->nxCompat)
1515 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
1516 if (!config->allowIsolation)
1517 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION;
1518 if (config->guardCF != GuardCFLevel::Off)
1519 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF;
1520 if (config->integrityCheck)
1521 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
1522 if (setNoSEHCharacteristic || config->noSEH)
1523 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH;
1524 if (config->terminalServerAware)
1525 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
1526 pe->NumberOfRvaAndSize = numberOfDataDirectory;
1527 if (textSec->getVirtualSize()) {
1528 pe->BaseOfCode = textSec->getRVA();
1529 pe->SizeOfCode = textSec->getRawSize();
1531 pe->SizeOfInitializedData = getSizeOfInitializedData();
1533 // Write data directory
1534 auto *dir = reinterpret_cast<data_directory *>(buf);
1535 buf += sizeof(*dir) * numberOfDataDirectory;
1536 if (edataStart) {
1537 dir[EXPORT_TABLE].RelativeVirtualAddress = edataStart->getRVA();
1538 dir[EXPORT_TABLE].Size =
1539 edataEnd->getRVA() + edataEnd->getSize() - edataStart->getRVA();
1541 if (importTableStart) {
1542 dir[IMPORT_TABLE].RelativeVirtualAddress = importTableStart->getRVA();
1543 dir[IMPORT_TABLE].Size = importTableSize;
1545 if (iatStart) {
1546 dir[IAT].RelativeVirtualAddress = iatStart->getRVA();
1547 dir[IAT].Size = iatSize;
1549 if (rsrcSec->getVirtualSize()) {
1550 dir[RESOURCE_TABLE].RelativeVirtualAddress = rsrcSec->getRVA();
1551 dir[RESOURCE_TABLE].Size = rsrcSec->getVirtualSize();
1553 if (firstPdata) {
1554 dir[EXCEPTION_TABLE].RelativeVirtualAddress = firstPdata->getRVA();
1555 dir[EXCEPTION_TABLE].Size =
1556 lastPdata->getRVA() + lastPdata->getSize() - firstPdata->getRVA();
1558 if (relocSec->getVirtualSize()) {
1559 dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = relocSec->getRVA();
1560 dir[BASE_RELOCATION_TABLE].Size = relocSec->getVirtualSize();
1562 if (Symbol *sym = ctx.symtab.findUnderscore("_tls_used")) {
1563 if (Defined *b = dyn_cast<Defined>(sym)) {
1564 dir[TLS_TABLE].RelativeVirtualAddress = b->getRVA();
1565 dir[TLS_TABLE].Size = config->is64()
1566 ? sizeof(object::coff_tls_directory64)
1567 : sizeof(object::coff_tls_directory32);
1570 if (debugDirectory) {
1571 dir[DEBUG_DIRECTORY].RelativeVirtualAddress = debugDirectory->getRVA();
1572 dir[DEBUG_DIRECTORY].Size = debugDirectory->getSize();
1574 if (Symbol *sym = ctx.symtab.findUnderscore("_load_config_used")) {
1575 if (auto *b = dyn_cast<DefinedRegular>(sym)) {
1576 SectionChunk *sc = b->getChunk();
1577 assert(b->getRVA() >= sc->getRVA());
1578 uint64_t offsetInChunk = b->getRVA() - sc->getRVA();
1579 if (!sc->hasData || offsetInChunk + 4 > sc->getSize())
1580 fatal("_load_config_used is malformed");
1582 ArrayRef<uint8_t> secContents = sc->getContents();
1583 uint32_t loadConfigSize =
1584 *reinterpret_cast<const ulittle32_t *>(&secContents[offsetInChunk]);
1585 if (offsetInChunk + loadConfigSize > sc->getSize())
1586 fatal("_load_config_used is too large");
1587 dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = b->getRVA();
1588 dir[LOAD_CONFIG_TABLE].Size = loadConfigSize;
1591 if (!delayIdata.empty()) {
1592 dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
1593 delayIdata.getDirRVA();
1594 dir[DELAY_IMPORT_DESCRIPTOR].Size = delayIdata.getDirSize();
1597 // Write section table
1598 for (OutputSection *sec : ctx.outputSections) {
1599 sec->writeHeaderTo(buf, config->debug);
1600 buf += sizeof(coff_section);
1602 sectionTable = ArrayRef<uint8_t>(
1603 buf - ctx.outputSections.size() * sizeof(coff_section), buf);
1605 if (outputSymtab.empty() && strtab.empty())
1606 return;
1608 coff->PointerToSymbolTable = pointerToSymbolTable;
1609 uint32_t numberOfSymbols = outputSymtab.size();
1610 coff->NumberOfSymbols = numberOfSymbols;
1611 auto *symbolTable = reinterpret_cast<coff_symbol16 *>(
1612 buffer->getBufferStart() + coff->PointerToSymbolTable);
1613 for (size_t i = 0; i != numberOfSymbols; ++i)
1614 symbolTable[i] = outputSymtab[i];
1615 // Create the string table, it follows immediately after the symbol table.
1616 // The first 4 bytes is length including itself.
1617 buf = reinterpret_cast<uint8_t *>(&symbolTable[numberOfSymbols]);
1618 write32le(buf, strtab.size() + 4);
1619 if (!strtab.empty())
1620 memcpy(buf + 4, strtab.data(), strtab.size());
1623 void Writer::openFile(StringRef path) {
1624 buffer = CHECK(
1625 FileOutputBuffer::create(path, fileSize, FileOutputBuffer::F_executable),
1626 "failed to open " + path);
1629 void Writer::createSEHTable() {
1630 SymbolRVASet handlers;
1631 for (ObjFile *file : ctx.objFileInstances) {
1632 if (!file->hasSafeSEH())
1633 error("/safeseh: " + file->getName() + " is not compatible with SEH");
1634 markSymbolsForRVATable(file, file->getSXDataChunks(), handlers);
1637 // Set the "no SEH" characteristic if there really were no handlers, or if
1638 // there is no load config object to point to the table of handlers.
1639 setNoSEHCharacteristic =
1640 handlers.empty() || !ctx.symtab.findUnderscore("_load_config_used");
1642 maybeAddRVATable(std::move(handlers), "__safe_se_handler_table",
1643 "__safe_se_handler_count");
1646 // Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set
1647 // cannot contain duplicates. Therefore, the set is uniqued by Chunk and the
1648 // symbol's offset into that Chunk.
1649 static void addSymbolToRVASet(SymbolRVASet &rvaSet, Defined *s) {
1650 Chunk *c = s->getChunk();
1651 if (auto *sc = dyn_cast<SectionChunk>(c))
1652 c = sc->repl; // Look through ICF replacement.
1653 uint32_t off = s->getRVA() - (c ? c->getRVA() : 0);
1654 rvaSet.insert({c, off});
1657 // Given a symbol, add it to the GFIDs table if it is a live, defined, function
1658 // symbol in an executable section.
1659 static void maybeAddAddressTakenFunction(SymbolRVASet &addressTakenSyms,
1660 Symbol *s) {
1661 if (!s)
1662 return;
1664 switch (s->kind()) {
1665 case Symbol::DefinedLocalImportKind:
1666 case Symbol::DefinedImportDataKind:
1667 // Defines an __imp_ pointer, so it is data, so it is ignored.
1668 break;
1669 case Symbol::DefinedCommonKind:
1670 // Common is always data, so it is ignored.
1671 break;
1672 case Symbol::DefinedAbsoluteKind:
1673 case Symbol::DefinedSyntheticKind:
1674 // Absolute is never code, synthetic generally isn't and usually isn't
1675 // determinable.
1676 break;
1677 case Symbol::LazyArchiveKind:
1678 case Symbol::LazyObjectKind:
1679 case Symbol::LazyDLLSymbolKind:
1680 case Symbol::UndefinedKind:
1681 // Undefined symbols resolve to zero, so they don't have an RVA. Lazy
1682 // symbols shouldn't have relocations.
1683 break;
1685 case Symbol::DefinedImportThunkKind:
1686 // Thunks are always code, include them.
1687 addSymbolToRVASet(addressTakenSyms, cast<Defined>(s));
1688 break;
1690 case Symbol::DefinedRegularKind: {
1691 // This is a regular, defined, symbol from a COFF file. Mark the symbol as
1692 // address taken if the symbol type is function and it's in an executable
1693 // section.
1694 auto *d = cast<DefinedRegular>(s);
1695 if (d->getCOFFSymbol().getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
1696 SectionChunk *sc = dyn_cast<SectionChunk>(d->getChunk());
1697 if (sc && sc->live &&
1698 sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE)
1699 addSymbolToRVASet(addressTakenSyms, d);
1701 break;
1706 // Visit all relocations from all section contributions of this object file and
1707 // mark the relocation target as address-taken.
1708 void Writer::markSymbolsWithRelocations(ObjFile *file,
1709 SymbolRVASet &usedSymbols) {
1710 for (Chunk *c : file->getChunks()) {
1711 // We only care about live section chunks. Common chunks and other chunks
1712 // don't generally contain relocations.
1713 SectionChunk *sc = dyn_cast<SectionChunk>(c);
1714 if (!sc || !sc->live)
1715 continue;
1717 for (const coff_relocation &reloc : sc->getRelocs()) {
1718 if (ctx.config.machine == I386 &&
1719 reloc.Type == COFF::IMAGE_REL_I386_REL32)
1720 // Ignore relative relocations on x86. On x86_64 they can't be ignored
1721 // since they're also used to compute absolute addresses.
1722 continue;
1724 Symbol *ref = sc->file->getSymbol(reloc.SymbolTableIndex);
1725 maybeAddAddressTakenFunction(usedSymbols, ref);
1730 // Create the guard function id table. This is a table of RVAs of all
1731 // address-taken functions. It is sorted and uniqued, just like the safe SEH
1732 // table.
1733 void Writer::createGuardCFTables() {
1734 Configuration *config = &ctx.config;
1736 SymbolRVASet addressTakenSyms;
1737 SymbolRVASet giatsRVASet;
1738 std::vector<Symbol *> giatsSymbols;
1739 SymbolRVASet longJmpTargets;
1740 SymbolRVASet ehContTargets;
1741 for (ObjFile *file : ctx.objFileInstances) {
1742 // If the object was compiled with /guard:cf, the address taken symbols
1743 // are in .gfids$y sections, and the longjmp targets are in .gljmp$y
1744 // sections. If the object was not compiled with /guard:cf, we assume there
1745 // were no setjmp targets, and that all code symbols with relocations are
1746 // possibly address-taken.
1747 if (file->hasGuardCF()) {
1748 markSymbolsForRVATable(file, file->getGuardFidChunks(), addressTakenSyms);
1749 markSymbolsForRVATable(file, file->getGuardIATChunks(), giatsRVASet);
1750 getSymbolsFromSections(file, file->getGuardIATChunks(), giatsSymbols);
1751 markSymbolsForRVATable(file, file->getGuardLJmpChunks(), longJmpTargets);
1752 } else {
1753 markSymbolsWithRelocations(file, addressTakenSyms);
1755 // If the object was compiled with /guard:ehcont, the ehcont targets are in
1756 // .gehcont$y sections.
1757 if (file->hasGuardEHCont())
1758 markSymbolsForRVATable(file, file->getGuardEHContChunks(), ehContTargets);
1761 // Mark the image entry as address-taken.
1762 if (config->entry)
1763 maybeAddAddressTakenFunction(addressTakenSyms, config->entry);
1765 // Mark exported symbols in executable sections as address-taken.
1766 for (Export &e : config->exports)
1767 maybeAddAddressTakenFunction(addressTakenSyms, e.sym);
1769 // For each entry in the .giats table, check if it has a corresponding load
1770 // thunk (e.g. because the DLL that defines it will be delay-loaded) and, if
1771 // so, add the load thunk to the address taken (.gfids) table.
1772 for (Symbol *s : giatsSymbols) {
1773 if (auto *di = dyn_cast<DefinedImportData>(s)) {
1774 if (di->loadThunkSym)
1775 addSymbolToRVASet(addressTakenSyms, di->loadThunkSym);
1779 // Ensure sections referenced in the gfid table are 16-byte aligned.
1780 for (const ChunkAndOffset &c : addressTakenSyms)
1781 if (c.inputChunk->getAlignment() < 16)
1782 c.inputChunk->setAlignment(16);
1784 maybeAddRVATable(std::move(addressTakenSyms), "__guard_fids_table",
1785 "__guard_fids_count");
1787 // Add the Guard Address Taken IAT Entry Table (.giats).
1788 maybeAddRVATable(std::move(giatsRVASet), "__guard_iat_table",
1789 "__guard_iat_count");
1791 // Add the longjmp target table unless the user told us not to.
1792 if (config->guardCF & GuardCFLevel::LongJmp)
1793 maybeAddRVATable(std::move(longJmpTargets), "__guard_longjmp_table",
1794 "__guard_longjmp_count");
1796 // Add the ehcont target table unless the user told us not to.
1797 if (config->guardCF & GuardCFLevel::EHCont)
1798 maybeAddRVATable(std::move(ehContTargets), "__guard_eh_cont_table",
1799 "__guard_eh_cont_count");
1801 // Set __guard_flags, which will be used in the load config to indicate that
1802 // /guard:cf was enabled.
1803 uint32_t guardFlags = uint32_t(GuardFlags::CF_INSTRUMENTED) |
1804 uint32_t(GuardFlags::CF_FUNCTION_TABLE_PRESENT);
1805 if (config->guardCF & GuardCFLevel::LongJmp)
1806 guardFlags |= uint32_t(GuardFlags::CF_LONGJUMP_TABLE_PRESENT);
1807 if (config->guardCF & GuardCFLevel::EHCont)
1808 guardFlags |= uint32_t(GuardFlags::EH_CONTINUATION_TABLE_PRESENT);
1809 Symbol *flagSym = ctx.symtab.findUnderscore("__guard_flags");
1810 cast<DefinedAbsolute>(flagSym)->setVA(guardFlags);
1813 // Take a list of input sections containing symbol table indices and add those
1814 // symbols to a vector. The challenge is that symbol RVAs are not known and
1815 // depend on the table size, so we can't directly build a set of integers.
1816 void Writer::getSymbolsFromSections(ObjFile *file,
1817 ArrayRef<SectionChunk *> symIdxChunks,
1818 std::vector<Symbol *> &symbols) {
1819 for (SectionChunk *c : symIdxChunks) {
1820 // Skip sections discarded by linker GC. This comes up when a .gfids section
1821 // is associated with something like a vtable and the vtable is discarded.
1822 // In this case, the associated gfids section is discarded, and we don't
1823 // mark the virtual member functions as address-taken by the vtable.
1824 if (!c->live)
1825 continue;
1827 // Validate that the contents look like symbol table indices.
1828 ArrayRef<uint8_t> data = c->getContents();
1829 if (data.size() % 4 != 0) {
1830 warn("ignoring " + c->getSectionName() +
1831 " symbol table index section in object " + toString(file));
1832 continue;
1835 // Read each symbol table index and check if that symbol was included in the
1836 // final link. If so, add it to the vector of symbols.
1837 ArrayRef<ulittle32_t> symIndices(
1838 reinterpret_cast<const ulittle32_t *>(data.data()), data.size() / 4);
1839 ArrayRef<Symbol *> objSymbols = file->getSymbols();
1840 for (uint32_t symIndex : symIndices) {
1841 if (symIndex >= objSymbols.size()) {
1842 warn("ignoring invalid symbol table index in section " +
1843 c->getSectionName() + " in object " + toString(file));
1844 continue;
1846 if (Symbol *s = objSymbols[symIndex]) {
1847 if (s->isLive())
1848 symbols.push_back(cast<Symbol>(s));
1854 // Take a list of input sections containing symbol table indices and add those
1855 // symbols to an RVA table.
1856 void Writer::markSymbolsForRVATable(ObjFile *file,
1857 ArrayRef<SectionChunk *> symIdxChunks,
1858 SymbolRVASet &tableSymbols) {
1859 std::vector<Symbol *> syms;
1860 getSymbolsFromSections(file, symIdxChunks, syms);
1862 for (Symbol *s : syms)
1863 addSymbolToRVASet(tableSymbols, cast<Defined>(s));
1866 // Replace the absolute table symbol with a synthetic symbol pointing to
1867 // tableChunk so that we can emit base relocations for it and resolve section
1868 // relative relocations.
1869 void Writer::maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
1870 StringRef countSym, bool hasFlag) {
1871 if (tableSymbols.empty())
1872 return;
1874 NonSectionChunk *tableChunk;
1875 if (hasFlag)
1876 tableChunk = make<RVAFlagTableChunk>(std::move(tableSymbols));
1877 else
1878 tableChunk = make<RVATableChunk>(std::move(tableSymbols));
1879 rdataSec->addChunk(tableChunk);
1881 Symbol *t = ctx.symtab.findUnderscore(tableSym);
1882 Symbol *c = ctx.symtab.findUnderscore(countSym);
1883 replaceSymbol<DefinedSynthetic>(t, t->getName(), tableChunk);
1884 cast<DefinedAbsolute>(c)->setVA(tableChunk->getSize() / (hasFlag ? 5 : 4));
1887 // MinGW specific. Gather all relocations that are imported from a DLL even
1888 // though the code didn't expect it to, produce the table that the runtime
1889 // uses for fixing them up, and provide the synthetic symbols that the
1890 // runtime uses for finding the table.
1891 void Writer::createRuntimePseudoRelocs() {
1892 std::vector<RuntimePseudoReloc> rels;
1894 for (Chunk *c : ctx.symtab.getChunks()) {
1895 auto *sc = dyn_cast<SectionChunk>(c);
1896 if (!sc || !sc->live)
1897 continue;
1898 sc->getRuntimePseudoRelocs(rels);
1901 if (!ctx.config.pseudoRelocs) {
1902 // Not writing any pseudo relocs; if some were needed, error out and
1903 // indicate what required them.
1904 for (const RuntimePseudoReloc &rpr : rels)
1905 error("automatic dllimport of " + rpr.sym->getName() + " in " +
1906 toString(rpr.target->file) + " requires pseudo relocations");
1907 return;
1910 if (!rels.empty())
1911 log("Writing " + Twine(rels.size()) + " runtime pseudo relocations");
1912 PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(rels);
1913 rdataSec->addChunk(table);
1914 EmptyChunk *endOfList = make<EmptyChunk>();
1915 rdataSec->addChunk(endOfList);
1917 Symbol *headSym = ctx.symtab.findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__");
1918 Symbol *endSym =
1919 ctx.symtab.findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__");
1920 replaceSymbol<DefinedSynthetic>(headSym, headSym->getName(), table);
1921 replaceSymbol<DefinedSynthetic>(endSym, endSym->getName(), endOfList);
1924 // MinGW specific.
1925 // The MinGW .ctors and .dtors lists have sentinels at each end;
1926 // a (uintptr_t)-1 at the start and a (uintptr_t)0 at the end.
1927 // There's a symbol pointing to the start sentinel pointer, __CTOR_LIST__
1928 // and __DTOR_LIST__ respectively.
1929 void Writer::insertCtorDtorSymbols() {
1930 AbsolutePointerChunk *ctorListHead = make<AbsolutePointerChunk>(ctx, -1);
1931 AbsolutePointerChunk *ctorListEnd = make<AbsolutePointerChunk>(ctx, 0);
1932 AbsolutePointerChunk *dtorListHead = make<AbsolutePointerChunk>(ctx, -1);
1933 AbsolutePointerChunk *dtorListEnd = make<AbsolutePointerChunk>(ctx, 0);
1934 ctorsSec->insertChunkAtStart(ctorListHead);
1935 ctorsSec->addChunk(ctorListEnd);
1936 dtorsSec->insertChunkAtStart(dtorListHead);
1937 dtorsSec->addChunk(dtorListEnd);
1939 Symbol *ctorListSym = ctx.symtab.findUnderscore("__CTOR_LIST__");
1940 Symbol *dtorListSym = ctx.symtab.findUnderscore("__DTOR_LIST__");
1941 replaceSymbol<DefinedSynthetic>(ctorListSym, ctorListSym->getName(),
1942 ctorListHead);
1943 replaceSymbol<DefinedSynthetic>(dtorListSym, dtorListSym->getName(),
1944 dtorListHead);
1947 // Handles /section options to allow users to overwrite
1948 // section attributes.
1949 void Writer::setSectionPermissions() {
1950 for (auto &p : ctx.config.section) {
1951 StringRef name = p.first;
1952 uint32_t perm = p.second;
1953 for (OutputSection *sec : ctx.outputSections)
1954 if (sec->name == name)
1955 sec->setPermissions(perm);
1959 // Write section contents to a mmap'ed file.
1960 void Writer::writeSections() {
1961 uint8_t *buf = buffer->getBufferStart();
1962 for (OutputSection *sec : ctx.outputSections) {
1963 uint8_t *secBuf = buf + sec->getFileOff();
1964 // Fill gaps between functions in .text with INT3 instructions
1965 // instead of leaving as NUL bytes (which can be interpreted as
1966 // ADD instructions).
1967 if ((sec->header.Characteristics & IMAGE_SCN_CNT_CODE) &&
1968 (ctx.config.machine == AMD64 || ctx.config.machine == I386))
1969 memset(secBuf, 0xCC, sec->getRawSize());
1970 parallelForEach(sec->chunks, [&](Chunk *c) {
1971 c->writeTo(secBuf + c->getRVA() - sec->getRVA());
1976 void Writer::writeBuildId() {
1977 // There are two important parts to the build ID.
1978 // 1) If building with debug info, the COFF debug directory contains a
1979 // timestamp as well as a Guid and Age of the PDB.
1980 // 2) In all cases, the PE COFF file header also contains a timestamp.
1981 // For reproducibility, instead of a timestamp we want to use a hash of the
1982 // PE contents.
1983 Configuration *config = &ctx.config;
1985 if (config->debug) {
1986 assert(buildId && "BuildId is not set!");
1987 // BuildId->BuildId was filled in when the PDB was written.
1990 // At this point the only fields in the COFF file which remain unset are the
1991 // "timestamp" in the COFF file header, and the ones in the coff debug
1992 // directory. Now we can hash the file and write that hash to the various
1993 // timestamp fields in the file.
1994 StringRef outputFileData(
1995 reinterpret_cast<const char *>(buffer->getBufferStart()),
1996 buffer->getBufferSize());
1998 uint32_t timestamp = config->timestamp;
1999 uint64_t hash = 0;
2000 bool generateSyntheticBuildId =
2001 config->mingw && config->debug && config->pdbPath.empty();
2003 if (config->repro || generateSyntheticBuildId)
2004 hash = xxh3_64bits(outputFileData);
2006 if (config->repro)
2007 timestamp = static_cast<uint32_t>(hash);
2009 if (generateSyntheticBuildId) {
2010 // For MinGW builds without a PDB file, we still generate a build id
2011 // to allow associating a crash dump to the executable.
2012 buildId->buildId->PDB70.CVSignature = OMF::Signature::PDB70;
2013 buildId->buildId->PDB70.Age = 1;
2014 memcpy(buildId->buildId->PDB70.Signature, &hash, 8);
2015 // xxhash only gives us 8 bytes, so put some fixed data in the other half.
2016 memcpy(&buildId->buildId->PDB70.Signature[8], "LLD PDB.", 8);
2019 if (debugDirectory)
2020 debugDirectory->setTimeDateStamp(timestamp);
2022 uint8_t *buf = buffer->getBufferStart();
2023 buf += dosStubSize + sizeof(PEMagic);
2024 object::coff_file_header *coffHeader =
2025 reinterpret_cast<coff_file_header *>(buf);
2026 coffHeader->TimeDateStamp = timestamp;
2029 // Sort .pdata section contents according to PE/COFF spec 5.5.
2030 void Writer::sortExceptionTable() {
2031 if (!firstPdata)
2032 return;
2033 // We assume .pdata contains function table entries only.
2034 auto bufAddr = [&](Chunk *c) {
2035 OutputSection *os = ctx.getOutputSection(c);
2036 return buffer->getBufferStart() + os->getFileOff() + c->getRVA() -
2037 os->getRVA();
2039 uint8_t *begin = bufAddr(firstPdata);
2040 uint8_t *end = bufAddr(lastPdata) + lastPdata->getSize();
2041 if (ctx.config.machine == AMD64) {
2042 struct Entry { ulittle32_t begin, end, unwind; };
2043 if ((end - begin) % sizeof(Entry) != 0) {
2044 fatal("unexpected .pdata size: " + Twine(end - begin) +
2045 " is not a multiple of " + Twine(sizeof(Entry)));
2047 parallelSort(
2048 MutableArrayRef<Entry>((Entry *)begin, (Entry *)end),
2049 [](const Entry &a, const Entry &b) { return a.begin < b.begin; });
2050 return;
2052 if (ctx.config.machine == ARMNT || ctx.config.machine == ARM64) {
2053 struct Entry { ulittle32_t begin, unwind; };
2054 if ((end - begin) % sizeof(Entry) != 0) {
2055 fatal("unexpected .pdata size: " + Twine(end - begin) +
2056 " is not a multiple of " + Twine(sizeof(Entry)));
2058 parallelSort(
2059 MutableArrayRef<Entry>((Entry *)begin, (Entry *)end),
2060 [](const Entry &a, const Entry &b) { return a.begin < b.begin; });
2061 return;
2063 lld::errs() << "warning: don't know how to handle .pdata.\n";
2066 // The CRT section contains, among other things, the array of function
2067 // pointers that initialize every global variable that is not trivially
2068 // constructed. The CRT calls them one after the other prior to invoking
2069 // main().
2071 // As per C++ spec, 3.6.2/2.3,
2072 // "Variables with ordered initialization defined within a single
2073 // translation unit shall be initialized in the order of their definitions
2074 // in the translation unit"
2076 // It is therefore critical to sort the chunks containing the function
2077 // pointers in the order that they are listed in the object file (top to
2078 // bottom), otherwise global objects might not be initialized in the
2079 // correct order.
2080 void Writer::sortCRTSectionChunks(std::vector<Chunk *> &chunks) {
2081 auto sectionChunkOrder = [](const Chunk *a, const Chunk *b) {
2082 auto sa = dyn_cast<SectionChunk>(a);
2083 auto sb = dyn_cast<SectionChunk>(b);
2084 assert(sa && sb && "Non-section chunks in CRT section!");
2086 StringRef sAObj = sa->file->mb.getBufferIdentifier();
2087 StringRef sBObj = sb->file->mb.getBufferIdentifier();
2089 return sAObj == sBObj && sa->getSectionNumber() < sb->getSectionNumber();
2091 llvm::stable_sort(chunks, sectionChunkOrder);
2093 if (ctx.config.verbose) {
2094 for (auto &c : chunks) {
2095 auto sc = dyn_cast<SectionChunk>(c);
2096 log(" " + sc->file->mb.getBufferIdentifier().str() +
2097 ", SectionID: " + Twine(sc->getSectionNumber()));
2102 OutputSection *Writer::findSection(StringRef name) {
2103 for (OutputSection *sec : ctx.outputSections)
2104 if (sec->name == name)
2105 return sec;
2106 return nullptr;
2109 uint32_t Writer::getSizeOfInitializedData() {
2110 uint32_t res = 0;
2111 for (OutputSection *s : ctx.outputSections)
2112 if (s->header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
2113 res += s->getRawSize();
2114 return res;
2117 // Add base relocations to .reloc section.
2118 void Writer::addBaserels() {
2119 if (!ctx.config.relocatable)
2120 return;
2121 relocSec->chunks.clear();
2122 std::vector<Baserel> v;
2123 for (OutputSection *sec : ctx.outputSections) {
2124 if (sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
2125 continue;
2126 // Collect all locations for base relocations.
2127 for (Chunk *c : sec->chunks)
2128 c->getBaserels(&v);
2129 // Add the addresses to .reloc section.
2130 if (!v.empty())
2131 addBaserelBlocks(v);
2132 v.clear();
2136 // Add addresses to .reloc section. Note that addresses are grouped by page.
2137 void Writer::addBaserelBlocks(std::vector<Baserel> &v) {
2138 const uint32_t mask = ~uint32_t(pageSize - 1);
2139 uint32_t page = v[0].rva & mask;
2140 size_t i = 0, j = 1;
2141 for (size_t e = v.size(); j < e; ++j) {
2142 uint32_t p = v[j].rva & mask;
2143 if (p == page)
2144 continue;
2145 relocSec->addChunk(make<BaserelChunk>(page, &v[i], &v[0] + j));
2146 i = j;
2147 page = p;
2149 if (i == j)
2150 return;
2151 relocSec->addChunk(make<BaserelChunk>(page, &v[i], &v[0] + j));
2154 PartialSection *Writer::createPartialSection(StringRef name,
2155 uint32_t outChars) {
2156 PartialSection *&pSec = partialSections[{name, outChars}];
2157 if (pSec)
2158 return pSec;
2159 pSec = make<PartialSection>(name, outChars);
2160 return pSec;
2163 PartialSection *Writer::findPartialSection(StringRef name, uint32_t outChars) {
2164 auto it = partialSections.find({name, outChars});
2165 if (it != partialSections.end())
2166 return it->second;
2167 return nullptr;
2170 void Writer::fixTlsAlignment() {
2171 Defined *tlsSym =
2172 dyn_cast_or_null<Defined>(ctx.symtab.findUnderscore("_tls_used"));
2173 if (!tlsSym)
2174 return;
2176 OutputSection *sec = ctx.getOutputSection(tlsSym->getChunk());
2177 assert(sec && tlsSym->getRVA() >= sec->getRVA() &&
2178 "no output section for _tls_used");
2180 uint8_t *secBuf = buffer->getBufferStart() + sec->getFileOff();
2181 uint64_t tlsOffset = tlsSym->getRVA() - sec->getRVA();
2182 uint64_t directorySize = ctx.config.is64()
2183 ? sizeof(object::coff_tls_directory64)
2184 : sizeof(object::coff_tls_directory32);
2186 if (tlsOffset + directorySize > sec->getRawSize())
2187 fatal("_tls_used sym is malformed");
2189 if (ctx.config.is64()) {
2190 object::coff_tls_directory64 *tlsDir =
2191 reinterpret_cast<object::coff_tls_directory64 *>(&secBuf[tlsOffset]);
2192 tlsDir->setAlignment(tlsAlignment);
2193 } else {
2194 object::coff_tls_directory32 *tlsDir =
2195 reinterpret_cast<object::coff_tls_directory32 *>(&secBuf[tlsOffset]);
2196 tlsDir->setAlignment(tlsAlignment);
2200 void Writer::checkLoadConfig() {
2201 Symbol *sym = ctx.symtab.findUnderscore("_load_config_used");
2202 auto *b = cast_if_present<DefinedRegular>(sym);
2203 if (!b) {
2204 if (ctx.config.guardCF != GuardCFLevel::Off)
2205 warn("Control Flow Guard is enabled but '_load_config_used' is missing");
2206 return;
2209 OutputSection *sec = ctx.getOutputSection(b->getChunk());
2210 uint8_t *buf = buffer->getBufferStart();
2211 uint8_t *secBuf = buf + sec->getFileOff();
2212 uint8_t *symBuf = secBuf + (b->getRVA() - sec->getRVA());
2213 uint32_t expectedAlign = ctx.config.is64() ? 8 : 4;
2214 if (b->getChunk()->getAlignment() < expectedAlign)
2215 warn("'_load_config_used' is misaligned (expected alignment to be " +
2216 Twine(expectedAlign) + " bytes, got " +
2217 Twine(b->getChunk()->getAlignment()) + " instead)");
2218 else if (!isAligned(Align(expectedAlign), b->getRVA()))
2219 warn("'_load_config_used' is misaligned (RVA is 0x" +
2220 Twine::utohexstr(b->getRVA()) + " not aligned to " +
2221 Twine(expectedAlign) + " bytes)");
2223 if (ctx.config.is64())
2224 checkLoadConfigGuardData(
2225 reinterpret_cast<const coff_load_configuration64 *>(symBuf));
2226 else
2227 checkLoadConfigGuardData(
2228 reinterpret_cast<const coff_load_configuration32 *>(symBuf));
2231 template <typename T>
2232 void Writer::checkLoadConfigGuardData(const T *loadConfig) {
2233 size_t loadConfigSize = loadConfig->Size;
2235 #define RETURN_IF_NOT_CONTAINS(field) \
2236 if (loadConfigSize < offsetof(T, field) + sizeof(T::field)) { \
2237 warn("'_load_config_used' structure too small to include " #field); \
2238 return; \
2241 #define IF_CONTAINS(field) \
2242 if (loadConfigSize >= offsetof(T, field) + sizeof(T::field))
2244 #define CHECK_VA(field, sym) \
2245 if (auto *s = dyn_cast<DefinedSynthetic>(ctx.symtab.findUnderscore(sym))) \
2246 if (loadConfig->field != ctx.config.imageBase + s->getRVA()) \
2247 warn(#field " not set correctly in '_load_config_used'");
2249 #define CHECK_ABSOLUTE(field, sym) \
2250 if (auto *s = dyn_cast<DefinedAbsolute>(ctx.symtab.findUnderscore(sym))) \
2251 if (loadConfig->field != s->getVA()) \
2252 warn(#field " not set correctly in '_load_config_used'");
2254 if (ctx.config.guardCF == GuardCFLevel::Off)
2255 return;
2256 RETURN_IF_NOT_CONTAINS(GuardFlags)
2257 CHECK_VA(GuardCFFunctionTable, "__guard_fids_table")
2258 CHECK_ABSOLUTE(GuardCFFunctionCount, "__guard_fids_count")
2259 CHECK_ABSOLUTE(GuardFlags, "__guard_flags")
2260 IF_CONTAINS(GuardAddressTakenIatEntryCount) {
2261 CHECK_VA(GuardAddressTakenIatEntryTable, "__guard_iat_table")
2262 CHECK_ABSOLUTE(GuardAddressTakenIatEntryCount, "__guard_iat_count")
2265 if (!(ctx.config.guardCF & GuardCFLevel::LongJmp))
2266 return;
2267 RETURN_IF_NOT_CONTAINS(GuardLongJumpTargetCount)
2268 CHECK_VA(GuardLongJumpTargetTable, "__guard_longjmp_table")
2269 CHECK_ABSOLUTE(GuardLongJumpTargetCount, "__guard_longjmp_count")
2271 if (!(ctx.config.guardCF & GuardCFLevel::EHCont))
2272 return;
2273 RETURN_IF_NOT_CONTAINS(GuardEHContinuationCount)
2274 CHECK_VA(GuardEHContinuationTable, "__guard_eh_cont_table")
2275 CHECK_ABSOLUTE(GuardEHContinuationCount, "__guard_eh_cont_count")
2277 #undef RETURN_IF_NOT_CONTAINS
2278 #undef IF_CONTAINS
2279 #undef CHECK_VA
2280 #undef CHECK_ABSOLUTE