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