1 //===- Writer.cpp ---------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
12 #include "InputFiles.h"
15 #include "SymbolTable.h"
17 #include "lld/Common/ErrorHandler.h"
18 #include "lld/Common/Memory.h"
19 #include "lld/Common/Threads.h"
20 #include "lld/Common/Timer.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/StringSwitch.h"
24 #include "llvm/Support/BinaryStreamReader.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/FileOutputBuffer.h"
28 #include "llvm/Support/Parallel.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/RandomNumberGenerator.h"
31 #include "llvm/Support/xxhash.h"
39 using namespace llvm::COFF;
40 using namespace llvm::object;
41 using namespace llvm::support;
42 using namespace llvm::support::endian;
44 using namespace lld::coff;
46 /* To re-generate DOSProgram:
47 $ cat > /tmp/DOSProgram.asm
52 ; Point ds:dx at the $-terminated string.
54 ; Int 21/AH=09h: Write string to standard output.
57 ; Int 21/AH=4Ch: Exit with return code (in AL).
61 db 'This program cannot be run in DOS mode.$'
63 $ nasm -fbin /tmp/DOSProgram.asm -o /tmp/DOSProgram.bin
64 $ xxd -i /tmp/DOSProgram.bin
66 static unsigned char dosProgram[] = {
67 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8, 0x01, 0x4c,
68 0xcd, 0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72,
69 0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x62, 0x65,
70 0x20, 0x72, 0x75, 0x6e, 0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20,
71 0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x24, 0x00, 0x00
73 static_assert(sizeof(dosProgram) % 8 == 0,
74 "DOSProgram size must be multiple of 8");
76 static const int dosStubSize = sizeof(dos_header) + sizeof(dosProgram);
77 static_assert(dosStubSize % 8 == 0, "DOSStub size must be multiple of 8");
79 static const int numberOfDataDirectory = 16;
81 // Global vector of all output sections. After output sections are finalized,
82 // this can be indexed by Chunk::getOutputSection.
83 static std::vector<OutputSection *> outputSections;
85 OutputSection *Chunk::getOutputSection() const {
86 return osidx == 0 ? nullptr : outputSections[osidx - 1];
91 class DebugDirectoryChunk : public NonSectionChunk {
93 DebugDirectoryChunk(const std::vector<Chunk *> &r, bool writeRepro)
94 : records(r), writeRepro(writeRepro) {}
96 size_t getSize() const override {
97 return (records.size() + int(writeRepro)) * sizeof(debug_directory);
100 void writeTo(uint8_t *b) const override {
101 auto *d = reinterpret_cast<debug_directory *>(b);
103 for (const Chunk *record : records) {
104 OutputSection *os = record->getOutputSection();
105 uint64_t offs = os->getFileOff() + (record->getRVA() - os->getRVA());
106 fillEntry(d, COFF::IMAGE_DEBUG_TYPE_CODEVIEW, record->getSize(),
107 record->getRVA(), offs);
112 // FIXME: The COFF spec allows either a 0-sized entry to just say
113 // "the timestamp field is really a hash", or a 4-byte size field
114 // followed by that many bytes containing a longer hash (with the
115 // lowest 4 bytes usually being the timestamp in little-endian order).
116 // Consider storing the full 8 bytes computed by xxHash64 here.
117 fillEntry(d, COFF::IMAGE_DEBUG_TYPE_REPRO, 0, 0, 0);
121 void setTimeDateStamp(uint32_t timeDateStamp) {
122 for (support::ulittle32_t *tds : timeDateStamps)
123 *tds = timeDateStamp;
127 void fillEntry(debug_directory *d, COFF::DebugType debugType, size_t size,
128 uint64_t rva, uint64_t offs) const {
129 d->Characteristics = 0;
130 d->TimeDateStamp = 0;
134 d->SizeOfData = size;
135 d->AddressOfRawData = rva;
136 d->PointerToRawData = offs;
138 timeDateStamps.push_back(&d->TimeDateStamp);
141 mutable std::vector<support::ulittle32_t *> timeDateStamps;
142 const std::vector<Chunk *> &records;
146 class CVDebugRecordChunk : public NonSectionChunk {
148 size_t getSize() const override {
149 return sizeof(codeview::DebugInfo) + config->pdbAltPath.size() + 1;
152 void writeTo(uint8_t *b) const override {
153 // Save off the DebugInfo entry to backfill the file signature (build id)
154 // in Writer::writeBuildId
155 buildId = reinterpret_cast<codeview::DebugInfo *>(b);
157 // variable sized field (PDB Path)
158 char *p = reinterpret_cast<char *>(b + sizeof(*buildId));
159 if (!config->pdbAltPath.empty())
160 memcpy(p, config->pdbAltPath.data(), config->pdbAltPath.size());
161 p[config->pdbAltPath.size()] = '\0';
164 mutable codeview::DebugInfo *buildId = nullptr;
167 // PartialSection represents a group of chunks that contribute to an
168 // OutputSection. Collating a collection of PartialSections of same name and
169 // characteristics constitutes the OutputSection.
170 class PartialSectionKey {
173 unsigned characteristics;
175 bool operator<(const PartialSectionKey &other) const {
176 int c = name.compare(other.name);
180 return characteristics < other.characteristics;
185 // The writer writes a SymbolTable result to a file.
188 Writer() : buffer(errorHandler().outputBuffer) {}
192 void createSections();
193 void createMiscChunks();
194 void createImportTables();
195 void appendImportThunks();
196 void locateImportTables();
197 void createExportTable();
198 void mergeSections();
199 void removeUnusedSections();
200 void assignAddresses();
201 void finalizeAddresses();
202 void removeEmptySections();
203 void assignOutputSectionIndices();
204 void createSymbolAndStringTable();
205 void openFile(StringRef outputPath);
206 template <typename PEHeaderTy> void writeHeader();
207 void createSEHTable();
208 void createRuntimePseudoRelocs();
209 void insertCtorDtorSymbols();
210 void createGuardCFTables();
211 void markSymbolsForRVATable(ObjFile *file,
212 ArrayRef<SectionChunk *> symIdxChunks,
213 SymbolRVASet &tableSymbols);
214 void maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
216 void setSectionPermissions();
217 void writeSections();
219 void sortExceptionTable();
220 void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
221 void addSyntheticIdata();
222 void fixPartialSectionChars(StringRef name, uint32_t chars);
223 bool fixGnuImportChunks();
224 PartialSection *createPartialSection(StringRef name, uint32_t outChars);
225 PartialSection *findPartialSection(StringRef name, uint32_t outChars);
227 llvm::Optional<coff_symbol16> createSymbol(Defined *d);
228 size_t addEntryToStringTable(StringRef str);
230 OutputSection *findSection(StringRef name);
232 void addBaserelBlocks(std::vector<Baserel> &v);
234 uint32_t getSizeOfInitializedData();
236 std::unique_ptr<FileOutputBuffer> &buffer;
237 std::map<PartialSectionKey, PartialSection *> partialSections;
238 std::vector<char> strtab;
239 std::vector<llvm::object::coff_symbol16> outputSymtab;
241 Chunk *importTableStart = nullptr;
242 uint64_t importTableSize = 0;
243 Chunk *iatStart = nullptr;
244 uint64_t iatSize = 0;
245 DelayLoadContents delayIdata;
247 bool setNoSEHCharacteristic = false;
249 DebugDirectoryChunk *debugDirectory = nullptr;
250 std::vector<Chunk *> debugRecords;
251 CVDebugRecordChunk *buildId = nullptr;
252 ArrayRef<uint8_t> sectionTable;
255 uint32_t pointerToSymbolTable = 0;
256 uint64_t sizeOfImage;
257 uint64_t sizeOfHeaders;
259 OutputSection *textSec;
260 OutputSection *rdataSec;
261 OutputSection *buildidSec;
262 OutputSection *dataSec;
263 OutputSection *pdataSec;
264 OutputSection *idataSec;
265 OutputSection *edataSec;
266 OutputSection *didatSec;
267 OutputSection *rsrcSec;
268 OutputSection *relocSec;
269 OutputSection *ctorsSec;
270 OutputSection *dtorsSec;
272 // The first and last .pdata sections in the output file.
274 // We need to keep track of the location of .pdata in whichever section it
275 // gets merged into so that we can sort its contents and emit a correct data
276 // directory entry for the exception table. This is also the case for some
277 // other sections (such as .edata) but because the contents of those sections
278 // are entirely linker-generated we can keep track of their locations using
279 // the chunks that the linker creates. All .pdata chunks come from input
280 // files, so we need to keep track of them separately.
281 Chunk *firstPdata = nullptr;
284 } // anonymous namespace
289 static Timer codeLayoutTimer("Code Layout", Timer::root());
290 static Timer diskCommitTimer("Commit Output File", Timer::root());
292 void writeResult() { Writer().run(); }
294 void OutputSection::addChunk(Chunk *c) {
298 void OutputSection::insertChunkAtStart(Chunk *c) {
299 chunks.insert(chunks.begin(), c);
302 void OutputSection::setPermissions(uint32_t c) {
303 header.Characteristics &= ~permMask;
304 header.Characteristics |= c;
307 void OutputSection::merge(OutputSection *other) {
308 chunks.insert(chunks.end(), other->chunks.begin(), other->chunks.end());
309 other->chunks.clear();
310 contribSections.insert(contribSections.end(), other->contribSections.begin(),
311 other->contribSections.end());
312 other->contribSections.clear();
315 // Write the section header to a given buffer.
316 void OutputSection::writeHeaderTo(uint8_t *buf) {
317 auto *hdr = reinterpret_cast<coff_section *>(buf);
319 if (stringTableOff) {
320 // If name is too long, write offset into the string table as a name.
321 sprintf(hdr->Name, "/%d", stringTableOff);
323 assert(!config->debug || name.size() <= COFF::NameSize ||
324 (hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0);
325 strncpy(hdr->Name, name.data(),
326 std::min(name.size(), (size_t)COFF::NameSize));
330 void OutputSection::addContributingPartialSection(PartialSection *sec) {
331 contribSections.push_back(sec);
337 // Check whether the target address S is in range from a relocation
338 // of type relType at address P.
339 static bool isInRange(uint16_t relType, uint64_t s, uint64_t p, int margin) {
340 if (config->machine == ARMNT) {
341 int64_t diff = AbsoluteDifference(s, p + 4) + margin;
343 case IMAGE_REL_ARM_BRANCH20T:
344 return isInt<21>(diff);
345 case IMAGE_REL_ARM_BRANCH24T:
346 case IMAGE_REL_ARM_BLX23T:
347 return isInt<25>(diff);
351 } else if (config->machine == ARM64) {
352 int64_t diff = AbsoluteDifference(s, p) + margin;
354 case IMAGE_REL_ARM64_BRANCH26:
355 return isInt<28>(diff);
356 case IMAGE_REL_ARM64_BRANCH19:
357 return isInt<21>(diff);
358 case IMAGE_REL_ARM64_BRANCH14:
359 return isInt<16>(diff);
364 llvm_unreachable("Unexpected architecture");
368 // Return the last thunk for the given target if it is in range,
369 // or create a new one.
370 static std::pair<Defined *, bool>
371 getThunk(DenseMap<uint64_t, Defined *> &lastThunks, Defined *target, uint64_t p,
372 uint16_t type, int margin) {
373 Defined *&lastThunk = lastThunks[target->getRVA()];
374 if (lastThunk && isInRange(type, lastThunk->getRVA(), p, margin))
375 return {lastThunk, false};
377 switch (config->machine) {
379 c = make<RangeExtensionThunkARM>(target);
382 c = make<RangeExtensionThunkARM64>(target);
385 llvm_unreachable("Unexpected architecture");
387 Defined *d = make<DefinedSynthetic>("", c);
392 // This checks all relocations, and for any relocation which isn't in range
393 // it adds a thunk after the section chunk that contains the relocation.
394 // If the latest thunk for the specific target is in range, that is used
395 // instead of creating a new thunk. All range checks are done with the
396 // specified margin, to make sure that relocations that originally are in
397 // range, but only barely, also get thunks - in case other added thunks makes
398 // the target go out of range.
400 // After adding thunks, we verify that all relocations are in range (with
401 // no extra margin requirements). If this failed, we restart (throwing away
402 // the previously created thunks) and retry with a wider margin.
403 static bool createThunks(OutputSection *os, int margin) {
404 bool addressesChanged = false;
405 DenseMap<uint64_t, Defined *> lastThunks;
406 DenseMap<std::pair<ObjFile *, Defined *>, uint32_t> thunkSymtabIndices;
407 size_t thunksSize = 0;
408 // Recheck Chunks.size() each iteration, since we can insert more
410 for (size_t i = 0; i != os->chunks.size(); ++i) {
411 SectionChunk *sc = dyn_cast_or_null<SectionChunk>(os->chunks[i]);
414 size_t thunkInsertionSpot = i + 1;
416 // Try to get a good enough estimate of where new thunks will be placed.
417 // Offset this by the size of the new thunks added so far, to make the
418 // estimate slightly better.
419 size_t thunkInsertionRVA = sc->getRVA() + sc->getSize() + thunksSize;
420 ObjFile *file = sc->file;
421 std::vector<std::pair<uint32_t, uint32_t>> relocReplacements;
422 ArrayRef<coff_relocation> originalRelocs =
423 file->getCOFFObj()->getRelocations(sc->header);
424 for (size_t j = 0, e = originalRelocs.size(); j < e; ++j) {
425 const coff_relocation &rel = originalRelocs[j];
426 Symbol *relocTarget = file->getSymbol(rel.SymbolTableIndex);
428 // The estimate of the source address P should be pretty accurate,
429 // but we don't know whether the target Symbol address should be
430 // offset by thunksSize or not (or by some of thunksSize but not all of
431 // it), giving us some uncertainty once we have added one thunk.
432 uint64_t p = sc->getRVA() + rel.VirtualAddress + thunksSize;
434 Defined *sym = dyn_cast_or_null<Defined>(relocTarget);
438 uint64_t s = sym->getRVA();
440 if (isInRange(rel.Type, s, p, margin))
443 // If the target isn't in range, hook it up to an existing or new
447 std::tie(thunk, wasNew) = getThunk(lastThunks, sym, p, rel.Type, margin);
449 Chunk *thunkChunk = thunk->getChunk();
451 thunkInsertionRVA); // Estimate of where it will be located.
452 os->chunks.insert(os->chunks.begin() + thunkInsertionSpot, thunkChunk);
453 thunkInsertionSpot++;
454 thunksSize += thunkChunk->getSize();
455 thunkInsertionRVA += thunkChunk->getSize();
456 addressesChanged = true;
459 // To redirect the relocation, add a symbol to the parent object file's
460 // symbol table, and replace the relocation symbol table index with the
462 auto insertion = thunkSymtabIndices.insert({{file, thunk}, ~0U});
463 uint32_t &thunkSymbolIndex = insertion.first->second;
464 if (insertion.second)
465 thunkSymbolIndex = file->addRangeThunkSymbol(thunk);
466 relocReplacements.push_back({j, thunkSymbolIndex});
469 // Get a writable copy of this section's relocations so they can be
470 // modified. If the relocations point into the object file, allocate new
471 // memory. Otherwise, this must be previously allocated memory that can be
472 // modified in place.
473 ArrayRef<coff_relocation> curRelocs = sc->getRelocs();
474 MutableArrayRef<coff_relocation> newRelocs;
475 if (originalRelocs.data() == curRelocs.data()) {
476 newRelocs = makeMutableArrayRef(
477 bAlloc.Allocate<coff_relocation>(originalRelocs.size()),
478 originalRelocs.size());
480 newRelocs = makeMutableArrayRef(
481 const_cast<coff_relocation *>(curRelocs.data()), curRelocs.size());
484 // Copy each relocation, but replace the symbol table indices which need
486 auto nextReplacement = relocReplacements.begin();
487 auto endReplacement = relocReplacements.end();
488 for (size_t i = 0, e = originalRelocs.size(); i != e; ++i) {
489 newRelocs[i] = originalRelocs[i];
490 if (nextReplacement != endReplacement && nextReplacement->first == i) {
491 newRelocs[i].SymbolTableIndex = nextReplacement->second;
496 sc->setRelocs(newRelocs);
498 return addressesChanged;
501 // Verify that all relocations are in range, with no extra margin requirements.
502 static bool verifyRanges(const std::vector<Chunk *> chunks) {
503 for (Chunk *c : chunks) {
504 SectionChunk *sc = dyn_cast_or_null<SectionChunk>(c);
508 ArrayRef<coff_relocation> relocs = sc->getRelocs();
509 for (size_t j = 0, e = relocs.size(); j < e; ++j) {
510 const coff_relocation &rel = relocs[j];
511 Symbol *relocTarget = sc->file->getSymbol(rel.SymbolTableIndex);
513 Defined *sym = dyn_cast_or_null<Defined>(relocTarget);
517 uint64_t p = sc->getRVA() + rel.VirtualAddress;
518 uint64_t s = sym->getRVA();
520 if (!isInRange(rel.Type, s, p, 0))
527 // Assign addresses and add thunks if necessary.
528 void Writer::finalizeAddresses() {
530 if (config->machine != ARMNT && config->machine != ARM64)
533 size_t origNumChunks = 0;
534 for (OutputSection *sec : outputSections) {
535 sec->origChunks = sec->chunks;
536 origNumChunks += sec->chunks.size();
540 int margin = 1024 * 100;
542 // First check whether we need thunks at all, or if the previous pass of
543 // adding them turned out ok.
544 bool rangesOk = true;
545 size_t numChunks = 0;
546 for (OutputSection *sec : outputSections) {
547 if (!verifyRanges(sec->chunks)) {
551 numChunks += sec->chunks.size();
555 log("Added " + Twine(numChunks - origNumChunks) + " thunks with " +
556 "margin " + Twine(margin) + " in " + Twine(pass) + " passes");
561 fatal("adding thunks hasn't converged after " + Twine(pass) + " passes");
564 // If the previous pass didn't work out, reset everything back to the
565 // original conditions before retrying with a wider margin. This should
566 // ideally never happen under real circumstances.
567 for (OutputSection *sec : outputSections)
568 sec->chunks = sec->origChunks;
572 // Try adding thunks everywhere where it is needed, with a margin
573 // to avoid things going out of range due to the added thunks.
574 bool addressesChanged = false;
575 for (OutputSection *sec : outputSections)
576 addressesChanged |= createThunks(sec, margin);
577 // If the verification above thought we needed thunks, we should have
579 assert(addressesChanged);
581 // Recalculate the layout for the whole image (and verify the ranges at
582 // the start of the next round).
589 // The main function of the writer.
591 ScopedTimer t1(codeLayoutTimer);
593 createImportTables();
596 appendImportThunks();
599 removeUnusedSections();
601 removeEmptySections();
602 assignOutputSectionIndices();
603 setSectionPermissions();
604 createSymbolAndStringTable();
606 if (fileSize > UINT32_MAX)
607 fatal("image size (" + Twine(fileSize) + ") " +
608 "exceeds maximum allowable size (" + Twine(UINT32_MAX) + ")");
610 openFile(config->outputFile);
611 if (config->is64()) {
612 writeHeader<pe32plus_header>();
614 writeHeader<pe32_header>();
617 sortExceptionTable();
621 if (!config->pdbPath.empty() && config->debug) {
623 createPDB(symtab, outputSections, sectionTable, buildId->buildId);
627 writeMapFile(outputSections);
632 ScopedTimer t2(diskCommitTimer);
633 if (auto e = buffer->commit())
634 fatal("failed to write the output file: " + toString(std::move(e)));
637 static StringRef getOutputSectionName(StringRef name) {
638 StringRef s = name.split('$').first;
640 // Treat a later period as a separator for MinGW, for sections like
642 return s.substr(0, s.find('.', 1));
646 static void sortBySectionOrder(std::vector<Chunk *> &chunks) {
647 auto getPriority = [](const Chunk *c) {
648 if (auto *sec = dyn_cast<SectionChunk>(c))
650 return config->order.lookup(sec->sym->getName());
654 llvm::stable_sort(chunks, [=](const Chunk *a, const Chunk *b) {
655 return getPriority(a) < getPriority(b);
659 // Change the characteristics of existing PartialSections that belong to the
660 // section Name to Chars.
661 void Writer::fixPartialSectionChars(StringRef name, uint32_t chars) {
662 for (auto it : partialSections) {
663 PartialSection *pSec = it.second;
664 StringRef curName = pSec->name;
665 if (!curName.consume_front(name) ||
666 (!curName.empty() && !curName.startswith("$")))
668 if (pSec->characteristics == chars)
670 PartialSection *destSec = createPartialSection(pSec->name, chars);
671 destSec->chunks.insert(destSec->chunks.end(), pSec->chunks.begin(),
673 pSec->chunks.clear();
677 // Sort concrete section chunks from GNU import libraries.
679 // GNU binutils doesn't use short import files, but instead produces import
680 // libraries that consist of object files, with section chunks for the .idata$*
681 // sections. These are linked just as regular static libraries. Each import
682 // library consists of one header object, one object file for every imported
683 // symbol, and one trailer object. In order for the .idata tables/lists to
684 // be formed correctly, the section chunks within each .idata$* section need
685 // to be grouped by library, and sorted alphabetically within each library
686 // (which makes sure the header comes first and the trailer last).
687 bool Writer::fixGnuImportChunks() {
688 uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
690 // Make sure all .idata$* section chunks are mapped as RDATA in order to
691 // be sorted into the same sections as our own synthesized .idata chunks.
692 fixPartialSectionChars(".idata", rdata);
694 bool hasIdata = false;
695 // Sort all .idata$* chunks, grouping chunks from the same library,
696 // with alphabetical ordering of the object fils within a library.
697 for (auto it : partialSections) {
698 PartialSection *pSec = it.second;
699 if (!pSec->name.startswith(".idata"))
702 if (!pSec->chunks.empty())
704 llvm::stable_sort(pSec->chunks, [&](Chunk *s, Chunk *t) {
705 SectionChunk *sc1 = dyn_cast_or_null<SectionChunk>(s);
706 SectionChunk *sc2 = dyn_cast_or_null<SectionChunk>(t);
708 // if SC1, order them ascending. If SC2 or both null,
709 // S is not less than T.
710 return sc1 != nullptr;
712 // Make a string with "libraryname/objectfile" for sorting, achieving
713 // both grouping by library and sorting of objects within a library,
716 (sc1->file->parentName + "/" + sc1->file->getName()).str();
718 (sc2->file->parentName + "/" + sc2->file->getName()).str();
725 // Add generated idata chunks, for imported symbols and DLLs, and a
726 // terminator in .idata$2.
727 void Writer::addSyntheticIdata() {
728 uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
731 // Add the .idata content in the right section groups, to allow
732 // chunks from other linked in object files to be grouped together.
733 // See Microsoft PE/COFF spec 5.4 for details.
734 auto add = [&](StringRef n, std::vector<Chunk *> &v) {
735 PartialSection *pSec = createPartialSection(n, rdata);
736 pSec->chunks.insert(pSec->chunks.end(), v.begin(), v.end());
739 // The loader assumes a specific order of data.
740 // Add each type in the correct order.
741 add(".idata$2", idata.dirs);
742 add(".idata$4", idata.lookups);
743 add(".idata$5", idata.addresses);
744 add(".idata$6", idata.hints);
745 add(".idata$7", idata.dllNames);
748 // Locate the first Chunk and size of the import directory list and the
750 void Writer::locateImportTables() {
751 uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
753 if (PartialSection *importDirs = findPartialSection(".idata$2", rdata)) {
754 if (!importDirs->chunks.empty())
755 importTableStart = importDirs->chunks.front();
756 for (Chunk *c : importDirs->chunks)
757 importTableSize += c->getSize();
760 if (PartialSection *importAddresses = findPartialSection(".idata$5", rdata)) {
761 if (!importAddresses->chunks.empty())
762 iatStart = importAddresses->chunks.front();
763 for (Chunk *c : importAddresses->chunks)
764 iatSize += c->getSize();
768 // Return whether a SectionChunk's suffix (the dollar and any trailing
769 // suffix) should be removed and sorted into the main suffixless
771 static bool shouldStripSectionSuffix(SectionChunk *sc, StringRef name) {
772 // On MinGW, comdat groups are formed by putting the comdat group name
773 // after the '$' in the section name. For .eh_frame$<symbol>, that must
774 // still be sorted before the .eh_frame trailer from crtend.o, thus just
775 // strip the section name trailer. For other sections, such as
776 // .tls$$<symbol> (where non-comdat .tls symbols are otherwise stored in
777 // ".tls$"), they must be strictly sorted after .tls. And for the
778 // hypothetical case of comdat .CRT$XCU, we definitely need to keep the
779 // suffix for sorting. Thus, to play it safe, only strip the suffix for
780 // the standard sections.
783 if (!sc || !sc->isCOMDAT())
785 return name.startswith(".text$") || name.startswith(".data$") ||
786 name.startswith(".rdata$") || name.startswith(".pdata$") ||
787 name.startswith(".xdata$") || name.startswith(".eh_frame$");
790 // Create output section objects and add them to OutputSections.
791 void Writer::createSections() {
792 // First, create the builtin sections.
793 const uint32_t data = IMAGE_SCN_CNT_INITIALIZED_DATA;
794 const uint32_t bss = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
795 const uint32_t code = IMAGE_SCN_CNT_CODE;
796 const uint32_t discardable = IMAGE_SCN_MEM_DISCARDABLE;
797 const uint32_t r = IMAGE_SCN_MEM_READ;
798 const uint32_t w = IMAGE_SCN_MEM_WRITE;
799 const uint32_t x = IMAGE_SCN_MEM_EXECUTE;
801 SmallDenseMap<std::pair<StringRef, uint32_t>, OutputSection *> sections;
802 auto createSection = [&](StringRef name, uint32_t outChars) {
803 OutputSection *&sec = sections[{name, outChars}];
805 sec = make<OutputSection>(name, outChars);
806 outputSections.push_back(sec);
811 // Try to match the section order used by link.exe.
812 textSec = createSection(".text", code | r | x);
813 createSection(".bss", bss | r | w);
814 rdataSec = createSection(".rdata", data | r);
815 buildidSec = createSection(".buildid", data | r);
816 dataSec = createSection(".data", data | r | w);
817 pdataSec = createSection(".pdata", data | r);
818 idataSec = createSection(".idata", data | r);
819 edataSec = createSection(".edata", data | r);
820 didatSec = createSection(".didat", data | r);
821 rsrcSec = createSection(".rsrc", data | r);
822 relocSec = createSection(".reloc", data | discardable | r);
823 ctorsSec = createSection(".ctors", data | r | w);
824 dtorsSec = createSection(".dtors", data | r | w);
826 // Then bin chunks by name and output characteristics.
827 for (Chunk *c : symtab->getChunks()) {
828 auto *sc = dyn_cast<SectionChunk>(c);
829 if (sc && !sc->live) {
831 sc->printDiscardedMessage();
834 StringRef name = c->getSectionName();
835 if (shouldStripSectionSuffix(sc, name))
836 name = name.split('$').first;
837 PartialSection *pSec = createPartialSection(name,
838 c->getOutputCharacteristics());
839 pSec->chunks.push_back(c);
842 fixPartialSectionChars(".rsrc", data | r);
843 // Even in non MinGW cases, we might need to link against GNU import
845 bool hasIdata = fixGnuImportChunks();
852 // Process an /order option.
853 if (!config->order.empty())
854 for (auto it : partialSections)
855 sortBySectionOrder(it.second->chunks);
858 locateImportTables();
860 // Then create an OutputSection for each section.
861 // '$' and all following characters in input section names are
862 // discarded when determining output section. So, .text$foo
863 // contributes to .text, for example. See PE/COFF spec 3.2.
864 for (auto it : partialSections) {
865 PartialSection *pSec = it.second;
866 StringRef name = getOutputSectionName(pSec->name);
867 uint32_t outChars = pSec->characteristics;
869 if (name == ".CRT") {
870 // In link.exe, there is a special case for the I386 target where .CRT
871 // sections are treated as if they have output characteristics DATA | R if
872 // their characteristics are DATA | R | W. This implements the same
873 // special case for all architectures.
876 log("Processing section " + pSec->name + " -> " + name);
878 sortCRTSectionChunks(pSec->chunks);
881 OutputSection *sec = createSection(name, outChars);
882 for (Chunk *c : pSec->chunks)
885 sec->addContributingPartialSection(pSec);
888 // Finally, move some output sections to the end.
889 auto sectionOrder = [&](const OutputSection *s) {
890 // Move DISCARDABLE (or non-memory-mapped) sections to the end of file
891 // because the loader cannot handle holes. Stripping can remove other
892 // discardable ones than .reloc, which is first of them (created early).
893 if (s->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
895 // .rsrc should come at the end of the non-discardable sections because its
896 // size may change by the Win32 UpdateResources() function, causing
897 // subsequent sections to move (see https://crbug.com/827082).
902 llvm::stable_sort(outputSections,
903 [&](const OutputSection *s, const OutputSection *t) {
904 return sectionOrder(s) < sectionOrder(t);
908 void Writer::createMiscChunks() {
909 for (MergeChunk *p : MergeChunk::instances) {
911 p->finalizeContents();
912 rdataSec->addChunk(p);
916 // Create thunks for locally-dllimported symbols.
917 if (!symtab->localImportChunks.empty()) {
918 for (Chunk *c : symtab->localImportChunks)
919 rdataSec->addChunk(c);
922 // Create Debug Information Chunks
923 OutputSection *debugInfoSec = config->mingw ? buildidSec : rdataSec;
924 if (config->debug || config->repro) {
925 debugDirectory = make<DebugDirectoryChunk>(debugRecords, config->repro);
926 debugInfoSec->addChunk(debugDirectory);
930 // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified. We
931 // output a PDB no matter what, and this chunk provides the only means of
932 // allowing a debugger to match a PDB and an executable. So we need it even
933 // if we're ultimately not going to write CodeView data to the PDB.
934 buildId = make<CVDebugRecordChunk>();
935 debugRecords.push_back(buildId);
937 for (Chunk *c : debugRecords)
938 debugInfoSec->addChunk(c);
941 // Create SEH table. x86-only.
945 // Create /guard:cf tables if requested.
946 if (config->guardCF != GuardCFLevel::Off)
947 createGuardCFTables();
950 createRuntimePseudoRelocs();
952 insertCtorDtorSymbols();
956 // Create .idata section for the DLL-imported symbol table.
957 // The format of this section is inherently Windows-specific.
958 // IdataContents class abstracted away the details for us,
959 // so we just let it create chunks and add them to the section.
960 void Writer::createImportTables() {
961 // Initialize DLLOrder so that import entries are ordered in
962 // the same order as in the command line. (That affects DLL
963 // initialization order, and this ordering is MSVC-compatible.)
964 for (ImportFile *file : ImportFile::instances) {
968 std::string dll = StringRef(file->dllName).lower();
969 if (config->dllOrder.count(dll) == 0)
970 config->dllOrder[dll] = config->dllOrder.size();
972 if (file->impSym && !isa<DefinedImportData>(file->impSym))
973 fatal(toString(*file->impSym) + " was replaced");
974 DefinedImportData *impSym = cast_or_null<DefinedImportData>(file->impSym);
975 if (config->delayLoads.count(StringRef(file->dllName).lower())) {
977 fatal("cannot delay-load " + toString(file) +
978 " due to import of data: " + toString(*impSym));
979 delayIdata.add(impSym);
986 void Writer::appendImportThunks() {
987 if (ImportFile::instances.empty())
990 for (ImportFile *file : ImportFile::instances) {
997 if (!isa<DefinedImportThunk>(file->thunkSym))
998 fatal(toString(*file->thunkSym) + " was replaced");
999 DefinedImportThunk *thunk = cast<DefinedImportThunk>(file->thunkSym);
1000 if (file->thunkLive)
1001 textSec->addChunk(thunk->getChunk());
1004 if (!delayIdata.empty()) {
1005 Defined *helper = cast<Defined>(config->delayLoadHelper);
1006 delayIdata.create(helper);
1007 for (Chunk *c : delayIdata.getChunks())
1008 didatSec->addChunk(c);
1009 for (Chunk *c : delayIdata.getDataChunks())
1010 dataSec->addChunk(c);
1011 for (Chunk *c : delayIdata.getCodeChunks())
1012 textSec->addChunk(c);
1016 void Writer::createExportTable() {
1017 if (config->exports.empty())
1019 for (Chunk *c : edata.chunks)
1020 edataSec->addChunk(c);
1023 void Writer::removeUnusedSections() {
1024 // Remove sections that we can be sure won't get content, to avoid
1025 // allocating space for their section headers.
1026 auto isUnused = [this](OutputSection *s) {
1028 return false; // This section is populated later.
1029 // MergeChunks have zero size at this point, as their size is finalized
1030 // later. Only remove sections that have no Chunks at all.
1031 return s->chunks.empty();
1033 outputSections.erase(
1034 std::remove_if(outputSections.begin(), outputSections.end(), isUnused),
1035 outputSections.end());
1038 // The Windows loader doesn't seem to like empty sections,
1039 // so we remove them if any.
1040 void Writer::removeEmptySections() {
1041 auto isEmpty = [](OutputSection *s) { return s->getVirtualSize() == 0; };
1042 outputSections.erase(
1043 std::remove_if(outputSections.begin(), outputSections.end(), isEmpty),
1044 outputSections.end());
1047 void Writer::assignOutputSectionIndices() {
1048 // Assign final output section indices, and assign each chunk to its output
1051 for (OutputSection *os : outputSections) {
1052 os->sectionIndex = idx;
1053 for (Chunk *c : os->chunks)
1054 c->setOutputSectionIdx(idx);
1058 // Merge chunks are containers of chunks, so assign those an output section
1060 for (MergeChunk *mc : MergeChunk::instances)
1062 for (SectionChunk *sc : mc->sections)
1064 sc->setOutputSectionIdx(mc->getOutputSectionIdx());
1067 size_t Writer::addEntryToStringTable(StringRef str) {
1068 assert(str.size() > COFF::NameSize);
1069 size_t offsetOfEntry = strtab.size() + 4; // +4 for the size field
1070 strtab.insert(strtab.end(), str.begin(), str.end());
1071 strtab.push_back('\0');
1072 return offsetOfEntry;
1075 Optional<coff_symbol16> Writer::createSymbol(Defined *def) {
1077 switch (def->kind()) {
1078 case Symbol::DefinedAbsoluteKind:
1079 sym.Value = def->getRVA();
1080 sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
1082 case Symbol::DefinedSyntheticKind:
1083 // Relative symbols are unrepresentable in a COFF symbol table.
1086 // Don't write symbols that won't be written to the output to the symbol
1088 Chunk *c = def->getChunk();
1091 OutputSection *os = c->getOutputSection();
1095 sym.Value = def->getRVA() - os->getRVA();
1096 sym.SectionNumber = os->sectionIndex;
1101 // Symbols that are runtime pseudo relocations don't point to the actual
1102 // symbol data itself (as they are imported), but points to the IAT entry
1103 // instead. Avoid emitting them to the symbol table, as they can confuse
1105 if (def->isRuntimePseudoReloc)
1108 StringRef name = def->getName();
1109 if (name.size() > COFF::NameSize) {
1110 sym.Name.Offset.Zeroes = 0;
1111 sym.Name.Offset.Offset = addEntryToStringTable(name);
1113 memset(sym.Name.ShortName, 0, COFF::NameSize);
1114 memcpy(sym.Name.ShortName, name.data(), name.size());
1117 if (auto *d = dyn_cast<DefinedCOFF>(def)) {
1118 COFFSymbolRef ref = d->getCOFFSymbol();
1119 sym.Type = ref.getType();
1120 sym.StorageClass = ref.getStorageClass();
1122 sym.Type = IMAGE_SYM_TYPE_NULL;
1123 sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
1125 sym.NumberOfAuxSymbols = 0;
1129 void Writer::createSymbolAndStringTable() {
1130 // PE/COFF images are limited to 8 byte section names. Longer names can be
1131 // supported by writing a non-standard string table, but this string table is
1132 // not mapped at runtime and the long names will therefore be inaccessible.
1133 // link.exe always truncates section names to 8 bytes, whereas binutils always
1134 // preserves long section names via the string table. LLD adopts a hybrid
1135 // solution where discardable sections have long names preserved and
1136 // non-discardable sections have their names truncated, to ensure that any
1137 // section which is mapped at runtime also has its name mapped at runtime.
1138 for (OutputSection *sec : outputSections) {
1139 if (sec->name.size() <= COFF::NameSize)
1141 if ((sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0)
1143 sec->setStringTableOff(addEntryToStringTable(sec->name));
1146 if (config->debugDwarf || config->debugSymtab) {
1147 for (ObjFile *file : ObjFile::instances) {
1148 for (Symbol *b : file->getSymbols()) {
1149 auto *d = dyn_cast_or_null<Defined>(b);
1150 if (!d || d->writtenToSymtab)
1152 d->writtenToSymtab = true;
1154 if (Optional<coff_symbol16> sym = createSymbol(d))
1155 outputSymtab.push_back(*sym);
1160 if (outputSymtab.empty() && strtab.empty())
1163 // We position the symbol table to be adjacent to the end of the last section.
1164 uint64_t fileOff = fileSize;
1165 pointerToSymbolTable = fileOff;
1166 fileOff += outputSymtab.size() * sizeof(coff_symbol16);
1167 fileOff += 4 + strtab.size();
1168 fileSize = alignTo(fileOff, config->fileAlign);
1171 void Writer::mergeSections() {
1172 if (!pdataSec->chunks.empty()) {
1173 firstPdata = pdataSec->chunks.front();
1174 lastPdata = pdataSec->chunks.back();
1177 for (auto &p : config->merge) {
1178 StringRef toName = p.second;
1179 if (p.first == toName)
1183 if (!names.insert(toName).second)
1184 fatal("/merge: cycle found for section '" + p.first + "'");
1185 auto i = config->merge.find(toName);
1186 if (i == config->merge.end())
1190 OutputSection *from = findSection(p.first);
1191 OutputSection *to = findSection(toName);
1195 from->name = toName;
1202 // Visits all sections to assign incremental, non-overlapping RVAs and
1204 void Writer::assignAddresses() {
1205 sizeOfHeaders = dosStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
1206 sizeof(data_directory) * numberOfDataDirectory +
1207 sizeof(coff_section) * outputSections.size();
1209 config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
1210 sizeOfHeaders = alignTo(sizeOfHeaders, config->fileAlign);
1211 fileSize = sizeOfHeaders;
1213 // The first page is kept unmapped.
1214 uint64_t rva = alignTo(sizeOfHeaders, config->align);
1216 for (OutputSection *sec : outputSections) {
1217 if (sec == relocSec)
1219 uint64_t rawSize = 0, virtualSize = 0;
1220 sec->header.VirtualAddress = rva;
1222 // If /FUNCTIONPADMIN is used, functions are padded in order to create a
1223 // hotpatchable image.
1224 const bool isCodeSection =
1225 (sec->header.Characteristics & IMAGE_SCN_CNT_CODE) &&
1226 (sec->header.Characteristics & IMAGE_SCN_MEM_READ) &&
1227 (sec->header.Characteristics & IMAGE_SCN_MEM_EXECUTE);
1228 uint32_t padding = isCodeSection ? config->functionPadMin : 0;
1230 for (Chunk *c : sec->chunks) {
1231 if (padding && c->isHotPatchable())
1232 virtualSize += padding;
1233 virtualSize = alignTo(virtualSize, c->getAlignment());
1234 c->setRVA(rva + virtualSize);
1235 virtualSize += c->getSize();
1237 rawSize = alignTo(virtualSize, config->fileAlign);
1239 if (virtualSize > UINT32_MAX)
1240 error("section larger than 4 GiB: " + sec->name);
1241 sec->header.VirtualSize = virtualSize;
1242 sec->header.SizeOfRawData = rawSize;
1244 sec->header.PointerToRawData = fileSize;
1245 rva += alignTo(virtualSize, config->align);
1246 fileSize += alignTo(rawSize, config->fileAlign);
1248 sizeOfImage = alignTo(rva, config->align);
1250 // Assign addresses to sections in MergeChunks.
1251 for (MergeChunk *mc : MergeChunk::instances)
1253 mc->assignSubsectionRVAs();
1256 template <typename PEHeaderTy> void Writer::writeHeader() {
1257 // Write DOS header. For backwards compatibility, the first part of a PE/COFF
1258 // executable consists of an MS-DOS MZ executable. If the executable is run
1259 // under DOS, that program gets run (usually to just print an error message).
1260 // When run under Windows, the loader looks at AddressOfNewExeHeader and uses
1261 // the PE header instead.
1262 uint8_t *buf = buffer->getBufferStart();
1263 auto *dos = reinterpret_cast<dos_header *>(buf);
1264 buf += sizeof(dos_header);
1265 dos->Magic[0] = 'M';
1266 dos->Magic[1] = 'Z';
1267 dos->UsedBytesInTheLastPage = dosStubSize % 512;
1268 dos->FileSizeInPages = divideCeil(dosStubSize, 512);
1269 dos->HeaderSizeInParagraphs = sizeof(dos_header) / 16;
1271 dos->AddressOfRelocationTable = sizeof(dos_header);
1272 dos->AddressOfNewExeHeader = dosStubSize;
1274 // Write DOS program.
1275 memcpy(buf, dosProgram, sizeof(dosProgram));
1276 buf += sizeof(dosProgram);
1279 memcpy(buf, PEMagic, sizeof(PEMagic));
1280 buf += sizeof(PEMagic);
1282 // Write COFF header
1283 auto *coff = reinterpret_cast<coff_file_header *>(buf);
1284 buf += sizeof(*coff);
1285 coff->Machine = config->machine;
1286 coff->NumberOfSections = outputSections.size();
1287 coff->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
1288 if (config->largeAddressAware)
1289 coff->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
1290 if (!config->is64())
1291 coff->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
1293 coff->Characteristics |= IMAGE_FILE_DLL;
1294 if (!config->relocatable)
1295 coff->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
1296 if (config->swaprunCD)
1297 coff->Characteristics |= IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP;
1298 if (config->swaprunNet)
1299 coff->Characteristics |= IMAGE_FILE_NET_RUN_FROM_SWAP;
1300 coff->SizeOfOptionalHeader =
1301 sizeof(PEHeaderTy) + sizeof(data_directory) * numberOfDataDirectory;
1304 auto *pe = reinterpret_cast<PEHeaderTy *>(buf);
1306 pe->Magic = config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
1308 // If {Major,Minor}LinkerVersion is left at 0.0, then for some
1309 // reason signing the resulting PE file with Authenticode produces a
1310 // signature that fails to validate on Windows 7 (but is OK on 10).
1311 // Set it to 14.0, which is what VS2015 outputs, and which avoids
1313 pe->MajorLinkerVersion = 14;
1314 pe->MinorLinkerVersion = 0;
1316 pe->ImageBase = config->imageBase;
1317 pe->SectionAlignment = config->align;
1318 pe->FileAlignment = config->fileAlign;
1319 pe->MajorImageVersion = config->majorImageVersion;
1320 pe->MinorImageVersion = config->minorImageVersion;
1321 pe->MajorOperatingSystemVersion = config->majorOSVersion;
1322 pe->MinorOperatingSystemVersion = config->minorOSVersion;
1323 pe->MajorSubsystemVersion = config->majorOSVersion;
1324 pe->MinorSubsystemVersion = config->minorOSVersion;
1325 pe->Subsystem = config->subsystem;
1326 pe->SizeOfImage = sizeOfImage;
1327 pe->SizeOfHeaders = sizeOfHeaders;
1328 if (!config->noEntry) {
1329 Defined *entry = cast<Defined>(config->entry);
1330 pe->AddressOfEntryPoint = entry->getRVA();
1331 // Pointer to thumb code must have the LSB set, so adjust it.
1332 if (config->machine == ARMNT)
1333 pe->AddressOfEntryPoint |= 1;
1335 pe->SizeOfStackReserve = config->stackReserve;
1336 pe->SizeOfStackCommit = config->stackCommit;
1337 pe->SizeOfHeapReserve = config->heapReserve;
1338 pe->SizeOfHeapCommit = config->heapCommit;
1339 if (config->appContainer)
1340 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER;
1341 if (config->dynamicBase)
1342 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
1343 if (config->highEntropyVA)
1344 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
1345 if (!config->allowBind)
1346 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND;
1347 if (config->nxCompat)
1348 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
1349 if (!config->allowIsolation)
1350 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION;
1351 if (config->guardCF != GuardCFLevel::Off)
1352 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF;
1353 if (config->integrityCheck)
1354 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
1355 if (setNoSEHCharacteristic)
1356 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH;
1357 if (config->terminalServerAware)
1358 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
1359 pe->NumberOfRvaAndSize = numberOfDataDirectory;
1360 if (textSec->getVirtualSize()) {
1361 pe->BaseOfCode = textSec->getRVA();
1362 pe->SizeOfCode = textSec->getRawSize();
1364 pe->SizeOfInitializedData = getSizeOfInitializedData();
1366 // Write data directory
1367 auto *dir = reinterpret_cast<data_directory *>(buf);
1368 buf += sizeof(*dir) * numberOfDataDirectory;
1369 if (!config->exports.empty()) {
1370 dir[EXPORT_TABLE].RelativeVirtualAddress = edata.getRVA();
1371 dir[EXPORT_TABLE].Size = edata.getSize();
1373 if (importTableStart) {
1374 dir[IMPORT_TABLE].RelativeVirtualAddress = importTableStart->getRVA();
1375 dir[IMPORT_TABLE].Size = importTableSize;
1378 dir[IAT].RelativeVirtualAddress = iatStart->getRVA();
1379 dir[IAT].Size = iatSize;
1381 if (rsrcSec->getVirtualSize()) {
1382 dir[RESOURCE_TABLE].RelativeVirtualAddress = rsrcSec->getRVA();
1383 dir[RESOURCE_TABLE].Size = rsrcSec->getVirtualSize();
1386 dir[EXCEPTION_TABLE].RelativeVirtualAddress = firstPdata->getRVA();
1387 dir[EXCEPTION_TABLE].Size =
1388 lastPdata->getRVA() + lastPdata->getSize() - firstPdata->getRVA();
1390 if (relocSec->getVirtualSize()) {
1391 dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = relocSec->getRVA();
1392 dir[BASE_RELOCATION_TABLE].Size = relocSec->getVirtualSize();
1394 if (Symbol *sym = symtab->findUnderscore("_tls_used")) {
1395 if (Defined *b = dyn_cast<Defined>(sym)) {
1396 dir[TLS_TABLE].RelativeVirtualAddress = b->getRVA();
1397 dir[TLS_TABLE].Size = config->is64()
1398 ? sizeof(object::coff_tls_directory64)
1399 : sizeof(object::coff_tls_directory32);
1402 if (debugDirectory) {
1403 dir[DEBUG_DIRECTORY].RelativeVirtualAddress = debugDirectory->getRVA();
1404 dir[DEBUG_DIRECTORY].Size = debugDirectory->getSize();
1406 if (Symbol *sym = symtab->findUnderscore("_load_config_used")) {
1407 if (auto *b = dyn_cast<DefinedRegular>(sym)) {
1408 SectionChunk *sc = b->getChunk();
1409 assert(b->getRVA() >= sc->getRVA());
1410 uint64_t offsetInChunk = b->getRVA() - sc->getRVA();
1411 if (!sc->hasData || offsetInChunk + 4 > sc->getSize())
1412 fatal("_load_config_used is malformed");
1414 ArrayRef<uint8_t> secContents = sc->getContents();
1415 uint32_t loadConfigSize =
1416 *reinterpret_cast<const ulittle32_t *>(&secContents[offsetInChunk]);
1417 if (offsetInChunk + loadConfigSize > sc->getSize())
1418 fatal("_load_config_used is too large");
1419 dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = b->getRVA();
1420 dir[LOAD_CONFIG_TABLE].Size = loadConfigSize;
1423 if (!delayIdata.empty()) {
1424 dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
1425 delayIdata.getDirRVA();
1426 dir[DELAY_IMPORT_DESCRIPTOR].Size = delayIdata.getDirSize();
1429 // Write section table
1430 for (OutputSection *sec : outputSections) {
1431 sec->writeHeaderTo(buf);
1432 buf += sizeof(coff_section);
1434 sectionTable = ArrayRef<uint8_t>(
1435 buf - outputSections.size() * sizeof(coff_section), buf);
1437 if (outputSymtab.empty() && strtab.empty())
1440 coff->PointerToSymbolTable = pointerToSymbolTable;
1441 uint32_t numberOfSymbols = outputSymtab.size();
1442 coff->NumberOfSymbols = numberOfSymbols;
1443 auto *symbolTable = reinterpret_cast<coff_symbol16 *>(
1444 buffer->getBufferStart() + coff->PointerToSymbolTable);
1445 for (size_t i = 0; i != numberOfSymbols; ++i)
1446 symbolTable[i] = outputSymtab[i];
1447 // Create the string table, it follows immediately after the symbol table.
1448 // The first 4 bytes is length including itself.
1449 buf = reinterpret_cast<uint8_t *>(&symbolTable[numberOfSymbols]);
1450 write32le(buf, strtab.size() + 4);
1451 if (!strtab.empty())
1452 memcpy(buf + 4, strtab.data(), strtab.size());
1455 void Writer::openFile(StringRef path) {
1457 FileOutputBuffer::create(path, fileSize, FileOutputBuffer::F_executable),
1458 "failed to open " + path);
1461 void Writer::createSEHTable() {
1462 SymbolRVASet handlers;
1463 for (ObjFile *file : ObjFile::instances) {
1464 if (!file->hasSafeSEH())
1465 error("/safeseh: " + file->getName() + " is not compatible with SEH");
1466 markSymbolsForRVATable(file, file->getSXDataChunks(), handlers);
1469 // Set the "no SEH" characteristic if there really were no handlers, or if
1470 // there is no load config object to point to the table of handlers.
1471 setNoSEHCharacteristic =
1472 handlers.empty() || !symtab->findUnderscore("_load_config_used");
1474 maybeAddRVATable(std::move(handlers), "__safe_se_handler_table",
1475 "__safe_se_handler_count");
1478 // Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set
1479 // cannot contain duplicates. Therefore, the set is uniqued by Chunk and the
1480 // symbol's offset into that Chunk.
1481 static void addSymbolToRVASet(SymbolRVASet &rvaSet, Defined *s) {
1482 Chunk *c = s->getChunk();
1483 if (auto *sc = dyn_cast<SectionChunk>(c))
1484 c = sc->repl; // Look through ICF replacement.
1485 uint32_t off = s->getRVA() - (c ? c->getRVA() : 0);
1486 rvaSet.insert({c, off});
1489 // Given a symbol, add it to the GFIDs table if it is a live, defined, function
1490 // symbol in an executable section.
1491 static void maybeAddAddressTakenFunction(SymbolRVASet &addressTakenSyms,
1496 switch (s->kind()) {
1497 case Symbol::DefinedLocalImportKind:
1498 case Symbol::DefinedImportDataKind:
1499 // Defines an __imp_ pointer, so it is data, so it is ignored.
1501 case Symbol::DefinedCommonKind:
1502 // Common is always data, so it is ignored.
1504 case Symbol::DefinedAbsoluteKind:
1505 case Symbol::DefinedSyntheticKind:
1506 // Absolute is never code, synthetic generally isn't and usually isn't
1509 case Symbol::LazyKind:
1510 case Symbol::UndefinedKind:
1511 // Undefined symbols resolve to zero, so they don't have an RVA. Lazy
1512 // symbols shouldn't have relocations.
1515 case Symbol::DefinedImportThunkKind:
1516 // Thunks are always code, include them.
1517 addSymbolToRVASet(addressTakenSyms, cast<Defined>(s));
1520 case Symbol::DefinedRegularKind: {
1521 // This is a regular, defined, symbol from a COFF file. Mark the symbol as
1522 // address taken if the symbol type is function and it's in an executable
1524 auto *d = cast<DefinedRegular>(s);
1525 if (d->getCOFFSymbol().getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
1526 SectionChunk *sc = dyn_cast<SectionChunk>(d->getChunk());
1527 if (sc && sc->live &&
1528 sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE)
1529 addSymbolToRVASet(addressTakenSyms, d);
1536 // Visit all relocations from all section contributions of this object file and
1537 // mark the relocation target as address-taken.
1538 static void markSymbolsWithRelocations(ObjFile *file,
1539 SymbolRVASet &usedSymbols) {
1540 for (Chunk *c : file->getChunks()) {
1541 // We only care about live section chunks. Common chunks and other chunks
1542 // don't generally contain relocations.
1543 SectionChunk *sc = dyn_cast<SectionChunk>(c);
1544 if (!sc || !sc->live)
1547 for (const coff_relocation &reloc : sc->getRelocs()) {
1548 if (config->machine == I386 && reloc.Type == COFF::IMAGE_REL_I386_REL32)
1549 // Ignore relative relocations on x86. On x86_64 they can't be ignored
1550 // since they're also used to compute absolute addresses.
1553 Symbol *ref = sc->file->getSymbol(reloc.SymbolTableIndex);
1554 maybeAddAddressTakenFunction(usedSymbols, ref);
1559 // Create the guard function id table. This is a table of RVAs of all
1560 // address-taken functions. It is sorted and uniqued, just like the safe SEH
1562 void Writer::createGuardCFTables() {
1563 SymbolRVASet addressTakenSyms;
1564 SymbolRVASet longJmpTargets;
1565 for (ObjFile *file : ObjFile::instances) {
1566 // If the object was compiled with /guard:cf, the address taken symbols
1567 // are in .gfids$y sections, and the longjmp targets are in .gljmp$y
1568 // sections. If the object was not compiled with /guard:cf, we assume there
1569 // were no setjmp targets, and that all code symbols with relocations are
1570 // possibly address-taken.
1571 if (file->hasGuardCF()) {
1572 markSymbolsForRVATable(file, file->getGuardFidChunks(), addressTakenSyms);
1573 markSymbolsForRVATable(file, file->getGuardLJmpChunks(), longJmpTargets);
1575 markSymbolsWithRelocations(file, addressTakenSyms);
1579 // Mark the image entry as address-taken.
1581 maybeAddAddressTakenFunction(addressTakenSyms, config->entry);
1583 // Mark exported symbols in executable sections as address-taken.
1584 for (Export &e : config->exports)
1585 maybeAddAddressTakenFunction(addressTakenSyms, e.sym);
1587 // Ensure sections referenced in the gfid table are 16-byte aligned.
1588 for (const ChunkAndOffset &c : addressTakenSyms)
1589 if (c.inputChunk->getAlignment() < 16)
1590 c.inputChunk->setAlignment(16);
1592 maybeAddRVATable(std::move(addressTakenSyms), "__guard_fids_table",
1593 "__guard_fids_count");
1595 // Add the longjmp target table unless the user told us not to.
1596 if (config->guardCF == GuardCFLevel::Full)
1597 maybeAddRVATable(std::move(longJmpTargets), "__guard_longjmp_table",
1598 "__guard_longjmp_count");
1600 // Set __guard_flags, which will be used in the load config to indicate that
1601 // /guard:cf was enabled.
1602 uint32_t guardFlags = uint32_t(coff_guard_flags::CFInstrumented) |
1603 uint32_t(coff_guard_flags::HasFidTable);
1604 if (config->guardCF == GuardCFLevel::Full)
1605 guardFlags |= uint32_t(coff_guard_flags::HasLongJmpTable);
1606 Symbol *flagSym = symtab->findUnderscore("__guard_flags");
1607 cast<DefinedAbsolute>(flagSym)->setVA(guardFlags);
1610 // Take a list of input sections containing symbol table indices and add those
1611 // symbols to an RVA table. The challenge is that symbol RVAs are not known and
1612 // depend on the table size, so we can't directly build a set of integers.
1613 void Writer::markSymbolsForRVATable(ObjFile *file,
1614 ArrayRef<SectionChunk *> symIdxChunks,
1615 SymbolRVASet &tableSymbols) {
1616 for (SectionChunk *c : symIdxChunks) {
1617 // Skip sections discarded by linker GC. This comes up when a .gfids section
1618 // is associated with something like a vtable and the vtable is discarded.
1619 // In this case, the associated gfids section is discarded, and we don't
1620 // mark the virtual member functions as address-taken by the vtable.
1624 // Validate that the contents look like symbol table indices.
1625 ArrayRef<uint8_t> data = c->getContents();
1626 if (data.size() % 4 != 0) {
1627 warn("ignoring " + c->getSectionName() +
1628 " symbol table index section in object " + toString(file));
1632 // Read each symbol table index and check if that symbol was included in the
1633 // final link. If so, add it to the table symbol set.
1634 ArrayRef<ulittle32_t> symIndices(
1635 reinterpret_cast<const ulittle32_t *>(data.data()), data.size() / 4);
1636 ArrayRef<Symbol *> objSymbols = file->getSymbols();
1637 for (uint32_t symIndex : symIndices) {
1638 if (symIndex >= objSymbols.size()) {
1639 warn("ignoring invalid symbol table index in section " +
1640 c->getSectionName() + " in object " + toString(file));
1643 if (Symbol *s = objSymbols[symIndex]) {
1645 addSymbolToRVASet(tableSymbols, cast<Defined>(s));
1651 // Replace the absolute table symbol with a synthetic symbol pointing to
1652 // tableChunk so that we can emit base relocations for it and resolve section
1653 // relative relocations.
1654 void Writer::maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
1655 StringRef countSym) {
1656 if (tableSymbols.empty())
1659 RVATableChunk *tableChunk = make<RVATableChunk>(std::move(tableSymbols));
1660 rdataSec->addChunk(tableChunk);
1662 Symbol *t = symtab->findUnderscore(tableSym);
1663 Symbol *c = symtab->findUnderscore(countSym);
1664 replaceSymbol<DefinedSynthetic>(t, t->getName(), tableChunk);
1665 cast<DefinedAbsolute>(c)->setVA(tableChunk->getSize() / 4);
1668 // MinGW specific. Gather all relocations that are imported from a DLL even
1669 // though the code didn't expect it to, produce the table that the runtime
1670 // uses for fixing them up, and provide the synthetic symbols that the
1671 // runtime uses for finding the table.
1672 void Writer::createRuntimePseudoRelocs() {
1673 std::vector<RuntimePseudoReloc> rels;
1675 for (Chunk *c : symtab->getChunks()) {
1676 auto *sc = dyn_cast<SectionChunk>(c);
1677 if (!sc || !sc->live)
1679 sc->getRuntimePseudoRelocs(rels);
1683 log("Writing " + Twine(rels.size()) + " runtime pseudo relocations");
1684 PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(rels);
1685 rdataSec->addChunk(table);
1686 EmptyChunk *endOfList = make<EmptyChunk>();
1687 rdataSec->addChunk(endOfList);
1689 Symbol *headSym = symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__");
1690 Symbol *endSym = symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__");
1691 replaceSymbol<DefinedSynthetic>(headSym, headSym->getName(), table);
1692 replaceSymbol<DefinedSynthetic>(endSym, endSym->getName(), endOfList);
1696 // The MinGW .ctors and .dtors lists have sentinels at each end;
1697 // a (uintptr_t)-1 at the start and a (uintptr_t)0 at the end.
1698 // There's a symbol pointing to the start sentinel pointer, __CTOR_LIST__
1699 // and __DTOR_LIST__ respectively.
1700 void Writer::insertCtorDtorSymbols() {
1701 AbsolutePointerChunk *ctorListHead = make<AbsolutePointerChunk>(-1);
1702 AbsolutePointerChunk *ctorListEnd = make<AbsolutePointerChunk>(0);
1703 AbsolutePointerChunk *dtorListHead = make<AbsolutePointerChunk>(-1);
1704 AbsolutePointerChunk *dtorListEnd = make<AbsolutePointerChunk>(0);
1705 ctorsSec->insertChunkAtStart(ctorListHead);
1706 ctorsSec->addChunk(ctorListEnd);
1707 dtorsSec->insertChunkAtStart(dtorListHead);
1708 dtorsSec->addChunk(dtorListEnd);
1710 Symbol *ctorListSym = symtab->findUnderscore("__CTOR_LIST__");
1711 Symbol *dtorListSym = symtab->findUnderscore("__DTOR_LIST__");
1712 replaceSymbol<DefinedSynthetic>(ctorListSym, ctorListSym->getName(),
1714 replaceSymbol<DefinedSynthetic>(dtorListSym, dtorListSym->getName(),
1718 // Handles /section options to allow users to overwrite
1719 // section attributes.
1720 void Writer::setSectionPermissions() {
1721 for (auto &p : config->section) {
1722 StringRef name = p.first;
1723 uint32_t perm = p.second;
1724 for (OutputSection *sec : outputSections)
1725 if (sec->name == name)
1726 sec->setPermissions(perm);
1730 // Write section contents to a mmap'ed file.
1731 void Writer::writeSections() {
1732 // Record the number of sections to apply section index relocations
1733 // against absolute symbols. See applySecIdx in Chunks.cpp..
1734 DefinedAbsolute::numOutputSections = outputSections.size();
1736 uint8_t *buf = buffer->getBufferStart();
1737 for (OutputSection *sec : outputSections) {
1738 uint8_t *secBuf = buf + sec->getFileOff();
1739 // Fill gaps between functions in .text with INT3 instructions
1740 // instead of leaving as NUL bytes (which can be interpreted as
1741 // ADD instructions).
1742 if (sec->header.Characteristics & IMAGE_SCN_CNT_CODE)
1743 memset(secBuf, 0xCC, sec->getRawSize());
1744 parallelForEach(sec->chunks, [&](Chunk *c) {
1745 c->writeTo(secBuf + c->getRVA() - sec->getRVA());
1750 void Writer::writeBuildId() {
1751 // There are two important parts to the build ID.
1752 // 1) If building with debug info, the COFF debug directory contains a
1753 // timestamp as well as a Guid and Age of the PDB.
1754 // 2) In all cases, the PE COFF file header also contains a timestamp.
1755 // For reproducibility, instead of a timestamp we want to use a hash of the
1757 if (config->debug) {
1758 assert(buildId && "BuildId is not set!");
1759 // BuildId->BuildId was filled in when the PDB was written.
1762 // At this point the only fields in the COFF file which remain unset are the
1763 // "timestamp" in the COFF file header, and the ones in the coff debug
1764 // directory. Now we can hash the file and write that hash to the various
1765 // timestamp fields in the file.
1766 StringRef outputFileData(
1767 reinterpret_cast<const char *>(buffer->getBufferStart()),
1768 buffer->getBufferSize());
1770 uint32_t timestamp = config->timestamp;
1772 bool generateSyntheticBuildId =
1773 config->mingw && config->debug && config->pdbPath.empty();
1775 if (config->repro || generateSyntheticBuildId)
1776 hash = xxHash64(outputFileData);
1779 timestamp = static_cast<uint32_t>(hash);
1781 if (generateSyntheticBuildId) {
1782 // For MinGW builds without a PDB file, we still generate a build id
1783 // to allow associating a crash dump to the executable.
1784 buildId->buildId->PDB70.CVSignature = OMF::Signature::PDB70;
1785 buildId->buildId->PDB70.Age = 1;
1786 memcpy(buildId->buildId->PDB70.Signature, &hash, 8);
1787 // xxhash only gives us 8 bytes, so put some fixed data in the other half.
1788 memcpy(&buildId->buildId->PDB70.Signature[8], "LLD PDB.", 8);
1792 debugDirectory->setTimeDateStamp(timestamp);
1794 uint8_t *buf = buffer->getBufferStart();
1795 buf += dosStubSize + sizeof(PEMagic);
1796 object::coff_file_header *coffHeader =
1797 reinterpret_cast<coff_file_header *>(buf);
1798 coffHeader->TimeDateStamp = timestamp;
1801 // Sort .pdata section contents according to PE/COFF spec 5.5.
1802 void Writer::sortExceptionTable() {
1805 // We assume .pdata contains function table entries only.
1806 auto bufAddr = [&](Chunk *c) {
1807 OutputSection *os = c->getOutputSection();
1808 return buffer->getBufferStart() + os->getFileOff() + c->getRVA() -
1811 uint8_t *begin = bufAddr(firstPdata);
1812 uint8_t *end = bufAddr(lastPdata) + lastPdata->getSize();
1813 if (config->machine == AMD64) {
1814 struct Entry { ulittle32_t begin, end, unwind; };
1816 MutableArrayRef<Entry>((Entry *)begin, (Entry *)end),
1817 [](const Entry &a, const Entry &b) { return a.begin < b.begin; });
1820 if (config->machine == ARMNT || config->machine == ARM64) {
1821 struct Entry { ulittle32_t begin, unwind; };
1823 MutableArrayRef<Entry>((Entry *)begin, (Entry *)end),
1824 [](const Entry &a, const Entry &b) { return a.begin < b.begin; });
1827 errs() << "warning: don't know how to handle .pdata.\n";
1830 // The CRT section contains, among other things, the array of function
1831 // pointers that initialize every global variable that is not trivially
1832 // constructed. The CRT calls them one after the other prior to invoking
1835 // As per C++ spec, 3.6.2/2.3,
1836 // "Variables with ordered initialization defined within a single
1837 // translation unit shall be initialized in the order of their definitions
1838 // in the translation unit"
1840 // It is therefore critical to sort the chunks containing the function
1841 // pointers in the order that they are listed in the object file (top to
1842 // bottom), otherwise global objects might not be initialized in the
1844 void Writer::sortCRTSectionChunks(std::vector<Chunk *> &chunks) {
1845 auto sectionChunkOrder = [](const Chunk *a, const Chunk *b) {
1846 auto sa = dyn_cast<SectionChunk>(a);
1847 auto sb = dyn_cast<SectionChunk>(b);
1848 assert(sa && sb && "Non-section chunks in CRT section!");
1850 StringRef sAObj = sa->file->mb.getBufferIdentifier();
1851 StringRef sBObj = sb->file->mb.getBufferIdentifier();
1853 return sAObj == sBObj && sa->getSectionNumber() < sb->getSectionNumber();
1855 llvm::stable_sort(chunks, sectionChunkOrder);
1857 if (config->verbose) {
1858 for (auto &c : chunks) {
1859 auto sc = dyn_cast<SectionChunk>(c);
1860 log(" " + sc->file->mb.getBufferIdentifier().str() +
1861 ", SectionID: " + Twine(sc->getSectionNumber()));
1866 OutputSection *Writer::findSection(StringRef name) {
1867 for (OutputSection *sec : outputSections)
1868 if (sec->name == name)
1873 uint32_t Writer::getSizeOfInitializedData() {
1875 for (OutputSection *s : outputSections)
1876 if (s->header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
1877 res += s->getRawSize();
1881 // Add base relocations to .reloc section.
1882 void Writer::addBaserels() {
1883 if (!config->relocatable)
1885 relocSec->chunks.clear();
1886 std::vector<Baserel> v;
1887 for (OutputSection *sec : outputSections) {
1888 if (sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
1890 // Collect all locations for base relocations.
1891 for (Chunk *c : sec->chunks)
1893 // Add the addresses to .reloc section.
1895 addBaserelBlocks(v);
1900 // Add addresses to .reloc section. Note that addresses are grouped by page.
1901 void Writer::addBaserelBlocks(std::vector<Baserel> &v) {
1902 const uint32_t mask = ~uint32_t(pageSize - 1);
1903 uint32_t page = v[0].rva & mask;
1904 size_t i = 0, j = 1;
1905 for (size_t e = v.size(); j < e; ++j) {
1906 uint32_t p = v[j].rva & mask;
1909 relocSec->addChunk(make<BaserelChunk>(page, &v[i], &v[0] + j));
1915 relocSec->addChunk(make<BaserelChunk>(page, &v[i], &v[0] + j));
1918 PartialSection *Writer::createPartialSection(StringRef name,
1919 uint32_t outChars) {
1920 PartialSection *&pSec = partialSections[{name, outChars}];
1923 pSec = make<PartialSection>(name, outChars);
1927 PartialSection *Writer::findPartialSection(StringRef name, uint32_t outChars) {
1928 auto it = partialSections.find({name, outChars});
1929 if (it != partialSections.end())