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