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 "ConcatOutputSection.h" 11 #include "Config.h" 12 #include "InputFiles.h" 13 #include "InputSection.h" 14 #include "MapFile.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 DataInCodeSection *dataInCodeSection = nullptr; 75 FunctionStartsSection *functionStartsSection = nullptr; 76 77 LCUuid *uuidCommand = nullptr; 78 OutputSegment *linkEditSegment = nullptr; 79 80 // Output sections are added to output segments in iteration order 81 // of ConcatOutputSection, so must have deterministic iteration order. 82 MapVector<NamePair, ConcatOutputSection *> concatOutputSections; 83 }; 84 85 // LC_DYLD_INFO_ONLY stores the offsets of symbol import/export information. 86 class LCDyldInfo final : public LoadCommand { 87 public: 88 LCDyldInfo(RebaseSection *rebaseSection, BindingSection *bindingSection, 89 WeakBindingSection *weakBindingSection, 90 LazyBindingSection *lazyBindingSection, 91 ExportSection *exportSection) 92 : rebaseSection(rebaseSection), bindingSection(bindingSection), 93 weakBindingSection(weakBindingSection), 94 lazyBindingSection(lazyBindingSection), exportSection(exportSection) {} 95 96 uint32_t getSize() const override { return sizeof(dyld_info_command); } 97 98 void writeTo(uint8_t *buf) const override { 99 auto *c = reinterpret_cast<dyld_info_command *>(buf); 100 c->cmd = LC_DYLD_INFO_ONLY; 101 c->cmdsize = getSize(); 102 if (rebaseSection->isNeeded()) { 103 c->rebase_off = rebaseSection->fileOff; 104 c->rebase_size = rebaseSection->getFileSize(); 105 } 106 if (bindingSection->isNeeded()) { 107 c->bind_off = bindingSection->fileOff; 108 c->bind_size = bindingSection->getFileSize(); 109 } 110 if (weakBindingSection->isNeeded()) { 111 c->weak_bind_off = weakBindingSection->fileOff; 112 c->weak_bind_size = weakBindingSection->getFileSize(); 113 } 114 if (lazyBindingSection->isNeeded()) { 115 c->lazy_bind_off = lazyBindingSection->fileOff; 116 c->lazy_bind_size = lazyBindingSection->getFileSize(); 117 } 118 if (exportSection->isNeeded()) { 119 c->export_off = exportSection->fileOff; 120 c->export_size = exportSection->getFileSize(); 121 } 122 } 123 124 RebaseSection *rebaseSection; 125 BindingSection *bindingSection; 126 WeakBindingSection *weakBindingSection; 127 LazyBindingSection *lazyBindingSection; 128 ExportSection *exportSection; 129 }; 130 131 class LCSubFramework final : public LoadCommand { 132 public: 133 LCSubFramework(StringRef umbrella) : umbrella(umbrella) {} 134 135 uint32_t getSize() const override { 136 return alignTo(sizeof(sub_framework_command) + umbrella.size() + 1, 137 target->wordSize); 138 } 139 140 void writeTo(uint8_t *buf) const override { 141 auto *c = reinterpret_cast<sub_framework_command *>(buf); 142 buf += sizeof(sub_framework_command); 143 144 c->cmd = LC_SUB_FRAMEWORK; 145 c->cmdsize = getSize(); 146 c->umbrella = sizeof(sub_framework_command); 147 148 memcpy(buf, umbrella.data(), umbrella.size()); 149 buf[umbrella.size()] = '\0'; 150 } 151 152 private: 153 const StringRef umbrella; 154 }; 155 156 class LCFunctionStarts final : public LoadCommand { 157 public: 158 explicit LCFunctionStarts(FunctionStartsSection *functionStartsSection) 159 : functionStartsSection(functionStartsSection) {} 160 161 uint32_t getSize() const override { return sizeof(linkedit_data_command); } 162 163 void writeTo(uint8_t *buf) const override { 164 auto *c = reinterpret_cast<linkedit_data_command *>(buf); 165 c->cmd = LC_FUNCTION_STARTS; 166 c->cmdsize = getSize(); 167 c->dataoff = functionStartsSection->fileOff; 168 c->datasize = functionStartsSection->getFileSize(); 169 } 170 171 private: 172 FunctionStartsSection *functionStartsSection; 173 }; 174 175 class LCDataInCode final : public LoadCommand { 176 public: 177 explicit LCDataInCode(DataInCodeSection *dataInCodeSection) 178 : dataInCodeSection(dataInCodeSection) {} 179 180 uint32_t getSize() const override { return sizeof(linkedit_data_command); } 181 182 void writeTo(uint8_t *buf) const override { 183 auto *c = reinterpret_cast<linkedit_data_command *>(buf); 184 c->cmd = LC_DATA_IN_CODE; 185 c->cmdsize = getSize(); 186 c->dataoff = dataInCodeSection->fileOff; 187 c->datasize = dataInCodeSection->getFileSize(); 188 } 189 190 private: 191 DataInCodeSection *dataInCodeSection; 192 }; 193 194 class LCDysymtab final : public LoadCommand { 195 public: 196 LCDysymtab(SymtabSection *symtabSection, 197 IndirectSymtabSection *indirectSymtabSection) 198 : symtabSection(symtabSection), 199 indirectSymtabSection(indirectSymtabSection) {} 200 201 uint32_t getSize() const override { return sizeof(dysymtab_command); } 202 203 void writeTo(uint8_t *buf) const override { 204 auto *c = reinterpret_cast<dysymtab_command *>(buf); 205 c->cmd = LC_DYSYMTAB; 206 c->cmdsize = getSize(); 207 208 c->ilocalsym = 0; 209 c->iextdefsym = c->nlocalsym = symtabSection->getNumLocalSymbols(); 210 c->nextdefsym = symtabSection->getNumExternalSymbols(); 211 c->iundefsym = c->iextdefsym + c->nextdefsym; 212 c->nundefsym = symtabSection->getNumUndefinedSymbols(); 213 214 c->indirectsymoff = indirectSymtabSection->fileOff; 215 c->nindirectsyms = indirectSymtabSection->getNumSymbols(); 216 } 217 218 SymtabSection *symtabSection; 219 IndirectSymtabSection *indirectSymtabSection; 220 }; 221 222 template <class LP> class LCSegment final : public LoadCommand { 223 public: 224 LCSegment(StringRef name, OutputSegment *seg) : name(name), seg(seg) {} 225 226 uint32_t getSize() const override { 227 return sizeof(typename LP::segment_command) + 228 seg->numNonHiddenSections() * sizeof(typename LP::section); 229 } 230 231 void writeTo(uint8_t *buf) const override { 232 using SegmentCommand = typename LP::segment_command; 233 using Section = typename LP::section; 234 235 auto *c = reinterpret_cast<SegmentCommand *>(buf); 236 buf += sizeof(SegmentCommand); 237 238 c->cmd = LP::segmentLCType; 239 c->cmdsize = getSize(); 240 memcpy(c->segname, name.data(), name.size()); 241 c->fileoff = seg->fileOff; 242 c->maxprot = seg->maxProt; 243 c->initprot = seg->initProt; 244 245 if (seg->getSections().empty()) 246 return; 247 248 c->vmaddr = seg->firstSection()->addr; 249 c->vmsize = seg->vmSize; 250 c->filesize = seg->fileSize; 251 c->nsects = seg->numNonHiddenSections(); 252 253 for (const OutputSection *osec : seg->getSections()) { 254 if (osec->isHidden()) 255 continue; 256 257 auto *sectHdr = reinterpret_cast<Section *>(buf); 258 buf += sizeof(Section); 259 260 memcpy(sectHdr->sectname, osec->name.data(), osec->name.size()); 261 memcpy(sectHdr->segname, name.data(), name.size()); 262 263 sectHdr->addr = osec->addr; 264 sectHdr->offset = osec->fileOff; 265 sectHdr->align = Log2_32(osec->align); 266 sectHdr->flags = osec->flags; 267 sectHdr->size = osec->getSize(); 268 sectHdr->reserved1 = osec->reserved1; 269 sectHdr->reserved2 = osec->reserved2; 270 } 271 } 272 273 private: 274 StringRef name; 275 OutputSegment *seg; 276 }; 277 278 class LCMain final : public LoadCommand { 279 uint32_t getSize() const override { 280 return sizeof(structs::entry_point_command); 281 } 282 283 void writeTo(uint8_t *buf) const override { 284 auto *c = reinterpret_cast<structs::entry_point_command *>(buf); 285 c->cmd = LC_MAIN; 286 c->cmdsize = getSize(); 287 288 if (config->entry->isInStubs()) 289 c->entryoff = 290 in.stubs->fileOff + config->entry->stubsIndex * target->stubSize; 291 else 292 c->entryoff = config->entry->getVA() - in.header->addr; 293 294 c->stacksize = 0; 295 } 296 }; 297 298 class LCSymtab final : public LoadCommand { 299 public: 300 LCSymtab(SymtabSection *symtabSection, StringTableSection *stringTableSection) 301 : symtabSection(symtabSection), stringTableSection(stringTableSection) {} 302 303 uint32_t getSize() const override { return sizeof(symtab_command); } 304 305 void writeTo(uint8_t *buf) const override { 306 auto *c = reinterpret_cast<symtab_command *>(buf); 307 c->cmd = LC_SYMTAB; 308 c->cmdsize = getSize(); 309 c->symoff = symtabSection->fileOff; 310 c->nsyms = symtabSection->getNumSymbols(); 311 c->stroff = stringTableSection->fileOff; 312 c->strsize = stringTableSection->getFileSize(); 313 } 314 315 SymtabSection *symtabSection = nullptr; 316 StringTableSection *stringTableSection = nullptr; 317 }; 318 319 // There are several dylib load commands that share the same structure: 320 // * LC_LOAD_DYLIB 321 // * LC_ID_DYLIB 322 // * LC_REEXPORT_DYLIB 323 class LCDylib final : public LoadCommand { 324 public: 325 LCDylib(LoadCommandType type, StringRef path, 326 uint32_t compatibilityVersion = 0, uint32_t currentVersion = 0) 327 : type(type), path(path), compatibilityVersion(compatibilityVersion), 328 currentVersion(currentVersion) { 329 instanceCount++; 330 } 331 332 uint32_t getSize() const override { 333 return alignTo(sizeof(dylib_command) + path.size() + 1, 8); 334 } 335 336 void writeTo(uint8_t *buf) const override { 337 auto *c = reinterpret_cast<dylib_command *>(buf); 338 buf += sizeof(dylib_command); 339 340 c->cmd = type; 341 c->cmdsize = getSize(); 342 c->dylib.name = sizeof(dylib_command); 343 c->dylib.timestamp = 0; 344 c->dylib.compatibility_version = compatibilityVersion; 345 c->dylib.current_version = currentVersion; 346 347 memcpy(buf, path.data(), path.size()); 348 buf[path.size()] = '\0'; 349 } 350 351 static uint32_t getInstanceCount() { return instanceCount; } 352 353 private: 354 LoadCommandType type; 355 StringRef path; 356 uint32_t compatibilityVersion; 357 uint32_t currentVersion; 358 static uint32_t instanceCount; 359 }; 360 361 uint32_t LCDylib::instanceCount = 0; 362 363 class LCLoadDylinker final : public LoadCommand { 364 public: 365 uint32_t getSize() const override { 366 return alignTo(sizeof(dylinker_command) + path.size() + 1, 8); 367 } 368 369 void writeTo(uint8_t *buf) const override { 370 auto *c = reinterpret_cast<dylinker_command *>(buf); 371 buf += sizeof(dylinker_command); 372 373 c->cmd = LC_LOAD_DYLINKER; 374 c->cmdsize = getSize(); 375 c->name = sizeof(dylinker_command); 376 377 memcpy(buf, path.data(), path.size()); 378 buf[path.size()] = '\0'; 379 } 380 381 private: 382 // Recent versions of Darwin won't run any binary that has dyld at a 383 // different location. 384 const StringRef path = "/usr/lib/dyld"; 385 }; 386 387 class LCRPath final : public LoadCommand { 388 public: 389 explicit LCRPath(StringRef path) : path(path) {} 390 391 uint32_t getSize() const override { 392 return alignTo(sizeof(rpath_command) + path.size() + 1, target->wordSize); 393 } 394 395 void writeTo(uint8_t *buf) const override { 396 auto *c = reinterpret_cast<rpath_command *>(buf); 397 buf += sizeof(rpath_command); 398 399 c->cmd = LC_RPATH; 400 c->cmdsize = getSize(); 401 c->path = sizeof(rpath_command); 402 403 memcpy(buf, path.data(), path.size()); 404 buf[path.size()] = '\0'; 405 } 406 407 private: 408 StringRef path; 409 }; 410 411 class LCMinVersion final : public LoadCommand { 412 public: 413 explicit LCMinVersion(const PlatformInfo &platformInfo) 414 : platformInfo(platformInfo) {} 415 416 uint32_t getSize() const override { return sizeof(version_min_command); } 417 418 void writeTo(uint8_t *buf) const override { 419 auto *c = reinterpret_cast<version_min_command *>(buf); 420 switch (platformInfo.target.Platform) { 421 case PlatformKind::macOS: 422 c->cmd = LC_VERSION_MIN_MACOSX; 423 break; 424 case PlatformKind::iOS: 425 case PlatformKind::iOSSimulator: 426 c->cmd = LC_VERSION_MIN_IPHONEOS; 427 break; 428 case PlatformKind::tvOS: 429 case PlatformKind::tvOSSimulator: 430 c->cmd = LC_VERSION_MIN_TVOS; 431 break; 432 case PlatformKind::watchOS: 433 case PlatformKind::watchOSSimulator: 434 c->cmd = LC_VERSION_MIN_WATCHOS; 435 break; 436 default: 437 llvm_unreachable("invalid platform"); 438 break; 439 } 440 c->cmdsize = getSize(); 441 c->version = encodeVersion(platformInfo.minimum); 442 c->sdk = encodeVersion(platformInfo.sdk); 443 } 444 445 private: 446 const PlatformInfo &platformInfo; 447 }; 448 449 class LCBuildVersion final : public LoadCommand { 450 public: 451 explicit LCBuildVersion(const PlatformInfo &platformInfo) 452 : platformInfo(platformInfo) {} 453 454 const int ntools = 1; 455 456 uint32_t getSize() const override { 457 return sizeof(build_version_command) + ntools * sizeof(build_tool_version); 458 } 459 460 void writeTo(uint8_t *buf) const override { 461 auto *c = reinterpret_cast<build_version_command *>(buf); 462 c->cmd = LC_BUILD_VERSION; 463 c->cmdsize = getSize(); 464 c->platform = static_cast<uint32_t>(platformInfo.target.Platform); 465 c->minos = encodeVersion(platformInfo.minimum); 466 c->sdk = encodeVersion(platformInfo.sdk); 467 c->ntools = ntools; 468 auto *t = reinterpret_cast<build_tool_version *>(&c[1]); 469 t->tool = TOOL_LD; 470 t->version = encodeVersion(VersionTuple( 471 LLVM_VERSION_MAJOR, LLVM_VERSION_MINOR, LLVM_VERSION_PATCH)); 472 } 473 474 private: 475 const PlatformInfo &platformInfo; 476 }; 477 478 // Stores a unique identifier for the output file based on an MD5 hash of its 479 // contents. In order to hash the contents, we must first write them, but 480 // LC_UUID itself must be part of the written contents in order for all the 481 // offsets to be calculated correctly. We resolve this circular paradox by 482 // first writing an LC_UUID with an all-zero UUID, then updating the UUID with 483 // its real value later. 484 class LCUuid final : public LoadCommand { 485 public: 486 uint32_t getSize() const override { return sizeof(uuid_command); } 487 488 void writeTo(uint8_t *buf) const override { 489 auto *c = reinterpret_cast<uuid_command *>(buf); 490 c->cmd = LC_UUID; 491 c->cmdsize = getSize(); 492 uuidBuf = c->uuid; 493 } 494 495 void writeUuid(uint64_t digest) const { 496 // xxhash only gives us 8 bytes, so put some fixed data in the other half. 497 static_assert(sizeof(uuid_command::uuid) == 16, "unexpected uuid size"); 498 memcpy(uuidBuf, "LLD\xa1UU1D", 8); 499 memcpy(uuidBuf + 8, &digest, 8); 500 501 // RFC 4122 conformance. We need to fix 4 bits in byte 6 and 2 bits in 502 // byte 8. Byte 6 is already fine due to the fixed data we put in. We don't 503 // want to lose bits of the digest in byte 8, so swap that with a byte of 504 // fixed data that happens to have the right bits set. 505 std::swap(uuidBuf[3], uuidBuf[8]); 506 507 // Claim that this is an MD5-based hash. It isn't, but this signals that 508 // this is not a time-based and not a random hash. MD5 seems like the least 509 // bad lie we can put here. 510 assert((uuidBuf[6] & 0xf0) == 0x30 && "See RFC 4122 Sections 4.2.2, 4.1.3"); 511 assert((uuidBuf[8] & 0xc0) == 0x80 && "See RFC 4122 Section 4.2.2"); 512 } 513 514 mutable uint8_t *uuidBuf; 515 }; 516 517 template <class LP> class LCEncryptionInfo final : public LoadCommand { 518 public: 519 uint32_t getSize() const override { 520 return sizeof(typename LP::encryption_info_command); 521 } 522 523 void writeTo(uint8_t *buf) const override { 524 using EncryptionInfo = typename LP::encryption_info_command; 525 auto *c = reinterpret_cast<EncryptionInfo *>(buf); 526 buf += sizeof(EncryptionInfo); 527 c->cmd = LP::encryptionInfoLCType; 528 c->cmdsize = getSize(); 529 c->cryptoff = in.header->getSize(); 530 auto it = find_if(outputSegments, [](const OutputSegment *seg) { 531 return seg->name == segment_names::text; 532 }); 533 assert(it != outputSegments.end()); 534 c->cryptsize = (*it)->fileSize - c->cryptoff; 535 } 536 }; 537 538 class LCCodeSignature final : public LoadCommand { 539 public: 540 LCCodeSignature(CodeSignatureSection *section) : section(section) {} 541 542 uint32_t getSize() const override { return sizeof(linkedit_data_command); } 543 544 void writeTo(uint8_t *buf) const override { 545 auto *c = reinterpret_cast<linkedit_data_command *>(buf); 546 c->cmd = LC_CODE_SIGNATURE; 547 c->cmdsize = getSize(); 548 c->dataoff = static_cast<uint32_t>(section->fileOff); 549 c->datasize = section->getSize(); 550 } 551 552 CodeSignatureSection *section; 553 }; 554 555 } // namespace 556 557 // Add stubs and bindings where necessary (e.g. if the symbol is a 558 // DylibSymbol.) 559 static void prepareBranchTarget(Symbol *sym) { 560 if (auto *dysym = dyn_cast<DylibSymbol>(sym)) { 561 if (in.stubs->addEntry(dysym)) { 562 if (sym->isWeakDef()) { 563 in.binding->addEntry(dysym, in.lazyPointers->isec, 564 sym->stubsIndex * target->wordSize); 565 in.weakBinding->addEntry(sym, in.lazyPointers->isec, 566 sym->stubsIndex * target->wordSize); 567 } else { 568 in.lazyBinding->addEntry(dysym); 569 } 570 } 571 } else if (auto *defined = dyn_cast<Defined>(sym)) { 572 if (defined->isExternalWeakDef()) { 573 if (in.stubs->addEntry(sym)) { 574 in.rebase->addEntry(in.lazyPointers->isec, 575 sym->stubsIndex * target->wordSize); 576 in.weakBinding->addEntry(sym, in.lazyPointers->isec, 577 sym->stubsIndex * target->wordSize); 578 } 579 } 580 } else { 581 llvm_unreachable("invalid branch target symbol type"); 582 } 583 } 584 585 // Can a symbol's address can only be resolved at runtime? 586 static bool needsBinding(const Symbol *sym) { 587 if (isa<DylibSymbol>(sym)) 588 return true; 589 if (const auto *defined = dyn_cast<Defined>(sym)) 590 return defined->isExternalWeakDef(); 591 return false; 592 } 593 594 static void prepareSymbolRelocation(Symbol *sym, const InputSection *isec, 595 const Reloc &r) { 596 const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type); 597 598 if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) { 599 prepareBranchTarget(sym); 600 } else if (relocAttrs.hasAttr(RelocAttrBits::GOT)) { 601 if (relocAttrs.hasAttr(RelocAttrBits::POINTER) || needsBinding(sym)) 602 in.got->addEntry(sym); 603 } else if (relocAttrs.hasAttr(RelocAttrBits::TLV)) { 604 if (needsBinding(sym)) 605 in.tlvPointers->addEntry(sym); 606 } else if (relocAttrs.hasAttr(RelocAttrBits::UNSIGNED)) { 607 // References from thread-local variable sections are treated as offsets 608 // relative to the start of the referent section, and therefore have no 609 // need of rebase opcodes. 610 if (!(isThreadLocalVariables(isec->getFlags()) && isa<Defined>(sym))) 611 addNonLazyBindingEntries(sym, isec, r.offset, r.addend); 612 } 613 } 614 615 void Writer::scanRelocations() { 616 TimeTraceScope timeScope("Scan relocations"); 617 for (ConcatInputSection *isec : inputSections) { 618 if (isec->shouldOmitFromOutput()) 619 continue; 620 621 for (auto it = isec->relocs.begin(); it != isec->relocs.end(); ++it) { 622 Reloc &r = *it; 623 if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) { 624 // Skip over the following UNSIGNED relocation -- it's just there as the 625 // minuend, and doesn't have the usual UNSIGNED semantics. We don't want 626 // to emit rebase opcodes for it. 627 it++; 628 continue; 629 } 630 if (auto *sym = r.referent.dyn_cast<Symbol *>()) { 631 if (auto *undefined = dyn_cast<Undefined>(sym)) 632 treatUndefinedSymbol(*undefined); 633 // treatUndefinedSymbol() can replace sym with a DylibSymbol; re-check. 634 if (!isa<Undefined>(sym) && validateSymbolRelocation(sym, isec, r)) 635 prepareSymbolRelocation(sym, isec, r); 636 } else { 637 // Canonicalize the referent so that later accesses in Writer won't 638 // have to worry about it. Perhaps we should do this for Defined::isec 639 // too... 640 auto *referentIsec = r.referent.get<InputSection *>(); 641 r.referent = referentIsec->canonical(); 642 if (!r.pcrel) 643 in.rebase->addEntry(isec, r.offset); 644 } 645 } 646 } 647 648 in.unwindInfo->prepareRelocations(); 649 } 650 651 void Writer::scanSymbols() { 652 TimeTraceScope timeScope("Scan symbols"); 653 for (const Symbol *sym : symtab->getSymbols()) { 654 if (const auto *defined = dyn_cast<Defined>(sym)) { 655 if (defined->overridesWeakDef && defined->isLive()) 656 in.weakBinding->addNonWeakDefinition(defined); 657 } else if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) { 658 // This branch intentionally doesn't check isLive(). 659 if (dysym->isDynamicLookup()) 660 continue; 661 dysym->getFile()->refState = 662 std::max(dysym->getFile()->refState, dysym->getRefState()); 663 } 664 } 665 } 666 667 // TODO: ld64 enforces the old load commands in a few other cases. 668 static bool useLCBuildVersion(const PlatformInfo &platformInfo) { 669 static const std::vector<std::pair<PlatformKind, VersionTuple>> minVersion = { 670 {PlatformKind::macOS, VersionTuple(10, 14)}, 671 {PlatformKind::iOS, VersionTuple(12, 0)}, 672 {PlatformKind::iOSSimulator, VersionTuple(13, 0)}, 673 {PlatformKind::tvOS, VersionTuple(12, 0)}, 674 {PlatformKind::tvOSSimulator, VersionTuple(13, 0)}, 675 {PlatformKind::watchOS, VersionTuple(5, 0)}, 676 {PlatformKind::watchOSSimulator, VersionTuple(6, 0)}}; 677 auto it = llvm::find_if(minVersion, [&](const auto &p) { 678 return p.first == platformInfo.target.Platform; 679 }); 680 return it == minVersion.end() ? true : platformInfo.minimum >= it->second; 681 } 682 683 template <class LP> void Writer::createLoadCommands() { 684 uint8_t segIndex = 0; 685 for (OutputSegment *seg : outputSegments) { 686 in.header->addLoadCommand(make<LCSegment<LP>>(seg->name, seg)); 687 seg->index = segIndex++; 688 } 689 690 in.header->addLoadCommand(make<LCDyldInfo>( 691 in.rebase, in.binding, in.weakBinding, in.lazyBinding, in.exports)); 692 in.header->addLoadCommand(make<LCSymtab>(symtabSection, stringTableSection)); 693 in.header->addLoadCommand( 694 make<LCDysymtab>(symtabSection, indirectSymtabSection)); 695 if (!config->umbrella.empty()) 696 in.header->addLoadCommand(make<LCSubFramework>(config->umbrella)); 697 if (functionStartsSection) 698 in.header->addLoadCommand(make<LCFunctionStarts>(functionStartsSection)); 699 if (dataInCodeSection) 700 in.header->addLoadCommand(make<LCDataInCode>(dataInCodeSection)); 701 if (config->emitEncryptionInfo) 702 in.header->addLoadCommand(make<LCEncryptionInfo<LP>>()); 703 for (StringRef path : config->runtimePaths) 704 in.header->addLoadCommand(make<LCRPath>(path)); 705 706 switch (config->outputType) { 707 case MH_EXECUTE: 708 in.header->addLoadCommand(make<LCLoadDylinker>()); 709 in.header->addLoadCommand(make<LCMain>()); 710 break; 711 case MH_DYLIB: 712 in.header->addLoadCommand(make<LCDylib>(LC_ID_DYLIB, config->installName, 713 config->dylibCompatibilityVersion, 714 config->dylibCurrentVersion)); 715 break; 716 case MH_BUNDLE: 717 break; 718 default: 719 llvm_unreachable("unhandled output file type"); 720 } 721 722 uuidCommand = make<LCUuid>(); 723 in.header->addLoadCommand(uuidCommand); 724 725 if (useLCBuildVersion(config->platformInfo)) 726 in.header->addLoadCommand(make<LCBuildVersion>(config->platformInfo)); 727 else 728 in.header->addLoadCommand(make<LCMinVersion>(config->platformInfo)); 729 730 int64_t dylibOrdinal = 1; 731 DenseMap<StringRef, int64_t> ordinalForInstallName; 732 for (InputFile *file : inputFiles) { 733 if (auto *dylibFile = dyn_cast<DylibFile>(file)) { 734 if (dylibFile->isBundleLoader) { 735 dylibFile->ordinal = BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE; 736 // Shortcut since bundle-loader does not re-export the symbols. 737 738 dylibFile->reexport = false; 739 continue; 740 } 741 742 // Don't emit load commands for a dylib that is not referenced if: 743 // - it was added implicitly (via a reexport, an LC_LOAD_DYLINKER -- 744 // if it's on the linker command line, it's explicit) 745 // - or it's marked MH_DEAD_STRIPPABLE_DYLIB 746 // - or the flag -dead_strip_dylibs is used 747 // FIXME: `isReferenced()` is currently computed before dead code 748 // stripping, so references from dead code keep a dylib alive. This 749 // matches ld64, but it's something we should do better. 750 if (!dylibFile->isReferenced() && !dylibFile->forceNeeded && 751 (!dylibFile->explicitlyLinked || dylibFile->deadStrippable || 752 config->deadStripDylibs)) 753 continue; 754 755 // Several DylibFiles can have the same installName. Only emit a single 756 // load command for that installName and give all these DylibFiles the 757 // same ordinal. 758 // This can happen in several cases: 759 // - a new framework could change its installName to an older 760 // framework name via an $ld$ symbol depending on platform_version 761 // - symlinks (for example, libpthread.tbd is a symlink to libSystem.tbd; 762 // Foo.framework/Foo.tbd is usually a symlink to 763 // Foo.framework/Versions/Current/Foo.tbd, where 764 // Foo.framework/Versions/Current is usually a symlink to 765 // Foo.framework/Versions/A) 766 // - a framework can be linked both explicitly on the linker 767 // command line and implicitly as a reexport from a different 768 // framework. The re-export will usually point to the tbd file 769 // in Foo.framework/Versions/A/Foo.tbd, while the explicit link will 770 // usually find Foo.framework/Foo.tbd. These are usually symlinks, 771 // but in a --reproduce archive they will be identical but distinct 772 // files. 773 // In the first case, *semantically distinct* DylibFiles will have the 774 // same installName. 775 int64_t &ordinal = ordinalForInstallName[dylibFile->installName]; 776 if (ordinal) { 777 dylibFile->ordinal = ordinal; 778 continue; 779 } 780 781 ordinal = dylibFile->ordinal = dylibOrdinal++; 782 LoadCommandType lcType = 783 dylibFile->forceWeakImport || dylibFile->refState == RefState::Weak 784 ? LC_LOAD_WEAK_DYLIB 785 : LC_LOAD_DYLIB; 786 in.header->addLoadCommand(make<LCDylib>(lcType, dylibFile->installName, 787 dylibFile->compatibilityVersion, 788 dylibFile->currentVersion)); 789 790 if (dylibFile->reexport) 791 in.header->addLoadCommand( 792 make<LCDylib>(LC_REEXPORT_DYLIB, dylibFile->installName)); 793 } 794 } 795 796 if (codeSignatureSection) 797 in.header->addLoadCommand(make<LCCodeSignature>(codeSignatureSection)); 798 799 const uint32_t MACOS_MAXPATHLEN = 1024; 800 config->headerPad = std::max( 801 config->headerPad, (config->headerPadMaxInstallNames 802 ? LCDylib::getInstanceCount() * MACOS_MAXPATHLEN 803 : 0)); 804 } 805 806 static size_t getSymbolPriority(const SymbolPriorityEntry &entry, 807 const InputFile *f) { 808 // We don't use toString(InputFile *) here because it returns the full path 809 // for object files, and we only want the basename. 810 StringRef filename; 811 if (f->archiveName.empty()) 812 filename = path::filename(f->getName()); 813 else 814 filename = saver.save(path::filename(f->archiveName) + "(" + 815 path::filename(f->getName()) + ")"); 816 return std::max(entry.objectFiles.lookup(filename), entry.anyObjectFile); 817 } 818 819 // Each section gets assigned the priority of the highest-priority symbol it 820 // contains. 821 static DenseMap<const InputSection *, size_t> buildInputSectionPriorities() { 822 DenseMap<const InputSection *, size_t> sectionPriorities; 823 824 if (config->priorities.empty()) 825 return sectionPriorities; 826 827 auto addSym = [&](Defined &sym) { 828 auto it = config->priorities.find(sym.getName()); 829 if (it == config->priorities.end()) 830 return; 831 832 SymbolPriorityEntry &entry = it->second; 833 size_t &priority = sectionPriorities[sym.isec]; 834 priority = 835 std::max(priority, getSymbolPriority(entry, sym.isec->getFile())); 836 }; 837 838 // TODO: Make sure this handles weak symbols correctly. 839 for (const InputFile *file : inputFiles) { 840 if (isa<ObjFile>(file)) 841 for (Symbol *sym : file->symbols) 842 if (auto *d = dyn_cast_or_null<Defined>(sym)) 843 addSym(*d); 844 } 845 846 return sectionPriorities; 847 } 848 849 // Sorting only can happen once all outputs have been collected. Here we sort 850 // segments, output sections within each segment, and input sections within each 851 // output segment. 852 static void sortSegmentsAndSections() { 853 TimeTraceScope timeScope("Sort segments and sections"); 854 sortOutputSegments(); 855 856 DenseMap<const InputSection *, size_t> isecPriorities = 857 buildInputSectionPriorities(); 858 859 uint32_t sectionIndex = 0; 860 for (OutputSegment *seg : outputSegments) { 861 seg->sortOutputSections(); 862 for (OutputSection *osec : seg->getSections()) { 863 // Now that the output sections are sorted, assign the final 864 // output section indices. 865 if (!osec->isHidden()) 866 osec->index = ++sectionIndex; 867 if (!firstTLVDataSection && isThreadLocalData(osec->flags)) 868 firstTLVDataSection = osec; 869 870 if (!isecPriorities.empty()) { 871 if (auto *merged = dyn_cast<ConcatOutputSection>(osec)) { 872 llvm::stable_sort(merged->inputs, 873 [&](InputSection *a, InputSection *b) { 874 return isecPriorities[a] > isecPriorities[b]; 875 }); 876 } 877 } 878 } 879 } 880 } 881 882 NamePair macho::maybeRenameSection(NamePair key) { 883 auto newNames = config->sectionRenameMap.find(key); 884 if (newNames != config->sectionRenameMap.end()) 885 return newNames->second; 886 auto newName = config->segmentRenameMap.find(key.first); 887 if (newName != config->segmentRenameMap.end()) 888 return std::make_pair(newName->second, key.second); 889 return key; 890 } 891 892 template <class LP> void Writer::createOutputSections() { 893 TimeTraceScope timeScope("Create output sections"); 894 // First, create hidden sections 895 stringTableSection = make<StringTableSection>(); 896 symtabSection = makeSymtabSection<LP>(*stringTableSection); 897 indirectSymtabSection = make<IndirectSymtabSection>(); 898 if (config->adhocCodesign) 899 codeSignatureSection = make<CodeSignatureSection>(); 900 if (config->emitDataInCodeInfo) 901 dataInCodeSection = make<DataInCodeSection>(); 902 if (config->emitFunctionStarts) 903 functionStartsSection = make<FunctionStartsSection>(); 904 if (config->emitBitcodeBundle) 905 make<BitcodeBundleSection>(); 906 907 switch (config->outputType) { 908 case MH_EXECUTE: 909 make<PageZeroSection>(); 910 break; 911 case MH_DYLIB: 912 case MH_BUNDLE: 913 break; 914 default: 915 llvm_unreachable("unhandled output file type"); 916 } 917 918 // Then add input sections to output sections. 919 for (ConcatInputSection *isec : inputSections) { 920 if (isec->shouldOmitFromOutput()) 921 continue; 922 NamePair names = maybeRenameSection({isec->getSegName(), isec->getName()}); 923 ConcatOutputSection *&osec = concatOutputSections[names]; 924 if (!osec) 925 osec = make<ConcatOutputSection>(names.second); 926 osec->addInput(isec); 927 osec->inputOrder = 928 std::min(osec->inputOrder, static_cast<int>(isec->outSecOff)); 929 } 930 931 // Once all the inputs are added, we can finalize the output section 932 // properties and create the corresponding output segments. 933 for (const auto &it : concatOutputSections) { 934 StringRef segname = it.first.first; 935 ConcatOutputSection *osec = it.second; 936 assert(segname != segment_names::ld); 937 getOrCreateOutputSegment(segname)->addOutputSection(osec); 938 } 939 940 for (SyntheticSection *ssec : syntheticSections) { 941 auto it = concatOutputSections.find({ssec->segname, ssec->name}); 942 if (ssec->isNeeded()) { 943 if (it == concatOutputSections.end()) { 944 getOrCreateOutputSegment(ssec->segname)->addOutputSection(ssec); 945 } else { 946 fatal("section from " + 947 toString(it->second->firstSection()->getFile()) + 948 " conflicts with synthetic section " + ssec->segname + "," + 949 ssec->name); 950 } 951 } 952 } 953 954 // dyld requires __LINKEDIT segment to always exist (even if empty). 955 linkEditSegment = getOrCreateOutputSegment(segment_names::linkEdit); 956 } 957 958 void Writer::finalizeAddresses() { 959 TimeTraceScope timeScope("Finalize addresses"); 960 uint64_t pageSize = target->getPageSize(); 961 // Ensure that segments (and the sections they contain) are allocated 962 // addresses in ascending order, which dyld requires. 963 // 964 // Note that at this point, __LINKEDIT sections are empty, but we need to 965 // determine addresses of other segments/sections before generating its 966 // contents. 967 for (OutputSegment *seg : outputSegments) { 968 if (seg == linkEditSegment) 969 continue; 970 assignAddresses(seg); 971 // codesign / libstuff checks for segment ordering by verifying that 972 // `fileOff + fileSize == next segment fileOff`. So we call alignTo() before 973 // (instead of after) computing fileSize to ensure that the segments are 974 // contiguous. We handle addr / vmSize similarly for the same reason. 975 fileOff = alignTo(fileOff, pageSize); 976 addr = alignTo(addr, pageSize); 977 seg->vmSize = addr - seg->firstSection()->addr; 978 seg->fileSize = fileOff - seg->fileOff; 979 } 980 } 981 982 void Writer::finalizeLinkEditSegment() { 983 TimeTraceScope timeScope("Finalize __LINKEDIT segment"); 984 // Fill __LINKEDIT contents. 985 std::vector<LinkEditSection *> linkEditSections{ 986 in.rebase, 987 in.binding, 988 in.weakBinding, 989 in.lazyBinding, 990 in.exports, 991 symtabSection, 992 indirectSymtabSection, 993 dataInCodeSection, 994 functionStartsSection, 995 }; 996 parallelForEach(linkEditSections, [](LinkEditSection *osec) { 997 if (osec) 998 osec->finalizeContents(); 999 }); 1000 1001 // Now that __LINKEDIT is filled out, do a proper calculation of its 1002 // addresses and offsets. 1003 assignAddresses(linkEditSegment); 1004 // No need to page-align fileOff / addr here since this is the last segment. 1005 linkEditSegment->vmSize = addr - linkEditSegment->firstSection()->addr; 1006 linkEditSegment->fileSize = fileOff - linkEditSegment->fileOff; 1007 } 1008 1009 void Writer::assignAddresses(OutputSegment *seg) { 1010 seg->fileOff = fileOff; 1011 1012 for (OutputSection *osec : seg->getSections()) { 1013 if (!osec->isNeeded()) 1014 continue; 1015 addr = alignTo(addr, osec->align); 1016 fileOff = alignTo(fileOff, osec->align); 1017 osec->addr = addr; 1018 osec->fileOff = isZeroFill(osec->flags) ? 0 : fileOff; 1019 osec->finalize(); 1020 1021 addr += osec->getSize(); 1022 fileOff += osec->getFileSize(); 1023 } 1024 } 1025 1026 void Writer::openFile() { 1027 Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr = 1028 FileOutputBuffer::create(config->outputFile, fileOff, 1029 FileOutputBuffer::F_executable); 1030 1031 if (!bufferOrErr) 1032 error("failed to open " + config->outputFile + ": " + 1033 llvm::toString(bufferOrErr.takeError())); 1034 else 1035 buffer = std::move(*bufferOrErr); 1036 } 1037 1038 void Writer::writeSections() { 1039 uint8_t *buf = buffer->getBufferStart(); 1040 for (const OutputSegment *seg : outputSegments) 1041 for (const OutputSection *osec : seg->getSections()) 1042 osec->writeTo(buf + osec->fileOff); 1043 } 1044 1045 // In order to utilize multiple cores, we first split the buffer into chunks, 1046 // compute a hash for each chunk, and then compute a hash value of the hash 1047 // values. 1048 void Writer::writeUuid() { 1049 TimeTraceScope timeScope("Computing UUID"); 1050 ArrayRef<uint8_t> data{buffer->getBufferStart(), buffer->getBufferEnd()}; 1051 unsigned chunkCount = parallel::strategy.compute_thread_count() * 10; 1052 // Round-up integer division 1053 size_t chunkSize = (data.size() + chunkCount - 1) / chunkCount; 1054 std::vector<ArrayRef<uint8_t>> chunks = split(data, chunkSize); 1055 std::vector<uint64_t> hashes(chunks.size()); 1056 parallelForEachN(0, chunks.size(), 1057 [&](size_t i) { hashes[i] = xxHash64(chunks[i]); }); 1058 uint64_t digest = xxHash64({reinterpret_cast<uint8_t *>(hashes.data()), 1059 hashes.size() * sizeof(uint64_t)}); 1060 uuidCommand->writeUuid(digest); 1061 } 1062 1063 void Writer::writeCodeSignature() { 1064 if (codeSignatureSection) 1065 codeSignatureSection->writeHashes(buffer->getBufferStart()); 1066 } 1067 1068 void Writer::writeOutputFile() { 1069 TimeTraceScope timeScope("Write output file"); 1070 openFile(); 1071 if (errorCount()) 1072 return; 1073 writeSections(); 1074 writeUuid(); 1075 writeCodeSignature(); 1076 1077 if (auto e = buffer->commit()) 1078 error("failed to write to the output file: " + toString(std::move(e))); 1079 } 1080 1081 template <class LP> void Writer::run() { 1082 if (config->entry && !isa<Undefined>(config->entry)) 1083 prepareBranchTarget(config->entry); 1084 scanRelocations(); 1085 if (in.stubHelper->isNeeded()) 1086 in.stubHelper->setup(); 1087 scanSymbols(); 1088 createOutputSections<LP>(); 1089 // After this point, we create no new segments; HOWEVER, we might 1090 // yet create branch-range extension thunks for architectures whose 1091 // hardware call instructions have limited range, e.g., ARM(64). 1092 // The thunks are created as InputSections interspersed among 1093 // the ordinary __TEXT,_text InputSections. 1094 sortSegmentsAndSections(); 1095 createLoadCommands<LP>(); 1096 finalizeAddresses(); 1097 finalizeLinkEditSegment(); 1098 writeMapFile(); 1099 writeOutputFile(); 1100 } 1101 1102 template <class LP> void macho::writeResult() { Writer().run<LP>(); } 1103 1104 void macho::createSyntheticSections() { 1105 in.header = make<MachHeaderSection>(); 1106 if (config->dedupLiterals) { 1107 in.cStringSection = make<DeduplicatedCStringSection>(); 1108 } else { 1109 in.cStringSection = make<CStringSection>(); 1110 } 1111 in.wordLiteralSection = 1112 config->dedupLiterals ? make<WordLiteralSection>() : nullptr; 1113 in.rebase = make<RebaseSection>(); 1114 in.binding = make<BindingSection>(); 1115 in.weakBinding = make<WeakBindingSection>(); 1116 in.lazyBinding = make<LazyBindingSection>(); 1117 in.exports = make<ExportSection>(); 1118 in.got = make<GotSection>(); 1119 in.tlvPointers = make<TlvPointerSection>(); 1120 in.lazyPointers = make<LazyPointerSection>(); 1121 in.stubs = make<StubsSection>(); 1122 in.stubHelper = make<StubHelperSection>(); 1123 in.unwindInfo = makeUnwindInfoSection(); 1124 1125 // This section contains space for just a single word, and will be used by 1126 // dyld to cache an address to the image loader it uses. 1127 uint8_t *arr = bAlloc.Allocate<uint8_t>(target->wordSize); 1128 memset(arr, 0, target->wordSize); 1129 in.imageLoaderCache = make<ConcatInputSection>( 1130 segment_names::data, section_names::data, /*file=*/nullptr, 1131 ArrayRef<uint8_t>{arr, target->wordSize}, 1132 /*align=*/target->wordSize, /*flags=*/S_REGULAR); 1133 // References from dyld are not visible to us, so ensure this section is 1134 // always treated as live. 1135 in.imageLoaderCache->live = true; 1136 } 1137 1138 OutputSection *macho::firstTLVDataSection = nullptr; 1139 1140 template void macho::writeResult<LP64>(); 1141 template void macho::writeResult<ILP32>(); 1142