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