1 //===- Writer.cpp ---------------------------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "Writer.h" 10 #include "Config.h" 11 #include "DLL.h" 12 #include "InputFiles.h" 13 #include "MapFile.h" 14 #include "PDB.h" 15 #include "SymbolTable.h" 16 #include "Symbols.h" 17 #include "lld/Common/ErrorHandler.h" 18 #include "lld/Common/Memory.h" 19 #include "lld/Common/Threads.h" 20 #include "lld/Common/Timer.h" 21 #include "llvm/ADT/DenseMap.h" 22 #include "llvm/ADT/STLExtras.h" 23 #include "llvm/ADT/StringSwitch.h" 24 #include "llvm/Support/BinaryStreamReader.h" 25 #include "llvm/Support/Debug.h" 26 #include "llvm/Support/Endian.h" 27 #include "llvm/Support/FileOutputBuffer.h" 28 #include "llvm/Support/Parallel.h" 29 #include "llvm/Support/Path.h" 30 #include "llvm/Support/RandomNumberGenerator.h" 31 #include "llvm/Support/xxhash.h" 32 #include <algorithm> 33 #include <cstdio> 34 #include <map> 35 #include <memory> 36 #include <utility> 37 38 using namespace llvm; 39 using namespace llvm::COFF; 40 using namespace llvm::object; 41 using namespace llvm::support; 42 using namespace llvm::support::endian; 43 using namespace lld; 44 using namespace lld::coff; 45 46 /* To re-generate DOSProgram: 47 $ cat > /tmp/DOSProgram.asm 48 org 0 49 ; Copy cs to ds. 50 push cs 51 pop ds 52 ; Point ds:dx at the $-terminated string. 53 mov dx, str 54 ; Int 21/AH=09h: Write string to standard output. 55 mov ah, 0x9 56 int 0x21 57 ; Int 21/AH=4Ch: Exit with return code (in AL). 58 mov ax, 0x4C01 59 int 0x21 60 str: 61 db 'This program cannot be run in DOS mode.$' 62 align 8, db 0 63 $ nasm -fbin /tmp/DOSProgram.asm -o /tmp/DOSProgram.bin 64 $ xxd -i /tmp/DOSProgram.bin 65 */ 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 72 }; 73 static_assert(sizeof(DOSProgram) % 8 == 0, 74 "DOSProgram size must be multiple of 8"); 75 76 static const int DOSStubSize = sizeof(dos_header) + sizeof(DOSProgram); 77 static_assert(DOSStubSize % 8 == 0, "DOSStub size must be multiple of 8"); 78 79 static const int NumberOfDataDirectory = 16; 80 81 // Global vector of all output sections. After output sections are finalized, 82 // this can be indexed by Chunk::getOutputSection. 83 static std::vector<OutputSection *> OutputSections; 84 85 OutputSection *Chunk::getOutputSection() const { 86 return OSIdx == 0 ? nullptr : OutputSections[OSIdx - 1]; 87 } 88 89 namespace { 90 91 class DebugDirectoryChunk : public NonSectionChunk { 92 public: 93 DebugDirectoryChunk(const std::vector<Chunk *> &R, bool WriteRepro) 94 : Records(R), WriteRepro(WriteRepro) {} 95 96 size_t getSize() const override { 97 return (Records.size() + int(WriteRepro)) * sizeof(debug_directory); 98 } 99 100 void writeTo(uint8_t *B) const override { 101 auto *D = reinterpret_cast<debug_directory *>(B); 102 103 for (const Chunk *Record : Records) { 104 OutputSection *OS = Record->getOutputSection(); 105 uint64_t Offs = OS->getFileOff() + (Record->getRVA() - OS->getRVA()); 106 fillEntry(D, COFF::IMAGE_DEBUG_TYPE_CODEVIEW, Record->getSize(), 107 Record->getRVA(), Offs); 108 ++D; 109 } 110 111 if (WriteRepro) { 112 // FIXME: The COFF spec allows either a 0-sized entry to just say 113 // "the timestamp field is really a hash", or a 4-byte size field 114 // followed by that many bytes containing a longer hash (with the 115 // lowest 4 bytes usually being the timestamp in little-endian order). 116 // Consider storing the full 8 bytes computed by xxHash64 here. 117 fillEntry(D, COFF::IMAGE_DEBUG_TYPE_REPRO, 0, 0, 0); 118 } 119 } 120 121 void setTimeDateStamp(uint32_t TimeDateStamp) { 122 for (support::ulittle32_t *TDS : TimeDateStamps) 123 *TDS = TimeDateStamp; 124 } 125 126 private: 127 void fillEntry(debug_directory *D, COFF::DebugType DebugType, size_t Size, 128 uint64_t RVA, uint64_t Offs) const { 129 D->Characteristics = 0; 130 D->TimeDateStamp = 0; 131 D->MajorVersion = 0; 132 D->MinorVersion = 0; 133 D->Type = DebugType; 134 D->SizeOfData = Size; 135 D->AddressOfRawData = RVA; 136 D->PointerToRawData = Offs; 137 138 TimeDateStamps.push_back(&D->TimeDateStamp); 139 } 140 141 mutable std::vector<support::ulittle32_t *> TimeDateStamps; 142 const std::vector<Chunk *> &Records; 143 bool WriteRepro; 144 }; 145 146 class CVDebugRecordChunk : public NonSectionChunk { 147 public: 148 size_t getSize() const override { 149 return sizeof(codeview::DebugInfo) + Config->PDBAltPath.size() + 1; 150 } 151 152 void writeTo(uint8_t *B) const override { 153 // Save off the DebugInfo entry to backfill the file signature (build id) 154 // in Writer::writeBuildId 155 BuildId = reinterpret_cast<codeview::DebugInfo *>(B); 156 157 // variable sized field (PDB Path) 158 char *P = reinterpret_cast<char *>(B + sizeof(*BuildId)); 159 if (!Config->PDBAltPath.empty()) 160 memcpy(P, Config->PDBAltPath.data(), Config->PDBAltPath.size()); 161 P[Config->PDBAltPath.size()] = '\0'; 162 } 163 164 mutable codeview::DebugInfo *BuildId = nullptr; 165 }; 166 167 // PartialSection represents a group of chunks that contribute to an 168 // OutputSection. Collating a collection of PartialSections of same name and 169 // characteristics constitutes the OutputSection. 170 class PartialSectionKey { 171 public: 172 StringRef Name; 173 unsigned Characteristics; 174 175 bool operator<(const PartialSectionKey &Other) const { 176 int C = Name.compare(Other.Name); 177 if (C == 1) 178 return false; 179 if (C == 0) 180 return Characteristics < Other.Characteristics; 181 return true; 182 } 183 }; 184 185 // The writer writes a SymbolTable result to a file. 186 class Writer { 187 public: 188 Writer() : Buffer(errorHandler().OutputBuffer) {} 189 void run(); 190 191 private: 192 void createSections(); 193 void createMiscChunks(); 194 void createImportTables(); 195 void appendImportThunks(); 196 void locateImportTables(); 197 void createExportTable(); 198 void mergeSections(); 199 void removeUnusedSections(); 200 void assignAddresses(); 201 void finalizeAddresses(); 202 void removeEmptySections(); 203 void assignOutputSectionIndices(); 204 void createSymbolAndStringTable(); 205 void openFile(StringRef OutputPath); 206 template <typename PEHeaderTy> void writeHeader(); 207 void createSEHTable(); 208 void createRuntimePseudoRelocs(); 209 void insertCtorDtorSymbols(); 210 void createGuardCFTables(); 211 void markSymbolsForRVATable(ObjFile *File, 212 ArrayRef<SectionChunk *> SymIdxChunks, 213 SymbolRVASet &TableSymbols); 214 void maybeAddRVATable(SymbolRVASet TableSymbols, StringRef TableSym, 215 StringRef CountSym); 216 void setSectionPermissions(); 217 void writeSections(); 218 void writeBuildId(); 219 void sortExceptionTable(); 220 void sortCRTSectionChunks(std::vector<Chunk *> &Chunks); 221 void addSyntheticIdata(); 222 bool fixGnuImportChunks(); 223 PartialSection *createPartialSection(StringRef Name, uint32_t OutChars); 224 PartialSection *findPartialSection(StringRef Name, uint32_t OutChars); 225 226 llvm::Optional<coff_symbol16> createSymbol(Defined *D); 227 size_t addEntryToStringTable(StringRef Str); 228 229 OutputSection *findSection(StringRef Name); 230 void addBaserels(); 231 void addBaserelBlocks(std::vector<Baserel> &V); 232 233 uint32_t getSizeOfInitializedData(); 234 235 std::unique_ptr<FileOutputBuffer> &Buffer; 236 std::map<PartialSectionKey, PartialSection *> PartialSections; 237 std::vector<char> Strtab; 238 std::vector<llvm::object::coff_symbol16> OutputSymtab; 239 IdataContents Idata; 240 Chunk *ImportTableStart = nullptr; 241 uint64_t ImportTableSize = 0; 242 Chunk *IATStart = nullptr; 243 uint64_t IATSize = 0; 244 DelayLoadContents DelayIdata; 245 EdataContents Edata; 246 bool SetNoSEHCharacteristic = false; 247 248 DebugDirectoryChunk *DebugDirectory = nullptr; 249 std::vector<Chunk *> DebugRecords; 250 CVDebugRecordChunk *BuildId = nullptr; 251 ArrayRef<uint8_t> SectionTable; 252 253 uint64_t FileSize; 254 uint32_t PointerToSymbolTable = 0; 255 uint64_t SizeOfImage; 256 uint64_t SizeOfHeaders; 257 258 OutputSection *TextSec; 259 OutputSection *RdataSec; 260 OutputSection *BuildidSec; 261 OutputSection *DataSec; 262 OutputSection *PdataSec; 263 OutputSection *IdataSec; 264 OutputSection *EdataSec; 265 OutputSection *DidatSec; 266 OutputSection *RsrcSec; 267 OutputSection *RelocSec; 268 OutputSection *CtorsSec; 269 OutputSection *DtorsSec; 270 271 // The first and last .pdata sections in the output file. 272 // 273 // We need to keep track of the location of .pdata in whichever section it 274 // gets merged into so that we can sort its contents and emit a correct data 275 // directory entry for the exception table. This is also the case for some 276 // other sections (such as .edata) but because the contents of those sections 277 // are entirely linker-generated we can keep track of their locations using 278 // the chunks that the linker creates. All .pdata chunks come from input 279 // files, so we need to keep track of them separately. 280 Chunk *FirstPdata = nullptr; 281 Chunk *LastPdata; 282 }; 283 } // anonymous namespace 284 285 namespace lld { 286 namespace coff { 287 288 static Timer CodeLayoutTimer("Code Layout", Timer::root()); 289 static Timer DiskCommitTimer("Commit Output File", Timer::root()); 290 291 void writeResult() { Writer().run(); } 292 293 void OutputSection::addChunk(Chunk *C) { 294 Chunks.push_back(C); 295 } 296 297 void OutputSection::insertChunkAtStart(Chunk *C) { 298 Chunks.insert(Chunks.begin(), C); 299 } 300 301 void OutputSection::setPermissions(uint32_t C) { 302 Header.Characteristics &= ~PermMask; 303 Header.Characteristics |= C; 304 } 305 306 void OutputSection::merge(OutputSection *Other) { 307 Chunks.insert(Chunks.end(), Other->Chunks.begin(), Other->Chunks.end()); 308 Other->Chunks.clear(); 309 ContribSections.insert(ContribSections.end(), Other->ContribSections.begin(), 310 Other->ContribSections.end()); 311 Other->ContribSections.clear(); 312 } 313 314 // Write the section header to a given buffer. 315 void OutputSection::writeHeaderTo(uint8_t *Buf) { 316 auto *Hdr = reinterpret_cast<coff_section *>(Buf); 317 *Hdr = Header; 318 if (StringTableOff) { 319 // If name is too long, write offset into the string table as a name. 320 sprintf(Hdr->Name, "/%d", StringTableOff); 321 } else { 322 assert(!Config->Debug || Name.size() <= COFF::NameSize || 323 (Hdr->Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0); 324 strncpy(Hdr->Name, Name.data(), 325 std::min(Name.size(), (size_t)COFF::NameSize)); 326 } 327 } 328 329 void OutputSection::addContributingPartialSection(PartialSection *Sec) { 330 ContribSections.push_back(Sec); 331 } 332 333 } // namespace coff 334 } // namespace lld 335 336 // Check whether the target address S is in range from a relocation 337 // of type RelType at address P. 338 static bool isInRange(uint16_t RelType, uint64_t S, uint64_t P, int Margin) { 339 if (Config->Machine == ARMNT) { 340 int64_t Diff = AbsoluteDifference(S, P + 4) + Margin; 341 switch (RelType) { 342 case IMAGE_REL_ARM_BRANCH20T: 343 return isInt<21>(Diff); 344 case IMAGE_REL_ARM_BRANCH24T: 345 case IMAGE_REL_ARM_BLX23T: 346 return isInt<25>(Diff); 347 default: 348 return true; 349 } 350 } else if (Config->Machine == ARM64) { 351 int64_t Diff = AbsoluteDifference(S, P) + Margin; 352 switch (RelType) { 353 case IMAGE_REL_ARM64_BRANCH26: 354 return isInt<28>(Diff); 355 case IMAGE_REL_ARM64_BRANCH19: 356 return isInt<21>(Diff); 357 case IMAGE_REL_ARM64_BRANCH14: 358 return isInt<16>(Diff); 359 default: 360 return true; 361 } 362 } else { 363 llvm_unreachable("Unexpected architecture"); 364 } 365 } 366 367 // Return the last thunk for the given target if it is in range, 368 // or create a new one. 369 static std::pair<Defined *, bool> 370 getThunk(DenseMap<uint64_t, Defined *> &LastThunks, Defined *Target, uint64_t P, 371 uint16_t Type, int Margin) { 372 Defined *&LastThunk = LastThunks[Target->getRVA()]; 373 if (LastThunk && isInRange(Type, LastThunk->getRVA(), P, Margin)) 374 return {LastThunk, false}; 375 Chunk *C; 376 switch (Config->Machine) { 377 case ARMNT: 378 C = make<RangeExtensionThunkARM>(Target); 379 break; 380 case ARM64: 381 C = make<RangeExtensionThunkARM64>(Target); 382 break; 383 default: 384 llvm_unreachable("Unexpected architecture"); 385 } 386 Defined *D = make<DefinedSynthetic>("", C); 387 LastThunk = D; 388 return {D, true}; 389 } 390 391 // This checks all relocations, and for any relocation which isn't in range 392 // it adds a thunk after the section chunk that contains the relocation. 393 // If the latest thunk for the specific target is in range, that is used 394 // instead of creating a new thunk. All range checks are done with the 395 // specified margin, to make sure that relocations that originally are in 396 // range, but only barely, also get thunks - in case other added thunks makes 397 // the target go out of range. 398 // 399 // After adding thunks, we verify that all relocations are in range (with 400 // no extra margin requirements). If this failed, we restart (throwing away 401 // the previously created thunks) and retry with a wider margin. 402 static bool createThunks(OutputSection *OS, int Margin) { 403 bool AddressesChanged = false; 404 DenseMap<uint64_t, Defined *> LastThunks; 405 DenseMap<std::pair<ObjFile *, Defined *>, uint32_t> ThunkSymtabIndices; 406 size_t ThunksSize = 0; 407 // Recheck Chunks.size() each iteration, since we can insert more 408 // elements into it. 409 for (size_t I = 0; I != OS->Chunks.size(); ++I) { 410 SectionChunk *SC = dyn_cast_or_null<SectionChunk>(OS->Chunks[I]); 411 if (!SC) 412 continue; 413 size_t ThunkInsertionSpot = I + 1; 414 415 // Try to get a good enough estimate of where new thunks will be placed. 416 // Offset this by the size of the new thunks added so far, to make the 417 // estimate slightly better. 418 size_t ThunkInsertionRVA = SC->getRVA() + SC->getSize() + ThunksSize; 419 ObjFile *File = SC->File; 420 std::vector<std::pair<uint32_t, uint32_t>> RelocReplacements; 421 ArrayRef<coff_relocation> OriginalRelocs = 422 File->getCOFFObj()->getRelocations(SC->Header); 423 for (size_t J = 0, E = OriginalRelocs.size(); J < E; ++J) { 424 const coff_relocation &Rel = OriginalRelocs[J]; 425 Symbol *RelocTarget = File->getSymbol(Rel.SymbolTableIndex); 426 427 // The estimate of the source address P should be pretty accurate, 428 // but we don't know whether the target Symbol address should be 429 // offset by ThunkSize or not (or by some of ThunksSize but not all of 430 // it), giving us some uncertainty once we have added one thunk. 431 uint64_t P = SC->getRVA() + Rel.VirtualAddress + ThunksSize; 432 433 Defined *Sym = dyn_cast_or_null<Defined>(RelocTarget); 434 if (!Sym) 435 continue; 436 437 uint64_t S = Sym->getRVA(); 438 439 if (isInRange(Rel.Type, S, P, Margin)) 440 continue; 441 442 // If the target isn't in range, hook it up to an existing or new 443 // thunk. 444 Defined *Thunk; 445 bool WasNew; 446 std::tie(Thunk, WasNew) = getThunk(LastThunks, Sym, P, Rel.Type, Margin); 447 if (WasNew) { 448 Chunk *ThunkChunk = Thunk->getChunk(); 449 ThunkChunk->setRVA( 450 ThunkInsertionRVA); // Estimate of where it will be located. 451 OS->Chunks.insert(OS->Chunks.begin() + ThunkInsertionSpot, ThunkChunk); 452 ThunkInsertionSpot++; 453 ThunksSize += ThunkChunk->getSize(); 454 ThunkInsertionRVA += ThunkChunk->getSize(); 455 AddressesChanged = true; 456 } 457 458 // To redirect the relocation, add a symbol to the parent object file's 459 // symbol table, and replace the relocation symbol table index with the 460 // new index. 461 auto Insertion = ThunkSymtabIndices.insert({{File, Thunk}, ~0U}); 462 uint32_t &ThunkSymbolIndex = Insertion.first->second; 463 if (Insertion.second) 464 ThunkSymbolIndex = File->addRangeThunkSymbol(Thunk); 465 RelocReplacements.push_back({J, ThunkSymbolIndex}); 466 } 467 468 // Get a writable copy of this section's relocations so they can be 469 // modified. If the relocations point into the object file, allocate new 470 // memory. Otherwise, this must be previously allocated memory that can be 471 // modified in place. 472 ArrayRef<coff_relocation> CurRelocs = SC->getRelocs(); 473 MutableArrayRef<coff_relocation> NewRelocs; 474 if (OriginalRelocs.data() == CurRelocs.data()) { 475 NewRelocs = makeMutableArrayRef( 476 BAlloc.Allocate<coff_relocation>(OriginalRelocs.size()), 477 OriginalRelocs.size()); 478 } else { 479 NewRelocs = makeMutableArrayRef( 480 const_cast<coff_relocation *>(CurRelocs.data()), CurRelocs.size()); 481 } 482 483 // Copy each relocation, but replace the symbol table indices which need 484 // thunks. 485 auto NextReplacement = RelocReplacements.begin(); 486 auto EndReplacement = RelocReplacements.end(); 487 for (size_t I = 0, E = OriginalRelocs.size(); I != E; ++I) { 488 NewRelocs[I] = OriginalRelocs[I]; 489 if (NextReplacement != EndReplacement && NextReplacement->first == I) { 490 NewRelocs[I].SymbolTableIndex = NextReplacement->second; 491 ++NextReplacement; 492 } 493 } 494 495 SC->setRelocs(NewRelocs); 496 } 497 return AddressesChanged; 498 } 499 500 // Verify that all relocations are in range, with no extra margin requirements. 501 static bool verifyRanges(const std::vector<Chunk *> Chunks) { 502 for (Chunk *C : Chunks) { 503 SectionChunk *SC = dyn_cast_or_null<SectionChunk>(C); 504 if (!SC) 505 continue; 506 507 ArrayRef<coff_relocation> Relocs = SC->getRelocs(); 508 for (size_t J = 0, E = Relocs.size(); J < E; ++J) { 509 const coff_relocation &Rel = Relocs[J]; 510 Symbol *RelocTarget = SC->File->getSymbol(Rel.SymbolTableIndex); 511 512 Defined *Sym = dyn_cast_or_null<Defined>(RelocTarget); 513 if (!Sym) 514 continue; 515 516 uint64_t P = SC->getRVA() + Rel.VirtualAddress; 517 uint64_t S = Sym->getRVA(); 518 519 if (!isInRange(Rel.Type, S, P, 0)) 520 return false; 521 } 522 } 523 return true; 524 } 525 526 // Assign addresses and add thunks if necessary. 527 void Writer::finalizeAddresses() { 528 assignAddresses(); 529 if (Config->Machine != ARMNT && Config->Machine != ARM64) 530 return; 531 532 size_t OrigNumChunks = 0; 533 for (OutputSection *Sec : OutputSections) { 534 Sec->OrigChunks = Sec->Chunks; 535 OrigNumChunks += Sec->Chunks.size(); 536 } 537 538 int Pass = 0; 539 int Margin = 1024 * 100; 540 while (true) { 541 // First check whether we need thunks at all, or if the previous pass of 542 // adding them turned out ok. 543 bool RangesOk = true; 544 size_t NumChunks = 0; 545 for (OutputSection *Sec : OutputSections) { 546 if (!verifyRanges(Sec->Chunks)) { 547 RangesOk = false; 548 break; 549 } 550 NumChunks += Sec->Chunks.size(); 551 } 552 if (RangesOk) { 553 if (Pass > 0) 554 log("Added " + Twine(NumChunks - OrigNumChunks) + " thunks with " + 555 "margin " + Twine(Margin) + " in " + Twine(Pass) + " passes"); 556 return; 557 } 558 559 if (Pass >= 10) 560 fatal("adding thunks hasn't converged after " + Twine(Pass) + " passes"); 561 562 if (Pass > 0) { 563 // If the previous pass didn't work out, reset everything back to the 564 // original conditions before retrying with a wider margin. This should 565 // ideally never happen under real circumstances. 566 for (OutputSection *Sec : OutputSections) 567 Sec->Chunks = Sec->OrigChunks; 568 Margin *= 2; 569 } 570 571 // Try adding thunks everywhere where it is needed, with a margin 572 // to avoid things going out of range due to the added thunks. 573 bool AddressesChanged = false; 574 for (OutputSection *Sec : OutputSections) 575 AddressesChanged |= createThunks(Sec, Margin); 576 // If the verification above thought we needed thunks, we should have 577 // added some. 578 assert(AddressesChanged); 579 580 // Recalculate the layout for the whole image (and verify the ranges at 581 // the start of the next round). 582 assignAddresses(); 583 584 Pass++; 585 } 586 } 587 588 // The main function of the writer. 589 void Writer::run() { 590 ScopedTimer T1(CodeLayoutTimer); 591 592 createImportTables(); 593 createSections(); 594 createMiscChunks(); 595 appendImportThunks(); 596 createExportTable(); 597 mergeSections(); 598 removeUnusedSections(); 599 finalizeAddresses(); 600 removeEmptySections(); 601 assignOutputSectionIndices(); 602 setSectionPermissions(); 603 createSymbolAndStringTable(); 604 605 if (FileSize > UINT32_MAX) 606 fatal("image size (" + Twine(FileSize) + ") " + 607 "exceeds maximum allowable size (" + Twine(UINT32_MAX) + ")"); 608 609 openFile(Config->OutputFile); 610 if (Config->is64()) { 611 writeHeader<pe32plus_header>(); 612 } else { 613 writeHeader<pe32_header>(); 614 } 615 writeSections(); 616 sortExceptionTable(); 617 618 T1.stop(); 619 620 if (!Config->PDBPath.empty() && Config->Debug) { 621 assert(BuildId); 622 createPDB(Symtab, OutputSections, SectionTable, BuildId->BuildId); 623 } 624 writeBuildId(); 625 626 writeMapFile(OutputSections); 627 628 ScopedTimer T2(DiskCommitTimer); 629 if (auto E = Buffer->commit()) 630 fatal("failed to write the output file: " + toString(std::move(E))); 631 } 632 633 static StringRef getOutputSectionName(StringRef Name) { 634 StringRef S = Name.split('$').first; 635 636 // Treat a later period as a separator for MinGW, for sections like 637 // ".ctors.01234". 638 return S.substr(0, S.find('.', 1)); 639 } 640 641 // For /order. 642 static void sortBySectionOrder(std::vector<Chunk *> &Chunks) { 643 auto GetPriority = [](const Chunk *C) { 644 if (auto *Sec = dyn_cast<SectionChunk>(C)) 645 if (Sec->Sym) 646 return Config->Order.lookup(Sec->Sym->getName()); 647 return 0; 648 }; 649 650 llvm::stable_sort(Chunks, [=](const Chunk *A, const Chunk *B) { 651 return GetPriority(A) < GetPriority(B); 652 }); 653 } 654 655 // Sort concrete section chunks from GNU import libraries. 656 // 657 // GNU binutils doesn't use short import files, but instead produces import 658 // libraries that consist of object files, with section chunks for the .idata$* 659 // sections. These are linked just as regular static libraries. Each import 660 // library consists of one header object, one object file for every imported 661 // symbol, and one trailer object. In order for the .idata tables/lists to 662 // be formed correctly, the section chunks within each .idata$* section need 663 // to be grouped by library, and sorted alphabetically within each library 664 // (which makes sure the header comes first and the trailer last). 665 bool Writer::fixGnuImportChunks() { 666 uint32_t RDATA = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ; 667 668 // Make sure all .idata$* section chunks are mapped as RDATA in order to 669 // be sorted into the same sections as our own synthesized .idata chunks. 670 for (auto It : PartialSections) { 671 PartialSection *PSec = It.second; 672 if (!PSec->Name.startswith(".idata")) 673 continue; 674 if (PSec->Characteristics == RDATA) 675 continue; 676 PartialSection *RDataSec = createPartialSection(PSec->Name, RDATA); 677 RDataSec->Chunks.insert(RDataSec->Chunks.end(), PSec->Chunks.begin(), 678 PSec->Chunks.end()); 679 PSec->Chunks.clear(); 680 } 681 682 bool HasIdata = false; 683 // Sort all .idata$* chunks, grouping chunks from the same library, 684 // with alphabetical ordering of the object fils within a library. 685 for (auto It : PartialSections) { 686 PartialSection *PSec = It.second; 687 if (!PSec->Name.startswith(".idata")) 688 continue; 689 690 if (!PSec->Chunks.empty()) 691 HasIdata = true; 692 llvm::stable_sort(PSec->Chunks, [&](Chunk *S, Chunk *T) { 693 SectionChunk *SC1 = dyn_cast_or_null<SectionChunk>(S); 694 SectionChunk *SC2 = dyn_cast_or_null<SectionChunk>(T); 695 if (!SC1 || !SC2) { 696 // if SC1, order them ascending. If SC2 or both null, 697 // S is not less than T. 698 return SC1 != nullptr; 699 } 700 // Make a string with "libraryname/objectfile" for sorting, achieving 701 // both grouping by library and sorting of objects within a library, 702 // at once. 703 std::string Key1 = 704 (SC1->File->ParentName + "/" + SC1->File->getName()).str(); 705 std::string Key2 = 706 (SC2->File->ParentName + "/" + SC2->File->getName()).str(); 707 return Key1 < Key2; 708 }); 709 } 710 return HasIdata; 711 } 712 713 // Add generated idata chunks, for imported symbols and DLLs, and a 714 // terminator in .idata$2. 715 void Writer::addSyntheticIdata() { 716 uint32_t RDATA = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ; 717 Idata.create(); 718 719 // Add the .idata content in the right section groups, to allow 720 // chunks from other linked in object files to be grouped together. 721 // See Microsoft PE/COFF spec 5.4 for details. 722 auto Add = [&](StringRef N, std::vector<Chunk *> &V) { 723 PartialSection *PSec = createPartialSection(N, RDATA); 724 PSec->Chunks.insert(PSec->Chunks.end(), V.begin(), V.end()); 725 }; 726 727 // The loader assumes a specific order of data. 728 // Add each type in the correct order. 729 Add(".idata$2", Idata.Dirs); 730 Add(".idata$4", Idata.Lookups); 731 Add(".idata$5", Idata.Addresses); 732 Add(".idata$6", Idata.Hints); 733 Add(".idata$7", Idata.DLLNames); 734 } 735 736 // Locate the first Chunk and size of the import directory list and the 737 // IAT. 738 void Writer::locateImportTables() { 739 uint32_t RDATA = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ; 740 741 if (PartialSection *ImportDirs = findPartialSection(".idata$2", RDATA)) { 742 if (!ImportDirs->Chunks.empty()) 743 ImportTableStart = ImportDirs->Chunks.front(); 744 for (Chunk *C : ImportDirs->Chunks) 745 ImportTableSize += C->getSize(); 746 } 747 748 if (PartialSection *ImportAddresses = findPartialSection(".idata$5", RDATA)) { 749 if (!ImportAddresses->Chunks.empty()) 750 IATStart = ImportAddresses->Chunks.front(); 751 for (Chunk *C : ImportAddresses->Chunks) 752 IATSize += C->getSize(); 753 } 754 } 755 756 // Create output section objects and add them to OutputSections. 757 void Writer::createSections() { 758 // First, create the builtin sections. 759 const uint32_t DATA = IMAGE_SCN_CNT_INITIALIZED_DATA; 760 const uint32_t BSS = IMAGE_SCN_CNT_UNINITIALIZED_DATA; 761 const uint32_t CODE = IMAGE_SCN_CNT_CODE; 762 const uint32_t DISCARDABLE = IMAGE_SCN_MEM_DISCARDABLE; 763 const uint32_t R = IMAGE_SCN_MEM_READ; 764 const uint32_t W = IMAGE_SCN_MEM_WRITE; 765 const uint32_t X = IMAGE_SCN_MEM_EXECUTE; 766 767 SmallDenseMap<std::pair<StringRef, uint32_t>, OutputSection *> Sections; 768 auto CreateSection = [&](StringRef Name, uint32_t OutChars) { 769 OutputSection *&Sec = Sections[{Name, OutChars}]; 770 if (!Sec) { 771 Sec = make<OutputSection>(Name, OutChars); 772 OutputSections.push_back(Sec); 773 } 774 return Sec; 775 }; 776 777 // Try to match the section order used by link.exe. 778 TextSec = CreateSection(".text", CODE | R | X); 779 CreateSection(".bss", BSS | R | W); 780 RdataSec = CreateSection(".rdata", DATA | R); 781 BuildidSec = CreateSection(".buildid", DATA | R); 782 DataSec = CreateSection(".data", DATA | R | W); 783 PdataSec = CreateSection(".pdata", DATA | R); 784 IdataSec = CreateSection(".idata", DATA | R); 785 EdataSec = CreateSection(".edata", DATA | R); 786 DidatSec = CreateSection(".didat", DATA | R); 787 RsrcSec = CreateSection(".rsrc", DATA | R); 788 RelocSec = CreateSection(".reloc", DATA | DISCARDABLE | R); 789 CtorsSec = CreateSection(".ctors", DATA | R | W); 790 DtorsSec = CreateSection(".dtors", DATA | R | W); 791 792 // Then bin chunks by name and output characteristics. 793 for (Chunk *C : Symtab->getChunks()) { 794 auto *SC = dyn_cast<SectionChunk>(C); 795 if (SC && !SC->Live) { 796 if (Config->Verbose) 797 SC->printDiscardedMessage(); 798 continue; 799 } 800 StringRef Name = C->getSectionName(); 801 // On MinGW, comdat groups are formed by putting the comdat group name 802 // after the '$' in the section name. Such a section name suffix shouldn't 803 // imply separate alphabetical sorting of those section chunks though. 804 if (Config->MinGW && SC && SC->isCOMDAT()) 805 Name = Name.split('$').first; 806 PartialSection *PSec = createPartialSection(Name, 807 C->getOutputCharacteristics()); 808 PSec->Chunks.push_back(C); 809 } 810 811 // Even in non MinGW cases, we might need to link against GNU import 812 // libraries. 813 bool HasIdata = fixGnuImportChunks(); 814 if (!Idata.empty()) 815 HasIdata = true; 816 817 if (HasIdata) 818 addSyntheticIdata(); 819 820 // Process an /order option. 821 if (!Config->Order.empty()) 822 for (auto It : PartialSections) 823 sortBySectionOrder(It.second->Chunks); 824 825 if (HasIdata) 826 locateImportTables(); 827 828 // Then create an OutputSection for each section. 829 // '$' and all following characters in input section names are 830 // discarded when determining output section. So, .text$foo 831 // contributes to .text, for example. See PE/COFF spec 3.2. 832 for (auto It : PartialSections) { 833 PartialSection *PSec = It.second; 834 StringRef Name = getOutputSectionName(PSec->Name); 835 uint32_t OutChars = PSec->Characteristics; 836 837 if (Name == ".CRT") { 838 // In link.exe, there is a special case for the I386 target where .CRT 839 // sections are treated as if they have output characteristics DATA | R if 840 // their characteristics are DATA | R | W. This implements the same 841 // special case for all architectures. 842 OutChars = DATA | R; 843 844 log("Processing section " + PSec->Name + " -> " + Name); 845 846 sortCRTSectionChunks(PSec->Chunks); 847 } 848 849 OutputSection *Sec = CreateSection(Name, OutChars); 850 for (Chunk *C : PSec->Chunks) 851 Sec->addChunk(C); 852 853 Sec->addContributingPartialSection(PSec); 854 } 855 856 // Finally, move some output sections to the end. 857 auto SectionOrder = [&](const OutputSection *S) { 858 // Move DISCARDABLE (or non-memory-mapped) sections to the end of file 859 // because the loader cannot handle holes. Stripping can remove other 860 // discardable ones than .reloc, which is first of them (created early). 861 if (S->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) 862 return 2; 863 // .rsrc should come at the end of the non-discardable sections because its 864 // size may change by the Win32 UpdateResources() function, causing 865 // subsequent sections to move (see https://crbug.com/827082). 866 if (S == RsrcSec) 867 return 1; 868 return 0; 869 }; 870 llvm::stable_sort(OutputSections, 871 [&](const OutputSection *S, const OutputSection *T) { 872 return SectionOrder(S) < SectionOrder(T); 873 }); 874 } 875 876 void Writer::createMiscChunks() { 877 for (MergeChunk *P : MergeChunk::Instances) { 878 if (P) { 879 P->finalizeContents(); 880 RdataSec->addChunk(P); 881 } 882 } 883 884 // Create thunks for locally-dllimported symbols. 885 if (!Symtab->LocalImportChunks.empty()) { 886 for (Chunk *C : Symtab->LocalImportChunks) 887 RdataSec->addChunk(C); 888 } 889 890 // Create Debug Information Chunks 891 OutputSection *DebugInfoSec = Config->MinGW ? BuildidSec : RdataSec; 892 if (Config->Debug || Config->Repro) { 893 DebugDirectory = make<DebugDirectoryChunk>(DebugRecords, Config->Repro); 894 DebugInfoSec->addChunk(DebugDirectory); 895 } 896 897 if (Config->Debug) { 898 // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified. We 899 // output a PDB no matter what, and this chunk provides the only means of 900 // allowing a debugger to match a PDB and an executable. So we need it even 901 // if we're ultimately not going to write CodeView data to the PDB. 902 BuildId = make<CVDebugRecordChunk>(); 903 DebugRecords.push_back(BuildId); 904 905 for (Chunk *C : DebugRecords) 906 DebugInfoSec->addChunk(C); 907 } 908 909 // Create SEH table. x86-only. 910 if (Config->Machine == I386) 911 createSEHTable(); 912 913 // Create /guard:cf tables if requested. 914 if (Config->GuardCF != GuardCFLevel::Off) 915 createGuardCFTables(); 916 917 if (Config->MinGW) { 918 createRuntimePseudoRelocs(); 919 920 insertCtorDtorSymbols(); 921 } 922 } 923 924 // Create .idata section for the DLL-imported symbol table. 925 // The format of this section is inherently Windows-specific. 926 // IdataContents class abstracted away the details for us, 927 // so we just let it create chunks and add them to the section. 928 void Writer::createImportTables() { 929 // Initialize DLLOrder so that import entries are ordered in 930 // the same order as in the command line. (That affects DLL 931 // initialization order, and this ordering is MSVC-compatible.) 932 for (ImportFile *File : ImportFile::Instances) { 933 if (!File->Live) 934 continue; 935 936 std::string DLL = StringRef(File->DLLName).lower(); 937 if (Config->DLLOrder.count(DLL) == 0) 938 Config->DLLOrder[DLL] = Config->DLLOrder.size(); 939 940 if (File->ImpSym && !isa<DefinedImportData>(File->ImpSym)) 941 fatal(toString(*File->ImpSym) + " was replaced"); 942 DefinedImportData *ImpSym = cast_or_null<DefinedImportData>(File->ImpSym); 943 if (Config->DelayLoads.count(StringRef(File->DLLName).lower())) { 944 if (!File->ThunkSym) 945 fatal("cannot delay-load " + toString(File) + 946 " due to import of data: " + toString(*ImpSym)); 947 DelayIdata.add(ImpSym); 948 } else { 949 Idata.add(ImpSym); 950 } 951 } 952 } 953 954 void Writer::appendImportThunks() { 955 if (ImportFile::Instances.empty()) 956 return; 957 958 for (ImportFile *File : ImportFile::Instances) { 959 if (!File->Live) 960 continue; 961 962 if (!File->ThunkSym) 963 continue; 964 965 if (!isa<DefinedImportThunk>(File->ThunkSym)) 966 fatal(toString(*File->ThunkSym) + " was replaced"); 967 DefinedImportThunk *Thunk = cast<DefinedImportThunk>(File->ThunkSym); 968 if (File->ThunkLive) 969 TextSec->addChunk(Thunk->getChunk()); 970 } 971 972 if (!DelayIdata.empty()) { 973 Defined *Helper = cast<Defined>(Config->DelayLoadHelper); 974 DelayIdata.create(Helper); 975 for (Chunk *C : DelayIdata.getChunks()) 976 DidatSec->addChunk(C); 977 for (Chunk *C : DelayIdata.getDataChunks()) 978 DataSec->addChunk(C); 979 for (Chunk *C : DelayIdata.getCodeChunks()) 980 TextSec->addChunk(C); 981 } 982 } 983 984 void Writer::createExportTable() { 985 if (Config->Exports.empty()) 986 return; 987 for (Chunk *C : Edata.Chunks) 988 EdataSec->addChunk(C); 989 } 990 991 void Writer::removeUnusedSections() { 992 // Remove sections that we can be sure won't get content, to avoid 993 // allocating space for their section headers. 994 auto IsUnused = [this](OutputSection *S) { 995 if (S == RelocSec) 996 return false; // This section is populated later. 997 // MergeChunks have zero size at this point, as their size is finalized 998 // later. Only remove sections that have no Chunks at all. 999 return S->Chunks.empty(); 1000 }; 1001 OutputSections.erase( 1002 std::remove_if(OutputSections.begin(), OutputSections.end(), IsUnused), 1003 OutputSections.end()); 1004 } 1005 1006 // The Windows loader doesn't seem to like empty sections, 1007 // so we remove them if any. 1008 void Writer::removeEmptySections() { 1009 auto IsEmpty = [](OutputSection *S) { return S->getVirtualSize() == 0; }; 1010 OutputSections.erase( 1011 std::remove_if(OutputSections.begin(), OutputSections.end(), IsEmpty), 1012 OutputSections.end()); 1013 } 1014 1015 void Writer::assignOutputSectionIndices() { 1016 // Assign final output section indices, and assign each chunk to its output 1017 // section. 1018 uint32_t Idx = 1; 1019 for (OutputSection *OS : OutputSections) { 1020 OS->SectionIndex = Idx; 1021 for (Chunk *C : OS->Chunks) 1022 C->setOutputSectionIdx(Idx); 1023 ++Idx; 1024 } 1025 1026 // Merge chunks are containers of chunks, so assign those an output section 1027 // too. 1028 for (MergeChunk *MC : MergeChunk::Instances) 1029 if (MC) 1030 for (SectionChunk *SC : MC->Sections) 1031 if (SC && SC->Live) 1032 SC->setOutputSectionIdx(MC->getOutputSectionIdx()); 1033 } 1034 1035 size_t Writer::addEntryToStringTable(StringRef Str) { 1036 assert(Str.size() > COFF::NameSize); 1037 size_t OffsetOfEntry = Strtab.size() + 4; // +4 for the size field 1038 Strtab.insert(Strtab.end(), Str.begin(), Str.end()); 1039 Strtab.push_back('\0'); 1040 return OffsetOfEntry; 1041 } 1042 1043 Optional<coff_symbol16> Writer::createSymbol(Defined *Def) { 1044 coff_symbol16 Sym; 1045 switch (Def->kind()) { 1046 case Symbol::DefinedAbsoluteKind: 1047 Sym.Value = Def->getRVA(); 1048 Sym.SectionNumber = IMAGE_SYM_ABSOLUTE; 1049 break; 1050 case Symbol::DefinedSyntheticKind: 1051 // Relative symbols are unrepresentable in a COFF symbol table. 1052 return None; 1053 default: { 1054 // Don't write symbols that won't be written to the output to the symbol 1055 // table. 1056 Chunk *C = Def->getChunk(); 1057 if (!C) 1058 return None; 1059 OutputSection *OS = C->getOutputSection(); 1060 if (!OS) 1061 return None; 1062 1063 Sym.Value = Def->getRVA() - OS->getRVA(); 1064 Sym.SectionNumber = OS->SectionIndex; 1065 break; 1066 } 1067 } 1068 1069 StringRef Name = Def->getName(); 1070 if (Name.size() > COFF::NameSize) { 1071 Sym.Name.Offset.Zeroes = 0; 1072 Sym.Name.Offset.Offset = addEntryToStringTable(Name); 1073 } else { 1074 memset(Sym.Name.ShortName, 0, COFF::NameSize); 1075 memcpy(Sym.Name.ShortName, Name.data(), Name.size()); 1076 } 1077 1078 if (auto *D = dyn_cast<DefinedCOFF>(Def)) { 1079 COFFSymbolRef Ref = D->getCOFFSymbol(); 1080 Sym.Type = Ref.getType(); 1081 Sym.StorageClass = Ref.getStorageClass(); 1082 } else { 1083 Sym.Type = IMAGE_SYM_TYPE_NULL; 1084 Sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL; 1085 } 1086 Sym.NumberOfAuxSymbols = 0; 1087 return Sym; 1088 } 1089 1090 void Writer::createSymbolAndStringTable() { 1091 // PE/COFF images are limited to 8 byte section names. Longer names can be 1092 // supported by writing a non-standard string table, but this string table is 1093 // not mapped at runtime and the long names will therefore be inaccessible. 1094 // link.exe always truncates section names to 8 bytes, whereas binutils always 1095 // preserves long section names via the string table. LLD adopts a hybrid 1096 // solution where discardable sections have long names preserved and 1097 // non-discardable sections have their names truncated, to ensure that any 1098 // section which is mapped at runtime also has its name mapped at runtime. 1099 for (OutputSection *Sec : OutputSections) { 1100 if (Sec->Name.size() <= COFF::NameSize) 1101 continue; 1102 if ((Sec->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) == 0) 1103 continue; 1104 Sec->setStringTableOff(addEntryToStringTable(Sec->Name)); 1105 } 1106 1107 if (Config->DebugDwarf || Config->DebugSymtab) { 1108 for (ObjFile *File : ObjFile::Instances) { 1109 for (Symbol *B : File->getSymbols()) { 1110 auto *D = dyn_cast_or_null<Defined>(B); 1111 if (!D || D->WrittenToSymtab) 1112 continue; 1113 D->WrittenToSymtab = true; 1114 1115 if (Optional<coff_symbol16> Sym = createSymbol(D)) 1116 OutputSymtab.push_back(*Sym); 1117 } 1118 } 1119 } 1120 1121 if (OutputSymtab.empty() && Strtab.empty()) 1122 return; 1123 1124 // We position the symbol table to be adjacent to the end of the last section. 1125 uint64_t FileOff = FileSize; 1126 PointerToSymbolTable = FileOff; 1127 FileOff += OutputSymtab.size() * sizeof(coff_symbol16); 1128 FileOff += 4 + Strtab.size(); 1129 FileSize = alignTo(FileOff, Config->FileAlign); 1130 } 1131 1132 void Writer::mergeSections() { 1133 if (!PdataSec->Chunks.empty()) { 1134 FirstPdata = PdataSec->Chunks.front(); 1135 LastPdata = PdataSec->Chunks.back(); 1136 } 1137 1138 for (auto &P : Config->Merge) { 1139 StringRef ToName = P.second; 1140 if (P.first == ToName) 1141 continue; 1142 StringSet<> Names; 1143 while (1) { 1144 if (!Names.insert(ToName).second) 1145 fatal("/merge: cycle found for section '" + P.first + "'"); 1146 auto I = Config->Merge.find(ToName); 1147 if (I == Config->Merge.end()) 1148 break; 1149 ToName = I->second; 1150 } 1151 OutputSection *From = findSection(P.first); 1152 OutputSection *To = findSection(ToName); 1153 if (!From) 1154 continue; 1155 if (!To) { 1156 From->Name = ToName; 1157 continue; 1158 } 1159 To->merge(From); 1160 } 1161 } 1162 1163 // Visits all sections to assign incremental, non-overlapping RVAs and 1164 // file offsets. 1165 void Writer::assignAddresses() { 1166 SizeOfHeaders = DOSStubSize + sizeof(PEMagic) + sizeof(coff_file_header) + 1167 sizeof(data_directory) * NumberOfDataDirectory + 1168 sizeof(coff_section) * OutputSections.size(); 1169 SizeOfHeaders += 1170 Config->is64() ? sizeof(pe32plus_header) : sizeof(pe32_header); 1171 SizeOfHeaders = alignTo(SizeOfHeaders, Config->FileAlign); 1172 uint64_t RVA = PageSize; // The first page is kept unmapped. 1173 FileSize = SizeOfHeaders; 1174 1175 for (OutputSection *Sec : OutputSections) { 1176 if (Sec == RelocSec) 1177 addBaserels(); 1178 uint64_t RawSize = 0, VirtualSize = 0; 1179 Sec->Header.VirtualAddress = RVA; 1180 1181 // If /FUNCTIONPADMIN is used, functions are padded in order to create a 1182 // hotpatchable image. 1183 const bool IsCodeSection = 1184 (Sec->Header.Characteristics & IMAGE_SCN_CNT_CODE) && 1185 (Sec->Header.Characteristics & IMAGE_SCN_MEM_READ) && 1186 (Sec->Header.Characteristics & IMAGE_SCN_MEM_EXECUTE); 1187 uint32_t Padding = IsCodeSection ? Config->FunctionPadMin : 0; 1188 1189 for (Chunk *C : Sec->Chunks) { 1190 if (Padding && C->isHotPatchable()) 1191 VirtualSize += Padding; 1192 VirtualSize = alignTo(VirtualSize, C->getAlignment()); 1193 C->setRVA(RVA + VirtualSize); 1194 VirtualSize += C->getSize(); 1195 if (C->hasData()) 1196 RawSize = alignTo(VirtualSize, Config->FileAlign); 1197 } 1198 if (VirtualSize > UINT32_MAX) 1199 error("section larger than 4 GiB: " + Sec->Name); 1200 Sec->Header.VirtualSize = VirtualSize; 1201 Sec->Header.SizeOfRawData = RawSize; 1202 if (RawSize != 0) 1203 Sec->Header.PointerToRawData = FileSize; 1204 RVA += alignTo(VirtualSize, PageSize); 1205 FileSize += alignTo(RawSize, Config->FileAlign); 1206 } 1207 SizeOfImage = alignTo(RVA, PageSize); 1208 1209 // Assign addresses to sections in MergeChunks. 1210 for (MergeChunk *MC : MergeChunk::Instances) 1211 if (MC) 1212 MC->assignSubsectionRVAs(); 1213 } 1214 1215 template <typename PEHeaderTy> void Writer::writeHeader() { 1216 // Write DOS header. For backwards compatibility, the first part of a PE/COFF 1217 // executable consists of an MS-DOS MZ executable. If the executable is run 1218 // under DOS, that program gets run (usually to just print an error message). 1219 // When run under Windows, the loader looks at AddressOfNewExeHeader and uses 1220 // the PE header instead. 1221 uint8_t *Buf = Buffer->getBufferStart(); 1222 auto *DOS = reinterpret_cast<dos_header *>(Buf); 1223 Buf += sizeof(dos_header); 1224 DOS->Magic[0] = 'M'; 1225 DOS->Magic[1] = 'Z'; 1226 DOS->UsedBytesInTheLastPage = DOSStubSize % 512; 1227 DOS->FileSizeInPages = divideCeil(DOSStubSize, 512); 1228 DOS->HeaderSizeInParagraphs = sizeof(dos_header) / 16; 1229 1230 DOS->AddressOfRelocationTable = sizeof(dos_header); 1231 DOS->AddressOfNewExeHeader = DOSStubSize; 1232 1233 // Write DOS program. 1234 memcpy(Buf, DOSProgram, sizeof(DOSProgram)); 1235 Buf += sizeof(DOSProgram); 1236 1237 // Write PE magic 1238 memcpy(Buf, PEMagic, sizeof(PEMagic)); 1239 Buf += sizeof(PEMagic); 1240 1241 // Write COFF header 1242 auto *COFF = reinterpret_cast<coff_file_header *>(Buf); 1243 Buf += sizeof(*COFF); 1244 COFF->Machine = Config->Machine; 1245 COFF->NumberOfSections = OutputSections.size(); 1246 COFF->Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE; 1247 if (Config->LargeAddressAware) 1248 COFF->Characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE; 1249 if (!Config->is64()) 1250 COFF->Characteristics |= IMAGE_FILE_32BIT_MACHINE; 1251 if (Config->DLL) 1252 COFF->Characteristics |= IMAGE_FILE_DLL; 1253 if (!Config->Relocatable) 1254 COFF->Characteristics |= IMAGE_FILE_RELOCS_STRIPPED; 1255 if (Config->SwaprunCD) 1256 COFF->Characteristics |= IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP; 1257 if (Config->SwaprunNet) 1258 COFF->Characteristics |= IMAGE_FILE_NET_RUN_FROM_SWAP; 1259 COFF->SizeOfOptionalHeader = 1260 sizeof(PEHeaderTy) + sizeof(data_directory) * NumberOfDataDirectory; 1261 1262 // Write PE header 1263 auto *PE = reinterpret_cast<PEHeaderTy *>(Buf); 1264 Buf += sizeof(*PE); 1265 PE->Magic = Config->is64() ? PE32Header::PE32_PLUS : PE32Header::PE32; 1266 1267 // If {Major,Minor}LinkerVersion is left at 0.0, then for some 1268 // reason signing the resulting PE file with Authenticode produces a 1269 // signature that fails to validate on Windows 7 (but is OK on 10). 1270 // Set it to 14.0, which is what VS2015 outputs, and which avoids 1271 // that problem. 1272 PE->MajorLinkerVersion = 14; 1273 PE->MinorLinkerVersion = 0; 1274 1275 PE->ImageBase = Config->ImageBase; 1276 PE->SectionAlignment = PageSize; 1277 PE->FileAlignment = Config->FileAlign; 1278 PE->MajorImageVersion = Config->MajorImageVersion; 1279 PE->MinorImageVersion = Config->MinorImageVersion; 1280 PE->MajorOperatingSystemVersion = Config->MajorOSVersion; 1281 PE->MinorOperatingSystemVersion = Config->MinorOSVersion; 1282 PE->MajorSubsystemVersion = Config->MajorOSVersion; 1283 PE->MinorSubsystemVersion = Config->MinorOSVersion; 1284 PE->Subsystem = Config->Subsystem; 1285 PE->SizeOfImage = SizeOfImage; 1286 PE->SizeOfHeaders = SizeOfHeaders; 1287 if (!Config->NoEntry) { 1288 Defined *Entry = cast<Defined>(Config->Entry); 1289 PE->AddressOfEntryPoint = Entry->getRVA(); 1290 // Pointer to thumb code must have the LSB set, so adjust it. 1291 if (Config->Machine == ARMNT) 1292 PE->AddressOfEntryPoint |= 1; 1293 } 1294 PE->SizeOfStackReserve = Config->StackReserve; 1295 PE->SizeOfStackCommit = Config->StackCommit; 1296 PE->SizeOfHeapReserve = Config->HeapReserve; 1297 PE->SizeOfHeapCommit = Config->HeapCommit; 1298 if (Config->AppContainer) 1299 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_APPCONTAINER; 1300 if (Config->DynamicBase) 1301 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE; 1302 if (Config->HighEntropyVA) 1303 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA; 1304 if (!Config->AllowBind) 1305 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_BIND; 1306 if (Config->NxCompat) 1307 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NX_COMPAT; 1308 if (!Config->AllowIsolation) 1309 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION; 1310 if (Config->GuardCF != GuardCFLevel::Off) 1311 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_GUARD_CF; 1312 if (Config->IntegrityCheck) 1313 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY; 1314 if (SetNoSEHCharacteristic) 1315 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_NO_SEH; 1316 if (Config->TerminalServerAware) 1317 PE->DLLCharacteristics |= IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE; 1318 PE->NumberOfRvaAndSize = NumberOfDataDirectory; 1319 if (TextSec->getVirtualSize()) { 1320 PE->BaseOfCode = TextSec->getRVA(); 1321 PE->SizeOfCode = TextSec->getRawSize(); 1322 } 1323 PE->SizeOfInitializedData = getSizeOfInitializedData(); 1324 1325 // Write data directory 1326 auto *Dir = reinterpret_cast<data_directory *>(Buf); 1327 Buf += sizeof(*Dir) * NumberOfDataDirectory; 1328 if (!Config->Exports.empty()) { 1329 Dir[EXPORT_TABLE].RelativeVirtualAddress = Edata.getRVA(); 1330 Dir[EXPORT_TABLE].Size = Edata.getSize(); 1331 } 1332 if (ImportTableStart) { 1333 Dir[IMPORT_TABLE].RelativeVirtualAddress = ImportTableStart->getRVA(); 1334 Dir[IMPORT_TABLE].Size = ImportTableSize; 1335 } 1336 if (IATStart) { 1337 Dir[IAT].RelativeVirtualAddress = IATStart->getRVA(); 1338 Dir[IAT].Size = IATSize; 1339 } 1340 if (RsrcSec->getVirtualSize()) { 1341 Dir[RESOURCE_TABLE].RelativeVirtualAddress = RsrcSec->getRVA(); 1342 Dir[RESOURCE_TABLE].Size = RsrcSec->getVirtualSize(); 1343 } 1344 if (FirstPdata) { 1345 Dir[EXCEPTION_TABLE].RelativeVirtualAddress = FirstPdata->getRVA(); 1346 Dir[EXCEPTION_TABLE].Size = 1347 LastPdata->getRVA() + LastPdata->getSize() - FirstPdata->getRVA(); 1348 } 1349 if (RelocSec->getVirtualSize()) { 1350 Dir[BASE_RELOCATION_TABLE].RelativeVirtualAddress = RelocSec->getRVA(); 1351 Dir[BASE_RELOCATION_TABLE].Size = RelocSec->getVirtualSize(); 1352 } 1353 if (Symbol *Sym = Symtab->findUnderscore("_tls_used")) { 1354 if (Defined *B = dyn_cast<Defined>(Sym)) { 1355 Dir[TLS_TABLE].RelativeVirtualAddress = B->getRVA(); 1356 Dir[TLS_TABLE].Size = Config->is64() 1357 ? sizeof(object::coff_tls_directory64) 1358 : sizeof(object::coff_tls_directory32); 1359 } 1360 } 1361 if (DebugDirectory) { 1362 Dir[DEBUG_DIRECTORY].RelativeVirtualAddress = DebugDirectory->getRVA(); 1363 Dir[DEBUG_DIRECTORY].Size = DebugDirectory->getSize(); 1364 } 1365 if (Symbol *Sym = Symtab->findUnderscore("_load_config_used")) { 1366 if (auto *B = dyn_cast<DefinedRegular>(Sym)) { 1367 SectionChunk *SC = B->getChunk(); 1368 assert(B->getRVA() >= SC->getRVA()); 1369 uint64_t OffsetInChunk = B->getRVA() - SC->getRVA(); 1370 if (!SC->hasData() || OffsetInChunk + 4 > SC->getSize()) 1371 fatal("_load_config_used is malformed"); 1372 1373 ArrayRef<uint8_t> SecContents = SC->getContents(); 1374 uint32_t LoadConfigSize = 1375 *reinterpret_cast<const ulittle32_t *>(&SecContents[OffsetInChunk]); 1376 if (OffsetInChunk + LoadConfigSize > SC->getSize()) 1377 fatal("_load_config_used is too large"); 1378 Dir[LOAD_CONFIG_TABLE].RelativeVirtualAddress = B->getRVA(); 1379 Dir[LOAD_CONFIG_TABLE].Size = LoadConfigSize; 1380 } 1381 } 1382 if (!DelayIdata.empty()) { 1383 Dir[DELAY_IMPORT_DESCRIPTOR].RelativeVirtualAddress = 1384 DelayIdata.getDirRVA(); 1385 Dir[DELAY_IMPORT_DESCRIPTOR].Size = DelayIdata.getDirSize(); 1386 } 1387 1388 // Write section table 1389 for (OutputSection *Sec : OutputSections) { 1390 Sec->writeHeaderTo(Buf); 1391 Buf += sizeof(coff_section); 1392 } 1393 SectionTable = ArrayRef<uint8_t>( 1394 Buf - OutputSections.size() * sizeof(coff_section), Buf); 1395 1396 if (OutputSymtab.empty() && Strtab.empty()) 1397 return; 1398 1399 COFF->PointerToSymbolTable = PointerToSymbolTable; 1400 uint32_t NumberOfSymbols = OutputSymtab.size(); 1401 COFF->NumberOfSymbols = NumberOfSymbols; 1402 auto *SymbolTable = reinterpret_cast<coff_symbol16 *>( 1403 Buffer->getBufferStart() + COFF->PointerToSymbolTable); 1404 for (size_t I = 0; I != NumberOfSymbols; ++I) 1405 SymbolTable[I] = OutputSymtab[I]; 1406 // Create the string table, it follows immediately after the symbol table. 1407 // The first 4 bytes is length including itself. 1408 Buf = reinterpret_cast<uint8_t *>(&SymbolTable[NumberOfSymbols]); 1409 write32le(Buf, Strtab.size() + 4); 1410 if (!Strtab.empty()) 1411 memcpy(Buf + 4, Strtab.data(), Strtab.size()); 1412 } 1413 1414 void Writer::openFile(StringRef Path) { 1415 Buffer = CHECK( 1416 FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable), 1417 "failed to open " + Path); 1418 } 1419 1420 void Writer::createSEHTable() { 1421 // Set the no SEH characteristic on x86 binaries unless we find exception 1422 // handlers. 1423 SetNoSEHCharacteristic = true; 1424 1425 SymbolRVASet Handlers; 1426 for (ObjFile *File : ObjFile::Instances) { 1427 // FIXME: We should error here instead of earlier unless /safeseh:no was 1428 // passed. 1429 if (!File->hasSafeSEH()) 1430 return; 1431 1432 markSymbolsForRVATable(File, File->getSXDataChunks(), Handlers); 1433 } 1434 1435 // Remove the "no SEH" characteristic if all object files were built with 1436 // safeseh, we found some exception handlers, and there is a load config in 1437 // the object. 1438 SetNoSEHCharacteristic = 1439 Handlers.empty() || !Symtab->findUnderscore("_load_config_used"); 1440 1441 maybeAddRVATable(std::move(Handlers), "__safe_se_handler_table", 1442 "__safe_se_handler_count"); 1443 } 1444 1445 // Add a symbol to an RVA set. Two symbols may have the same RVA, but an RVA set 1446 // cannot contain duplicates. Therefore, the set is uniqued by Chunk and the 1447 // symbol's offset into that Chunk. 1448 static void addSymbolToRVASet(SymbolRVASet &RVASet, Defined *S) { 1449 Chunk *C = S->getChunk(); 1450 if (auto *SC = dyn_cast<SectionChunk>(C)) 1451 C = SC->Repl; // Look through ICF replacement. 1452 uint32_t Off = S->getRVA() - (C ? C->getRVA() : 0); 1453 RVASet.insert({C, Off}); 1454 } 1455 1456 // Given a symbol, add it to the GFIDs table if it is a live, defined, function 1457 // symbol in an executable section. 1458 static void maybeAddAddressTakenFunction(SymbolRVASet &AddressTakenSyms, 1459 Symbol *S) { 1460 if (!S) 1461 return; 1462 1463 switch (S->kind()) { 1464 case Symbol::DefinedLocalImportKind: 1465 case Symbol::DefinedImportDataKind: 1466 // Defines an __imp_ pointer, so it is data, so it is ignored. 1467 break; 1468 case Symbol::DefinedCommonKind: 1469 // Common is always data, so it is ignored. 1470 break; 1471 case Symbol::DefinedAbsoluteKind: 1472 case Symbol::DefinedSyntheticKind: 1473 // Absolute is never code, synthetic generally isn't and usually isn't 1474 // determinable. 1475 break; 1476 case Symbol::LazyKind: 1477 case Symbol::UndefinedKind: 1478 // Undefined symbols resolve to zero, so they don't have an RVA. Lazy 1479 // symbols shouldn't have relocations. 1480 break; 1481 1482 case Symbol::DefinedImportThunkKind: 1483 // Thunks are always code, include them. 1484 addSymbolToRVASet(AddressTakenSyms, cast<Defined>(S)); 1485 break; 1486 1487 case Symbol::DefinedRegularKind: { 1488 // This is a regular, defined, symbol from a COFF file. Mark the symbol as 1489 // address taken if the symbol type is function and it's in an executable 1490 // section. 1491 auto *D = cast<DefinedRegular>(S); 1492 if (D->getCOFFSymbol().getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) { 1493 SectionChunk *SC = dyn_cast<SectionChunk>(D->getChunk()); 1494 if (SC && SC->Live && 1495 SC->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) 1496 addSymbolToRVASet(AddressTakenSyms, D); 1497 } 1498 break; 1499 } 1500 } 1501 } 1502 1503 // Visit all relocations from all section contributions of this object file and 1504 // mark the relocation target as address-taken. 1505 static void markSymbolsWithRelocations(ObjFile *File, 1506 SymbolRVASet &UsedSymbols) { 1507 for (Chunk *C : File->getChunks()) { 1508 // We only care about live section chunks. Common chunks and other chunks 1509 // don't generally contain relocations. 1510 SectionChunk *SC = dyn_cast<SectionChunk>(C); 1511 if (!SC || !SC->Live) 1512 continue; 1513 1514 for (const coff_relocation &Reloc : SC->getRelocs()) { 1515 if (Config->Machine == I386 && Reloc.Type == COFF::IMAGE_REL_I386_REL32) 1516 // Ignore relative relocations on x86. On x86_64 they can't be ignored 1517 // since they're also used to compute absolute addresses. 1518 continue; 1519 1520 Symbol *Ref = SC->File->getSymbol(Reloc.SymbolTableIndex); 1521 maybeAddAddressTakenFunction(UsedSymbols, Ref); 1522 } 1523 } 1524 } 1525 1526 // Create the guard function id table. This is a table of RVAs of all 1527 // address-taken functions. It is sorted and uniqued, just like the safe SEH 1528 // table. 1529 void Writer::createGuardCFTables() { 1530 SymbolRVASet AddressTakenSyms; 1531 SymbolRVASet LongJmpTargets; 1532 for (ObjFile *File : ObjFile::Instances) { 1533 // If the object was compiled with /guard:cf, the address taken symbols 1534 // are in .gfids$y sections, and the longjmp targets are in .gljmp$y 1535 // sections. If the object was not compiled with /guard:cf, we assume there 1536 // were no setjmp targets, and that all code symbols with relocations are 1537 // possibly address-taken. 1538 if (File->hasGuardCF()) { 1539 markSymbolsForRVATable(File, File->getGuardFidChunks(), AddressTakenSyms); 1540 markSymbolsForRVATable(File, File->getGuardLJmpChunks(), LongJmpTargets); 1541 } else { 1542 markSymbolsWithRelocations(File, AddressTakenSyms); 1543 } 1544 } 1545 1546 // Mark the image entry as address-taken. 1547 if (Config->Entry) 1548 maybeAddAddressTakenFunction(AddressTakenSyms, Config->Entry); 1549 1550 // Mark exported symbols in executable sections as address-taken. 1551 for (Export &E : Config->Exports) 1552 maybeAddAddressTakenFunction(AddressTakenSyms, E.Sym); 1553 1554 // Ensure sections referenced in the gfid table are 16-byte aligned. 1555 for (const ChunkAndOffset &C : AddressTakenSyms) 1556 if (C.InputChunk->getAlignment() < 16) 1557 C.InputChunk->setAlignment(16); 1558 1559 maybeAddRVATable(std::move(AddressTakenSyms), "__guard_fids_table", 1560 "__guard_fids_count"); 1561 1562 // Add the longjmp target table unless the user told us not to. 1563 if (Config->GuardCF == GuardCFLevel::Full) 1564 maybeAddRVATable(std::move(LongJmpTargets), "__guard_longjmp_table", 1565 "__guard_longjmp_count"); 1566 1567 // Set __guard_flags, which will be used in the load config to indicate that 1568 // /guard:cf was enabled. 1569 uint32_t GuardFlags = uint32_t(coff_guard_flags::CFInstrumented) | 1570 uint32_t(coff_guard_flags::HasFidTable); 1571 if (Config->GuardCF == GuardCFLevel::Full) 1572 GuardFlags |= uint32_t(coff_guard_flags::HasLongJmpTable); 1573 Symbol *FlagSym = Symtab->findUnderscore("__guard_flags"); 1574 cast<DefinedAbsolute>(FlagSym)->setVA(GuardFlags); 1575 } 1576 1577 // Take a list of input sections containing symbol table indices and add those 1578 // symbols to an RVA table. The challenge is that symbol RVAs are not known and 1579 // depend on the table size, so we can't directly build a set of integers. 1580 void Writer::markSymbolsForRVATable(ObjFile *File, 1581 ArrayRef<SectionChunk *> SymIdxChunks, 1582 SymbolRVASet &TableSymbols) { 1583 for (SectionChunk *C : SymIdxChunks) { 1584 // Skip sections discarded by linker GC. This comes up when a .gfids section 1585 // is associated with something like a vtable and the vtable is discarded. 1586 // In this case, the associated gfids section is discarded, and we don't 1587 // mark the virtual member functions as address-taken by the vtable. 1588 if (!C->Live) 1589 continue; 1590 1591 // Validate that the contents look like symbol table indices. 1592 ArrayRef<uint8_t> Data = C->getContents(); 1593 if (Data.size() % 4 != 0) { 1594 warn("ignoring " + C->getSectionName() + 1595 " symbol table index section in object " + toString(File)); 1596 continue; 1597 } 1598 1599 // Read each symbol table index and check if that symbol was included in the 1600 // final link. If so, add it to the table symbol set. 1601 ArrayRef<ulittle32_t> SymIndices( 1602 reinterpret_cast<const ulittle32_t *>(Data.data()), Data.size() / 4); 1603 ArrayRef<Symbol *> ObjSymbols = File->getSymbols(); 1604 for (uint32_t SymIndex : SymIndices) { 1605 if (SymIndex >= ObjSymbols.size()) { 1606 warn("ignoring invalid symbol table index in section " + 1607 C->getSectionName() + " in object " + toString(File)); 1608 continue; 1609 } 1610 if (Symbol *S = ObjSymbols[SymIndex]) { 1611 if (S->isLive()) 1612 addSymbolToRVASet(TableSymbols, cast<Defined>(S)); 1613 } 1614 } 1615 } 1616 } 1617 1618 // Replace the absolute table symbol with a synthetic symbol pointing to 1619 // TableChunk so that we can emit base relocations for it and resolve section 1620 // relative relocations. 1621 void Writer::maybeAddRVATable(SymbolRVASet TableSymbols, StringRef TableSym, 1622 StringRef CountSym) { 1623 if (TableSymbols.empty()) 1624 return; 1625 1626 RVATableChunk *TableChunk = make<RVATableChunk>(std::move(TableSymbols)); 1627 RdataSec->addChunk(TableChunk); 1628 1629 Symbol *T = Symtab->findUnderscore(TableSym); 1630 Symbol *C = Symtab->findUnderscore(CountSym); 1631 replaceSymbol<DefinedSynthetic>(T, T->getName(), TableChunk); 1632 cast<DefinedAbsolute>(C)->setVA(TableChunk->getSize() / 4); 1633 } 1634 1635 // MinGW specific. Gather all relocations that are imported from a DLL even 1636 // though the code didn't expect it to, produce the table that the runtime 1637 // uses for fixing them up, and provide the synthetic symbols that the 1638 // runtime uses for finding the table. 1639 void Writer::createRuntimePseudoRelocs() { 1640 std::vector<RuntimePseudoReloc> Rels; 1641 1642 for (Chunk *C : Symtab->getChunks()) { 1643 auto *SC = dyn_cast<SectionChunk>(C); 1644 if (!SC || !SC->Live) 1645 continue; 1646 SC->getRuntimePseudoRelocs(Rels); 1647 } 1648 1649 if (!Rels.empty()) 1650 log("Writing " + Twine(Rels.size()) + " runtime pseudo relocations"); 1651 PseudoRelocTableChunk *Table = make<PseudoRelocTableChunk>(Rels); 1652 RdataSec->addChunk(Table); 1653 EmptyChunk *EndOfList = make<EmptyChunk>(); 1654 RdataSec->addChunk(EndOfList); 1655 1656 Symbol *HeadSym = Symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__"); 1657 Symbol *EndSym = Symtab->findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__"); 1658 replaceSymbol<DefinedSynthetic>(HeadSym, HeadSym->getName(), Table); 1659 replaceSymbol<DefinedSynthetic>(EndSym, EndSym->getName(), EndOfList); 1660 } 1661 1662 // MinGW specific. 1663 // The MinGW .ctors and .dtors lists have sentinels at each end; 1664 // a (uintptr_t)-1 at the start and a (uintptr_t)0 at the end. 1665 // There's a symbol pointing to the start sentinel pointer, __CTOR_LIST__ 1666 // and __DTOR_LIST__ respectively. 1667 void Writer::insertCtorDtorSymbols() { 1668 AbsolutePointerChunk *CtorListHead = make<AbsolutePointerChunk>(-1); 1669 AbsolutePointerChunk *CtorListEnd = make<AbsolutePointerChunk>(0); 1670 AbsolutePointerChunk *DtorListHead = make<AbsolutePointerChunk>(-1); 1671 AbsolutePointerChunk *DtorListEnd = make<AbsolutePointerChunk>(0); 1672 CtorsSec->insertChunkAtStart(CtorListHead); 1673 CtorsSec->addChunk(CtorListEnd); 1674 DtorsSec->insertChunkAtStart(DtorListHead); 1675 DtorsSec->addChunk(DtorListEnd); 1676 1677 Symbol *CtorListSym = Symtab->findUnderscore("__CTOR_LIST__"); 1678 Symbol *DtorListSym = Symtab->findUnderscore("__DTOR_LIST__"); 1679 replaceSymbol<DefinedSynthetic>(CtorListSym, CtorListSym->getName(), 1680 CtorListHead); 1681 replaceSymbol<DefinedSynthetic>(DtorListSym, DtorListSym->getName(), 1682 DtorListHead); 1683 } 1684 1685 // Handles /section options to allow users to overwrite 1686 // section attributes. 1687 void Writer::setSectionPermissions() { 1688 for (auto &P : Config->Section) { 1689 StringRef Name = P.first; 1690 uint32_t Perm = P.second; 1691 for (OutputSection *Sec : OutputSections) 1692 if (Sec->Name == Name) 1693 Sec->setPermissions(Perm); 1694 } 1695 } 1696 1697 // Write section contents to a mmap'ed file. 1698 void Writer::writeSections() { 1699 // Record the number of sections to apply section index relocations 1700 // against absolute symbols. See applySecIdx in Chunks.cpp.. 1701 DefinedAbsolute::NumOutputSections = OutputSections.size(); 1702 1703 uint8_t *Buf = Buffer->getBufferStart(); 1704 for (OutputSection *Sec : OutputSections) { 1705 uint8_t *SecBuf = Buf + Sec->getFileOff(); 1706 // Fill gaps between functions in .text with INT3 instructions 1707 // instead of leaving as NUL bytes (which can be interpreted as 1708 // ADD instructions). 1709 if (Sec->Header.Characteristics & IMAGE_SCN_CNT_CODE) 1710 memset(SecBuf, 0xCC, Sec->getRawSize()); 1711 parallelForEach(Sec->Chunks, [&](Chunk *C) { 1712 C->writeTo(SecBuf + C->getRVA() - Sec->getRVA()); 1713 }); 1714 } 1715 } 1716 1717 void Writer::writeBuildId() { 1718 // There are two important parts to the build ID. 1719 // 1) If building with debug info, the COFF debug directory contains a 1720 // timestamp as well as a Guid and Age of the PDB. 1721 // 2) In all cases, the PE COFF file header also contains a timestamp. 1722 // For reproducibility, instead of a timestamp we want to use a hash of the 1723 // PE contents. 1724 if (Config->Debug) { 1725 assert(BuildId && "BuildId is not set!"); 1726 // BuildId->BuildId was filled in when the PDB was written. 1727 } 1728 1729 // At this point the only fields in the COFF file which remain unset are the 1730 // "timestamp" in the COFF file header, and the ones in the coff debug 1731 // directory. Now we can hash the file and write that hash to the various 1732 // timestamp fields in the file. 1733 StringRef OutputFileData( 1734 reinterpret_cast<const char *>(Buffer->getBufferStart()), 1735 Buffer->getBufferSize()); 1736 1737 uint32_t Timestamp = Config->Timestamp; 1738 uint64_t Hash = 0; 1739 bool GenerateSyntheticBuildId = 1740 Config->MinGW && Config->Debug && Config->PDBPath.empty(); 1741 1742 if (Config->Repro || GenerateSyntheticBuildId) 1743 Hash = xxHash64(OutputFileData); 1744 1745 if (Config->Repro) 1746 Timestamp = static_cast<uint32_t>(Hash); 1747 1748 if (GenerateSyntheticBuildId) { 1749 // For MinGW builds without a PDB file, we still generate a build id 1750 // to allow associating a crash dump to the executable. 1751 BuildId->BuildId->PDB70.CVSignature = OMF::Signature::PDB70; 1752 BuildId->BuildId->PDB70.Age = 1; 1753 memcpy(BuildId->BuildId->PDB70.Signature, &Hash, 8); 1754 // xxhash only gives us 8 bytes, so put some fixed data in the other half. 1755 memcpy(&BuildId->BuildId->PDB70.Signature[8], "LLD PDB.", 8); 1756 } 1757 1758 if (DebugDirectory) 1759 DebugDirectory->setTimeDateStamp(Timestamp); 1760 1761 uint8_t *Buf = Buffer->getBufferStart(); 1762 Buf += DOSStubSize + sizeof(PEMagic); 1763 object::coff_file_header *CoffHeader = 1764 reinterpret_cast<coff_file_header *>(Buf); 1765 CoffHeader->TimeDateStamp = Timestamp; 1766 } 1767 1768 // Sort .pdata section contents according to PE/COFF spec 5.5. 1769 void Writer::sortExceptionTable() { 1770 if (!FirstPdata) 1771 return; 1772 // We assume .pdata contains function table entries only. 1773 auto BufAddr = [&](Chunk *C) { 1774 OutputSection *OS = C->getOutputSection(); 1775 return Buffer->getBufferStart() + OS->getFileOff() + C->getRVA() - 1776 OS->getRVA(); 1777 }; 1778 uint8_t *Begin = BufAddr(FirstPdata); 1779 uint8_t *End = BufAddr(LastPdata) + LastPdata->getSize(); 1780 if (Config->Machine == AMD64) { 1781 struct Entry { ulittle32_t Begin, End, Unwind; }; 1782 parallelSort( 1783 MutableArrayRef<Entry>((Entry *)Begin, (Entry *)End), 1784 [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; }); 1785 return; 1786 } 1787 if (Config->Machine == ARMNT || Config->Machine == ARM64) { 1788 struct Entry { ulittle32_t Begin, Unwind; }; 1789 parallelSort( 1790 MutableArrayRef<Entry>((Entry *)Begin, (Entry *)End), 1791 [](const Entry &A, const Entry &B) { return A.Begin < B.Begin; }); 1792 return; 1793 } 1794 errs() << "warning: don't know how to handle .pdata.\n"; 1795 } 1796 1797 // The CRT section contains, among other things, the array of function 1798 // pointers that initialize every global variable that is not trivially 1799 // constructed. The CRT calls them one after the other prior to invoking 1800 // main(). 1801 // 1802 // As per C++ spec, 3.6.2/2.3, 1803 // "Variables with ordered initialization defined within a single 1804 // translation unit shall be initialized in the order of their definitions 1805 // in the translation unit" 1806 // 1807 // It is therefore critical to sort the chunks containing the function 1808 // pointers in the order that they are listed in the object file (top to 1809 // bottom), otherwise global objects might not be initialized in the 1810 // correct order. 1811 void Writer::sortCRTSectionChunks(std::vector<Chunk *> &Chunks) { 1812 auto SectionChunkOrder = [](const Chunk *A, const Chunk *B) { 1813 auto SA = dyn_cast<SectionChunk>(A); 1814 auto SB = dyn_cast<SectionChunk>(B); 1815 assert(SA && SB && "Non-section chunks in CRT section!"); 1816 1817 StringRef SAObj = SA->File->MB.getBufferIdentifier(); 1818 StringRef SBObj = SB->File->MB.getBufferIdentifier(); 1819 1820 return SAObj == SBObj && SA->getSectionNumber() < SB->getSectionNumber(); 1821 }; 1822 llvm::stable_sort(Chunks, SectionChunkOrder); 1823 1824 if (Config->Verbose) { 1825 for (auto &C : Chunks) { 1826 auto SC = dyn_cast<SectionChunk>(C); 1827 log(" " + SC->File->MB.getBufferIdentifier().str() + 1828 ", SectionID: " + Twine(SC->getSectionNumber())); 1829 } 1830 } 1831 } 1832 1833 OutputSection *Writer::findSection(StringRef Name) { 1834 for (OutputSection *Sec : OutputSections) 1835 if (Sec->Name == Name) 1836 return Sec; 1837 return nullptr; 1838 } 1839 1840 uint32_t Writer::getSizeOfInitializedData() { 1841 uint32_t Res = 0; 1842 for (OutputSection *S : OutputSections) 1843 if (S->Header.Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) 1844 Res += S->getRawSize(); 1845 return Res; 1846 } 1847 1848 // Add base relocations to .reloc section. 1849 void Writer::addBaserels() { 1850 if (!Config->Relocatable) 1851 return; 1852 RelocSec->Chunks.clear(); 1853 std::vector<Baserel> V; 1854 for (OutputSection *Sec : OutputSections) { 1855 if (Sec->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) 1856 continue; 1857 // Collect all locations for base relocations. 1858 for (Chunk *C : Sec->Chunks) 1859 C->getBaserels(&V); 1860 // Add the addresses to .reloc section. 1861 if (!V.empty()) 1862 addBaserelBlocks(V); 1863 V.clear(); 1864 } 1865 } 1866 1867 // Add addresses to .reloc section. Note that addresses are grouped by page. 1868 void Writer::addBaserelBlocks(std::vector<Baserel> &V) { 1869 const uint32_t Mask = ~uint32_t(PageSize - 1); 1870 uint32_t Page = V[0].RVA & Mask; 1871 size_t I = 0, J = 1; 1872 for (size_t E = V.size(); J < E; ++J) { 1873 uint32_t P = V[J].RVA & Mask; 1874 if (P == Page) 1875 continue; 1876 RelocSec->addChunk(make<BaserelChunk>(Page, &V[I], &V[0] + J)); 1877 I = J; 1878 Page = P; 1879 } 1880 if (I == J) 1881 return; 1882 RelocSec->addChunk(make<BaserelChunk>(Page, &V[I], &V[0] + J)); 1883 } 1884 1885 PartialSection *Writer::createPartialSection(StringRef Name, 1886 uint32_t OutChars) { 1887 PartialSection *&PSec = PartialSections[{Name, OutChars}]; 1888 if (PSec) 1889 return PSec; 1890 PSec = make<PartialSection>(Name, OutChars); 1891 return PSec; 1892 } 1893 1894 PartialSection *Writer::findPartialSection(StringRef Name, uint32_t OutChars) { 1895 auto It = PartialSections.find({Name, OutChars}); 1896 if (It != PartialSections.end()) 1897 return It->second; 1898 return nullptr; 1899 } 1900