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" 100b57cec5SDimitry Andric #include "Config.h" 110b57cec5SDimitry Andric #include "DLL.h" 120b57cec5SDimitry Andric #include "InputFiles.h" 130b57cec5SDimitry Andric #include "MapFile.h" 140b57cec5SDimitry Andric #include "PDB.h" 150b57cec5SDimitry Andric #include "SymbolTable.h" 160b57cec5SDimitry Andric #include "Symbols.h" 170b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h" 180b57cec5SDimitry Andric #include "lld/Common/Memory.h" 190b57cec5SDimitry Andric #include "lld/Common/Threads.h" 200b57cec5SDimitry Andric #include "lld/Common/Timer.h" 210b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 220b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 230b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h" 240b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h" 250b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 260b57cec5SDimitry Andric #include "llvm/Support/Endian.h" 270b57cec5SDimitry Andric #include "llvm/Support/FileOutputBuffer.h" 280b57cec5SDimitry Andric #include "llvm/Support/Parallel.h" 290b57cec5SDimitry Andric #include "llvm/Support/Path.h" 300b57cec5SDimitry Andric #include "llvm/Support/RandomNumberGenerator.h" 310b57cec5SDimitry Andric #include "llvm/Support/xxhash.h" 320b57cec5SDimitry Andric #include <algorithm> 330b57cec5SDimitry Andric #include <cstdio> 340b57cec5SDimitry Andric #include <map> 350b57cec5SDimitry Andric #include <memory> 360b57cec5SDimitry Andric #include <utility> 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric using namespace llvm; 390b57cec5SDimitry Andric using namespace llvm::COFF; 400b57cec5SDimitry Andric using namespace llvm::object; 410b57cec5SDimitry Andric using namespace llvm::support; 420b57cec5SDimitry Andric using namespace llvm::support::endian; 43*85868e8aSDimitry Andric 44*85868e8aSDimitry Andric namespace lld { 45*85868e8aSDimitry Andric namespace coff { 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric /* To re-generate DOSProgram: 480b57cec5SDimitry Andric $ cat > /tmp/DOSProgram.asm 490b57cec5SDimitry Andric org 0 500b57cec5SDimitry Andric ; Copy cs to ds. 510b57cec5SDimitry Andric push cs 520b57cec5SDimitry Andric pop ds 530b57cec5SDimitry Andric ; Point ds:dx at the $-terminated string. 540b57cec5SDimitry Andric mov dx, str 550b57cec5SDimitry Andric ; Int 21/AH=09h: Write string to standard output. 560b57cec5SDimitry Andric mov ah, 0x9 570b57cec5SDimitry Andric int 0x21 580b57cec5SDimitry Andric ; Int 21/AH=4Ch: Exit with return code (in AL). 590b57cec5SDimitry Andric mov ax, 0x4C01 600b57cec5SDimitry Andric int 0x21 610b57cec5SDimitry Andric str: 620b57cec5SDimitry Andric db 'This program cannot be run in DOS mode.$' 630b57cec5SDimitry Andric align 8, db 0 640b57cec5SDimitry Andric $ nasm -fbin /tmp/DOSProgram.asm -o /tmp/DOSProgram.bin 650b57cec5SDimitry Andric $ xxd -i /tmp/DOSProgram.bin 660b57cec5SDimitry Andric */ 670b57cec5SDimitry Andric static unsigned char dosProgram[] = { 680b57cec5SDimitry Andric 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8, 0x01, 0x4c, 690b57cec5SDimitry Andric 0xcd, 0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, 700b57cec5SDimitry Andric 0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x62, 0x65, 710b57cec5SDimitry Andric 0x20, 0x72, 0x75, 0x6e, 0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20, 720b57cec5SDimitry Andric 0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x24, 0x00, 0x00 730b57cec5SDimitry Andric }; 740b57cec5SDimitry Andric static_assert(sizeof(dosProgram) % 8 == 0, 750b57cec5SDimitry Andric "DOSProgram size must be multiple of 8"); 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric static const int dosStubSize = sizeof(dos_header) + sizeof(dosProgram); 780b57cec5SDimitry Andric static_assert(dosStubSize % 8 == 0, "DOSStub size must be multiple of 8"); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric static const int numberOfDataDirectory = 16; 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric // Global vector of all output sections. After output sections are finalized, 830b57cec5SDimitry Andric // this can be indexed by Chunk::getOutputSection. 840b57cec5SDimitry Andric static std::vector<OutputSection *> outputSections; 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric OutputSection *Chunk::getOutputSection() const { 870b57cec5SDimitry Andric return osidx == 0 ? nullptr : outputSections[osidx - 1]; 880b57cec5SDimitry Andric } 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric namespace { 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric class DebugDirectoryChunk : public NonSectionChunk { 930b57cec5SDimitry Andric public: 940b57cec5SDimitry Andric DebugDirectoryChunk(const std::vector<Chunk *> &r, bool writeRepro) 950b57cec5SDimitry Andric : records(r), writeRepro(writeRepro) {} 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric size_t getSize() const override { 980b57cec5SDimitry Andric return (records.size() + int(writeRepro)) * sizeof(debug_directory); 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric void writeTo(uint8_t *b) const override { 1020b57cec5SDimitry Andric auto *d = reinterpret_cast<debug_directory *>(b); 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric for (const Chunk *record : records) { 1050b57cec5SDimitry Andric OutputSection *os = record->getOutputSection(); 1060b57cec5SDimitry Andric uint64_t offs = os->getFileOff() + (record->getRVA() - os->getRVA()); 1070b57cec5SDimitry Andric fillEntry(d, COFF::IMAGE_DEBUG_TYPE_CODEVIEW, record->getSize(), 1080b57cec5SDimitry Andric record->getRVA(), offs); 1090b57cec5SDimitry Andric ++d; 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric if (writeRepro) { 1130b57cec5SDimitry Andric // FIXME: The COFF spec allows either a 0-sized entry to just say 1140b57cec5SDimitry Andric // "the timestamp field is really a hash", or a 4-byte size field 1150b57cec5SDimitry Andric // followed by that many bytes containing a longer hash (with the 1160b57cec5SDimitry Andric // lowest 4 bytes usually being the timestamp in little-endian order). 1170b57cec5SDimitry Andric // Consider storing the full 8 bytes computed by xxHash64 here. 1180b57cec5SDimitry Andric fillEntry(d, COFF::IMAGE_DEBUG_TYPE_REPRO, 0, 0, 0); 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric } 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric void setTimeDateStamp(uint32_t timeDateStamp) { 1230b57cec5SDimitry Andric for (support::ulittle32_t *tds : timeDateStamps) 1240b57cec5SDimitry Andric *tds = timeDateStamp; 1250b57cec5SDimitry Andric } 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric private: 1280b57cec5SDimitry Andric void fillEntry(debug_directory *d, COFF::DebugType debugType, size_t size, 1290b57cec5SDimitry Andric uint64_t rva, uint64_t offs) const { 1300b57cec5SDimitry Andric d->Characteristics = 0; 1310b57cec5SDimitry Andric d->TimeDateStamp = 0; 1320b57cec5SDimitry Andric d->MajorVersion = 0; 1330b57cec5SDimitry Andric d->MinorVersion = 0; 1340b57cec5SDimitry Andric d->Type = debugType; 1350b57cec5SDimitry Andric d->SizeOfData = size; 1360b57cec5SDimitry Andric d->AddressOfRawData = rva; 1370b57cec5SDimitry Andric d->PointerToRawData = offs; 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric timeDateStamps.push_back(&d->TimeDateStamp); 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric mutable std::vector<support::ulittle32_t *> timeDateStamps; 1430b57cec5SDimitry Andric const std::vector<Chunk *> &records; 1440b57cec5SDimitry Andric bool writeRepro; 1450b57cec5SDimitry Andric }; 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric class CVDebugRecordChunk : public NonSectionChunk { 1480b57cec5SDimitry Andric public: 1490b57cec5SDimitry Andric size_t getSize() const override { 1500b57cec5SDimitry Andric return sizeof(codeview::DebugInfo) + 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)); 1600b57cec5SDimitry Andric if (!config->pdbAltPath.empty()) 1610b57cec5SDimitry Andric memcpy(p, config->pdbAltPath.data(), config->pdbAltPath.size()); 1620b57cec5SDimitry Andric p[config->pdbAltPath.size()] = '\0'; 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric mutable codeview::DebugInfo *buildId = nullptr; 1660b57cec5SDimitry Andric }; 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric // PartialSection represents a group of chunks that contribute to an 1690b57cec5SDimitry Andric // OutputSection. Collating a collection of PartialSections of same name and 1700b57cec5SDimitry Andric // characteristics constitutes the OutputSection. 1710b57cec5SDimitry Andric class PartialSectionKey { 1720b57cec5SDimitry Andric public: 1730b57cec5SDimitry Andric StringRef name; 1740b57cec5SDimitry Andric unsigned characteristics; 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric bool operator<(const PartialSectionKey &other) const { 1770b57cec5SDimitry Andric int c = name.compare(other.name); 1780b57cec5SDimitry Andric if (c == 1) 1790b57cec5SDimitry Andric return false; 1800b57cec5SDimitry Andric if (c == 0) 1810b57cec5SDimitry Andric return characteristics < other.characteristics; 1820b57cec5SDimitry Andric return true; 1830b57cec5SDimitry Andric } 1840b57cec5SDimitry Andric }; 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric // The writer writes a SymbolTable result to a file. 1870b57cec5SDimitry Andric class Writer { 1880b57cec5SDimitry Andric public: 1890b57cec5SDimitry Andric Writer() : buffer(errorHandler().outputBuffer) {} 1900b57cec5SDimitry Andric void run(); 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric private: 1930b57cec5SDimitry Andric void createSections(); 1940b57cec5SDimitry Andric void createMiscChunks(); 1950b57cec5SDimitry Andric void createImportTables(); 1960b57cec5SDimitry Andric void appendImportThunks(); 1970b57cec5SDimitry Andric void locateImportTables(); 1980b57cec5SDimitry Andric void createExportTable(); 1990b57cec5SDimitry Andric void mergeSections(); 2000b57cec5SDimitry Andric void removeUnusedSections(); 2010b57cec5SDimitry Andric void assignAddresses(); 2020b57cec5SDimitry Andric void finalizeAddresses(); 2030b57cec5SDimitry Andric void removeEmptySections(); 2040b57cec5SDimitry Andric void assignOutputSectionIndices(); 2050b57cec5SDimitry Andric void createSymbolAndStringTable(); 2060b57cec5SDimitry Andric void openFile(StringRef outputPath); 2070b57cec5SDimitry Andric template <typename PEHeaderTy> void writeHeader(); 2080b57cec5SDimitry Andric void createSEHTable(); 2090b57cec5SDimitry Andric void createRuntimePseudoRelocs(); 2100b57cec5SDimitry Andric void insertCtorDtorSymbols(); 2110b57cec5SDimitry Andric void createGuardCFTables(); 2120b57cec5SDimitry Andric void markSymbolsForRVATable(ObjFile *file, 2130b57cec5SDimitry Andric ArrayRef<SectionChunk *> symIdxChunks, 2140b57cec5SDimitry Andric SymbolRVASet &tableSymbols); 2150b57cec5SDimitry Andric void maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym, 2160b57cec5SDimitry Andric StringRef countSym); 2170b57cec5SDimitry Andric void setSectionPermissions(); 2180b57cec5SDimitry Andric void writeSections(); 2190b57cec5SDimitry Andric void writeBuildId(); 2200b57cec5SDimitry Andric void sortExceptionTable(); 2210b57cec5SDimitry Andric void sortCRTSectionChunks(std::vector<Chunk *> &chunks); 2220b57cec5SDimitry Andric void addSyntheticIdata(); 2230b57cec5SDimitry Andric void fixPartialSectionChars(StringRef name, uint32_t chars); 2240b57cec5SDimitry Andric bool fixGnuImportChunks(); 2250b57cec5SDimitry Andric PartialSection *createPartialSection(StringRef name, uint32_t outChars); 2260b57cec5SDimitry Andric PartialSection *findPartialSection(StringRef name, uint32_t outChars); 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric llvm::Optional<coff_symbol16> createSymbol(Defined *d); 2290b57cec5SDimitry Andric size_t addEntryToStringTable(StringRef str); 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric OutputSection *findSection(StringRef name); 2320b57cec5SDimitry Andric void addBaserels(); 2330b57cec5SDimitry Andric void addBaserelBlocks(std::vector<Baserel> &v); 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric uint32_t getSizeOfInitializedData(); 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric std::unique_ptr<FileOutputBuffer> &buffer; 2380b57cec5SDimitry Andric std::map<PartialSectionKey, PartialSection *> partialSections; 2390b57cec5SDimitry Andric std::vector<char> strtab; 2400b57cec5SDimitry Andric std::vector<llvm::object::coff_symbol16> outputSymtab; 2410b57cec5SDimitry Andric IdataContents idata; 2420b57cec5SDimitry Andric Chunk *importTableStart = nullptr; 2430b57cec5SDimitry Andric uint64_t importTableSize = 0; 244*85868e8aSDimitry Andric Chunk *edataStart = nullptr; 245*85868e8aSDimitry Andric Chunk *edataEnd = nullptr; 2460b57cec5SDimitry Andric Chunk *iatStart = nullptr; 2470b57cec5SDimitry Andric uint64_t iatSize = 0; 2480b57cec5SDimitry Andric DelayLoadContents delayIdata; 2490b57cec5SDimitry Andric EdataContents edata; 2500b57cec5SDimitry Andric bool setNoSEHCharacteristic = false; 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric DebugDirectoryChunk *debugDirectory = nullptr; 2530b57cec5SDimitry Andric std::vector<Chunk *> debugRecords; 2540b57cec5SDimitry Andric CVDebugRecordChunk *buildId = nullptr; 2550b57cec5SDimitry Andric ArrayRef<uint8_t> sectionTable; 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric uint64_t fileSize; 2580b57cec5SDimitry Andric uint32_t pointerToSymbolTable = 0; 2590b57cec5SDimitry Andric uint64_t sizeOfImage; 2600b57cec5SDimitry Andric uint64_t sizeOfHeaders; 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric OutputSection *textSec; 2630b57cec5SDimitry Andric OutputSection *rdataSec; 2640b57cec5SDimitry Andric OutputSection *buildidSec; 2650b57cec5SDimitry Andric OutputSection *dataSec; 2660b57cec5SDimitry Andric OutputSection *pdataSec; 2670b57cec5SDimitry Andric OutputSection *idataSec; 2680b57cec5SDimitry Andric OutputSection *edataSec; 2690b57cec5SDimitry Andric OutputSection *didatSec; 2700b57cec5SDimitry Andric OutputSection *rsrcSec; 2710b57cec5SDimitry Andric OutputSection *relocSec; 2720b57cec5SDimitry Andric OutputSection *ctorsSec; 2730b57cec5SDimitry Andric OutputSection *dtorsSec; 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric // The first and last .pdata sections in the output file. 2760b57cec5SDimitry Andric // 2770b57cec5SDimitry Andric // We need to keep track of the location of .pdata in whichever section it 2780b57cec5SDimitry Andric // gets merged into so that we can sort its contents and emit a correct data 2790b57cec5SDimitry Andric // directory entry for the exception table. This is also the case for some 2800b57cec5SDimitry Andric // other sections (such as .edata) but because the contents of those sections 2810b57cec5SDimitry Andric // are entirely linker-generated we can keep track of their locations using 2820b57cec5SDimitry Andric // the chunks that the linker creates. All .pdata chunks come from input 2830b57cec5SDimitry Andric // files, so we need to keep track of them separately. 2840b57cec5SDimitry Andric Chunk *firstPdata = nullptr; 2850b57cec5SDimitry Andric Chunk *lastPdata; 2860b57cec5SDimitry Andric }; 2870b57cec5SDimitry Andric } // anonymous namespace 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric static Timer codeLayoutTimer("Code Layout", Timer::root()); 2900b57cec5SDimitry Andric static Timer diskCommitTimer("Commit Output File", Timer::root()); 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric void writeResult() { Writer().run(); } 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric void OutputSection::addChunk(Chunk *c) { 2950b57cec5SDimitry Andric chunks.push_back(c); 2960b57cec5SDimitry Andric } 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric void OutputSection::insertChunkAtStart(Chunk *c) { 2990b57cec5SDimitry Andric chunks.insert(chunks.begin(), c); 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric void OutputSection::setPermissions(uint32_t c) { 3030b57cec5SDimitry Andric header.Characteristics &= ~permMask; 3040b57cec5SDimitry Andric header.Characteristics |= c; 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric void OutputSection::merge(OutputSection *other) { 3080b57cec5SDimitry Andric chunks.insert(chunks.end(), other->chunks.begin(), other->chunks.end()); 3090b57cec5SDimitry Andric other->chunks.clear(); 3100b57cec5SDimitry Andric contribSections.insert(contribSections.end(), other->contribSections.begin(), 3110b57cec5SDimitry Andric other->contribSections.end()); 3120b57cec5SDimitry Andric other->contribSections.clear(); 3130b57cec5SDimitry Andric } 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric // Write the section header to a given buffer. 3160b57cec5SDimitry Andric void OutputSection::writeHeaderTo(uint8_t *buf) { 3170b57cec5SDimitry Andric auto *hdr = reinterpret_cast<coff_section *>(buf); 3180b57cec5SDimitry Andric *hdr = header; 3190b57cec5SDimitry Andric if (stringTableOff) { 3200b57cec5SDimitry Andric // If name is too long, write offset into the string table as a name. 3210b57cec5SDimitry Andric sprintf(hdr->Name, "/%d", stringTableOff); 3220b57cec5SDimitry Andric } else { 3230b57cec5SDimitry Andric assert(!config->debug || name.size() <= COFF::NameSize || 3240b57cec5SDimitry Andric (hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0); 3250b57cec5SDimitry Andric strncpy(hdr->Name, name.data(), 3260b57cec5SDimitry Andric std::min(name.size(), (size_t)COFF::NameSize)); 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric } 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric void OutputSection::addContributingPartialSection(PartialSection *sec) { 3310b57cec5SDimitry Andric contribSections.push_back(sec); 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric // Check whether the target address S is in range from a relocation 3350b57cec5SDimitry Andric // of type relType at address P. 3360b57cec5SDimitry Andric static bool isInRange(uint16_t relType, uint64_t s, uint64_t p, int margin) { 3370b57cec5SDimitry Andric if (config->machine == ARMNT) { 3380b57cec5SDimitry Andric int64_t diff = AbsoluteDifference(s, p + 4) + margin; 3390b57cec5SDimitry Andric switch (relType) { 3400b57cec5SDimitry Andric case IMAGE_REL_ARM_BRANCH20T: 3410b57cec5SDimitry Andric return isInt<21>(diff); 3420b57cec5SDimitry Andric case IMAGE_REL_ARM_BRANCH24T: 3430b57cec5SDimitry Andric case IMAGE_REL_ARM_BLX23T: 3440b57cec5SDimitry Andric return isInt<25>(diff); 3450b57cec5SDimitry Andric default: 3460b57cec5SDimitry Andric return true; 3470b57cec5SDimitry Andric } 3480b57cec5SDimitry Andric } else if (config->machine == ARM64) { 3490b57cec5SDimitry Andric int64_t diff = AbsoluteDifference(s, p) + margin; 3500b57cec5SDimitry Andric switch (relType) { 3510b57cec5SDimitry Andric case IMAGE_REL_ARM64_BRANCH26: 3520b57cec5SDimitry Andric return isInt<28>(diff); 3530b57cec5SDimitry Andric case IMAGE_REL_ARM64_BRANCH19: 3540b57cec5SDimitry Andric return isInt<21>(diff); 3550b57cec5SDimitry Andric case IMAGE_REL_ARM64_BRANCH14: 3560b57cec5SDimitry Andric return isInt<16>(diff); 3570b57cec5SDimitry Andric default: 3580b57cec5SDimitry Andric return true; 3590b57cec5SDimitry Andric } 3600b57cec5SDimitry Andric } else { 3610b57cec5SDimitry Andric llvm_unreachable("Unexpected architecture"); 3620b57cec5SDimitry Andric } 3630b57cec5SDimitry Andric } 3640b57cec5SDimitry Andric 3650b57cec5SDimitry Andric // Return the last thunk for the given target if it is in range, 3660b57cec5SDimitry Andric // or create a new one. 3670b57cec5SDimitry Andric static std::pair<Defined *, bool> 3680b57cec5SDimitry Andric getThunk(DenseMap<uint64_t, Defined *> &lastThunks, Defined *target, uint64_t p, 3690b57cec5SDimitry Andric uint16_t type, int margin) { 3700b57cec5SDimitry Andric Defined *&lastThunk = lastThunks[target->getRVA()]; 3710b57cec5SDimitry Andric if (lastThunk && isInRange(type, lastThunk->getRVA(), p, margin)) 3720b57cec5SDimitry Andric return {lastThunk, false}; 3730b57cec5SDimitry Andric Chunk *c; 3740b57cec5SDimitry Andric switch (config->machine) { 3750b57cec5SDimitry Andric case ARMNT: 3760b57cec5SDimitry Andric c = make<RangeExtensionThunkARM>(target); 3770b57cec5SDimitry Andric break; 3780b57cec5SDimitry Andric case ARM64: 3790b57cec5SDimitry Andric c = make<RangeExtensionThunkARM64>(target); 3800b57cec5SDimitry Andric break; 3810b57cec5SDimitry Andric default: 3820b57cec5SDimitry Andric llvm_unreachable("Unexpected architecture"); 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric Defined *d = make<DefinedSynthetic>("", c); 3850b57cec5SDimitry Andric lastThunk = d; 3860b57cec5SDimitry Andric return {d, true}; 3870b57cec5SDimitry Andric } 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric // This checks all relocations, and for any relocation which isn't in range 3900b57cec5SDimitry Andric // it adds a thunk after the section chunk that contains the relocation. 3910b57cec5SDimitry Andric // If the latest thunk for the specific target is in range, that is used 3920b57cec5SDimitry Andric // instead of creating a new thunk. All range checks are done with the 3930b57cec5SDimitry Andric // specified margin, to make sure that relocations that originally are in 3940b57cec5SDimitry Andric // range, but only barely, also get thunks - in case other added thunks makes 3950b57cec5SDimitry Andric // the target go out of range. 3960b57cec5SDimitry Andric // 3970b57cec5SDimitry Andric // After adding thunks, we verify that all relocations are in range (with 3980b57cec5SDimitry Andric // no extra margin requirements). If this failed, we restart (throwing away 3990b57cec5SDimitry Andric // the previously created thunks) and retry with a wider margin. 4000b57cec5SDimitry Andric static bool createThunks(OutputSection *os, int margin) { 4010b57cec5SDimitry Andric bool addressesChanged = false; 4020b57cec5SDimitry Andric DenseMap<uint64_t, Defined *> lastThunks; 4030b57cec5SDimitry Andric DenseMap<std::pair<ObjFile *, Defined *>, uint32_t> thunkSymtabIndices; 4040b57cec5SDimitry Andric size_t thunksSize = 0; 4050b57cec5SDimitry Andric // Recheck Chunks.size() each iteration, since we can insert more 4060b57cec5SDimitry Andric // elements into it. 4070b57cec5SDimitry Andric for (size_t i = 0; i != os->chunks.size(); ++i) { 4080b57cec5SDimitry Andric SectionChunk *sc = dyn_cast_or_null<SectionChunk>(os->chunks[i]); 4090b57cec5SDimitry Andric if (!sc) 4100b57cec5SDimitry Andric continue; 4110b57cec5SDimitry Andric size_t thunkInsertionSpot = i + 1; 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric // Try to get a good enough estimate of where new thunks will be placed. 4140b57cec5SDimitry Andric // Offset this by the size of the new thunks added so far, to make the 4150b57cec5SDimitry Andric // estimate slightly better. 4160b57cec5SDimitry Andric size_t thunkInsertionRVA = sc->getRVA() + sc->getSize() + thunksSize; 4170b57cec5SDimitry Andric ObjFile *file = sc->file; 4180b57cec5SDimitry Andric std::vector<std::pair<uint32_t, uint32_t>> relocReplacements; 4190b57cec5SDimitry Andric ArrayRef<coff_relocation> originalRelocs = 4200b57cec5SDimitry Andric file->getCOFFObj()->getRelocations(sc->header); 4210b57cec5SDimitry Andric for (size_t j = 0, e = originalRelocs.size(); j < e; ++j) { 4220b57cec5SDimitry Andric const coff_relocation &rel = originalRelocs[j]; 4230b57cec5SDimitry Andric Symbol *relocTarget = file->getSymbol(rel.SymbolTableIndex); 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric // The estimate of the source address P should be pretty accurate, 4260b57cec5SDimitry Andric // but we don't know whether the target Symbol address should be 4270b57cec5SDimitry Andric // offset by thunksSize or not (or by some of thunksSize but not all of 4280b57cec5SDimitry Andric // it), giving us some uncertainty once we have added one thunk. 4290b57cec5SDimitry Andric uint64_t p = sc->getRVA() + rel.VirtualAddress + thunksSize; 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric Defined *sym = dyn_cast_or_null<Defined>(relocTarget); 4320b57cec5SDimitry Andric if (!sym) 4330b57cec5SDimitry Andric continue; 4340b57cec5SDimitry Andric 4350b57cec5SDimitry Andric uint64_t s = sym->getRVA(); 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric if (isInRange(rel.Type, s, p, margin)) 4380b57cec5SDimitry Andric continue; 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric // If the target isn't in range, hook it up to an existing or new 4410b57cec5SDimitry Andric // thunk. 4420b57cec5SDimitry Andric Defined *thunk; 4430b57cec5SDimitry Andric bool wasNew; 4440b57cec5SDimitry Andric std::tie(thunk, wasNew) = getThunk(lastThunks, sym, p, rel.Type, margin); 4450b57cec5SDimitry Andric if (wasNew) { 4460b57cec5SDimitry Andric Chunk *thunkChunk = thunk->getChunk(); 4470b57cec5SDimitry Andric thunkChunk->setRVA( 4480b57cec5SDimitry Andric thunkInsertionRVA); // Estimate of where it will be located. 4490b57cec5SDimitry Andric os->chunks.insert(os->chunks.begin() + thunkInsertionSpot, thunkChunk); 4500b57cec5SDimitry Andric thunkInsertionSpot++; 4510b57cec5SDimitry Andric thunksSize += thunkChunk->getSize(); 4520b57cec5SDimitry Andric thunkInsertionRVA += thunkChunk->getSize(); 4530b57cec5SDimitry Andric addressesChanged = true; 4540b57cec5SDimitry Andric } 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric // To redirect the relocation, add a symbol to the parent object file's 4570b57cec5SDimitry Andric // symbol table, and replace the relocation symbol table index with the 4580b57cec5SDimitry Andric // new index. 4590b57cec5SDimitry Andric auto insertion = thunkSymtabIndices.insert({{file, thunk}, ~0U}); 4600b57cec5SDimitry Andric uint32_t &thunkSymbolIndex = insertion.first->second; 4610b57cec5SDimitry Andric if (insertion.second) 4620b57cec5SDimitry Andric thunkSymbolIndex = file->addRangeThunkSymbol(thunk); 4630b57cec5SDimitry Andric relocReplacements.push_back({j, thunkSymbolIndex}); 4640b57cec5SDimitry Andric } 4650b57cec5SDimitry Andric 4660b57cec5SDimitry Andric // Get a writable copy of this section's relocations so they can be 4670b57cec5SDimitry Andric // modified. If the relocations point into the object file, allocate new 4680b57cec5SDimitry Andric // memory. Otherwise, this must be previously allocated memory that can be 4690b57cec5SDimitry Andric // modified in place. 4700b57cec5SDimitry Andric ArrayRef<coff_relocation> curRelocs = sc->getRelocs(); 4710b57cec5SDimitry Andric MutableArrayRef<coff_relocation> newRelocs; 4720b57cec5SDimitry Andric if (originalRelocs.data() == curRelocs.data()) { 4730b57cec5SDimitry Andric newRelocs = makeMutableArrayRef( 4740b57cec5SDimitry Andric bAlloc.Allocate<coff_relocation>(originalRelocs.size()), 4750b57cec5SDimitry Andric originalRelocs.size()); 4760b57cec5SDimitry Andric } else { 4770b57cec5SDimitry Andric newRelocs = makeMutableArrayRef( 4780b57cec5SDimitry Andric const_cast<coff_relocation *>(curRelocs.data()), curRelocs.size()); 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric // Copy each relocation, but replace the symbol table indices which need 4820b57cec5SDimitry Andric // thunks. 4830b57cec5SDimitry Andric auto nextReplacement = relocReplacements.begin(); 4840b57cec5SDimitry Andric auto endReplacement = relocReplacements.end(); 4850b57cec5SDimitry Andric for (size_t i = 0, e = originalRelocs.size(); i != e; ++i) { 4860b57cec5SDimitry Andric newRelocs[i] = originalRelocs[i]; 4870b57cec5SDimitry Andric if (nextReplacement != endReplacement && nextReplacement->first == i) { 4880b57cec5SDimitry Andric newRelocs[i].SymbolTableIndex = nextReplacement->second; 4890b57cec5SDimitry Andric ++nextReplacement; 4900b57cec5SDimitry Andric } 4910b57cec5SDimitry Andric } 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric sc->setRelocs(newRelocs); 4940b57cec5SDimitry Andric } 4950b57cec5SDimitry Andric return addressesChanged; 4960b57cec5SDimitry Andric } 4970b57cec5SDimitry Andric 4980b57cec5SDimitry Andric // Verify that all relocations are in range, with no extra margin requirements. 4990b57cec5SDimitry Andric static bool verifyRanges(const std::vector<Chunk *> chunks) { 5000b57cec5SDimitry Andric for (Chunk *c : chunks) { 5010b57cec5SDimitry Andric SectionChunk *sc = dyn_cast_or_null<SectionChunk>(c); 5020b57cec5SDimitry Andric if (!sc) 5030b57cec5SDimitry Andric continue; 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric ArrayRef<coff_relocation> relocs = sc->getRelocs(); 5060b57cec5SDimitry Andric for (size_t j = 0, e = relocs.size(); j < e; ++j) { 5070b57cec5SDimitry Andric const coff_relocation &rel = relocs[j]; 5080b57cec5SDimitry Andric Symbol *relocTarget = sc->file->getSymbol(rel.SymbolTableIndex); 5090b57cec5SDimitry Andric 5100b57cec5SDimitry Andric Defined *sym = dyn_cast_or_null<Defined>(relocTarget); 5110b57cec5SDimitry Andric if (!sym) 5120b57cec5SDimitry Andric continue; 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric uint64_t p = sc->getRVA() + rel.VirtualAddress; 5150b57cec5SDimitry Andric uint64_t s = sym->getRVA(); 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric if (!isInRange(rel.Type, s, p, 0)) 5180b57cec5SDimitry Andric return false; 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric } 5210b57cec5SDimitry Andric return true; 5220b57cec5SDimitry Andric } 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andric // Assign addresses and add thunks if necessary. 5250b57cec5SDimitry Andric void Writer::finalizeAddresses() { 5260b57cec5SDimitry Andric assignAddresses(); 5270b57cec5SDimitry Andric if (config->machine != ARMNT && config->machine != ARM64) 5280b57cec5SDimitry Andric return; 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric size_t origNumChunks = 0; 5310b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 5320b57cec5SDimitry Andric sec->origChunks = sec->chunks; 5330b57cec5SDimitry Andric origNumChunks += sec->chunks.size(); 5340b57cec5SDimitry Andric } 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric int pass = 0; 5370b57cec5SDimitry Andric int margin = 1024 * 100; 5380b57cec5SDimitry Andric while (true) { 5390b57cec5SDimitry Andric // First check whether we need thunks at all, or if the previous pass of 5400b57cec5SDimitry Andric // adding them turned out ok. 5410b57cec5SDimitry Andric bool rangesOk = true; 5420b57cec5SDimitry Andric size_t numChunks = 0; 5430b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 5440b57cec5SDimitry Andric if (!verifyRanges(sec->chunks)) { 5450b57cec5SDimitry Andric rangesOk = false; 5460b57cec5SDimitry Andric break; 5470b57cec5SDimitry Andric } 5480b57cec5SDimitry Andric numChunks += sec->chunks.size(); 5490b57cec5SDimitry Andric } 5500b57cec5SDimitry Andric if (rangesOk) { 5510b57cec5SDimitry Andric if (pass > 0) 5520b57cec5SDimitry Andric log("Added " + Twine(numChunks - origNumChunks) + " thunks with " + 5530b57cec5SDimitry Andric "margin " + Twine(margin) + " in " + Twine(pass) + " passes"); 5540b57cec5SDimitry Andric return; 5550b57cec5SDimitry Andric } 5560b57cec5SDimitry Andric 5570b57cec5SDimitry Andric if (pass >= 10) 5580b57cec5SDimitry Andric fatal("adding thunks hasn't converged after " + Twine(pass) + " passes"); 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric if (pass > 0) { 5610b57cec5SDimitry Andric // If the previous pass didn't work out, reset everything back to the 5620b57cec5SDimitry Andric // original conditions before retrying with a wider margin. This should 5630b57cec5SDimitry Andric // ideally never happen under real circumstances. 5640b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 5650b57cec5SDimitry Andric sec->chunks = sec->origChunks; 5660b57cec5SDimitry Andric margin *= 2; 5670b57cec5SDimitry Andric } 5680b57cec5SDimitry Andric 5690b57cec5SDimitry Andric // Try adding thunks everywhere where it is needed, with a margin 5700b57cec5SDimitry Andric // to avoid things going out of range due to the added thunks. 5710b57cec5SDimitry Andric bool addressesChanged = false; 5720b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 5730b57cec5SDimitry Andric addressesChanged |= createThunks(sec, margin); 5740b57cec5SDimitry Andric // If the verification above thought we needed thunks, we should have 5750b57cec5SDimitry Andric // added some. 5760b57cec5SDimitry Andric assert(addressesChanged); 5770b57cec5SDimitry Andric 5780b57cec5SDimitry Andric // Recalculate the layout for the whole image (and verify the ranges at 5790b57cec5SDimitry Andric // the start of the next round). 5800b57cec5SDimitry Andric assignAddresses(); 5810b57cec5SDimitry Andric 5820b57cec5SDimitry Andric pass++; 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric } 5850b57cec5SDimitry Andric 5860b57cec5SDimitry Andric // The main function of the writer. 5870b57cec5SDimitry Andric void Writer::run() { 5880b57cec5SDimitry Andric ScopedTimer t1(codeLayoutTimer); 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric createImportTables(); 5910b57cec5SDimitry Andric createSections(); 5920b57cec5SDimitry Andric createMiscChunks(); 5930b57cec5SDimitry Andric appendImportThunks(); 5940b57cec5SDimitry Andric createExportTable(); 5950b57cec5SDimitry Andric mergeSections(); 5960b57cec5SDimitry Andric removeUnusedSections(); 5970b57cec5SDimitry Andric finalizeAddresses(); 5980b57cec5SDimitry Andric removeEmptySections(); 5990b57cec5SDimitry Andric assignOutputSectionIndices(); 6000b57cec5SDimitry Andric setSectionPermissions(); 6010b57cec5SDimitry Andric createSymbolAndStringTable(); 6020b57cec5SDimitry Andric 6030b57cec5SDimitry Andric if (fileSize > UINT32_MAX) 6040b57cec5SDimitry Andric fatal("image size (" + Twine(fileSize) + ") " + 6050b57cec5SDimitry Andric "exceeds maximum allowable size (" + Twine(UINT32_MAX) + ")"); 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric openFile(config->outputFile); 6080b57cec5SDimitry Andric if (config->is64()) { 6090b57cec5SDimitry Andric writeHeader<pe32plus_header>(); 6100b57cec5SDimitry Andric } else { 6110b57cec5SDimitry Andric writeHeader<pe32_header>(); 6120b57cec5SDimitry Andric } 6130b57cec5SDimitry Andric writeSections(); 6140b57cec5SDimitry Andric sortExceptionTable(); 6150b57cec5SDimitry Andric 6160b57cec5SDimitry Andric t1.stop(); 6170b57cec5SDimitry Andric 6180b57cec5SDimitry Andric if (!config->pdbPath.empty() && config->debug) { 6190b57cec5SDimitry Andric assert(buildId); 6200b57cec5SDimitry Andric createPDB(symtab, outputSections, sectionTable, buildId->buildId); 6210b57cec5SDimitry Andric } 6220b57cec5SDimitry Andric writeBuildId(); 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric writeMapFile(outputSections); 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric if (errorCount()) 6270b57cec5SDimitry Andric return; 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric ScopedTimer t2(diskCommitTimer); 6300b57cec5SDimitry Andric if (auto e = buffer->commit()) 6310b57cec5SDimitry Andric fatal("failed to write the output file: " + toString(std::move(e))); 6320b57cec5SDimitry Andric } 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric static StringRef getOutputSectionName(StringRef name) { 6350b57cec5SDimitry Andric StringRef s = name.split('$').first; 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric // Treat a later period as a separator for MinGW, for sections like 6380b57cec5SDimitry Andric // ".ctors.01234". 6390b57cec5SDimitry Andric return s.substr(0, s.find('.', 1)); 6400b57cec5SDimitry Andric } 6410b57cec5SDimitry Andric 6420b57cec5SDimitry Andric // For /order. 6430b57cec5SDimitry Andric static void sortBySectionOrder(std::vector<Chunk *> &chunks) { 6440b57cec5SDimitry Andric auto getPriority = [](const Chunk *c) { 6450b57cec5SDimitry Andric if (auto *sec = dyn_cast<SectionChunk>(c)) 6460b57cec5SDimitry Andric if (sec->sym) 6470b57cec5SDimitry Andric return config->order.lookup(sec->sym->getName()); 6480b57cec5SDimitry Andric return 0; 6490b57cec5SDimitry Andric }; 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric llvm::stable_sort(chunks, [=](const Chunk *a, const Chunk *b) { 6520b57cec5SDimitry Andric return getPriority(a) < getPriority(b); 6530b57cec5SDimitry Andric }); 6540b57cec5SDimitry Andric } 6550b57cec5SDimitry Andric 6560b57cec5SDimitry Andric // Change the characteristics of existing PartialSections that belong to the 6570b57cec5SDimitry Andric // section Name to Chars. 6580b57cec5SDimitry Andric void Writer::fixPartialSectionChars(StringRef name, uint32_t chars) { 6590b57cec5SDimitry Andric for (auto it : partialSections) { 6600b57cec5SDimitry Andric PartialSection *pSec = it.second; 6610b57cec5SDimitry Andric StringRef curName = pSec->name; 6620b57cec5SDimitry Andric if (!curName.consume_front(name) || 6630b57cec5SDimitry Andric (!curName.empty() && !curName.startswith("$"))) 6640b57cec5SDimitry Andric continue; 6650b57cec5SDimitry Andric if (pSec->characteristics == chars) 6660b57cec5SDimitry Andric continue; 6670b57cec5SDimitry Andric PartialSection *destSec = createPartialSection(pSec->name, chars); 6680b57cec5SDimitry Andric destSec->chunks.insert(destSec->chunks.end(), pSec->chunks.begin(), 6690b57cec5SDimitry Andric pSec->chunks.end()); 6700b57cec5SDimitry Andric pSec->chunks.clear(); 6710b57cec5SDimitry Andric } 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric 6740b57cec5SDimitry Andric // Sort concrete section chunks from GNU import libraries. 6750b57cec5SDimitry Andric // 6760b57cec5SDimitry Andric // GNU binutils doesn't use short import files, but instead produces import 6770b57cec5SDimitry Andric // libraries that consist of object files, with section chunks for the .idata$* 6780b57cec5SDimitry Andric // sections. These are linked just as regular static libraries. Each import 6790b57cec5SDimitry Andric // library consists of one header object, one object file for every imported 6800b57cec5SDimitry Andric // symbol, and one trailer object. In order for the .idata tables/lists to 6810b57cec5SDimitry Andric // be formed correctly, the section chunks within each .idata$* section need 6820b57cec5SDimitry Andric // to be grouped by library, and sorted alphabetically within each library 6830b57cec5SDimitry Andric // (which makes sure the header comes first and the trailer last). 6840b57cec5SDimitry Andric bool Writer::fixGnuImportChunks() { 6850b57cec5SDimitry Andric uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ; 6860b57cec5SDimitry Andric 6870b57cec5SDimitry Andric // Make sure all .idata$* section chunks are mapped as RDATA in order to 6880b57cec5SDimitry Andric // be sorted into the same sections as our own synthesized .idata chunks. 6890b57cec5SDimitry Andric fixPartialSectionChars(".idata", rdata); 6900b57cec5SDimitry Andric 6910b57cec5SDimitry Andric bool hasIdata = false; 6920b57cec5SDimitry Andric // Sort all .idata$* chunks, grouping chunks from the same library, 6930b57cec5SDimitry Andric // with alphabetical ordering of the object fils within a library. 6940b57cec5SDimitry Andric for (auto it : partialSections) { 6950b57cec5SDimitry Andric PartialSection *pSec = it.second; 6960b57cec5SDimitry Andric if (!pSec->name.startswith(".idata")) 6970b57cec5SDimitry Andric continue; 6980b57cec5SDimitry Andric 6990b57cec5SDimitry Andric if (!pSec->chunks.empty()) 7000b57cec5SDimitry Andric hasIdata = true; 7010b57cec5SDimitry Andric llvm::stable_sort(pSec->chunks, [&](Chunk *s, Chunk *t) { 7020b57cec5SDimitry Andric SectionChunk *sc1 = dyn_cast_or_null<SectionChunk>(s); 7030b57cec5SDimitry Andric SectionChunk *sc2 = dyn_cast_or_null<SectionChunk>(t); 7040b57cec5SDimitry Andric if (!sc1 || !sc2) { 7050b57cec5SDimitry Andric // if SC1, order them ascending. If SC2 or both null, 7060b57cec5SDimitry Andric // S is not less than T. 7070b57cec5SDimitry Andric return sc1 != nullptr; 7080b57cec5SDimitry Andric } 7090b57cec5SDimitry Andric // Make a string with "libraryname/objectfile" for sorting, achieving 7100b57cec5SDimitry Andric // both grouping by library and sorting of objects within a library, 7110b57cec5SDimitry Andric // at once. 7120b57cec5SDimitry Andric std::string key1 = 7130b57cec5SDimitry Andric (sc1->file->parentName + "/" + sc1->file->getName()).str(); 7140b57cec5SDimitry Andric std::string key2 = 7150b57cec5SDimitry Andric (sc2->file->parentName + "/" + sc2->file->getName()).str(); 7160b57cec5SDimitry Andric return key1 < key2; 7170b57cec5SDimitry Andric }); 7180b57cec5SDimitry Andric } 7190b57cec5SDimitry Andric return hasIdata; 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric 7220b57cec5SDimitry Andric // Add generated idata chunks, for imported symbols and DLLs, and a 7230b57cec5SDimitry Andric // terminator in .idata$2. 7240b57cec5SDimitry Andric void Writer::addSyntheticIdata() { 7250b57cec5SDimitry Andric uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ; 7260b57cec5SDimitry Andric idata.create(); 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric // Add the .idata content in the right section groups, to allow 7290b57cec5SDimitry Andric // chunks from other linked in object files to be grouped together. 7300b57cec5SDimitry Andric // See Microsoft PE/COFF spec 5.4 for details. 7310b57cec5SDimitry Andric auto add = [&](StringRef n, std::vector<Chunk *> &v) { 7320b57cec5SDimitry Andric PartialSection *pSec = createPartialSection(n, rdata); 7330b57cec5SDimitry Andric pSec->chunks.insert(pSec->chunks.end(), v.begin(), v.end()); 7340b57cec5SDimitry Andric }; 7350b57cec5SDimitry Andric 7360b57cec5SDimitry Andric // The loader assumes a specific order of data. 7370b57cec5SDimitry Andric // Add each type in the correct order. 7380b57cec5SDimitry Andric add(".idata$2", idata.dirs); 7390b57cec5SDimitry Andric add(".idata$4", idata.lookups); 7400b57cec5SDimitry Andric add(".idata$5", idata.addresses); 741*85868e8aSDimitry Andric if (!idata.hints.empty()) 7420b57cec5SDimitry Andric add(".idata$6", idata.hints); 7430b57cec5SDimitry Andric add(".idata$7", idata.dllNames); 7440b57cec5SDimitry Andric } 7450b57cec5SDimitry Andric 7460b57cec5SDimitry Andric // Locate the first Chunk and size of the import directory list and the 7470b57cec5SDimitry Andric // IAT. 7480b57cec5SDimitry Andric void Writer::locateImportTables() { 7490b57cec5SDimitry Andric uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ; 7500b57cec5SDimitry Andric 7510b57cec5SDimitry Andric if (PartialSection *importDirs = findPartialSection(".idata$2", rdata)) { 7520b57cec5SDimitry Andric if (!importDirs->chunks.empty()) 7530b57cec5SDimitry Andric importTableStart = importDirs->chunks.front(); 7540b57cec5SDimitry Andric for (Chunk *c : importDirs->chunks) 7550b57cec5SDimitry Andric importTableSize += c->getSize(); 7560b57cec5SDimitry Andric } 7570b57cec5SDimitry Andric 7580b57cec5SDimitry Andric if (PartialSection *importAddresses = findPartialSection(".idata$5", rdata)) { 7590b57cec5SDimitry Andric if (!importAddresses->chunks.empty()) 7600b57cec5SDimitry Andric iatStart = importAddresses->chunks.front(); 7610b57cec5SDimitry Andric for (Chunk *c : importAddresses->chunks) 7620b57cec5SDimitry Andric iatSize += c->getSize(); 7630b57cec5SDimitry Andric } 7640b57cec5SDimitry Andric } 7650b57cec5SDimitry Andric 7660b57cec5SDimitry Andric // Return whether a SectionChunk's suffix (the dollar and any trailing 7670b57cec5SDimitry Andric // suffix) should be removed and sorted into the main suffixless 7680b57cec5SDimitry Andric // PartialSection. 7690b57cec5SDimitry Andric static bool shouldStripSectionSuffix(SectionChunk *sc, StringRef name) { 7700b57cec5SDimitry Andric // On MinGW, comdat groups are formed by putting the comdat group name 7710b57cec5SDimitry Andric // after the '$' in the section name. For .eh_frame$<symbol>, that must 7720b57cec5SDimitry Andric // still be sorted before the .eh_frame trailer from crtend.o, thus just 7730b57cec5SDimitry Andric // strip the section name trailer. For other sections, such as 7740b57cec5SDimitry Andric // .tls$$<symbol> (where non-comdat .tls symbols are otherwise stored in 7750b57cec5SDimitry Andric // ".tls$"), they must be strictly sorted after .tls. And for the 7760b57cec5SDimitry Andric // hypothetical case of comdat .CRT$XCU, we definitely need to keep the 7770b57cec5SDimitry Andric // suffix for sorting. Thus, to play it safe, only strip the suffix for 7780b57cec5SDimitry Andric // the standard sections. 7790b57cec5SDimitry Andric if (!config->mingw) 7800b57cec5SDimitry Andric return false; 7810b57cec5SDimitry Andric if (!sc || !sc->isCOMDAT()) 7820b57cec5SDimitry Andric return false; 7830b57cec5SDimitry Andric return name.startswith(".text$") || name.startswith(".data$") || 7840b57cec5SDimitry Andric name.startswith(".rdata$") || name.startswith(".pdata$") || 7850b57cec5SDimitry Andric name.startswith(".xdata$") || name.startswith(".eh_frame$"); 7860b57cec5SDimitry Andric } 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric // Create output section objects and add them to OutputSections. 7890b57cec5SDimitry Andric void Writer::createSections() { 7900b57cec5SDimitry Andric // First, create the builtin sections. 7910b57cec5SDimitry Andric const uint32_t data = IMAGE_SCN_CNT_INITIALIZED_DATA; 7920b57cec5SDimitry Andric const uint32_t bss = IMAGE_SCN_CNT_UNINITIALIZED_DATA; 7930b57cec5SDimitry Andric const uint32_t code = IMAGE_SCN_CNT_CODE; 7940b57cec5SDimitry Andric const uint32_t discardable = IMAGE_SCN_MEM_DISCARDABLE; 7950b57cec5SDimitry Andric const uint32_t r = IMAGE_SCN_MEM_READ; 7960b57cec5SDimitry Andric const uint32_t w = IMAGE_SCN_MEM_WRITE; 7970b57cec5SDimitry Andric const uint32_t x = IMAGE_SCN_MEM_EXECUTE; 7980b57cec5SDimitry Andric 7990b57cec5SDimitry Andric SmallDenseMap<std::pair<StringRef, uint32_t>, OutputSection *> sections; 8000b57cec5SDimitry Andric auto createSection = [&](StringRef name, uint32_t outChars) { 8010b57cec5SDimitry Andric OutputSection *&sec = sections[{name, outChars}]; 8020b57cec5SDimitry Andric if (!sec) { 8030b57cec5SDimitry Andric sec = make<OutputSection>(name, outChars); 8040b57cec5SDimitry Andric outputSections.push_back(sec); 8050b57cec5SDimitry Andric } 8060b57cec5SDimitry Andric return sec; 8070b57cec5SDimitry Andric }; 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric // Try to match the section order used by link.exe. 8100b57cec5SDimitry Andric textSec = createSection(".text", code | r | x); 8110b57cec5SDimitry Andric createSection(".bss", bss | r | w); 8120b57cec5SDimitry Andric rdataSec = createSection(".rdata", data | r); 8130b57cec5SDimitry Andric buildidSec = createSection(".buildid", data | r); 8140b57cec5SDimitry Andric dataSec = createSection(".data", data | r | w); 8150b57cec5SDimitry Andric pdataSec = createSection(".pdata", data | r); 8160b57cec5SDimitry Andric idataSec = createSection(".idata", data | r); 8170b57cec5SDimitry Andric edataSec = createSection(".edata", data | r); 8180b57cec5SDimitry Andric didatSec = createSection(".didat", data | r); 8190b57cec5SDimitry Andric rsrcSec = createSection(".rsrc", data | r); 8200b57cec5SDimitry Andric relocSec = createSection(".reloc", data | discardable | r); 8210b57cec5SDimitry Andric ctorsSec = createSection(".ctors", data | r | w); 8220b57cec5SDimitry Andric dtorsSec = createSection(".dtors", data | r | w); 8230b57cec5SDimitry Andric 8240b57cec5SDimitry Andric // Then bin chunks by name and output characteristics. 8250b57cec5SDimitry Andric for (Chunk *c : symtab->getChunks()) { 8260b57cec5SDimitry Andric auto *sc = dyn_cast<SectionChunk>(c); 8270b57cec5SDimitry Andric if (sc && !sc->live) { 8280b57cec5SDimitry Andric if (config->verbose) 8290b57cec5SDimitry Andric sc->printDiscardedMessage(); 8300b57cec5SDimitry Andric continue; 8310b57cec5SDimitry Andric } 8320b57cec5SDimitry Andric StringRef name = c->getSectionName(); 8330b57cec5SDimitry Andric if (shouldStripSectionSuffix(sc, name)) 8340b57cec5SDimitry Andric name = name.split('$').first; 8350b57cec5SDimitry Andric PartialSection *pSec = createPartialSection(name, 8360b57cec5SDimitry Andric c->getOutputCharacteristics()); 8370b57cec5SDimitry Andric pSec->chunks.push_back(c); 8380b57cec5SDimitry Andric } 8390b57cec5SDimitry Andric 8400b57cec5SDimitry Andric fixPartialSectionChars(".rsrc", data | r); 841*85868e8aSDimitry Andric fixPartialSectionChars(".edata", data | r); 8420b57cec5SDimitry Andric // Even in non MinGW cases, we might need to link against GNU import 8430b57cec5SDimitry Andric // libraries. 8440b57cec5SDimitry Andric bool hasIdata = fixGnuImportChunks(); 8450b57cec5SDimitry Andric if (!idata.empty()) 8460b57cec5SDimitry Andric hasIdata = true; 8470b57cec5SDimitry Andric 8480b57cec5SDimitry Andric if (hasIdata) 8490b57cec5SDimitry Andric addSyntheticIdata(); 8500b57cec5SDimitry Andric 8510b57cec5SDimitry Andric // Process an /order option. 8520b57cec5SDimitry Andric if (!config->order.empty()) 8530b57cec5SDimitry Andric for (auto it : partialSections) 8540b57cec5SDimitry Andric sortBySectionOrder(it.second->chunks); 8550b57cec5SDimitry Andric 8560b57cec5SDimitry Andric if (hasIdata) 8570b57cec5SDimitry Andric locateImportTables(); 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric // Then create an OutputSection for each section. 8600b57cec5SDimitry Andric // '$' and all following characters in input section names are 8610b57cec5SDimitry Andric // discarded when determining output section. So, .text$foo 8620b57cec5SDimitry Andric // contributes to .text, for example. See PE/COFF spec 3.2. 8630b57cec5SDimitry Andric for (auto it : partialSections) { 8640b57cec5SDimitry Andric PartialSection *pSec = it.second; 8650b57cec5SDimitry Andric StringRef name = getOutputSectionName(pSec->name); 8660b57cec5SDimitry Andric uint32_t outChars = pSec->characteristics; 8670b57cec5SDimitry Andric 8680b57cec5SDimitry Andric if (name == ".CRT") { 8690b57cec5SDimitry Andric // In link.exe, there is a special case for the I386 target where .CRT 8700b57cec5SDimitry Andric // sections are treated as if they have output characteristics DATA | R if 8710b57cec5SDimitry Andric // their characteristics are DATA | R | W. This implements the same 8720b57cec5SDimitry Andric // special case for all architectures. 8730b57cec5SDimitry Andric outChars = data | r; 8740b57cec5SDimitry Andric 8750b57cec5SDimitry Andric log("Processing section " + pSec->name + " -> " + name); 8760b57cec5SDimitry Andric 8770b57cec5SDimitry Andric sortCRTSectionChunks(pSec->chunks); 8780b57cec5SDimitry Andric } 8790b57cec5SDimitry Andric 8800b57cec5SDimitry Andric OutputSection *sec = createSection(name, outChars); 8810b57cec5SDimitry Andric for (Chunk *c : pSec->chunks) 8820b57cec5SDimitry Andric sec->addChunk(c); 8830b57cec5SDimitry Andric 8840b57cec5SDimitry Andric sec->addContributingPartialSection(pSec); 8850b57cec5SDimitry Andric } 8860b57cec5SDimitry Andric 8870b57cec5SDimitry Andric // Finally, move some output sections to the end. 8880b57cec5SDimitry Andric auto sectionOrder = [&](const OutputSection *s) { 8890b57cec5SDimitry Andric // Move DISCARDABLE (or non-memory-mapped) sections to the end of file 8900b57cec5SDimitry Andric // because the loader cannot handle holes. Stripping can remove other 8910b57cec5SDimitry Andric // discardable ones than .reloc, which is first of them (created early). 8920b57cec5SDimitry Andric if (s->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) 8930b57cec5SDimitry Andric return 2; 8940b57cec5SDimitry Andric // .rsrc should come at the end of the non-discardable sections because its 8950b57cec5SDimitry Andric // size may change by the Win32 UpdateResources() function, causing 8960b57cec5SDimitry Andric // subsequent sections to move (see https://crbug.com/827082). 8970b57cec5SDimitry Andric if (s == rsrcSec) 8980b57cec5SDimitry Andric return 1; 8990b57cec5SDimitry Andric return 0; 9000b57cec5SDimitry Andric }; 9010b57cec5SDimitry Andric llvm::stable_sort(outputSections, 9020b57cec5SDimitry Andric [&](const OutputSection *s, const OutputSection *t) { 9030b57cec5SDimitry Andric return sectionOrder(s) < sectionOrder(t); 9040b57cec5SDimitry Andric }); 9050b57cec5SDimitry Andric } 9060b57cec5SDimitry Andric 9070b57cec5SDimitry Andric void Writer::createMiscChunks() { 9080b57cec5SDimitry Andric for (MergeChunk *p : MergeChunk::instances) { 9090b57cec5SDimitry Andric if (p) { 9100b57cec5SDimitry Andric p->finalizeContents(); 9110b57cec5SDimitry Andric rdataSec->addChunk(p); 9120b57cec5SDimitry Andric } 9130b57cec5SDimitry Andric } 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric // Create thunks for locally-dllimported symbols. 9160b57cec5SDimitry Andric if (!symtab->localImportChunks.empty()) { 9170b57cec5SDimitry Andric for (Chunk *c : symtab->localImportChunks) 9180b57cec5SDimitry Andric rdataSec->addChunk(c); 9190b57cec5SDimitry Andric } 9200b57cec5SDimitry Andric 9210b57cec5SDimitry Andric // Create Debug Information Chunks 9220b57cec5SDimitry Andric OutputSection *debugInfoSec = config->mingw ? buildidSec : rdataSec; 9230b57cec5SDimitry Andric if (config->debug || config->repro) { 9240b57cec5SDimitry Andric debugDirectory = make<DebugDirectoryChunk>(debugRecords, config->repro); 9250b57cec5SDimitry Andric debugInfoSec->addChunk(debugDirectory); 9260b57cec5SDimitry Andric } 9270b57cec5SDimitry Andric 9280b57cec5SDimitry Andric if (config->debug) { 9290b57cec5SDimitry Andric // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified. We 9300b57cec5SDimitry Andric // output a PDB no matter what, and this chunk provides the only means of 9310b57cec5SDimitry Andric // allowing a debugger to match a PDB and an executable. So we need it even 9320b57cec5SDimitry Andric // if we're ultimately not going to write CodeView data to the PDB. 9330b57cec5SDimitry Andric buildId = make<CVDebugRecordChunk>(); 9340b57cec5SDimitry Andric debugRecords.push_back(buildId); 9350b57cec5SDimitry Andric 9360b57cec5SDimitry Andric for (Chunk *c : debugRecords) 9370b57cec5SDimitry Andric debugInfoSec->addChunk(c); 9380b57cec5SDimitry Andric } 9390b57cec5SDimitry Andric 9400b57cec5SDimitry Andric // Create SEH table. x86-only. 9410b57cec5SDimitry Andric if (config->safeSEH) 9420b57cec5SDimitry Andric createSEHTable(); 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric // Create /guard:cf tables if requested. 9450b57cec5SDimitry Andric if (config->guardCF != GuardCFLevel::Off) 9460b57cec5SDimitry Andric createGuardCFTables(); 9470b57cec5SDimitry Andric 9480b57cec5SDimitry Andric if (config->mingw) { 9490b57cec5SDimitry Andric createRuntimePseudoRelocs(); 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric insertCtorDtorSymbols(); 9520b57cec5SDimitry Andric } 9530b57cec5SDimitry Andric } 9540b57cec5SDimitry Andric 9550b57cec5SDimitry Andric // Create .idata section for the DLL-imported symbol table. 9560b57cec5SDimitry Andric // The format of this section is inherently Windows-specific. 9570b57cec5SDimitry Andric // IdataContents class abstracted away the details for us, 9580b57cec5SDimitry Andric // so we just let it create chunks and add them to the section. 9590b57cec5SDimitry Andric void Writer::createImportTables() { 9600b57cec5SDimitry Andric // Initialize DLLOrder so that import entries are ordered in 9610b57cec5SDimitry Andric // the same order as in the command line. (That affects DLL 9620b57cec5SDimitry Andric // initialization order, and this ordering is MSVC-compatible.) 9630b57cec5SDimitry Andric for (ImportFile *file : ImportFile::instances) { 9640b57cec5SDimitry Andric if (!file->live) 9650b57cec5SDimitry Andric continue; 9660b57cec5SDimitry Andric 9670b57cec5SDimitry Andric std::string dll = StringRef(file->dllName).lower(); 9680b57cec5SDimitry Andric if (config->dllOrder.count(dll) == 0) 9690b57cec5SDimitry Andric config->dllOrder[dll] = config->dllOrder.size(); 9700b57cec5SDimitry Andric 9710b57cec5SDimitry Andric if (file->impSym && !isa<DefinedImportData>(file->impSym)) 9720b57cec5SDimitry Andric fatal(toString(*file->impSym) + " was replaced"); 9730b57cec5SDimitry Andric DefinedImportData *impSym = cast_or_null<DefinedImportData>(file->impSym); 9740b57cec5SDimitry Andric if (config->delayLoads.count(StringRef(file->dllName).lower())) { 9750b57cec5SDimitry Andric if (!file->thunkSym) 9760b57cec5SDimitry Andric fatal("cannot delay-load " + toString(file) + 9770b57cec5SDimitry Andric " due to import of data: " + toString(*impSym)); 9780b57cec5SDimitry Andric delayIdata.add(impSym); 9790b57cec5SDimitry Andric } else { 9800b57cec5SDimitry Andric idata.add(impSym); 9810b57cec5SDimitry Andric } 9820b57cec5SDimitry Andric } 9830b57cec5SDimitry Andric } 9840b57cec5SDimitry Andric 9850b57cec5SDimitry Andric void Writer::appendImportThunks() { 9860b57cec5SDimitry Andric if (ImportFile::instances.empty()) 9870b57cec5SDimitry Andric return; 9880b57cec5SDimitry Andric 9890b57cec5SDimitry Andric for (ImportFile *file : ImportFile::instances) { 9900b57cec5SDimitry Andric if (!file->live) 9910b57cec5SDimitry Andric continue; 9920b57cec5SDimitry Andric 9930b57cec5SDimitry Andric if (!file->thunkSym) 9940b57cec5SDimitry Andric continue; 9950b57cec5SDimitry Andric 9960b57cec5SDimitry Andric if (!isa<DefinedImportThunk>(file->thunkSym)) 9970b57cec5SDimitry Andric fatal(toString(*file->thunkSym) + " was replaced"); 9980b57cec5SDimitry Andric DefinedImportThunk *thunk = cast<DefinedImportThunk>(file->thunkSym); 9990b57cec5SDimitry Andric if (file->thunkLive) 10000b57cec5SDimitry Andric textSec->addChunk(thunk->getChunk()); 10010b57cec5SDimitry Andric } 10020b57cec5SDimitry Andric 10030b57cec5SDimitry Andric if (!delayIdata.empty()) { 10040b57cec5SDimitry Andric Defined *helper = cast<Defined>(config->delayLoadHelper); 10050b57cec5SDimitry Andric delayIdata.create(helper); 10060b57cec5SDimitry Andric for (Chunk *c : delayIdata.getChunks()) 10070b57cec5SDimitry Andric didatSec->addChunk(c); 10080b57cec5SDimitry Andric for (Chunk *c : delayIdata.getDataChunks()) 10090b57cec5SDimitry Andric dataSec->addChunk(c); 10100b57cec5SDimitry Andric for (Chunk *c : delayIdata.getCodeChunks()) 10110b57cec5SDimitry Andric textSec->addChunk(c); 10120b57cec5SDimitry Andric } 10130b57cec5SDimitry Andric } 10140b57cec5SDimitry Andric 10150b57cec5SDimitry Andric void Writer::createExportTable() { 1016*85868e8aSDimitry Andric if (!edataSec->chunks.empty()) { 1017*85868e8aSDimitry Andric // Allow using a custom built export table from input object files, instead 1018*85868e8aSDimitry Andric // of having the linker synthesize the tables. 1019*85868e8aSDimitry Andric if (config->hadExplicitExports) 1020*85868e8aSDimitry Andric warn("literal .edata sections override exports"); 1021*85868e8aSDimitry Andric } else if (!config->exports.empty()) { 10220b57cec5SDimitry Andric for (Chunk *c : edata.chunks) 10230b57cec5SDimitry Andric edataSec->addChunk(c); 10240b57cec5SDimitry Andric } 1025*85868e8aSDimitry Andric if (!edataSec->chunks.empty()) { 1026*85868e8aSDimitry Andric edataStart = edataSec->chunks.front(); 1027*85868e8aSDimitry Andric edataEnd = edataSec->chunks.back(); 1028*85868e8aSDimitry Andric } 1029*85868e8aSDimitry Andric } 10300b57cec5SDimitry Andric 10310b57cec5SDimitry Andric void Writer::removeUnusedSections() { 10320b57cec5SDimitry Andric // Remove sections that we can be sure won't get content, to avoid 10330b57cec5SDimitry Andric // allocating space for their section headers. 10340b57cec5SDimitry Andric auto isUnused = [this](OutputSection *s) { 10350b57cec5SDimitry Andric if (s == relocSec) 10360b57cec5SDimitry Andric return false; // This section is populated later. 10370b57cec5SDimitry Andric // MergeChunks have zero size at this point, as their size is finalized 10380b57cec5SDimitry Andric // later. Only remove sections that have no Chunks at all. 10390b57cec5SDimitry Andric return s->chunks.empty(); 10400b57cec5SDimitry Andric }; 10410b57cec5SDimitry Andric outputSections.erase( 10420b57cec5SDimitry Andric std::remove_if(outputSections.begin(), outputSections.end(), isUnused), 10430b57cec5SDimitry Andric outputSections.end()); 10440b57cec5SDimitry Andric } 10450b57cec5SDimitry Andric 10460b57cec5SDimitry Andric // The Windows loader doesn't seem to like empty sections, 10470b57cec5SDimitry Andric // so we remove them if any. 10480b57cec5SDimitry Andric void Writer::removeEmptySections() { 10490b57cec5SDimitry Andric auto isEmpty = [](OutputSection *s) { return s->getVirtualSize() == 0; }; 10500b57cec5SDimitry Andric outputSections.erase( 10510b57cec5SDimitry Andric std::remove_if(outputSections.begin(), outputSections.end(), isEmpty), 10520b57cec5SDimitry Andric outputSections.end()); 10530b57cec5SDimitry Andric } 10540b57cec5SDimitry Andric 10550b57cec5SDimitry Andric void Writer::assignOutputSectionIndices() { 10560b57cec5SDimitry Andric // Assign final output section indices, and assign each chunk to its output 10570b57cec5SDimitry Andric // section. 10580b57cec5SDimitry Andric uint32_t idx = 1; 10590b57cec5SDimitry Andric for (OutputSection *os : outputSections) { 10600b57cec5SDimitry Andric os->sectionIndex = idx; 10610b57cec5SDimitry Andric for (Chunk *c : os->chunks) 10620b57cec5SDimitry Andric c->setOutputSectionIdx(idx); 10630b57cec5SDimitry Andric ++idx; 10640b57cec5SDimitry Andric } 10650b57cec5SDimitry Andric 10660b57cec5SDimitry Andric // Merge chunks are containers of chunks, so assign those an output section 10670b57cec5SDimitry Andric // too. 10680b57cec5SDimitry Andric for (MergeChunk *mc : MergeChunk::instances) 10690b57cec5SDimitry Andric if (mc) 10700b57cec5SDimitry Andric for (SectionChunk *sc : mc->sections) 10710b57cec5SDimitry Andric if (sc && sc->live) 10720b57cec5SDimitry Andric sc->setOutputSectionIdx(mc->getOutputSectionIdx()); 10730b57cec5SDimitry Andric } 10740b57cec5SDimitry Andric 10750b57cec5SDimitry Andric size_t Writer::addEntryToStringTable(StringRef str) { 10760b57cec5SDimitry Andric assert(str.size() > COFF::NameSize); 10770b57cec5SDimitry Andric size_t offsetOfEntry = strtab.size() + 4; // +4 for the size field 10780b57cec5SDimitry Andric strtab.insert(strtab.end(), str.begin(), str.end()); 10790b57cec5SDimitry Andric strtab.push_back('\0'); 10800b57cec5SDimitry Andric return offsetOfEntry; 10810b57cec5SDimitry Andric } 10820b57cec5SDimitry Andric 10830b57cec5SDimitry Andric Optional<coff_symbol16> Writer::createSymbol(Defined *def) { 10840b57cec5SDimitry Andric coff_symbol16 sym; 10850b57cec5SDimitry Andric switch (def->kind()) { 10860b57cec5SDimitry Andric case Symbol::DefinedAbsoluteKind: 10870b57cec5SDimitry Andric sym.Value = def->getRVA(); 10880b57cec5SDimitry Andric sym.SectionNumber = IMAGE_SYM_ABSOLUTE; 10890b57cec5SDimitry Andric break; 10900b57cec5SDimitry Andric case Symbol::DefinedSyntheticKind: 10910b57cec5SDimitry Andric // Relative symbols are unrepresentable in a COFF symbol table. 10920b57cec5SDimitry Andric return None; 10930b57cec5SDimitry Andric default: { 10940b57cec5SDimitry Andric // Don't write symbols that won't be written to the output to the symbol 10950b57cec5SDimitry Andric // table. 10960b57cec5SDimitry Andric Chunk *c = def->getChunk(); 10970b57cec5SDimitry Andric if (!c) 10980b57cec5SDimitry Andric return None; 10990b57cec5SDimitry Andric OutputSection *os = c->getOutputSection(); 11000b57cec5SDimitry Andric if (!os) 11010b57cec5SDimitry Andric return None; 11020b57cec5SDimitry Andric 11030b57cec5SDimitry Andric sym.Value = def->getRVA() - os->getRVA(); 11040b57cec5SDimitry Andric sym.SectionNumber = os->sectionIndex; 11050b57cec5SDimitry Andric break; 11060b57cec5SDimitry Andric } 11070b57cec5SDimitry Andric } 11080b57cec5SDimitry Andric 11090b57cec5SDimitry Andric // Symbols that are runtime pseudo relocations don't point to the actual 11100b57cec5SDimitry Andric // symbol data itself (as they are imported), but points to the IAT entry 11110b57cec5SDimitry Andric // instead. Avoid emitting them to the symbol table, as they can confuse 11120b57cec5SDimitry Andric // debuggers. 11130b57cec5SDimitry Andric if (def->isRuntimePseudoReloc) 11140b57cec5SDimitry Andric return None; 11150b57cec5SDimitry Andric 11160b57cec5SDimitry Andric StringRef name = def->getName(); 11170b57cec5SDimitry Andric if (name.size() > COFF::NameSize) { 11180b57cec5SDimitry Andric sym.Name.Offset.Zeroes = 0; 11190b57cec5SDimitry Andric sym.Name.Offset.Offset = addEntryToStringTable(name); 11200b57cec5SDimitry Andric } else { 11210b57cec5SDimitry Andric memset(sym.Name.ShortName, 0, COFF::NameSize); 11220b57cec5SDimitry Andric memcpy(sym.Name.ShortName, name.data(), name.size()); 11230b57cec5SDimitry Andric } 11240b57cec5SDimitry Andric 11250b57cec5SDimitry Andric if (auto *d = dyn_cast<DefinedCOFF>(def)) { 11260b57cec5SDimitry Andric COFFSymbolRef ref = d->getCOFFSymbol(); 11270b57cec5SDimitry Andric sym.Type = ref.getType(); 11280b57cec5SDimitry Andric sym.StorageClass = ref.getStorageClass(); 11290b57cec5SDimitry Andric } else { 11300b57cec5SDimitry Andric sym.Type = IMAGE_SYM_TYPE_NULL; 11310b57cec5SDimitry Andric sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL; 11320b57cec5SDimitry Andric } 11330b57cec5SDimitry Andric sym.NumberOfAuxSymbols = 0; 11340b57cec5SDimitry Andric return sym; 11350b57cec5SDimitry Andric } 11360b57cec5SDimitry Andric 11370b57cec5SDimitry Andric void Writer::createSymbolAndStringTable() { 11380b57cec5SDimitry Andric // PE/COFF images are limited to 8 byte section names. Longer names can be 11390b57cec5SDimitry Andric // supported by writing a non-standard string table, but this string table is 11400b57cec5SDimitry Andric // not mapped at runtime and the long names will therefore be inaccessible. 11410b57cec5SDimitry Andric // link.exe always truncates section names to 8 bytes, whereas binutils always 11420b57cec5SDimitry Andric // preserves long section names via the string table. LLD adopts a hybrid 11430b57cec5SDimitry Andric // solution where discardable sections have long names preserved and 11440b57cec5SDimitry Andric // non-discardable sections have their names truncated, to ensure that any 11450b57cec5SDimitry Andric // section which is mapped at runtime also has its name mapped at runtime. 11460b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 11470b57cec5SDimitry Andric if (sec->name.size() <= COFF::NameSize) 11480b57cec5SDimitry Andric continue; 11490b57cec5SDimitry Andric if ((sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0) 11500b57cec5SDimitry Andric continue; 11510b57cec5SDimitry Andric sec->setStringTableOff(addEntryToStringTable(sec->name)); 11520b57cec5SDimitry Andric } 11530b57cec5SDimitry Andric 11540b57cec5SDimitry Andric if (config->debugDwarf || config->debugSymtab) { 11550b57cec5SDimitry Andric for (ObjFile *file : ObjFile::instances) { 11560b57cec5SDimitry Andric for (Symbol *b : file->getSymbols()) { 11570b57cec5SDimitry Andric auto *d = dyn_cast_or_null<Defined>(b); 11580b57cec5SDimitry Andric if (!d || d->writtenToSymtab) 11590b57cec5SDimitry Andric continue; 11600b57cec5SDimitry Andric d->writtenToSymtab = true; 11610b57cec5SDimitry Andric 11620b57cec5SDimitry Andric if (Optional<coff_symbol16> sym = createSymbol(d)) 11630b57cec5SDimitry Andric outputSymtab.push_back(*sym); 11640b57cec5SDimitry Andric } 11650b57cec5SDimitry Andric } 11660b57cec5SDimitry Andric } 11670b57cec5SDimitry Andric 11680b57cec5SDimitry Andric if (outputSymtab.empty() && strtab.empty()) 11690b57cec5SDimitry Andric return; 11700b57cec5SDimitry Andric 11710b57cec5SDimitry Andric // We position the symbol table to be adjacent to the end of the last section. 11720b57cec5SDimitry Andric uint64_t fileOff = fileSize; 11730b57cec5SDimitry Andric pointerToSymbolTable = fileOff; 11740b57cec5SDimitry Andric fileOff += outputSymtab.size() * sizeof(coff_symbol16); 11750b57cec5SDimitry Andric fileOff += 4 + strtab.size(); 11760b57cec5SDimitry Andric fileSize = alignTo(fileOff, config->fileAlign); 11770b57cec5SDimitry Andric } 11780b57cec5SDimitry Andric 11790b57cec5SDimitry Andric void Writer::mergeSections() { 11800b57cec5SDimitry Andric if (!pdataSec->chunks.empty()) { 11810b57cec5SDimitry Andric firstPdata = pdataSec->chunks.front(); 11820b57cec5SDimitry Andric lastPdata = pdataSec->chunks.back(); 11830b57cec5SDimitry Andric } 11840b57cec5SDimitry Andric 11850b57cec5SDimitry Andric for (auto &p : config->merge) { 11860b57cec5SDimitry Andric StringRef toName = p.second; 11870b57cec5SDimitry Andric if (p.first == toName) 11880b57cec5SDimitry Andric continue; 11890b57cec5SDimitry Andric StringSet<> names; 11900b57cec5SDimitry Andric while (1) { 11910b57cec5SDimitry Andric if (!names.insert(toName).second) 11920b57cec5SDimitry Andric fatal("/merge: cycle found for section '" + p.first + "'"); 11930b57cec5SDimitry Andric auto i = config->merge.find(toName); 11940b57cec5SDimitry Andric if (i == config->merge.end()) 11950b57cec5SDimitry Andric break; 11960b57cec5SDimitry Andric toName = i->second; 11970b57cec5SDimitry Andric } 11980b57cec5SDimitry Andric OutputSection *from = findSection(p.first); 11990b57cec5SDimitry Andric OutputSection *to = findSection(toName); 12000b57cec5SDimitry Andric if (!from) 12010b57cec5SDimitry Andric continue; 12020b57cec5SDimitry Andric if (!to) { 12030b57cec5SDimitry Andric from->name = toName; 12040b57cec5SDimitry Andric continue; 12050b57cec5SDimitry Andric } 12060b57cec5SDimitry Andric to->merge(from); 12070b57cec5SDimitry Andric } 12080b57cec5SDimitry Andric } 12090b57cec5SDimitry Andric 12100b57cec5SDimitry Andric // Visits all sections to assign incremental, non-overlapping RVAs and 12110b57cec5SDimitry Andric // file offsets. 12120b57cec5SDimitry Andric void Writer::assignAddresses() { 12130b57cec5SDimitry Andric sizeOfHeaders = dosStubSize + sizeof(PEMagic) + sizeof(coff_file_header) + 12140b57cec5SDimitry Andric sizeof(data_directory) * numberOfDataDirectory + 12150b57cec5SDimitry Andric sizeof(coff_section) * outputSections.size(); 12160b57cec5SDimitry Andric sizeOfHeaders += 12170b57cec5SDimitry Andric config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header); 12180b57cec5SDimitry Andric sizeOfHeaders = alignTo(sizeOfHeaders, config->fileAlign); 12190b57cec5SDimitry Andric fileSize = sizeOfHeaders; 12200b57cec5SDimitry Andric 12210b57cec5SDimitry Andric // The first page is kept unmapped. 12220b57cec5SDimitry Andric uint64_t rva = alignTo(sizeOfHeaders, config->align); 12230b57cec5SDimitry Andric 12240b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 12250b57cec5SDimitry Andric if (sec == relocSec) 12260b57cec5SDimitry Andric addBaserels(); 12270b57cec5SDimitry Andric uint64_t rawSize = 0, virtualSize = 0; 12280b57cec5SDimitry Andric sec->header.VirtualAddress = rva; 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric // If /FUNCTIONPADMIN is used, functions are padded in order to create a 12310b57cec5SDimitry Andric // hotpatchable image. 12320b57cec5SDimitry Andric const bool isCodeSection = 12330b57cec5SDimitry Andric (sec->header.Characteristics & IMAGE_SCN_CNT_CODE) && 12340b57cec5SDimitry Andric (sec->header.Characteristics & IMAGE_SCN_MEM_READ) && 12350b57cec5SDimitry Andric (sec->header.Characteristics & IMAGE_SCN_MEM_EXECUTE); 12360b57cec5SDimitry Andric uint32_t padding = isCodeSection ? config->functionPadMin : 0; 12370b57cec5SDimitry Andric 12380b57cec5SDimitry Andric for (Chunk *c : sec->chunks) { 12390b57cec5SDimitry Andric if (padding && c->isHotPatchable()) 12400b57cec5SDimitry Andric virtualSize += padding; 12410b57cec5SDimitry Andric virtualSize = alignTo(virtualSize, c->getAlignment()); 12420b57cec5SDimitry Andric c->setRVA(rva + virtualSize); 12430b57cec5SDimitry Andric virtualSize += c->getSize(); 12440b57cec5SDimitry Andric if (c->hasData) 12450b57cec5SDimitry Andric rawSize = alignTo(virtualSize, config->fileAlign); 12460b57cec5SDimitry Andric } 12470b57cec5SDimitry Andric if (virtualSize > UINT32_MAX) 12480b57cec5SDimitry Andric error("section larger than 4 GiB: " + sec->name); 12490b57cec5SDimitry Andric sec->header.VirtualSize = virtualSize; 12500b57cec5SDimitry Andric sec->header.SizeOfRawData = rawSize; 12510b57cec5SDimitry Andric if (rawSize != 0) 12520b57cec5SDimitry Andric sec->header.PointerToRawData = fileSize; 12530b57cec5SDimitry Andric rva += alignTo(virtualSize, config->align); 12540b57cec5SDimitry Andric fileSize += alignTo(rawSize, config->fileAlign); 12550b57cec5SDimitry Andric } 12560b57cec5SDimitry Andric sizeOfImage = alignTo(rva, config->align); 12570b57cec5SDimitry Andric 12580b57cec5SDimitry Andric // Assign addresses to sections in MergeChunks. 12590b57cec5SDimitry Andric for (MergeChunk *mc : MergeChunk::instances) 12600b57cec5SDimitry Andric if (mc) 12610b57cec5SDimitry Andric mc->assignSubsectionRVAs(); 12620b57cec5SDimitry Andric } 12630b57cec5SDimitry Andric 12640b57cec5SDimitry Andric template <typename PEHeaderTy> void Writer::writeHeader() { 12650b57cec5SDimitry Andric // Write DOS header. For backwards compatibility, the first part of a PE/COFF 12660b57cec5SDimitry Andric // executable consists of an MS-DOS MZ executable. If the executable is run 12670b57cec5SDimitry Andric // under DOS, that program gets run (usually to just print an error message). 12680b57cec5SDimitry Andric // When run under Windows, the loader looks at AddressOfNewExeHeader and uses 12690b57cec5SDimitry Andric // the PE header instead. 12700b57cec5SDimitry Andric uint8_t *buf = buffer->getBufferStart(); 12710b57cec5SDimitry Andric auto *dos = reinterpret_cast<dos_header *>(buf); 12720b57cec5SDimitry Andric buf += sizeof(dos_header); 12730b57cec5SDimitry Andric dos->Magic[0] = 'M'; 12740b57cec5SDimitry Andric dos->Magic[1] = 'Z'; 12750b57cec5SDimitry Andric dos->UsedBytesInTheLastPage = dosStubSize % 512; 12760b57cec5SDimitry Andric dos->FileSizeInPages = divideCeil(dosStubSize, 512); 12770b57cec5SDimitry Andric dos->HeaderSizeInParagraphs = sizeof(dos_header) / 16; 12780b57cec5SDimitry Andric 12790b57cec5SDimitry Andric dos->AddressOfRelocationTable = sizeof(dos_header); 12800b57cec5SDimitry Andric dos->AddressOfNewExeHeader = dosStubSize; 12810b57cec5SDimitry Andric 12820b57cec5SDimitry Andric // Write DOS program. 12830b57cec5SDimitry Andric memcpy(buf, dosProgram, sizeof(dosProgram)); 12840b57cec5SDimitry Andric buf += sizeof(dosProgram); 12850b57cec5SDimitry Andric 12860b57cec5SDimitry Andric // Write PE magic 12870b57cec5SDimitry Andric memcpy(buf, PEMagic, sizeof(PEMagic)); 12880b57cec5SDimitry Andric buf += sizeof(PEMagic); 12890b57cec5SDimitry Andric 12900b57cec5SDimitry Andric // Write COFF header 12910b57cec5SDimitry Andric auto *coff = reinterpret_cast<coff_file_header *>(buf); 12920b57cec5SDimitry Andric buf += sizeof(*coff); 12930b57cec5SDimitry Andric coff->Machine = config->machine; 12940b57cec5SDimitry Andric coff->NumberOfSections = outputSections.size(); 12950b57cec5SDimitry Andric coff->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE; 12960b57cec5SDimitry Andric if (config->largeAddressAware) 12970b57cec5SDimitry Andric coff->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE; 12980b57cec5SDimitry Andric if (!config->is64()) 12990b57cec5SDimitry Andric coff->Characteristics |= IMAGE_FILE_32BIT_MACHINE; 13000b57cec5SDimitry Andric if (config->dll) 13010b57cec5SDimitry Andric coff->Characteristics |= IMAGE_FILE_DLL; 13020b57cec5SDimitry Andric if (!config->relocatable) 13030b57cec5SDimitry Andric coff->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED; 13040b57cec5SDimitry Andric if (config->swaprunCD) 13050b57cec5SDimitry Andric coff->Characteristics |= IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP; 13060b57cec5SDimitry Andric if (config->swaprunNet) 13070b57cec5SDimitry Andric coff->Characteristics |= IMAGE_FILE_NET_RUN_FROM_SWAP; 13080b57cec5SDimitry Andric coff->SizeOfOptionalHeader = 13090b57cec5SDimitry Andric sizeof(PEHeaderTy) + sizeof(data_directory) * numberOfDataDirectory; 13100b57cec5SDimitry Andric 13110b57cec5SDimitry Andric // Write PE header 13120b57cec5SDimitry Andric auto *pe = reinterpret_cast<PEHeaderTy *>(buf); 13130b57cec5SDimitry Andric buf += sizeof(*pe); 13140b57cec5SDimitry Andric pe->Magic = config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32; 13150b57cec5SDimitry Andric 13160b57cec5SDimitry Andric // If {Major,Minor}LinkerVersion is left at 0.0, then for some 13170b57cec5SDimitry Andric // reason signing the resulting PE file with Authenticode produces a 13180b57cec5SDimitry Andric // signature that fails to validate on Windows 7 (but is OK on 10). 13190b57cec5SDimitry Andric // Set it to 14.0, which is what VS2015 outputs, and which avoids 13200b57cec5SDimitry Andric // that problem. 13210b57cec5SDimitry Andric pe->MajorLinkerVersion = 14; 13220b57cec5SDimitry Andric pe->MinorLinkerVersion = 0; 13230b57cec5SDimitry Andric 13240b57cec5SDimitry Andric pe->ImageBase = config->imageBase; 13250b57cec5SDimitry Andric pe->SectionAlignment = config->align; 13260b57cec5SDimitry Andric pe->FileAlignment = config->fileAlign; 13270b57cec5SDimitry Andric pe->MajorImageVersion = config->majorImageVersion; 13280b57cec5SDimitry Andric pe->MinorImageVersion = config->minorImageVersion; 13290b57cec5SDimitry Andric pe->MajorOperatingSystemVersion = config->majorOSVersion; 13300b57cec5SDimitry Andric pe->MinorOperatingSystemVersion = config->minorOSVersion; 13310b57cec5SDimitry Andric pe->MajorSubsystemVersion = config->majorOSVersion; 13320b57cec5SDimitry Andric pe->MinorSubsystemVersion = config->minorOSVersion; 13330b57cec5SDimitry Andric pe->Subsystem = config->subsystem; 13340b57cec5SDimitry Andric pe->SizeOfImage = sizeOfImage; 13350b57cec5SDimitry Andric pe->SizeOfHeaders = sizeOfHeaders; 13360b57cec5SDimitry Andric if (!config->noEntry) { 13370b57cec5SDimitry Andric Defined *entry = cast<Defined>(config->entry); 13380b57cec5SDimitry Andric pe->AddressOfEntryPoint = entry->getRVA(); 13390b57cec5SDimitry Andric // Pointer to thumb code must have the LSB set, so adjust it. 13400b57cec5SDimitry Andric if (config->machine == ARMNT) 13410b57cec5SDimitry Andric pe->AddressOfEntryPoint |= 1; 13420b57cec5SDimitry Andric } 13430b57cec5SDimitry Andric pe->SizeOfStackReserve = config->stackReserve; 13440b57cec5SDimitry Andric pe->SizeOfStackCommit = config->stackCommit; 13450b57cec5SDimitry Andric pe->SizeOfHeapReserve = config->heapReserve; 13460b57cec5SDimitry Andric pe->SizeOfHeapCommit = config->heapCommit; 13470b57cec5SDimitry Andric if (config->appContainer) 13480b57cec5SDimitry Andric pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER; 13490b57cec5SDimitry Andric if (config->dynamicBase) 13500b57cec5SDimitry Andric pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE; 13510b57cec5SDimitry Andric if (config->highEntropyVA) 13520b57cec5SDimitry Andric pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA; 13530b57cec5SDimitry Andric if (!config->allowBind) 13540b57cec5SDimitry Andric pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND; 13550b57cec5SDimitry Andric if (config->nxCompat) 13560b57cec5SDimitry Andric pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT; 13570b57cec5SDimitry Andric if (!config->allowIsolation) 13580b57cec5SDimitry Andric pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION; 13590b57cec5SDimitry Andric if (config->guardCF != GuardCFLevel::Off) 13600b57cec5SDimitry Andric pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF; 13610b57cec5SDimitry Andric if (config->integrityCheck) 13620b57cec5SDimitry Andric pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY; 13630b57cec5SDimitry Andric if (setNoSEHCharacteristic) 13640b57cec5SDimitry Andric pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH; 13650b57cec5SDimitry Andric if (config->terminalServerAware) 13660b57cec5SDimitry Andric pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE; 13670b57cec5SDimitry Andric pe->NumberOfRvaAndSize = numberOfDataDirectory; 13680b57cec5SDimitry Andric if (textSec->getVirtualSize()) { 13690b57cec5SDimitry Andric pe->BaseOfCode = textSec->getRVA(); 13700b57cec5SDimitry Andric pe->SizeOfCode = textSec->getRawSize(); 13710b57cec5SDimitry Andric } 13720b57cec5SDimitry Andric pe->SizeOfInitializedData = getSizeOfInitializedData(); 13730b57cec5SDimitry Andric 13740b57cec5SDimitry Andric // Write data directory 13750b57cec5SDimitry Andric auto *dir = reinterpret_cast<data_directory *>(buf); 13760b57cec5SDimitry Andric buf += sizeof(*dir) * numberOfDataDirectory; 1377*85868e8aSDimitry Andric if (edataStart) { 1378*85868e8aSDimitry Andric dir[EXPORT_TABLE].RelativeVirtualAddress = edataStart->getRVA(); 1379*85868e8aSDimitry Andric dir[EXPORT_TABLE].Size = 1380*85868e8aSDimitry Andric edataEnd->getRVA() + edataEnd->getSize() - edataStart->getRVA(); 13810b57cec5SDimitry Andric } 13820b57cec5SDimitry Andric if (importTableStart) { 13830b57cec5SDimitry Andric dir[IMPORT_TABLE].RelativeVirtualAddress = importTableStart->getRVA(); 13840b57cec5SDimitry Andric dir[IMPORT_TABLE].Size = importTableSize; 13850b57cec5SDimitry Andric } 13860b57cec5SDimitry Andric if (iatStart) { 13870b57cec5SDimitry Andric dir[IAT].RelativeVirtualAddress = iatStart->getRVA(); 13880b57cec5SDimitry Andric dir[IAT].Size = iatSize; 13890b57cec5SDimitry Andric } 13900b57cec5SDimitry Andric if (rsrcSec->getVirtualSize()) { 13910b57cec5SDimitry Andric dir[RESOURCE_TABLE].RelativeVirtualAddress = rsrcSec->getRVA(); 13920b57cec5SDimitry Andric dir[RESOURCE_TABLE].Size = rsrcSec->getVirtualSize(); 13930b57cec5SDimitry Andric } 13940b57cec5SDimitry Andric if (firstPdata) { 13950b57cec5SDimitry Andric dir[EXCEPTION_TABLE].RelativeVirtualAddress = firstPdata->getRVA(); 13960b57cec5SDimitry Andric dir[EXCEPTION_TABLE].Size = 13970b57cec5SDimitry Andric lastPdata->getRVA() + lastPdata->getSize() - firstPdata->getRVA(); 13980b57cec5SDimitry Andric } 13990b57cec5SDimitry Andric if (relocSec->getVirtualSize()) { 14000b57cec5SDimitry Andric dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = relocSec->getRVA(); 14010b57cec5SDimitry Andric dir[BASE_RELOCATION_TABLE].Size = relocSec->getVirtualSize(); 14020b57cec5SDimitry Andric } 14030b57cec5SDimitry Andric if (Symbol *sym = symtab->findUnderscore("_tls_used")) { 14040b57cec5SDimitry Andric if (Defined *b = dyn_cast<Defined>(sym)) { 14050b57cec5SDimitry Andric dir[TLS_TABLE].RelativeVirtualAddress = b->getRVA(); 14060b57cec5SDimitry Andric dir[TLS_TABLE].Size = config->is64() 14070b57cec5SDimitry Andric ? sizeof(object::coff_tls_directory64) 14080b57cec5SDimitry Andric : sizeof(object::coff_tls_directory32); 14090b57cec5SDimitry Andric } 14100b57cec5SDimitry Andric } 14110b57cec5SDimitry Andric if (debugDirectory) { 14120b57cec5SDimitry Andric dir[DEBUG_DIRECTORY].RelativeVirtualAddress = debugDirectory->getRVA(); 14130b57cec5SDimitry Andric dir[DEBUG_DIRECTORY].Size = debugDirectory->getSize(); 14140b57cec5SDimitry Andric } 14150b57cec5SDimitry Andric if (Symbol *sym = symtab->findUnderscore("_load_config_used")) { 14160b57cec5SDimitry Andric if (auto *b = dyn_cast<DefinedRegular>(sym)) { 14170b57cec5SDimitry Andric SectionChunk *sc = b->getChunk(); 14180b57cec5SDimitry Andric assert(b->getRVA() >= sc->getRVA()); 14190b57cec5SDimitry Andric uint64_t offsetInChunk = b->getRVA() - sc->getRVA(); 14200b57cec5SDimitry Andric if (!sc->hasData || offsetInChunk + 4 > sc->getSize()) 14210b57cec5SDimitry Andric fatal("_load_config_used is malformed"); 14220b57cec5SDimitry Andric 14230b57cec5SDimitry Andric ArrayRef<uint8_t> secContents = sc->getContents(); 14240b57cec5SDimitry Andric uint32_t loadConfigSize = 14250b57cec5SDimitry Andric *reinterpret_cast<const ulittle32_t *>(&secContents[offsetInChunk]); 14260b57cec5SDimitry Andric if (offsetInChunk + loadConfigSize > sc->getSize()) 14270b57cec5SDimitry Andric fatal("_load_config_used is too large"); 14280b57cec5SDimitry Andric dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = b->getRVA(); 14290b57cec5SDimitry Andric dir[LOAD_CONFIG_TABLE].Size = loadConfigSize; 14300b57cec5SDimitry Andric } 14310b57cec5SDimitry Andric } 14320b57cec5SDimitry Andric if (!delayIdata.empty()) { 14330b57cec5SDimitry Andric dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress = 14340b57cec5SDimitry Andric delayIdata.getDirRVA(); 14350b57cec5SDimitry Andric dir[DELAY_IMPORT_DESCRIPTOR].Size = delayIdata.getDirSize(); 14360b57cec5SDimitry Andric } 14370b57cec5SDimitry Andric 14380b57cec5SDimitry Andric // Write section table 14390b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 14400b57cec5SDimitry Andric sec->writeHeaderTo(buf); 14410b57cec5SDimitry Andric buf += sizeof(coff_section); 14420b57cec5SDimitry Andric } 14430b57cec5SDimitry Andric sectionTable = ArrayRef<uint8_t>( 14440b57cec5SDimitry Andric buf - outputSections.size() * sizeof(coff_section), buf); 14450b57cec5SDimitry Andric 14460b57cec5SDimitry Andric if (outputSymtab.empty() && strtab.empty()) 14470b57cec5SDimitry Andric return; 14480b57cec5SDimitry Andric 14490b57cec5SDimitry Andric coff->PointerToSymbolTable = pointerToSymbolTable; 14500b57cec5SDimitry Andric uint32_t numberOfSymbols = outputSymtab.size(); 14510b57cec5SDimitry Andric coff->NumberOfSymbols = numberOfSymbols; 14520b57cec5SDimitry Andric auto *symbolTable = reinterpret_cast<coff_symbol16 *>( 14530b57cec5SDimitry Andric buffer->getBufferStart() + coff->PointerToSymbolTable); 14540b57cec5SDimitry Andric for (size_t i = 0; i != numberOfSymbols; ++i) 14550b57cec5SDimitry Andric symbolTable[i] = outputSymtab[i]; 14560b57cec5SDimitry Andric // Create the string table, it follows immediately after the symbol table. 14570b57cec5SDimitry Andric // The first 4 bytes is length including itself. 14580b57cec5SDimitry Andric buf = reinterpret_cast<uint8_t *>(&symbolTable[numberOfSymbols]); 14590b57cec5SDimitry Andric write32le(buf, strtab.size() + 4); 14600b57cec5SDimitry Andric if (!strtab.empty()) 14610b57cec5SDimitry Andric memcpy(buf + 4, strtab.data(), strtab.size()); 14620b57cec5SDimitry Andric } 14630b57cec5SDimitry Andric 14640b57cec5SDimitry Andric void Writer::openFile(StringRef path) { 14650b57cec5SDimitry Andric buffer = CHECK( 14660b57cec5SDimitry Andric FileOutputBuffer::create(path, fileSize, FileOutputBuffer::F_executable), 14670b57cec5SDimitry Andric "failed to open " + path); 14680b57cec5SDimitry Andric } 14690b57cec5SDimitry Andric 14700b57cec5SDimitry Andric void Writer::createSEHTable() { 14710b57cec5SDimitry Andric SymbolRVASet handlers; 14720b57cec5SDimitry Andric for (ObjFile *file : ObjFile::instances) { 14730b57cec5SDimitry Andric if (!file->hasSafeSEH()) 14740b57cec5SDimitry Andric error("/safeseh: " + file->getName() + " is not compatible with SEH"); 14750b57cec5SDimitry Andric markSymbolsForRVATable(file, file->getSXDataChunks(), handlers); 14760b57cec5SDimitry Andric } 14770b57cec5SDimitry Andric 14780b57cec5SDimitry Andric // Set the "no SEH" characteristic if there really were no handlers, or if 14790b57cec5SDimitry Andric // there is no load config object to point to the table of handlers. 14800b57cec5SDimitry Andric setNoSEHCharacteristic = 14810b57cec5SDimitry Andric handlers.empty() || !symtab->findUnderscore("_load_config_used"); 14820b57cec5SDimitry Andric 14830b57cec5SDimitry Andric maybeAddRVATable(std::move(handlers), "__safe_se_handler_table", 14840b57cec5SDimitry Andric "__safe_se_handler_count"); 14850b57cec5SDimitry Andric } 14860b57cec5SDimitry Andric 14870b57cec5SDimitry Andric // Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set 14880b57cec5SDimitry Andric // cannot contain duplicates. Therefore, the set is uniqued by Chunk and the 14890b57cec5SDimitry Andric // symbol's offset into that Chunk. 14900b57cec5SDimitry Andric static void addSymbolToRVASet(SymbolRVASet &rvaSet, Defined *s) { 14910b57cec5SDimitry Andric Chunk *c = s->getChunk(); 14920b57cec5SDimitry Andric if (auto *sc = dyn_cast<SectionChunk>(c)) 14930b57cec5SDimitry Andric c = sc->repl; // Look through ICF replacement. 14940b57cec5SDimitry Andric uint32_t off = s->getRVA() - (c ? c->getRVA() : 0); 14950b57cec5SDimitry Andric rvaSet.insert({c, off}); 14960b57cec5SDimitry Andric } 14970b57cec5SDimitry Andric 14980b57cec5SDimitry Andric // Given a symbol, add it to the GFIDs table if it is a live, defined, function 14990b57cec5SDimitry Andric // symbol in an executable section. 15000b57cec5SDimitry Andric static void maybeAddAddressTakenFunction(SymbolRVASet &addressTakenSyms, 15010b57cec5SDimitry Andric Symbol *s) { 15020b57cec5SDimitry Andric if (!s) 15030b57cec5SDimitry Andric return; 15040b57cec5SDimitry Andric 15050b57cec5SDimitry Andric switch (s->kind()) { 15060b57cec5SDimitry Andric case Symbol::DefinedLocalImportKind: 15070b57cec5SDimitry Andric case Symbol::DefinedImportDataKind: 15080b57cec5SDimitry Andric // Defines an __imp_ pointer, so it is data, so it is ignored. 15090b57cec5SDimitry Andric break; 15100b57cec5SDimitry Andric case Symbol::DefinedCommonKind: 15110b57cec5SDimitry Andric // Common is always data, so it is ignored. 15120b57cec5SDimitry Andric break; 15130b57cec5SDimitry Andric case Symbol::DefinedAbsoluteKind: 15140b57cec5SDimitry Andric case Symbol::DefinedSyntheticKind: 15150b57cec5SDimitry Andric // Absolute is never code, synthetic generally isn't and usually isn't 15160b57cec5SDimitry Andric // determinable. 15170b57cec5SDimitry Andric break; 1518*85868e8aSDimitry Andric case Symbol::LazyArchiveKind: 1519*85868e8aSDimitry Andric case Symbol::LazyObjectKind: 15200b57cec5SDimitry Andric case Symbol::UndefinedKind: 15210b57cec5SDimitry Andric // Undefined symbols resolve to zero, so they don't have an RVA. Lazy 15220b57cec5SDimitry Andric // symbols shouldn't have relocations. 15230b57cec5SDimitry Andric break; 15240b57cec5SDimitry Andric 15250b57cec5SDimitry Andric case Symbol::DefinedImportThunkKind: 15260b57cec5SDimitry Andric // Thunks are always code, include them. 15270b57cec5SDimitry Andric addSymbolToRVASet(addressTakenSyms, cast<Defined>(s)); 15280b57cec5SDimitry Andric break; 15290b57cec5SDimitry Andric 15300b57cec5SDimitry Andric case Symbol::DefinedRegularKind: { 15310b57cec5SDimitry Andric // This is a regular, defined, symbol from a COFF file. Mark the symbol as 15320b57cec5SDimitry Andric // address taken if the symbol type is function and it's in an executable 15330b57cec5SDimitry Andric // section. 15340b57cec5SDimitry Andric auto *d = cast<DefinedRegular>(s); 15350b57cec5SDimitry Andric if (d->getCOFFSymbol().getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) { 15360b57cec5SDimitry Andric SectionChunk *sc = dyn_cast<SectionChunk>(d->getChunk()); 15370b57cec5SDimitry Andric if (sc && sc->live && 15380b57cec5SDimitry Andric sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) 15390b57cec5SDimitry Andric addSymbolToRVASet(addressTakenSyms, d); 15400b57cec5SDimitry Andric } 15410b57cec5SDimitry Andric break; 15420b57cec5SDimitry Andric } 15430b57cec5SDimitry Andric } 15440b57cec5SDimitry Andric } 15450b57cec5SDimitry Andric 15460b57cec5SDimitry Andric // Visit all relocations from all section contributions of this object file and 15470b57cec5SDimitry Andric // mark the relocation target as address-taken. 15480b57cec5SDimitry Andric static void markSymbolsWithRelocations(ObjFile *file, 15490b57cec5SDimitry Andric SymbolRVASet &usedSymbols) { 15500b57cec5SDimitry Andric for (Chunk *c : file->getChunks()) { 15510b57cec5SDimitry Andric // We only care about live section chunks. Common chunks and other chunks 15520b57cec5SDimitry Andric // don't generally contain relocations. 15530b57cec5SDimitry Andric SectionChunk *sc = dyn_cast<SectionChunk>(c); 15540b57cec5SDimitry Andric if (!sc || !sc->live) 15550b57cec5SDimitry Andric continue; 15560b57cec5SDimitry Andric 15570b57cec5SDimitry Andric for (const coff_relocation &reloc : sc->getRelocs()) { 15580b57cec5SDimitry Andric if (config->machine == I386 && reloc.Type == COFF::IMAGE_REL_I386_REL32) 15590b57cec5SDimitry Andric // Ignore relative relocations on x86. On x86_64 they can't be ignored 15600b57cec5SDimitry Andric // since they're also used to compute absolute addresses. 15610b57cec5SDimitry Andric continue; 15620b57cec5SDimitry Andric 15630b57cec5SDimitry Andric Symbol *ref = sc->file->getSymbol(reloc.SymbolTableIndex); 15640b57cec5SDimitry Andric maybeAddAddressTakenFunction(usedSymbols, ref); 15650b57cec5SDimitry Andric } 15660b57cec5SDimitry Andric } 15670b57cec5SDimitry Andric } 15680b57cec5SDimitry Andric 15690b57cec5SDimitry Andric // Create the guard function id table. This is a table of RVAs of all 15700b57cec5SDimitry Andric // address-taken functions. It is sorted and uniqued, just like the safe SEH 15710b57cec5SDimitry Andric // table. 15720b57cec5SDimitry Andric void Writer::createGuardCFTables() { 15730b57cec5SDimitry Andric SymbolRVASet addressTakenSyms; 15740b57cec5SDimitry Andric SymbolRVASet longJmpTargets; 15750b57cec5SDimitry Andric for (ObjFile *file : ObjFile::instances) { 15760b57cec5SDimitry Andric // If the object was compiled with /guard:cf, the address taken symbols 15770b57cec5SDimitry Andric // are in .gfids$y sections, and the longjmp targets are in .gljmp$y 15780b57cec5SDimitry Andric // sections. If the object was not compiled with /guard:cf, we assume there 15790b57cec5SDimitry Andric // were no setjmp targets, and that all code symbols with relocations are 15800b57cec5SDimitry Andric // possibly address-taken. 15810b57cec5SDimitry Andric if (file->hasGuardCF()) { 15820b57cec5SDimitry Andric markSymbolsForRVATable(file, file->getGuardFidChunks(), addressTakenSyms); 15830b57cec5SDimitry Andric markSymbolsForRVATable(file, file->getGuardLJmpChunks(), longJmpTargets); 15840b57cec5SDimitry Andric } else { 15850b57cec5SDimitry Andric markSymbolsWithRelocations(file, addressTakenSyms); 15860b57cec5SDimitry Andric } 15870b57cec5SDimitry Andric } 15880b57cec5SDimitry Andric 15890b57cec5SDimitry Andric // Mark the image entry as address-taken. 15900b57cec5SDimitry Andric if (config->entry) 15910b57cec5SDimitry Andric maybeAddAddressTakenFunction(addressTakenSyms, config->entry); 15920b57cec5SDimitry Andric 15930b57cec5SDimitry Andric // Mark exported symbols in executable sections as address-taken. 15940b57cec5SDimitry Andric for (Export &e : config->exports) 15950b57cec5SDimitry Andric maybeAddAddressTakenFunction(addressTakenSyms, e.sym); 15960b57cec5SDimitry Andric 15970b57cec5SDimitry Andric // Ensure sections referenced in the gfid table are 16-byte aligned. 15980b57cec5SDimitry Andric for (const ChunkAndOffset &c : addressTakenSyms) 15990b57cec5SDimitry Andric if (c.inputChunk->getAlignment() < 16) 16000b57cec5SDimitry Andric c.inputChunk->setAlignment(16); 16010b57cec5SDimitry Andric 16020b57cec5SDimitry Andric maybeAddRVATable(std::move(addressTakenSyms), "__guard_fids_table", 16030b57cec5SDimitry Andric "__guard_fids_count"); 16040b57cec5SDimitry Andric 16050b57cec5SDimitry Andric // Add the longjmp target table unless the user told us not to. 16060b57cec5SDimitry Andric if (config->guardCF == GuardCFLevel::Full) 16070b57cec5SDimitry Andric maybeAddRVATable(std::move(longJmpTargets), "__guard_longjmp_table", 16080b57cec5SDimitry Andric "__guard_longjmp_count"); 16090b57cec5SDimitry Andric 16100b57cec5SDimitry Andric // Set __guard_flags, which will be used in the load config to indicate that 16110b57cec5SDimitry Andric // /guard:cf was enabled. 16120b57cec5SDimitry Andric uint32_t guardFlags = uint32_t(coff_guard_flags::CFInstrumented) | 16130b57cec5SDimitry Andric uint32_t(coff_guard_flags::HasFidTable); 16140b57cec5SDimitry Andric if (config->guardCF == GuardCFLevel::Full) 16150b57cec5SDimitry Andric guardFlags |= uint32_t(coff_guard_flags::HasLongJmpTable); 16160b57cec5SDimitry Andric Symbol *flagSym = symtab->findUnderscore("__guard_flags"); 16170b57cec5SDimitry Andric cast<DefinedAbsolute>(flagSym)->setVA(guardFlags); 16180b57cec5SDimitry Andric } 16190b57cec5SDimitry Andric 16200b57cec5SDimitry Andric // Take a list of input sections containing symbol table indices and add those 16210b57cec5SDimitry Andric // symbols to an RVA table. The challenge is that symbol RVAs are not known and 16220b57cec5SDimitry Andric // depend on the table size, so we can't directly build a set of integers. 16230b57cec5SDimitry Andric void Writer::markSymbolsForRVATable(ObjFile *file, 16240b57cec5SDimitry Andric ArrayRef<SectionChunk *> symIdxChunks, 16250b57cec5SDimitry Andric SymbolRVASet &tableSymbols) { 16260b57cec5SDimitry Andric for (SectionChunk *c : symIdxChunks) { 16270b57cec5SDimitry Andric // Skip sections discarded by linker GC. This comes up when a .gfids section 16280b57cec5SDimitry Andric // is associated with something like a vtable and the vtable is discarded. 16290b57cec5SDimitry Andric // In this case, the associated gfids section is discarded, and we don't 16300b57cec5SDimitry Andric // mark the virtual member functions as address-taken by the vtable. 16310b57cec5SDimitry Andric if (!c->live) 16320b57cec5SDimitry Andric continue; 16330b57cec5SDimitry Andric 16340b57cec5SDimitry Andric // Validate that the contents look like symbol table indices. 16350b57cec5SDimitry Andric ArrayRef<uint8_t> data = c->getContents(); 16360b57cec5SDimitry Andric if (data.size() % 4 != 0) { 16370b57cec5SDimitry Andric warn("ignoring " + c->getSectionName() + 16380b57cec5SDimitry Andric " symbol table index section in object " + toString(file)); 16390b57cec5SDimitry Andric continue; 16400b57cec5SDimitry Andric } 16410b57cec5SDimitry Andric 16420b57cec5SDimitry Andric // Read each symbol table index and check if that symbol was included in the 16430b57cec5SDimitry Andric // final link. If so, add it to the table symbol set. 16440b57cec5SDimitry Andric ArrayRef<ulittle32_t> symIndices( 16450b57cec5SDimitry Andric reinterpret_cast<const ulittle32_t *>(data.data()), data.size() / 4); 16460b57cec5SDimitry Andric ArrayRef<Symbol *> objSymbols = file->getSymbols(); 16470b57cec5SDimitry Andric for (uint32_t symIndex : symIndices) { 16480b57cec5SDimitry Andric if (symIndex >= objSymbols.size()) { 16490b57cec5SDimitry Andric warn("ignoring invalid symbol table index in section " + 16500b57cec5SDimitry Andric c->getSectionName() + " in object " + toString(file)); 16510b57cec5SDimitry Andric continue; 16520b57cec5SDimitry Andric } 16530b57cec5SDimitry Andric if (Symbol *s = objSymbols[symIndex]) { 16540b57cec5SDimitry Andric if (s->isLive()) 16550b57cec5SDimitry Andric addSymbolToRVASet(tableSymbols, cast<Defined>(s)); 16560b57cec5SDimitry Andric } 16570b57cec5SDimitry Andric } 16580b57cec5SDimitry Andric } 16590b57cec5SDimitry Andric } 16600b57cec5SDimitry Andric 16610b57cec5SDimitry Andric // Replace the absolute table symbol with a synthetic symbol pointing to 16620b57cec5SDimitry Andric // tableChunk so that we can emit base relocations for it and resolve section 16630b57cec5SDimitry Andric // relative relocations. 16640b57cec5SDimitry Andric void Writer::maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym, 16650b57cec5SDimitry Andric StringRef countSym) { 16660b57cec5SDimitry Andric if (tableSymbols.empty()) 16670b57cec5SDimitry Andric return; 16680b57cec5SDimitry Andric 16690b57cec5SDimitry Andric RVATableChunk *tableChunk = make<RVATableChunk>(std::move(tableSymbols)); 16700b57cec5SDimitry Andric rdataSec->addChunk(tableChunk); 16710b57cec5SDimitry Andric 16720b57cec5SDimitry Andric Symbol *t = symtab->findUnderscore(tableSym); 16730b57cec5SDimitry Andric Symbol *c = symtab->findUnderscore(countSym); 16740b57cec5SDimitry Andric replaceSymbol<DefinedSynthetic>(t, t->getName(), tableChunk); 16750b57cec5SDimitry Andric cast<DefinedAbsolute>(c)->setVA(tableChunk->getSize() / 4); 16760b57cec5SDimitry Andric } 16770b57cec5SDimitry Andric 16780b57cec5SDimitry Andric // MinGW specific. Gather all relocations that are imported from a DLL even 16790b57cec5SDimitry Andric // though the code didn't expect it to, produce the table that the runtime 16800b57cec5SDimitry Andric // uses for fixing them up, and provide the synthetic symbols that the 16810b57cec5SDimitry Andric // runtime uses for finding the table. 16820b57cec5SDimitry Andric void Writer::createRuntimePseudoRelocs() { 16830b57cec5SDimitry Andric std::vector<RuntimePseudoReloc> rels; 16840b57cec5SDimitry Andric 16850b57cec5SDimitry Andric for (Chunk *c : symtab->getChunks()) { 16860b57cec5SDimitry Andric auto *sc = dyn_cast<SectionChunk>(c); 16870b57cec5SDimitry Andric if (!sc || !sc->live) 16880b57cec5SDimitry Andric continue; 16890b57cec5SDimitry Andric sc->getRuntimePseudoRelocs(rels); 16900b57cec5SDimitry Andric } 16910b57cec5SDimitry Andric 16920b57cec5SDimitry Andric if (!rels.empty()) 16930b57cec5SDimitry Andric log("Writing " + Twine(rels.size()) + " runtime pseudo relocations"); 16940b57cec5SDimitry Andric PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(rels); 16950b57cec5SDimitry Andric rdataSec->addChunk(table); 16960b57cec5SDimitry Andric EmptyChunk *endOfList = make<EmptyChunk>(); 16970b57cec5SDimitry Andric rdataSec->addChunk(endOfList); 16980b57cec5SDimitry Andric 16990b57cec5SDimitry Andric Symbol *headSym = symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__"); 17000b57cec5SDimitry Andric Symbol *endSym = symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__"); 17010b57cec5SDimitry Andric replaceSymbol<DefinedSynthetic>(headSym, headSym->getName(), table); 17020b57cec5SDimitry Andric replaceSymbol<DefinedSynthetic>(endSym, endSym->getName(), endOfList); 17030b57cec5SDimitry Andric } 17040b57cec5SDimitry Andric 17050b57cec5SDimitry Andric // MinGW specific. 17060b57cec5SDimitry Andric // The MinGW .ctors and .dtors lists have sentinels at each end; 17070b57cec5SDimitry Andric // a (uintptr_t)-1 at the start and a (uintptr_t)0 at the end. 17080b57cec5SDimitry Andric // There's a symbol pointing to the start sentinel pointer, __CTOR_LIST__ 17090b57cec5SDimitry Andric // and __DTOR_LIST__ respectively. 17100b57cec5SDimitry Andric void Writer::insertCtorDtorSymbols() { 17110b57cec5SDimitry Andric AbsolutePointerChunk *ctorListHead = make<AbsolutePointerChunk>(-1); 17120b57cec5SDimitry Andric AbsolutePointerChunk *ctorListEnd = make<AbsolutePointerChunk>(0); 17130b57cec5SDimitry Andric AbsolutePointerChunk *dtorListHead = make<AbsolutePointerChunk>(-1); 17140b57cec5SDimitry Andric AbsolutePointerChunk *dtorListEnd = make<AbsolutePointerChunk>(0); 17150b57cec5SDimitry Andric ctorsSec->insertChunkAtStart(ctorListHead); 17160b57cec5SDimitry Andric ctorsSec->addChunk(ctorListEnd); 17170b57cec5SDimitry Andric dtorsSec->insertChunkAtStart(dtorListHead); 17180b57cec5SDimitry Andric dtorsSec->addChunk(dtorListEnd); 17190b57cec5SDimitry Andric 17200b57cec5SDimitry Andric Symbol *ctorListSym = symtab->findUnderscore("__CTOR_LIST__"); 17210b57cec5SDimitry Andric Symbol *dtorListSym = symtab->findUnderscore("__DTOR_LIST__"); 17220b57cec5SDimitry Andric replaceSymbol<DefinedSynthetic>(ctorListSym, ctorListSym->getName(), 17230b57cec5SDimitry Andric ctorListHead); 17240b57cec5SDimitry Andric replaceSymbol<DefinedSynthetic>(dtorListSym, dtorListSym->getName(), 17250b57cec5SDimitry Andric dtorListHead); 17260b57cec5SDimitry Andric } 17270b57cec5SDimitry Andric 17280b57cec5SDimitry Andric // Handles /section options to allow users to overwrite 17290b57cec5SDimitry Andric // section attributes. 17300b57cec5SDimitry Andric void Writer::setSectionPermissions() { 17310b57cec5SDimitry Andric for (auto &p : config->section) { 17320b57cec5SDimitry Andric StringRef name = p.first; 17330b57cec5SDimitry Andric uint32_t perm = p.second; 17340b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 17350b57cec5SDimitry Andric if (sec->name == name) 17360b57cec5SDimitry Andric sec->setPermissions(perm); 17370b57cec5SDimitry Andric } 17380b57cec5SDimitry Andric } 17390b57cec5SDimitry Andric 17400b57cec5SDimitry Andric // Write section contents to a mmap'ed file. 17410b57cec5SDimitry Andric void Writer::writeSections() { 17420b57cec5SDimitry Andric // Record the number of sections to apply section index relocations 17430b57cec5SDimitry Andric // against absolute symbols. See applySecIdx in Chunks.cpp.. 17440b57cec5SDimitry Andric DefinedAbsolute::numOutputSections = outputSections.size(); 17450b57cec5SDimitry Andric 17460b57cec5SDimitry Andric uint8_t *buf = buffer->getBufferStart(); 17470b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 17480b57cec5SDimitry Andric uint8_t *secBuf = buf + sec->getFileOff(); 17490b57cec5SDimitry Andric // Fill gaps between functions in .text with INT3 instructions 17500b57cec5SDimitry Andric // instead of leaving as NUL bytes (which can be interpreted as 17510b57cec5SDimitry Andric // ADD instructions). 17520b57cec5SDimitry Andric if (sec->header.Characteristics & IMAGE_SCN_CNT_CODE) 17530b57cec5SDimitry Andric memset(secBuf, 0xCC, sec->getRawSize()); 17540b57cec5SDimitry Andric parallelForEach(sec->chunks, [&](Chunk *c) { 17550b57cec5SDimitry Andric c->writeTo(secBuf + c->getRVA() - sec->getRVA()); 17560b57cec5SDimitry Andric }); 17570b57cec5SDimitry Andric } 17580b57cec5SDimitry Andric } 17590b57cec5SDimitry Andric 17600b57cec5SDimitry Andric void Writer::writeBuildId() { 17610b57cec5SDimitry Andric // There are two important parts to the build ID. 17620b57cec5SDimitry Andric // 1) If building with debug info, the COFF debug directory contains a 17630b57cec5SDimitry Andric // timestamp as well as a Guid and Age of the PDB. 17640b57cec5SDimitry Andric // 2) In all cases, the PE COFF file header also contains a timestamp. 17650b57cec5SDimitry Andric // For reproducibility, instead of a timestamp we want to use a hash of the 17660b57cec5SDimitry Andric // PE contents. 17670b57cec5SDimitry Andric if (config->debug) { 17680b57cec5SDimitry Andric assert(buildId && "BuildId is not set!"); 17690b57cec5SDimitry Andric // BuildId->BuildId was filled in when the PDB was written. 17700b57cec5SDimitry Andric } 17710b57cec5SDimitry Andric 17720b57cec5SDimitry Andric // At this point the only fields in the COFF file which remain unset are the 17730b57cec5SDimitry Andric // "timestamp" in the COFF file header, and the ones in the coff debug 17740b57cec5SDimitry Andric // directory. Now we can hash the file and write that hash to the various 17750b57cec5SDimitry Andric // timestamp fields in the file. 17760b57cec5SDimitry Andric StringRef outputFileData( 17770b57cec5SDimitry Andric reinterpret_cast<const char *>(buffer->getBufferStart()), 17780b57cec5SDimitry Andric buffer->getBufferSize()); 17790b57cec5SDimitry Andric 17800b57cec5SDimitry Andric uint32_t timestamp = config->timestamp; 17810b57cec5SDimitry Andric uint64_t hash = 0; 17820b57cec5SDimitry Andric bool generateSyntheticBuildId = 17830b57cec5SDimitry Andric config->mingw && config->debug && config->pdbPath.empty(); 17840b57cec5SDimitry Andric 17850b57cec5SDimitry Andric if (config->repro || generateSyntheticBuildId) 17860b57cec5SDimitry Andric hash = xxHash64(outputFileData); 17870b57cec5SDimitry Andric 17880b57cec5SDimitry Andric if (config->repro) 17890b57cec5SDimitry Andric timestamp = static_cast<uint32_t>(hash); 17900b57cec5SDimitry Andric 17910b57cec5SDimitry Andric if (generateSyntheticBuildId) { 17920b57cec5SDimitry Andric // For MinGW builds without a PDB file, we still generate a build id 17930b57cec5SDimitry Andric // to allow associating a crash dump to the executable. 17940b57cec5SDimitry Andric buildId->buildId->PDB70.CVSignature = OMF::Signature::PDB70; 17950b57cec5SDimitry Andric buildId->buildId->PDB70.Age = 1; 17960b57cec5SDimitry Andric memcpy(buildId->buildId->PDB70.Signature, &hash, 8); 17970b57cec5SDimitry Andric // xxhash only gives us 8 bytes, so put some fixed data in the other half. 17980b57cec5SDimitry Andric memcpy(&buildId->buildId->PDB70.Signature[8], "LLD PDB.", 8); 17990b57cec5SDimitry Andric } 18000b57cec5SDimitry Andric 18010b57cec5SDimitry Andric if (debugDirectory) 18020b57cec5SDimitry Andric debugDirectory->setTimeDateStamp(timestamp); 18030b57cec5SDimitry Andric 18040b57cec5SDimitry Andric uint8_t *buf = buffer->getBufferStart(); 18050b57cec5SDimitry Andric buf += dosStubSize + sizeof(PEMagic); 18060b57cec5SDimitry Andric object::coff_file_header *coffHeader = 18070b57cec5SDimitry Andric reinterpret_cast<coff_file_header *>(buf); 18080b57cec5SDimitry Andric coffHeader->TimeDateStamp = timestamp; 18090b57cec5SDimitry Andric } 18100b57cec5SDimitry Andric 18110b57cec5SDimitry Andric // Sort .pdata section contents according to PE/COFF spec 5.5. 18120b57cec5SDimitry Andric void Writer::sortExceptionTable() { 18130b57cec5SDimitry Andric if (!firstPdata) 18140b57cec5SDimitry Andric return; 18150b57cec5SDimitry Andric // We assume .pdata contains function table entries only. 18160b57cec5SDimitry Andric auto bufAddr = [&](Chunk *c) { 18170b57cec5SDimitry Andric OutputSection *os = c->getOutputSection(); 18180b57cec5SDimitry Andric return buffer->getBufferStart() + os->getFileOff() + c->getRVA() - 18190b57cec5SDimitry Andric os->getRVA(); 18200b57cec5SDimitry Andric }; 18210b57cec5SDimitry Andric uint8_t *begin = bufAddr(firstPdata); 18220b57cec5SDimitry Andric uint8_t *end = bufAddr(lastPdata) + lastPdata->getSize(); 18230b57cec5SDimitry Andric if (config->machine == AMD64) { 18240b57cec5SDimitry Andric struct Entry { ulittle32_t begin, end, unwind; }; 18250b57cec5SDimitry Andric parallelSort( 18260b57cec5SDimitry Andric MutableArrayRef<Entry>((Entry *)begin, (Entry *)end), 18270b57cec5SDimitry Andric [](const Entry &a, const Entry &b) { return a.begin < b.begin; }); 18280b57cec5SDimitry Andric return; 18290b57cec5SDimitry Andric } 18300b57cec5SDimitry Andric if (config->machine == ARMNT || config->machine == ARM64) { 18310b57cec5SDimitry Andric struct Entry { ulittle32_t begin, unwind; }; 18320b57cec5SDimitry Andric parallelSort( 18330b57cec5SDimitry Andric MutableArrayRef<Entry>((Entry *)begin, (Entry *)end), 18340b57cec5SDimitry Andric [](const Entry &a, const Entry &b) { return a.begin < b.begin; }); 18350b57cec5SDimitry Andric return; 18360b57cec5SDimitry Andric } 18370b57cec5SDimitry Andric errs() << "warning: don't know how to handle .pdata.\n"; 18380b57cec5SDimitry Andric } 18390b57cec5SDimitry Andric 18400b57cec5SDimitry Andric // The CRT section contains, among other things, the array of function 18410b57cec5SDimitry Andric // pointers that initialize every global variable that is not trivially 18420b57cec5SDimitry Andric // constructed. The CRT calls them one after the other prior to invoking 18430b57cec5SDimitry Andric // main(). 18440b57cec5SDimitry Andric // 18450b57cec5SDimitry Andric // As per C++ spec, 3.6.2/2.3, 18460b57cec5SDimitry Andric // "Variables with ordered initialization defined within a single 18470b57cec5SDimitry Andric // translation unit shall be initialized in the order of their definitions 18480b57cec5SDimitry Andric // in the translation unit" 18490b57cec5SDimitry Andric // 18500b57cec5SDimitry Andric // It is therefore critical to sort the chunks containing the function 18510b57cec5SDimitry Andric // pointers in the order that they are listed in the object file (top to 18520b57cec5SDimitry Andric // bottom), otherwise global objects might not be initialized in the 18530b57cec5SDimitry Andric // correct order. 18540b57cec5SDimitry Andric void Writer::sortCRTSectionChunks(std::vector<Chunk *> &chunks) { 18550b57cec5SDimitry Andric auto sectionChunkOrder = [](const Chunk *a, const Chunk *b) { 18560b57cec5SDimitry Andric auto sa = dyn_cast<SectionChunk>(a); 18570b57cec5SDimitry Andric auto sb = dyn_cast<SectionChunk>(b); 18580b57cec5SDimitry Andric assert(sa && sb && "Non-section chunks in CRT section!"); 18590b57cec5SDimitry Andric 18600b57cec5SDimitry Andric StringRef sAObj = sa->file->mb.getBufferIdentifier(); 18610b57cec5SDimitry Andric StringRef sBObj = sb->file->mb.getBufferIdentifier(); 18620b57cec5SDimitry Andric 18630b57cec5SDimitry Andric return sAObj == sBObj && sa->getSectionNumber() < sb->getSectionNumber(); 18640b57cec5SDimitry Andric }; 18650b57cec5SDimitry Andric llvm::stable_sort(chunks, sectionChunkOrder); 18660b57cec5SDimitry Andric 18670b57cec5SDimitry Andric if (config->verbose) { 18680b57cec5SDimitry Andric for (auto &c : chunks) { 18690b57cec5SDimitry Andric auto sc = dyn_cast<SectionChunk>(c); 18700b57cec5SDimitry Andric log(" " + sc->file->mb.getBufferIdentifier().str() + 18710b57cec5SDimitry Andric ", SectionID: " + Twine(sc->getSectionNumber())); 18720b57cec5SDimitry Andric } 18730b57cec5SDimitry Andric } 18740b57cec5SDimitry Andric } 18750b57cec5SDimitry Andric 18760b57cec5SDimitry Andric OutputSection *Writer::findSection(StringRef name) { 18770b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 18780b57cec5SDimitry Andric if (sec->name == name) 18790b57cec5SDimitry Andric return sec; 18800b57cec5SDimitry Andric return nullptr; 18810b57cec5SDimitry Andric } 18820b57cec5SDimitry Andric 18830b57cec5SDimitry Andric uint32_t Writer::getSizeOfInitializedData() { 18840b57cec5SDimitry Andric uint32_t res = 0; 18850b57cec5SDimitry Andric for (OutputSection *s : outputSections) 18860b57cec5SDimitry Andric if (s->header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) 18870b57cec5SDimitry Andric res += s->getRawSize(); 18880b57cec5SDimitry Andric return res; 18890b57cec5SDimitry Andric } 18900b57cec5SDimitry Andric 18910b57cec5SDimitry Andric // Add base relocations to .reloc section. 18920b57cec5SDimitry Andric void Writer::addBaserels() { 18930b57cec5SDimitry Andric if (!config->relocatable) 18940b57cec5SDimitry Andric return; 18950b57cec5SDimitry Andric relocSec->chunks.clear(); 18960b57cec5SDimitry Andric std::vector<Baserel> v; 18970b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 18980b57cec5SDimitry Andric if (sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) 18990b57cec5SDimitry Andric continue; 19000b57cec5SDimitry Andric // Collect all locations for base relocations. 19010b57cec5SDimitry Andric for (Chunk *c : sec->chunks) 19020b57cec5SDimitry Andric c->getBaserels(&v); 19030b57cec5SDimitry Andric // Add the addresses to .reloc section. 19040b57cec5SDimitry Andric if (!v.empty()) 19050b57cec5SDimitry Andric addBaserelBlocks(v); 19060b57cec5SDimitry Andric v.clear(); 19070b57cec5SDimitry Andric } 19080b57cec5SDimitry Andric } 19090b57cec5SDimitry Andric 19100b57cec5SDimitry Andric // Add addresses to .reloc section. Note that addresses are grouped by page. 19110b57cec5SDimitry Andric void Writer::addBaserelBlocks(std::vector<Baserel> &v) { 19120b57cec5SDimitry Andric const uint32_t mask = ~uint32_t(pageSize - 1); 19130b57cec5SDimitry Andric uint32_t page = v[0].rva & mask; 19140b57cec5SDimitry Andric size_t i = 0, j = 1; 19150b57cec5SDimitry Andric for (size_t e = v.size(); j < e; ++j) { 19160b57cec5SDimitry Andric uint32_t p = v[j].rva & mask; 19170b57cec5SDimitry Andric if (p == page) 19180b57cec5SDimitry Andric continue; 19190b57cec5SDimitry Andric relocSec->addChunk(make<BaserelChunk>(page, &v[i], &v[0] + j)); 19200b57cec5SDimitry Andric i = j; 19210b57cec5SDimitry Andric page = p; 19220b57cec5SDimitry Andric } 19230b57cec5SDimitry Andric if (i == j) 19240b57cec5SDimitry Andric return; 19250b57cec5SDimitry Andric relocSec->addChunk(make<BaserelChunk>(page, &v[i], &v[0] + j)); 19260b57cec5SDimitry Andric } 19270b57cec5SDimitry Andric 19280b57cec5SDimitry Andric PartialSection *Writer::createPartialSection(StringRef name, 19290b57cec5SDimitry Andric uint32_t outChars) { 19300b57cec5SDimitry Andric PartialSection *&pSec = partialSections[{name, outChars}]; 19310b57cec5SDimitry Andric if (pSec) 19320b57cec5SDimitry Andric return pSec; 19330b57cec5SDimitry Andric pSec = make<PartialSection>(name, outChars); 19340b57cec5SDimitry Andric return pSec; 19350b57cec5SDimitry Andric } 19360b57cec5SDimitry Andric 19370b57cec5SDimitry Andric PartialSection *Writer::findPartialSection(StringRef name, uint32_t outChars) { 19380b57cec5SDimitry Andric auto it = partialSections.find({name, outChars}); 19390b57cec5SDimitry Andric if (it != partialSections.end()) 19400b57cec5SDimitry Andric return it->second; 19410b57cec5SDimitry Andric return nullptr; 19420b57cec5SDimitry Andric } 1943*85868e8aSDimitry Andric 1944*85868e8aSDimitry Andric } // namespace coff 1945*85868e8aSDimitry Andric } // namespace lld 1946