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