1 //===- Writer.cpp ---------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "Writer.h" 10 #include "Config.h" 11 #include "InputFiles.h" 12 #include "InputSection.h" 13 #include "MapFile.h" 14 #include "MergedOutputSection.h" 15 #include "OutputSection.h" 16 #include "OutputSegment.h" 17 #include "SymbolTable.h" 18 #include "Symbols.h" 19 #include "SyntheticSections.h" 20 #include "Target.h" 21 #include "UnwindInfoSection.h" 22 23 #include "lld/Common/Arrays.h" 24 #include "lld/Common/ErrorHandler.h" 25 #include "lld/Common/Memory.h" 26 #include "llvm/BinaryFormat/MachO.h" 27 #include "llvm/Config/llvm-config.h" 28 #include "llvm/Support/LEB128.h" 29 #include "llvm/Support/MathExtras.h" 30 #include "llvm/Support/Parallel.h" 31 #include "llvm/Support/Path.h" 32 #include "llvm/Support/TimeProfiler.h" 33 #include "llvm/Support/xxhash.h" 34 35 #include <algorithm> 36 37 using namespace llvm; 38 using namespace llvm::MachO; 39 using namespace llvm::sys; 40 using namespace lld; 41 using namespace lld::macho; 42 43 namespace { 44 class LCUuid; 45 46 class Writer { 47 public: 48 Writer() : buffer(errorHandler().outputBuffer) {} 49 50 void scanRelocations(); 51 void scanSymbols(); 52 template <class LP> void createOutputSections(); 53 template <class LP> void createLoadCommands(); 54 void finalizeAddresses(); 55 void finalizeLinkEditSegment(); 56 void assignAddresses(OutputSegment *); 57 58 void openFile(); 59 void writeSections(); 60 void writeUuid(); 61 void writeCodeSignature(); 62 void writeOutputFile(); 63 64 template <class LP> void run(); 65 66 std::unique_ptr<FileOutputBuffer> &buffer; 67 uint64_t addr = 0; 68 uint64_t fileOff = 0; 69 MachHeaderSection *header = nullptr; 70 StringTableSection *stringTableSection = nullptr; 71 SymtabSection *symtabSection = nullptr; 72 IndirectSymtabSection *indirectSymtabSection = nullptr; 73 CodeSignatureSection *codeSignatureSection = nullptr; 74 FunctionStartsSection *functionStartsSection = nullptr; 75 76 LCUuid *uuidCommand = nullptr; 77 OutputSegment *linkEditSegment = nullptr; 78 }; 79 80 // LC_DYLD_INFO_ONLY stores the offsets of symbol import/export information. 81 class LCDyldInfo : public LoadCommand { 82 public: 83 LCDyldInfo(RebaseSection *rebaseSection, BindingSection *bindingSection, 84 WeakBindingSection *weakBindingSection, 85 LazyBindingSection *lazyBindingSection, 86 ExportSection *exportSection) 87 : rebaseSection(rebaseSection), bindingSection(bindingSection), 88 weakBindingSection(weakBindingSection), 89 lazyBindingSection(lazyBindingSection), exportSection(exportSection) {} 90 91 uint32_t getSize() const override { return sizeof(dyld_info_command); } 92 93 void writeTo(uint8_t *buf) const override { 94 auto *c = reinterpret_cast<dyld_info_command *>(buf); 95 c->cmd = LC_DYLD_INFO_ONLY; 96 c->cmdsize = getSize(); 97 if (rebaseSection->isNeeded()) { 98 c->rebase_off = rebaseSection->fileOff; 99 c->rebase_size = rebaseSection->getFileSize(); 100 } 101 if (bindingSection->isNeeded()) { 102 c->bind_off = bindingSection->fileOff; 103 c->bind_size = bindingSection->getFileSize(); 104 } 105 if (weakBindingSection->isNeeded()) { 106 c->weak_bind_off = weakBindingSection->fileOff; 107 c->weak_bind_size = weakBindingSection->getFileSize(); 108 } 109 if (lazyBindingSection->isNeeded()) { 110 c->lazy_bind_off = lazyBindingSection->fileOff; 111 c->lazy_bind_size = lazyBindingSection->getFileSize(); 112 } 113 if (exportSection->isNeeded()) { 114 c->export_off = exportSection->fileOff; 115 c->export_size = exportSection->getFileSize(); 116 } 117 } 118 119 RebaseSection *rebaseSection; 120 BindingSection *bindingSection; 121 WeakBindingSection *weakBindingSection; 122 LazyBindingSection *lazyBindingSection; 123 ExportSection *exportSection; 124 }; 125 126 class LCFunctionStarts : public LoadCommand { 127 public: 128 explicit LCFunctionStarts(FunctionStartsSection *functionStartsSection) 129 : functionStartsSection(functionStartsSection) {} 130 131 uint32_t getSize() const override { return sizeof(linkedit_data_command); } 132 133 void writeTo(uint8_t *buf) const override { 134 auto *c = reinterpret_cast<linkedit_data_command *>(buf); 135 c->cmd = LC_FUNCTION_STARTS; 136 c->cmdsize = getSize(); 137 c->dataoff = functionStartsSection->fileOff; 138 c->datasize = functionStartsSection->getFileSize(); 139 } 140 141 private: 142 FunctionStartsSection *functionStartsSection; 143 }; 144 145 class LCDysymtab : public LoadCommand { 146 public: 147 LCDysymtab(SymtabSection *symtabSection, 148 IndirectSymtabSection *indirectSymtabSection) 149 : symtabSection(symtabSection), 150 indirectSymtabSection(indirectSymtabSection) {} 151 152 uint32_t getSize() const override { return sizeof(dysymtab_command); } 153 154 void writeTo(uint8_t *buf) const override { 155 auto *c = reinterpret_cast<dysymtab_command *>(buf); 156 c->cmd = LC_DYSYMTAB; 157 c->cmdsize = getSize(); 158 159 c->ilocalsym = 0; 160 c->iextdefsym = c->nlocalsym = symtabSection->getNumLocalSymbols(); 161 c->nextdefsym = symtabSection->getNumExternalSymbols(); 162 c->iundefsym = c->iextdefsym + c->nextdefsym; 163 c->nundefsym = symtabSection->getNumUndefinedSymbols(); 164 165 c->indirectsymoff = indirectSymtabSection->fileOff; 166 c->nindirectsyms = indirectSymtabSection->getNumSymbols(); 167 } 168 169 SymtabSection *symtabSection; 170 IndirectSymtabSection *indirectSymtabSection; 171 }; 172 173 template <class LP> class LCSegment : public LoadCommand { 174 public: 175 LCSegment(StringRef name, OutputSegment *seg) : name(name), seg(seg) {} 176 177 uint32_t getSize() const override { 178 return sizeof(typename LP::segment_command) + 179 seg->numNonHiddenSections() * sizeof(typename LP::section); 180 } 181 182 void writeTo(uint8_t *buf) const override { 183 using SegmentCommand = typename LP::segment_command; 184 using Section = typename LP::section; 185 186 auto *c = reinterpret_cast<SegmentCommand *>(buf); 187 buf += sizeof(SegmentCommand); 188 189 c->cmd = LP::segmentLCType; 190 c->cmdsize = getSize(); 191 memcpy(c->segname, name.data(), name.size()); 192 c->fileoff = seg->fileOff; 193 c->maxprot = seg->maxProt; 194 c->initprot = seg->initProt; 195 196 if (seg->getSections().empty()) 197 return; 198 199 c->vmaddr = seg->firstSection()->addr; 200 c->vmsize = seg->vmSize; 201 c->filesize = seg->fileSize; 202 c->nsects = seg->numNonHiddenSections(); 203 204 for (const OutputSection *osec : seg->getSections()) { 205 if (osec->isHidden()) 206 continue; 207 208 auto *sectHdr = reinterpret_cast<Section *>(buf); 209 buf += sizeof(Section); 210 211 memcpy(sectHdr->sectname, osec->name.data(), osec->name.size()); 212 memcpy(sectHdr->segname, name.data(), name.size()); 213 214 sectHdr->addr = osec->addr; 215 sectHdr->offset = osec->fileOff; 216 sectHdr->align = Log2_32(osec->align); 217 sectHdr->flags = osec->flags; 218 sectHdr->size = osec->getSize(); 219 sectHdr->reserved1 = osec->reserved1; 220 sectHdr->reserved2 = osec->reserved2; 221 } 222 } 223 224 private: 225 StringRef name; 226 OutputSegment *seg; 227 }; 228 229 class LCMain : public LoadCommand { 230 uint32_t getSize() const override { 231 return sizeof(structs::entry_point_command); 232 } 233 234 void writeTo(uint8_t *buf) const override { 235 auto *c = reinterpret_cast<structs::entry_point_command *>(buf); 236 c->cmd = LC_MAIN; 237 c->cmdsize = getSize(); 238 239 if (config->entry->isInStubs()) 240 c->entryoff = 241 in.stubs->fileOff + config->entry->stubsIndex * target->stubSize; 242 else 243 c->entryoff = config->entry->getFileOffset(); 244 245 c->stacksize = 0; 246 } 247 }; 248 249 class LCSymtab : public LoadCommand { 250 public: 251 LCSymtab(SymtabSection *symtabSection, StringTableSection *stringTableSection) 252 : symtabSection(symtabSection), stringTableSection(stringTableSection) {} 253 254 uint32_t getSize() const override { return sizeof(symtab_command); } 255 256 void writeTo(uint8_t *buf) const override { 257 auto *c = reinterpret_cast<symtab_command *>(buf); 258 c->cmd = LC_SYMTAB; 259 c->cmdsize = getSize(); 260 c->symoff = symtabSection->fileOff; 261 c->nsyms = symtabSection->getNumSymbols(); 262 c->stroff = stringTableSection->fileOff; 263 c->strsize = stringTableSection->getFileSize(); 264 } 265 266 SymtabSection *symtabSection = nullptr; 267 StringTableSection *stringTableSection = nullptr; 268 }; 269 270 // There are several dylib load commands that share the same structure: 271 // * LC_LOAD_DYLIB 272 // * LC_ID_DYLIB 273 // * LC_REEXPORT_DYLIB 274 class LCDylib : public LoadCommand { 275 public: 276 LCDylib(LoadCommandType type, StringRef path, 277 uint32_t compatibilityVersion = 0, uint32_t currentVersion = 0) 278 : type(type), path(path), compatibilityVersion(compatibilityVersion), 279 currentVersion(currentVersion) { 280 instanceCount++; 281 } 282 283 uint32_t getSize() const override { 284 return alignTo(sizeof(dylib_command) + path.size() + 1, 8); 285 } 286 287 void writeTo(uint8_t *buf) const override { 288 auto *c = reinterpret_cast<dylib_command *>(buf); 289 buf += sizeof(dylib_command); 290 291 c->cmd = type; 292 c->cmdsize = getSize(); 293 c->dylib.name = sizeof(dylib_command); 294 c->dylib.timestamp = 0; 295 c->dylib.compatibility_version = compatibilityVersion; 296 c->dylib.current_version = currentVersion; 297 298 memcpy(buf, path.data(), path.size()); 299 buf[path.size()] = '\0'; 300 } 301 302 static uint32_t getInstanceCount() { return instanceCount; } 303 304 private: 305 LoadCommandType type; 306 StringRef path; 307 uint32_t compatibilityVersion; 308 uint32_t currentVersion; 309 static uint32_t instanceCount; 310 }; 311 312 uint32_t LCDylib::instanceCount = 0; 313 314 class LCLoadDylinker : public LoadCommand { 315 public: 316 uint32_t getSize() const override { 317 return alignTo(sizeof(dylinker_command) + path.size() + 1, 8); 318 } 319 320 void writeTo(uint8_t *buf) const override { 321 auto *c = reinterpret_cast<dylinker_command *>(buf); 322 buf += sizeof(dylinker_command); 323 324 c->cmd = LC_LOAD_DYLINKER; 325 c->cmdsize = getSize(); 326 c->name = sizeof(dylinker_command); 327 328 memcpy(buf, path.data(), path.size()); 329 buf[path.size()] = '\0'; 330 } 331 332 private: 333 // Recent versions of Darwin won't run any binary that has dyld at a 334 // different location. 335 const StringRef path = "/usr/lib/dyld"; 336 }; 337 338 class LCRPath : public LoadCommand { 339 public: 340 LCRPath(StringRef path) : path(path) {} 341 342 uint32_t getSize() const override { 343 return alignTo(sizeof(rpath_command) + path.size() + 1, target->wordSize); 344 } 345 346 void writeTo(uint8_t *buf) const override { 347 auto *c = reinterpret_cast<rpath_command *>(buf); 348 buf += sizeof(rpath_command); 349 350 c->cmd = LC_RPATH; 351 c->cmdsize = getSize(); 352 c->path = sizeof(rpath_command); 353 354 memcpy(buf, path.data(), path.size()); 355 buf[path.size()] = '\0'; 356 } 357 358 private: 359 StringRef path; 360 }; 361 362 class LCBuildVersion : public LoadCommand { 363 public: 364 LCBuildVersion(PlatformKind platform, const PlatformInfo &platformInfo) 365 : platform(platform), platformInfo(platformInfo) {} 366 367 const int ntools = 1; 368 369 uint32_t getSize() const override { 370 return sizeof(build_version_command) + ntools * sizeof(build_tool_version); 371 } 372 373 void writeTo(uint8_t *buf) const override { 374 auto *c = reinterpret_cast<build_version_command *>(buf); 375 c->cmd = LC_BUILD_VERSION; 376 c->cmdsize = getSize(); 377 c->platform = static_cast<uint32_t>(platform); 378 c->minos = ((platformInfo.minimum.getMajor() << 020) | 379 (platformInfo.minimum.getMinor().getValueOr(0) << 010) | 380 platformInfo.minimum.getSubminor().getValueOr(0)); 381 c->sdk = ((platformInfo.sdk.getMajor() << 020) | 382 (platformInfo.sdk.getMinor().getValueOr(0) << 010) | 383 platformInfo.sdk.getSubminor().getValueOr(0)); 384 c->ntools = ntools; 385 auto *t = reinterpret_cast<build_tool_version *>(&c[1]); 386 t->tool = TOOL_LD; 387 t->version = (LLVM_VERSION_MAJOR << 020) | (LLVM_VERSION_MINOR << 010) | 388 LLVM_VERSION_PATCH; 389 } 390 391 PlatformKind platform; 392 const PlatformInfo &platformInfo; 393 }; 394 395 // Stores a unique identifier for the output file based on an MD5 hash of its 396 // contents. In order to hash the contents, we must first write them, but 397 // LC_UUID itself must be part of the written contents in order for all the 398 // offsets to be calculated correctly. We resolve this circular paradox by 399 // first writing an LC_UUID with an all-zero UUID, then updating the UUID with 400 // its real value later. 401 class LCUuid : public LoadCommand { 402 public: 403 uint32_t getSize() const override { return sizeof(uuid_command); } 404 405 void writeTo(uint8_t *buf) const override { 406 auto *c = reinterpret_cast<uuid_command *>(buf); 407 c->cmd = LC_UUID; 408 c->cmdsize = getSize(); 409 uuidBuf = c->uuid; 410 } 411 412 void writeUuid(uint64_t digest) const { 413 // xxhash only gives us 8 bytes, so put some fixed data in the other half. 414 static_assert(sizeof(uuid_command::uuid) == 16, "unexpected uuid size"); 415 memcpy(uuidBuf, "LLD\xa1UU1D", 8); 416 memcpy(uuidBuf + 8, &digest, 8); 417 418 // RFC 4122 conformance. We need to fix 4 bits in byte 6 and 2 bits in 419 // byte 8. Byte 6 is already fine due to the fixed data we put in. We don't 420 // want to lose bits of the digest in byte 8, so swap that with a byte of 421 // fixed data that happens to have the right bits set. 422 std::swap(uuidBuf[3], uuidBuf[8]); 423 424 // Claim that this is an MD5-based hash. It isn't, but this signals that 425 // this is not a time-based and not a random hash. MD5 seems like the least 426 // bad lie we can put here. 427 assert((uuidBuf[6] & 0xf0) == 0x30 && "See RFC 4122 Sections 4.2.2, 4.1.3"); 428 assert((uuidBuf[8] & 0xc0) == 0x80 && "See RFC 4122 Section 4.2.2"); 429 } 430 431 mutable uint8_t *uuidBuf; 432 }; 433 434 class LCCodeSignature : public LoadCommand { 435 public: 436 LCCodeSignature(CodeSignatureSection *section) : section(section) {} 437 438 uint32_t getSize() const override { return sizeof(linkedit_data_command); } 439 440 void writeTo(uint8_t *buf) const override { 441 auto *c = reinterpret_cast<linkedit_data_command *>(buf); 442 c->cmd = LC_CODE_SIGNATURE; 443 c->cmdsize = getSize(); 444 c->dataoff = static_cast<uint32_t>(section->fileOff); 445 c->datasize = section->getSize(); 446 } 447 448 CodeSignatureSection *section; 449 }; 450 451 } // namespace 452 453 // Adds stubs and bindings where necessary (e.g. if the symbol is a 454 // DylibSymbol.) 455 static void prepareBranchTarget(Symbol *sym) { 456 if (auto *dysym = dyn_cast<DylibSymbol>(sym)) { 457 if (in.stubs->addEntry(dysym)) { 458 if (sym->isWeakDef()) { 459 in.binding->addEntry(dysym, in.lazyPointers->isec, 460 sym->stubsIndex * target->wordSize); 461 in.weakBinding->addEntry(sym, in.lazyPointers->isec, 462 sym->stubsIndex * target->wordSize); 463 } else { 464 in.lazyBinding->addEntry(dysym); 465 } 466 } 467 } else if (auto *defined = dyn_cast<Defined>(sym)) { 468 if (defined->isExternalWeakDef()) { 469 if (in.stubs->addEntry(sym)) { 470 in.rebase->addEntry(in.lazyPointers->isec, 471 sym->stubsIndex * target->wordSize); 472 in.weakBinding->addEntry(sym, in.lazyPointers->isec, 473 sym->stubsIndex * target->wordSize); 474 } 475 } 476 } 477 } 478 479 // Can a symbol's address can only be resolved at runtime? 480 static bool needsBinding(const Symbol *sym) { 481 if (isa<DylibSymbol>(sym)) 482 return true; 483 if (const auto *defined = dyn_cast<Defined>(sym)) 484 return defined->isExternalWeakDef(); 485 return false; 486 } 487 488 static void prepareSymbolRelocation(Symbol *sym, const InputSection *isec, 489 const Reloc &r) { 490 const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type); 491 492 if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) { 493 prepareBranchTarget(sym); 494 } else if (relocAttrs.hasAttr(RelocAttrBits::GOT)) { 495 if (relocAttrs.hasAttr(RelocAttrBits::POINTER) || needsBinding(sym)) 496 in.got->addEntry(sym); 497 } else if (relocAttrs.hasAttr(RelocAttrBits::TLV)) { 498 if (needsBinding(sym)) 499 in.tlvPointers->addEntry(sym); 500 } else if (relocAttrs.hasAttr(RelocAttrBits::UNSIGNED)) { 501 // References from thread-local variable sections are treated as offsets 502 // relative to the start of the referent section, and therefore have no 503 // need of rebase opcodes. 504 if (!(isThreadLocalVariables(isec->flags) && isa<Defined>(sym))) 505 addNonLazyBindingEntries(sym, isec, r.offset, r.addend); 506 } 507 } 508 509 void Writer::scanRelocations() { 510 TimeTraceScope timeScope("Scan relocations"); 511 for (InputSection *isec : inputSections) { 512 if (isec->segname == segment_names::ld) { 513 in.unwindInfo->prepareRelocations(isec); 514 continue; 515 } 516 517 for (auto it = isec->relocs.begin(); it != isec->relocs.end(); ++it) { 518 Reloc &r = *it; 519 if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) { 520 // Skip over the following UNSIGNED relocation -- it's just there as the 521 // minuend, and doesn't have the usual UNSIGNED semantics. We don't want 522 // to emit rebase opcodes for it. 523 it = std::next(it); 524 continue; 525 } 526 if (auto *sym = r.referent.dyn_cast<Symbol *>()) { 527 if (auto *undefined = dyn_cast<Undefined>(sym)) 528 treatUndefinedSymbol(*undefined); 529 // treatUndefinedSymbol() can replace sym with a DylibSymbol; re-check. 530 if (!isa<Undefined>(sym) && validateSymbolRelocation(sym, isec, r)) 531 prepareSymbolRelocation(sym, isec, r); 532 } else { 533 assert(r.referent.is<InputSection *>()); 534 if (!r.pcrel) 535 in.rebase->addEntry(isec, r.offset); 536 } 537 } 538 } 539 } 540 541 void Writer::scanSymbols() { 542 TimeTraceScope timeScope("Scan symbols"); 543 for (const Symbol *sym : symtab->getSymbols()) { 544 if (const auto *defined = dyn_cast<Defined>(sym)) { 545 if (defined->overridesWeakDef) 546 in.weakBinding->addNonWeakDefinition(defined); 547 } else if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) { 548 if (dysym->isDynamicLookup()) 549 continue; 550 dysym->getFile()->refState = 551 std::max(dysym->getFile()->refState, dysym->refState); 552 } 553 } 554 } 555 556 template <class LP> void Writer::createLoadCommands() { 557 uint8_t segIndex = 0; 558 for (OutputSegment *seg : outputSegments) { 559 in.header->addLoadCommand(make<LCSegment<LP>>(seg->name, seg)); 560 seg->index = segIndex++; 561 } 562 563 in.header->addLoadCommand(make<LCDyldInfo>( 564 in.rebase, in.binding, in.weakBinding, in.lazyBinding, in.exports)); 565 in.header->addLoadCommand(make<LCSymtab>(symtabSection, stringTableSection)); 566 in.header->addLoadCommand( 567 make<LCDysymtab>(symtabSection, indirectSymtabSection)); 568 if (functionStartsSection) 569 in.header->addLoadCommand(make<LCFunctionStarts>(functionStartsSection)); 570 for (StringRef path : config->runtimePaths) 571 in.header->addLoadCommand(make<LCRPath>(path)); 572 573 switch (config->outputType) { 574 case MH_EXECUTE: 575 in.header->addLoadCommand(make<LCLoadDylinker>()); 576 in.header->addLoadCommand(make<LCMain>()); 577 break; 578 case MH_DYLIB: 579 in.header->addLoadCommand(make<LCDylib>(LC_ID_DYLIB, config->installName, 580 config->dylibCompatibilityVersion, 581 config->dylibCurrentVersion)); 582 break; 583 case MH_BUNDLE: 584 break; 585 default: 586 llvm_unreachable("unhandled output file type"); 587 } 588 589 uuidCommand = make<LCUuid>(); 590 in.header->addLoadCommand(uuidCommand); 591 592 in.header->addLoadCommand( 593 make<LCBuildVersion>(config->target.Platform, config->platformInfo)); 594 595 int64_t dylibOrdinal = 1; 596 for (InputFile *file : inputFiles) { 597 if (auto *dylibFile = dyn_cast<DylibFile>(file)) { 598 if (dylibFile->isBundleLoader) { 599 dylibFile->ordinal = BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE; 600 // Shortcut since bundle-loader does not re-export the symbols. 601 602 dylibFile->reexport = false; 603 continue; 604 } 605 606 dylibFile->ordinal = dylibOrdinal++; 607 LoadCommandType lcType = 608 dylibFile->forceWeakImport || dylibFile->refState == RefState::Weak 609 ? LC_LOAD_WEAK_DYLIB 610 : LC_LOAD_DYLIB; 611 in.header->addLoadCommand(make<LCDylib>(lcType, dylibFile->dylibName, 612 dylibFile->compatibilityVersion, 613 dylibFile->currentVersion)); 614 615 if (dylibFile->reexport) 616 in.header->addLoadCommand( 617 make<LCDylib>(LC_REEXPORT_DYLIB, dylibFile->dylibName)); 618 } 619 } 620 621 if (codeSignatureSection) 622 in.header->addLoadCommand(make<LCCodeSignature>(codeSignatureSection)); 623 624 const uint32_t MACOS_MAXPATHLEN = 1024; 625 config->headerPad = std::max( 626 config->headerPad, (config->headerPadMaxInstallNames 627 ? LCDylib::getInstanceCount() * MACOS_MAXPATHLEN 628 : 0)); 629 } 630 631 static size_t getSymbolPriority(const SymbolPriorityEntry &entry, 632 const InputFile *f) { 633 // We don't use toString(InputFile *) here because it returns the full path 634 // for object files, and we only want the basename. 635 StringRef filename; 636 if (f->archiveName.empty()) 637 filename = path::filename(f->getName()); 638 else 639 filename = saver.save(path::filename(f->archiveName) + "(" + 640 path::filename(f->getName()) + ")"); 641 return std::max(entry.objectFiles.lookup(filename), entry.anyObjectFile); 642 } 643 644 // Each section gets assigned the priority of the highest-priority symbol it 645 // contains. 646 static DenseMap<const InputSection *, size_t> buildInputSectionPriorities() { 647 DenseMap<const InputSection *, size_t> sectionPriorities; 648 649 if (config->priorities.empty()) 650 return sectionPriorities; 651 652 auto addSym = [&](Defined &sym) { 653 auto it = config->priorities.find(sym.getName()); 654 if (it == config->priorities.end()) 655 return; 656 657 SymbolPriorityEntry &entry = it->second; 658 size_t &priority = sectionPriorities[sym.isec]; 659 priority = std::max(priority, getSymbolPriority(entry, sym.isec->file)); 660 }; 661 662 // TODO: Make sure this handles weak symbols correctly. 663 for (const InputFile *file : inputFiles) { 664 if (isa<ObjFile>(file)) 665 for (Symbol *sym : file->symbols) 666 if (auto *d = dyn_cast<Defined>(sym)) 667 addSym(*d); 668 } 669 670 return sectionPriorities; 671 } 672 673 static int segmentOrder(OutputSegment *seg) { 674 return StringSwitch<int>(seg->name) 675 .Case(segment_names::pageZero, -4) 676 .Case(segment_names::text, -3) 677 .Case(segment_names::dataConst, -2) 678 .Case(segment_names::data, -1) 679 .Case(segment_names::llvm, std::numeric_limits<int>::max() - 1) 680 // Make sure __LINKEDIT is the last segment (i.e. all its hidden 681 // sections must be ordered after other sections). 682 .Case(segment_names::linkEdit, std::numeric_limits<int>::max()) 683 .Default(0); 684 } 685 686 static int sectionOrder(OutputSection *osec) { 687 StringRef segname = osec->parent->name; 688 // Sections are uniquely identified by their segment + section name. 689 if (segname == segment_names::text) { 690 return StringSwitch<int>(osec->name) 691 .Case(section_names::header, -4) 692 .Case(section_names::text, -3) 693 .Case(section_names::stubs, -2) 694 .Case(section_names::stubHelper, -1) 695 .Case(section_names::unwindInfo, std::numeric_limits<int>::max() - 1) 696 .Case(section_names::ehFrame, std::numeric_limits<int>::max()) 697 .Default(0); 698 } else if (segname == segment_names::data) { 699 // For each thread spawned, dyld will initialize its TLVs by copying the 700 // address range from the start of the first thread-local data section to 701 // the end of the last one. We therefore arrange these sections contiguously 702 // to minimize the amount of memory used. Additionally, since zerofill 703 // sections must be at the end of their segments, and since TLV data 704 // sections can be zerofills, we end up putting all TLV data sections at the 705 // end of the segment. 706 switch (sectionType(osec->flags)) { 707 case S_THREAD_LOCAL_REGULAR: 708 return std::numeric_limits<int>::max() - 2; 709 case S_THREAD_LOCAL_ZEROFILL: 710 return std::numeric_limits<int>::max() - 1; 711 case S_ZEROFILL: 712 return std::numeric_limits<int>::max(); 713 default: 714 return StringSwitch<int>(osec->name) 715 .Case(section_names::laSymbolPtr, -2) 716 .Case(section_names::data, -1) 717 .Default(0); 718 } 719 } else if (segname == segment_names::linkEdit) { 720 return StringSwitch<int>(osec->name) 721 .Case(section_names::rebase, -9) 722 .Case(section_names::binding, -8) 723 .Case(section_names::weakBinding, -7) 724 .Case(section_names::lazyBinding, -6) 725 .Case(section_names::export_, -5) 726 .Case(section_names::functionStarts, -4) 727 .Case(section_names::symbolTable, -3) 728 .Case(section_names::indirectSymbolTable, -2) 729 .Case(section_names::stringTable, -1) 730 .Case(section_names::codeSignature, std::numeric_limits<int>::max()) 731 .Default(0); 732 } 733 // ZeroFill sections must always be the at the end of their segments, 734 // otherwise subsequent sections may get overwritten with zeroes at runtime. 735 if (sectionType(osec->flags) == S_ZEROFILL) 736 return std::numeric_limits<int>::max(); 737 return 0; 738 } 739 740 template <typename T, typename F> 741 static std::function<bool(T, T)> compareByOrder(F ord) { 742 return [=](T a, T b) { return ord(a) < ord(b); }; 743 } 744 745 // Sorting only can happen once all outputs have been collected. Here we sort 746 // segments, output sections within each segment, and input sections within each 747 // output segment. 748 static void sortSegmentsAndSections() { 749 TimeTraceScope timeScope("Sort segments and sections"); 750 751 llvm::stable_sort(outputSegments, 752 compareByOrder<OutputSegment *>(segmentOrder)); 753 754 DenseMap<const InputSection *, size_t> isecPriorities = 755 buildInputSectionPriorities(); 756 757 uint32_t sectionIndex = 0; 758 for (OutputSegment *seg : outputSegments) { 759 seg->sortOutputSections(compareByOrder<OutputSection *>(sectionOrder)); 760 for (OutputSection *osec : seg->getSections()) { 761 // Now that the output sections are sorted, assign the final 762 // output section indices. 763 if (!osec->isHidden()) 764 osec->index = ++sectionIndex; 765 if (!firstTLVDataSection && isThreadLocalData(osec->flags)) 766 firstTLVDataSection = osec; 767 768 if (!isecPriorities.empty()) { 769 if (auto *merged = dyn_cast<MergedOutputSection>(osec)) { 770 llvm::stable_sort(merged->inputs, 771 [&](InputSection *a, InputSection *b) { 772 return isecPriorities[a] > isecPriorities[b]; 773 }); 774 } 775 } 776 } 777 } 778 } 779 780 static NamePair maybeRenameSection(NamePair key) { 781 auto newNames = config->sectionRenameMap.find(key); 782 if (newNames != config->sectionRenameMap.end()) 783 return newNames->second; 784 auto newName = config->segmentRenameMap.find(key.first); 785 if (newName != config->segmentRenameMap.end()) 786 return std::make_pair(newName->second, key.second); 787 return key; 788 } 789 790 template <class LP> void Writer::createOutputSections() { 791 TimeTraceScope timeScope("Create output sections"); 792 // First, create hidden sections 793 stringTableSection = make<StringTableSection>(); 794 symtabSection = makeSymtabSection<LP>(*stringTableSection); 795 indirectSymtabSection = make<IndirectSymtabSection>(); 796 if (config->adhocCodesign) 797 codeSignatureSection = make<CodeSignatureSection>(); 798 if (config->emitFunctionStarts) 799 functionStartsSection = make<FunctionStartsSection>(); 800 if (config->emitBitcodeBundle) 801 make<BitcodeBundleSection>(); 802 803 switch (config->outputType) { 804 case MH_EXECUTE: 805 make<PageZeroSection>(); 806 break; 807 case MH_DYLIB: 808 case MH_BUNDLE: 809 break; 810 default: 811 llvm_unreachable("unhandled output file type"); 812 } 813 814 // Then merge input sections into output sections. 815 MapVector<NamePair, MergedOutputSection *> mergedOutputSections; 816 for (InputSection *isec : inputSections) { 817 NamePair names = maybeRenameSection({isec->segname, isec->name}); 818 MergedOutputSection *&osec = mergedOutputSections[names]; 819 if (osec == nullptr) 820 osec = make<MergedOutputSection>(names.second); 821 osec->mergeInput(isec); 822 } 823 824 for (const auto &it : mergedOutputSections) { 825 StringRef segname = it.first.first; 826 MergedOutputSection *osec = it.second; 827 if (segname == segment_names::ld) { 828 assert(osec->name == section_names::compactUnwind); 829 in.unwindInfo->setCompactUnwindSection(osec); 830 } else { 831 getOrCreateOutputSegment(segname)->addOutputSection(osec); 832 } 833 } 834 835 for (SyntheticSection *ssec : syntheticSections) { 836 auto it = mergedOutputSections.find({ssec->segname, ssec->name}); 837 if (it == mergedOutputSections.end()) { 838 if (ssec->isNeeded()) 839 getOrCreateOutputSegment(ssec->segname)->addOutputSection(ssec); 840 } else { 841 error("section from " + toString(it->second->firstSection()->file) + 842 " conflicts with synthetic section " + ssec->segname + "," + 843 ssec->name); 844 } 845 } 846 847 // dyld requires __LINKEDIT segment to always exist (even if empty). 848 linkEditSegment = getOrCreateOutputSegment(segment_names::linkEdit); 849 } 850 851 void Writer::finalizeAddresses() { 852 TimeTraceScope timeScope("Finalize addresses"); 853 uint64_t pageSize = target->getPageSize(); 854 // Ensure that segments (and the sections they contain) are allocated 855 // addresses in ascending order, which dyld requires. 856 // 857 // Note that at this point, __LINKEDIT sections are empty, but we need to 858 // determine addresses of other segments/sections before generating its 859 // contents. 860 for (OutputSegment *seg : outputSegments) { 861 if (seg == linkEditSegment) 862 continue; 863 assignAddresses(seg); 864 // codesign / libstuff checks for segment ordering by verifying that 865 // `fileOff + fileSize == next segment fileOff`. So we call alignTo() before 866 // (instead of after) computing fileSize to ensure that the segments are 867 // contiguous. We handle addr / vmSize similarly for the same reason. 868 fileOff = alignTo(fileOff, pageSize); 869 addr = alignTo(addr, pageSize); 870 seg->vmSize = addr - seg->firstSection()->addr; 871 seg->fileSize = fileOff - seg->fileOff; 872 } 873 874 // FIXME(gkm): create branch-extension thunks here, then adjust addresses 875 } 876 877 void Writer::finalizeLinkEditSegment() { 878 TimeTraceScope timeScope("Finalize __LINKEDIT segment"); 879 // Fill __LINKEDIT contents. 880 std::vector<LinkEditSection *> linkEditSections{ 881 in.rebase, in.binding, in.weakBinding, in.lazyBinding, 882 in.exports, symtabSection, indirectSymtabSection, functionStartsSection, 883 }; 884 parallelForEach(linkEditSections, [](LinkEditSection *osec) { 885 if (osec) 886 osec->finalizeContents(); 887 }); 888 889 // Now that __LINKEDIT is filled out, do a proper calculation of its 890 // addresses and offsets. 891 assignAddresses(linkEditSegment); 892 // No need to page-align fileOff / addr here since this is the last segment. 893 linkEditSegment->vmSize = addr - linkEditSegment->firstSection()->addr; 894 linkEditSegment->fileSize = fileOff - linkEditSegment->fileOff; 895 } 896 897 void Writer::assignAddresses(OutputSegment *seg) { 898 seg->fileOff = fileOff; 899 900 for (OutputSection *osec : seg->getSections()) { 901 if (!osec->isNeeded()) 902 continue; 903 addr = alignTo(addr, osec->align); 904 fileOff = alignTo(fileOff, osec->align); 905 osec->addr = addr; 906 osec->fileOff = isZeroFill(osec->flags) ? 0 : fileOff; 907 osec->finalize(); 908 909 addr += osec->getSize(); 910 fileOff += osec->getFileSize(); 911 } 912 } 913 914 void Writer::openFile() { 915 Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr = 916 FileOutputBuffer::create(config->outputFile, fileOff, 917 FileOutputBuffer::F_executable); 918 919 if (!bufferOrErr) 920 error("failed to open " + config->outputFile + ": " + 921 llvm::toString(bufferOrErr.takeError())); 922 else 923 buffer = std::move(*bufferOrErr); 924 } 925 926 void Writer::writeSections() { 927 uint8_t *buf = buffer->getBufferStart(); 928 for (const OutputSegment *seg : outputSegments) 929 for (const OutputSection *osec : seg->getSections()) 930 osec->writeTo(buf + osec->fileOff); 931 } 932 933 // In order to utilize multiple cores, we first split the buffer into chunks, 934 // compute a hash for each chunk, and then compute a hash value of the hash 935 // values. 936 void Writer::writeUuid() { 937 TimeTraceScope timeScope("Computing UUID"); 938 ArrayRef<uint8_t> data{buffer->getBufferStart(), buffer->getBufferEnd()}; 939 unsigned chunkCount = parallel::strategy.compute_thread_count() * 10; 940 // Round-up integer division 941 size_t chunkSize = (data.size() + chunkCount - 1) / chunkCount; 942 std::vector<ArrayRef<uint8_t>> chunks = split(data, chunkSize); 943 std::vector<uint64_t> hashes(chunks.size()); 944 parallelForEachN(0, chunks.size(), 945 [&](size_t i) { hashes[i] = xxHash64(chunks[i]); }); 946 uint64_t digest = xxHash64({reinterpret_cast<uint8_t *>(hashes.data()), 947 hashes.size() * sizeof(uint64_t)}); 948 uuidCommand->writeUuid(digest); 949 } 950 951 void Writer::writeCodeSignature() { 952 if (codeSignatureSection) 953 codeSignatureSection->writeHashes(buffer->getBufferStart()); 954 } 955 956 void Writer::writeOutputFile() { 957 TimeTraceScope timeScope("Write output file"); 958 openFile(); 959 if (errorCount()) 960 return; 961 writeSections(); 962 writeUuid(); 963 writeCodeSignature(); 964 965 if (auto e = buffer->commit()) 966 error("failed to write to the output file: " + toString(std::move(e))); 967 } 968 969 template <class LP> void Writer::run() { 970 prepareBranchTarget(config->entry); 971 scanRelocations(); 972 if (in.stubHelper->isNeeded()) 973 in.stubHelper->setup(); 974 scanSymbols(); 975 createOutputSections<LP>(); 976 // No more sections nor segments are created beyond this point. 977 sortSegmentsAndSections(); 978 createLoadCommands<LP>(); 979 finalizeAddresses(); 980 finalizeLinkEditSegment(); 981 writeMapFile(); 982 writeOutputFile(); 983 } 984 985 template <class LP> void macho::writeResult() { Writer().run<LP>(); } 986 987 template <class LP> void macho::createSyntheticSections() { 988 in.header = makeMachHeaderSection<LP>(); 989 in.rebase = make<RebaseSection>(); 990 in.binding = make<BindingSection>(); 991 in.weakBinding = make<WeakBindingSection>(); 992 in.lazyBinding = make<LazyBindingSection>(); 993 in.exports = make<ExportSection>(); 994 in.got = make<GotSection>(); 995 in.tlvPointers = make<TlvPointerSection>(); 996 in.lazyPointers = make<LazyPointerSection>(); 997 in.stubs = make<StubsSection>(); 998 in.stubHelper = make<StubHelperSection>(); 999 in.imageLoaderCache = make<ImageLoaderCacheSection>(); 1000 in.unwindInfo = makeUnwindInfoSection(); 1001 } 1002 1003 OutputSection *macho::firstTLVDataSection = nullptr; 1004 1005 template void macho::writeResult<LP64>(); 1006 template void macho::writeResult<ILP32>(); 1007 template void macho::createSyntheticSections<LP64>(); 1008 template void macho::createSyntheticSections<ILP32>(); 1009