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/StringSet.h"
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/Support/BinaryStreamReader.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Endian.h"
28 #include "llvm/Support/FileOutputBuffer.h"
29 #include "llvm/Support/Parallel.h"
30 #include "llvm/Support/Path.h"
31 #include "llvm/Support/RandomNumberGenerator.h"
32 #include "llvm/Support/xxhash.h"
40 using namespace llvm::COFF;
41 using namespace llvm::object;
42 using namespace llvm::support;
43 using namespace llvm::support::endian;
48 /* To re-generate DOSProgram:
49 $ cat > /tmp/DOSProgram.asm
54 ; Point ds:dx at the $-terminated string.
56 ; Int 21/AH=09h: Write string to standard output.
59 ; Int 21/AH=4Ch: Exit with return code (in AL).
63 db 'This program cannot be run in DOS mode.$'
65 $ nasm -fbin /tmp/DOSProgram.asm -o /tmp/DOSProgram.bin
66 $ xxd -i /tmp/DOSProgram.bin
68 static unsigned char dosProgram[] = {
69 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8, 0x01, 0x4c,
70 0xcd, 0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72,
71 0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x62, 0x65,
72 0x20, 0x72, 0x75, 0x6e, 0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20,
73 0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x24, 0x00, 0x00
75 static_assert(sizeof(dosProgram) % 8 == 0,
76 "DOSProgram size must be multiple of 8");
78 static const int dosStubSize = sizeof(dos_header) + sizeof(dosProgram);
79 static_assert(dosStubSize % 8 == 0, "DOSStub size must be multiple of 8");
81 static const int numberOfDataDirectory = 16;
83 // Global vector of all output sections. After output sections are finalized,
84 // this can be indexed by Chunk::getOutputSection.
85 static std::vector<OutputSection *> outputSections;
87 OutputSection *Chunk::getOutputSection() const {
88 return osidx == 0 ? nullptr : outputSections[osidx - 1];
93 class DebugDirectoryChunk : public NonSectionChunk {
95 DebugDirectoryChunk(const std::vector<Chunk *> &r, bool writeRepro)
96 : records(r), writeRepro(writeRepro) {}
98 size_t getSize() const override {
99 return (records.size() + int(writeRepro)) * sizeof(debug_directory);
102 void writeTo(uint8_t *b) const override {
103 auto *d = reinterpret_cast<debug_directory *>(b);
105 for (const Chunk *record : records) {
106 OutputSection *os = record->getOutputSection();
107 uint64_t offs = os->getFileOff() + (record->getRVA() - os->getRVA());
108 fillEntry(d, COFF::IMAGE_DEBUG_TYPE_CODEVIEW, record->getSize(),
109 record->getRVA(), offs);
114 // FIXME: The COFF spec allows either a 0-sized entry to just say
115 // "the timestamp field is really a hash", or a 4-byte size field
116 // followed by that many bytes containing a longer hash (with the
117 // lowest 4 bytes usually being the timestamp in little-endian order).
118 // Consider storing the full 8 bytes computed by xxHash64 here.
119 fillEntry(d, COFF::IMAGE_DEBUG_TYPE_REPRO, 0, 0, 0);
123 void setTimeDateStamp(uint32_t timeDateStamp) {
124 for (support::ulittle32_t *tds : timeDateStamps)
125 *tds = timeDateStamp;
129 void fillEntry(debug_directory *d, COFF::DebugType debugType, size_t size,
130 uint64_t rva, uint64_t offs) const {
131 d->Characteristics = 0;
132 d->TimeDateStamp = 0;
136 d->SizeOfData = size;
137 d->AddressOfRawData = rva;
138 d->PointerToRawData = offs;
140 timeDateStamps.push_back(&d->TimeDateStamp);
143 mutable std::vector<support::ulittle32_t *> timeDateStamps;
144 const std::vector<Chunk *> &records;
148 class CVDebugRecordChunk : public NonSectionChunk {
150 size_t getSize() const override {
151 return sizeof(codeview::DebugInfo) + config->pdbAltPath.size() + 1;
154 void writeTo(uint8_t *b) const override {
155 // Save off the DebugInfo entry to backfill the file signature (build id)
156 // in Writer::writeBuildId
157 buildId = reinterpret_cast<codeview::DebugInfo *>(b);
159 // variable sized field (PDB Path)
160 char *p = reinterpret_cast<char *>(b + sizeof(*buildId));
161 if (!config->pdbAltPath.empty())
162 memcpy(p, config->pdbAltPath.data(), config->pdbAltPath.size());
163 p[config->pdbAltPath.size()] = '\0';
166 mutable codeview::DebugInfo *buildId = nullptr;
169 // PartialSection represents a group of chunks that contribute to an
170 // OutputSection. Collating a collection of PartialSections of same name and
171 // characteristics constitutes the OutputSection.
172 class PartialSectionKey {
175 unsigned characteristics;
177 bool operator<(const PartialSectionKey &other) const {
178 int c = name.compare(other.name);
182 return characteristics < other.characteristics;
187 // The writer writes a SymbolTable result to a file.
190 Writer() : buffer(errorHandler().outputBuffer) {}
194 void createSections();
195 void createMiscChunks();
196 void createImportTables();
197 void appendImportThunks();
198 void locateImportTables();
199 void createExportTable();
200 void mergeSections();
201 void removeUnusedSections();
202 void assignAddresses();
203 void finalizeAddresses();
204 void removeEmptySections();
205 void assignOutputSectionIndices();
206 void createSymbolAndStringTable();
207 void openFile(StringRef outputPath);
208 template <typename PEHeaderTy> void writeHeader();
209 void createSEHTable();
210 void createRuntimePseudoRelocs();
211 void insertCtorDtorSymbols();
212 void createGuardCFTables();
213 void markSymbolsForRVATable(ObjFile *file,
214 ArrayRef<SectionChunk *> symIdxChunks,
215 SymbolRVASet &tableSymbols);
216 void maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
218 void setSectionPermissions();
219 void writeSections();
221 void sortExceptionTable();
222 void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
223 void addSyntheticIdata();
224 void fixPartialSectionChars(StringRef name, uint32_t chars);
225 bool fixGnuImportChunks();
226 PartialSection *createPartialSection(StringRef name, uint32_t outChars);
227 PartialSection *findPartialSection(StringRef name, uint32_t outChars);
229 llvm::Optional<coff_symbol16> createSymbol(Defined *d);
230 size_t addEntryToStringTable(StringRef str);
232 OutputSection *findSection(StringRef name);
234 void addBaserelBlocks(std::vector<Baserel> &v);
236 uint32_t getSizeOfInitializedData();
238 std::unique_ptr<FileOutputBuffer> &buffer;
239 std::map<PartialSectionKey, PartialSection *> partialSections;
240 std::vector<char> strtab;
241 std::vector<llvm::object::coff_symbol16> outputSymtab;
243 Chunk *importTableStart = nullptr;
244 uint64_t importTableSize = 0;
245 Chunk *edataStart = nullptr;
246 Chunk *edataEnd = nullptr;
247 Chunk *iatStart = nullptr;
248 uint64_t iatSize = 0;
249 DelayLoadContents delayIdata;
251 bool setNoSEHCharacteristic = false;
253 DebugDirectoryChunk *debugDirectory = nullptr;
254 std::vector<Chunk *> debugRecords;
255 CVDebugRecordChunk *buildId = nullptr;
256 ArrayRef<uint8_t> sectionTable;
259 uint32_t pointerToSymbolTable = 0;
260 uint64_t sizeOfImage;
261 uint64_t sizeOfHeaders;
263 OutputSection *textSec;
264 OutputSection *rdataSec;
265 OutputSection *buildidSec;
266 OutputSection *dataSec;
267 OutputSection *pdataSec;
268 OutputSection *idataSec;
269 OutputSection *edataSec;
270 OutputSection *didatSec;
271 OutputSection *rsrcSec;
272 OutputSection *relocSec;
273 OutputSection *ctorsSec;
274 OutputSection *dtorsSec;
276 // The first and last .pdata sections in the output file.
278 // We need to keep track of the location of .pdata in whichever section it
279 // gets merged into so that we can sort its contents and emit a correct data
280 // directory entry for the exception table. This is also the case for some
281 // other sections (such as .edata) but because the contents of those sections
282 // are entirely linker-generated we can keep track of their locations using
283 // the chunks that the linker creates. All .pdata chunks come from input
284 // files, so we need to keep track of them separately.
285 Chunk *firstPdata = nullptr;
288 } // anonymous namespace
290 static Timer codeLayoutTimer("Code Layout", Timer::root());
291 static Timer diskCommitTimer("Commit Output File", Timer::root());
293 void writeResult() { Writer().run(); }
295 void OutputSection::addChunk(Chunk *c) {
299 void OutputSection::insertChunkAtStart(Chunk *c) {
300 chunks.insert(chunks.begin(), c);
303 void OutputSection::setPermissions(uint32_t c) {
304 header.Characteristics &= ~permMask;
305 header.Characteristics |= c;
308 void OutputSection::merge(OutputSection *other) {
309 chunks.insert(chunks.end(), other->chunks.begin(), other->chunks.end());
310 other->chunks.clear();
311 contribSections.insert(contribSections.end(), other->contribSections.begin(),
312 other->contribSections.end());
313 other->contribSections.clear();
316 // Write the section header to a given buffer.
317 void OutputSection::writeHeaderTo(uint8_t *buf) {
318 auto *hdr = reinterpret_cast<coff_section *>(buf);
320 if (stringTableOff) {
321 // If name is too long, write offset into the string table as a name.
322 sprintf(hdr->Name, "/%d", stringTableOff);
324 assert(!config->debug || name.size() <= COFF::NameSize ||
325 (hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0);
326 strncpy(hdr->Name, name.data(),
327 std::min(name.size(), (size_t)COFF::NameSize));
331 void OutputSection::addContributingPartialSection(PartialSection *sec) {
332 contribSections.push_back(sec);
335 // Check whether the target address S is in range from a relocation
336 // of type relType at address P.
337 static bool isInRange(uint16_t relType, uint64_t s, uint64_t p, int margin) {
338 if (config->machine == ARMNT) {
339 int64_t diff = AbsoluteDifference(s, p + 4) + margin;
341 case IMAGE_REL_ARM_BRANCH20T:
342 return isInt<21>(diff);
343 case IMAGE_REL_ARM_BRANCH24T:
344 case IMAGE_REL_ARM_BLX23T:
345 return isInt<25>(diff);
349 } else if (config->machine == ARM64) {
350 int64_t diff = AbsoluteDifference(s, p) + margin;
352 case IMAGE_REL_ARM64_BRANCH26:
353 return isInt<28>(diff);
354 case IMAGE_REL_ARM64_BRANCH19:
355 return isInt<21>(diff);
356 case IMAGE_REL_ARM64_BRANCH14:
357 return isInt<16>(diff);
362 llvm_unreachable("Unexpected architecture");
366 // Return the last thunk for the given target if it is in range,
367 // or create a new one.
368 static std::pair<Defined *, bool>
369 getThunk(DenseMap<uint64_t, Defined *> &lastThunks, Defined *target, uint64_t p,
370 uint16_t type, int margin) {
371 Defined *&lastThunk = lastThunks[target->getRVA()];
372 if (lastThunk && isInRange(type, lastThunk->getRVA(), p, margin))
373 return {lastThunk, false};
375 switch (config->machine) {
377 c = make<RangeExtensionThunkARM>(target);
380 c = make<RangeExtensionThunkARM64>(target);
383 llvm_unreachable("Unexpected architecture");
385 Defined *d = make<DefinedSynthetic>("", c);
390 // This checks all relocations, and for any relocation which isn't in range
391 // it adds a thunk after the section chunk that contains the relocation.
392 // If the latest thunk for the specific target is in range, that is used
393 // instead of creating a new thunk. All range checks are done with the
394 // specified margin, to make sure that relocations that originally are in
395 // range, but only barely, also get thunks - in case other added thunks makes
396 // the target go out of range.
398 // After adding thunks, we verify that all relocations are in range (with
399 // no extra margin requirements). If this failed, we restart (throwing away
400 // the previously created thunks) and retry with a wider margin.
401 static bool createThunks(OutputSection *os, int margin) {
402 bool addressesChanged = false;
403 DenseMap<uint64_t, Defined *> lastThunks;
404 DenseMap<std::pair<ObjFile *, Defined *>, uint32_t> thunkSymtabIndices;
405 size_t thunksSize = 0;
406 // Recheck Chunks.size() each iteration, since we can insert more
408 for (size_t i = 0; i != os->chunks.size(); ++i) {
409 SectionChunk *sc = dyn_cast_or_null<SectionChunk>(os->chunks[i]);
412 size_t thunkInsertionSpot = i + 1;
414 // Try to get a good enough estimate of where new thunks will be placed.
415 // Offset this by the size of the new thunks added so far, to make the
416 // estimate slightly better.
417 size_t thunkInsertionRVA = sc->getRVA() + sc->getSize() + thunksSize;
418 ObjFile *file = sc->file;
419 std::vector<std::pair<uint32_t, uint32_t>> relocReplacements;
420 ArrayRef<coff_relocation> originalRelocs =
421 file->getCOFFObj()->getRelocations(sc->header);
422 for (size_t j = 0, e = originalRelocs.size(); j < e; ++j) {
423 const coff_relocation &rel = originalRelocs[j];
424 Symbol *relocTarget = file->getSymbol(rel.SymbolTableIndex);
426 // The estimate of the source address P should be pretty accurate,
427 // but we don't know whether the target Symbol address should be
428 // offset by thunksSize or not (or by some of thunksSize but not all of
429 // it), giving us some uncertainty once we have added one thunk.
430 uint64_t p = sc->getRVA() + rel.VirtualAddress + thunksSize;
432 Defined *sym = dyn_cast_or_null<Defined>(relocTarget);
436 uint64_t s = sym->getRVA();
438 if (isInRange(rel.Type, s, p, margin))
441 // If the target isn't in range, hook it up to an existing or new
445 std::tie(thunk, wasNew) = getThunk(lastThunks, sym, p, rel.Type, margin);
447 Chunk *thunkChunk = thunk->getChunk();
449 thunkInsertionRVA); // Estimate of where it will be located.
450 os->chunks.insert(os->chunks.begin() + thunkInsertionSpot, thunkChunk);
451 thunkInsertionSpot++;
452 thunksSize += thunkChunk->getSize();
453 thunkInsertionRVA += thunkChunk->getSize();
454 addressesChanged = true;
457 // To redirect the relocation, add a symbol to the parent object file's
458 // symbol table, and replace the relocation symbol table index with the
460 auto insertion = thunkSymtabIndices.insert({{file, thunk}, ~0U});
461 uint32_t &thunkSymbolIndex = insertion.first->second;
462 if (insertion.second)
463 thunkSymbolIndex = file->addRangeThunkSymbol(thunk);
464 relocReplacements.push_back({j, thunkSymbolIndex});
467 // Get a writable copy of this section's relocations so they can be
468 // modified. If the relocations point into the object file, allocate new
469 // memory. Otherwise, this must be previously allocated memory that can be
470 // modified in place.
471 ArrayRef<coff_relocation> curRelocs = sc->getRelocs();
472 MutableArrayRef<coff_relocation> newRelocs;
473 if (originalRelocs.data() == curRelocs.data()) {
474 newRelocs = makeMutableArrayRef(
475 bAlloc.Allocate<coff_relocation>(originalRelocs.size()),
476 originalRelocs.size());
478 newRelocs = makeMutableArrayRef(
479 const_cast<coff_relocation *>(curRelocs.data()), curRelocs.size());
482 // Copy each relocation, but replace the symbol table indices which need
484 auto nextReplacement = relocReplacements.begin();
485 auto endReplacement = relocReplacements.end();
486 for (size_t i = 0, e = originalRelocs.size(); i != e; ++i) {
487 newRelocs[i] = originalRelocs[i];
488 if (nextReplacement != endReplacement && nextReplacement->first == i) {
489 newRelocs[i].SymbolTableIndex = nextReplacement->second;
494 sc->setRelocs(newRelocs);
496 return addressesChanged;
499 // Verify that all relocations are in range, with no extra margin requirements.
500 static bool verifyRanges(const std::vector<Chunk *> chunks) {
501 for (Chunk *c : chunks) {
502 SectionChunk *sc = dyn_cast_or_null<SectionChunk>(c);
506 ArrayRef<coff_relocation> relocs = sc->getRelocs();
507 for (size_t j = 0, e = relocs.size(); j < e; ++j) {
508 const coff_relocation &rel = relocs[j];
509 Symbol *relocTarget = sc->file->getSymbol(rel.SymbolTableIndex);
511 Defined *sym = dyn_cast_or_null<Defined>(relocTarget);
515 uint64_t p = sc->getRVA() + rel.VirtualAddress;
516 uint64_t s = sym->getRVA();
518 if (!isInRange(rel.Type, s, p, 0))
525 // Assign addresses and add thunks if necessary.
526 void Writer::finalizeAddresses() {
528 if (config->machine != ARMNT && config->machine != ARM64)
531 size_t origNumChunks = 0;
532 for (OutputSection *sec : outputSections) {
533 sec->origChunks = sec->chunks;
534 origNumChunks += sec->chunks.size();
538 int margin = 1024 * 100;
540 // First check whether we need thunks at all, or if the previous pass of
541 // adding them turned out ok.
542 bool rangesOk = true;
543 size_t numChunks = 0;
544 for (OutputSection *sec : outputSections) {
545 if (!verifyRanges(sec->chunks)) {
549 numChunks += sec->chunks.size();
553 log("Added " + Twine(numChunks - origNumChunks) + " thunks with " +
554 "margin " + Twine(margin) + " in " + Twine(pass) + " passes");
559 fatal("adding thunks hasn't converged after " + Twine(pass) + " passes");
562 // If the previous pass didn't work out, reset everything back to the
563 // original conditions before retrying with a wider margin. This should
564 // ideally never happen under real circumstances.
565 for (OutputSection *sec : outputSections)
566 sec->chunks = sec->origChunks;
570 // Try adding thunks everywhere where it is needed, with a margin
571 // to avoid things going out of range due to the added thunks.
572 bool addressesChanged = false;
573 for (OutputSection *sec : outputSections)
574 addressesChanged |= createThunks(sec, margin);
575 // If the verification above thought we needed thunks, we should have
577 assert(addressesChanged);
579 // Recalculate the layout for the whole image (and verify the ranges at
580 // the start of the next round).
587 // The main function of the writer.
589 ScopedTimer t1(codeLayoutTimer);
591 createImportTables();
594 appendImportThunks();
597 removeUnusedSections();
599 removeEmptySections();
600 assignOutputSectionIndices();
601 setSectionPermissions();
602 createSymbolAndStringTable();
604 if (fileSize > UINT32_MAX)
605 fatal("image size (" + Twine(fileSize) + ") " +
606 "exceeds maximum allowable size (" + Twine(UINT32_MAX) + ")");
608 openFile(config->outputFile);
609 if (config->is64()) {
610 writeHeader<pe32plus_header>();
612 writeHeader<pe32_header>();
615 sortExceptionTable();
619 if (!config->pdbPath.empty() && config->debug) {
621 createPDB(symtab, outputSections, sectionTable, buildId->buildId);
625 writeMapFile(outputSections);
630 ScopedTimer t2(diskCommitTimer);
631 if (auto e = buffer->commit())
632 fatal("failed to write the output file: " + toString(std::move(e)));
635 static StringRef getOutputSectionName(StringRef name) {
636 StringRef s = name.split('$').first;
638 // Treat a later period as a separator for MinGW, for sections like
640 return s.substr(0, s.find('.', 1));
644 static void sortBySectionOrder(std::vector<Chunk *> &chunks) {
645 auto getPriority = [](const Chunk *c) {
646 if (auto *sec = dyn_cast<SectionChunk>(c))
648 return config->order.lookup(sec->sym->getName());
652 llvm::stable_sort(chunks, [=](const Chunk *a, const Chunk *b) {
653 return getPriority(a) < getPriority(b);
657 // Change the characteristics of existing PartialSections that belong to the
658 // section Name to Chars.
659 void Writer::fixPartialSectionChars(StringRef name, uint32_t chars) {
660 for (auto it : partialSections) {
661 PartialSection *pSec = it.second;
662 StringRef curName = pSec->name;
663 if (!curName.consume_front(name) ||
664 (!curName.empty() && !curName.startswith("$")))
666 if (pSec->characteristics == chars)
668 PartialSection *destSec = createPartialSection(pSec->name, chars);
669 destSec->chunks.insert(destSec->chunks.end(), pSec->chunks.begin(),
671 pSec->chunks.clear();
675 // Sort concrete section chunks from GNU import libraries.
677 // GNU binutils doesn't use short import files, but instead produces import
678 // libraries that consist of object files, with section chunks for the .idata$*
679 // sections. These are linked just as regular static libraries. Each import
680 // library consists of one header object, one object file for every imported
681 // symbol, and one trailer object. In order for the .idata tables/lists to
682 // be formed correctly, the section chunks within each .idata$* section need
683 // to be grouped by library, and sorted alphabetically within each library
684 // (which makes sure the header comes first and the trailer last).
685 bool Writer::fixGnuImportChunks() {
686 uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
688 // Make sure all .idata$* section chunks are mapped as RDATA in order to
689 // be sorted into the same sections as our own synthesized .idata chunks.
690 fixPartialSectionChars(".idata", rdata);
692 bool hasIdata = false;
693 // Sort all .idata$* chunks, grouping chunks from the same library,
694 // with alphabetical ordering of the object fils within a library.
695 for (auto it : partialSections) {
696 PartialSection *pSec = it.second;
697 if (!pSec->name.startswith(".idata"))
700 if (!pSec->chunks.empty())
702 llvm::stable_sort(pSec->chunks, [&](Chunk *s, Chunk *t) {
703 SectionChunk *sc1 = dyn_cast_or_null<SectionChunk>(s);
704 SectionChunk *sc2 = dyn_cast_or_null<SectionChunk>(t);
706 // if SC1, order them ascending. If SC2 or both null,
707 // S is not less than T.
708 return sc1 != nullptr;
710 // Make a string with "libraryname/objectfile" for sorting, achieving
711 // both grouping by library and sorting of objects within a library,
714 (sc1->file->parentName + "/" + sc1->file->getName()).str();
716 (sc2->file->parentName + "/" + sc2->file->getName()).str();
723 // Add generated idata chunks, for imported symbols and DLLs, and a
724 // terminator in .idata$2.
725 void Writer::addSyntheticIdata() {
726 uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
729 // Add the .idata content in the right section groups, to allow
730 // chunks from other linked in object files to be grouped together.
731 // See Microsoft PE/COFF spec 5.4 for details.
732 auto add = [&](StringRef n, std::vector<Chunk *> &v) {
733 PartialSection *pSec = createPartialSection(n, rdata);
734 pSec->chunks.insert(pSec->chunks.end(), v.begin(), v.end());
737 // The loader assumes a specific order of data.
738 // Add each type in the correct order.
739 add(".idata$2", idata.dirs);
740 add(".idata$4", idata.lookups);
741 add(".idata$5", idata.addresses);
742 if (!idata.hints.empty())
743 add(".idata$6", idata.hints);
744 add(".idata$7", idata.dllNames);
747 // Locate the first Chunk and size of the import directory list and the
749 void Writer::locateImportTables() {
750 uint32_t rdata = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
752 if (PartialSection *importDirs = findPartialSection(".idata$2", rdata)) {
753 if (!importDirs->chunks.empty())
754 importTableStart = importDirs->chunks.front();
755 for (Chunk *c : importDirs->chunks)
756 importTableSize += c->getSize();
759 if (PartialSection *importAddresses = findPartialSection(".idata$5", rdata)) {
760 if (!importAddresses->chunks.empty())
761 iatStart = importAddresses->chunks.front();
762 for (Chunk *c : importAddresses->chunks)
763 iatSize += c->getSize();
767 // Return whether a SectionChunk's suffix (the dollar and any trailing
768 // suffix) should be removed and sorted into the main suffixless
770 static bool shouldStripSectionSuffix(SectionChunk *sc, StringRef name) {
771 // On MinGW, comdat groups are formed by putting the comdat group name
772 // after the '$' in the section name. For .eh_frame$<symbol>, that must
773 // still be sorted before the .eh_frame trailer from crtend.o, thus just
774 // strip the section name trailer. For other sections, such as
775 // .tls$$<symbol> (where non-comdat .tls symbols are otherwise stored in
776 // ".tls$"), they must be strictly sorted after .tls. And for the
777 // hypothetical case of comdat .CRT$XCU, we definitely need to keep the
778 // suffix for sorting. Thus, to play it safe, only strip the suffix for
779 // the standard sections.
782 if (!sc || !sc->isCOMDAT())
784 return name.startswith(".text$") || name.startswith(".data$") ||
785 name.startswith(".rdata$") || name.startswith(".pdata$") ||
786 name.startswith(".xdata$") || name.startswith(".eh_frame$");
789 // Create output section objects and add them to OutputSections.
790 void Writer::createSections() {
791 // First, create the builtin sections.
792 const uint32_t data = IMAGE_SCN_CNT_INITIALIZED_DATA;
793 const uint32_t bss = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
794 const uint32_t code = IMAGE_SCN_CNT_CODE;
795 const uint32_t discardable = IMAGE_SCN_MEM_DISCARDABLE;
796 const uint32_t r = IMAGE_SCN_MEM_READ;
797 const uint32_t w = IMAGE_SCN_MEM_WRITE;
798 const uint32_t x = IMAGE_SCN_MEM_EXECUTE;
800 SmallDenseMap<std::pair<StringRef, uint32_t>, OutputSection *> sections;
801 auto createSection = [&](StringRef name, uint32_t outChars) {
802 OutputSection *&sec = sections[{name, outChars}];
804 sec = make<OutputSection>(name, outChars);
805 outputSections.push_back(sec);
810 // Try to match the section order used by link.exe.
811 textSec = createSection(".text", code | r | x);
812 createSection(".bss", bss | r | w);
813 rdataSec = createSection(".rdata", data | r);
814 buildidSec = createSection(".buildid", data | r);
815 dataSec = createSection(".data", data | r | w);
816 pdataSec = createSection(".pdata", data | r);
817 idataSec = createSection(".idata", data | r);
818 edataSec = createSection(".edata", data | r);
819 didatSec = createSection(".didat", data | r);
820 rsrcSec = createSection(".rsrc", data | r);
821 relocSec = createSection(".reloc", data | discardable | r);
822 ctorsSec = createSection(".ctors", data | r | w);
823 dtorsSec = createSection(".dtors", data | r | w);
825 // Then bin chunks by name and output characteristics.
826 for (Chunk *c : symtab->getChunks()) {
827 auto *sc = dyn_cast<SectionChunk>(c);
828 if (sc && !sc->live) {
830 sc->printDiscardedMessage();
833 StringRef name = c->getSectionName();
834 if (shouldStripSectionSuffix(sc, name))
835 name = name.split('$').first;
836 PartialSection *pSec = createPartialSection(name,
837 c->getOutputCharacteristics());
838 pSec->chunks.push_back(c);
841 fixPartialSectionChars(".rsrc", data | r);
842 fixPartialSectionChars(".edata", 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 (!edataSec->chunks.empty()) {
1018 // Allow using a custom built export table from input object files, instead
1019 // of having the linker synthesize the tables.
1020 if (config->hadExplicitExports)
1021 warn("literal .edata sections override exports");
1022 } else if (!config->exports.empty()) {
1023 for (Chunk *c : edata.chunks)
1024 edataSec->addChunk(c);
1026 if (!edataSec->chunks.empty()) {
1027 edataStart = edataSec->chunks.front();
1028 edataEnd = edataSec->chunks.back();
1032 void Writer::removeUnusedSections() {
1033 // Remove sections that we can be sure won't get content, to avoid
1034 // allocating space for their section headers.
1035 auto isUnused = [this](OutputSection *s) {
1037 return false; // This section is populated later.
1038 // MergeChunks have zero size at this point, as their size is finalized
1039 // later. Only remove sections that have no Chunks at all.
1040 return s->chunks.empty();
1042 outputSections.erase(
1043 std::remove_if(outputSections.begin(), outputSections.end(), isUnused),
1044 outputSections.end());
1047 // The Windows loader doesn't seem to like empty sections,
1048 // so we remove them if any.
1049 void Writer::removeEmptySections() {
1050 auto isEmpty = [](OutputSection *s) { return s->getVirtualSize() == 0; };
1051 outputSections.erase(
1052 std::remove_if(outputSections.begin(), outputSections.end(), isEmpty),
1053 outputSections.end());
1056 void Writer::assignOutputSectionIndices() {
1057 // Assign final output section indices, and assign each chunk to its output
1060 for (OutputSection *os : outputSections) {
1061 os->sectionIndex = idx;
1062 for (Chunk *c : os->chunks)
1063 c->setOutputSectionIdx(idx);
1067 // Merge chunks are containers of chunks, so assign those an output section
1069 for (MergeChunk *mc : MergeChunk::instances)
1071 for (SectionChunk *sc : mc->sections)
1073 sc->setOutputSectionIdx(mc->getOutputSectionIdx());
1076 size_t Writer::addEntryToStringTable(StringRef str) {
1077 assert(str.size() > COFF::NameSize);
1078 size_t offsetOfEntry = strtab.size() + 4; // +4 for the size field
1079 strtab.insert(strtab.end(), str.begin(), str.end());
1080 strtab.push_back('\0');
1081 return offsetOfEntry;
1084 Optional<coff_symbol16> Writer::createSymbol(Defined *def) {
1086 switch (def->kind()) {
1087 case Symbol::DefinedAbsoluteKind:
1088 sym.Value = def->getRVA();
1089 sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
1091 case Symbol::DefinedSyntheticKind:
1092 // Relative symbols are unrepresentable in a COFF symbol table.
1095 // Don't write symbols that won't be written to the output to the symbol
1097 Chunk *c = def->getChunk();
1100 OutputSection *os = c->getOutputSection();
1104 sym.Value = def->getRVA() - os->getRVA();
1105 sym.SectionNumber = os->sectionIndex;
1110 // Symbols that are runtime pseudo relocations don't point to the actual
1111 // symbol data itself (as they are imported), but points to the IAT entry
1112 // instead. Avoid emitting them to the symbol table, as they can confuse
1114 if (def->isRuntimePseudoReloc)
1117 StringRef name = def->getName();
1118 if (name.size() > COFF::NameSize) {
1119 sym.Name.Offset.Zeroes = 0;
1120 sym.Name.Offset.Offset = addEntryToStringTable(name);
1122 memset(sym.Name.ShortName, 0, COFF::NameSize);
1123 memcpy(sym.Name.ShortName, name.data(), name.size());
1126 if (auto *d = dyn_cast<DefinedCOFF>(def)) {
1127 COFFSymbolRef ref = d->getCOFFSymbol();
1128 sym.Type = ref.getType();
1129 sym.StorageClass = ref.getStorageClass();
1131 sym.Type = IMAGE_SYM_TYPE_NULL;
1132 sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
1134 sym.NumberOfAuxSymbols = 0;
1138 void Writer::createSymbolAndStringTable() {
1139 // PE/COFF images are limited to 8 byte section names. Longer names can be
1140 // supported by writing a non-standard string table, but this string table is
1141 // not mapped at runtime and the long names will therefore be inaccessible.
1142 // link.exe always truncates section names to 8 bytes, whereas binutils always
1143 // preserves long section names via the string table. LLD adopts a hybrid
1144 // solution where discardable sections have long names preserved and
1145 // non-discardable sections have their names truncated, to ensure that any
1146 // section which is mapped at runtime also has its name mapped at runtime.
1147 for (OutputSection *sec : outputSections) {
1148 if (sec->name.size() <= COFF::NameSize)
1150 if ((sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0)
1152 if (config->warnLongSectionNames) {
1153 warn("section name " + sec->name +
1154 " is longer than 8 characters and will use a non-standard string "
1157 sec->setStringTableOff(addEntryToStringTable(sec->name));
1160 if (config->debugDwarf || config->debugSymtab) {
1161 for (ObjFile *file : ObjFile::instances) {
1162 for (Symbol *b : file->getSymbols()) {
1163 auto *d = dyn_cast_or_null<Defined>(b);
1164 if (!d || d->writtenToSymtab)
1166 d->writtenToSymtab = true;
1168 if (Optional<coff_symbol16> sym = createSymbol(d))
1169 outputSymtab.push_back(*sym);
1174 if (outputSymtab.empty() && strtab.empty())
1177 // We position the symbol table to be adjacent to the end of the last section.
1178 uint64_t fileOff = fileSize;
1179 pointerToSymbolTable = fileOff;
1180 fileOff += outputSymtab.size() * sizeof(coff_symbol16);
1181 fileOff += 4 + strtab.size();
1182 fileSize = alignTo(fileOff, config->fileAlign);
1185 void Writer::mergeSections() {
1186 if (!pdataSec->chunks.empty()) {
1187 firstPdata = pdataSec->chunks.front();
1188 lastPdata = pdataSec->chunks.back();
1191 for (auto &p : config->merge) {
1192 StringRef toName = p.second;
1193 if (p.first == toName)
1197 if (!names.insert(toName).second)
1198 fatal("/merge: cycle found for section '" + p.first + "'");
1199 auto i = config->merge.find(toName);
1200 if (i == config->merge.end())
1204 OutputSection *from = findSection(p.first);
1205 OutputSection *to = findSection(toName);
1209 from->name = toName;
1216 // Visits all sections to assign incremental, non-overlapping RVAs and
1218 void Writer::assignAddresses() {
1219 sizeOfHeaders = dosStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
1220 sizeof(data_directory) * numberOfDataDirectory +
1221 sizeof(coff_section) * outputSections.size();
1223 config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
1224 sizeOfHeaders = alignTo(sizeOfHeaders, config->fileAlign);
1225 fileSize = sizeOfHeaders;
1227 // The first page is kept unmapped.
1228 uint64_t rva = alignTo(sizeOfHeaders, config->align);
1230 for (OutputSection *sec : outputSections) {
1231 if (sec == relocSec)
1233 uint64_t rawSize = 0, virtualSize = 0;
1234 sec->header.VirtualAddress = rva;
1236 // If /FUNCTIONPADMIN is used, functions are padded in order to create a
1237 // hotpatchable image.
1238 const bool isCodeSection =
1239 (sec->header.Characteristics & IMAGE_SCN_CNT_CODE) &&
1240 (sec->header.Characteristics & IMAGE_SCN_MEM_READ) &&
1241 (sec->header.Characteristics & IMAGE_SCN_MEM_EXECUTE);
1242 uint32_t padding = isCodeSection ? config->functionPadMin : 0;
1244 for (Chunk *c : sec->chunks) {
1245 if (padding && c->isHotPatchable())
1246 virtualSize += padding;
1247 virtualSize = alignTo(virtualSize, c->getAlignment());
1248 c->setRVA(rva + virtualSize);
1249 virtualSize += c->getSize();
1251 rawSize = alignTo(virtualSize, config->fileAlign);
1253 if (virtualSize > UINT32_MAX)
1254 error("section larger than 4 GiB: " + sec->name);
1255 sec->header.VirtualSize = virtualSize;
1256 sec->header.SizeOfRawData = rawSize;
1258 sec->header.PointerToRawData = fileSize;
1259 rva += alignTo(virtualSize, config->align);
1260 fileSize += alignTo(rawSize, config->fileAlign);
1262 sizeOfImage = alignTo(rva, config->align);
1264 // Assign addresses to sections in MergeChunks.
1265 for (MergeChunk *mc : MergeChunk::instances)
1267 mc->assignSubsectionRVAs();
1270 template <typename PEHeaderTy> void Writer::writeHeader() {
1271 // Write DOS header. For backwards compatibility, the first part of a PE/COFF
1272 // executable consists of an MS-DOS MZ executable. If the executable is run
1273 // under DOS, that program gets run (usually to just print an error message).
1274 // When run under Windows, the loader looks at AddressOfNewExeHeader and uses
1275 // the PE header instead.
1276 uint8_t *buf = buffer->getBufferStart();
1277 auto *dos = reinterpret_cast<dos_header *>(buf);
1278 buf += sizeof(dos_header);
1279 dos->Magic[0] = 'M';
1280 dos->Magic[1] = 'Z';
1281 dos->UsedBytesInTheLastPage = dosStubSize % 512;
1282 dos->FileSizeInPages = divideCeil(dosStubSize, 512);
1283 dos->HeaderSizeInParagraphs = sizeof(dos_header) / 16;
1285 dos->AddressOfRelocationTable = sizeof(dos_header);
1286 dos->AddressOfNewExeHeader = dosStubSize;
1288 // Write DOS program.
1289 memcpy(buf, dosProgram, sizeof(dosProgram));
1290 buf += sizeof(dosProgram);
1293 memcpy(buf, PEMagic, sizeof(PEMagic));
1294 buf += sizeof(PEMagic);
1296 // Write COFF header
1297 auto *coff = reinterpret_cast<coff_file_header *>(buf);
1298 buf += sizeof(*coff);
1299 coff->Machine = config->machine;
1300 coff->NumberOfSections = outputSections.size();
1301 coff->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
1302 if (config->largeAddressAware)
1303 coff->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
1304 if (!config->is64())
1305 coff->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
1307 coff->Characteristics |= IMAGE_FILE_DLL;
1308 if (config->driverUponly)
1309 coff->Characteristics |= IMAGE_FILE_UP_SYSTEM_ONLY;
1310 if (!config->relocatable)
1311 coff->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
1312 if (config->swaprunCD)
1313 coff->Characteristics |= IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP;
1314 if (config->swaprunNet)
1315 coff->Characteristics |= IMAGE_FILE_NET_RUN_FROM_SWAP;
1316 coff->SizeOfOptionalHeader =
1317 sizeof(PEHeaderTy) + sizeof(data_directory) * numberOfDataDirectory;
1320 auto *pe = reinterpret_cast<PEHeaderTy *>(buf);
1322 pe->Magic = config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
1324 // If {Major,Minor}LinkerVersion is left at 0.0, then for some
1325 // reason signing the resulting PE file with Authenticode produces a
1326 // signature that fails to validate on Windows 7 (but is OK on 10).
1327 // Set it to 14.0, which is what VS2015 outputs, and which avoids
1329 pe->MajorLinkerVersion = 14;
1330 pe->MinorLinkerVersion = 0;
1332 pe->ImageBase = config->imageBase;
1333 pe->SectionAlignment = config->align;
1334 pe->FileAlignment = config->fileAlign;
1335 pe->MajorImageVersion = config->majorImageVersion;
1336 pe->MinorImageVersion = config->minorImageVersion;
1337 pe->MajorOperatingSystemVersion = config->majorOSVersion;
1338 pe->MinorOperatingSystemVersion = config->minorOSVersion;
1339 pe->MajorSubsystemVersion = config->majorOSVersion;
1340 pe->MinorSubsystemVersion = config->minorOSVersion;
1341 pe->Subsystem = config->subsystem;
1342 pe->SizeOfImage = sizeOfImage;
1343 pe->SizeOfHeaders = sizeOfHeaders;
1344 if (!config->noEntry) {
1345 Defined *entry = cast<Defined>(config->entry);
1346 pe->AddressOfEntryPoint = entry->getRVA();
1347 // Pointer to thumb code must have the LSB set, so adjust it.
1348 if (config->machine == ARMNT)
1349 pe->AddressOfEntryPoint |= 1;
1351 pe->SizeOfStackReserve = config->stackReserve;
1352 pe->SizeOfStackCommit = config->stackCommit;
1353 pe->SizeOfHeapReserve = config->heapReserve;
1354 pe->SizeOfHeapCommit = config->heapCommit;
1355 if (config->appContainer)
1356 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER;
1357 if (config->driverWdm)
1358 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER;
1359 if (config->dynamicBase)
1360 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
1361 if (config->highEntropyVA)
1362 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
1363 if (!config->allowBind)
1364 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND;
1365 if (config->nxCompat)
1366 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
1367 if (!config->allowIsolation)
1368 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION;
1369 if (config->guardCF != GuardCFLevel::Off)
1370 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF;
1371 if (config->integrityCheck)
1372 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
1373 if (setNoSEHCharacteristic)
1374 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH;
1375 if (config->terminalServerAware)
1376 pe->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
1377 pe->NumberOfRvaAndSize = numberOfDataDirectory;
1378 if (textSec->getVirtualSize()) {
1379 pe->BaseOfCode = textSec->getRVA();
1380 pe->SizeOfCode = textSec->getRawSize();
1382 pe->SizeOfInitializedData = getSizeOfInitializedData();
1384 // Write data directory
1385 auto *dir = reinterpret_cast<data_directory *>(buf);
1386 buf += sizeof(*dir) * numberOfDataDirectory;
1388 dir[EXPORT_TABLE].RelativeVirtualAddress = edataStart->getRVA();
1389 dir[EXPORT_TABLE].Size =
1390 edataEnd->getRVA() + edataEnd->getSize() - edataStart->getRVA();
1392 if (importTableStart) {
1393 dir[IMPORT_TABLE].RelativeVirtualAddress = importTableStart->getRVA();
1394 dir[IMPORT_TABLE].Size = importTableSize;
1397 dir[IAT].RelativeVirtualAddress = iatStart->getRVA();
1398 dir[IAT].Size = iatSize;
1400 if (rsrcSec->getVirtualSize()) {
1401 dir[RESOURCE_TABLE].RelativeVirtualAddress = rsrcSec->getRVA();
1402 dir[RESOURCE_TABLE].Size = rsrcSec->getVirtualSize();
1405 dir[EXCEPTION_TABLE].RelativeVirtualAddress = firstPdata->getRVA();
1406 dir[EXCEPTION_TABLE].Size =
1407 lastPdata->getRVA() + lastPdata->getSize() - firstPdata->getRVA();
1409 if (relocSec->getVirtualSize()) {
1410 dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = relocSec->getRVA();
1411 dir[BASE_RELOCATION_TABLE].Size = relocSec->getVirtualSize();
1413 if (Symbol *sym = symtab->findUnderscore("_tls_used")) {
1414 if (Defined *b = dyn_cast<Defined>(sym)) {
1415 dir[TLS_TABLE].RelativeVirtualAddress = b->getRVA();
1416 dir[TLS_TABLE].Size = config->is64()
1417 ? sizeof(object::coff_tls_directory64)
1418 : sizeof(object::coff_tls_directory32);
1421 if (debugDirectory) {
1422 dir[DEBUG_DIRECTORY].RelativeVirtualAddress = debugDirectory->getRVA();
1423 dir[DEBUG_DIRECTORY].Size = debugDirectory->getSize();
1425 if (Symbol *sym = symtab->findUnderscore("_load_config_used")) {
1426 if (auto *b = dyn_cast<DefinedRegular>(sym)) {
1427 SectionChunk *sc = b->getChunk();
1428 assert(b->getRVA() >= sc->getRVA());
1429 uint64_t offsetInChunk = b->getRVA() - sc->getRVA();
1430 if (!sc->hasData || offsetInChunk + 4 > sc->getSize())
1431 fatal("_load_config_used is malformed");
1433 ArrayRef<uint8_t> secContents = sc->getContents();
1434 uint32_t loadConfigSize =
1435 *reinterpret_cast<const ulittle32_t *>(&secContents[offsetInChunk]);
1436 if (offsetInChunk + loadConfigSize > sc->getSize())
1437 fatal("_load_config_used is too large");
1438 dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = b->getRVA();
1439 dir[LOAD_CONFIG_TABLE].Size = loadConfigSize;
1442 if (!delayIdata.empty()) {
1443 dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
1444 delayIdata.getDirRVA();
1445 dir[DELAY_IMPORT_DESCRIPTOR].Size = delayIdata.getDirSize();
1448 // Write section table
1449 for (OutputSection *sec : outputSections) {
1450 sec->writeHeaderTo(buf);
1451 buf += sizeof(coff_section);
1453 sectionTable = ArrayRef<uint8_t>(
1454 buf - outputSections.size() * sizeof(coff_section), buf);
1456 if (outputSymtab.empty() && strtab.empty())
1459 coff->PointerToSymbolTable = pointerToSymbolTable;
1460 uint32_t numberOfSymbols = outputSymtab.size();
1461 coff->NumberOfSymbols = numberOfSymbols;
1462 auto *symbolTable = reinterpret_cast<coff_symbol16 *>(
1463 buffer->getBufferStart() + coff->PointerToSymbolTable);
1464 for (size_t i = 0; i != numberOfSymbols; ++i)
1465 symbolTable[i] = outputSymtab[i];
1466 // Create the string table, it follows immediately after the symbol table.
1467 // The first 4 bytes is length including itself.
1468 buf = reinterpret_cast<uint8_t *>(&symbolTable[numberOfSymbols]);
1469 write32le(buf, strtab.size() + 4);
1470 if (!strtab.empty())
1471 memcpy(buf + 4, strtab.data(), strtab.size());
1474 void Writer::openFile(StringRef path) {
1476 FileOutputBuffer::create(path, fileSize, FileOutputBuffer::F_executable),
1477 "failed to open " + path);
1480 void Writer::createSEHTable() {
1481 SymbolRVASet handlers;
1482 for (ObjFile *file : ObjFile::instances) {
1483 if (!file->hasSafeSEH())
1484 error("/safeseh: " + file->getName() + " is not compatible with SEH");
1485 markSymbolsForRVATable(file, file->getSXDataChunks(), handlers);
1488 // Set the "no SEH" characteristic if there really were no handlers, or if
1489 // there is no load config object to point to the table of handlers.
1490 setNoSEHCharacteristic =
1491 handlers.empty() || !symtab->findUnderscore("_load_config_used");
1493 maybeAddRVATable(std::move(handlers), "__safe_se_handler_table",
1494 "__safe_se_handler_count");
1497 // Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set
1498 // cannot contain duplicates. Therefore, the set is uniqued by Chunk and the
1499 // symbol's offset into that Chunk.
1500 static void addSymbolToRVASet(SymbolRVASet &rvaSet, Defined *s) {
1501 Chunk *c = s->getChunk();
1502 if (auto *sc = dyn_cast<SectionChunk>(c))
1503 c = sc->repl; // Look through ICF replacement.
1504 uint32_t off = s->getRVA() - (c ? c->getRVA() : 0);
1505 rvaSet.insert({c, off});
1508 // Given a symbol, add it to the GFIDs table if it is a live, defined, function
1509 // symbol in an executable section.
1510 static void maybeAddAddressTakenFunction(SymbolRVASet &addressTakenSyms,
1515 switch (s->kind()) {
1516 case Symbol::DefinedLocalImportKind:
1517 case Symbol::DefinedImportDataKind:
1518 // Defines an __imp_ pointer, so it is data, so it is ignored.
1520 case Symbol::DefinedCommonKind:
1521 // Common is always data, so it is ignored.
1523 case Symbol::DefinedAbsoluteKind:
1524 case Symbol::DefinedSyntheticKind:
1525 // Absolute is never code, synthetic generally isn't and usually isn't
1528 case Symbol::LazyArchiveKind:
1529 case Symbol::LazyObjectKind:
1530 case Symbol::UndefinedKind:
1531 // Undefined symbols resolve to zero, so they don't have an RVA. Lazy
1532 // symbols shouldn't have relocations.
1535 case Symbol::DefinedImportThunkKind:
1536 // Thunks are always code, include them.
1537 addSymbolToRVASet(addressTakenSyms, cast<Defined>(s));
1540 case Symbol::DefinedRegularKind: {
1541 // This is a regular, defined, symbol from a COFF file. Mark the symbol as
1542 // address taken if the symbol type is function and it's in an executable
1544 auto *d = cast<DefinedRegular>(s);
1545 if (d->getCOFFSymbol().getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
1546 SectionChunk *sc = dyn_cast<SectionChunk>(d->getChunk());
1547 if (sc && sc->live &&
1548 sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE)
1549 addSymbolToRVASet(addressTakenSyms, d);
1556 // Visit all relocations from all section contributions of this object file and
1557 // mark the relocation target as address-taken.
1558 static void markSymbolsWithRelocations(ObjFile *file,
1559 SymbolRVASet &usedSymbols) {
1560 for (Chunk *c : file->getChunks()) {
1561 // We only care about live section chunks. Common chunks and other chunks
1562 // don't generally contain relocations.
1563 SectionChunk *sc = dyn_cast<SectionChunk>(c);
1564 if (!sc || !sc->live)
1567 for (const coff_relocation &reloc : sc->getRelocs()) {
1568 if (config->machine == I386 && reloc.Type == COFF::IMAGE_REL_I386_REL32)
1569 // Ignore relative relocations on x86. On x86_64 they can't be ignored
1570 // since they're also used to compute absolute addresses.
1573 Symbol *ref = sc->file->getSymbol(reloc.SymbolTableIndex);
1574 maybeAddAddressTakenFunction(usedSymbols, ref);
1579 // Create the guard function id table. This is a table of RVAs of all
1580 // address-taken functions. It is sorted and uniqued, just like the safe SEH
1582 void Writer::createGuardCFTables() {
1583 SymbolRVASet addressTakenSyms;
1584 SymbolRVASet longJmpTargets;
1585 for (ObjFile *file : ObjFile::instances) {
1586 // If the object was compiled with /guard:cf, the address taken symbols
1587 // are in .gfids$y sections, and the longjmp targets are in .gljmp$y
1588 // sections. If the object was not compiled with /guard:cf, we assume there
1589 // were no setjmp targets, and that all code symbols with relocations are
1590 // possibly address-taken.
1591 if (file->hasGuardCF()) {
1592 markSymbolsForRVATable(file, file->getGuardFidChunks(), addressTakenSyms);
1593 markSymbolsForRVATable(file, file->getGuardLJmpChunks(), longJmpTargets);
1595 markSymbolsWithRelocations(file, addressTakenSyms);
1599 // Mark the image entry as address-taken.
1601 maybeAddAddressTakenFunction(addressTakenSyms, config->entry);
1603 // Mark exported symbols in executable sections as address-taken.
1604 for (Export &e : config->exports)
1605 maybeAddAddressTakenFunction(addressTakenSyms, e.sym);
1607 // Ensure sections referenced in the gfid table are 16-byte aligned.
1608 for (const ChunkAndOffset &c : addressTakenSyms)
1609 if (c.inputChunk->getAlignment() < 16)
1610 c.inputChunk->setAlignment(16);
1612 maybeAddRVATable(std::move(addressTakenSyms), "__guard_fids_table",
1613 "__guard_fids_count");
1615 // Add the longjmp target table unless the user told us not to.
1616 if (config->guardCF == GuardCFLevel::Full)
1617 maybeAddRVATable(std::move(longJmpTargets), "__guard_longjmp_table",
1618 "__guard_longjmp_count");
1620 // Set __guard_flags, which will be used in the load config to indicate that
1621 // /guard:cf was enabled.
1622 uint32_t guardFlags = uint32_t(coff_guard_flags::CFInstrumented) |
1623 uint32_t(coff_guard_flags::HasFidTable);
1624 if (config->guardCF == GuardCFLevel::Full)
1625 guardFlags |= uint32_t(coff_guard_flags::HasLongJmpTable);
1626 Symbol *flagSym = symtab->findUnderscore("__guard_flags");
1627 cast<DefinedAbsolute>(flagSym)->setVA(guardFlags);
1630 // Take a list of input sections containing symbol table indices and add those
1631 // symbols to an RVA table. The challenge is that symbol RVAs are not known and
1632 // depend on the table size, so we can't directly build a set of integers.
1633 void Writer::markSymbolsForRVATable(ObjFile *file,
1634 ArrayRef<SectionChunk *> symIdxChunks,
1635 SymbolRVASet &tableSymbols) {
1636 for (SectionChunk *c : symIdxChunks) {
1637 // Skip sections discarded by linker GC. This comes up when a .gfids section
1638 // is associated with something like a vtable and the vtable is discarded.
1639 // In this case, the associated gfids section is discarded, and we don't
1640 // mark the virtual member functions as address-taken by the vtable.
1644 // Validate that the contents look like symbol table indices.
1645 ArrayRef<uint8_t> data = c->getContents();
1646 if (data.size() % 4 != 0) {
1647 warn("ignoring " + c->getSectionName() +
1648 " symbol table index section in object " + toString(file));
1652 // Read each symbol table index and check if that symbol was included in the
1653 // final link. If so, add it to the table symbol set.
1654 ArrayRef<ulittle32_t> symIndices(
1655 reinterpret_cast<const ulittle32_t *>(data.data()), data.size() / 4);
1656 ArrayRef<Symbol *> objSymbols = file->getSymbols();
1657 for (uint32_t symIndex : symIndices) {
1658 if (symIndex >= objSymbols.size()) {
1659 warn("ignoring invalid symbol table index in section " +
1660 c->getSectionName() + " in object " + toString(file));
1663 if (Symbol *s = objSymbols[symIndex]) {
1665 addSymbolToRVASet(tableSymbols, cast<Defined>(s));
1671 // Replace the absolute table symbol with a synthetic symbol pointing to
1672 // tableChunk so that we can emit base relocations for it and resolve section
1673 // relative relocations.
1674 void Writer::maybeAddRVATable(SymbolRVASet tableSymbols, StringRef tableSym,
1675 StringRef countSym) {
1676 if (tableSymbols.empty())
1679 RVATableChunk *tableChunk = make<RVATableChunk>(std::move(tableSymbols));
1680 rdataSec->addChunk(tableChunk);
1682 Symbol *t = symtab->findUnderscore(tableSym);
1683 Symbol *c = symtab->findUnderscore(countSym);
1684 replaceSymbol<DefinedSynthetic>(t, t->getName(), tableChunk);
1685 cast<DefinedAbsolute>(c)->setVA(tableChunk->getSize() / 4);
1688 // MinGW specific. Gather all relocations that are imported from a DLL even
1689 // though the code didn't expect it to, produce the table that the runtime
1690 // uses for fixing them up, and provide the synthetic symbols that the
1691 // runtime uses for finding the table.
1692 void Writer::createRuntimePseudoRelocs() {
1693 std::vector<RuntimePseudoReloc> rels;
1695 for (Chunk *c : symtab->getChunks()) {
1696 auto *sc = dyn_cast<SectionChunk>(c);
1697 if (!sc || !sc->live)
1699 sc->getRuntimePseudoRelocs(rels);
1703 log("Writing " + Twine(rels.size()) + " runtime pseudo relocations");
1704 PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(rels);
1705 rdataSec->addChunk(table);
1706 EmptyChunk *endOfList = make<EmptyChunk>();
1707 rdataSec->addChunk(endOfList);
1709 Symbol *headSym = symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__");
1710 Symbol *endSym = symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__");
1711 replaceSymbol<DefinedSynthetic>(headSym, headSym->getName(), table);
1712 replaceSymbol<DefinedSynthetic>(endSym, endSym->getName(), endOfList);
1716 // The MinGW .ctors and .dtors lists have sentinels at each end;
1717 // a (uintptr_t)-1 at the start and a (uintptr_t)0 at the end.
1718 // There's a symbol pointing to the start sentinel pointer, __CTOR_LIST__
1719 // and __DTOR_LIST__ respectively.
1720 void Writer::insertCtorDtorSymbols() {
1721 AbsolutePointerChunk *ctorListHead = make<AbsolutePointerChunk>(-1);
1722 AbsolutePointerChunk *ctorListEnd = make<AbsolutePointerChunk>(0);
1723 AbsolutePointerChunk *dtorListHead = make<AbsolutePointerChunk>(-1);
1724 AbsolutePointerChunk *dtorListEnd = make<AbsolutePointerChunk>(0);
1725 ctorsSec->insertChunkAtStart(ctorListHead);
1726 ctorsSec->addChunk(ctorListEnd);
1727 dtorsSec->insertChunkAtStart(dtorListHead);
1728 dtorsSec->addChunk(dtorListEnd);
1730 Symbol *ctorListSym = symtab->findUnderscore("__CTOR_LIST__");
1731 Symbol *dtorListSym = symtab->findUnderscore("__DTOR_LIST__");
1732 replaceSymbol<DefinedSynthetic>(ctorListSym, ctorListSym->getName(),
1734 replaceSymbol<DefinedSynthetic>(dtorListSym, dtorListSym->getName(),
1738 // Handles /section options to allow users to overwrite
1739 // section attributes.
1740 void Writer::setSectionPermissions() {
1741 for (auto &p : config->section) {
1742 StringRef name = p.first;
1743 uint32_t perm = p.second;
1744 for (OutputSection *sec : outputSections)
1745 if (sec->name == name)
1746 sec->setPermissions(perm);
1750 // Write section contents to a mmap'ed file.
1751 void Writer::writeSections() {
1752 // Record the number of sections to apply section index relocations
1753 // against absolute symbols. See applySecIdx in Chunks.cpp..
1754 DefinedAbsolute::numOutputSections = outputSections.size();
1756 uint8_t *buf = buffer->getBufferStart();
1757 for (OutputSection *sec : outputSections) {
1758 uint8_t *secBuf = buf + sec->getFileOff();
1759 // Fill gaps between functions in .text with INT3 instructions
1760 // instead of leaving as NUL bytes (which can be interpreted as
1761 // ADD instructions).
1762 if (sec->header.Characteristics & IMAGE_SCN_CNT_CODE)
1763 memset(secBuf, 0xCC, sec->getRawSize());
1764 parallelForEach(sec->chunks, [&](Chunk *c) {
1765 c->writeTo(secBuf + c->getRVA() - sec->getRVA());
1770 void Writer::writeBuildId() {
1771 // There are two important parts to the build ID.
1772 // 1) If building with debug info, the COFF debug directory contains a
1773 // timestamp as well as a Guid and Age of the PDB.
1774 // 2) In all cases, the PE COFF file header also contains a timestamp.
1775 // For reproducibility, instead of a timestamp we want to use a hash of the
1777 if (config->debug) {
1778 assert(buildId && "BuildId is not set!");
1779 // BuildId->BuildId was filled in when the PDB was written.
1782 // At this point the only fields in the COFF file which remain unset are the
1783 // "timestamp" in the COFF file header, and the ones in the coff debug
1784 // directory. Now we can hash the file and write that hash to the various
1785 // timestamp fields in the file.
1786 StringRef outputFileData(
1787 reinterpret_cast<const char *>(buffer->getBufferStart()),
1788 buffer->getBufferSize());
1790 uint32_t timestamp = config->timestamp;
1792 bool generateSyntheticBuildId =
1793 config->mingw && config->debug && config->pdbPath.empty();
1795 if (config->repro || generateSyntheticBuildId)
1796 hash = xxHash64(outputFileData);
1799 timestamp = static_cast<uint32_t>(hash);
1801 if (generateSyntheticBuildId) {
1802 // For MinGW builds without a PDB file, we still generate a build id
1803 // to allow associating a crash dump to the executable.
1804 buildId->buildId->PDB70.CVSignature = OMF::Signature::PDB70;
1805 buildId->buildId->PDB70.Age = 1;
1806 memcpy(buildId->buildId->PDB70.Signature, &hash, 8);
1807 // xxhash only gives us 8 bytes, so put some fixed data in the other half.
1808 memcpy(&buildId->buildId->PDB70.Signature[8], "LLD PDB.", 8);
1812 debugDirectory->setTimeDateStamp(timestamp);
1814 uint8_t *buf = buffer->getBufferStart();
1815 buf += dosStubSize + sizeof(PEMagic);
1816 object::coff_file_header *coffHeader =
1817 reinterpret_cast<coff_file_header *>(buf);
1818 coffHeader->TimeDateStamp = timestamp;
1821 // Sort .pdata section contents according to PE/COFF spec 5.5.
1822 void Writer::sortExceptionTable() {
1825 // We assume .pdata contains function table entries only.
1826 auto bufAddr = [&](Chunk *c) {
1827 OutputSection *os = c->getOutputSection();
1828 return buffer->getBufferStart() + os->getFileOff() + c->getRVA() -
1831 uint8_t *begin = bufAddr(firstPdata);
1832 uint8_t *end = bufAddr(lastPdata) + lastPdata->getSize();
1833 if (config->machine == AMD64) {
1834 struct Entry { ulittle32_t begin, end, unwind; };
1836 MutableArrayRef<Entry>((Entry *)begin, (Entry *)end),
1837 [](const Entry &a, const Entry &b) { return a.begin < b.begin; });
1840 if (config->machine == ARMNT || config->machine == ARM64) {
1841 struct Entry { ulittle32_t begin, unwind; };
1843 MutableArrayRef<Entry>((Entry *)begin, (Entry *)end),
1844 [](const Entry &a, const Entry &b) { return a.begin < b.begin; });
1847 lld::errs() << "warning: don't know how to handle .pdata.\n";
1850 // The CRT section contains, among other things, the array of function
1851 // pointers that initialize every global variable that is not trivially
1852 // constructed. The CRT calls them one after the other prior to invoking
1855 // As per C++ spec, 3.6.2/2.3,
1856 // "Variables with ordered initialization defined within a single
1857 // translation unit shall be initialized in the order of their definitions
1858 // in the translation unit"
1860 // It is therefore critical to sort the chunks containing the function
1861 // pointers in the order that they are listed in the object file (top to
1862 // bottom), otherwise global objects might not be initialized in the
1864 void Writer::sortCRTSectionChunks(std::vector<Chunk *> &chunks) {
1865 auto sectionChunkOrder = [](const Chunk *a, const Chunk *b) {
1866 auto sa = dyn_cast<SectionChunk>(a);
1867 auto sb = dyn_cast<SectionChunk>(b);
1868 assert(sa && sb && "Non-section chunks in CRT section!");
1870 StringRef sAObj = sa->file->mb.getBufferIdentifier();
1871 StringRef sBObj = sb->file->mb.getBufferIdentifier();
1873 return sAObj == sBObj && sa->getSectionNumber() < sb->getSectionNumber();
1875 llvm::stable_sort(chunks, sectionChunkOrder);
1877 if (config->verbose) {
1878 for (auto &c : chunks) {
1879 auto sc = dyn_cast<SectionChunk>(c);
1880 log(" " + sc->file->mb.getBufferIdentifier().str() +
1881 ", SectionID: " + Twine(sc->getSectionNumber()));
1886 OutputSection *Writer::findSection(StringRef name) {
1887 for (OutputSection *sec : outputSections)
1888 if (sec->name == name)
1893 uint32_t Writer::getSizeOfInitializedData() {
1895 for (OutputSection *s : outputSections)
1896 if (s->header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
1897 res += s->getRawSize();
1901 // Add base relocations to .reloc section.
1902 void Writer::addBaserels() {
1903 if (!config->relocatable)
1905 relocSec->chunks.clear();
1906 std::vector<Baserel> v;
1907 for (OutputSection *sec : outputSections) {
1908 if (sec->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
1910 // Collect all locations for base relocations.
1911 for (Chunk *c : sec->chunks)
1913 // Add the addresses to .reloc section.
1915 addBaserelBlocks(v);
1920 // Add addresses to .reloc section. Note that addresses are grouped by page.
1921 void Writer::addBaserelBlocks(std::vector<Baserel> &v) {
1922 const uint32_t mask = ~uint32_t(pageSize - 1);
1923 uint32_t page = v[0].rva & mask;
1924 size_t i = 0, j = 1;
1925 for (size_t e = v.size(); j < e; ++j) {
1926 uint32_t p = v[j].rva & mask;
1929 relocSec->addChunk(make<BaserelChunk>(page, &v[i], &v[0] + j));
1935 relocSec->addChunk(make<BaserelChunk>(page, &v[i], &v[0] + j));
1938 PartialSection *Writer::createPartialSection(StringRef name,
1939 uint32_t outChars) {
1940 PartialSection *&pSec = partialSections[{name, outChars}];
1943 pSec = make<PartialSection>(name, outChars);
1947 PartialSection *Writer::findPartialSection(StringRef name, uint32_t outChars) {
1948 auto it = partialSections.find({name, outChars});
1949 if (it != partialSections.end())