1 //===- Writer.cpp ---------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 #include "InputFiles.h"
16 #include "SymbolTable.h"
18 #include "lld/Common/ErrorHandler.h"
19 #include "lld/Common/Memory.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 SectorSize = 512;
77 static const int DOSStubSize = sizeof(dos_header) + sizeof(DOSProgram);
78 static_assert(DOSStubSize % 8 == 0, "DOSStub size must be multiple of 8");
80 static const int NumberOfDataDirectory = 16;
84 class DebugDirectoryChunk : public Chunk {
86 DebugDirectoryChunk(const std::vector<Chunk *> &R, bool WriteRepro)
87 : Records(R), WriteRepro(WriteRepro) {}
89 size_t getSize() const override {
90 return (Records.size() + int(WriteRepro)) * sizeof(debug_directory);
93 void writeTo(uint8_t *B) const override {
94 auto *D = reinterpret_cast<debug_directory *>(B + OutputSectionOff);
96 for (const Chunk *Record : Records) {
97 OutputSection *OS = Record->getOutputSection();
98 uint64_t Offs = OS->getFileOff() + (Record->getRVA() - OS->getRVA());
99 fillEntry(D, COFF::IMAGE_DEBUG_TYPE_CODEVIEW, Record->getSize(),
100 Record->getRVA(), Offs);
105 // FIXME: The COFF spec allows either a 0-sized entry to just say
106 // "the timestamp field is really a hash", or a 4-byte size field
107 // followed by that many bytes containing a longer hash (with the
108 // lowest 4 bytes usually being the timestamp in little-endian order).
109 // Consider storing the full 8 bytes computed by xxHash64 here.
110 fillEntry(D, COFF::IMAGE_DEBUG_TYPE_REPRO, 0, 0, 0);
114 void setTimeDateStamp(uint32_t TimeDateStamp) {
115 for (support::ulittle32_t *TDS : TimeDateStamps)
116 *TDS = TimeDateStamp;
120 void fillEntry(debug_directory *D, COFF::DebugType DebugType, size_t Size,
121 uint64_t RVA, uint64_t Offs) const {
122 D->Characteristics = 0;
123 D->TimeDateStamp = 0;
127 D->SizeOfData = Size;
128 D->AddressOfRawData = RVA;
129 D->PointerToRawData = Offs;
131 TimeDateStamps.push_back(&D->TimeDateStamp);
134 mutable std::vector<support::ulittle32_t *> TimeDateStamps;
135 const std::vector<Chunk *> &Records;
139 class CVDebugRecordChunk : public Chunk {
141 size_t getSize() const override {
142 return sizeof(codeview::DebugInfo) + Config->PDBAltPath.size() + 1;
145 void writeTo(uint8_t *B) const override {
146 // Save off the DebugInfo entry to backfill the file signature (build id)
147 // in Writer::writeBuildId
148 BuildId = reinterpret_cast<codeview::DebugInfo *>(B + OutputSectionOff);
150 // variable sized field (PDB Path)
151 char *P = reinterpret_cast<char *>(B + OutputSectionOff + sizeof(*BuildId));
152 if (!Config->PDBAltPath.empty())
153 memcpy(P, Config->PDBAltPath.data(), Config->PDBAltPath.size());
154 P[Config->PDBAltPath.size()] = '\0';
157 mutable codeview::DebugInfo *BuildId = nullptr;
160 // The writer writes a SymbolTable result to a file.
163 Writer() : Buffer(errorHandler().OutputBuffer) {}
167 void createSections();
168 void createMiscChunks();
169 void createImportTables();
170 void appendImportThunks();
171 void locateImportTables(
172 std::map<std::pair<StringRef, uint32_t>, std::vector<Chunk *>> &Map);
173 void createExportTable();
174 void mergeSections();
175 void readRelocTargets();
176 void removeUnusedSections();
177 void assignAddresses();
178 void finalizeAddresses();
179 void removeEmptySections();
180 void createSymbolAndStringTable();
181 void openFile(StringRef OutputPath);
182 template <typename PEHeaderTy> void writeHeader();
183 void createSEHTable();
184 void createRuntimePseudoRelocs();
185 void insertCtorDtorSymbols();
186 void createGuardCFTables();
187 void markSymbolsForRVATable(ObjFile *File,
188 ArrayRef<SectionChunk *> SymIdxChunks,
189 SymbolRVASet &TableSymbols);
190 void maybeAddRVATable(SymbolRVASet TableSymbols, StringRef TableSym,
192 void setSectionPermissions();
193 void writeSections();
195 void sortExceptionTable();
196 void sortCRTSectionChunks(std::vector<Chunk *> &Chunks);
198 llvm::Optional<coff_symbol16> createSymbol(Defined *D);
199 size_t addEntryToStringTable(StringRef Str);
201 OutputSection *findSection(StringRef Name);
203 void addBaserelBlocks(std::vector<Baserel> &V);
205 uint32_t getSizeOfInitializedData();
206 std::map<StringRef, std::vector<DefinedImportData *>> binImports();
208 std::unique_ptr<FileOutputBuffer> &Buffer;
209 std::vector<OutputSection *> OutputSections;
210 std::vector<char> Strtab;
211 std::vector<llvm::object::coff_symbol16> OutputSymtab;
213 Chunk *ImportTableStart = nullptr;
214 uint64_t ImportTableSize = 0;
215 Chunk *IATStart = nullptr;
216 uint64_t IATSize = 0;
217 DelayLoadContents DelayIdata;
219 bool SetNoSEHCharacteristic = false;
221 DebugDirectoryChunk *DebugDirectory = nullptr;
222 std::vector<Chunk *> DebugRecords;
223 CVDebugRecordChunk *BuildId = nullptr;
224 ArrayRef<uint8_t> SectionTable;
227 uint32_t PointerToSymbolTable = 0;
228 uint64_t SizeOfImage;
229 uint64_t SizeOfHeaders;
231 OutputSection *TextSec;
232 OutputSection *RdataSec;
233 OutputSection *BuildidSec;
234 OutputSection *DataSec;
235 OutputSection *PdataSec;
236 OutputSection *IdataSec;
237 OutputSection *EdataSec;
238 OutputSection *DidatSec;
239 OutputSection *RsrcSec;
240 OutputSection *RelocSec;
241 OutputSection *CtorsSec;
242 OutputSection *DtorsSec;
244 // The first and last .pdata sections in the output file.
246 // We need to keep track of the location of .pdata in whichever section it
247 // gets merged into so that we can sort its contents and emit a correct data
248 // directory entry for the exception table. This is also the case for some
249 // other sections (such as .edata) but because the contents of those sections
250 // are entirely linker-generated we can keep track of their locations using
251 // the chunks that the linker creates. All .pdata chunks come from input
252 // files, so we need to keep track of them separately.
253 Chunk *FirstPdata = nullptr;
256 } // anonymous namespace
261 static Timer CodeLayoutTimer("Code Layout", Timer::root());
262 static Timer DiskCommitTimer("Commit Output File", Timer::root());
264 void writeResult() { Writer().run(); }
266 void OutputSection::addChunk(Chunk *C) {
268 C->setOutputSection(this);
271 void OutputSection::insertChunkAtStart(Chunk *C) {
272 Chunks.insert(Chunks.begin(), C);
273 C->setOutputSection(this);
276 void OutputSection::setPermissions(uint32_t C) {
277 Header.Characteristics &= ~PermMask;
278 Header.Characteristics |= C;
281 void OutputSection::merge(OutputSection *Other) {
282 for (Chunk *C : Other->Chunks)
283 C->setOutputSection(this);
284 Chunks.insert(Chunks.end(), Other->Chunks.begin(), Other->Chunks.end());
285 Other->Chunks.clear();
288 // Write the section header to a given buffer.
289 void OutputSection::writeHeaderTo(uint8_t *Buf) {
290 auto *Hdr = reinterpret_cast<coff_section *>(Buf);
292 if (StringTableOff) {
293 // If name is too long, write offset into the string table as a name.
294 sprintf(Hdr->Name, "/%d", StringTableOff);
296 assert(!Config->Debug || Name.size() <= COFF::NameSize ||
297 (Hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0);
298 strncpy(Hdr->Name, Name.data(),
299 std::min(Name.size(), (size_t)COFF::NameSize));
306 // Check whether the target address S is in range from a relocation
307 // of type RelType at address P.
308 static bool isInRange(uint16_t RelType, uint64_t S, uint64_t P, int Margin) {
309 if (Config->Machine == ARMNT) {
310 int64_t Diff = AbsoluteDifference(S, P + 4) + Margin;
312 case IMAGE_REL_ARM_BRANCH20T:
313 return isInt<21>(Diff);
314 case IMAGE_REL_ARM_BRANCH24T:
315 case IMAGE_REL_ARM_BLX23T:
316 return isInt<25>(Diff);
320 } else if (Config->Machine == ARM64) {
321 int64_t Diff = AbsoluteDifference(S, P) + Margin;
323 case IMAGE_REL_ARM64_BRANCH26:
324 return isInt<28>(Diff);
325 case IMAGE_REL_ARM64_BRANCH19:
326 return isInt<21>(Diff);
327 case IMAGE_REL_ARM64_BRANCH14:
328 return isInt<16>(Diff);
333 llvm_unreachable("Unexpected architecture");
337 // Return the last thunk for the given target if it is in range,
338 // or create a new one.
339 static std::pair<Defined *, bool>
340 getThunk(DenseMap<uint64_t, Defined *> &LastThunks, Defined *Target, uint64_t P,
341 uint16_t Type, int Margin) {
342 Defined *&LastThunk = LastThunks[Target->getRVA()];
343 if (LastThunk && isInRange(Type, LastThunk->getRVA(), P, Margin))
344 return {LastThunk, false};
346 switch (Config->Machine) {
348 C = make<RangeExtensionThunkARM>(Target);
351 C = make<RangeExtensionThunkARM64>(Target);
354 llvm_unreachable("Unexpected architecture");
356 Defined *D = make<DefinedSynthetic>("", C);
361 // This checks all relocations, and for any relocation which isn't in range
362 // it adds a thunk after the section chunk that contains the relocation.
363 // If the latest thunk for the specific target is in range, that is used
364 // instead of creating a new thunk. All range checks are done with the
365 // specified margin, to make sure that relocations that originally are in
366 // range, but only barely, also get thunks - in case other added thunks makes
367 // the target go out of range.
369 // After adding thunks, we verify that all relocations are in range (with
370 // no extra margin requirements). If this failed, we restart (throwing away
371 // the previously created thunks) and retry with a wider margin.
372 static bool createThunks(OutputSection *OS, int Margin) {
373 bool AddressesChanged = false;
374 DenseMap<uint64_t, Defined *> LastThunks;
375 size_t ThunksSize = 0;
376 // Recheck Chunks.size() each iteration, since we can insert more
378 for (size_t I = 0; I != OS->Chunks.size(); ++I) {
379 SectionChunk *SC = dyn_cast_or_null<SectionChunk>(OS->Chunks[I]);
382 size_t ThunkInsertionSpot = I + 1;
384 // Try to get a good enough estimate of where new thunks will be placed.
385 // Offset this by the size of the new thunks added so far, to make the
386 // estimate slightly better.
387 size_t ThunkInsertionRVA = SC->getRVA() + SC->getSize() + ThunksSize;
388 for (size_t J = 0, E = SC->Relocs.size(); J < E; ++J) {
389 const coff_relocation &Rel = SC->Relocs[J];
390 Symbol *&RelocTarget = SC->RelocTargets[J];
392 // The estimate of the source address P should be pretty accurate,
393 // but we don't know whether the target Symbol address should be
394 // offset by ThunkSize or not (or by some of ThunksSize but not all of
395 // it), giving us some uncertainty once we have added one thunk.
396 uint64_t P = SC->getRVA() + Rel.VirtualAddress + ThunksSize;
398 Defined *Sym = dyn_cast_or_null<Defined>(RelocTarget);
402 uint64_t S = Sym->getRVA();
404 if (isInRange(Rel.Type, S, P, Margin))
407 // If the target isn't in range, hook it up to an existing or new
411 std::tie(Thunk, WasNew) = getThunk(LastThunks, Sym, P, Rel.Type, Margin);
413 Chunk *ThunkChunk = Thunk->getChunk();
415 ThunkInsertionRVA); // Estimate of where it will be located.
416 ThunkChunk->setOutputSection(OS);
417 OS->Chunks.insert(OS->Chunks.begin() + ThunkInsertionSpot, ThunkChunk);
418 ThunkInsertionSpot++;
419 ThunksSize += ThunkChunk->getSize();
420 ThunkInsertionRVA += ThunkChunk->getSize();
421 AddressesChanged = true;
426 return AddressesChanged;
429 // Verify that all relocations are in range, with no extra margin requirements.
430 static bool verifyRanges(const std::vector<Chunk *> Chunks) {
431 for (Chunk *C : Chunks) {
432 SectionChunk *SC = dyn_cast_or_null<SectionChunk>(C);
436 for (size_t J = 0, E = SC->Relocs.size(); J < E; ++J) {
437 const coff_relocation &Rel = SC->Relocs[J];
438 Symbol *RelocTarget = SC->RelocTargets[J];
440 Defined *Sym = dyn_cast_or_null<Defined>(RelocTarget);
444 uint64_t P = SC->getRVA() + Rel.VirtualAddress;
445 uint64_t S = Sym->getRVA();
447 if (!isInRange(Rel.Type, S, P, 0))
454 // Assign addresses and add thunks if necessary.
455 void Writer::finalizeAddresses() {
457 if (Config->Machine != ARMNT && Config->Machine != ARM64)
460 size_t OrigNumChunks = 0;
461 for (OutputSection *Sec : OutputSections) {
462 Sec->OrigChunks = Sec->Chunks;
463 OrigNumChunks += Sec->Chunks.size();
467 int Margin = 1024 * 100;
469 // First check whether we need thunks at all, or if the previous pass of
470 // adding them turned out ok.
471 bool RangesOk = true;
472 size_t NumChunks = 0;
473 for (OutputSection *Sec : OutputSections) {
474 if (!verifyRanges(Sec->Chunks)) {
478 NumChunks += Sec->Chunks.size();
482 log("Added " + Twine(NumChunks - OrigNumChunks) + " thunks with " +
483 "margin " + Twine(Margin) + " in " + Twine(Pass) + " passes");
488 fatal("adding thunks hasn't converged after " + Twine(Pass) + " passes");
491 // If the previous pass didn't work out, reset everything back to the
492 // original conditions before retrying with a wider margin. This should
493 // ideally never happen under real circumstances.
494 for (OutputSection *Sec : OutputSections) {
495 Sec->Chunks = Sec->OrigChunks;
496 for (Chunk *C : Sec->Chunks)
497 C->resetRelocTargets();
502 // Try adding thunks everywhere where it is needed, with a margin
503 // to avoid things going out of range due to the added thunks.
504 bool AddressesChanged = false;
505 for (OutputSection *Sec : OutputSections)
506 AddressesChanged |= createThunks(Sec, Margin);
507 // If the verification above thought we needed thunks, we should have
509 assert(AddressesChanged);
511 // Recalculate the layout for the whole image (and verify the ranges at
512 // the start of the next round).
519 // The main function of the writer.
521 ScopedTimer T1(CodeLayoutTimer);
523 createImportTables();
526 appendImportThunks();
530 removeUnusedSections();
532 removeEmptySections();
533 setSectionPermissions();
534 createSymbolAndStringTable();
536 if (FileSize > UINT32_MAX)
537 fatal("image size (" + Twine(FileSize) + ") " +
538 "exceeds maximum allowable size (" + Twine(UINT32_MAX) + ")");
540 openFile(Config->OutputFile);
541 if (Config->is64()) {
542 writeHeader<pe32plus_header>();
544 writeHeader<pe32_header>();
547 sortExceptionTable();
551 if (!Config->PDBPath.empty() && Config->Debug) {
553 createPDB(Symtab, OutputSections, SectionTable, BuildId->BuildId);
557 writeMapFile(OutputSections);
559 ScopedTimer T2(DiskCommitTimer);
560 if (auto E = Buffer->commit())
561 fatal("failed to write the output file: " + toString(std::move(E)));
564 static StringRef getOutputSectionName(StringRef Name) {
565 StringRef S = Name.split('$').first;
567 // Treat a later period as a separator for MinGW, for sections like
569 return S.substr(0, S.find('.', 1));
573 static void sortBySectionOrder(std::vector<Chunk *> &Chunks) {
574 auto GetPriority = [](const Chunk *C) {
575 if (auto *Sec = dyn_cast<SectionChunk>(C))
577 return Config->Order.lookup(Sec->Sym->getName());
581 std::stable_sort(Chunks.begin(), Chunks.end(),
582 [=](const Chunk *A, const Chunk *B) {
583 return GetPriority(A) < GetPriority(B);
587 // Sort concrete section chunks from GNU import libraries.
589 // GNU binutils doesn't use short import files, but instead produces import
590 // libraries that consist of object files, with section chunks for the .idata$*
591 // sections. These are linked just as regular static libraries. Each import
592 // library consists of one header object, one object file for every imported
593 // symbol, and one trailer object. In order for the .idata tables/lists to
594 // be formed correctly, the section chunks within each .idata$* section need
595 // to be grouped by library, and sorted alphabetically within each library
596 // (which makes sure the header comes first and the trailer last).
597 static bool fixGnuImportChunks(
598 std::map<std::pair<StringRef, uint32_t>, std::vector<Chunk *>> &Map) {
599 uint32_t RDATA = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
601 // Make sure all .idata$* section chunks are mapped as RDATA in order to
602 // be sorted into the same sections as our own synthesized .idata chunks.
603 for (auto &Pair : Map) {
604 StringRef SectionName = Pair.first.first;
605 uint32_t OutChars = Pair.first.second;
606 if (!SectionName.startswith(".idata"))
608 if (OutChars == RDATA)
610 std::vector<Chunk *> &SrcVect = Pair.second;
611 std::vector<Chunk *> &DestVect = Map[{SectionName, RDATA}];
612 DestVect.insert(DestVect.end(), SrcVect.begin(), SrcVect.end());
616 bool HasIdata = false;
617 // Sort all .idata$* chunks, grouping chunks from the same library,
618 // with alphabetical ordering of the object fils within a library.
619 for (auto &Pair : Map) {
620 StringRef SectionName = Pair.first.first;
621 if (!SectionName.startswith(".idata"))
624 std::vector<Chunk *> &Chunks = Pair.second;
627 std::stable_sort(Chunks.begin(), Chunks.end(), [&](Chunk *S, Chunk *T) {
628 SectionChunk *SC1 = dyn_cast_or_null<SectionChunk>(S);
629 SectionChunk *SC2 = dyn_cast_or_null<SectionChunk>(T);
631 // if SC1, order them ascending. If SC2 or both null,
632 // S is not less than T.
633 return SC1 != nullptr;
635 // Make a string with "libraryname/objectfile" for sorting, achieving
636 // both grouping by library and sorting of objects within a library,
639 (SC1->File->ParentName + "/" + SC1->File->getName()).str();
641 (SC2->File->ParentName + "/" + SC2->File->getName()).str();
648 // Add generated idata chunks, for imported symbols and DLLs, and a
649 // terminator in .idata$2.
650 static void addSyntheticIdata(
651 IdataContents &Idata,
652 std::map<std::pair<StringRef, uint32_t>, std::vector<Chunk *>> &Map) {
653 uint32_t RDATA = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
656 // Add the .idata content in the right section groups, to allow
657 // chunks from other linked in object files to be grouped together.
658 // See Microsoft PE/COFF spec 5.4 for details.
659 auto Add = [&](StringRef N, std::vector<Chunk *> &V) {
660 std::vector<Chunk *> &DestVect = Map[{N, RDATA}];
661 DestVect.insert(DestVect.end(), V.begin(), V.end());
664 // The loader assumes a specific order of data.
665 // Add each type in the correct order.
666 Add(".idata$2", Idata.Dirs);
667 Add(".idata$4", Idata.Lookups);
668 Add(".idata$5", Idata.Addresses);
669 Add(".idata$6", Idata.Hints);
670 Add(".idata$7", Idata.DLLNames);
673 // Locate the first Chunk and size of the import directory list and the
675 void Writer::locateImportTables(
676 std::map<std::pair<StringRef, uint32_t>, std::vector<Chunk *>> &Map) {
677 uint32_t RDATA = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
678 std::vector<Chunk *> &ImportTables = Map[{".idata$2", RDATA}];
679 if (!ImportTables.empty())
680 ImportTableStart = ImportTables.front();
681 for (Chunk *C : ImportTables)
682 ImportTableSize += C->getSize();
684 std::vector<Chunk *> &IAT = Map[{".idata$5", RDATA}];
686 IATStart = IAT.front();
688 IATSize += C->getSize();
691 // Create output section objects and add them to OutputSections.
692 void Writer::createSections() {
693 // First, create the builtin sections.
694 const uint32_t DATA = IMAGE_SCN_CNT_INITIALIZED_DATA;
695 const uint32_t BSS = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
696 const uint32_t CODE = IMAGE_SCN_CNT_CODE;
697 const uint32_t DISCARDABLE = IMAGE_SCN_MEM_DISCARDABLE;
698 const uint32_t R = IMAGE_SCN_MEM_READ;
699 const uint32_t W = IMAGE_SCN_MEM_WRITE;
700 const uint32_t X = IMAGE_SCN_MEM_EXECUTE;
702 SmallDenseMap<std::pair<StringRef, uint32_t>, OutputSection *> Sections;
703 auto CreateSection = [&](StringRef Name, uint32_t OutChars) {
704 OutputSection *&Sec = Sections[{Name, OutChars}];
706 Sec = make<OutputSection>(Name, OutChars);
707 OutputSections.push_back(Sec);
712 // Try to match the section order used by link.exe.
713 TextSec = CreateSection(".text", CODE | R | X);
714 CreateSection(".bss", BSS | R | W);
715 RdataSec = CreateSection(".rdata", DATA | R);
716 BuildidSec = CreateSection(".buildid", DATA | R);
717 DataSec = CreateSection(".data", DATA | R | W);
718 PdataSec = CreateSection(".pdata", DATA | R);
719 IdataSec = CreateSection(".idata", DATA | R);
720 EdataSec = CreateSection(".edata", DATA | R);
721 DidatSec = CreateSection(".didat", DATA | R);
722 RsrcSec = CreateSection(".rsrc", DATA | R);
723 RelocSec = CreateSection(".reloc", DATA | DISCARDABLE | R);
724 CtorsSec = CreateSection(".ctors", DATA | R | W);
725 DtorsSec = CreateSection(".dtors", DATA | R | W);
727 // Then bin chunks by name and output characteristics.
728 std::map<std::pair<StringRef, uint32_t>, std::vector<Chunk *>> Map;
729 for (Chunk *C : Symtab->getChunks()) {
730 auto *SC = dyn_cast<SectionChunk>(C);
731 if (SC && !SC->Live) {
733 SC->printDiscardedMessage();
736 Map[{C->getSectionName(), C->getOutputCharacteristics()}].push_back(C);
739 // Even in non MinGW cases, we might need to link against GNU import
741 bool HasIdata = fixGnuImportChunks(Map);
746 addSyntheticIdata(Idata, Map);
748 // Process an /order option.
749 if (!Config->Order.empty())
750 for (auto &Pair : Map)
751 sortBySectionOrder(Pair.second);
754 locateImportTables(Map);
756 // Then create an OutputSection for each section.
757 // '$' and all following characters in input section names are
758 // discarded when determining output section. So, .text$foo
759 // contributes to .text, for example. See PE/COFF spec 3.2.
760 for (auto &Pair : Map) {
761 StringRef Name = getOutputSectionName(Pair.first.first);
762 uint32_t OutChars = Pair.first.second;
764 if (Name == ".CRT") {
765 // In link.exe, there is a special case for the I386 target where .CRT
766 // sections are treated as if they have output characteristics DATA | R if
767 // their characteristics are DATA | R | W. This implements the same
768 // special case for all architectures.
771 log("Processing section " + Pair.first.first + " -> " + Name);
773 sortCRTSectionChunks(Pair.second);
776 OutputSection *Sec = CreateSection(Name, OutChars);
777 std::vector<Chunk *> &Chunks = Pair.second;
778 for (Chunk *C : Chunks)
782 // Finally, move some output sections to the end.
783 auto SectionOrder = [&](OutputSection *S) {
784 // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because
785 // the loader cannot handle holes. Stripping can remove other discardable ones
786 // than .reloc, which is first of them (created early).
787 if (S->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
789 // .rsrc should come at the end of the non-discardable sections because its
790 // size may change by the Win32 UpdateResources() function, causing
791 // subsequent sections to move (see https://crbug.com/827082).
796 std::stable_sort(OutputSections.begin(), OutputSections.end(),
797 [&](OutputSection *S, OutputSection *T) {
798 return SectionOrder(S) < SectionOrder(T);
802 void Writer::createMiscChunks() {
803 for (auto &P : MergeChunk::Instances)
804 RdataSec->addChunk(P.second);
806 // Create thunks for locally-dllimported symbols.
807 if (!Symtab->LocalImportChunks.empty()) {
808 for (Chunk *C : Symtab->LocalImportChunks)
809 RdataSec->addChunk(C);
812 // Create Debug Information Chunks
813 OutputSection *DebugInfoSec = Config->MinGW ? BuildidSec : RdataSec;
814 if (Config->Debug || Config->Repro) {
815 DebugDirectory = make<DebugDirectoryChunk>(DebugRecords, Config->Repro);
816 DebugInfoSec->addChunk(DebugDirectory);
820 // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified. We
821 // output a PDB no matter what, and this chunk provides the only means of
822 // allowing a debugger to match a PDB and an executable. So we need it even
823 // if we're ultimately not going to write CodeView data to the PDB.
824 BuildId = make<CVDebugRecordChunk>();
825 DebugRecords.push_back(BuildId);
827 for (Chunk *C : DebugRecords)
828 DebugInfoSec->addChunk(C);
831 // Create SEH table. x86-only.
832 if (Config->Machine == I386)
835 // Create /guard:cf tables if requested.
836 if (Config->GuardCF != GuardCFLevel::Off)
837 createGuardCFTables();
840 createRuntimePseudoRelocs();
842 insertCtorDtorSymbols();
846 // Create .idata section for the DLL-imported symbol table.
847 // The format of this section is inherently Windows-specific.
848 // IdataContents class abstracted away the details for us,
849 // so we just let it create chunks and add them to the section.
850 void Writer::createImportTables() {
851 // Initialize DLLOrder so that import entries are ordered in
852 // the same order as in the command line. (That affects DLL
853 // initialization order, and this ordering is MSVC-compatible.)
854 for (ImportFile *File : ImportFile::Instances) {
858 std::string DLL = StringRef(File->DLLName).lower();
859 if (Config->DLLOrder.count(DLL) == 0)
860 Config->DLLOrder[DLL] = Config->DLLOrder.size();
862 if (File->ImpSym && !isa<DefinedImportData>(File->ImpSym))
863 fatal(toString(*File->ImpSym) + " was replaced");
864 DefinedImportData *ImpSym = cast_or_null<DefinedImportData>(File->ImpSym);
865 if (Config->DelayLoads.count(StringRef(File->DLLName).lower())) {
867 fatal("cannot delay-load " + toString(File) +
868 " due to import of data: " + toString(*ImpSym));
869 DelayIdata.add(ImpSym);
876 void Writer::appendImportThunks() {
877 if (ImportFile::Instances.empty())
880 for (ImportFile *File : ImportFile::Instances) {
887 if (!isa<DefinedImportThunk>(File->ThunkSym))
888 fatal(toString(*File->ThunkSym) + " was replaced");
889 DefinedImportThunk *Thunk = cast<DefinedImportThunk>(File->ThunkSym);
891 TextSec->addChunk(Thunk->getChunk());
894 if (!DelayIdata.empty()) {
895 Defined *Helper = cast<Defined>(Config->DelayLoadHelper);
896 DelayIdata.create(Helper);
897 for (Chunk *C : DelayIdata.getChunks())
898 DidatSec->addChunk(C);
899 for (Chunk *C : DelayIdata.getDataChunks())
900 DataSec->addChunk(C);
901 for (Chunk *C : DelayIdata.getCodeChunks())
902 TextSec->addChunk(C);
906 void Writer::createExportTable() {
907 if (Config->Exports.empty())
909 for (Chunk *C : Edata.Chunks)
910 EdataSec->addChunk(C);
913 void Writer::removeUnusedSections() {
914 // Remove sections that we can be sure won't get content, to avoid
915 // allocating space for their section headers.
916 auto IsUnused = [this](OutputSection *S) {
918 return false; // This section is populated later.
919 // MergeChunks have zero size at this point, as their size is finalized
920 // later. Only remove sections that have no Chunks at all.
921 return S->Chunks.empty();
923 OutputSections.erase(
924 std::remove_if(OutputSections.begin(), OutputSections.end(), IsUnused),
925 OutputSections.end());
928 // The Windows loader doesn't seem to like empty sections,
929 // so we remove them if any.
930 void Writer::removeEmptySections() {
931 auto IsEmpty = [](OutputSection *S) { return S->getVirtualSize() == 0; };
932 OutputSections.erase(
933 std::remove_if(OutputSections.begin(), OutputSections.end(), IsEmpty),
934 OutputSections.end());
936 for (OutputSection *Sec : OutputSections)
937 Sec->SectionIndex = Idx++;
940 size_t Writer::addEntryToStringTable(StringRef Str) {
941 assert(Str.size() > COFF::NameSize);
942 size_t OffsetOfEntry = Strtab.size() + 4; // +4 for the size field
943 Strtab.insert(Strtab.end(), Str.begin(), Str.end());
944 Strtab.push_back('\0');
945 return OffsetOfEntry;
948 Optional<coff_symbol16> Writer::createSymbol(Defined *Def) {
950 switch (Def->kind()) {
951 case Symbol::DefinedAbsoluteKind:
952 Sym.Value = Def->getRVA();
953 Sym.SectionNumber = IMAGE_SYM_ABSOLUTE;
955 case Symbol::DefinedSyntheticKind:
956 // Relative symbols are unrepresentable in a COFF symbol table.
959 // Don't write symbols that won't be written to the output to the symbol
961 Chunk *C = Def->getChunk();
964 OutputSection *OS = C->getOutputSection();
968 Sym.Value = Def->getRVA() - OS->getRVA();
969 Sym.SectionNumber = OS->SectionIndex;
974 StringRef Name = Def->getName();
975 if (Name.size() > COFF::NameSize) {
976 Sym.Name.Offset.Zeroes = 0;
977 Sym.Name.Offset.Offset = addEntryToStringTable(Name);
979 memset(Sym.Name.ShortName, 0, COFF::NameSize);
980 memcpy(Sym.Name.ShortName, Name.data(), Name.size());
983 if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
984 COFFSymbolRef Ref = D->getCOFFSymbol();
985 Sym.Type = Ref.getType();
986 Sym.StorageClass = Ref.getStorageClass();
988 Sym.Type = IMAGE_SYM_TYPE_NULL;
989 Sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
991 Sym.NumberOfAuxSymbols = 0;
995 void Writer::createSymbolAndStringTable() {
996 // PE/COFF images are limited to 8 byte section names. Longer names can be
997 // supported by writing a non-standard string table, but this string table is
998 // not mapped at runtime and the long names will therefore be inaccessible.
999 // link.exe always truncates section names to 8 bytes, whereas binutils always
1000 // preserves long section names via the string table. LLD adopts a hybrid
1001 // solution where discardable sections have long names preserved and
1002 // non-discardable sections have their names truncated, to ensure that any
1003 // section which is mapped at runtime also has its name mapped at runtime.
1004 for (OutputSection *Sec : OutputSections) {
1005 if (Sec->Name.size() <= COFF::NameSize)
1007 if ((Sec->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0)
1009 Sec->setStringTableOff(addEntryToStringTable(Sec->Name));
1012 if (Config->DebugDwarf || Config->DebugSymtab) {
1013 for (ObjFile *File : ObjFile::Instances) {
1014 for (Symbol *B : File->getSymbols()) {
1015 auto *D = dyn_cast_or_null<Defined>(B);
1016 if (!D || D->WrittenToSymtab)
1018 D->WrittenToSymtab = true;
1020 if (Optional<coff_symbol16> Sym = createSymbol(D))
1021 OutputSymtab.push_back(*Sym);
1026 if (OutputSymtab.empty() && Strtab.empty())
1029 // We position the symbol table to be adjacent to the end of the last section.
1030 uint64_t FileOff = FileSize;
1031 PointerToSymbolTable = FileOff;
1032 FileOff += OutputSymtab.size() * sizeof(coff_symbol16);
1033 FileOff += 4 + Strtab.size();
1034 FileSize = alignTo(FileOff, SectorSize);
1037 void Writer::mergeSections() {
1038 if (!PdataSec->Chunks.empty()) {
1039 FirstPdata = PdataSec->Chunks.front();
1040 LastPdata = PdataSec->Chunks.back();
1043 for (auto &P : Config->Merge) {
1044 StringRef ToName = P.second;
1045 if (P.first == ToName)
1049 if (!Names.insert(ToName).second)
1050 fatal("/merge: cycle found for section '" + P.first + "'");
1051 auto I = Config->Merge.find(ToName);
1052 if (I == Config->Merge.end())
1056 OutputSection *From = findSection(P.first);
1057 OutputSection *To = findSection(ToName);
1061 From->Name = ToName;
1068 // Visits all sections to initialize their relocation targets.
1069 void Writer::readRelocTargets() {
1070 for (OutputSection *Sec : OutputSections)
1071 for_each(parallel::par, Sec->Chunks.begin(), Sec->Chunks.end(),
1072 [&](Chunk *C) { C->readRelocTargets(); });
1075 // Visits all sections to assign incremental, non-overlapping RVAs and
1077 void Writer::assignAddresses() {
1078 SizeOfHeaders = DOSStubSize + sizeof(PEMagic) + sizeof(coff_file_header) +
1079 sizeof(data_directory) * NumberOfDataDirectory +
1080 sizeof(coff_section) * OutputSections.size();
1082 Config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header);
1083 SizeOfHeaders = alignTo(SizeOfHeaders, SectorSize);
1084 uint64_t RVA = PageSize; // The first page is kept unmapped.
1085 FileSize = SizeOfHeaders;
1087 for (OutputSection *Sec : OutputSections) {
1088 if (Sec == RelocSec)
1090 uint64_t RawSize = 0, VirtualSize = 0;
1091 Sec->Header.VirtualAddress = RVA;
1092 for (Chunk *C : Sec->Chunks) {
1093 VirtualSize = alignTo(VirtualSize, C->Alignment);
1094 C->setRVA(RVA + VirtualSize);
1095 C->OutputSectionOff = VirtualSize;
1096 C->finalizeContents();
1097 VirtualSize += C->getSize();
1099 RawSize = alignTo(VirtualSize, SectorSize);
1101 if (VirtualSize > UINT32_MAX)
1102 error("section larger than 4 GiB: " + Sec->Name);
1103 Sec->Header.VirtualSize = VirtualSize;
1104 Sec->Header.SizeOfRawData = RawSize;
1106 Sec->Header.PointerToRawData = FileSize;
1107 RVA += alignTo(VirtualSize, PageSize);
1108 FileSize += alignTo(RawSize, SectorSize);
1110 SizeOfImage = alignTo(RVA, PageSize);
1113 template <typename PEHeaderTy> void Writer::writeHeader() {
1114 // Write DOS header. For backwards compatibility, the first part of a PE/COFF
1115 // executable consists of an MS-DOS MZ executable. If the executable is run
1116 // under DOS, that program gets run (usually to just print an error message).
1117 // When run under Windows, the loader looks at AddressOfNewExeHeader and uses
1118 // the PE header instead.
1119 uint8_t *Buf = Buffer->getBufferStart();
1120 auto *DOS = reinterpret_cast<dos_header *>(Buf);
1121 Buf += sizeof(dos_header);
1122 DOS->Magic[0] = 'M';
1123 DOS->Magic[1] = 'Z';
1124 DOS->UsedBytesInTheLastPage = DOSStubSize % 512;
1125 DOS->FileSizeInPages = divideCeil(DOSStubSize, 512);
1126 DOS->HeaderSizeInParagraphs = sizeof(dos_header) / 16;
1128 DOS->AddressOfRelocationTable = sizeof(dos_header);
1129 DOS->AddressOfNewExeHeader = DOSStubSize;
1131 // Write DOS program.
1132 memcpy(Buf, DOSProgram, sizeof(DOSProgram));
1133 Buf += sizeof(DOSProgram);
1136 memcpy(Buf, PEMagic, sizeof(PEMagic));
1137 Buf += sizeof(PEMagic);
1139 // Write COFF header
1140 auto *COFF = reinterpret_cast<coff_file_header *>(Buf);
1141 Buf += sizeof(*COFF);
1142 COFF->Machine = Config->Machine;
1143 COFF->NumberOfSections = OutputSections.size();
1144 COFF->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
1145 if (Config->LargeAddressAware)
1146 COFF->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
1147 if (!Config->is64())
1148 COFF->Characteristics |= IMAGE_FILE_32BIT_MACHINE;
1150 COFF->Characteristics |= IMAGE_FILE_DLL;
1151 if (!Config->Relocatable)
1152 COFF->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
1153 COFF->SizeOfOptionalHeader =
1154 sizeof(PEHeaderTy) + sizeof(data_directory) * NumberOfDataDirectory;
1157 auto *PE = reinterpret_cast<PEHeaderTy *>(Buf);
1159 PE->Magic = Config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32;
1161 // If {Major,Minor}LinkerVersion is left at 0.0, then for some
1162 // reason signing the resulting PE file with Authenticode produces a
1163 // signature that fails to validate on Windows 7 (but is OK on 10).
1164 // Set it to 14.0, which is what VS2015 outputs, and which avoids
1166 PE->MajorLinkerVersion = 14;
1167 PE->MinorLinkerVersion = 0;
1169 PE->ImageBase = Config->ImageBase;
1170 PE->SectionAlignment = PageSize;
1171 PE->FileAlignment = SectorSize;
1172 PE->MajorImageVersion = Config->MajorImageVersion;
1173 PE->MinorImageVersion = Config->MinorImageVersion;
1174 PE->MajorOperatingSystemVersion = Config->MajorOSVersion;
1175 PE->MinorOperatingSystemVersion = Config->MinorOSVersion;
1176 PE->MajorSubsystemVersion = Config->MajorOSVersion;
1177 PE->MinorSubsystemVersion = Config->MinorOSVersion;
1178 PE->Subsystem = Config->Subsystem;
1179 PE->SizeOfImage = SizeOfImage;
1180 PE->SizeOfHeaders = SizeOfHeaders;
1181 if (!Config->NoEntry) {
1182 Defined *Entry = cast<Defined>(Config->Entry);
1183 PE->AddressOfEntryPoint = Entry->getRVA();
1184 // Pointer to thumb code must have the LSB set, so adjust it.
1185 if (Config->Machine == ARMNT)
1186 PE->AddressOfEntryPoint |= 1;
1188 PE->SizeOfStackReserve = Config->StackReserve;
1189 PE->SizeOfStackCommit = Config->StackCommit;
1190 PE->SizeOfHeapReserve = Config->HeapReserve;
1191 PE->SizeOfHeapCommit = Config->HeapCommit;
1192 if (Config->AppContainer)
1193 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER;
1194 if (Config->DynamicBase)
1195 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE;
1196 if (Config->HighEntropyVA)
1197 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA;
1198 if (!Config->AllowBind)
1199 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND;
1200 if (Config->NxCompat)
1201 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT;
1202 if (!Config->AllowIsolation)
1203 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION;
1204 if (Config->GuardCF != GuardCFLevel::Off)
1205 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF;
1206 if (Config->IntegrityCheck)
1207 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY;
1208 if (SetNoSEHCharacteristic)
1209 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH;
1210 if (Config->TerminalServerAware)
1211 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE;
1212 PE->NumberOfRvaAndSize = NumberOfDataDirectory;
1213 if (TextSec->getVirtualSize()) {
1214 PE->BaseOfCode = TextSec->getRVA();
1215 PE->SizeOfCode = TextSec->getRawSize();
1217 PE->SizeOfInitializedData = getSizeOfInitializedData();
1219 // Write data directory
1220 auto *Dir = reinterpret_cast<data_directory *>(Buf);
1221 Buf += sizeof(*Dir) * NumberOfDataDirectory;
1222 if (!Config->Exports.empty()) {
1223 Dir[EXPORT_TABLE].RelativeVirtualAddress = Edata.getRVA();
1224 Dir[EXPORT_TABLE].Size = Edata.getSize();
1226 if (ImportTableStart) {
1227 Dir[IMPORT_TABLE].RelativeVirtualAddress = ImportTableStart->getRVA();
1228 Dir[IMPORT_TABLE].Size = ImportTableSize;
1231 Dir[IAT].RelativeVirtualAddress = IATStart->getRVA();
1232 Dir[IAT].Size = IATSize;
1234 if (RsrcSec->getVirtualSize()) {
1235 Dir[RESOURCE_TABLE].RelativeVirtualAddress = RsrcSec->getRVA();
1236 Dir[RESOURCE_TABLE].Size = RsrcSec->getVirtualSize();
1239 Dir[EXCEPTION_TABLE].RelativeVirtualAddress = FirstPdata->getRVA();
1240 Dir[EXCEPTION_TABLE].Size =
1241 LastPdata->getRVA() + LastPdata->getSize() - FirstPdata->getRVA();
1243 if (RelocSec->getVirtualSize()) {
1244 Dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = RelocSec->getRVA();
1245 Dir[BASE_RELOCATION_TABLE].Size = RelocSec->getVirtualSize();
1247 if (Symbol *Sym = Symtab->findUnderscore("_tls_used")) {
1248 if (Defined *B = dyn_cast<Defined>(Sym)) {
1249 Dir[TLS_TABLE].RelativeVirtualAddress = B->getRVA();
1250 Dir[TLS_TABLE].Size = Config->is64()
1251 ? sizeof(object::coff_tls_directory64)
1252 : sizeof(object::coff_tls_directory32);
1255 if (DebugDirectory) {
1256 Dir[DEBUG_DIRECTORY].RelativeVirtualAddress = DebugDirectory->getRVA();
1257 Dir[DEBUG_DIRECTORY].Size = DebugDirectory->getSize();
1259 if (Symbol *Sym = Symtab->findUnderscore("_load_config_used")) {
1260 if (auto *B = dyn_cast<DefinedRegular>(Sym)) {
1261 SectionChunk *SC = B->getChunk();
1262 assert(B->getRVA() >= SC->getRVA());
1263 uint64_t OffsetInChunk = B->getRVA() - SC->getRVA();
1264 if (!SC->hasData() || OffsetInChunk + 4 > SC->getSize())
1265 fatal("_load_config_used is malformed");
1267 ArrayRef<uint8_t> SecContents = SC->getContents();
1268 uint32_t LoadConfigSize =
1269 *reinterpret_cast<const ulittle32_t *>(&SecContents[OffsetInChunk]);
1270 if (OffsetInChunk + LoadConfigSize > SC->getSize())
1271 fatal("_load_config_used is too large");
1272 Dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = B->getRVA();
1273 Dir[LOAD_CONFIG_TABLE].Size = LoadConfigSize;
1276 if (!DelayIdata.empty()) {
1277 Dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress =
1278 DelayIdata.getDirRVA();
1279 Dir[DELAY_IMPORT_DESCRIPTOR].Size = DelayIdata.getDirSize();
1282 // Write section table
1283 for (OutputSection *Sec : OutputSections) {
1284 Sec->writeHeaderTo(Buf);
1285 Buf += sizeof(coff_section);
1287 SectionTable = ArrayRef<uint8_t>(
1288 Buf - OutputSections.size() * sizeof(coff_section), Buf);
1290 if (OutputSymtab.empty() && Strtab.empty())
1293 COFF->PointerToSymbolTable = PointerToSymbolTable;
1294 uint32_t NumberOfSymbols = OutputSymtab.size();
1295 COFF->NumberOfSymbols = NumberOfSymbols;
1296 auto *SymbolTable = reinterpret_cast<coff_symbol16 *>(
1297 Buffer->getBufferStart() + COFF->PointerToSymbolTable);
1298 for (size_t I = 0; I != NumberOfSymbols; ++I)
1299 SymbolTable[I] = OutputSymtab[I];
1300 // Create the string table, it follows immediately after the symbol table.
1301 // The first 4 bytes is length including itself.
1302 Buf = reinterpret_cast<uint8_t *>(&SymbolTable[NumberOfSymbols]);
1303 write32le(Buf, Strtab.size() + 4);
1304 if (!Strtab.empty())
1305 memcpy(Buf + 4, Strtab.data(), Strtab.size());
1308 void Writer::openFile(StringRef Path) {
1310 FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable),
1311 "failed to open " + Path);
1314 void Writer::createSEHTable() {
1315 // Set the no SEH characteristic on x86 binaries unless we find exception
1317 SetNoSEHCharacteristic = true;
1319 SymbolRVASet Handlers;
1320 for (ObjFile *File : ObjFile::Instances) {
1321 // FIXME: We should error here instead of earlier unless /safeseh:no was
1323 if (!File->hasSafeSEH())
1326 markSymbolsForRVATable(File, File->getSXDataChunks(), Handlers);
1329 // Remove the "no SEH" characteristic if all object files were built with
1330 // safeseh, we found some exception handlers, and there is a load config in
1332 SetNoSEHCharacteristic =
1333 Handlers.empty() || !Symtab->findUnderscore("_load_config_used");
1335 maybeAddRVATable(std::move(Handlers), "__safe_se_handler_table",
1336 "__safe_se_handler_count");
1339 // Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set
1340 // cannot contain duplicates. Therefore, the set is uniqued by Chunk and the
1341 // symbol's offset into that Chunk.
1342 static void addSymbolToRVASet(SymbolRVASet &RVASet, Defined *S) {
1343 Chunk *C = S->getChunk();
1344 if (auto *SC = dyn_cast<SectionChunk>(C))
1345 C = SC->Repl; // Look through ICF replacement.
1346 uint32_t Off = S->getRVA() - (C ? C->getRVA() : 0);
1347 RVASet.insert({C, Off});
1350 // Given a symbol, add it to the GFIDs table if it is a live, defined, function
1351 // symbol in an executable section.
1352 static void maybeAddAddressTakenFunction(SymbolRVASet &AddressTakenSyms,
1354 auto *D = dyn_cast_or_null<DefinedCOFF>(S);
1356 // Ignore undefined symbols and references to non-functions (e.g. globals and
1359 D->getCOFFSymbol().getComplexType() != COFF::IMAGE_SYM_DTYPE_FUNCTION)
1362 // Mark the symbol as address taken if it's in an executable section.
1363 Chunk *RefChunk = D->getChunk();
1364 OutputSection *OS = RefChunk ? RefChunk->getOutputSection() : nullptr;
1365 if (OS && OS->Header.Characteristics & IMAGE_SCN_MEM_EXECUTE)
1366 addSymbolToRVASet(AddressTakenSyms, D);
1369 // Visit all relocations from all section contributions of this object file and
1370 // mark the relocation target as address-taken.
1371 static void markSymbolsWithRelocations(ObjFile *File,
1372 SymbolRVASet &UsedSymbols) {
1373 for (Chunk *C : File->getChunks()) {
1374 // We only care about live section chunks. Common chunks and other chunks
1375 // don't generally contain relocations.
1376 SectionChunk *SC = dyn_cast<SectionChunk>(C);
1377 if (!SC || !SC->Live)
1380 for (const coff_relocation &Reloc : SC->Relocs) {
1381 if (Config->Machine == I386 && Reloc.Type == COFF::IMAGE_REL_I386_REL32)
1382 // Ignore relative relocations on x86. On x86_64 they can't be ignored
1383 // since they're also used to compute absolute addresses.
1386 Symbol *Ref = SC->File->getSymbol(Reloc.SymbolTableIndex);
1387 maybeAddAddressTakenFunction(UsedSymbols, Ref);
1392 // Create the guard function id table. This is a table of RVAs of all
1393 // address-taken functions. It is sorted and uniqued, just like the safe SEH
1395 void Writer::createGuardCFTables() {
1396 SymbolRVASet AddressTakenSyms;
1397 SymbolRVASet LongJmpTargets;
1398 for (ObjFile *File : ObjFile::Instances) {
1399 // If the object was compiled with /guard:cf, the address taken symbols
1400 // are in .gfids$y sections, and the longjmp targets are in .gljmp$y
1401 // sections. If the object was not compiled with /guard:cf, we assume there
1402 // were no setjmp targets, and that all code symbols with relocations are
1403 // possibly address-taken.
1404 if (File->hasGuardCF()) {
1405 markSymbolsForRVATable(File, File->getGuardFidChunks(), AddressTakenSyms);
1406 markSymbolsForRVATable(File, File->getGuardLJmpChunks(), LongJmpTargets);
1408 markSymbolsWithRelocations(File, AddressTakenSyms);
1412 // Mark the image entry as address-taken.
1414 maybeAddAddressTakenFunction(AddressTakenSyms, Config->Entry);
1416 // Mark exported symbols in executable sections as address-taken.
1417 for (Export &E : Config->Exports)
1418 maybeAddAddressTakenFunction(AddressTakenSyms, E.Sym);
1420 // Ensure sections referenced in the gfid table are 16-byte aligned.
1421 for (const ChunkAndOffset &C : AddressTakenSyms)
1422 if (C.InputChunk->Alignment < 16)
1423 C.InputChunk->Alignment = 16;
1425 maybeAddRVATable(std::move(AddressTakenSyms), "__guard_fids_table",
1426 "__guard_fids_count");
1428 // Add the longjmp target table unless the user told us not to.
1429 if (Config->GuardCF == GuardCFLevel::Full)
1430 maybeAddRVATable(std::move(LongJmpTargets), "__guard_longjmp_table",
1431 "__guard_longjmp_count");
1433 // Set __guard_flags, which will be used in the load config to indicate that
1434 // /guard:cf was enabled.
1435 uint32_t GuardFlags = uint32_t(coff_guard_flags::CFInstrumented) |
1436 uint32_t(coff_guard_flags::HasFidTable);
1437 if (Config->GuardCF == GuardCFLevel::Full)
1438 GuardFlags |= uint32_t(coff_guard_flags::HasLongJmpTable);
1439 Symbol *FlagSym = Symtab->findUnderscore("__guard_flags");
1440 cast<DefinedAbsolute>(FlagSym)->setVA(GuardFlags);
1443 // Take a list of input sections containing symbol table indices and add those
1444 // symbols to an RVA table. The challenge is that symbol RVAs are not known and
1445 // depend on the table size, so we can't directly build a set of integers.
1446 void Writer::markSymbolsForRVATable(ObjFile *File,
1447 ArrayRef<SectionChunk *> SymIdxChunks,
1448 SymbolRVASet &TableSymbols) {
1449 for (SectionChunk *C : SymIdxChunks) {
1450 // Skip sections discarded by linker GC. This comes up when a .gfids section
1451 // is associated with something like a vtable and the vtable is discarded.
1452 // In this case, the associated gfids section is discarded, and we don't
1453 // mark the virtual member functions as address-taken by the vtable.
1457 // Validate that the contents look like symbol table indices.
1458 ArrayRef<uint8_t> Data = C->getContents();
1459 if (Data.size() % 4 != 0) {
1460 warn("ignoring " + C->getSectionName() +
1461 " symbol table index section in object " + toString(File));
1465 // Read each symbol table index and check if that symbol was included in the
1466 // final link. If so, add it to the table symbol set.
1467 ArrayRef<ulittle32_t> SymIndices(
1468 reinterpret_cast<const ulittle32_t *>(Data.data()), Data.size() / 4);
1469 ArrayRef<Symbol *> ObjSymbols = File->getSymbols();
1470 for (uint32_t SymIndex : SymIndices) {
1471 if (SymIndex >= ObjSymbols.size()) {
1472 warn("ignoring invalid symbol table index in section " +
1473 C->getSectionName() + " in object " + toString(File));
1476 if (Symbol *S = ObjSymbols[SymIndex]) {
1478 addSymbolToRVASet(TableSymbols, cast<Defined>(S));
1484 // Replace the absolute table symbol with a synthetic symbol pointing to
1485 // TableChunk so that we can emit base relocations for it and resolve section
1486 // relative relocations.
1487 void Writer::maybeAddRVATable(SymbolRVASet TableSymbols, StringRef TableSym,
1488 StringRef CountSym) {
1489 if (TableSymbols.empty())
1492 RVATableChunk *TableChunk = make<RVATableChunk>(std::move(TableSymbols));
1493 RdataSec->addChunk(TableChunk);
1495 Symbol *T = Symtab->findUnderscore(TableSym);
1496 Symbol *C = Symtab->findUnderscore(CountSym);
1497 replaceSymbol<DefinedSynthetic>(T, T->getName(), TableChunk);
1498 cast<DefinedAbsolute>(C)->setVA(TableChunk->getSize() / 4);
1501 // MinGW specific. Gather all relocations that are imported from a DLL even
1502 // though the code didn't expect it to, produce the table that the runtime
1503 // uses for fixing them up, and provide the synthetic symbols that the
1504 // runtime uses for finding the table.
1505 void Writer::createRuntimePseudoRelocs() {
1506 std::vector<RuntimePseudoReloc> Rels;
1508 for (Chunk *C : Symtab->getChunks()) {
1509 auto *SC = dyn_cast<SectionChunk>(C);
1510 if (!SC || !SC->Live)
1512 SC->getRuntimePseudoRelocs(Rels);
1516 log("Writing " + Twine(Rels.size()) + " runtime pseudo relocations");
1517 PseudoRelocTableChunk *Table = make<PseudoRelocTableChunk>(Rels);
1518 RdataSec->addChunk(Table);
1519 EmptyChunk *EndOfList = make<EmptyChunk>();
1520 RdataSec->addChunk(EndOfList);
1522 Symbol *HeadSym = Symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__");
1523 Symbol *EndSym = Symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__");
1524 replaceSymbol<DefinedSynthetic>(HeadSym, HeadSym->getName(), Table);
1525 replaceSymbol<DefinedSynthetic>(EndSym, EndSym->getName(), EndOfList);
1529 // The MinGW .ctors and .dtors lists have sentinels at each end;
1530 // a (uintptr_t)-1 at the start and a (uintptr_t)0 at the end.
1531 // There's a symbol pointing to the start sentinel pointer, __CTOR_LIST__
1532 // and __DTOR_LIST__ respectively.
1533 void Writer::insertCtorDtorSymbols() {
1534 AbsolutePointerChunk *CtorListHead = make<AbsolutePointerChunk>(-1);
1535 AbsolutePointerChunk *CtorListEnd = make<AbsolutePointerChunk>(0);
1536 AbsolutePointerChunk *DtorListHead = make<AbsolutePointerChunk>(-1);
1537 AbsolutePointerChunk *DtorListEnd = make<AbsolutePointerChunk>(0);
1538 CtorsSec->insertChunkAtStart(CtorListHead);
1539 CtorsSec->addChunk(CtorListEnd);
1540 DtorsSec->insertChunkAtStart(DtorListHead);
1541 DtorsSec->addChunk(DtorListEnd);
1543 Symbol *CtorListSym = Symtab->findUnderscore("__CTOR_LIST__");
1544 Symbol *DtorListSym = Symtab->findUnderscore("__DTOR_LIST__");
1545 replaceSymbol<DefinedSynthetic>(CtorListSym, CtorListSym->getName(),
1547 replaceSymbol<DefinedSynthetic>(DtorListSym, DtorListSym->getName(),
1551 // Handles /section options to allow users to overwrite
1552 // section attributes.
1553 void Writer::setSectionPermissions() {
1554 for (auto &P : Config->Section) {
1555 StringRef Name = P.first;
1556 uint32_t Perm = P.second;
1557 for (OutputSection *Sec : OutputSections)
1558 if (Sec->Name == Name)
1559 Sec->setPermissions(Perm);
1563 // Write section contents to a mmap'ed file.
1564 void Writer::writeSections() {
1565 // Record the number of sections to apply section index relocations
1566 // against absolute symbols. See applySecIdx in Chunks.cpp..
1567 DefinedAbsolute::NumOutputSections = OutputSections.size();
1569 uint8_t *Buf = Buffer->getBufferStart();
1570 for (OutputSection *Sec : OutputSections) {
1571 uint8_t *SecBuf = Buf + Sec->getFileOff();
1572 // Fill gaps between functions in .text with INT3 instructions
1573 // instead of leaving as NUL bytes (which can be interpreted as
1574 // ADD instructions).
1575 if (Sec->Header.Characteristics & IMAGE_SCN_CNT_CODE)
1576 memset(SecBuf, 0xCC, Sec->getRawSize());
1577 for_each(parallel::par, Sec->Chunks.begin(), Sec->Chunks.end(),
1578 [&](Chunk *C) { C->writeTo(SecBuf); });
1582 void Writer::writeBuildId() {
1583 // There are two important parts to the build ID.
1584 // 1) If building with debug info, the COFF debug directory contains a
1585 // timestamp as well as a Guid and Age of the PDB.
1586 // 2) In all cases, the PE COFF file header also contains a timestamp.
1587 // For reproducibility, instead of a timestamp we want to use a hash of the
1589 if (Config->Debug) {
1590 assert(BuildId && "BuildId is not set!");
1591 // BuildId->BuildId was filled in when the PDB was written.
1594 // At this point the only fields in the COFF file which remain unset are the
1595 // "timestamp" in the COFF file header, and the ones in the coff debug
1596 // directory. Now we can hash the file and write that hash to the various
1597 // timestamp fields in the file.
1598 StringRef OutputFileData(
1599 reinterpret_cast<const char *>(Buffer->getBufferStart()),
1600 Buffer->getBufferSize());
1602 uint32_t Timestamp = Config->Timestamp;
1604 bool GenerateSyntheticBuildId =
1605 Config->MinGW && Config->Debug && Config->PDBPath.empty();
1607 if (Config->Repro || GenerateSyntheticBuildId)
1608 Hash = xxHash64(OutputFileData);
1611 Timestamp = static_cast<uint32_t>(Hash);
1613 if (GenerateSyntheticBuildId) {
1614 // For MinGW builds without a PDB file, we still generate a build id
1615 // to allow associating a crash dump to the executable.
1616 BuildId->BuildId->PDB70.CVSignature = OMF::Signature::PDB70;
1617 BuildId->BuildId->PDB70.Age = 1;
1618 memcpy(BuildId->BuildId->PDB70.Signature, &Hash, 8);
1619 // xxhash only gives us 8 bytes, so put some fixed data in the other half.
1620 memcpy(&BuildId->BuildId->PDB70.Signature[8], "LLD PDB.", 8);
1624 DebugDirectory->setTimeDateStamp(Timestamp);
1626 uint8_t *Buf = Buffer->getBufferStart();
1627 Buf += DOSStubSize + sizeof(PEMagic);
1628 object::coff_file_header *CoffHeader =
1629 reinterpret_cast<coff_file_header *>(Buf);
1630 CoffHeader->TimeDateStamp = Timestamp;
1633 // Sort .pdata section contents according to PE/COFF spec 5.5.
1634 void Writer::sortExceptionTable() {
1637 // We assume .pdata contains function table entries only.
1638 auto BufAddr = [&](Chunk *C) {
1639 return Buffer->getBufferStart() + C->getOutputSection()->getFileOff() +
1640 C->getRVA() - C->getOutputSection()->getRVA();
1642 uint8_t *Begin = BufAddr(FirstPdata);
1643 uint8_t *End = BufAddr(LastPdata) + LastPdata->getSize();
1644 if (Config->Machine == AMD64) {
1645 struct Entry { ulittle32_t Begin, End, Unwind; };
1646 sort(parallel::par, (Entry *)Begin, (Entry *)End,
1647 [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
1650 if (Config->Machine == ARMNT || Config->Machine == ARM64) {
1651 struct Entry { ulittle32_t Begin, Unwind; };
1652 sort(parallel::par, (Entry *)Begin, (Entry *)End,
1653 [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; });
1656 errs() << "warning: don't know how to handle .pdata.\n";
1659 // The CRT section contains, among other things, the array of function
1660 // pointers that initialize every global variable that is not trivially
1661 // constructed. The CRT calls them one after the other prior to invoking
1664 // As per C++ spec, 3.6.2/2.3,
1665 // "Variables with ordered initialization defined within a single
1666 // translation unit shall be initialized in the order of their definitions
1667 // in the translation unit"
1669 // It is therefore critical to sort the chunks containing the function
1670 // pointers in the order that they are listed in the object file (top to
1671 // bottom), otherwise global objects might not be initialized in the
1673 void Writer::sortCRTSectionChunks(std::vector<Chunk *> &Chunks) {
1674 auto SectionChunkOrder = [](const Chunk *A, const Chunk *B) {
1675 auto SA = dyn_cast<SectionChunk>(A);
1676 auto SB = dyn_cast<SectionChunk>(B);
1677 assert(SA && SB && "Non-section chunks in CRT section!");
1679 StringRef SAObj = SA->File->MB.getBufferIdentifier();
1680 StringRef SBObj = SB->File->MB.getBufferIdentifier();
1682 return SAObj == SBObj && SA->getSectionNumber() < SB->getSectionNumber();
1684 std::stable_sort(Chunks.begin(), Chunks.end(), SectionChunkOrder);
1686 if (Config->Verbose) {
1687 for (auto &C : Chunks) {
1688 auto SC = dyn_cast<SectionChunk>(C);
1689 log(" " + SC->File->MB.getBufferIdentifier().str() +
1690 ", SectionID: " + Twine(SC->getSectionNumber()));
1695 OutputSection *Writer::findSection(StringRef Name) {
1696 for (OutputSection *Sec : OutputSections)
1697 if (Sec->Name == Name)
1702 uint32_t Writer::getSizeOfInitializedData() {
1704 for (OutputSection *S : OutputSections)
1705 if (S->Header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
1706 Res += S->getRawSize();
1710 // Add base relocations to .reloc section.
1711 void Writer::addBaserels() {
1712 if (!Config->Relocatable)
1714 RelocSec->Chunks.clear();
1715 std::vector<Baserel> V;
1716 for (OutputSection *Sec : OutputSections) {
1717 if (Sec->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
1719 // Collect all locations for base relocations.
1720 for (Chunk *C : Sec->Chunks)
1722 // Add the addresses to .reloc section.
1724 addBaserelBlocks(V);
1729 // Add addresses to .reloc section. Note that addresses are grouped by page.
1730 void Writer::addBaserelBlocks(std::vector<Baserel> &V) {
1731 const uint32_t Mask = ~uint32_t(PageSize - 1);
1732 uint32_t Page = V[0].RVA & Mask;
1733 size_t I = 0, J = 1;
1734 for (size_t E = V.size(); J < E; ++J) {
1735 uint32_t P = V[J].RVA & Mask;
1738 RelocSec->addChunk(make<BaserelChunk>(Page, &V[I], &V[0] + J));
1744 RelocSec->addChunk(make<BaserelChunk>(Page, &V[I], &V[0] + J));