xref: /freebsd/contrib/llvm-project/lld/COFF/Writer.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===- Writer.cpp ---------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "Writer.h"
10349cc55cSDimitry Andric #include "COFFLinkerContext.h"
11e8d8bef9SDimitry Andric #include "CallGraphSort.h"
120b57cec5SDimitry Andric #include "Config.h"
130b57cec5SDimitry Andric #include "DLL.h"
140b57cec5SDimitry Andric #include "InputFiles.h"
155ffd83dbSDimitry Andric #include "LLDMapFile.h"
160b57cec5SDimitry Andric #include "MapFile.h"
170b57cec5SDimitry Andric #include "PDB.h"
180b57cec5SDimitry Andric #include "SymbolTable.h"
190b57cec5SDimitry Andric #include "Symbols.h"
200b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h"
210b57cec5SDimitry Andric #include "lld/Common/Memory.h"
220b57cec5SDimitry Andric #include "lld/Common/Timer.h"
230b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
240b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
25480093f4SDimitry Andric #include "llvm/ADT/StringSet.h"
26bdd1243dSDimitry Andric #include "llvm/BinaryFormat/COFF.h"
270b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
280b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
290b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
300b57cec5SDimitry Andric #include "llvm/Support/FileOutputBuffer.h"
310b57cec5SDimitry Andric #include "llvm/Support/Parallel.h"
320b57cec5SDimitry Andric #include "llvm/Support/Path.h"
330b57cec5SDimitry Andric #include "llvm/Support/RandomNumberGenerator.h"
34*5f757f3fSDimitry Andric #include "llvm/Support/TimeProfiler.h"
350b57cec5SDimitry Andric #include "llvm/Support/xxhash.h"
360b57cec5SDimitry Andric #include <algorithm>
370b57cec5SDimitry Andric #include <cstdio>
380b57cec5SDimitry Andric #include <map>
390b57cec5SDimitry Andric #include <memory>
400b57cec5SDimitry Andric #include <utility>
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric using namespace llvm;
430b57cec5SDimitry Andric using namespace llvm::COFF;
440b57cec5SDimitry Andric using namespace llvm::object;
450b57cec5SDimitry Andric using namespace llvm::support;
460b57cec5SDimitry Andric using namespace llvm::support::endian;
475ffd83dbSDimitry Andric using namespace lld;
485ffd83dbSDimitry Andric using namespace lld::coff;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric /* To re-generate DOSProgram:
510b57cec5SDimitry Andric $ cat > /tmp/DOSProgram.asm
520b57cec5SDimitry Andric org 0
530b57cec5SDimitry Andric         ; Copy cs to ds.
540b57cec5SDimitry Andric         push cs
550b57cec5SDimitry Andric         pop ds
560b57cec5SDimitry Andric         ; Point ds:dx at the $-terminated string.
570b57cec5SDimitry Andric         mov dx, str
580b57cec5SDimitry Andric         ; Int 21/AH=09h: Write string to standard output.
590b57cec5SDimitry Andric         mov ah, 0x9
600b57cec5SDimitry Andric         int 0x21
610b57cec5SDimitry Andric         ; Int 21/AH=4Ch: Exit with return code (in AL).
620b57cec5SDimitry Andric         mov ax, 0x4C01
630b57cec5SDimitry Andric         int 0x21
640b57cec5SDimitry Andric str:
650b57cec5SDimitry Andric         db 'This program cannot be run in DOS mode.$'
660b57cec5SDimitry Andric align 8, db 0
670b57cec5SDimitry Andric $ nasm -fbin /tmp/DOSProgram.asm -o /tmp/DOSProgram.bin
680b57cec5SDimitry Andric $ xxd -i /tmp/DOSProgram.bin
690b57cec5SDimitry Andric */
700b57cec5SDimitry Andric static unsigned char dosProgram[] = {
710b57cec5SDimitry Andric   0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8, 0x01, 0x4c,
720b57cec5SDimitry Andric   0xcd, 0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72,
730b57cec5SDimitry Andric   0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x62, 0x65,
740b57cec5SDimitry Andric   0x20, 0x72, 0x75, 0x6e, 0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20,
750b57cec5SDimitry Andric   0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x24, 0x00, 0x00
760b57cec5SDimitry Andric };
770b57cec5SDimitry Andric static_assert(sizeof(dosProgram) % 8 == 0,
780b57cec5SDimitry Andric               "DOSProgram size must be multiple of 8");
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric static const int dosStubSize = sizeof(dos_header) + sizeof(dosProgram);
810b57cec5SDimitry Andric static_assert(dosStubSize % 8 == 0, "DOSStub size must be multiple of 8");
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric static const int numberOfDataDirectory = 16;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric namespace {
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric class DebugDirectoryChunk : public NonSectionChunk {
880b57cec5SDimitry Andric public:
89bdd1243dSDimitry Andric   DebugDirectoryChunk(const COFFLinkerContext &c,
90349cc55cSDimitry Andric                       const std::vector<std::pair<COFF::DebugType, Chunk *>> &r,
915ffd83dbSDimitry Andric                       bool writeRepro)
92349cc55cSDimitry Andric       : records(r), writeRepro(writeRepro), ctx(c) {}
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   size_t getSize() const override {
950b57cec5SDimitry Andric     return (records.size() + int(writeRepro)) * sizeof(debug_directory);
960b57cec5SDimitry Andric   }
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   void writeTo(uint8_t *b) const override {
990b57cec5SDimitry Andric     auto *d = reinterpret_cast<debug_directory *>(b);
1000b57cec5SDimitry Andric 
1015ffd83dbSDimitry Andric     for (const std::pair<COFF::DebugType, Chunk *>& record : records) {
1025ffd83dbSDimitry Andric       Chunk *c = record.second;
103bdd1243dSDimitry Andric       const OutputSection *os = ctx.getOutputSection(c);
1045ffd83dbSDimitry Andric       uint64_t offs = os->getFileOff() + (c->getRVA() - os->getRVA());
1055ffd83dbSDimitry Andric       fillEntry(d, record.first, c->getSize(), c->getRVA(), offs);
1060b57cec5SDimitry Andric       ++d;
1070b57cec5SDimitry Andric     }
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric     if (writeRepro) {
1100b57cec5SDimitry Andric       // FIXME: The COFF spec allows either a 0-sized entry to just say
1110b57cec5SDimitry Andric       // "the timestamp field is really a hash", or a 4-byte size field
1120b57cec5SDimitry Andric       // followed by that many bytes containing a longer hash (with the
1130b57cec5SDimitry Andric       // lowest 4 bytes usually being the timestamp in little-endian order).
11406c3fb27SDimitry Andric       // Consider storing the full 8 bytes computed by xxh3_64bits here.
1150b57cec5SDimitry Andric       fillEntry(d, COFF::IMAGE_DEBUG_TYPE_REPRO, 0, 0, 0);
1160b57cec5SDimitry Andric     }
1170b57cec5SDimitry Andric   }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   void setTimeDateStamp(uint32_t timeDateStamp) {
1200b57cec5SDimitry Andric     for (support::ulittle32_t *tds : timeDateStamps)
1210b57cec5SDimitry Andric       *tds = timeDateStamp;
1220b57cec5SDimitry Andric   }
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric private:
1250b57cec5SDimitry Andric   void fillEntry(debug_directory *d, COFF::DebugType debugType, size_t size,
1260b57cec5SDimitry Andric                  uint64_t rva, uint64_t offs) const {
1270b57cec5SDimitry Andric     d->Characteristics = 0;
1280b57cec5SDimitry Andric     d->TimeDateStamp = 0;
1290b57cec5SDimitry Andric     d->MajorVersion = 0;
1300b57cec5SDimitry Andric     d->MinorVersion = 0;
1310b57cec5SDimitry Andric     d->Type = debugType;
1320b57cec5SDimitry Andric     d->SizeOfData = size;
1330b57cec5SDimitry Andric     d->AddressOfRawData = rva;
1340b57cec5SDimitry Andric     d->PointerToRawData = offs;
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric     timeDateStamps.push_back(&d->TimeDateStamp);
1370b57cec5SDimitry Andric   }
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   mutable std::vector<support::ulittle32_t *> timeDateStamps;
1405ffd83dbSDimitry Andric   const std::vector<std::pair<COFF::DebugType, Chunk *>> &records;
1410b57cec5SDimitry Andric   bool writeRepro;
142bdd1243dSDimitry Andric   const COFFLinkerContext &ctx;
1430b57cec5SDimitry Andric };
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric class CVDebugRecordChunk : public NonSectionChunk {
1460b57cec5SDimitry Andric public:
147bdd1243dSDimitry Andric   CVDebugRecordChunk(const COFFLinkerContext &c) : ctx(c) {}
148bdd1243dSDimitry Andric 
1490b57cec5SDimitry Andric   size_t getSize() const override {
150bdd1243dSDimitry Andric     return sizeof(codeview::DebugInfo) + ctx.config.pdbAltPath.size() + 1;
1510b57cec5SDimitry Andric   }
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   void writeTo(uint8_t *b) const override {
1540b57cec5SDimitry Andric     // Save off the DebugInfo entry to backfill the file signature (build id)
1550b57cec5SDimitry Andric     // in Writer::writeBuildId
1560b57cec5SDimitry Andric     buildId = reinterpret_cast<codeview::DebugInfo *>(b);
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric     // variable sized field (PDB Path)
1590b57cec5SDimitry Andric     char *p = reinterpret_cast<char *>(b + sizeof(*buildId));
160bdd1243dSDimitry Andric     if (!ctx.config.pdbAltPath.empty())
161bdd1243dSDimitry Andric       memcpy(p, ctx.config.pdbAltPath.data(), ctx.config.pdbAltPath.size());
162bdd1243dSDimitry Andric     p[ctx.config.pdbAltPath.size()] = '\0';
1630b57cec5SDimitry Andric   }
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric   mutable codeview::DebugInfo *buildId = nullptr;
166bdd1243dSDimitry Andric 
167bdd1243dSDimitry Andric private:
168bdd1243dSDimitry Andric   const COFFLinkerContext &ctx;
1690b57cec5SDimitry Andric };
1700b57cec5SDimitry Andric 
1715ffd83dbSDimitry Andric class ExtendedDllCharacteristicsChunk : public NonSectionChunk {
1725ffd83dbSDimitry Andric public:
1735ffd83dbSDimitry Andric   ExtendedDllCharacteristicsChunk(uint32_t c) : characteristics(c) {}
1745ffd83dbSDimitry Andric 
1755ffd83dbSDimitry Andric   size_t getSize() const override { return 4; }
1765ffd83dbSDimitry Andric 
1775ffd83dbSDimitry Andric   void writeTo(uint8_t *buf) const override { write32le(buf, characteristics); }
1785ffd83dbSDimitry Andric 
1795ffd83dbSDimitry Andric   uint32_t characteristics = 0;
1805ffd83dbSDimitry Andric };
1815ffd83dbSDimitry Andric 
1820b57cec5SDimitry Andric // PartialSection represents a group of chunks that contribute to an
1830b57cec5SDimitry Andric // OutputSection. Collating a collection of PartialSections of same name and
1840b57cec5SDimitry Andric // characteristics constitutes the OutputSection.
1850b57cec5SDimitry Andric class PartialSectionKey {
1860b57cec5SDimitry Andric public:
1870b57cec5SDimitry Andric   StringRef name;
1880b57cec5SDimitry Andric   unsigned characteristics;
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric   bool operator<(const PartialSectionKey &other) const {
1910b57cec5SDimitry Andric     int c = name.compare(other.name);
192bdd1243dSDimitry Andric     if (c > 0)
1930b57cec5SDimitry Andric       return false;
1940b57cec5SDimitry Andric     if (c == 0)
1950b57cec5SDimitry Andric       return characteristics < other.characteristics;
1960b57cec5SDimitry Andric     return true;
1970b57cec5SDimitry Andric   }
1980b57cec5SDimitry Andric };
1990b57cec5SDimitry Andric 
200*5f757f3fSDimitry Andric struct ChunkRange {
201*5f757f3fSDimitry Andric   Chunk *first = nullptr, *last;
202*5f757f3fSDimitry Andric };
203*5f757f3fSDimitry Andric 
2040b57cec5SDimitry Andric // The writer writes a SymbolTable result to a file.
2050b57cec5SDimitry Andric class Writer {
2060b57cec5SDimitry Andric public:
207bdd1243dSDimitry Andric   Writer(COFFLinkerContext &c)
208bdd1243dSDimitry Andric       : buffer(errorHandler().outputBuffer), delayIdata(c), edata(c), ctx(c) {}
2090b57cec5SDimitry Andric   void run();
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric private:
2120b57cec5SDimitry Andric   void createSections();
2130b57cec5SDimitry Andric   void createMiscChunks();
2140b57cec5SDimitry Andric   void createImportTables();
2150b57cec5SDimitry Andric   void appendImportThunks();
2160b57cec5SDimitry Andric   void locateImportTables();
2170b57cec5SDimitry Andric   void createExportTable();
2180b57cec5SDimitry Andric   void mergeSections();
219*5f757f3fSDimitry Andric   void sortECChunks();
2200b57cec5SDimitry Andric   void removeUnusedSections();
2210b57cec5SDimitry Andric   void assignAddresses();
222bdd1243dSDimitry Andric   bool isInRange(uint16_t relType, uint64_t s, uint64_t p, int margin);
223bdd1243dSDimitry Andric   std::pair<Defined *, bool> getThunk(DenseMap<uint64_t, Defined *> &lastThunks,
224bdd1243dSDimitry Andric                                       Defined *target, uint64_t p,
225bdd1243dSDimitry Andric                                       uint16_t type, int margin);
226bdd1243dSDimitry Andric   bool createThunks(OutputSection *os, int margin);
227bdd1243dSDimitry Andric   bool verifyRanges(const std::vector<Chunk *> chunks);
228*5f757f3fSDimitry Andric   void createECCodeMap();
2290b57cec5SDimitry Andric   void finalizeAddresses();
2300b57cec5SDimitry Andric   void removeEmptySections();
2310b57cec5SDimitry Andric   void assignOutputSectionIndices();
2320b57cec5SDimitry Andric   void createSymbolAndStringTable();
2330b57cec5SDimitry Andric   void openFile(StringRef outputPath);
2340b57cec5SDimitry Andric   template <typename PEHeaderTy> void writeHeader();
2350b57cec5SDimitry Andric   void createSEHTable();
2360b57cec5SDimitry Andric   void createRuntimePseudoRelocs();
237*5f757f3fSDimitry Andric   void createECChunks();
2380b57cec5SDimitry Andric   void insertCtorDtorSymbols();
239bdd1243dSDimitry Andric   void markSymbolsWithRelocations(ObjFile *file, SymbolRVASet &usedSymbols);
2400b57cec5SDimitry Andric   void createGuardCFTables();
2410b57cec5SDimitry Andric   void markSymbolsForRVATable(ObjFile *file,
2420b57cec5SDimitry Andric                               ArrayRef<SectionChunk *> symIdxChunks,
2430b57cec5SDimitry Andric                               SymbolRVASet &tableSymbols);
244e8d8bef9SDimitry Andric   void getSymbolsFromSections(ObjFile *file,
245e8d8bef9SDimitry Andric                               ArrayRef<SectionChunk *> symIdxChunks,
246e8d8bef9SDimitry Andric                               std::vector<Symbol *> &symbols);
2470b57cec5SDimitry Andric   void maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
248fe6060f1SDimitry Andric                         StringRef countSym, bool hasFlag=false);
2490b57cec5SDimitry Andric   void setSectionPermissions();
250*5f757f3fSDimitry Andric   void setECSymbols();
2510b57cec5SDimitry Andric   void writeSections();
2520b57cec5SDimitry Andric   void writeBuildId();
253bdd1243dSDimitry Andric   void writePEChecksum();
254e8d8bef9SDimitry Andric   void sortSections();
255*5f757f3fSDimitry Andric   template <typename T> void sortExceptionTable(ChunkRange &exceptionTable);
256*5f757f3fSDimitry Andric   void sortExceptionTables();
2570b57cec5SDimitry Andric   void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
2580b57cec5SDimitry Andric   void addSyntheticIdata();
259bdd1243dSDimitry Andric   void sortBySectionOrder(std::vector<Chunk *> &chunks);
2600b57cec5SDimitry Andric   void fixPartialSectionChars(StringRef name, uint32_t chars);
2610b57cec5SDimitry Andric   bool fixGnuImportChunks();
262e8d8bef9SDimitry Andric   void fixTlsAlignment();
2630b57cec5SDimitry Andric   PartialSection *createPartialSection(StringRef name, uint32_t outChars);
2640b57cec5SDimitry Andric   PartialSection *findPartialSection(StringRef name, uint32_t outChars);
2650b57cec5SDimitry Andric 
266bdd1243dSDimitry Andric   std::optional<coff_symbol16> createSymbol(Defined *d);
2670b57cec5SDimitry Andric   size_t addEntryToStringTable(StringRef str);
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric   OutputSection *findSection(StringRef name);
2700b57cec5SDimitry Andric   void addBaserels();
2710b57cec5SDimitry Andric   void addBaserelBlocks(std::vector<Baserel> &v);
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric   uint32_t getSizeOfInitializedData();
2740b57cec5SDimitry Andric 
275*5f757f3fSDimitry Andric   void prepareLoadConfig();
276*5f757f3fSDimitry Andric   template <typename T> void prepareLoadConfig(T *loadConfig);
277bdd1243dSDimitry Andric   template <typename T> void checkLoadConfigGuardData(const T *loadConfig);
278bdd1243dSDimitry Andric 
2790b57cec5SDimitry Andric   std::unique_ptr<FileOutputBuffer> &buffer;
2800b57cec5SDimitry Andric   std::map<PartialSectionKey, PartialSection *> partialSections;
2810b57cec5SDimitry Andric   std::vector<char> strtab;
2820b57cec5SDimitry Andric   std::vector<llvm::object::coff_symbol16> outputSymtab;
283*5f757f3fSDimitry Andric   std::vector<ECCodeMapEntry> codeMap;
2840b57cec5SDimitry Andric   IdataContents idata;
2850b57cec5SDimitry Andric   Chunk *importTableStart = nullptr;
2860b57cec5SDimitry Andric   uint64_t importTableSize = 0;
28785868e8aSDimitry Andric   Chunk *edataStart = nullptr;
28885868e8aSDimitry Andric   Chunk *edataEnd = nullptr;
2890b57cec5SDimitry Andric   Chunk *iatStart = nullptr;
2900b57cec5SDimitry Andric   uint64_t iatSize = 0;
2910b57cec5SDimitry Andric   DelayLoadContents delayIdata;
2920b57cec5SDimitry Andric   EdataContents edata;
2930b57cec5SDimitry Andric   bool setNoSEHCharacteristic = false;
294e8d8bef9SDimitry Andric   uint32_t tlsAlignment = 0;
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric   DebugDirectoryChunk *debugDirectory = nullptr;
2975ffd83dbSDimitry Andric   std::vector<std::pair<COFF::DebugType, Chunk *>> debugRecords;
2980b57cec5SDimitry Andric   CVDebugRecordChunk *buildId = nullptr;
2990b57cec5SDimitry Andric   ArrayRef<uint8_t> sectionTable;
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric   uint64_t fileSize;
3020b57cec5SDimitry Andric   uint32_t pointerToSymbolTable = 0;
3030b57cec5SDimitry Andric   uint64_t sizeOfImage;
3040b57cec5SDimitry Andric   uint64_t sizeOfHeaders;
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric   OutputSection *textSec;
3070b57cec5SDimitry Andric   OutputSection *rdataSec;
3080b57cec5SDimitry Andric   OutputSection *buildidSec;
3090b57cec5SDimitry Andric   OutputSection *dataSec;
3100b57cec5SDimitry Andric   OutputSection *pdataSec;
3110b57cec5SDimitry Andric   OutputSection *idataSec;
3120b57cec5SDimitry Andric   OutputSection *edataSec;
3130b57cec5SDimitry Andric   OutputSection *didatSec;
3140b57cec5SDimitry Andric   OutputSection *rsrcSec;
3150b57cec5SDimitry Andric   OutputSection *relocSec;
3160b57cec5SDimitry Andric   OutputSection *ctorsSec;
3170b57cec5SDimitry Andric   OutputSection *dtorsSec;
318*5f757f3fSDimitry Andric   // Either .rdata section or .buildid section.
319*5f757f3fSDimitry Andric   OutputSection *debugInfoSec;
3200b57cec5SDimitry Andric 
321*5f757f3fSDimitry Andric   // The range of .pdata sections in the output file.
3220b57cec5SDimitry Andric   //
3230b57cec5SDimitry Andric   // We need to keep track of the location of .pdata in whichever section it
3240b57cec5SDimitry Andric   // gets merged into so that we can sort its contents and emit a correct data
3250b57cec5SDimitry Andric   // directory entry for the exception table. This is also the case for some
3260b57cec5SDimitry Andric   // other sections (such as .edata) but because the contents of those sections
3270b57cec5SDimitry Andric   // are entirely linker-generated we can keep track of their locations using
3280b57cec5SDimitry Andric   // the chunks that the linker creates. All .pdata chunks come from input
3290b57cec5SDimitry Andric   // files, so we need to keep track of them separately.
330*5f757f3fSDimitry Andric   ChunkRange pdata;
331*5f757f3fSDimitry Andric 
332*5f757f3fSDimitry Andric   // x86_64 .pdata sections on ARM64EC/ARM64X targets.
333*5f757f3fSDimitry Andric   ChunkRange hybridPdata;
334349cc55cSDimitry Andric 
335349cc55cSDimitry Andric   COFFLinkerContext &ctx;
3360b57cec5SDimitry Andric };
3370b57cec5SDimitry Andric } // anonymous namespace
3380b57cec5SDimitry Andric 
339*5f757f3fSDimitry Andric void lld::coff::writeResult(COFFLinkerContext &ctx) {
340*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Write output(s)");
341*5f757f3fSDimitry Andric   Writer(ctx).run();
342*5f757f3fSDimitry Andric }
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric void OutputSection::addChunk(Chunk *c) {
3450b57cec5SDimitry Andric   chunks.push_back(c);
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric void OutputSection::insertChunkAtStart(Chunk *c) {
3490b57cec5SDimitry Andric   chunks.insert(chunks.begin(), c);
3500b57cec5SDimitry Andric }
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric void OutputSection::setPermissions(uint32_t c) {
3530b57cec5SDimitry Andric   header.Characteristics &= ~permMask;
3540b57cec5SDimitry Andric   header.Characteristics |= c;
3550b57cec5SDimitry Andric }
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric void OutputSection::merge(OutputSection *other) {
3580b57cec5SDimitry Andric   chunks.insert(chunks.end(), other->chunks.begin(), other->chunks.end());
3590b57cec5SDimitry Andric   other->chunks.clear();
3600b57cec5SDimitry Andric   contribSections.insert(contribSections.end(), other->contribSections.begin(),
3610b57cec5SDimitry Andric                          other->contribSections.end());
3620b57cec5SDimitry Andric   other->contribSections.clear();
363*5f757f3fSDimitry Andric 
364*5f757f3fSDimitry Andric   // MS link.exe compatibility: when merging a code section into a data section,
365*5f757f3fSDimitry Andric   // mark the target section as a code section.
366*5f757f3fSDimitry Andric   if (other->header.Characteristics & IMAGE_SCN_CNT_CODE) {
367*5f757f3fSDimitry Andric     header.Characteristics |= IMAGE_SCN_CNT_CODE;
368*5f757f3fSDimitry Andric     header.Characteristics &=
369*5f757f3fSDimitry Andric         ~(IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_CNT_UNINITIALIZED_DATA);
370*5f757f3fSDimitry Andric   }
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric // Write the section header to a given buffer.
374bdd1243dSDimitry Andric void OutputSection::writeHeaderTo(uint8_t *buf, bool isDebug) {
3750b57cec5SDimitry Andric   auto *hdr = reinterpret_cast<coff_section *>(buf);
3760b57cec5SDimitry Andric   *hdr = header;
3770b57cec5SDimitry Andric   if (stringTableOff) {
3780b57cec5SDimitry Andric     // If name is too long, write offset into the string table as a name.
37981ad6265SDimitry Andric     encodeSectionName(hdr->Name, stringTableOff);
3800b57cec5SDimitry Andric   } else {
381bdd1243dSDimitry Andric     assert(!isDebug || name.size() <= COFF::NameSize ||
3820b57cec5SDimitry Andric            (hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0);
3830b57cec5SDimitry Andric     strncpy(hdr->Name, name.data(),
3840b57cec5SDimitry Andric             std::min(name.size(), (size_t)COFF::NameSize));
3850b57cec5SDimitry Andric   }
3860b57cec5SDimitry Andric }
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric void OutputSection::addContributingPartialSection(PartialSection *sec) {
3890b57cec5SDimitry Andric   contribSections.push_back(sec);
3900b57cec5SDimitry Andric }
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric // Check whether the target address S is in range from a relocation
3930b57cec5SDimitry Andric // of type relType at address P.
394bdd1243dSDimitry Andric bool Writer::isInRange(uint16_t relType, uint64_t s, uint64_t p, int margin) {
395bdd1243dSDimitry Andric   if (ctx.config.machine == ARMNT) {
3960b57cec5SDimitry Andric     int64_t diff = AbsoluteDifference(s, p + 4) + margin;
3970b57cec5SDimitry Andric     switch (relType) {
3980b57cec5SDimitry Andric     case IMAGE_REL_ARM_BRANCH20T:
3990b57cec5SDimitry Andric       return isInt<21>(diff);
4000b57cec5SDimitry Andric     case IMAGE_REL_ARM_BRANCH24T:
4010b57cec5SDimitry Andric     case IMAGE_REL_ARM_BLX23T:
4020b57cec5SDimitry Andric       return isInt<25>(diff);
4030b57cec5SDimitry Andric     default:
4040b57cec5SDimitry Andric       return true;
4050b57cec5SDimitry Andric     }
406bdd1243dSDimitry Andric   } else if (ctx.config.machine == ARM64) {
4070b57cec5SDimitry Andric     int64_t diff = AbsoluteDifference(s, p) + margin;
4080b57cec5SDimitry Andric     switch (relType) {
4090b57cec5SDimitry Andric     case IMAGE_REL_ARM64_BRANCH26:
4100b57cec5SDimitry Andric       return isInt<28>(diff);
4110b57cec5SDimitry Andric     case IMAGE_REL_ARM64_BRANCH19:
4120b57cec5SDimitry Andric       return isInt<21>(diff);
4130b57cec5SDimitry Andric     case IMAGE_REL_ARM64_BRANCH14:
4140b57cec5SDimitry Andric       return isInt<16>(diff);
4150b57cec5SDimitry Andric     default:
4160b57cec5SDimitry Andric       return true;
4170b57cec5SDimitry Andric     }
4180b57cec5SDimitry Andric   } else {
4190b57cec5SDimitry Andric     llvm_unreachable("Unexpected architecture");
4200b57cec5SDimitry Andric   }
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric // Return the last thunk for the given target if it is in range,
4240b57cec5SDimitry Andric // or create a new one.
425bdd1243dSDimitry Andric std::pair<Defined *, bool>
426bdd1243dSDimitry Andric Writer::getThunk(DenseMap<uint64_t, Defined *> &lastThunks, Defined *target,
427bdd1243dSDimitry Andric                  uint64_t p, uint16_t type, int margin) {
4280b57cec5SDimitry Andric   Defined *&lastThunk = lastThunks[target->getRVA()];
4290b57cec5SDimitry Andric   if (lastThunk && isInRange(type, lastThunk->getRVA(), p, margin))
4300b57cec5SDimitry Andric     return {lastThunk, false};
4310b57cec5SDimitry Andric   Chunk *c;
432bdd1243dSDimitry Andric   switch (ctx.config.machine) {
4330b57cec5SDimitry Andric   case ARMNT:
434bdd1243dSDimitry Andric     c = make<RangeExtensionThunkARM>(ctx, target);
4350b57cec5SDimitry Andric     break;
4360b57cec5SDimitry Andric   case ARM64:
437bdd1243dSDimitry Andric     c = make<RangeExtensionThunkARM64>(ctx, target);
4380b57cec5SDimitry Andric     break;
4390b57cec5SDimitry Andric   default:
4400b57cec5SDimitry Andric     llvm_unreachable("Unexpected architecture");
4410b57cec5SDimitry Andric   }
4426246ae0bSDimitry Andric   Defined *d = make<DefinedSynthetic>("range_extension_thunk", c);
4430b57cec5SDimitry Andric   lastThunk = d;
4440b57cec5SDimitry Andric   return {d, true};
4450b57cec5SDimitry Andric }
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric // This checks all relocations, and for any relocation which isn't in range
4480b57cec5SDimitry Andric // it adds a thunk after the section chunk that contains the relocation.
4490b57cec5SDimitry Andric // If the latest thunk for the specific target is in range, that is used
4500b57cec5SDimitry Andric // instead of creating a new thunk. All range checks are done with the
4510b57cec5SDimitry Andric // specified margin, to make sure that relocations that originally are in
4520b57cec5SDimitry Andric // range, but only barely, also get thunks - in case other added thunks makes
4530b57cec5SDimitry Andric // the target go out of range.
4540b57cec5SDimitry Andric //
4550b57cec5SDimitry Andric // After adding thunks, we verify that all relocations are in range (with
4560b57cec5SDimitry Andric // no extra margin requirements). If this failed, we restart (throwing away
4570b57cec5SDimitry Andric // the previously created thunks) and retry with a wider margin.
458bdd1243dSDimitry Andric bool Writer::createThunks(OutputSection *os, int margin) {
4590b57cec5SDimitry Andric   bool addressesChanged = false;
4600b57cec5SDimitry Andric   DenseMap<uint64_t, Defined *> lastThunks;
4610b57cec5SDimitry Andric   DenseMap<std::pair<ObjFile *, Defined *>, uint32_t> thunkSymtabIndices;
4620b57cec5SDimitry Andric   size_t thunksSize = 0;
4630b57cec5SDimitry Andric   // Recheck Chunks.size() each iteration, since we can insert more
4640b57cec5SDimitry Andric   // elements into it.
4650b57cec5SDimitry Andric   for (size_t i = 0; i != os->chunks.size(); ++i) {
4660b57cec5SDimitry Andric     SectionChunk *sc = dyn_cast_or_null<SectionChunk>(os->chunks[i]);
4670b57cec5SDimitry Andric     if (!sc)
4680b57cec5SDimitry Andric       continue;
4690b57cec5SDimitry Andric     size_t thunkInsertionSpot = i + 1;
4700b57cec5SDimitry Andric 
4710b57cec5SDimitry Andric     // Try to get a good enough estimate of where new thunks will be placed.
4720b57cec5SDimitry Andric     // Offset this by the size of the new thunks added so far, to make the
4730b57cec5SDimitry Andric     // estimate slightly better.
4740b57cec5SDimitry Andric     size_t thunkInsertionRVA = sc->getRVA() + sc->getSize() + thunksSize;
4750b57cec5SDimitry Andric     ObjFile *file = sc->file;
4760b57cec5SDimitry Andric     std::vector<std::pair<uint32_t, uint32_t>> relocReplacements;
4770b57cec5SDimitry Andric     ArrayRef<coff_relocation> originalRelocs =
4780b57cec5SDimitry Andric         file->getCOFFObj()->getRelocations(sc->header);
4790b57cec5SDimitry Andric     for (size_t j = 0, e = originalRelocs.size(); j < e; ++j) {
4800b57cec5SDimitry Andric       const coff_relocation &rel = originalRelocs[j];
4810b57cec5SDimitry Andric       Symbol *relocTarget = file->getSymbol(rel.SymbolTableIndex);
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric       // The estimate of the source address P should be pretty accurate,
4840b57cec5SDimitry Andric       // but we don't know whether the target Symbol address should be
4850b57cec5SDimitry Andric       // offset by thunksSize or not (or by some of thunksSize but not all of
4860b57cec5SDimitry Andric       // it), giving us some uncertainty once we have added one thunk.
4870b57cec5SDimitry Andric       uint64_t p = sc->getRVA() + rel.VirtualAddress + thunksSize;
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric       Defined *sym = dyn_cast_or_null<Defined>(relocTarget);
4900b57cec5SDimitry Andric       if (!sym)
4910b57cec5SDimitry Andric         continue;
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric       uint64_t s = sym->getRVA();
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric       if (isInRange(rel.Type, s, p, margin))
4960b57cec5SDimitry Andric         continue;
4970b57cec5SDimitry Andric 
498bdd1243dSDimitry Andric       // If the target isn't in range, hook it up to an existing or new thunk.
499bdd1243dSDimitry Andric       auto [thunk, wasNew] = getThunk(lastThunks, sym, p, rel.Type, margin);
5000b57cec5SDimitry Andric       if (wasNew) {
5010b57cec5SDimitry Andric         Chunk *thunkChunk = thunk->getChunk();
5020b57cec5SDimitry Andric         thunkChunk->setRVA(
5030b57cec5SDimitry Andric             thunkInsertionRVA); // Estimate of where it will be located.
5040b57cec5SDimitry Andric         os->chunks.insert(os->chunks.begin() + thunkInsertionSpot, thunkChunk);
5050b57cec5SDimitry Andric         thunkInsertionSpot++;
5060b57cec5SDimitry Andric         thunksSize += thunkChunk->getSize();
5070b57cec5SDimitry Andric         thunkInsertionRVA += thunkChunk->getSize();
5080b57cec5SDimitry Andric         addressesChanged = true;
5090b57cec5SDimitry Andric       }
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric       // To redirect the relocation, add a symbol to the parent object file's
5120b57cec5SDimitry Andric       // symbol table, and replace the relocation symbol table index with the
5130b57cec5SDimitry Andric       // new index.
5140b57cec5SDimitry Andric       auto insertion = thunkSymtabIndices.insert({{file, thunk}, ~0U});
5150b57cec5SDimitry Andric       uint32_t &thunkSymbolIndex = insertion.first->second;
5160b57cec5SDimitry Andric       if (insertion.second)
5170b57cec5SDimitry Andric         thunkSymbolIndex = file->addRangeThunkSymbol(thunk);
51806c3fb27SDimitry Andric       relocReplacements.emplace_back(j, thunkSymbolIndex);
5190b57cec5SDimitry Andric     }
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric     // Get a writable copy of this section's relocations so they can be
5220b57cec5SDimitry Andric     // modified. If the relocations point into the object file, allocate new
5230b57cec5SDimitry Andric     // memory. Otherwise, this must be previously allocated memory that can be
5240b57cec5SDimitry Andric     // modified in place.
5250b57cec5SDimitry Andric     ArrayRef<coff_relocation> curRelocs = sc->getRelocs();
5260b57cec5SDimitry Andric     MutableArrayRef<coff_relocation> newRelocs;
5270b57cec5SDimitry Andric     if (originalRelocs.data() == curRelocs.data()) {
528bdd1243dSDimitry Andric       newRelocs = MutableArrayRef(
52904eeddc0SDimitry Andric           bAlloc().Allocate<coff_relocation>(originalRelocs.size()),
5300b57cec5SDimitry Andric           originalRelocs.size());
5310b57cec5SDimitry Andric     } else {
532bdd1243dSDimitry Andric       newRelocs = MutableArrayRef(
5330b57cec5SDimitry Andric           const_cast<coff_relocation *>(curRelocs.data()), curRelocs.size());
5340b57cec5SDimitry Andric     }
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric     // Copy each relocation, but replace the symbol table indices which need
5370b57cec5SDimitry Andric     // thunks.
5380b57cec5SDimitry Andric     auto nextReplacement = relocReplacements.begin();
5390b57cec5SDimitry Andric     auto endReplacement = relocReplacements.end();
5400b57cec5SDimitry Andric     for (size_t i = 0, e = originalRelocs.size(); i != e; ++i) {
5410b57cec5SDimitry Andric       newRelocs[i] = originalRelocs[i];
5420b57cec5SDimitry Andric       if (nextReplacement != endReplacement && nextReplacement->first == i) {
5430b57cec5SDimitry Andric         newRelocs[i].SymbolTableIndex = nextReplacement->second;
5440b57cec5SDimitry Andric         ++nextReplacement;
5450b57cec5SDimitry Andric       }
5460b57cec5SDimitry Andric     }
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric     sc->setRelocs(newRelocs);
5490b57cec5SDimitry Andric   }
5500b57cec5SDimitry Andric   return addressesChanged;
5510b57cec5SDimitry Andric }
5520b57cec5SDimitry Andric 
553*5f757f3fSDimitry Andric // Create a code map for CHPE metadata.
554*5f757f3fSDimitry Andric void Writer::createECCodeMap() {
555*5f757f3fSDimitry Andric   if (!isArm64EC(ctx.config.machine))
556*5f757f3fSDimitry Andric     return;
557*5f757f3fSDimitry Andric 
558*5f757f3fSDimitry Andric   // Clear the map in case we were're recomputing the map after adding
559*5f757f3fSDimitry Andric   // a range extension thunk.
560*5f757f3fSDimitry Andric   codeMap.clear();
561*5f757f3fSDimitry Andric 
562*5f757f3fSDimitry Andric   std::optional<chpe_range_type> lastType;
563*5f757f3fSDimitry Andric   Chunk *first = nullptr, *last = nullptr;
564*5f757f3fSDimitry Andric 
565*5f757f3fSDimitry Andric   auto closeRange = [&]() {
566*5f757f3fSDimitry Andric     if (lastType) {
567*5f757f3fSDimitry Andric       codeMap.push_back({first, last, *lastType});
568*5f757f3fSDimitry Andric       lastType.reset();
569*5f757f3fSDimitry Andric     }
570*5f757f3fSDimitry Andric   };
571*5f757f3fSDimitry Andric 
572*5f757f3fSDimitry Andric   for (OutputSection *sec : ctx.outputSections) {
573*5f757f3fSDimitry Andric     for (Chunk *c : sec->chunks) {
574*5f757f3fSDimitry Andric       // Skip empty section chunks. MS link.exe does not seem to do that and
575*5f757f3fSDimitry Andric       // generates empty code ranges in some cases.
576*5f757f3fSDimitry Andric       if (isa<SectionChunk>(c) && !c->getSize())
577*5f757f3fSDimitry Andric         continue;
578*5f757f3fSDimitry Andric 
579*5f757f3fSDimitry Andric       std::optional<chpe_range_type> chunkType = c->getArm64ECRangeType();
580*5f757f3fSDimitry Andric       if (chunkType != lastType) {
581*5f757f3fSDimitry Andric         closeRange();
582*5f757f3fSDimitry Andric         first = c;
583*5f757f3fSDimitry Andric         lastType = chunkType;
584*5f757f3fSDimitry Andric       }
585*5f757f3fSDimitry Andric       last = c;
586*5f757f3fSDimitry Andric     }
587*5f757f3fSDimitry Andric   }
588*5f757f3fSDimitry Andric 
589*5f757f3fSDimitry Andric   closeRange();
590*5f757f3fSDimitry Andric 
591*5f757f3fSDimitry Andric   Symbol *tableCountSym = ctx.symtab.findUnderscore("__hybrid_code_map_count");
592*5f757f3fSDimitry Andric   cast<DefinedAbsolute>(tableCountSym)->setVA(codeMap.size());
593*5f757f3fSDimitry Andric }
594*5f757f3fSDimitry Andric 
5950b57cec5SDimitry Andric // Verify that all relocations are in range, with no extra margin requirements.
596bdd1243dSDimitry Andric bool Writer::verifyRanges(const std::vector<Chunk *> chunks) {
5970b57cec5SDimitry Andric   for (Chunk *c : chunks) {
5980b57cec5SDimitry Andric     SectionChunk *sc = dyn_cast_or_null<SectionChunk>(c);
5990b57cec5SDimitry Andric     if (!sc)
6000b57cec5SDimitry Andric       continue;
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric     ArrayRef<coff_relocation> relocs = sc->getRelocs();
60306c3fb27SDimitry Andric     for (const coff_relocation &rel : relocs) {
6040b57cec5SDimitry Andric       Symbol *relocTarget = sc->file->getSymbol(rel.SymbolTableIndex);
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric       Defined *sym = dyn_cast_or_null<Defined>(relocTarget);
6070b57cec5SDimitry Andric       if (!sym)
6080b57cec5SDimitry Andric         continue;
6090b57cec5SDimitry Andric 
6100b57cec5SDimitry Andric       uint64_t p = sc->getRVA() + rel.VirtualAddress;
6110b57cec5SDimitry Andric       uint64_t s = sym->getRVA();
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric       if (!isInRange(rel.Type, s, p, 0))
6140b57cec5SDimitry Andric         return false;
6150b57cec5SDimitry Andric     }
6160b57cec5SDimitry Andric   }
6170b57cec5SDimitry Andric   return true;
6180b57cec5SDimitry Andric }
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric // Assign addresses and add thunks if necessary.
6210b57cec5SDimitry Andric void Writer::finalizeAddresses() {
6220b57cec5SDimitry Andric   assignAddresses();
623bdd1243dSDimitry Andric   if (ctx.config.machine != ARMNT && ctx.config.machine != ARM64)
6240b57cec5SDimitry Andric     return;
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric   size_t origNumChunks = 0;
627349cc55cSDimitry Andric   for (OutputSection *sec : ctx.outputSections) {
6280b57cec5SDimitry Andric     sec->origChunks = sec->chunks;
6290b57cec5SDimitry Andric     origNumChunks += sec->chunks.size();
6300b57cec5SDimitry Andric   }
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric   int pass = 0;
6330b57cec5SDimitry Andric   int margin = 1024 * 100;
6340b57cec5SDimitry Andric   while (true) {
635*5f757f3fSDimitry Andric     llvm::TimeTraceScope timeScope2("Add thunks pass");
636*5f757f3fSDimitry Andric 
6370b57cec5SDimitry Andric     // First check whether we need thunks at all, or if the previous pass of
6380b57cec5SDimitry Andric     // adding them turned out ok.
6390b57cec5SDimitry Andric     bool rangesOk = true;
6400b57cec5SDimitry Andric     size_t numChunks = 0;
641*5f757f3fSDimitry Andric     {
642*5f757f3fSDimitry Andric       llvm::TimeTraceScope timeScope3("Verify ranges");
643349cc55cSDimitry Andric       for (OutputSection *sec : ctx.outputSections) {
6440b57cec5SDimitry Andric         if (!verifyRanges(sec->chunks)) {
6450b57cec5SDimitry Andric           rangesOk = false;
6460b57cec5SDimitry Andric           break;
6470b57cec5SDimitry Andric         }
6480b57cec5SDimitry Andric         numChunks += sec->chunks.size();
6490b57cec5SDimitry Andric       }
650*5f757f3fSDimitry Andric     }
6510b57cec5SDimitry Andric     if (rangesOk) {
6520b57cec5SDimitry Andric       if (pass > 0)
6530b57cec5SDimitry Andric         log("Added " + Twine(numChunks - origNumChunks) + " thunks with " +
6540b57cec5SDimitry Andric             "margin " + Twine(margin) + " in " + Twine(pass) + " passes");
6550b57cec5SDimitry Andric       return;
6560b57cec5SDimitry Andric     }
6570b57cec5SDimitry Andric 
6580b57cec5SDimitry Andric     if (pass >= 10)
6590b57cec5SDimitry Andric       fatal("adding thunks hasn't converged after " + Twine(pass) + " passes");
6600b57cec5SDimitry Andric 
6610b57cec5SDimitry Andric     if (pass > 0) {
6620b57cec5SDimitry Andric       // If the previous pass didn't work out, reset everything back to the
6630b57cec5SDimitry Andric       // original conditions before retrying with a wider margin. This should
6640b57cec5SDimitry Andric       // ideally never happen under real circumstances.
665349cc55cSDimitry Andric       for (OutputSection *sec : ctx.outputSections)
6660b57cec5SDimitry Andric         sec->chunks = sec->origChunks;
6670b57cec5SDimitry Andric       margin *= 2;
6680b57cec5SDimitry Andric     }
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric     // Try adding thunks everywhere where it is needed, with a margin
6710b57cec5SDimitry Andric     // to avoid things going out of range due to the added thunks.
6720b57cec5SDimitry Andric     bool addressesChanged = false;
673*5f757f3fSDimitry Andric     {
674*5f757f3fSDimitry Andric       llvm::TimeTraceScope timeScope3("Create thunks");
675349cc55cSDimitry Andric       for (OutputSection *sec : ctx.outputSections)
6760b57cec5SDimitry Andric         addressesChanged |= createThunks(sec, margin);
677*5f757f3fSDimitry Andric     }
6780b57cec5SDimitry Andric     // If the verification above thought we needed thunks, we should have
6790b57cec5SDimitry Andric     // added some.
6800b57cec5SDimitry Andric     assert(addressesChanged);
681fe6060f1SDimitry Andric     (void)addressesChanged;
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric     // Recalculate the layout for the whole image (and verify the ranges at
6840b57cec5SDimitry Andric     // the start of the next round).
6850b57cec5SDimitry Andric     assignAddresses();
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric     pass++;
6880b57cec5SDimitry Andric   }
6890b57cec5SDimitry Andric }
6900b57cec5SDimitry Andric 
691bdd1243dSDimitry Andric void Writer::writePEChecksum() {
692bdd1243dSDimitry Andric   if (!ctx.config.writeCheckSum) {
693bdd1243dSDimitry Andric     return;
694bdd1243dSDimitry Andric   }
695bdd1243dSDimitry Andric 
696*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("PE checksum");
697*5f757f3fSDimitry Andric 
698bdd1243dSDimitry Andric   // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#checksum
699bdd1243dSDimitry Andric   uint32_t *buf = (uint32_t *)buffer->getBufferStart();
700bdd1243dSDimitry Andric   uint32_t size = (uint32_t)(buffer->getBufferSize());
701bdd1243dSDimitry Andric 
702bdd1243dSDimitry Andric   coff_file_header *coffHeader =
703bdd1243dSDimitry Andric       (coff_file_header *)((uint8_t *)buf + dosStubSize + sizeof(PEMagic));
704bdd1243dSDimitry Andric   pe32_header *peHeader =
705bdd1243dSDimitry Andric       (pe32_header *)((uint8_t *)coffHeader + sizeof(coff_file_header));
706bdd1243dSDimitry Andric 
707bdd1243dSDimitry Andric   uint64_t sum = 0;
708bdd1243dSDimitry Andric   uint32_t count = size;
709bdd1243dSDimitry Andric   ulittle16_t *addr = (ulittle16_t *)buf;
710bdd1243dSDimitry Andric 
711bdd1243dSDimitry Andric   // The PE checksum algorithm, implemented as suggested in RFC1071
712bdd1243dSDimitry Andric   while (count > 1) {
713bdd1243dSDimitry Andric     sum += *addr++;
714bdd1243dSDimitry Andric     count -= 2;
715bdd1243dSDimitry Andric   }
716bdd1243dSDimitry Andric 
717bdd1243dSDimitry Andric   // Add left-over byte, if any
718bdd1243dSDimitry Andric   if (count > 0)
719bdd1243dSDimitry Andric     sum += *(unsigned char *)addr;
720bdd1243dSDimitry Andric 
721bdd1243dSDimitry Andric   // Fold 32-bit sum to 16 bits
722bdd1243dSDimitry Andric   while (sum >> 16) {
723bdd1243dSDimitry Andric     sum = (sum & 0xffff) + (sum >> 16);
724bdd1243dSDimitry Andric   }
725bdd1243dSDimitry Andric 
726bdd1243dSDimitry Andric   sum += size;
727bdd1243dSDimitry Andric   peHeader->CheckSum = sum;
728bdd1243dSDimitry Andric }
729bdd1243dSDimitry Andric 
7300b57cec5SDimitry Andric // The main function of the writer.
7310b57cec5SDimitry Andric void Writer::run() {
732*5f757f3fSDimitry Andric   {
733*5f757f3fSDimitry Andric     llvm::TimeTraceScope timeScope("Write PE");
734349cc55cSDimitry Andric     ScopedTimer t1(ctx.codeLayoutTimer);
7350b57cec5SDimitry Andric 
7360b57cec5SDimitry Andric     createImportTables();
7370b57cec5SDimitry Andric     createSections();
7380b57cec5SDimitry Andric     appendImportThunks();
739*5f757f3fSDimitry Andric     // Import thunks must be added before the Control Flow Guard tables are
740*5f757f3fSDimitry Andric     // added.
741e8d8bef9SDimitry Andric     createMiscChunks();
7420b57cec5SDimitry Andric     createExportTable();
7430b57cec5SDimitry Andric     mergeSections();
744*5f757f3fSDimitry Andric     sortECChunks();
7450b57cec5SDimitry Andric     removeUnusedSections();
7460b57cec5SDimitry Andric     finalizeAddresses();
7470b57cec5SDimitry Andric     removeEmptySections();
7480b57cec5SDimitry Andric     assignOutputSectionIndices();
7490b57cec5SDimitry Andric     setSectionPermissions();
750*5f757f3fSDimitry Andric     setECSymbols();
7510b57cec5SDimitry Andric     createSymbolAndStringTable();
7520b57cec5SDimitry Andric 
7530b57cec5SDimitry Andric     if (fileSize > UINT32_MAX)
7540b57cec5SDimitry Andric       fatal("image size (" + Twine(fileSize) + ") " +
7550b57cec5SDimitry Andric             "exceeds maximum allowable size (" + Twine(UINT32_MAX) + ")");
7560b57cec5SDimitry Andric 
757bdd1243dSDimitry Andric     openFile(ctx.config.outputFile);
758bdd1243dSDimitry Andric     if (ctx.config.is64()) {
7590b57cec5SDimitry Andric       writeHeader<pe32plus_header>();
7600b57cec5SDimitry Andric     } else {
7610b57cec5SDimitry Andric       writeHeader<pe32_header>();
7620b57cec5SDimitry Andric     }
7630b57cec5SDimitry Andric     writeSections();
764*5f757f3fSDimitry Andric     prepareLoadConfig();
765*5f757f3fSDimitry Andric     sortExceptionTables();
7660b57cec5SDimitry Andric 
767e8d8bef9SDimitry Andric     // Fix up the alignment in the TLS Directory's characteristic field,
768e8d8bef9SDimitry Andric     // if a specific alignment value is needed
769e8d8bef9SDimitry Andric     if (tlsAlignment)
770e8d8bef9SDimitry Andric       fixTlsAlignment();
771*5f757f3fSDimitry Andric   }
7720b57cec5SDimitry Andric 
773bdd1243dSDimitry Andric   if (!ctx.config.pdbPath.empty() && ctx.config.debug) {
7740b57cec5SDimitry Andric     assert(buildId);
775349cc55cSDimitry Andric     createPDB(ctx, sectionTable, buildId->buildId);
7760b57cec5SDimitry Andric   }
7770b57cec5SDimitry Andric   writeBuildId();
7780b57cec5SDimitry Andric 
779349cc55cSDimitry Andric   writeLLDMapFile(ctx);
780349cc55cSDimitry Andric   writeMapFile(ctx);
7810b57cec5SDimitry Andric 
782bdd1243dSDimitry Andric   writePEChecksum();
783bdd1243dSDimitry Andric 
7840b57cec5SDimitry Andric   if (errorCount())
7850b57cec5SDimitry Andric     return;
7860b57cec5SDimitry Andric 
787*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Commit PE to disk");
788349cc55cSDimitry Andric   ScopedTimer t2(ctx.outputCommitTimer);
7890b57cec5SDimitry Andric   if (auto e = buffer->commit())
790bdd1243dSDimitry Andric     fatal("failed to write output '" + buffer->getPath() +
791bdd1243dSDimitry Andric           "': " + toString(std::move(e)));
7920b57cec5SDimitry Andric }
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric static StringRef getOutputSectionName(StringRef name) {
7950b57cec5SDimitry Andric   StringRef s = name.split('$').first;
7960b57cec5SDimitry Andric 
7970b57cec5SDimitry Andric   // Treat a later period as a separator for MinGW, for sections like
7980b57cec5SDimitry Andric   // ".ctors.01234".
7990b57cec5SDimitry Andric   return s.substr(0, s.find('.', 1));
8000b57cec5SDimitry Andric }
8010b57cec5SDimitry Andric 
8020b57cec5SDimitry Andric // For /order.
803bdd1243dSDimitry Andric void Writer::sortBySectionOrder(std::vector<Chunk *> &chunks) {
804bdd1243dSDimitry Andric   auto getPriority = [&ctx = ctx](const Chunk *c) {
8050b57cec5SDimitry Andric     if (auto *sec = dyn_cast<SectionChunk>(c))
8060b57cec5SDimitry Andric       if (sec->sym)
807bdd1243dSDimitry Andric         return ctx.config.order.lookup(sec->sym->getName());
8080b57cec5SDimitry Andric     return 0;
8090b57cec5SDimitry Andric   };
8100b57cec5SDimitry Andric 
8110b57cec5SDimitry Andric   llvm::stable_sort(chunks, [=](const Chunk *a, const Chunk *b) {
8120b57cec5SDimitry Andric     return getPriority(a) < getPriority(b);
8130b57cec5SDimitry Andric   });
8140b57cec5SDimitry Andric }
8150b57cec5SDimitry Andric 
8160b57cec5SDimitry Andric // Change the characteristics of existing PartialSections that belong to the
8170b57cec5SDimitry Andric // section Name to Chars.
8180b57cec5SDimitry Andric void Writer::fixPartialSectionChars(StringRef name, uint32_t chars) {
8190b57cec5SDimitry Andric   for (auto it : partialSections) {
8200b57cec5SDimitry Andric     PartialSection *pSec = it.second;
8210b57cec5SDimitry Andric     StringRef curName = pSec->name;
8220b57cec5SDimitry Andric     if (!curName.consume_front(name) ||
82306c3fb27SDimitry Andric         (!curName.empty() && !curName.starts_with("$")))
8240b57cec5SDimitry Andric       continue;
8250b57cec5SDimitry Andric     if (pSec->characteristics == chars)
8260b57cec5SDimitry Andric       continue;
8270b57cec5SDimitry Andric     PartialSection *destSec = createPartialSection(pSec->name, chars);
8280b57cec5SDimitry Andric     destSec->chunks.insert(destSec->chunks.end(), pSec->chunks.begin(),
8290b57cec5SDimitry Andric                            pSec->chunks.end());
8300b57cec5SDimitry Andric     pSec->chunks.clear();
8310b57cec5SDimitry Andric   }
8320b57cec5SDimitry Andric }
8330b57cec5SDimitry Andric 
8340b57cec5SDimitry Andric // Sort concrete section chunks from GNU import libraries.
8350b57cec5SDimitry Andric //
8360b57cec5SDimitry Andric // GNU binutils doesn't use short import files, but instead produces import
8370b57cec5SDimitry Andric // libraries that consist of object files, with section chunks for the .idata$*
8380b57cec5SDimitry Andric // sections. These are linked just as regular static libraries. Each import
8390b57cec5SDimitry Andric // library consists of one header object, one object file for every imported
8400b57cec5SDimitry Andric // symbol, and one trailer object. In order for the .idata tables/lists to
8410b57cec5SDimitry Andric // be formed correctly, the section chunks within each .idata$* section need
8420b57cec5SDimitry Andric // to be grouped by library, and sorted alphabetically within each library
8430b57cec5SDimitry Andric // (which makes sure the header comes first and the trailer last).
8440b57cec5SDimitry Andric bool Writer::fixGnuImportChunks() {
8450b57cec5SDimitry Andric   uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric   // Make sure all .idata$* section chunks are mapped as RDATA in order to
8480b57cec5SDimitry Andric   // be sorted into the same sections as our own synthesized .idata chunks.
8490b57cec5SDimitry Andric   fixPartialSectionChars(".idata", rdata);
8500b57cec5SDimitry Andric 
8510b57cec5SDimitry Andric   bool hasIdata = false;
8520b57cec5SDimitry Andric   // Sort all .idata$* chunks, grouping chunks from the same library,
85381ad6265SDimitry Andric   // with alphabetical ordering of the object files within a library.
8540b57cec5SDimitry Andric   for (auto it : partialSections) {
8550b57cec5SDimitry Andric     PartialSection *pSec = it.second;
85606c3fb27SDimitry Andric     if (!pSec->name.starts_with(".idata"))
8570b57cec5SDimitry Andric       continue;
8580b57cec5SDimitry Andric 
8590b57cec5SDimitry Andric     if (!pSec->chunks.empty())
8600b57cec5SDimitry Andric       hasIdata = true;
8610b57cec5SDimitry Andric     llvm::stable_sort(pSec->chunks, [&](Chunk *s, Chunk *t) {
8620b57cec5SDimitry Andric       SectionChunk *sc1 = dyn_cast_or_null<SectionChunk>(s);
8630b57cec5SDimitry Andric       SectionChunk *sc2 = dyn_cast_or_null<SectionChunk>(t);
8640b57cec5SDimitry Andric       if (!sc1 || !sc2) {
8650b57cec5SDimitry Andric         // if SC1, order them ascending. If SC2 or both null,
8660b57cec5SDimitry Andric         // S is not less than T.
8670b57cec5SDimitry Andric         return sc1 != nullptr;
8680b57cec5SDimitry Andric       }
8690b57cec5SDimitry Andric       // Make a string with "libraryname/objectfile" for sorting, achieving
8700b57cec5SDimitry Andric       // both grouping by library and sorting of objects within a library,
8710b57cec5SDimitry Andric       // at once.
8720b57cec5SDimitry Andric       std::string key1 =
8730b57cec5SDimitry Andric           (sc1->file->parentName + "/" + sc1->file->getName()).str();
8740b57cec5SDimitry Andric       std::string key2 =
8750b57cec5SDimitry Andric           (sc2->file->parentName + "/" + sc2->file->getName()).str();
8760b57cec5SDimitry Andric       return key1 < key2;
8770b57cec5SDimitry Andric     });
8780b57cec5SDimitry Andric   }
8790b57cec5SDimitry Andric   return hasIdata;
8800b57cec5SDimitry Andric }
8810b57cec5SDimitry Andric 
8820b57cec5SDimitry Andric // Add generated idata chunks, for imported symbols and DLLs, and a
8830b57cec5SDimitry Andric // terminator in .idata$2.
8840b57cec5SDimitry Andric void Writer::addSyntheticIdata() {
8850b57cec5SDimitry Andric   uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
886bdd1243dSDimitry Andric   idata.create(ctx);
8870b57cec5SDimitry Andric 
8880b57cec5SDimitry Andric   // Add the .idata content in the right section groups, to allow
8890b57cec5SDimitry Andric   // chunks from other linked in object files to be grouped together.
8900b57cec5SDimitry Andric   // See Microsoft PE/COFF spec 5.4 for details.
8910b57cec5SDimitry Andric   auto add = [&](StringRef n, std::vector<Chunk *> &v) {
8920b57cec5SDimitry Andric     PartialSection *pSec = createPartialSection(n, rdata);
8930b57cec5SDimitry Andric     pSec->chunks.insert(pSec->chunks.end(), v.begin(), v.end());
8940b57cec5SDimitry Andric   };
8950b57cec5SDimitry Andric 
8960b57cec5SDimitry Andric   // The loader assumes a specific order of data.
8970b57cec5SDimitry Andric   // Add each type in the correct order.
8980b57cec5SDimitry Andric   add(".idata$2", idata.dirs);
8990b57cec5SDimitry Andric   add(".idata$4", idata.lookups);
9000b57cec5SDimitry Andric   add(".idata$5", idata.addresses);
90185868e8aSDimitry Andric   if (!idata.hints.empty())
9020b57cec5SDimitry Andric     add(".idata$6", idata.hints);
9030b57cec5SDimitry Andric   add(".idata$7", idata.dllNames);
9040b57cec5SDimitry Andric }
9050b57cec5SDimitry Andric 
9060b57cec5SDimitry Andric // Locate the first Chunk and size of the import directory list and the
9070b57cec5SDimitry Andric // IAT.
9080b57cec5SDimitry Andric void Writer::locateImportTables() {
9090b57cec5SDimitry Andric   uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
9100b57cec5SDimitry Andric 
9110b57cec5SDimitry Andric   if (PartialSection *importDirs = findPartialSection(".idata$2", rdata)) {
9120b57cec5SDimitry Andric     if (!importDirs->chunks.empty())
9130b57cec5SDimitry Andric       importTableStart = importDirs->chunks.front();
9140b57cec5SDimitry Andric     for (Chunk *c : importDirs->chunks)
9150b57cec5SDimitry Andric       importTableSize += c->getSize();
9160b57cec5SDimitry Andric   }
9170b57cec5SDimitry Andric 
9180b57cec5SDimitry Andric   if (PartialSection *importAddresses = findPartialSection(".idata$5", rdata)) {
9190b57cec5SDimitry Andric     if (!importAddresses->chunks.empty())
9200b57cec5SDimitry Andric       iatStart = importAddresses->chunks.front();
9210b57cec5SDimitry Andric     for (Chunk *c : importAddresses->chunks)
9220b57cec5SDimitry Andric       iatSize += c->getSize();
9230b57cec5SDimitry Andric   }
9240b57cec5SDimitry Andric }
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric // Return whether a SectionChunk's suffix (the dollar and any trailing
9270b57cec5SDimitry Andric // suffix) should be removed and sorted into the main suffixless
9280b57cec5SDimitry Andric // PartialSection.
929bdd1243dSDimitry Andric static bool shouldStripSectionSuffix(SectionChunk *sc, StringRef name,
930bdd1243dSDimitry Andric                                      bool isMinGW) {
9310b57cec5SDimitry Andric   // On MinGW, comdat groups are formed by putting the comdat group name
9320b57cec5SDimitry Andric   // after the '$' in the section name. For .eh_frame$<symbol>, that must
9330b57cec5SDimitry Andric   // still be sorted before the .eh_frame trailer from crtend.o, thus just
9340b57cec5SDimitry Andric   // strip the section name trailer. For other sections, such as
9350b57cec5SDimitry Andric   // .tls$$<symbol> (where non-comdat .tls symbols are otherwise stored in
9360b57cec5SDimitry Andric   // ".tls$"), they must be strictly sorted after .tls. And for the
9370b57cec5SDimitry Andric   // hypothetical case of comdat .CRT$XCU, we definitely need to keep the
9380b57cec5SDimitry Andric   // suffix for sorting. Thus, to play it safe, only strip the suffix for
9390b57cec5SDimitry Andric   // the standard sections.
940bdd1243dSDimitry Andric   if (!isMinGW)
9410b57cec5SDimitry Andric     return false;
9420b57cec5SDimitry Andric   if (!sc || !sc->isCOMDAT())
9430b57cec5SDimitry Andric     return false;
94406c3fb27SDimitry Andric   return name.starts_with(".text$") || name.starts_with(".data$") ||
94506c3fb27SDimitry Andric          name.starts_with(".rdata$") || name.starts_with(".pdata$") ||
94606c3fb27SDimitry Andric          name.starts_with(".xdata$") || name.starts_with(".eh_frame$");
9470b57cec5SDimitry Andric }
9480b57cec5SDimitry Andric 
949e8d8bef9SDimitry Andric void Writer::sortSections() {
950bdd1243dSDimitry Andric   if (!ctx.config.callGraphProfile.empty()) {
951349cc55cSDimitry Andric     DenseMap<const SectionChunk *, int> order =
952349cc55cSDimitry Andric         computeCallGraphProfileOrder(ctx);
953e8d8bef9SDimitry Andric     for (auto it : order) {
954e8d8bef9SDimitry Andric       if (DefinedRegular *sym = it.first->sym)
955bdd1243dSDimitry Andric         ctx.config.order[sym->getName()] = it.second;
956e8d8bef9SDimitry Andric     }
957e8d8bef9SDimitry Andric   }
958bdd1243dSDimitry Andric   if (!ctx.config.order.empty())
959e8d8bef9SDimitry Andric     for (auto it : partialSections)
960e8d8bef9SDimitry Andric       sortBySectionOrder(it.second->chunks);
961e8d8bef9SDimitry Andric }
962e8d8bef9SDimitry Andric 
9630b57cec5SDimitry Andric // Create output section objects and add them to OutputSections.
9640b57cec5SDimitry Andric void Writer::createSections() {
965*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Output sections");
9660b57cec5SDimitry Andric   // First, create the builtin sections.
9670b57cec5SDimitry Andric   const uint32_t data = IMAGE_SCN_CNT_INITIALIZED_DATA;
9680b57cec5SDimitry Andric   const uint32_t bss = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
9690b57cec5SDimitry Andric   const uint32_t code = IMAGE_SCN_CNT_CODE;
9700b57cec5SDimitry Andric   const uint32_t discardable = IMAGE_SCN_MEM_DISCARDABLE;
9710b57cec5SDimitry Andric   const uint32_t r = IMAGE_SCN_MEM_READ;
9720b57cec5SDimitry Andric   const uint32_t w = IMAGE_SCN_MEM_WRITE;
9730b57cec5SDimitry Andric   const uint32_t x = IMAGE_SCN_MEM_EXECUTE;
9740b57cec5SDimitry Andric 
9750b57cec5SDimitry Andric   SmallDenseMap<std::pair<StringRef, uint32_t>, OutputSection *> sections;
9760b57cec5SDimitry Andric   auto createSection = [&](StringRef name, uint32_t outChars) {
9770b57cec5SDimitry Andric     OutputSection *&sec = sections[{name, outChars}];
9780b57cec5SDimitry Andric     if (!sec) {
9790b57cec5SDimitry Andric       sec = make<OutputSection>(name, outChars);
980349cc55cSDimitry Andric       ctx.outputSections.push_back(sec);
9810b57cec5SDimitry Andric     }
9820b57cec5SDimitry Andric     return sec;
9830b57cec5SDimitry Andric   };
9840b57cec5SDimitry Andric 
9850b57cec5SDimitry Andric   // Try to match the section order used by link.exe.
9860b57cec5SDimitry Andric   textSec = createSection(".text", code | r | x);
9870b57cec5SDimitry Andric   createSection(".bss", bss | r | w);
9880b57cec5SDimitry Andric   rdataSec = createSection(".rdata", data | r);
9890b57cec5SDimitry Andric   buildidSec = createSection(".buildid", data | r);
9900b57cec5SDimitry Andric   dataSec = createSection(".data", data | r | w);
9910b57cec5SDimitry Andric   pdataSec = createSection(".pdata", data | r);
9920b57cec5SDimitry Andric   idataSec = createSection(".idata", data | r);
9930b57cec5SDimitry Andric   edataSec = createSection(".edata", data | r);
9940b57cec5SDimitry Andric   didatSec = createSection(".didat", data | r);
9950b57cec5SDimitry Andric   rsrcSec = createSection(".rsrc", data | r);
9960b57cec5SDimitry Andric   relocSec = createSection(".reloc", data | discardable | r);
9970b57cec5SDimitry Andric   ctorsSec = createSection(".ctors", data | r | w);
9980b57cec5SDimitry Andric   dtorsSec = createSection(".dtors", data | r | w);
9990b57cec5SDimitry Andric 
10000b57cec5SDimitry Andric   // Then bin chunks by name and output characteristics.
1001349cc55cSDimitry Andric   for (Chunk *c : ctx.symtab.getChunks()) {
10020b57cec5SDimitry Andric     auto *sc = dyn_cast<SectionChunk>(c);
10030b57cec5SDimitry Andric     if (sc && !sc->live) {
1004bdd1243dSDimitry Andric       if (ctx.config.verbose)
10050b57cec5SDimitry Andric         sc->printDiscardedMessage();
10060b57cec5SDimitry Andric       continue;
10070b57cec5SDimitry Andric     }
10080b57cec5SDimitry Andric     StringRef name = c->getSectionName();
1009bdd1243dSDimitry Andric     if (shouldStripSectionSuffix(sc, name, ctx.config.mingw))
10100b57cec5SDimitry Andric       name = name.split('$').first;
1011e8d8bef9SDimitry Andric 
101206c3fb27SDimitry Andric     if (name.starts_with(".tls"))
1013e8d8bef9SDimitry Andric       tlsAlignment = std::max(tlsAlignment, c->getAlignment());
1014e8d8bef9SDimitry Andric 
10150b57cec5SDimitry Andric     PartialSection *pSec = createPartialSection(name,
10160b57cec5SDimitry Andric                                                 c->getOutputCharacteristics());
10170b57cec5SDimitry Andric     pSec->chunks.push_back(c);
10180b57cec5SDimitry Andric   }
10190b57cec5SDimitry Andric 
10200b57cec5SDimitry Andric   fixPartialSectionChars(".rsrc", data | r);
102185868e8aSDimitry Andric   fixPartialSectionChars(".edata", data | r);
10220b57cec5SDimitry Andric   // Even in non MinGW cases, we might need to link against GNU import
10230b57cec5SDimitry Andric   // libraries.
10240b57cec5SDimitry Andric   bool hasIdata = fixGnuImportChunks();
10250b57cec5SDimitry Andric   if (!idata.empty())
10260b57cec5SDimitry Andric     hasIdata = true;
10270b57cec5SDimitry Andric 
10280b57cec5SDimitry Andric   if (hasIdata)
10290b57cec5SDimitry Andric     addSyntheticIdata();
10300b57cec5SDimitry Andric 
1031e8d8bef9SDimitry Andric   sortSections();
10320b57cec5SDimitry Andric 
10330b57cec5SDimitry Andric   if (hasIdata)
10340b57cec5SDimitry Andric     locateImportTables();
10350b57cec5SDimitry Andric 
10360b57cec5SDimitry Andric   // Then create an OutputSection for each section.
10370b57cec5SDimitry Andric   // '$' and all following characters in input section names are
10380b57cec5SDimitry Andric   // discarded when determining output section. So, .text$foo
10390b57cec5SDimitry Andric   // contributes to .text, for example. See PE/COFF spec 3.2.
10400b57cec5SDimitry Andric   for (auto it : partialSections) {
10410b57cec5SDimitry Andric     PartialSection *pSec = it.second;
10420b57cec5SDimitry Andric     StringRef name = getOutputSectionName(pSec->name);
10430b57cec5SDimitry Andric     uint32_t outChars = pSec->characteristics;
10440b57cec5SDimitry Andric 
10450b57cec5SDimitry Andric     if (name == ".CRT") {
10460b57cec5SDimitry Andric       // In link.exe, there is a special case for the I386 target where .CRT
10470b57cec5SDimitry Andric       // sections are treated as if they have output characteristics DATA | R if
10480b57cec5SDimitry Andric       // their characteristics are DATA | R | W. This implements the same
10490b57cec5SDimitry Andric       // special case for all architectures.
10500b57cec5SDimitry Andric       outChars = data | r;
10510b57cec5SDimitry Andric 
10520b57cec5SDimitry Andric       log("Processing section " + pSec->name + " -> " + name);
10530b57cec5SDimitry Andric 
10540b57cec5SDimitry Andric       sortCRTSectionChunks(pSec->chunks);
10550b57cec5SDimitry Andric     }
10560b57cec5SDimitry Andric 
10570b57cec5SDimitry Andric     OutputSection *sec = createSection(name, outChars);
10580b57cec5SDimitry Andric     for (Chunk *c : pSec->chunks)
10590b57cec5SDimitry Andric       sec->addChunk(c);
10600b57cec5SDimitry Andric 
10610b57cec5SDimitry Andric     sec->addContributingPartialSection(pSec);
10620b57cec5SDimitry Andric   }
10630b57cec5SDimitry Andric 
10640b57cec5SDimitry Andric   // Finally, move some output sections to the end.
10650b57cec5SDimitry Andric   auto sectionOrder = [&](const OutputSection *s) {
10660b57cec5SDimitry Andric     // Move DISCARDABLE (or non-memory-mapped) sections to the end of file
10670b57cec5SDimitry Andric     // because the loader cannot handle holes. Stripping can remove other
10680b57cec5SDimitry Andric     // discardable ones than .reloc, which is first of them (created early).
1069fb03ea46SDimitry Andric     if (s->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) {
1070fb03ea46SDimitry Andric       // Move discardable sections named .debug_ to the end, after other
1071fb03ea46SDimitry Andric       // discardable sections. Stripping only removes the sections named
1072fb03ea46SDimitry Andric       // .debug_* - thus try to avoid leaving holes after stripping.
107306c3fb27SDimitry Andric       if (s->name.starts_with(".debug_"))
1074fb03ea46SDimitry Andric         return 3;
10750b57cec5SDimitry Andric       return 2;
1076fb03ea46SDimitry Andric     }
10770b57cec5SDimitry Andric     // .rsrc should come at the end of the non-discardable sections because its
10780b57cec5SDimitry Andric     // size may change by the Win32 UpdateResources() function, causing
10790b57cec5SDimitry Andric     // subsequent sections to move (see https://crbug.com/827082).
10800b57cec5SDimitry Andric     if (s == rsrcSec)
10810b57cec5SDimitry Andric       return 1;
10820b57cec5SDimitry Andric     return 0;
10830b57cec5SDimitry Andric   };
1084349cc55cSDimitry Andric   llvm::stable_sort(ctx.outputSections,
10850b57cec5SDimitry Andric                     [&](const OutputSection *s, const OutputSection *t) {
10860b57cec5SDimitry Andric                       return sectionOrder(s) < sectionOrder(t);
10870b57cec5SDimitry Andric                     });
10880b57cec5SDimitry Andric }
10890b57cec5SDimitry Andric 
10900b57cec5SDimitry Andric void Writer::createMiscChunks() {
1091*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Misc chunks");
1092bdd1243dSDimitry Andric   Configuration *config = &ctx.config;
1093bdd1243dSDimitry Andric 
1094349cc55cSDimitry Andric   for (MergeChunk *p : ctx.mergeChunkInstances) {
10950b57cec5SDimitry Andric     if (p) {
10960b57cec5SDimitry Andric       p->finalizeContents();
10970b57cec5SDimitry Andric       rdataSec->addChunk(p);
10980b57cec5SDimitry Andric     }
10990b57cec5SDimitry Andric   }
11000b57cec5SDimitry Andric 
11010b57cec5SDimitry Andric   // Create thunks for locally-dllimported symbols.
1102349cc55cSDimitry Andric   if (!ctx.symtab.localImportChunks.empty()) {
1103349cc55cSDimitry Andric     for (Chunk *c : ctx.symtab.localImportChunks)
11040b57cec5SDimitry Andric       rdataSec->addChunk(c);
11050b57cec5SDimitry Andric   }
11060b57cec5SDimitry Andric 
11070b57cec5SDimitry Andric   // Create Debug Information Chunks
1108*5f757f3fSDimitry Andric   debugInfoSec = config->mingw ? buildidSec : rdataSec;
1109*5f757f3fSDimitry Andric   if (config->buildIDHash != BuildIDHash::None || config->debug ||
1110*5f757f3fSDimitry Andric       config->repro || config->cetCompat) {
1111349cc55cSDimitry Andric     debugDirectory =
1112349cc55cSDimitry Andric         make<DebugDirectoryChunk>(ctx, debugRecords, config->repro);
11135ffd83dbSDimitry Andric     debugDirectory->setAlignment(4);
11140b57cec5SDimitry Andric     debugInfoSec->addChunk(debugDirectory);
11150b57cec5SDimitry Andric   }
11160b57cec5SDimitry Andric 
1117*5f757f3fSDimitry Andric   if (config->debug || config->buildIDHash != BuildIDHash::None) {
11180b57cec5SDimitry Andric     // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified.  We
11190b57cec5SDimitry Andric     // output a PDB no matter what, and this chunk provides the only means of
11200b57cec5SDimitry Andric     // allowing a debugger to match a PDB and an executable.  So we need it even
11210b57cec5SDimitry Andric     // if we're ultimately not going to write CodeView data to the PDB.
1122bdd1243dSDimitry Andric     buildId = make<CVDebugRecordChunk>(ctx);
112306c3fb27SDimitry Andric     debugRecords.emplace_back(COFF::IMAGE_DEBUG_TYPE_CODEVIEW, buildId);
1124*5f757f3fSDimitry Andric     if (Symbol *buildidSym = ctx.symtab.findUnderscore("__buildid"))
1125*5f757f3fSDimitry Andric       replaceSymbol<DefinedSynthetic>(buildidSym, buildidSym->getName(),
1126*5f757f3fSDimitry Andric                                       buildId, 4);
11275ffd83dbSDimitry Andric   }
11280b57cec5SDimitry Andric 
11295ffd83dbSDimitry Andric   if (config->cetCompat) {
113006c3fb27SDimitry Andric     debugRecords.emplace_back(COFF::IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS,
11315ffd83dbSDimitry Andric                               make<ExtendedDllCharacteristicsChunk>(
113206c3fb27SDimitry Andric                                   IMAGE_DLL_CHARACTERISTICS_EX_CET_COMPAT));
11335ffd83dbSDimitry Andric   }
11345ffd83dbSDimitry Andric 
1135e8d8bef9SDimitry Andric   // Align and add each chunk referenced by the debug data directory.
1136e8d8bef9SDimitry Andric   for (std::pair<COFF::DebugType, Chunk *> r : debugRecords) {
1137e8d8bef9SDimitry Andric     r.second->setAlignment(4);
11385ffd83dbSDimitry Andric     debugInfoSec->addChunk(r.second);
11390b57cec5SDimitry Andric   }
11400b57cec5SDimitry Andric 
11410b57cec5SDimitry Andric   // Create SEH table. x86-only.
11420b57cec5SDimitry Andric   if (config->safeSEH)
11430b57cec5SDimitry Andric     createSEHTable();
11440b57cec5SDimitry Andric 
11450b57cec5SDimitry Andric   // Create /guard:cf tables if requested.
11460b57cec5SDimitry Andric   if (config->guardCF != GuardCFLevel::Off)
11470b57cec5SDimitry Andric     createGuardCFTables();
11480b57cec5SDimitry Andric 
1149*5f757f3fSDimitry Andric   if (isArm64EC(config->machine))
1150*5f757f3fSDimitry Andric     createECChunks();
1151*5f757f3fSDimitry Andric 
11525ffd83dbSDimitry Andric   if (config->autoImport)
11530b57cec5SDimitry Andric     createRuntimePseudoRelocs();
11540b57cec5SDimitry Andric 
11555ffd83dbSDimitry Andric   if (config->mingw)
11560b57cec5SDimitry Andric     insertCtorDtorSymbols();
11570b57cec5SDimitry Andric }
11580b57cec5SDimitry Andric 
11590b57cec5SDimitry Andric // Create .idata section for the DLL-imported symbol table.
11600b57cec5SDimitry Andric // The format of this section is inherently Windows-specific.
11610b57cec5SDimitry Andric // IdataContents class abstracted away the details for us,
11620b57cec5SDimitry Andric // so we just let it create chunks and add them to the section.
11630b57cec5SDimitry Andric void Writer::createImportTables() {
1164*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Import tables");
11650b57cec5SDimitry Andric   // Initialize DLLOrder so that import entries are ordered in
11660b57cec5SDimitry Andric   // the same order as in the command line. (That affects DLL
11670b57cec5SDimitry Andric   // initialization order, and this ordering is MSVC-compatible.)
1168349cc55cSDimitry Andric   for (ImportFile *file : ctx.importFileInstances) {
11690b57cec5SDimitry Andric     if (!file->live)
11700b57cec5SDimitry Andric       continue;
11710b57cec5SDimitry Andric 
11720b57cec5SDimitry Andric     std::string dll = StringRef(file->dllName).lower();
1173bdd1243dSDimitry Andric     if (ctx.config.dllOrder.count(dll) == 0)
1174bdd1243dSDimitry Andric       ctx.config.dllOrder[dll] = ctx.config.dllOrder.size();
11750b57cec5SDimitry Andric 
11760b57cec5SDimitry Andric     if (file->impSym && !isa<DefinedImportData>(file->impSym))
1177bdd1243dSDimitry Andric       fatal(toString(ctx, *file->impSym) + " was replaced");
11780b57cec5SDimitry Andric     DefinedImportData *impSym = cast_or_null<DefinedImportData>(file->impSym);
1179bdd1243dSDimitry Andric     if (ctx.config.delayLoads.count(StringRef(file->dllName).lower())) {
11800b57cec5SDimitry Andric       if (!file->thunkSym)
11810b57cec5SDimitry Andric         fatal("cannot delay-load " + toString(file) +
1182bdd1243dSDimitry Andric               " due to import of data: " + toString(ctx, *impSym));
11830b57cec5SDimitry Andric       delayIdata.add(impSym);
11840b57cec5SDimitry Andric     } else {
11850b57cec5SDimitry Andric       idata.add(impSym);
11860b57cec5SDimitry Andric     }
11870b57cec5SDimitry Andric   }
11880b57cec5SDimitry Andric }
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric void Writer::appendImportThunks() {
1191349cc55cSDimitry Andric   if (ctx.importFileInstances.empty())
11920b57cec5SDimitry Andric     return;
11930b57cec5SDimitry Andric 
1194*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Import thunks");
1195349cc55cSDimitry Andric   for (ImportFile *file : ctx.importFileInstances) {
11960b57cec5SDimitry Andric     if (!file->live)
11970b57cec5SDimitry Andric       continue;
11980b57cec5SDimitry Andric 
11990b57cec5SDimitry Andric     if (!file->thunkSym)
12000b57cec5SDimitry Andric       continue;
12010b57cec5SDimitry Andric 
12020b57cec5SDimitry Andric     if (!isa<DefinedImportThunk>(file->thunkSym))
1203bdd1243dSDimitry Andric       fatal(toString(ctx, *file->thunkSym) + " was replaced");
12040b57cec5SDimitry Andric     DefinedImportThunk *thunk = cast<DefinedImportThunk>(file->thunkSym);
12050b57cec5SDimitry Andric     if (file->thunkLive)
12060b57cec5SDimitry Andric       textSec->addChunk(thunk->getChunk());
12070b57cec5SDimitry Andric   }
12080b57cec5SDimitry Andric 
12090b57cec5SDimitry Andric   if (!delayIdata.empty()) {
1210bdd1243dSDimitry Andric     Defined *helper = cast<Defined>(ctx.config.delayLoadHelper);
1211bdd1243dSDimitry Andric     delayIdata.create(helper);
12120b57cec5SDimitry Andric     for (Chunk *c : delayIdata.getChunks())
12130b57cec5SDimitry Andric       didatSec->addChunk(c);
12140b57cec5SDimitry Andric     for (Chunk *c : delayIdata.getDataChunks())
12150b57cec5SDimitry Andric       dataSec->addChunk(c);
12160b57cec5SDimitry Andric     for (Chunk *c : delayIdata.getCodeChunks())
12170b57cec5SDimitry Andric       textSec->addChunk(c);
1218bdd1243dSDimitry Andric     for (Chunk *c : delayIdata.getCodePData())
1219bdd1243dSDimitry Andric       pdataSec->addChunk(c);
1220bdd1243dSDimitry Andric     for (Chunk *c : delayIdata.getCodeUnwindInfo())
1221bdd1243dSDimitry Andric       rdataSec->addChunk(c);
12220b57cec5SDimitry Andric   }
12230b57cec5SDimitry Andric }
12240b57cec5SDimitry Andric 
12250b57cec5SDimitry Andric void Writer::createExportTable() {
1226*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Export table");
122785868e8aSDimitry Andric   if (!edataSec->chunks.empty()) {
122885868e8aSDimitry Andric     // Allow using a custom built export table from input object files, instead
122985868e8aSDimitry Andric     // of having the linker synthesize the tables.
1230bdd1243dSDimitry Andric     if (ctx.config.hadExplicitExports)
123185868e8aSDimitry Andric       warn("literal .edata sections override exports");
1232bdd1243dSDimitry Andric   } else if (!ctx.config.exports.empty()) {
12330b57cec5SDimitry Andric     for (Chunk *c : edata.chunks)
12340b57cec5SDimitry Andric       edataSec->addChunk(c);
12350b57cec5SDimitry Andric   }
123685868e8aSDimitry Andric   if (!edataSec->chunks.empty()) {
123785868e8aSDimitry Andric     edataStart = edataSec->chunks.front();
123885868e8aSDimitry Andric     edataEnd = edataSec->chunks.back();
123985868e8aSDimitry Andric   }
1240fe6060f1SDimitry Andric   // Warn on exported deleting destructor.
1241bdd1243dSDimitry Andric   for (auto e : ctx.config.exports)
124206c3fb27SDimitry Andric     if (e.sym && e.sym->getName().starts_with("??_G"))
1243bdd1243dSDimitry Andric       warn("export of deleting dtor: " + toString(ctx, *e.sym));
124485868e8aSDimitry Andric }
12450b57cec5SDimitry Andric 
12460b57cec5SDimitry Andric void Writer::removeUnusedSections() {
1247*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Remove unused sections");
12480b57cec5SDimitry Andric   // Remove sections that we can be sure won't get content, to avoid
12490b57cec5SDimitry Andric   // allocating space for their section headers.
12500b57cec5SDimitry Andric   auto isUnused = [this](OutputSection *s) {
12510b57cec5SDimitry Andric     if (s == relocSec)
12520b57cec5SDimitry Andric       return false; // This section is populated later.
12530b57cec5SDimitry Andric     // MergeChunks have zero size at this point, as their size is finalized
12540b57cec5SDimitry Andric     // later. Only remove sections that have no Chunks at all.
12550b57cec5SDimitry Andric     return s->chunks.empty();
12560b57cec5SDimitry Andric   };
1257349cc55cSDimitry Andric   llvm::erase_if(ctx.outputSections, isUnused);
12580b57cec5SDimitry Andric }
12590b57cec5SDimitry Andric 
12600b57cec5SDimitry Andric // The Windows loader doesn't seem to like empty sections,
12610b57cec5SDimitry Andric // so we remove them if any.
12620b57cec5SDimitry Andric void Writer::removeEmptySections() {
1263*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Remove empty sections");
12640b57cec5SDimitry Andric   auto isEmpty = [](OutputSection *s) { return s->getVirtualSize() == 0; };
1265349cc55cSDimitry Andric   llvm::erase_if(ctx.outputSections, isEmpty);
12660b57cec5SDimitry Andric }
12670b57cec5SDimitry Andric 
12680b57cec5SDimitry Andric void Writer::assignOutputSectionIndices() {
1269*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Output sections indices");
12700b57cec5SDimitry Andric   // Assign final output section indices, and assign each chunk to its output
12710b57cec5SDimitry Andric   // section.
12720b57cec5SDimitry Andric   uint32_t idx = 1;
1273349cc55cSDimitry Andric   for (OutputSection *os : ctx.outputSections) {
12740b57cec5SDimitry Andric     os->sectionIndex = idx;
12750b57cec5SDimitry Andric     for (Chunk *c : os->chunks)
12760b57cec5SDimitry Andric       c->setOutputSectionIdx(idx);
12770b57cec5SDimitry Andric     ++idx;
12780b57cec5SDimitry Andric   }
12790b57cec5SDimitry Andric 
12800b57cec5SDimitry Andric   // Merge chunks are containers of chunks, so assign those an output section
12810b57cec5SDimitry Andric   // too.
1282349cc55cSDimitry Andric   for (MergeChunk *mc : ctx.mergeChunkInstances)
12830b57cec5SDimitry Andric     if (mc)
12840b57cec5SDimitry Andric       for (SectionChunk *sc : mc->sections)
12850b57cec5SDimitry Andric         if (sc && sc->live)
12860b57cec5SDimitry Andric           sc->setOutputSectionIdx(mc->getOutputSectionIdx());
12870b57cec5SDimitry Andric }
12880b57cec5SDimitry Andric 
12890b57cec5SDimitry Andric size_t Writer::addEntryToStringTable(StringRef str) {
12900b57cec5SDimitry Andric   assert(str.size() > COFF::NameSize);
12910b57cec5SDimitry Andric   size_t offsetOfEntry = strtab.size() + 4; // +4 for the size field
12920b57cec5SDimitry Andric   strtab.insert(strtab.end(), str.begin(), str.end());
12930b57cec5SDimitry Andric   strtab.push_back('\0');
12940b57cec5SDimitry Andric   return offsetOfEntry;
12950b57cec5SDimitry Andric }
12960b57cec5SDimitry Andric 
1297bdd1243dSDimitry Andric std::optional<coff_symbol16> Writer::createSymbol(Defined *def) {
12980b57cec5SDimitry Andric   coff_symbol16 sym;
12990b57cec5SDimitry Andric   switch (def->kind()) {
1300bdd1243dSDimitry Andric   case Symbol::DefinedAbsoluteKind: {
1301bdd1243dSDimitry Andric     auto *da = dyn_cast<DefinedAbsolute>(def);
1302bdd1243dSDimitry Andric     // Note: COFF symbol can only store 32-bit values, so 64-bit absolute
1303bdd1243dSDimitry Andric     // values will be truncated.
1304bdd1243dSDimitry Andric     sym.Value = da->getVA();
13050b57cec5SDimitry Andric     sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
13060b57cec5SDimitry Andric     break;
1307bdd1243dSDimitry Andric   }
13080b57cec5SDimitry Andric   default: {
13090b57cec5SDimitry Andric     // Don't write symbols that won't be written to the output to the symbol
13100b57cec5SDimitry Andric     // table.
1311bdd1243dSDimitry Andric     // We also try to write DefinedSynthetic as a normal symbol. Some of these
1312bdd1243dSDimitry Andric     // symbols do point to an actual chunk, like __safe_se_handler_table. Others
1313bdd1243dSDimitry Andric     // like __ImageBase are outside of sections and thus cannot be represented.
13140b57cec5SDimitry Andric     Chunk *c = def->getChunk();
13150b57cec5SDimitry Andric     if (!c)
1316bdd1243dSDimitry Andric       return std::nullopt;
1317349cc55cSDimitry Andric     OutputSection *os = ctx.getOutputSection(c);
13180b57cec5SDimitry Andric     if (!os)
1319bdd1243dSDimitry Andric       return std::nullopt;
13200b57cec5SDimitry Andric 
13210b57cec5SDimitry Andric     sym.Value = def->getRVA() - os->getRVA();
13220b57cec5SDimitry Andric     sym.SectionNumber = os->sectionIndex;
13230b57cec5SDimitry Andric     break;
13240b57cec5SDimitry Andric   }
13250b57cec5SDimitry Andric   }
13260b57cec5SDimitry Andric 
13270b57cec5SDimitry Andric   // Symbols that are runtime pseudo relocations don't point to the actual
13280b57cec5SDimitry Andric   // symbol data itself (as they are imported), but points to the IAT entry
13290b57cec5SDimitry Andric   // instead. Avoid emitting them to the symbol table, as they can confuse
13300b57cec5SDimitry Andric   // debuggers.
13310b57cec5SDimitry Andric   if (def->isRuntimePseudoReloc)
1332bdd1243dSDimitry Andric     return std::nullopt;
13330b57cec5SDimitry Andric 
13340b57cec5SDimitry Andric   StringRef name = def->getName();
13350b57cec5SDimitry Andric   if (name.size() > COFF::NameSize) {
13360b57cec5SDimitry Andric     sym.Name.Offset.Zeroes = 0;
13370b57cec5SDimitry Andric     sym.Name.Offset.Offset = addEntryToStringTable(name);
13380b57cec5SDimitry Andric   } else {
13390b57cec5SDimitry Andric     memset(sym.Name.ShortName, 0, COFF::NameSize);
13400b57cec5SDimitry Andric     memcpy(sym.Name.ShortName, name.data(), name.size());
13410b57cec5SDimitry Andric   }
13420b57cec5SDimitry Andric 
13430b57cec5SDimitry Andric   if (auto *d = dyn_cast<DefinedCOFF>(def)) {
13440b57cec5SDimitry Andric     COFFSymbolRef ref = d->getCOFFSymbol();
13450b57cec5SDimitry Andric     sym.Type = ref.getType();
13460b57cec5SDimitry Andric     sym.StorageClass = ref.getStorageClass();
1347bdd1243dSDimitry Andric   } else if (def->kind() == Symbol::DefinedImportThunkKind) {
1348bdd1243dSDimitry Andric     sym.Type = (IMAGE_SYM_DTYPE_FUNCTION << SCT_COMPLEX_TYPE_SHIFT) |
1349bdd1243dSDimitry Andric                IMAGE_SYM_TYPE_NULL;
1350bdd1243dSDimitry Andric     sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
13510b57cec5SDimitry Andric   } else {
13520b57cec5SDimitry Andric     sym.Type = IMAGE_SYM_TYPE_NULL;
13530b57cec5SDimitry Andric     sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
13540b57cec5SDimitry Andric   }
13550b57cec5SDimitry Andric   sym.NumberOfAuxSymbols = 0;
13560b57cec5SDimitry Andric   return sym;
13570b57cec5SDimitry Andric }
13580b57cec5SDimitry Andric 
13590b57cec5SDimitry Andric void Writer::createSymbolAndStringTable() {
1360*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Symbol and string table");
13610b57cec5SDimitry Andric   // PE/COFF images are limited to 8 byte section names. Longer names can be
13620b57cec5SDimitry Andric   // supported by writing a non-standard string table, but this string table is
13630b57cec5SDimitry Andric   // not mapped at runtime and the long names will therefore be inaccessible.
13640b57cec5SDimitry Andric   // link.exe always truncates section names to 8 bytes, whereas binutils always
13650b57cec5SDimitry Andric   // preserves long section names via the string table. LLD adopts a hybrid
13660b57cec5SDimitry Andric   // solution where discardable sections have long names preserved and
13670b57cec5SDimitry Andric   // non-discardable sections have their names truncated, to ensure that any
13680b57cec5SDimitry Andric   // section which is mapped at runtime also has its name mapped at runtime.
1369349cc55cSDimitry Andric   for (OutputSection *sec : ctx.outputSections) {
13700b57cec5SDimitry Andric     if (sec->name.size() <= COFF::NameSize)
13710b57cec5SDimitry Andric       continue;
13720b57cec5SDimitry Andric     if ((sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0)
13730b57cec5SDimitry Andric       continue;
1374bdd1243dSDimitry Andric     if (ctx.config.warnLongSectionNames) {
1375480093f4SDimitry Andric       warn("section name " + sec->name +
1376480093f4SDimitry Andric            " is longer than 8 characters and will use a non-standard string "
1377480093f4SDimitry Andric            "table");
1378480093f4SDimitry Andric     }
13790b57cec5SDimitry Andric     sec->setStringTableOff(addEntryToStringTable(sec->name));
13800b57cec5SDimitry Andric   }
13810b57cec5SDimitry Andric 
1382*5f757f3fSDimitry Andric   if (ctx.config.writeSymtab) {
1383349cc55cSDimitry Andric     for (ObjFile *file : ctx.objFileInstances) {
13840b57cec5SDimitry Andric       for (Symbol *b : file->getSymbols()) {
13850b57cec5SDimitry Andric         auto *d = dyn_cast_or_null<Defined>(b);
13860b57cec5SDimitry Andric         if (!d || d->writtenToSymtab)
13870b57cec5SDimitry Andric           continue;
13880b57cec5SDimitry Andric         d->writtenToSymtab = true;
13894824e7fdSDimitry Andric         if (auto *dc = dyn_cast_or_null<DefinedCOFF>(d)) {
13904824e7fdSDimitry Andric           COFFSymbolRef symRef = dc->getCOFFSymbol();
13914824e7fdSDimitry Andric           if (symRef.isSectionDefinition() ||
13924824e7fdSDimitry Andric               symRef.getStorageClass() == COFF::IMAGE_SYM_CLASS_LABEL)
13934824e7fdSDimitry Andric             continue;
13944824e7fdSDimitry Andric         }
13950b57cec5SDimitry Andric 
1396bdd1243dSDimitry Andric         if (std::optional<coff_symbol16> sym = createSymbol(d))
13970b57cec5SDimitry Andric           outputSymtab.push_back(*sym);
1398bdd1243dSDimitry Andric 
1399bdd1243dSDimitry Andric         if (auto *dthunk = dyn_cast<DefinedImportThunk>(d)) {
1400bdd1243dSDimitry Andric           if (!dthunk->wrappedSym->writtenToSymtab) {
1401bdd1243dSDimitry Andric             dthunk->wrappedSym->writtenToSymtab = true;
1402bdd1243dSDimitry Andric             if (std::optional<coff_symbol16> sym =
1403bdd1243dSDimitry Andric                     createSymbol(dthunk->wrappedSym))
1404bdd1243dSDimitry Andric               outputSymtab.push_back(*sym);
1405bdd1243dSDimitry Andric           }
1406bdd1243dSDimitry Andric         }
14070b57cec5SDimitry Andric       }
14080b57cec5SDimitry Andric     }
14090b57cec5SDimitry Andric   }
14100b57cec5SDimitry Andric 
14110b57cec5SDimitry Andric   if (outputSymtab.empty() && strtab.empty())
14120b57cec5SDimitry Andric     return;
14130b57cec5SDimitry Andric 
14140b57cec5SDimitry Andric   // We position the symbol table to be adjacent to the end of the last section.
14150b57cec5SDimitry Andric   uint64_t fileOff = fileSize;
14160b57cec5SDimitry Andric   pointerToSymbolTable = fileOff;
14170b57cec5SDimitry Andric   fileOff += outputSymtab.size() * sizeof(coff_symbol16);
14180b57cec5SDimitry Andric   fileOff += 4 + strtab.size();
1419bdd1243dSDimitry Andric   fileSize = alignTo(fileOff, ctx.config.fileAlign);
14200b57cec5SDimitry Andric }
14210b57cec5SDimitry Andric 
14220b57cec5SDimitry Andric void Writer::mergeSections() {
1423*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Merge sections");
14240b57cec5SDimitry Andric   if (!pdataSec->chunks.empty()) {
1425*5f757f3fSDimitry Andric     if (isArm64EC(ctx.config.machine)) {
1426*5f757f3fSDimitry Andric       // On ARM64EC .pdata may contain both ARM64 and X64 data. Split them by
1427*5f757f3fSDimitry Andric       // sorting and store their regions separately.
1428*5f757f3fSDimitry Andric       llvm::stable_sort(pdataSec->chunks, [=](const Chunk *a, const Chunk *b) {
1429*5f757f3fSDimitry Andric         return (a->getMachine() == AMD64) < (b->getMachine() == AMD64);
1430*5f757f3fSDimitry Andric       });
1431*5f757f3fSDimitry Andric 
1432*5f757f3fSDimitry Andric       for (auto chunk : pdataSec->chunks) {
1433*5f757f3fSDimitry Andric         if (chunk->getMachine() == AMD64) {
1434*5f757f3fSDimitry Andric           hybridPdata.first = chunk;
1435*5f757f3fSDimitry Andric           hybridPdata.last = pdataSec->chunks.back();
1436*5f757f3fSDimitry Andric           break;
1437*5f757f3fSDimitry Andric         }
1438*5f757f3fSDimitry Andric 
1439*5f757f3fSDimitry Andric         if (!pdata.first)
1440*5f757f3fSDimitry Andric           pdata.first = chunk;
1441*5f757f3fSDimitry Andric         pdata.last = chunk;
1442*5f757f3fSDimitry Andric       }
1443*5f757f3fSDimitry Andric     } else {
1444*5f757f3fSDimitry Andric       pdata.first = pdataSec->chunks.front();
1445*5f757f3fSDimitry Andric       pdata.last = pdataSec->chunks.back();
1446*5f757f3fSDimitry Andric     }
14470b57cec5SDimitry Andric   }
14480b57cec5SDimitry Andric 
1449bdd1243dSDimitry Andric   for (auto &p : ctx.config.merge) {
14500b57cec5SDimitry Andric     StringRef toName = p.second;
14510b57cec5SDimitry Andric     if (p.first == toName)
14520b57cec5SDimitry Andric       continue;
14530b57cec5SDimitry Andric     StringSet<> names;
145404eeddc0SDimitry Andric     while (true) {
14550b57cec5SDimitry Andric       if (!names.insert(toName).second)
14560b57cec5SDimitry Andric         fatal("/merge: cycle found for section '" + p.first + "'");
1457bdd1243dSDimitry Andric       auto i = ctx.config.merge.find(toName);
1458bdd1243dSDimitry Andric       if (i == ctx.config.merge.end())
14590b57cec5SDimitry Andric         break;
14600b57cec5SDimitry Andric       toName = i->second;
14610b57cec5SDimitry Andric     }
14620b57cec5SDimitry Andric     OutputSection *from = findSection(p.first);
14630b57cec5SDimitry Andric     OutputSection *to = findSection(toName);
14640b57cec5SDimitry Andric     if (!from)
14650b57cec5SDimitry Andric       continue;
14660b57cec5SDimitry Andric     if (!to) {
14670b57cec5SDimitry Andric       from->name = toName;
14680b57cec5SDimitry Andric       continue;
14690b57cec5SDimitry Andric     }
14700b57cec5SDimitry Andric     to->merge(from);
14710b57cec5SDimitry Andric   }
14720b57cec5SDimitry Andric }
14730b57cec5SDimitry Andric 
1474*5f757f3fSDimitry Andric // EC targets may have chunks of various architectures mixed together at this
1475*5f757f3fSDimitry Andric // point. Group code chunks of the same architecture together by sorting chunks
1476*5f757f3fSDimitry Andric // by their EC range type.
1477*5f757f3fSDimitry Andric void Writer::sortECChunks() {
1478*5f757f3fSDimitry Andric   if (!isArm64EC(ctx.config.machine))
1479*5f757f3fSDimitry Andric     return;
1480*5f757f3fSDimitry Andric 
1481*5f757f3fSDimitry Andric   for (OutputSection *sec : ctx.outputSections) {
1482*5f757f3fSDimitry Andric     if (sec->isCodeSection())
1483*5f757f3fSDimitry Andric       llvm::stable_sort(sec->chunks, [=](const Chunk *a, const Chunk *b) {
1484*5f757f3fSDimitry Andric         std::optional<chpe_range_type> aType = a->getArm64ECRangeType(),
1485*5f757f3fSDimitry Andric                                        bType = b->getArm64ECRangeType();
1486*5f757f3fSDimitry Andric         return bType && (!aType || *aType < *bType);
1487*5f757f3fSDimitry Andric       });
1488*5f757f3fSDimitry Andric   }
1489*5f757f3fSDimitry Andric }
1490*5f757f3fSDimitry Andric 
14910b57cec5SDimitry Andric // Visits all sections to assign incremental, non-overlapping RVAs and
14920b57cec5SDimitry Andric // file offsets.
14930b57cec5SDimitry Andric void Writer::assignAddresses() {
1494*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Assign addresses");
1495bdd1243dSDimitry Andric   Configuration *config = &ctx.config;
1496bdd1243dSDimitry Andric 
1497*5f757f3fSDimitry Andric   // We need to create EC code map so that ECCodeMapChunk knows its size.
1498*5f757f3fSDimitry Andric   // We do it here to make sure that we account for range extension chunks.
1499*5f757f3fSDimitry Andric   createECCodeMap();
1500*5f757f3fSDimitry Andric 
15010b57cec5SDimitry Andric   sizeOfHeaders = dosStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
15020b57cec5SDimitry Andric                   sizeof(data_directory) * numberOfDataDirectory +
1503349cc55cSDimitry Andric                   sizeof(coff_section) * ctx.outputSections.size();
15040b57cec5SDimitry Andric   sizeOfHeaders +=
15050b57cec5SDimitry Andric       config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
15060b57cec5SDimitry Andric   sizeOfHeaders = alignTo(sizeOfHeaders, config->fileAlign);
15070b57cec5SDimitry Andric   fileSize = sizeOfHeaders;
15080b57cec5SDimitry Andric 
15090b57cec5SDimitry Andric   // The first page is kept unmapped.
15100b57cec5SDimitry Andric   uint64_t rva = alignTo(sizeOfHeaders, config->align);
15110b57cec5SDimitry Andric 
1512349cc55cSDimitry Andric   for (OutputSection *sec : ctx.outputSections) {
1513*5f757f3fSDimitry Andric     llvm::TimeTraceScope timeScope("Section: ", sec->name);
15140b57cec5SDimitry Andric     if (sec == relocSec)
15150b57cec5SDimitry Andric       addBaserels();
15160b57cec5SDimitry Andric     uint64_t rawSize = 0, virtualSize = 0;
15170b57cec5SDimitry Andric     sec->header.VirtualAddress = rva;
15180b57cec5SDimitry Andric 
15190b57cec5SDimitry Andric     // If /FUNCTIONPADMIN is used, functions are padded in order to create a
15200b57cec5SDimitry Andric     // hotpatchable image.
1521*5f757f3fSDimitry Andric     uint32_t padding = sec->isCodeSection() ? config->functionPadMin : 0;
1522*5f757f3fSDimitry Andric     std::optional<chpe_range_type> prevECRange;
15230b57cec5SDimitry Andric 
15240b57cec5SDimitry Andric     for (Chunk *c : sec->chunks) {
1525*5f757f3fSDimitry Andric       // Alignment EC code range baudaries.
1526*5f757f3fSDimitry Andric       if (isArm64EC(ctx.config.machine) && sec->isCodeSection()) {
1527*5f757f3fSDimitry Andric         std::optional<chpe_range_type> rangeType = c->getArm64ECRangeType();
1528*5f757f3fSDimitry Andric         if (rangeType != prevECRange) {
1529*5f757f3fSDimitry Andric           virtualSize = alignTo(virtualSize, 4096);
1530*5f757f3fSDimitry Andric           prevECRange = rangeType;
1531*5f757f3fSDimitry Andric         }
1532*5f757f3fSDimitry Andric       }
15330b57cec5SDimitry Andric       if (padding && c->isHotPatchable())
15340b57cec5SDimitry Andric         virtualSize += padding;
15350b57cec5SDimitry Andric       virtualSize = alignTo(virtualSize, c->getAlignment());
15360b57cec5SDimitry Andric       c->setRVA(rva + virtualSize);
15370b57cec5SDimitry Andric       virtualSize += c->getSize();
15380b57cec5SDimitry Andric       if (c->hasData)
15390b57cec5SDimitry Andric         rawSize = alignTo(virtualSize, config->fileAlign);
15400b57cec5SDimitry Andric     }
15410b57cec5SDimitry Andric     if (virtualSize > UINT32_MAX)
15420b57cec5SDimitry Andric       error("section larger than 4 GiB: " + sec->name);
15430b57cec5SDimitry Andric     sec->header.VirtualSize = virtualSize;
15440b57cec5SDimitry Andric     sec->header.SizeOfRawData = rawSize;
15450b57cec5SDimitry Andric     if (rawSize != 0)
15460b57cec5SDimitry Andric       sec->header.PointerToRawData = fileSize;
15470b57cec5SDimitry Andric     rva += alignTo(virtualSize, config->align);
15480b57cec5SDimitry Andric     fileSize += alignTo(rawSize, config->fileAlign);
15490b57cec5SDimitry Andric   }
15500b57cec5SDimitry Andric   sizeOfImage = alignTo(rva, config->align);
15510b57cec5SDimitry Andric 
15520b57cec5SDimitry Andric   // Assign addresses to sections in MergeChunks.
1553349cc55cSDimitry Andric   for (MergeChunk *mc : ctx.mergeChunkInstances)
15540b57cec5SDimitry Andric     if (mc)
15550b57cec5SDimitry Andric       mc->assignSubsectionRVAs();
15560b57cec5SDimitry Andric }
15570b57cec5SDimitry Andric 
15580b57cec5SDimitry Andric template <typename PEHeaderTy> void Writer::writeHeader() {
15590b57cec5SDimitry Andric   // Write DOS header. For backwards compatibility, the first part of a PE/COFF
15600b57cec5SDimitry Andric   // executable consists of an MS-DOS MZ executable. If the executable is run
15610b57cec5SDimitry Andric   // under DOS, that program gets run (usually to just print an error message).
15620b57cec5SDimitry Andric   // When run under Windows, the loader looks at AddressOfNewExeHeader and uses
15630b57cec5SDimitry Andric   // the PE header instead.
1564bdd1243dSDimitry Andric   Configuration *config = &ctx.config;
15650b57cec5SDimitry Andric   uint8_t *buf = buffer->getBufferStart();
15660b57cec5SDimitry Andric   auto *dos = reinterpret_cast<dos_header *>(buf);
15670b57cec5SDimitry Andric   buf += sizeof(dos_header);
15680b57cec5SDimitry Andric   dos->Magic[0] = 'M';
15690b57cec5SDimitry Andric   dos->Magic[1] = 'Z';
15700b57cec5SDimitry Andric   dos->UsedBytesInTheLastPage = dosStubSize % 512;
15710b57cec5SDimitry Andric   dos->FileSizeInPages = divideCeil(dosStubSize, 512);
15720b57cec5SDimitry Andric   dos->HeaderSizeInParagraphs = sizeof(dos_header) / 16;
15730b57cec5SDimitry Andric 
15740b57cec5SDimitry Andric   dos->AddressOfRelocationTable = sizeof(dos_header);
15750b57cec5SDimitry Andric   dos->AddressOfNewExeHeader = dosStubSize;
15760b57cec5SDimitry Andric 
15770b57cec5SDimitry Andric   // Write DOS program.
15780b57cec5SDimitry Andric   memcpy(buf, dosProgram, sizeof(dosProgram));
15790b57cec5SDimitry Andric   buf += sizeof(dosProgram);
15800b57cec5SDimitry Andric 
15810b57cec5SDimitry Andric   // Write PE magic
15820b57cec5SDimitry Andric   memcpy(buf, PEMagic, sizeof(PEMagic));
15830b57cec5SDimitry Andric   buf += sizeof(PEMagic);
15840b57cec5SDimitry Andric 
15850b57cec5SDimitry Andric   // Write COFF header
15860b57cec5SDimitry Andric   auto *coff = reinterpret_cast<coff_file_header *>(buf);
15870b57cec5SDimitry Andric   buf += sizeof(*coff);
158806c3fb27SDimitry Andric   switch (config->machine) {
158906c3fb27SDimitry Andric   case ARM64EC:
159006c3fb27SDimitry Andric     coff->Machine = AMD64;
159106c3fb27SDimitry Andric     break;
159206c3fb27SDimitry Andric   case ARM64X:
159306c3fb27SDimitry Andric     coff->Machine = ARM64;
159406c3fb27SDimitry Andric     break;
159506c3fb27SDimitry Andric   default:
15960b57cec5SDimitry Andric     coff->Machine = config->machine;
159706c3fb27SDimitry Andric   }
1598349cc55cSDimitry Andric   coff->NumberOfSections = ctx.outputSections.size();
15990b57cec5SDimitry Andric   coff->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
16000b57cec5SDimitry Andric   if (config->largeAddressAware)
16010b57cec5SDimitry Andric     coff->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
16020b57cec5SDimitry Andric   if (!config->is64())
16030b57cec5SDimitry Andric     coff->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
16040b57cec5SDimitry Andric   if (config->dll)
16050b57cec5SDimitry Andric     coff->Characteristics |= IMAGE_FILE_DLL;
1606480093f4SDimitry Andric   if (config->driverUponly)
1607480093f4SDimitry Andric     coff->Characteristics |= IMAGE_FILE_UP_SYSTEM_ONLY;
16080b57cec5SDimitry Andric   if (!config->relocatable)
16090b57cec5SDimitry Andric     coff->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
16100b57cec5SDimitry Andric   if (config->swaprunCD)
16110b57cec5SDimitry Andric     coff->Characteristics |= IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP;
16120b57cec5SDimitry Andric   if (config->swaprunNet)
16130b57cec5SDimitry Andric     coff->Characteristics |= IMAGE_FILE_NET_RUN_FROM_SWAP;
16140b57cec5SDimitry Andric   coff->SizeOfOptionalHeader =
16150b57cec5SDimitry Andric       sizeof(PEHeaderTy) + sizeof(data_directory) * numberOfDataDirectory;
16160b57cec5SDimitry Andric 
16170b57cec5SDimitry Andric   // Write PE header
16180b57cec5SDimitry Andric   auto *pe = reinterpret_cast<PEHeaderTy *>(buf);
16190b57cec5SDimitry Andric   buf += sizeof(*pe);
16200b57cec5SDimitry Andric   pe->Magic = config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
16210b57cec5SDimitry Andric 
16220b57cec5SDimitry Andric   // If {Major,Minor}LinkerVersion is left at 0.0, then for some
16230b57cec5SDimitry Andric   // reason signing the resulting PE file with Authenticode produces a
16240b57cec5SDimitry Andric   // signature that fails to validate on Windows 7 (but is OK on 10).
16250b57cec5SDimitry Andric   // Set it to 14.0, which is what VS2015 outputs, and which avoids
16260b57cec5SDimitry Andric   // that problem.
16270b57cec5SDimitry Andric   pe->MajorLinkerVersion = 14;
16280b57cec5SDimitry Andric   pe->MinorLinkerVersion = 0;
16290b57cec5SDimitry Andric 
16300b57cec5SDimitry Andric   pe->ImageBase = config->imageBase;
16310b57cec5SDimitry Andric   pe->SectionAlignment = config->align;
16320b57cec5SDimitry Andric   pe->FileAlignment = config->fileAlign;
16330b57cec5SDimitry Andric   pe->MajorImageVersion = config->majorImageVersion;
16340b57cec5SDimitry Andric   pe->MinorImageVersion = config->minorImageVersion;
16350b57cec5SDimitry Andric   pe->MajorOperatingSystemVersion = config->majorOSVersion;
16360b57cec5SDimitry Andric   pe->MinorOperatingSystemVersion = config->minorOSVersion;
1637e8d8bef9SDimitry Andric   pe->MajorSubsystemVersion = config->majorSubsystemVersion;
1638e8d8bef9SDimitry Andric   pe->MinorSubsystemVersion = config->minorSubsystemVersion;
16390b57cec5SDimitry Andric   pe->Subsystem = config->subsystem;
16400b57cec5SDimitry Andric   pe->SizeOfImage = sizeOfImage;
16410b57cec5SDimitry Andric   pe->SizeOfHeaders = sizeOfHeaders;
16420b57cec5SDimitry Andric   if (!config->noEntry) {
16430b57cec5SDimitry Andric     Defined *entry = cast<Defined>(config->entry);
16440b57cec5SDimitry Andric     pe->AddressOfEntryPoint = entry->getRVA();
16450b57cec5SDimitry Andric     // Pointer to thumb code must have the LSB set, so adjust it.
16460b57cec5SDimitry Andric     if (config->machine == ARMNT)
16470b57cec5SDimitry Andric       pe->AddressOfEntryPoint |= 1;
16480b57cec5SDimitry Andric   }
16490b57cec5SDimitry Andric   pe->SizeOfStackReserve = config->stackReserve;
16500b57cec5SDimitry Andric   pe->SizeOfStackCommit = config->stackCommit;
16510b57cec5SDimitry Andric   pe->SizeOfHeapReserve = config->heapReserve;
16520b57cec5SDimitry Andric   pe->SizeOfHeapCommit = config->heapCommit;
16530b57cec5SDimitry Andric   if (config->appContainer)
16540b57cec5SDimitry Andric     pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER;
1655480093f4SDimitry Andric   if (config->driverWdm)
1656480093f4SDimitry Andric     pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER;
16570b57cec5SDimitry Andric   if (config->dynamicBase)
16580b57cec5SDimitry Andric     pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
16590b57cec5SDimitry Andric   if (config->highEntropyVA)
16600b57cec5SDimitry Andric     pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
16610b57cec5SDimitry Andric   if (!config->allowBind)
16620b57cec5SDimitry Andric     pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND;
16630b57cec5SDimitry Andric   if (config->nxCompat)
16640b57cec5SDimitry Andric     pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
16650b57cec5SDimitry Andric   if (!config->allowIsolation)
16660b57cec5SDimitry Andric     pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION;
16670b57cec5SDimitry Andric   if (config->guardCF != GuardCFLevel::Off)
16680b57cec5SDimitry Andric     pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF;
16690b57cec5SDimitry Andric   if (config->integrityCheck)
16700b57cec5SDimitry Andric     pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
1671979e22ffSDimitry Andric   if (setNoSEHCharacteristic || config->noSEH)
16720b57cec5SDimitry Andric     pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH;
16730b57cec5SDimitry Andric   if (config->terminalServerAware)
16740b57cec5SDimitry Andric     pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
16750b57cec5SDimitry Andric   pe->NumberOfRvaAndSize = numberOfDataDirectory;
16760b57cec5SDimitry Andric   if (textSec->getVirtualSize()) {
16770b57cec5SDimitry Andric     pe->BaseOfCode = textSec->getRVA();
16780b57cec5SDimitry Andric     pe->SizeOfCode = textSec->getRawSize();
16790b57cec5SDimitry Andric   }
16800b57cec5SDimitry Andric   pe->SizeOfInitializedData = getSizeOfInitializedData();
16810b57cec5SDimitry Andric 
16820b57cec5SDimitry Andric   // Write data directory
16830b57cec5SDimitry Andric   auto *dir = reinterpret_cast<data_directory *>(buf);
16840b57cec5SDimitry Andric   buf += sizeof(*dir) * numberOfDataDirectory;
168585868e8aSDimitry Andric   if (edataStart) {
168685868e8aSDimitry Andric     dir[EXPORT_TABLE].RelativeVirtualAddress = edataStart->getRVA();
168785868e8aSDimitry Andric     dir[EXPORT_TABLE].Size =
168885868e8aSDimitry Andric         edataEnd->getRVA() + edataEnd->getSize() - edataStart->getRVA();
16890b57cec5SDimitry Andric   }
16900b57cec5SDimitry Andric   if (importTableStart) {
16910b57cec5SDimitry Andric     dir[IMPORT_TABLE].RelativeVirtualAddress = importTableStart->getRVA();
16920b57cec5SDimitry Andric     dir[IMPORT_TABLE].Size = importTableSize;
16930b57cec5SDimitry Andric   }
16940b57cec5SDimitry Andric   if (iatStart) {
16950b57cec5SDimitry Andric     dir[IAT].RelativeVirtualAddress = iatStart->getRVA();
16960b57cec5SDimitry Andric     dir[IAT].Size = iatSize;
16970b57cec5SDimitry Andric   }
16980b57cec5SDimitry Andric   if (rsrcSec->getVirtualSize()) {
16990b57cec5SDimitry Andric     dir[RESOURCE_TABLE].RelativeVirtualAddress = rsrcSec->getRVA();
17000b57cec5SDimitry Andric     dir[RESOURCE_TABLE].Size = rsrcSec->getVirtualSize();
17010b57cec5SDimitry Andric   }
1702*5f757f3fSDimitry Andric   // ARM64EC (but not ARM64X) contains x86_64 exception table in data directory.
1703*5f757f3fSDimitry Andric   ChunkRange &exceptionTable =
1704*5f757f3fSDimitry Andric       ctx.config.machine == ARM64EC ? hybridPdata : pdata;
1705*5f757f3fSDimitry Andric   if (exceptionTable.first) {
1706*5f757f3fSDimitry Andric     dir[EXCEPTION_TABLE].RelativeVirtualAddress =
1707*5f757f3fSDimitry Andric         exceptionTable.first->getRVA();
1708*5f757f3fSDimitry Andric     dir[EXCEPTION_TABLE].Size = exceptionTable.last->getRVA() +
1709*5f757f3fSDimitry Andric                                 exceptionTable.last->getSize() -
1710*5f757f3fSDimitry Andric                                 exceptionTable.first->getRVA();
17110b57cec5SDimitry Andric   }
17120b57cec5SDimitry Andric   if (relocSec->getVirtualSize()) {
17130b57cec5SDimitry Andric     dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = relocSec->getRVA();
17140b57cec5SDimitry Andric     dir[BASE_RELOCATION_TABLE].Size = relocSec->getVirtualSize();
17150b57cec5SDimitry Andric   }
1716349cc55cSDimitry Andric   if (Symbol *sym = ctx.symtab.findUnderscore("_tls_used")) {
17170b57cec5SDimitry Andric     if (Defined *b = dyn_cast<Defined>(sym)) {
17180b57cec5SDimitry Andric       dir[TLS_TABLE].RelativeVirtualAddress = b->getRVA();
17190b57cec5SDimitry Andric       dir[TLS_TABLE].Size = config->is64()
17200b57cec5SDimitry Andric                                 ? sizeof(object::coff_tls_directory64)
17210b57cec5SDimitry Andric                                 : sizeof(object::coff_tls_directory32);
17220b57cec5SDimitry Andric     }
17230b57cec5SDimitry Andric   }
17240b57cec5SDimitry Andric   if (debugDirectory) {
17250b57cec5SDimitry Andric     dir[DEBUG_DIRECTORY].RelativeVirtualAddress = debugDirectory->getRVA();
17260b57cec5SDimitry Andric     dir[DEBUG_DIRECTORY].Size = debugDirectory->getSize();
17270b57cec5SDimitry Andric   }
1728349cc55cSDimitry Andric   if (Symbol *sym = ctx.symtab.findUnderscore("_load_config_used")) {
17290b57cec5SDimitry Andric     if (auto *b = dyn_cast<DefinedRegular>(sym)) {
17300b57cec5SDimitry Andric       SectionChunk *sc = b->getChunk();
17310b57cec5SDimitry Andric       assert(b->getRVA() >= sc->getRVA());
17320b57cec5SDimitry Andric       uint64_t offsetInChunk = b->getRVA() - sc->getRVA();
17330b57cec5SDimitry Andric       if (!sc->hasData || offsetInChunk + 4 > sc->getSize())
17340b57cec5SDimitry Andric         fatal("_load_config_used is malformed");
17350b57cec5SDimitry Andric 
17360b57cec5SDimitry Andric       ArrayRef<uint8_t> secContents = sc->getContents();
17370b57cec5SDimitry Andric       uint32_t loadConfigSize =
17380b57cec5SDimitry Andric           *reinterpret_cast<const ulittle32_t *>(&secContents[offsetInChunk]);
17390b57cec5SDimitry Andric       if (offsetInChunk + loadConfigSize > sc->getSize())
17400b57cec5SDimitry Andric         fatal("_load_config_used is too large");
17410b57cec5SDimitry Andric       dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = b->getRVA();
17420b57cec5SDimitry Andric       dir[LOAD_CONFIG_TABLE].Size = loadConfigSize;
17430b57cec5SDimitry Andric     }
17440b57cec5SDimitry Andric   }
17450b57cec5SDimitry Andric   if (!delayIdata.empty()) {
17460b57cec5SDimitry Andric     dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
17470b57cec5SDimitry Andric         delayIdata.getDirRVA();
17480b57cec5SDimitry Andric     dir[DELAY_IMPORT_DESCRIPTOR].Size = delayIdata.getDirSize();
17490b57cec5SDimitry Andric   }
17500b57cec5SDimitry Andric 
17510b57cec5SDimitry Andric   // Write section table
1752349cc55cSDimitry Andric   for (OutputSection *sec : ctx.outputSections) {
1753bdd1243dSDimitry Andric     sec->writeHeaderTo(buf, config->debug);
17540b57cec5SDimitry Andric     buf += sizeof(coff_section);
17550b57cec5SDimitry Andric   }
17560b57cec5SDimitry Andric   sectionTable = ArrayRef<uint8_t>(
1757349cc55cSDimitry Andric       buf - ctx.outputSections.size() * sizeof(coff_section), buf);
17580b57cec5SDimitry Andric 
17590b57cec5SDimitry Andric   if (outputSymtab.empty() && strtab.empty())
17600b57cec5SDimitry Andric     return;
17610b57cec5SDimitry Andric 
17620b57cec5SDimitry Andric   coff->PointerToSymbolTable = pointerToSymbolTable;
17630b57cec5SDimitry Andric   uint32_t numberOfSymbols = outputSymtab.size();
17640b57cec5SDimitry Andric   coff->NumberOfSymbols = numberOfSymbols;
17650b57cec5SDimitry Andric   auto *symbolTable = reinterpret_cast<coff_symbol16 *>(
17660b57cec5SDimitry Andric       buffer->getBufferStart() + coff->PointerToSymbolTable);
17670b57cec5SDimitry Andric   for (size_t i = 0; i != numberOfSymbols; ++i)
17680b57cec5SDimitry Andric     symbolTable[i] = outputSymtab[i];
17690b57cec5SDimitry Andric   // Create the string table, it follows immediately after the symbol table.
17700b57cec5SDimitry Andric   // The first 4 bytes is length including itself.
17710b57cec5SDimitry Andric   buf = reinterpret_cast<uint8_t *>(&symbolTable[numberOfSymbols]);
17720b57cec5SDimitry Andric   write32le(buf, strtab.size() + 4);
17730b57cec5SDimitry Andric   if (!strtab.empty())
17740b57cec5SDimitry Andric     memcpy(buf + 4, strtab.data(), strtab.size());
17750b57cec5SDimitry Andric }
17760b57cec5SDimitry Andric 
17770b57cec5SDimitry Andric void Writer::openFile(StringRef path) {
17780b57cec5SDimitry Andric   buffer = CHECK(
17790b57cec5SDimitry Andric       FileOutputBuffer::create(path, fileSize, FileOutputBuffer::F_executable),
17800b57cec5SDimitry Andric       "failed to open " + path);
17810b57cec5SDimitry Andric }
17820b57cec5SDimitry Andric 
17830b57cec5SDimitry Andric void Writer::createSEHTable() {
17840b57cec5SDimitry Andric   SymbolRVASet handlers;
1785349cc55cSDimitry Andric   for (ObjFile *file : ctx.objFileInstances) {
17860b57cec5SDimitry Andric     if (!file->hasSafeSEH())
17870b57cec5SDimitry Andric       error("/safeseh: " + file->getName() + " is not compatible with SEH");
17880b57cec5SDimitry Andric     markSymbolsForRVATable(file, file->getSXDataChunks(), handlers);
17890b57cec5SDimitry Andric   }
17900b57cec5SDimitry Andric 
17910b57cec5SDimitry Andric   // Set the "no SEH" characteristic if there really were no handlers, or if
17920b57cec5SDimitry Andric   // there is no load config object to point to the table of handlers.
17930b57cec5SDimitry Andric   setNoSEHCharacteristic =
1794349cc55cSDimitry Andric       handlers.empty() || !ctx.symtab.findUnderscore("_load_config_used");
17950b57cec5SDimitry Andric 
17960b57cec5SDimitry Andric   maybeAddRVATable(std::move(handlers), "__safe_se_handler_table",
17970b57cec5SDimitry Andric                    "__safe_se_handler_count");
17980b57cec5SDimitry Andric }
17990b57cec5SDimitry Andric 
18000b57cec5SDimitry Andric // Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set
18010b57cec5SDimitry Andric // cannot contain duplicates. Therefore, the set is uniqued by Chunk and the
18020b57cec5SDimitry Andric // symbol's offset into that Chunk.
18030b57cec5SDimitry Andric static void addSymbolToRVASet(SymbolRVASet &rvaSet, Defined *s) {
18040b57cec5SDimitry Andric   Chunk *c = s->getChunk();
18050b57cec5SDimitry Andric   if (auto *sc = dyn_cast<SectionChunk>(c))
18060b57cec5SDimitry Andric     c = sc->repl; // Look through ICF replacement.
18070b57cec5SDimitry Andric   uint32_t off = s->getRVA() - (c ? c->getRVA() : 0);
18080b57cec5SDimitry Andric   rvaSet.insert({c, off});
18090b57cec5SDimitry Andric }
18100b57cec5SDimitry Andric 
18110b57cec5SDimitry Andric // Given a symbol, add it to the GFIDs table if it is a live, defined, function
18120b57cec5SDimitry Andric // symbol in an executable section.
18130b57cec5SDimitry Andric static void maybeAddAddressTakenFunction(SymbolRVASet &addressTakenSyms,
18140b57cec5SDimitry Andric                                          Symbol *s) {
18150b57cec5SDimitry Andric   if (!s)
18160b57cec5SDimitry Andric     return;
18170b57cec5SDimitry Andric 
18180b57cec5SDimitry Andric   switch (s->kind()) {
18190b57cec5SDimitry Andric   case Symbol::DefinedLocalImportKind:
18200b57cec5SDimitry Andric   case Symbol::DefinedImportDataKind:
18210b57cec5SDimitry Andric     // Defines an __imp_ pointer, so it is data, so it is ignored.
18220b57cec5SDimitry Andric     break;
18230b57cec5SDimitry Andric   case Symbol::DefinedCommonKind:
18240b57cec5SDimitry Andric     // Common is always data, so it is ignored.
18250b57cec5SDimitry Andric     break;
18260b57cec5SDimitry Andric   case Symbol::DefinedAbsoluteKind:
18270b57cec5SDimitry Andric   case Symbol::DefinedSyntheticKind:
18280b57cec5SDimitry Andric     // Absolute is never code, synthetic generally isn't and usually isn't
18290b57cec5SDimitry Andric     // determinable.
18300b57cec5SDimitry Andric     break;
183185868e8aSDimitry Andric   case Symbol::LazyArchiveKind:
183285868e8aSDimitry Andric   case Symbol::LazyObjectKind:
1833fe6060f1SDimitry Andric   case Symbol::LazyDLLSymbolKind:
18340b57cec5SDimitry Andric   case Symbol::UndefinedKind:
18350b57cec5SDimitry Andric     // Undefined symbols resolve to zero, so they don't have an RVA. Lazy
18360b57cec5SDimitry Andric     // symbols shouldn't have relocations.
18370b57cec5SDimitry Andric     break;
18380b57cec5SDimitry Andric 
18390b57cec5SDimitry Andric   case Symbol::DefinedImportThunkKind:
18400b57cec5SDimitry Andric     // Thunks are always code, include them.
18410b57cec5SDimitry Andric     addSymbolToRVASet(addressTakenSyms, cast<Defined>(s));
18420b57cec5SDimitry Andric     break;
18430b57cec5SDimitry Andric 
18440b57cec5SDimitry Andric   case Symbol::DefinedRegularKind: {
18450b57cec5SDimitry Andric     // This is a regular, defined, symbol from a COFF file. Mark the symbol as
18460b57cec5SDimitry Andric     // address taken if the symbol type is function and it's in an executable
18470b57cec5SDimitry Andric     // section.
18480b57cec5SDimitry Andric     auto *d = cast<DefinedRegular>(s);
18490b57cec5SDimitry Andric     if (d->getCOFFSymbol().getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
18500b57cec5SDimitry Andric       SectionChunk *sc = dyn_cast<SectionChunk>(d->getChunk());
18510b57cec5SDimitry Andric       if (sc && sc->live &&
18520b57cec5SDimitry Andric           sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE)
18530b57cec5SDimitry Andric         addSymbolToRVASet(addressTakenSyms, d);
18540b57cec5SDimitry Andric     }
18550b57cec5SDimitry Andric     break;
18560b57cec5SDimitry Andric   }
18570b57cec5SDimitry Andric   }
18580b57cec5SDimitry Andric }
18590b57cec5SDimitry Andric 
18600b57cec5SDimitry Andric // Visit all relocations from all section contributions of this object file and
18610b57cec5SDimitry Andric // mark the relocation target as address-taken.
1862bdd1243dSDimitry Andric void Writer::markSymbolsWithRelocations(ObjFile *file,
18630b57cec5SDimitry Andric                                         SymbolRVASet &usedSymbols) {
18640b57cec5SDimitry Andric   for (Chunk *c : file->getChunks()) {
18650b57cec5SDimitry Andric     // We only care about live section chunks. Common chunks and other chunks
18660b57cec5SDimitry Andric     // don't generally contain relocations.
18670b57cec5SDimitry Andric     SectionChunk *sc = dyn_cast<SectionChunk>(c);
18680b57cec5SDimitry Andric     if (!sc || !sc->live)
18690b57cec5SDimitry Andric       continue;
18700b57cec5SDimitry Andric 
18710b57cec5SDimitry Andric     for (const coff_relocation &reloc : sc->getRelocs()) {
1872bdd1243dSDimitry Andric       if (ctx.config.machine == I386 &&
1873bdd1243dSDimitry Andric           reloc.Type == COFF::IMAGE_REL_I386_REL32)
18740b57cec5SDimitry Andric         // Ignore relative relocations on x86. On x86_64 they can't be ignored
18750b57cec5SDimitry Andric         // since they're also used to compute absolute addresses.
18760b57cec5SDimitry Andric         continue;
18770b57cec5SDimitry Andric 
18780b57cec5SDimitry Andric       Symbol *ref = sc->file->getSymbol(reloc.SymbolTableIndex);
18790b57cec5SDimitry Andric       maybeAddAddressTakenFunction(usedSymbols, ref);
18800b57cec5SDimitry Andric     }
18810b57cec5SDimitry Andric   }
18820b57cec5SDimitry Andric }
18830b57cec5SDimitry Andric 
18840b57cec5SDimitry Andric // Create the guard function id table. This is a table of RVAs of all
18850b57cec5SDimitry Andric // address-taken functions. It is sorted and uniqued, just like the safe SEH
18860b57cec5SDimitry Andric // table.
18870b57cec5SDimitry Andric void Writer::createGuardCFTables() {
1888bdd1243dSDimitry Andric   Configuration *config = &ctx.config;
1889bdd1243dSDimitry Andric 
18900b57cec5SDimitry Andric   SymbolRVASet addressTakenSyms;
1891e8d8bef9SDimitry Andric   SymbolRVASet giatsRVASet;
1892e8d8bef9SDimitry Andric   std::vector<Symbol *> giatsSymbols;
18930b57cec5SDimitry Andric   SymbolRVASet longJmpTargets;
1894fe6060f1SDimitry Andric   SymbolRVASet ehContTargets;
1895349cc55cSDimitry Andric   for (ObjFile *file : ctx.objFileInstances) {
18960b57cec5SDimitry Andric     // If the object was compiled with /guard:cf, the address taken symbols
189706c3fb27SDimitry Andric     // are in .gfids$y sections, and the longjmp targets are in .gljmp$y
189806c3fb27SDimitry Andric     // sections. If the object was not compiled with /guard:cf, we assume there
189906c3fb27SDimitry Andric     // were no setjmp targets, and that all code symbols with relocations are
190006c3fb27SDimitry Andric     // possibly address-taken.
19010b57cec5SDimitry Andric     if (file->hasGuardCF()) {
19020b57cec5SDimitry Andric       markSymbolsForRVATable(file, file->getGuardFidChunks(), addressTakenSyms);
1903e8d8bef9SDimitry Andric       markSymbolsForRVATable(file, file->getGuardIATChunks(), giatsRVASet);
1904e8d8bef9SDimitry Andric       getSymbolsFromSections(file, file->getGuardIATChunks(), giatsSymbols);
19050b57cec5SDimitry Andric       markSymbolsForRVATable(file, file->getGuardLJmpChunks(), longJmpTargets);
19060b57cec5SDimitry Andric     } else {
19070b57cec5SDimitry Andric       markSymbolsWithRelocations(file, addressTakenSyms);
19080b57cec5SDimitry Andric     }
190906c3fb27SDimitry Andric     // If the object was compiled with /guard:ehcont, the ehcont targets are in
191006c3fb27SDimitry Andric     // .gehcont$y sections.
191106c3fb27SDimitry Andric     if (file->hasGuardEHCont())
191206c3fb27SDimitry Andric       markSymbolsForRVATable(file, file->getGuardEHContChunks(), ehContTargets);
19130b57cec5SDimitry Andric   }
19140b57cec5SDimitry Andric 
19150b57cec5SDimitry Andric   // Mark the image entry as address-taken.
19160b57cec5SDimitry Andric   if (config->entry)
19170b57cec5SDimitry Andric     maybeAddAddressTakenFunction(addressTakenSyms, config->entry);
19180b57cec5SDimitry Andric 
19190b57cec5SDimitry Andric   // Mark exported symbols in executable sections as address-taken.
19200b57cec5SDimitry Andric   for (Export &e : config->exports)
19210b57cec5SDimitry Andric     maybeAddAddressTakenFunction(addressTakenSyms, e.sym);
19220b57cec5SDimitry Andric 
1923e8d8bef9SDimitry Andric   // For each entry in the .giats table, check if it has a corresponding load
1924e8d8bef9SDimitry Andric   // thunk (e.g. because the DLL that defines it will be delay-loaded) and, if
1925e8d8bef9SDimitry Andric   // so, add the load thunk to the address taken (.gfids) table.
1926e8d8bef9SDimitry Andric   for (Symbol *s : giatsSymbols) {
1927e8d8bef9SDimitry Andric     if (auto *di = dyn_cast<DefinedImportData>(s)) {
1928e8d8bef9SDimitry Andric       if (di->loadThunkSym)
1929e8d8bef9SDimitry Andric         addSymbolToRVASet(addressTakenSyms, di->loadThunkSym);
1930e8d8bef9SDimitry Andric     }
1931e8d8bef9SDimitry Andric   }
1932e8d8bef9SDimitry Andric 
19330b57cec5SDimitry Andric   // Ensure sections referenced in the gfid table are 16-byte aligned.
19340b57cec5SDimitry Andric   for (const ChunkAndOffset &c : addressTakenSyms)
19350b57cec5SDimitry Andric     if (c.inputChunk->getAlignment() < 16)
19360b57cec5SDimitry Andric       c.inputChunk->setAlignment(16);
19370b57cec5SDimitry Andric 
19380b57cec5SDimitry Andric   maybeAddRVATable(std::move(addressTakenSyms), "__guard_fids_table",
19390b57cec5SDimitry Andric                    "__guard_fids_count");
19400b57cec5SDimitry Andric 
1941e8d8bef9SDimitry Andric   // Add the Guard Address Taken IAT Entry Table (.giats).
1942e8d8bef9SDimitry Andric   maybeAddRVATable(std::move(giatsRVASet), "__guard_iat_table",
1943e8d8bef9SDimitry Andric                    "__guard_iat_count");
1944e8d8bef9SDimitry Andric 
19450b57cec5SDimitry Andric   // Add the longjmp target table unless the user told us not to.
1946fe6060f1SDimitry Andric   if (config->guardCF & GuardCFLevel::LongJmp)
19470b57cec5SDimitry Andric     maybeAddRVATable(std::move(longJmpTargets), "__guard_longjmp_table",
19480b57cec5SDimitry Andric                      "__guard_longjmp_count");
19490b57cec5SDimitry Andric 
1950fe6060f1SDimitry Andric   // Add the ehcont target table unless the user told us not to.
1951fe6060f1SDimitry Andric   if (config->guardCF & GuardCFLevel::EHCont)
1952fe6060f1SDimitry Andric     maybeAddRVATable(std::move(ehContTargets), "__guard_eh_cont_table",
19534542f901SDimitry Andric                      "__guard_eh_cont_count");
1954fe6060f1SDimitry Andric 
19550b57cec5SDimitry Andric   // Set __guard_flags, which will be used in the load config to indicate that
19560b57cec5SDimitry Andric   // /guard:cf was enabled.
1957bdd1243dSDimitry Andric   uint32_t guardFlags = uint32_t(GuardFlags::CF_INSTRUMENTED) |
1958bdd1243dSDimitry Andric                         uint32_t(GuardFlags::CF_FUNCTION_TABLE_PRESENT);
1959fe6060f1SDimitry Andric   if (config->guardCF & GuardCFLevel::LongJmp)
1960bdd1243dSDimitry Andric     guardFlags |= uint32_t(GuardFlags::CF_LONGJUMP_TABLE_PRESENT);
1961fe6060f1SDimitry Andric   if (config->guardCF & GuardCFLevel::EHCont)
1962bdd1243dSDimitry Andric     guardFlags |= uint32_t(GuardFlags::EH_CONTINUATION_TABLE_PRESENT);
1963349cc55cSDimitry Andric   Symbol *flagSym = ctx.symtab.findUnderscore("__guard_flags");
19640b57cec5SDimitry Andric   cast<DefinedAbsolute>(flagSym)->setVA(guardFlags);
19650b57cec5SDimitry Andric }
19660b57cec5SDimitry Andric 
19670b57cec5SDimitry Andric // Take a list of input sections containing symbol table indices and add those
1968e8d8bef9SDimitry Andric // symbols to a vector. The challenge is that symbol RVAs are not known and
19690b57cec5SDimitry Andric // depend on the table size, so we can't directly build a set of integers.
1970e8d8bef9SDimitry Andric void Writer::getSymbolsFromSections(ObjFile *file,
19710b57cec5SDimitry Andric                                     ArrayRef<SectionChunk *> symIdxChunks,
1972e8d8bef9SDimitry Andric                                     std::vector<Symbol *> &symbols) {
19730b57cec5SDimitry Andric   for (SectionChunk *c : symIdxChunks) {
19740b57cec5SDimitry Andric     // Skip sections discarded by linker GC. This comes up when a .gfids section
19750b57cec5SDimitry Andric     // is associated with something like a vtable and the vtable is discarded.
19760b57cec5SDimitry Andric     // In this case, the associated gfids section is discarded, and we don't
19770b57cec5SDimitry Andric     // mark the virtual member functions as address-taken by the vtable.
19780b57cec5SDimitry Andric     if (!c->live)
19790b57cec5SDimitry Andric       continue;
19800b57cec5SDimitry Andric 
19810b57cec5SDimitry Andric     // Validate that the contents look like symbol table indices.
19820b57cec5SDimitry Andric     ArrayRef<uint8_t> data = c->getContents();
19830b57cec5SDimitry Andric     if (data.size() % 4 != 0) {
19840b57cec5SDimitry Andric       warn("ignoring " + c->getSectionName() +
19850b57cec5SDimitry Andric            " symbol table index section in object " + toString(file));
19860b57cec5SDimitry Andric       continue;
19870b57cec5SDimitry Andric     }
19880b57cec5SDimitry Andric 
19890b57cec5SDimitry Andric     // Read each symbol table index and check if that symbol was included in the
1990e8d8bef9SDimitry Andric     // final link. If so, add it to the vector of symbols.
19910b57cec5SDimitry Andric     ArrayRef<ulittle32_t> symIndices(
19920b57cec5SDimitry Andric         reinterpret_cast<const ulittle32_t *>(data.data()), data.size() / 4);
19930b57cec5SDimitry Andric     ArrayRef<Symbol *> objSymbols = file->getSymbols();
19940b57cec5SDimitry Andric     for (uint32_t symIndex : symIndices) {
19950b57cec5SDimitry Andric       if (symIndex >= objSymbols.size()) {
19960b57cec5SDimitry Andric         warn("ignoring invalid symbol table index in section " +
19970b57cec5SDimitry Andric              c->getSectionName() + " in object " + toString(file));
19980b57cec5SDimitry Andric         continue;
19990b57cec5SDimitry Andric       }
20000b57cec5SDimitry Andric       if (Symbol *s = objSymbols[symIndex]) {
20010b57cec5SDimitry Andric         if (s->isLive())
2002e8d8bef9SDimitry Andric           symbols.push_back(cast<Symbol>(s));
2003e8d8bef9SDimitry Andric       }
2004e8d8bef9SDimitry Andric     }
2005e8d8bef9SDimitry Andric   }
2006e8d8bef9SDimitry Andric }
2007e8d8bef9SDimitry Andric 
2008e8d8bef9SDimitry Andric // Take a list of input sections containing symbol table indices and add those
2009e8d8bef9SDimitry Andric // symbols to an RVA table.
2010e8d8bef9SDimitry Andric void Writer::markSymbolsForRVATable(ObjFile *file,
2011e8d8bef9SDimitry Andric                                     ArrayRef<SectionChunk *> symIdxChunks,
2012e8d8bef9SDimitry Andric                                     SymbolRVASet &tableSymbols) {
2013e8d8bef9SDimitry Andric   std::vector<Symbol *> syms;
2014e8d8bef9SDimitry Andric   getSymbolsFromSections(file, symIdxChunks, syms);
2015e8d8bef9SDimitry Andric 
2016e8d8bef9SDimitry Andric   for (Symbol *s : syms)
20170b57cec5SDimitry Andric     addSymbolToRVASet(tableSymbols, cast<Defined>(s));
20180b57cec5SDimitry Andric }
20190b57cec5SDimitry Andric 
20200b57cec5SDimitry Andric // Replace the absolute table symbol with a synthetic symbol pointing to
20210b57cec5SDimitry Andric // tableChunk so that we can emit base relocations for it and resolve section
20220b57cec5SDimitry Andric // relative relocations.
20230b57cec5SDimitry Andric void Writer::maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
2024fe6060f1SDimitry Andric                               StringRef countSym, bool hasFlag) {
20250b57cec5SDimitry Andric   if (tableSymbols.empty())
20260b57cec5SDimitry Andric     return;
20270b57cec5SDimitry Andric 
2028fe6060f1SDimitry Andric   NonSectionChunk *tableChunk;
2029fe6060f1SDimitry Andric   if (hasFlag)
2030fe6060f1SDimitry Andric     tableChunk = make<RVAFlagTableChunk>(std::move(tableSymbols));
2031fe6060f1SDimitry Andric   else
2032fe6060f1SDimitry Andric     tableChunk = make<RVATableChunk>(std::move(tableSymbols));
20330b57cec5SDimitry Andric   rdataSec->addChunk(tableChunk);
20340b57cec5SDimitry Andric 
2035349cc55cSDimitry Andric   Symbol *t = ctx.symtab.findUnderscore(tableSym);
2036349cc55cSDimitry Andric   Symbol *c = ctx.symtab.findUnderscore(countSym);
20370b57cec5SDimitry Andric   replaceSymbol<DefinedSynthetic>(t, t->getName(), tableChunk);
2038fe6060f1SDimitry Andric   cast<DefinedAbsolute>(c)->setVA(tableChunk->getSize() / (hasFlag ? 5 : 4));
20390b57cec5SDimitry Andric }
20400b57cec5SDimitry Andric 
2041*5f757f3fSDimitry Andric // Create CHPE metadata chunks.
2042*5f757f3fSDimitry Andric void Writer::createECChunks() {
2043*5f757f3fSDimitry Andric   auto codeMapChunk = make<ECCodeMapChunk>(codeMap);
2044*5f757f3fSDimitry Andric   rdataSec->addChunk(codeMapChunk);
2045*5f757f3fSDimitry Andric   Symbol *codeMapSym = ctx.symtab.findUnderscore("__hybrid_code_map");
2046*5f757f3fSDimitry Andric   replaceSymbol<DefinedSynthetic>(codeMapSym, codeMapSym->getName(),
2047*5f757f3fSDimitry Andric                                   codeMapChunk);
2048*5f757f3fSDimitry Andric }
2049*5f757f3fSDimitry Andric 
20500b57cec5SDimitry Andric // MinGW specific. Gather all relocations that are imported from a DLL even
20510b57cec5SDimitry Andric // though the code didn't expect it to, produce the table that the runtime
20520b57cec5SDimitry Andric // uses for fixing them up, and provide the synthetic symbols that the
20530b57cec5SDimitry Andric // runtime uses for finding the table.
20540b57cec5SDimitry Andric void Writer::createRuntimePseudoRelocs() {
20550b57cec5SDimitry Andric   std::vector<RuntimePseudoReloc> rels;
20560b57cec5SDimitry Andric 
2057349cc55cSDimitry Andric   for (Chunk *c : ctx.symtab.getChunks()) {
20580b57cec5SDimitry Andric     auto *sc = dyn_cast<SectionChunk>(c);
20590b57cec5SDimitry Andric     if (!sc || !sc->live)
20600b57cec5SDimitry Andric       continue;
20610b57cec5SDimitry Andric     sc->getRuntimePseudoRelocs(rels);
20620b57cec5SDimitry Andric   }
20630b57cec5SDimitry Andric 
2064bdd1243dSDimitry Andric   if (!ctx.config.pseudoRelocs) {
20655ffd83dbSDimitry Andric     // Not writing any pseudo relocs; if some were needed, error out and
20665ffd83dbSDimitry Andric     // indicate what required them.
20675ffd83dbSDimitry Andric     for (const RuntimePseudoReloc &rpr : rels)
20685ffd83dbSDimitry Andric       error("automatic dllimport of " + rpr.sym->getName() + " in " +
20695ffd83dbSDimitry Andric             toString(rpr.target->file) + " requires pseudo relocations");
20705ffd83dbSDimitry Andric     return;
20715ffd83dbSDimitry Andric   }
20725ffd83dbSDimitry Andric 
20730b57cec5SDimitry Andric   if (!rels.empty())
20740b57cec5SDimitry Andric     log("Writing " + Twine(rels.size()) + " runtime pseudo relocations");
20750b57cec5SDimitry Andric   PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(rels);
20760b57cec5SDimitry Andric   rdataSec->addChunk(table);
20770b57cec5SDimitry Andric   EmptyChunk *endOfList = make<EmptyChunk>();
20780b57cec5SDimitry Andric   rdataSec->addChunk(endOfList);
20790b57cec5SDimitry Andric 
2080349cc55cSDimitry Andric   Symbol *headSym = ctx.symtab.findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__");
2081349cc55cSDimitry Andric   Symbol *endSym =
2082349cc55cSDimitry Andric       ctx.symtab.findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__");
20830b57cec5SDimitry Andric   replaceSymbol<DefinedSynthetic>(headSym, headSym->getName(), table);
20840b57cec5SDimitry Andric   replaceSymbol<DefinedSynthetic>(endSym, endSym->getName(), endOfList);
20850b57cec5SDimitry Andric }
20860b57cec5SDimitry Andric 
20870b57cec5SDimitry Andric // MinGW specific.
20880b57cec5SDimitry Andric // The MinGW .ctors and .dtors lists have sentinels at each end;
20890b57cec5SDimitry Andric // a (uintptr_t)-1 at the start and a (uintptr_t)0 at the end.
20900b57cec5SDimitry Andric // There's a symbol pointing to the start sentinel pointer, __CTOR_LIST__
20910b57cec5SDimitry Andric // and __DTOR_LIST__ respectively.
20920b57cec5SDimitry Andric void Writer::insertCtorDtorSymbols() {
2093bdd1243dSDimitry Andric   AbsolutePointerChunk *ctorListHead = make<AbsolutePointerChunk>(ctx, -1);
2094bdd1243dSDimitry Andric   AbsolutePointerChunk *ctorListEnd = make<AbsolutePointerChunk>(ctx, 0);
2095bdd1243dSDimitry Andric   AbsolutePointerChunk *dtorListHead = make<AbsolutePointerChunk>(ctx, -1);
2096bdd1243dSDimitry Andric   AbsolutePointerChunk *dtorListEnd = make<AbsolutePointerChunk>(ctx, 0);
20970b57cec5SDimitry Andric   ctorsSec->insertChunkAtStart(ctorListHead);
20980b57cec5SDimitry Andric   ctorsSec->addChunk(ctorListEnd);
20990b57cec5SDimitry Andric   dtorsSec->insertChunkAtStart(dtorListHead);
21000b57cec5SDimitry Andric   dtorsSec->addChunk(dtorListEnd);
21010b57cec5SDimitry Andric 
2102349cc55cSDimitry Andric   Symbol *ctorListSym = ctx.symtab.findUnderscore("__CTOR_LIST__");
2103349cc55cSDimitry Andric   Symbol *dtorListSym = ctx.symtab.findUnderscore("__DTOR_LIST__");
21040b57cec5SDimitry Andric   replaceSymbol<DefinedSynthetic>(ctorListSym, ctorListSym->getName(),
21050b57cec5SDimitry Andric                                   ctorListHead);
21060b57cec5SDimitry Andric   replaceSymbol<DefinedSynthetic>(dtorListSym, dtorListSym->getName(),
21070b57cec5SDimitry Andric                                   dtorListHead);
21080b57cec5SDimitry Andric }
21090b57cec5SDimitry Andric 
21100b57cec5SDimitry Andric // Handles /section options to allow users to overwrite
21110b57cec5SDimitry Andric // section attributes.
21120b57cec5SDimitry Andric void Writer::setSectionPermissions() {
2113*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Sections permissions");
2114bdd1243dSDimitry Andric   for (auto &p : ctx.config.section) {
21150b57cec5SDimitry Andric     StringRef name = p.first;
21160b57cec5SDimitry Andric     uint32_t perm = p.second;
2117349cc55cSDimitry Andric     for (OutputSection *sec : ctx.outputSections)
21180b57cec5SDimitry Andric       if (sec->name == name)
21190b57cec5SDimitry Andric         sec->setPermissions(perm);
21200b57cec5SDimitry Andric   }
21210b57cec5SDimitry Andric }
21220b57cec5SDimitry Andric 
2123*5f757f3fSDimitry Andric // Set symbols used by ARM64EC metadata.
2124*5f757f3fSDimitry Andric void Writer::setECSymbols() {
2125*5f757f3fSDimitry Andric   if (!isArm64EC(ctx.config.machine))
2126*5f757f3fSDimitry Andric     return;
2127*5f757f3fSDimitry Andric 
2128*5f757f3fSDimitry Andric   Symbol *rfeTableSym = ctx.symtab.findUnderscore("__arm64x_extra_rfe_table");
2129*5f757f3fSDimitry Andric   replaceSymbol<DefinedSynthetic>(rfeTableSym, "__arm64x_extra_rfe_table",
2130*5f757f3fSDimitry Andric                                   pdata.first);
2131*5f757f3fSDimitry Andric 
2132*5f757f3fSDimitry Andric   if (pdata.first) {
2133*5f757f3fSDimitry Andric     Symbol *rfeSizeSym =
2134*5f757f3fSDimitry Andric         ctx.symtab.findUnderscore("__arm64x_extra_rfe_table_size");
2135*5f757f3fSDimitry Andric     cast<DefinedAbsolute>(rfeSizeSym)
2136*5f757f3fSDimitry Andric         ->setVA(pdata.last->getRVA() + pdata.last->getSize() -
2137*5f757f3fSDimitry Andric                 pdata.first->getRVA());
2138*5f757f3fSDimitry Andric   }
2139*5f757f3fSDimitry Andric }
2140*5f757f3fSDimitry Andric 
21410b57cec5SDimitry Andric // Write section contents to a mmap'ed file.
21420b57cec5SDimitry Andric void Writer::writeSections() {
2143*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Write sections");
21440b57cec5SDimitry Andric   uint8_t *buf = buffer->getBufferStart();
2145349cc55cSDimitry Andric   for (OutputSection *sec : ctx.outputSections) {
21460b57cec5SDimitry Andric     uint8_t *secBuf = buf + sec->getFileOff();
21470b57cec5SDimitry Andric     // Fill gaps between functions in .text with INT3 instructions
21480b57cec5SDimitry Andric     // instead of leaving as NUL bytes (which can be interpreted as
2149*5f757f3fSDimitry Andric     // ADD instructions). Only fill the gaps between chunks. Most
2150*5f757f3fSDimitry Andric     // chunks overwrite it anyway, but uninitialized data chunks
2151*5f757f3fSDimitry Andric     // merged into a code section don't.
215206c3fb27SDimitry Andric     if ((sec->header.Characteristics & IMAGE_SCN_CNT_CODE) &&
2153*5f757f3fSDimitry Andric         (ctx.config.machine == AMD64 || ctx.config.machine == I386)) {
2154*5f757f3fSDimitry Andric       uint32_t prevEnd = 0;
2155*5f757f3fSDimitry Andric       for (Chunk *c : sec->chunks) {
2156*5f757f3fSDimitry Andric         uint32_t off = c->getRVA() - sec->getRVA();
2157*5f757f3fSDimitry Andric         memset(secBuf + prevEnd, 0xCC, off - prevEnd);
2158*5f757f3fSDimitry Andric         prevEnd = off + c->getSize();
2159*5f757f3fSDimitry Andric       }
2160*5f757f3fSDimitry Andric       memset(secBuf + prevEnd, 0xCC, sec->getRawSize() - prevEnd);
2161*5f757f3fSDimitry Andric     }
2162*5f757f3fSDimitry Andric 
21630b57cec5SDimitry Andric     parallelForEach(sec->chunks, [&](Chunk *c) {
21640b57cec5SDimitry Andric       c->writeTo(secBuf + c->getRVA() - sec->getRVA());
21650b57cec5SDimitry Andric     });
21660b57cec5SDimitry Andric   }
21670b57cec5SDimitry Andric }
21680b57cec5SDimitry Andric 
21690b57cec5SDimitry Andric void Writer::writeBuildId() {
2170*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Write build ID");
2171*5f757f3fSDimitry Andric 
21720b57cec5SDimitry Andric   // There are two important parts to the build ID.
21730b57cec5SDimitry Andric   // 1) If building with debug info, the COFF debug directory contains a
21740b57cec5SDimitry Andric   //    timestamp as well as a Guid and Age of the PDB.
21750b57cec5SDimitry Andric   // 2) In all cases, the PE COFF file header also contains a timestamp.
21760b57cec5SDimitry Andric   // For reproducibility, instead of a timestamp we want to use a hash of the
21770b57cec5SDimitry Andric   // PE contents.
2178bdd1243dSDimitry Andric   Configuration *config = &ctx.config;
2179*5f757f3fSDimitry Andric   bool generateSyntheticBuildId = config->buildIDHash == BuildIDHash::Binary;
2180*5f757f3fSDimitry Andric   if (generateSyntheticBuildId) {
21810b57cec5SDimitry Andric     assert(buildId && "BuildId is not set!");
21820b57cec5SDimitry Andric     // BuildId->BuildId was filled in when the PDB was written.
21830b57cec5SDimitry Andric   }
21840b57cec5SDimitry Andric 
21850b57cec5SDimitry Andric   // At this point the only fields in the COFF file which remain unset are the
21860b57cec5SDimitry Andric   // "timestamp" in the COFF file header, and the ones in the coff debug
21870b57cec5SDimitry Andric   // directory.  Now we can hash the file and write that hash to the various
21880b57cec5SDimitry Andric   // timestamp fields in the file.
21890b57cec5SDimitry Andric   StringRef outputFileData(
21900b57cec5SDimitry Andric       reinterpret_cast<const char *>(buffer->getBufferStart()),
21910b57cec5SDimitry Andric       buffer->getBufferSize());
21920b57cec5SDimitry Andric 
21930b57cec5SDimitry Andric   uint32_t timestamp = config->timestamp;
21940b57cec5SDimitry Andric   uint64_t hash = 0;
21950b57cec5SDimitry Andric 
21960b57cec5SDimitry Andric   if (config->repro || generateSyntheticBuildId)
219706c3fb27SDimitry Andric     hash = xxh3_64bits(outputFileData);
21980b57cec5SDimitry Andric 
21990b57cec5SDimitry Andric   if (config->repro)
22000b57cec5SDimitry Andric     timestamp = static_cast<uint32_t>(hash);
22010b57cec5SDimitry Andric 
22020b57cec5SDimitry Andric   if (generateSyntheticBuildId) {
22030b57cec5SDimitry Andric     buildId->buildId->PDB70.CVSignature = OMF::Signature::PDB70;
22040b57cec5SDimitry Andric     buildId->buildId->PDB70.Age = 1;
22050b57cec5SDimitry Andric     memcpy(buildId->buildId->PDB70.Signature, &hash, 8);
22060b57cec5SDimitry Andric     // xxhash only gives us 8 bytes, so put some fixed data in the other half.
22070b57cec5SDimitry Andric     memcpy(&buildId->buildId->PDB70.Signature[8], "LLD PDB.", 8);
22080b57cec5SDimitry Andric   }
22090b57cec5SDimitry Andric 
22100b57cec5SDimitry Andric   if (debugDirectory)
22110b57cec5SDimitry Andric     debugDirectory->setTimeDateStamp(timestamp);
22120b57cec5SDimitry Andric 
22130b57cec5SDimitry Andric   uint8_t *buf = buffer->getBufferStart();
22140b57cec5SDimitry Andric   buf += dosStubSize + sizeof(PEMagic);
22150b57cec5SDimitry Andric   object::coff_file_header *coffHeader =
22160b57cec5SDimitry Andric       reinterpret_cast<coff_file_header *>(buf);
22170b57cec5SDimitry Andric   coffHeader->TimeDateStamp = timestamp;
22180b57cec5SDimitry Andric }
22190b57cec5SDimitry Andric 
22200b57cec5SDimitry Andric // Sort .pdata section contents according to PE/COFF spec 5.5.
2221*5f757f3fSDimitry Andric template <typename T>
2222*5f757f3fSDimitry Andric void Writer::sortExceptionTable(ChunkRange &exceptionTable) {
2223*5f757f3fSDimitry Andric   if (!exceptionTable.first)
22240b57cec5SDimitry Andric     return;
2225*5f757f3fSDimitry Andric 
22260b57cec5SDimitry Andric   // We assume .pdata contains function table entries only.
22270b57cec5SDimitry Andric   auto bufAddr = [&](Chunk *c) {
2228349cc55cSDimitry Andric     OutputSection *os = ctx.getOutputSection(c);
22290b57cec5SDimitry Andric     return buffer->getBufferStart() + os->getFileOff() + c->getRVA() -
22300b57cec5SDimitry Andric            os->getRVA();
22310b57cec5SDimitry Andric   };
2232*5f757f3fSDimitry Andric   uint8_t *begin = bufAddr(exceptionTable.first);
2233*5f757f3fSDimitry Andric   uint8_t *end = bufAddr(exceptionTable.last) + exceptionTable.last->getSize();
2234*5f757f3fSDimitry Andric   if ((end - begin) % sizeof(T) != 0) {
22355ffd83dbSDimitry Andric     fatal("unexpected .pdata size: " + Twine(end - begin) +
2236*5f757f3fSDimitry Andric           " is not a multiple of " + Twine(sizeof(T)));
22375ffd83dbSDimitry Andric   }
2238*5f757f3fSDimitry Andric 
2239*5f757f3fSDimitry Andric   parallelSort(MutableArrayRef<T>(reinterpret_cast<T *>(begin),
2240*5f757f3fSDimitry Andric                                   reinterpret_cast<T *>(end)),
2241*5f757f3fSDimitry Andric                [](const T &a, const T &b) { return a.begin < b.begin; });
22420b57cec5SDimitry Andric }
2243*5f757f3fSDimitry Andric 
2244*5f757f3fSDimitry Andric // Sort .pdata section contents according to PE/COFF spec 5.5.
2245*5f757f3fSDimitry Andric void Writer::sortExceptionTables() {
2246*5f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Sort exception table");
2247*5f757f3fSDimitry Andric 
2248*5f757f3fSDimitry Andric   struct EntryX64 {
2249*5f757f3fSDimitry Andric     ulittle32_t begin, end, unwind;
2250*5f757f3fSDimitry Andric   };
2251*5f757f3fSDimitry Andric   struct EntryArm {
2252*5f757f3fSDimitry Andric     ulittle32_t begin, unwind;
2253*5f757f3fSDimitry Andric   };
2254*5f757f3fSDimitry Andric 
2255*5f757f3fSDimitry Andric   switch (ctx.config.machine) {
2256*5f757f3fSDimitry Andric   case AMD64:
2257*5f757f3fSDimitry Andric     sortExceptionTable<EntryX64>(pdata);
2258*5f757f3fSDimitry Andric     break;
2259*5f757f3fSDimitry Andric   case ARM64EC:
2260*5f757f3fSDimitry Andric   case ARM64X:
2261*5f757f3fSDimitry Andric     sortExceptionTable<EntryX64>(hybridPdata);
2262*5f757f3fSDimitry Andric     [[fallthrough]];
2263*5f757f3fSDimitry Andric   case ARMNT:
2264*5f757f3fSDimitry Andric   case ARM64:
2265*5f757f3fSDimitry Andric     sortExceptionTable<EntryArm>(pdata);
2266*5f757f3fSDimitry Andric     break;
2267*5f757f3fSDimitry Andric   default:
2268*5f757f3fSDimitry Andric     if (pdata.first)
2269480093f4SDimitry Andric       lld::errs() << "warning: don't know how to handle .pdata.\n";
2270*5f757f3fSDimitry Andric     break;
2271*5f757f3fSDimitry Andric   }
22720b57cec5SDimitry Andric }
22730b57cec5SDimitry Andric 
22740b57cec5SDimitry Andric // The CRT section contains, among other things, the array of function
22750b57cec5SDimitry Andric // pointers that initialize every global variable that is not trivially
22760b57cec5SDimitry Andric // constructed. The CRT calls them one after the other prior to invoking
22770b57cec5SDimitry Andric // main().
22780b57cec5SDimitry Andric //
22790b57cec5SDimitry Andric // As per C++ spec, 3.6.2/2.3,
22800b57cec5SDimitry Andric // "Variables with ordered initialization defined within a single
22810b57cec5SDimitry Andric // translation unit shall be initialized in the order of their definitions
22820b57cec5SDimitry Andric // in the translation unit"
22830b57cec5SDimitry Andric //
22840b57cec5SDimitry Andric // It is therefore critical to sort the chunks containing the function
22850b57cec5SDimitry Andric // pointers in the order that they are listed in the object file (top to
22860b57cec5SDimitry Andric // bottom), otherwise global objects might not be initialized in the
22870b57cec5SDimitry Andric // correct order.
22880b57cec5SDimitry Andric void Writer::sortCRTSectionChunks(std::vector<Chunk *> &chunks) {
22890b57cec5SDimitry Andric   auto sectionChunkOrder = [](const Chunk *a, const Chunk *b) {
22900b57cec5SDimitry Andric     auto sa = dyn_cast<SectionChunk>(a);
22910b57cec5SDimitry Andric     auto sb = dyn_cast<SectionChunk>(b);
22920b57cec5SDimitry Andric     assert(sa && sb && "Non-section chunks in CRT section!");
22930b57cec5SDimitry Andric 
22940b57cec5SDimitry Andric     StringRef sAObj = sa->file->mb.getBufferIdentifier();
22950b57cec5SDimitry Andric     StringRef sBObj = sb->file->mb.getBufferIdentifier();
22960b57cec5SDimitry Andric 
22970b57cec5SDimitry Andric     return sAObj == sBObj && sa->getSectionNumber() < sb->getSectionNumber();
22980b57cec5SDimitry Andric   };
22990b57cec5SDimitry Andric   llvm::stable_sort(chunks, sectionChunkOrder);
23000b57cec5SDimitry Andric 
2301bdd1243dSDimitry Andric   if (ctx.config.verbose) {
23020b57cec5SDimitry Andric     for (auto &c : chunks) {
23030b57cec5SDimitry Andric       auto sc = dyn_cast<SectionChunk>(c);
23040b57cec5SDimitry Andric       log("  " + sc->file->mb.getBufferIdentifier().str() +
23050b57cec5SDimitry Andric           ", SectionID: " + Twine(sc->getSectionNumber()));
23060b57cec5SDimitry Andric     }
23070b57cec5SDimitry Andric   }
23080b57cec5SDimitry Andric }
23090b57cec5SDimitry Andric 
23100b57cec5SDimitry Andric OutputSection *Writer::findSection(StringRef name) {
2311349cc55cSDimitry Andric   for (OutputSection *sec : ctx.outputSections)
23120b57cec5SDimitry Andric     if (sec->name == name)
23130b57cec5SDimitry Andric       return sec;
23140b57cec5SDimitry Andric   return nullptr;
23150b57cec5SDimitry Andric }
23160b57cec5SDimitry Andric 
23170b57cec5SDimitry Andric uint32_t Writer::getSizeOfInitializedData() {
23180b57cec5SDimitry Andric   uint32_t res = 0;
2319349cc55cSDimitry Andric   for (OutputSection *s : ctx.outputSections)
23200b57cec5SDimitry Andric     if (s->header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
23210b57cec5SDimitry Andric       res += s->getRawSize();
23220b57cec5SDimitry Andric   return res;
23230b57cec5SDimitry Andric }
23240b57cec5SDimitry Andric 
23250b57cec5SDimitry Andric // Add base relocations to .reloc section.
23260b57cec5SDimitry Andric void Writer::addBaserels() {
2327bdd1243dSDimitry Andric   if (!ctx.config.relocatable)
23280b57cec5SDimitry Andric     return;
23290b57cec5SDimitry Andric   relocSec->chunks.clear();
23300b57cec5SDimitry Andric   std::vector<Baserel> v;
2331349cc55cSDimitry Andric   for (OutputSection *sec : ctx.outputSections) {
23320b57cec5SDimitry Andric     if (sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
23330b57cec5SDimitry Andric       continue;
2334*5f757f3fSDimitry Andric     llvm::TimeTraceScope timeScope("Base relocations: ", sec->name);
23350b57cec5SDimitry Andric     // Collect all locations for base relocations.
23360b57cec5SDimitry Andric     for (Chunk *c : sec->chunks)
23370b57cec5SDimitry Andric       c->getBaserels(&v);
23380b57cec5SDimitry Andric     // Add the addresses to .reloc section.
23390b57cec5SDimitry Andric     if (!v.empty())
23400b57cec5SDimitry Andric       addBaserelBlocks(v);
23410b57cec5SDimitry Andric     v.clear();
23420b57cec5SDimitry Andric   }
23430b57cec5SDimitry Andric }
23440b57cec5SDimitry Andric 
23450b57cec5SDimitry Andric // Add addresses to .reloc section. Note that addresses are grouped by page.
23460b57cec5SDimitry Andric void Writer::addBaserelBlocks(std::vector<Baserel> &v) {
23470b57cec5SDimitry Andric   const uint32_t mask = ~uint32_t(pageSize - 1);
23480b57cec5SDimitry Andric   uint32_t page = v[0].rva & mask;
23490b57cec5SDimitry Andric   size_t i = 0, j = 1;
23500b57cec5SDimitry Andric   for (size_t e = v.size(); j < e; ++j) {
23510b57cec5SDimitry Andric     uint32_t p = v[j].rva & mask;
23520b57cec5SDimitry Andric     if (p == page)
23530b57cec5SDimitry Andric       continue;
23540b57cec5SDimitry Andric     relocSec->addChunk(make<BaserelChunk>(page, &v[i], &v[0] + j));
23550b57cec5SDimitry Andric     i = j;
23560b57cec5SDimitry Andric     page = p;
23570b57cec5SDimitry Andric   }
23580b57cec5SDimitry Andric   if (i == j)
23590b57cec5SDimitry Andric     return;
23600b57cec5SDimitry Andric   relocSec->addChunk(make<BaserelChunk>(page, &v[i], &v[0] + j));
23610b57cec5SDimitry Andric }
23620b57cec5SDimitry Andric 
23630b57cec5SDimitry Andric PartialSection *Writer::createPartialSection(StringRef name,
23640b57cec5SDimitry Andric                                              uint32_t outChars) {
23650b57cec5SDimitry Andric   PartialSection *&pSec = partialSections[{name, outChars}];
23660b57cec5SDimitry Andric   if (pSec)
23670b57cec5SDimitry Andric     return pSec;
23680b57cec5SDimitry Andric   pSec = make<PartialSection>(name, outChars);
23690b57cec5SDimitry Andric   return pSec;
23700b57cec5SDimitry Andric }
23710b57cec5SDimitry Andric 
23720b57cec5SDimitry Andric PartialSection *Writer::findPartialSection(StringRef name, uint32_t outChars) {
23730b57cec5SDimitry Andric   auto it = partialSections.find({name, outChars});
23740b57cec5SDimitry Andric   if (it != partialSections.end())
23750b57cec5SDimitry Andric     return it->second;
23760b57cec5SDimitry Andric   return nullptr;
23770b57cec5SDimitry Andric }
2378e8d8bef9SDimitry Andric 
2379e8d8bef9SDimitry Andric void Writer::fixTlsAlignment() {
2380e8d8bef9SDimitry Andric   Defined *tlsSym =
2381349cc55cSDimitry Andric       dyn_cast_or_null<Defined>(ctx.symtab.findUnderscore("_tls_used"));
2382e8d8bef9SDimitry Andric   if (!tlsSym)
2383e8d8bef9SDimitry Andric     return;
2384e8d8bef9SDimitry Andric 
2385349cc55cSDimitry Andric   OutputSection *sec = ctx.getOutputSection(tlsSym->getChunk());
2386e8d8bef9SDimitry Andric   assert(sec && tlsSym->getRVA() >= sec->getRVA() &&
2387e8d8bef9SDimitry Andric          "no output section for _tls_used");
2388e8d8bef9SDimitry Andric 
2389e8d8bef9SDimitry Andric   uint8_t *secBuf = buffer->getBufferStart() + sec->getFileOff();
2390e8d8bef9SDimitry Andric   uint64_t tlsOffset = tlsSym->getRVA() - sec->getRVA();
2391bdd1243dSDimitry Andric   uint64_t directorySize = ctx.config.is64()
2392e8d8bef9SDimitry Andric                                ? sizeof(object::coff_tls_directory64)
2393e8d8bef9SDimitry Andric                                : sizeof(object::coff_tls_directory32);
2394e8d8bef9SDimitry Andric 
2395e8d8bef9SDimitry Andric   if (tlsOffset + directorySize > sec->getRawSize())
2396e8d8bef9SDimitry Andric     fatal("_tls_used sym is malformed");
2397e8d8bef9SDimitry Andric 
2398bdd1243dSDimitry Andric   if (ctx.config.is64()) {
2399e8d8bef9SDimitry Andric     object::coff_tls_directory64 *tlsDir =
2400e8d8bef9SDimitry Andric         reinterpret_cast<object::coff_tls_directory64 *>(&secBuf[tlsOffset]);
2401e8d8bef9SDimitry Andric     tlsDir->setAlignment(tlsAlignment);
2402e8d8bef9SDimitry Andric   } else {
2403e8d8bef9SDimitry Andric     object::coff_tls_directory32 *tlsDir =
2404e8d8bef9SDimitry Andric         reinterpret_cast<object::coff_tls_directory32 *>(&secBuf[tlsOffset]);
2405e8d8bef9SDimitry Andric     tlsDir->setAlignment(tlsAlignment);
2406e8d8bef9SDimitry Andric   }
2407e8d8bef9SDimitry Andric }
2408bdd1243dSDimitry Andric 
2409*5f757f3fSDimitry Andric void Writer::prepareLoadConfig() {
2410bdd1243dSDimitry Andric   Symbol *sym = ctx.symtab.findUnderscore("_load_config_used");
2411bdd1243dSDimitry Andric   auto *b = cast_if_present<DefinedRegular>(sym);
2412bdd1243dSDimitry Andric   if (!b) {
2413bdd1243dSDimitry Andric     if (ctx.config.guardCF != GuardCFLevel::Off)
2414bdd1243dSDimitry Andric       warn("Control Flow Guard is enabled but '_load_config_used' is missing");
2415bdd1243dSDimitry Andric     return;
2416bdd1243dSDimitry Andric   }
2417bdd1243dSDimitry Andric 
2418bdd1243dSDimitry Andric   OutputSection *sec = ctx.getOutputSection(b->getChunk());
2419bdd1243dSDimitry Andric   uint8_t *buf = buffer->getBufferStart();
2420bdd1243dSDimitry Andric   uint8_t *secBuf = buf + sec->getFileOff();
2421bdd1243dSDimitry Andric   uint8_t *symBuf = secBuf + (b->getRVA() - sec->getRVA());
2422bdd1243dSDimitry Andric   uint32_t expectedAlign = ctx.config.is64() ? 8 : 4;
2423bdd1243dSDimitry Andric   if (b->getChunk()->getAlignment() < expectedAlign)
2424bdd1243dSDimitry Andric     warn("'_load_config_used' is misaligned (expected alignment to be " +
2425bdd1243dSDimitry Andric          Twine(expectedAlign) + " bytes, got " +
2426bdd1243dSDimitry Andric          Twine(b->getChunk()->getAlignment()) + " instead)");
2427bdd1243dSDimitry Andric   else if (!isAligned(Align(expectedAlign), b->getRVA()))
2428bdd1243dSDimitry Andric     warn("'_load_config_used' is misaligned (RVA is 0x" +
2429bdd1243dSDimitry Andric          Twine::utohexstr(b->getRVA()) + " not aligned to " +
2430bdd1243dSDimitry Andric          Twine(expectedAlign) + " bytes)");
2431bdd1243dSDimitry Andric 
2432bdd1243dSDimitry Andric   if (ctx.config.is64())
2433*5f757f3fSDimitry Andric     prepareLoadConfig(reinterpret_cast<coff_load_configuration64 *>(symBuf));
2434bdd1243dSDimitry Andric   else
2435*5f757f3fSDimitry Andric     prepareLoadConfig(reinterpret_cast<coff_load_configuration32 *>(symBuf));
2436*5f757f3fSDimitry Andric }
2437*5f757f3fSDimitry Andric 
2438*5f757f3fSDimitry Andric template <typename T> void Writer::prepareLoadConfig(T *loadConfig) {
2439*5f757f3fSDimitry Andric   if (ctx.config.dependentLoadFlags)
2440*5f757f3fSDimitry Andric     loadConfig->DependentLoadFlags = ctx.config.dependentLoadFlags;
2441*5f757f3fSDimitry Andric 
2442*5f757f3fSDimitry Andric   checkLoadConfigGuardData(loadConfig);
2443bdd1243dSDimitry Andric }
2444bdd1243dSDimitry Andric 
2445bdd1243dSDimitry Andric template <typename T>
2446bdd1243dSDimitry Andric void Writer::checkLoadConfigGuardData(const T *loadConfig) {
2447bdd1243dSDimitry Andric   size_t loadConfigSize = loadConfig->Size;
2448bdd1243dSDimitry Andric 
2449bdd1243dSDimitry Andric #define RETURN_IF_NOT_CONTAINS(field)                                          \
2450bdd1243dSDimitry Andric   if (loadConfigSize < offsetof(T, field) + sizeof(T::field)) {                \
2451bdd1243dSDimitry Andric     warn("'_load_config_used' structure too small to include " #field);        \
2452bdd1243dSDimitry Andric     return;                                                                    \
2453bdd1243dSDimitry Andric   }
2454bdd1243dSDimitry Andric 
2455bdd1243dSDimitry Andric #define IF_CONTAINS(field)                                                     \
2456bdd1243dSDimitry Andric   if (loadConfigSize >= offsetof(T, field) + sizeof(T::field))
2457bdd1243dSDimitry Andric 
2458bdd1243dSDimitry Andric #define CHECK_VA(field, sym)                                                   \
2459bdd1243dSDimitry Andric   if (auto *s = dyn_cast<DefinedSynthetic>(ctx.symtab.findUnderscore(sym)))    \
2460bdd1243dSDimitry Andric     if (loadConfig->field != ctx.config.imageBase + s->getRVA())               \
2461bdd1243dSDimitry Andric       warn(#field " not set correctly in '_load_config_used'");
2462bdd1243dSDimitry Andric 
2463bdd1243dSDimitry Andric #define CHECK_ABSOLUTE(field, sym)                                             \
2464bdd1243dSDimitry Andric   if (auto *s = dyn_cast<DefinedAbsolute>(ctx.symtab.findUnderscore(sym)))     \
2465bdd1243dSDimitry Andric     if (loadConfig->field != s->getVA())                                       \
2466bdd1243dSDimitry Andric       warn(#field " not set correctly in '_load_config_used'");
2467bdd1243dSDimitry Andric 
2468bdd1243dSDimitry Andric   if (ctx.config.guardCF == GuardCFLevel::Off)
2469bdd1243dSDimitry Andric     return;
2470bdd1243dSDimitry Andric   RETURN_IF_NOT_CONTAINS(GuardFlags)
2471bdd1243dSDimitry Andric   CHECK_VA(GuardCFFunctionTable, "__guard_fids_table")
2472bdd1243dSDimitry Andric   CHECK_ABSOLUTE(GuardCFFunctionCount, "__guard_fids_count")
2473bdd1243dSDimitry Andric   CHECK_ABSOLUTE(GuardFlags, "__guard_flags")
2474bdd1243dSDimitry Andric   IF_CONTAINS(GuardAddressTakenIatEntryCount) {
2475bdd1243dSDimitry Andric     CHECK_VA(GuardAddressTakenIatEntryTable, "__guard_iat_table")
2476bdd1243dSDimitry Andric     CHECK_ABSOLUTE(GuardAddressTakenIatEntryCount, "__guard_iat_count")
2477bdd1243dSDimitry Andric   }
2478bdd1243dSDimitry Andric 
2479bdd1243dSDimitry Andric   if (!(ctx.config.guardCF & GuardCFLevel::LongJmp))
2480bdd1243dSDimitry Andric     return;
2481bdd1243dSDimitry Andric   RETURN_IF_NOT_CONTAINS(GuardLongJumpTargetCount)
2482bdd1243dSDimitry Andric   CHECK_VA(GuardLongJumpTargetTable, "__guard_longjmp_table")
2483bdd1243dSDimitry Andric   CHECK_ABSOLUTE(GuardLongJumpTargetCount, "__guard_longjmp_count")
2484bdd1243dSDimitry Andric 
2485bdd1243dSDimitry Andric   if (!(ctx.config.guardCF & GuardCFLevel::EHCont))
2486bdd1243dSDimitry Andric     return;
2487bdd1243dSDimitry Andric   RETURN_IF_NOT_CONTAINS(GuardEHContinuationCount)
2488bdd1243dSDimitry Andric   CHECK_VA(GuardEHContinuationTable, "__guard_eh_cont_table")
2489bdd1243dSDimitry Andric   CHECK_ABSOLUTE(GuardEHContinuationCount, "__guard_eh_cont_count")
2490bdd1243dSDimitry Andric 
2491bdd1243dSDimitry Andric #undef RETURN_IF_NOT_CONTAINS
2492bdd1243dSDimitry Andric #undef IF_CONTAINS
2493bdd1243dSDimitry Andric #undef CHECK_VA
2494bdd1243dSDimitry Andric #undef CHECK_ABSOLUTE
2495bdd1243dSDimitry Andric }
2496