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 c->vmaddr = seg->addr; 243 c->vmsize = seg->vmSize; 244 c->filesize = seg->fileSize; 245 c->nsects = seg->numNonHiddenSections(); 246 247 for (const OutputSection *osec : seg->getSections()) { 248 if (osec->isHidden()) 249 continue; 250 251 auto *sectHdr = reinterpret_cast<Section *>(buf); 252 buf += sizeof(Section); 253 254 memcpy(sectHdr->sectname, osec->name.data(), osec->name.size()); 255 memcpy(sectHdr->segname, name.data(), name.size()); 256 257 sectHdr->addr = osec->addr; 258 sectHdr->offset = osec->fileOff; 259 sectHdr->align = Log2_32(osec->align); 260 sectHdr->flags = osec->flags; 261 sectHdr->size = osec->getSize(); 262 sectHdr->reserved1 = osec->reserved1; 263 sectHdr->reserved2 = osec->reserved2; 264 } 265 } 266 267 private: 268 StringRef name; 269 OutputSegment *seg; 270 }; 271 272 class LCMain final : public LoadCommand { 273 uint32_t getSize() const override { 274 return sizeof(structs::entry_point_command); 275 } 276 277 void writeTo(uint8_t *buf) const override { 278 auto *c = reinterpret_cast<structs::entry_point_command *>(buf); 279 c->cmd = LC_MAIN; 280 c->cmdsize = getSize(); 281 282 if (config->entry->isInStubs()) 283 c->entryoff = 284 in.stubs->fileOff + config->entry->stubsIndex * target->stubSize; 285 else 286 c->entryoff = config->entry->getVA() - in.header->addr; 287 288 c->stacksize = 0; 289 } 290 }; 291 292 class LCSymtab final : public LoadCommand { 293 public: 294 LCSymtab(SymtabSection *symtabSection, StringTableSection *stringTableSection) 295 : symtabSection(symtabSection), stringTableSection(stringTableSection) {} 296 297 uint32_t getSize() const override { return sizeof(symtab_command); } 298 299 void writeTo(uint8_t *buf) const override { 300 auto *c = reinterpret_cast<symtab_command *>(buf); 301 c->cmd = LC_SYMTAB; 302 c->cmdsize = getSize(); 303 c->symoff = symtabSection->fileOff; 304 c->nsyms = symtabSection->getNumSymbols(); 305 c->stroff = stringTableSection->fileOff; 306 c->strsize = stringTableSection->getFileSize(); 307 } 308 309 SymtabSection *symtabSection = nullptr; 310 StringTableSection *stringTableSection = nullptr; 311 }; 312 313 // There are several dylib load commands that share the same structure: 314 // * LC_LOAD_DYLIB 315 // * LC_ID_DYLIB 316 // * LC_REEXPORT_DYLIB 317 class LCDylib final : public LoadCommand { 318 public: 319 LCDylib(LoadCommandType type, StringRef path, 320 uint32_t compatibilityVersion = 0, uint32_t currentVersion = 0) 321 : type(type), path(path), compatibilityVersion(compatibilityVersion), 322 currentVersion(currentVersion) { 323 instanceCount++; 324 } 325 326 uint32_t getSize() const override { 327 return alignTo(sizeof(dylib_command) + path.size() + 1, 8); 328 } 329 330 void writeTo(uint8_t *buf) const override { 331 auto *c = reinterpret_cast<dylib_command *>(buf); 332 buf += sizeof(dylib_command); 333 334 c->cmd = type; 335 c->cmdsize = getSize(); 336 c->dylib.name = sizeof(dylib_command); 337 c->dylib.timestamp = 0; 338 c->dylib.compatibility_version = compatibilityVersion; 339 c->dylib.current_version = currentVersion; 340 341 memcpy(buf, path.data(), path.size()); 342 buf[path.size()] = '\0'; 343 } 344 345 static uint32_t getInstanceCount() { return instanceCount; } 346 347 private: 348 LoadCommandType type; 349 StringRef path; 350 uint32_t compatibilityVersion; 351 uint32_t currentVersion; 352 static uint32_t instanceCount; 353 }; 354 355 uint32_t LCDylib::instanceCount = 0; 356 357 class LCLoadDylinker final : public LoadCommand { 358 public: 359 uint32_t getSize() const override { 360 return alignTo(sizeof(dylinker_command) + path.size() + 1, 8); 361 } 362 363 void writeTo(uint8_t *buf) const override { 364 auto *c = reinterpret_cast<dylinker_command *>(buf); 365 buf += sizeof(dylinker_command); 366 367 c->cmd = LC_LOAD_DYLINKER; 368 c->cmdsize = getSize(); 369 c->name = sizeof(dylinker_command); 370 371 memcpy(buf, path.data(), path.size()); 372 buf[path.size()] = '\0'; 373 } 374 375 private: 376 // Recent versions of Darwin won't run any binary that has dyld at a 377 // different location. 378 const StringRef path = "/usr/lib/dyld"; 379 }; 380 381 class LCRPath final : public LoadCommand { 382 public: 383 explicit LCRPath(StringRef path) : path(path) {} 384 385 uint32_t getSize() const override { 386 return alignTo(sizeof(rpath_command) + path.size() + 1, target->wordSize); 387 } 388 389 void writeTo(uint8_t *buf) const override { 390 auto *c = reinterpret_cast<rpath_command *>(buf); 391 buf += sizeof(rpath_command); 392 393 c->cmd = LC_RPATH; 394 c->cmdsize = getSize(); 395 c->path = sizeof(rpath_command); 396 397 memcpy(buf, path.data(), path.size()); 398 buf[path.size()] = '\0'; 399 } 400 401 private: 402 StringRef path; 403 }; 404 405 class LCMinVersion final : public LoadCommand { 406 public: 407 explicit LCMinVersion(const PlatformInfo &platformInfo) 408 : platformInfo(platformInfo) {} 409 410 uint32_t getSize() const override { return sizeof(version_min_command); } 411 412 void writeTo(uint8_t *buf) const override { 413 auto *c = reinterpret_cast<version_min_command *>(buf); 414 switch (platformInfo.target.Platform) { 415 case PlatformKind::macOS: 416 c->cmd = LC_VERSION_MIN_MACOSX; 417 break; 418 case PlatformKind::iOS: 419 case PlatformKind::iOSSimulator: 420 c->cmd = LC_VERSION_MIN_IPHONEOS; 421 break; 422 case PlatformKind::tvOS: 423 case PlatformKind::tvOSSimulator: 424 c->cmd = LC_VERSION_MIN_TVOS; 425 break; 426 case PlatformKind::watchOS: 427 case PlatformKind::watchOSSimulator: 428 c->cmd = LC_VERSION_MIN_WATCHOS; 429 break; 430 default: 431 llvm_unreachable("invalid platform"); 432 break; 433 } 434 c->cmdsize = getSize(); 435 c->version = encodeVersion(platformInfo.minimum); 436 c->sdk = encodeVersion(platformInfo.sdk); 437 } 438 439 private: 440 const PlatformInfo &platformInfo; 441 }; 442 443 class LCBuildVersion final : public LoadCommand { 444 public: 445 explicit LCBuildVersion(const PlatformInfo &platformInfo) 446 : platformInfo(platformInfo) {} 447 448 const int ntools = 1; 449 450 uint32_t getSize() const override { 451 return sizeof(build_version_command) + ntools * sizeof(build_tool_version); 452 } 453 454 void writeTo(uint8_t *buf) const override { 455 auto *c = reinterpret_cast<build_version_command *>(buf); 456 c->cmd = LC_BUILD_VERSION; 457 c->cmdsize = getSize(); 458 c->platform = static_cast<uint32_t>(platformInfo.target.Platform); 459 c->minos = encodeVersion(platformInfo.minimum); 460 c->sdk = encodeVersion(platformInfo.sdk); 461 c->ntools = ntools; 462 auto *t = reinterpret_cast<build_tool_version *>(&c[1]); 463 t->tool = TOOL_LD; 464 t->version = encodeVersion(VersionTuple( 465 LLVM_VERSION_MAJOR, LLVM_VERSION_MINOR, LLVM_VERSION_PATCH)); 466 } 467 468 private: 469 const PlatformInfo &platformInfo; 470 }; 471 472 // Stores a unique identifier for the output file based on an MD5 hash of its 473 // contents. In order to hash the contents, we must first write them, but 474 // LC_UUID itself must be part of the written contents in order for all the 475 // offsets to be calculated correctly. We resolve this circular paradox by 476 // first writing an LC_UUID with an all-zero UUID, then updating the UUID with 477 // its real value later. 478 class LCUuid final : public LoadCommand { 479 public: 480 uint32_t getSize() const override { return sizeof(uuid_command); } 481 482 void writeTo(uint8_t *buf) const override { 483 auto *c = reinterpret_cast<uuid_command *>(buf); 484 c->cmd = LC_UUID; 485 c->cmdsize = getSize(); 486 uuidBuf = c->uuid; 487 } 488 489 void writeUuid(uint64_t digest) const { 490 // xxhash only gives us 8 bytes, so put some fixed data in the other half. 491 static_assert(sizeof(uuid_command::uuid) == 16, "unexpected uuid size"); 492 memcpy(uuidBuf, "LLD\xa1UU1D", 8); 493 memcpy(uuidBuf + 8, &digest, 8); 494 495 // RFC 4122 conformance. We need to fix 4 bits in byte 6 and 2 bits in 496 // byte 8. Byte 6 is already fine due to the fixed data we put in. We don't 497 // want to lose bits of the digest in byte 8, so swap that with a byte of 498 // fixed data that happens to have the right bits set. 499 std::swap(uuidBuf[3], uuidBuf[8]); 500 501 // Claim that this is an MD5-based hash. It isn't, but this signals that 502 // this is not a time-based and not a random hash. MD5 seems like the least 503 // bad lie we can put here. 504 assert((uuidBuf[6] & 0xf0) == 0x30 && "See RFC 4122 Sections 4.2.2, 4.1.3"); 505 assert((uuidBuf[8] & 0xc0) == 0x80 && "See RFC 4122 Section 4.2.2"); 506 } 507 508 mutable uint8_t *uuidBuf; 509 }; 510 511 template <class LP> class LCEncryptionInfo final : public LoadCommand { 512 public: 513 uint32_t getSize() const override { 514 return sizeof(typename LP::encryption_info_command); 515 } 516 517 void writeTo(uint8_t *buf) const override { 518 using EncryptionInfo = typename LP::encryption_info_command; 519 auto *c = reinterpret_cast<EncryptionInfo *>(buf); 520 buf += sizeof(EncryptionInfo); 521 c->cmd = LP::encryptionInfoLCType; 522 c->cmdsize = getSize(); 523 c->cryptoff = in.header->getSize(); 524 auto it = find_if(outputSegments, [](const OutputSegment *seg) { 525 return seg->name == segment_names::text; 526 }); 527 assert(it != outputSegments.end()); 528 c->cryptsize = (*it)->fileSize - c->cryptoff; 529 } 530 }; 531 532 class LCCodeSignature final : public LoadCommand { 533 public: 534 LCCodeSignature(CodeSignatureSection *section) : section(section) {} 535 536 uint32_t getSize() const override { return sizeof(linkedit_data_command); } 537 538 void writeTo(uint8_t *buf) const override { 539 auto *c = reinterpret_cast<linkedit_data_command *>(buf); 540 c->cmd = LC_CODE_SIGNATURE; 541 c->cmdsize = getSize(); 542 c->dataoff = static_cast<uint32_t>(section->fileOff); 543 c->datasize = section->getSize(); 544 } 545 546 CodeSignatureSection *section; 547 }; 548 549 } // namespace 550 551 void Writer::treatSpecialUndefineds() { 552 if (config->entry) 553 if (auto *undefined = dyn_cast<Undefined>(config->entry)) 554 treatUndefinedSymbol(*undefined, "the entry point"); 555 556 // FIXME: This prints symbols that are undefined both in input files and 557 // via -u flag twice. 558 for (const Symbol *sym : config->explicitUndefineds) { 559 if (const auto *undefined = dyn_cast<Undefined>(sym)) 560 treatUndefinedSymbol(*undefined, "-u"); 561 } 562 // Literal exported-symbol names must be defined, but glob 563 // patterns need not match. 564 for (const CachedHashStringRef &cachedName : 565 config->exportedSymbols.literals) { 566 if (const Symbol *sym = symtab->find(cachedName)) 567 if (const auto *undefined = dyn_cast<Undefined>(sym)) 568 treatUndefinedSymbol(*undefined, "-exported_symbol(s_list)"); 569 } 570 } 571 572 // Add stubs and bindings where necessary (e.g. if the symbol is a 573 // DylibSymbol.) 574 static void prepareBranchTarget(Symbol *sym) { 575 if (auto *dysym = dyn_cast<DylibSymbol>(sym)) { 576 if (in.stubs->addEntry(dysym)) { 577 if (sym->isWeakDef()) { 578 in.binding->addEntry(dysym, in.lazyPointers->isec, 579 sym->stubsIndex * target->wordSize); 580 in.weakBinding->addEntry(sym, in.lazyPointers->isec, 581 sym->stubsIndex * target->wordSize); 582 } else { 583 in.lazyBinding->addEntry(dysym); 584 } 585 } 586 } else if (auto *defined = dyn_cast<Defined>(sym)) { 587 if (defined->isExternalWeakDef()) { 588 if (in.stubs->addEntry(sym)) { 589 in.rebase->addEntry(in.lazyPointers->isec, 590 sym->stubsIndex * target->wordSize); 591 in.weakBinding->addEntry(sym, in.lazyPointers->isec, 592 sym->stubsIndex * target->wordSize); 593 } 594 } 595 } else { 596 llvm_unreachable("invalid branch target symbol type"); 597 } 598 } 599 600 // Can a symbol's address can only be resolved at runtime? 601 static bool needsBinding(const Symbol *sym) { 602 if (isa<DylibSymbol>(sym)) 603 return true; 604 if (const auto *defined = dyn_cast<Defined>(sym)) 605 return defined->isExternalWeakDef(); 606 return false; 607 } 608 609 static void prepareSymbolRelocation(Symbol *sym, const InputSection *isec, 610 const Reloc &r) { 611 assert(sym->isLive()); 612 const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type); 613 614 if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) { 615 prepareBranchTarget(sym); 616 } else if (relocAttrs.hasAttr(RelocAttrBits::GOT)) { 617 if (relocAttrs.hasAttr(RelocAttrBits::POINTER) || needsBinding(sym)) 618 in.got->addEntry(sym); 619 } else if (relocAttrs.hasAttr(RelocAttrBits::TLV)) { 620 if (needsBinding(sym)) 621 in.tlvPointers->addEntry(sym); 622 } else if (relocAttrs.hasAttr(RelocAttrBits::UNSIGNED)) { 623 // References from thread-local variable sections are treated as offsets 624 // relative to the start of the referent section, and therefore have no 625 // need of rebase opcodes. 626 if (!(isThreadLocalVariables(isec->getFlags()) && isa<Defined>(sym))) 627 addNonLazyBindingEntries(sym, isec, r.offset, r.addend); 628 } 629 } 630 631 void Writer::scanRelocations() { 632 TimeTraceScope timeScope("Scan relocations"); 633 for (ConcatInputSection *isec : inputSections) { 634 if (isec->shouldOmitFromOutput()) 635 continue; 636 637 for (auto it = isec->relocs.begin(); it != isec->relocs.end(); ++it) { 638 Reloc &r = *it; 639 if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) { 640 // Skip over the following UNSIGNED relocation -- it's just there as the 641 // minuend, and doesn't have the usual UNSIGNED semantics. We don't want 642 // to emit rebase opcodes for it. 643 it++; 644 continue; 645 } 646 if (auto *sym = r.referent.dyn_cast<Symbol *>()) { 647 if (auto *undefined = dyn_cast<Undefined>(sym)) 648 treatUndefinedSymbol(*undefined); 649 // treatUndefinedSymbol() can replace sym with a DylibSymbol; re-check. 650 if (!isa<Undefined>(sym) && validateSymbolRelocation(sym, isec, r)) 651 prepareSymbolRelocation(sym, isec, r); 652 } else { 653 // Canonicalize the referent so that later accesses in Writer won't 654 // have to worry about it. Perhaps we should do this for Defined::isec 655 // too... 656 auto *referentIsec = r.referent.get<InputSection *>(); 657 r.referent = referentIsec->canonical(); 658 if (!r.pcrel) 659 in.rebase->addEntry(isec, r.offset); 660 } 661 } 662 } 663 664 in.unwindInfo->prepareRelocations(); 665 } 666 667 void Writer::scanSymbols() { 668 TimeTraceScope timeScope("Scan symbols"); 669 for (const Symbol *sym : symtab->getSymbols()) { 670 if (const auto *defined = dyn_cast<Defined>(sym)) { 671 if (defined->overridesWeakDef && defined->isLive()) 672 in.weakBinding->addNonWeakDefinition(defined); 673 } else if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) { 674 // This branch intentionally doesn't check isLive(). 675 if (dysym->isDynamicLookup()) 676 continue; 677 dysym->getFile()->refState = 678 std::max(dysym->getFile()->refState, dysym->getRefState()); 679 } 680 } 681 } 682 683 // TODO: ld64 enforces the old load commands in a few other cases. 684 static bool useLCBuildVersion(const PlatformInfo &platformInfo) { 685 static const std::vector<std::pair<PlatformKind, VersionTuple>> minVersion = { 686 {PlatformKind::macOS, VersionTuple(10, 14)}, 687 {PlatformKind::iOS, VersionTuple(12, 0)}, 688 {PlatformKind::iOSSimulator, VersionTuple(13, 0)}, 689 {PlatformKind::tvOS, VersionTuple(12, 0)}, 690 {PlatformKind::tvOSSimulator, VersionTuple(13, 0)}, 691 {PlatformKind::watchOS, VersionTuple(5, 0)}, 692 {PlatformKind::watchOSSimulator, VersionTuple(6, 0)}}; 693 auto it = llvm::find_if(minVersion, [&](const auto &p) { 694 return p.first == platformInfo.target.Platform; 695 }); 696 return it == minVersion.end() ? true : platformInfo.minimum >= it->second; 697 } 698 699 template <class LP> void Writer::createLoadCommands() { 700 uint8_t segIndex = 0; 701 for (OutputSegment *seg : outputSegments) { 702 in.header->addLoadCommand(make<LCSegment<LP>>(seg->name, seg)); 703 seg->index = segIndex++; 704 } 705 706 in.header->addLoadCommand(make<LCDyldInfo>( 707 in.rebase, in.binding, in.weakBinding, in.lazyBinding, in.exports)); 708 in.header->addLoadCommand(make<LCSymtab>(symtabSection, stringTableSection)); 709 in.header->addLoadCommand( 710 make<LCDysymtab>(symtabSection, indirectSymtabSection)); 711 if (!config->umbrella.empty()) 712 in.header->addLoadCommand(make<LCSubFramework>(config->umbrella)); 713 if (config->emitEncryptionInfo) 714 in.header->addLoadCommand(make<LCEncryptionInfo<LP>>()); 715 for (StringRef path : config->runtimePaths) 716 in.header->addLoadCommand(make<LCRPath>(path)); 717 718 switch (config->outputType) { 719 case MH_EXECUTE: 720 in.header->addLoadCommand(make<LCLoadDylinker>()); 721 break; 722 case MH_DYLIB: 723 in.header->addLoadCommand(make<LCDylib>(LC_ID_DYLIB, config->installName, 724 config->dylibCompatibilityVersion, 725 config->dylibCurrentVersion)); 726 break; 727 case MH_BUNDLE: 728 break; 729 default: 730 llvm_unreachable("unhandled output file type"); 731 } 732 733 uuidCommand = make<LCUuid>(); 734 in.header->addLoadCommand(uuidCommand); 735 736 if (useLCBuildVersion(config->platformInfo)) 737 in.header->addLoadCommand(make<LCBuildVersion>(config->platformInfo)); 738 else 739 in.header->addLoadCommand(make<LCMinVersion>(config->platformInfo)); 740 741 // This is down here to match ld64's load command order. 742 if (config->outputType == MH_EXECUTE) 743 in.header->addLoadCommand(make<LCMain>()); 744 745 int64_t dylibOrdinal = 1; 746 DenseMap<StringRef, int64_t> ordinalForInstallName; 747 for (InputFile *file : inputFiles) { 748 if (auto *dylibFile = dyn_cast<DylibFile>(file)) { 749 if (dylibFile->isBundleLoader) { 750 dylibFile->ordinal = BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE; 751 // Shortcut since bundle-loader does not re-export the symbols. 752 753 dylibFile->reexport = false; 754 continue; 755 } 756 757 // Don't emit load commands for a dylib that is not referenced if: 758 // - it was added implicitly (via a reexport, an LC_LOAD_DYLINKER -- 759 // if it's on the linker command line, it's explicit) 760 // - or it's marked MH_DEAD_STRIPPABLE_DYLIB 761 // - or the flag -dead_strip_dylibs is used 762 // FIXME: `isReferenced()` is currently computed before dead code 763 // stripping, so references from dead code keep a dylib alive. This 764 // matches ld64, but it's something we should do better. 765 if (!dylibFile->isReferenced() && !dylibFile->forceNeeded && 766 (!dylibFile->explicitlyLinked || dylibFile->deadStrippable || 767 config->deadStripDylibs)) 768 continue; 769 770 // Several DylibFiles can have the same installName. Only emit a single 771 // load command for that installName and give all these DylibFiles the 772 // same ordinal. 773 // This can happen in several cases: 774 // - a new framework could change its installName to an older 775 // framework name via an $ld$ symbol depending on platform_version 776 // - symlinks (for example, libpthread.tbd is a symlink to libSystem.tbd; 777 // Foo.framework/Foo.tbd is usually a symlink to 778 // Foo.framework/Versions/Current/Foo.tbd, where 779 // Foo.framework/Versions/Current is usually a symlink to 780 // Foo.framework/Versions/A) 781 // - a framework can be linked both explicitly on the linker 782 // command line and implicitly as a reexport from a different 783 // framework. The re-export will usually point to the tbd file 784 // in Foo.framework/Versions/A/Foo.tbd, while the explicit link will 785 // usually find Foo.framework/Foo.tbd. These are usually symlinks, 786 // but in a --reproduce archive they will be identical but distinct 787 // files. 788 // In the first case, *semantically distinct* DylibFiles will have the 789 // same installName. 790 int64_t &ordinal = ordinalForInstallName[dylibFile->installName]; 791 if (ordinal) { 792 dylibFile->ordinal = ordinal; 793 continue; 794 } 795 796 ordinal = dylibFile->ordinal = dylibOrdinal++; 797 LoadCommandType lcType = 798 dylibFile->forceWeakImport || dylibFile->refState == RefState::Weak 799 ? LC_LOAD_WEAK_DYLIB 800 : LC_LOAD_DYLIB; 801 in.header->addLoadCommand(make<LCDylib>(lcType, dylibFile->installName, 802 dylibFile->compatibilityVersion, 803 dylibFile->currentVersion)); 804 805 if (dylibFile->reexport) 806 in.header->addLoadCommand( 807 make<LCDylib>(LC_REEXPORT_DYLIB, dylibFile->installName)); 808 } 809 } 810 811 if (functionStartsSection) 812 in.header->addLoadCommand(make<LCFunctionStarts>(functionStartsSection)); 813 if (dataInCodeSection) 814 in.header->addLoadCommand(make<LCDataInCode>(dataInCodeSection)); 815 if (codeSignatureSection) 816 in.header->addLoadCommand(make<LCCodeSignature>(codeSignatureSection)); 817 818 const uint32_t MACOS_MAXPATHLEN = 1024; 819 config->headerPad = std::max( 820 config->headerPad, (config->headerPadMaxInstallNames 821 ? LCDylib::getInstanceCount() * MACOS_MAXPATHLEN 822 : 0)); 823 } 824 825 static size_t getSymbolPriority(const SymbolPriorityEntry &entry, 826 const InputFile *f) { 827 // We don't use toString(InputFile *) here because it returns the full path 828 // for object files, and we only want the basename. 829 StringRef filename; 830 if (f->archiveName.empty()) 831 filename = path::filename(f->getName()); 832 else 833 filename = saver.save(path::filename(f->archiveName) + "(" + 834 path::filename(f->getName()) + ")"); 835 return std::max(entry.objectFiles.lookup(filename), entry.anyObjectFile); 836 } 837 838 // Each section gets assigned the priority of the highest-priority symbol it 839 // contains. 840 static DenseMap<const InputSection *, size_t> buildInputSectionPriorities() { 841 DenseMap<const InputSection *, size_t> sectionPriorities; 842 843 if (config->priorities.empty()) 844 return sectionPriorities; 845 846 auto addSym = [&](Defined &sym) { 847 if (sym.isAbsolute()) 848 return; 849 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 seg->addr = addr; 981 assignAddresses(seg); 982 // codesign / libstuff checks for segment ordering by verifying that 983 // `fileOff + fileSize == next segment fileOff`. So we call alignTo() before 984 // (instead of after) computing fileSize to ensure that the segments are 985 // contiguous. We handle addr / vmSize similarly for the same reason. 986 fileOff = alignTo(fileOff, pageSize); 987 addr = alignTo(addr, pageSize); 988 seg->vmSize = addr - seg->addr; 989 seg->fileSize = fileOff - seg->fileOff; 990 } 991 } 992 993 void Writer::finalizeLinkEditSegment() { 994 TimeTraceScope timeScope("Finalize __LINKEDIT segment"); 995 // Fill __LINKEDIT contents. 996 std::vector<LinkEditSection *> linkEditSections{ 997 in.rebase, 998 in.binding, 999 in.weakBinding, 1000 in.lazyBinding, 1001 in.exports, 1002 symtabSection, 1003 indirectSymtabSection, 1004 dataInCodeSection, 1005 functionStartsSection, 1006 }; 1007 parallelForEach(linkEditSections, [](LinkEditSection *osec) { 1008 if (osec) 1009 osec->finalizeContents(); 1010 }); 1011 1012 // Now that __LINKEDIT is filled out, do a proper calculation of its 1013 // addresses and offsets. 1014 linkEditSegment->addr = addr; 1015 assignAddresses(linkEditSegment); 1016 // No need to page-align fileOff / addr here since this is the last segment. 1017 linkEditSegment->vmSize = addr - linkEditSegment->addr; 1018 linkEditSegment->fileSize = fileOff - linkEditSegment->fileOff; 1019 } 1020 1021 void Writer::assignAddresses(OutputSegment *seg) { 1022 seg->fileOff = fileOff; 1023 1024 for (OutputSection *osec : seg->getSections()) { 1025 if (!osec->isNeeded()) 1026 continue; 1027 addr = alignTo(addr, osec->align); 1028 fileOff = alignTo(fileOff, osec->align); 1029 osec->addr = addr; 1030 osec->fileOff = isZeroFill(osec->flags) ? 0 : fileOff; 1031 osec->finalize(); 1032 1033 addr += osec->getSize(); 1034 fileOff += osec->getFileSize(); 1035 } 1036 } 1037 1038 void Writer::openFile() { 1039 Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr = 1040 FileOutputBuffer::create(config->outputFile, fileOff, 1041 FileOutputBuffer::F_executable); 1042 1043 if (!bufferOrErr) 1044 error("failed to open " + config->outputFile + ": " + 1045 llvm::toString(bufferOrErr.takeError())); 1046 else 1047 buffer = std::move(*bufferOrErr); 1048 } 1049 1050 void Writer::writeSections() { 1051 uint8_t *buf = buffer->getBufferStart(); 1052 for (const OutputSegment *seg : outputSegments) 1053 for (const OutputSection *osec : seg->getSections()) 1054 osec->writeTo(buf + osec->fileOff); 1055 } 1056 1057 // In order to utilize multiple cores, we first split the buffer into chunks, 1058 // compute a hash for each chunk, and then compute a hash value of the hash 1059 // values. 1060 void Writer::writeUuid() { 1061 TimeTraceScope timeScope("Computing UUID"); 1062 ArrayRef<uint8_t> data{buffer->getBufferStart(), buffer->getBufferEnd()}; 1063 unsigned chunkCount = parallel::strategy.compute_thread_count() * 10; 1064 // Round-up integer division 1065 size_t chunkSize = (data.size() + chunkCount - 1) / chunkCount; 1066 std::vector<ArrayRef<uint8_t>> chunks = split(data, chunkSize); 1067 std::vector<uint64_t> hashes(chunks.size()); 1068 parallelForEachN(0, chunks.size(), 1069 [&](size_t i) { hashes[i] = xxHash64(chunks[i]); }); 1070 uint64_t digest = xxHash64({reinterpret_cast<uint8_t *>(hashes.data()), 1071 hashes.size() * sizeof(uint64_t)}); 1072 uuidCommand->writeUuid(digest); 1073 } 1074 1075 void Writer::writeCodeSignature() { 1076 if (codeSignatureSection) 1077 codeSignatureSection->writeHashes(buffer->getBufferStart()); 1078 } 1079 1080 void Writer::writeOutputFile() { 1081 TimeTraceScope timeScope("Write output file"); 1082 openFile(); 1083 if (errorCount()) 1084 return; 1085 writeSections(); 1086 writeUuid(); 1087 writeCodeSignature(); 1088 1089 if (auto e = buffer->commit()) 1090 error("failed to write to the output file: " + toString(std::move(e))); 1091 } 1092 1093 template <class LP> void Writer::run() { 1094 treatSpecialUndefineds(); 1095 if (config->entry && !isa<Undefined>(config->entry)) 1096 prepareBranchTarget(config->entry); 1097 scanRelocations(); 1098 if (in.stubHelper->isNeeded()) 1099 in.stubHelper->setup(); 1100 scanSymbols(); 1101 createOutputSections<LP>(); 1102 // After this point, we create no new segments; HOWEVER, we might 1103 // yet create branch-range extension thunks for architectures whose 1104 // hardware call instructions have limited range, e.g., ARM(64). 1105 // The thunks are created as InputSections interspersed among 1106 // the ordinary __TEXT,_text InputSections. 1107 sortSegmentsAndSections(); 1108 createLoadCommands<LP>(); 1109 finalizeAddresses(); 1110 finalizeLinkEditSegment(); 1111 writeMapFile(); 1112 writeOutputFile(); 1113 } 1114 1115 template <class LP> void macho::writeResult() { Writer().run<LP>(); } 1116 1117 void macho::createSyntheticSections() { 1118 in.header = make<MachHeaderSection>(); 1119 if (config->dedupLiterals) { 1120 in.cStringSection = make<DeduplicatedCStringSection>(); 1121 } else { 1122 in.cStringSection = make<CStringSection>(); 1123 } 1124 in.wordLiteralSection = 1125 config->dedupLiterals ? make<WordLiteralSection>() : nullptr; 1126 in.rebase = make<RebaseSection>(); 1127 in.binding = make<BindingSection>(); 1128 in.weakBinding = make<WeakBindingSection>(); 1129 in.lazyBinding = make<LazyBindingSection>(); 1130 in.exports = make<ExportSection>(); 1131 in.got = make<GotSection>(); 1132 in.tlvPointers = make<TlvPointerSection>(); 1133 in.lazyPointers = make<LazyPointerSection>(); 1134 in.stubs = make<StubsSection>(); 1135 in.stubHelper = make<StubHelperSection>(); 1136 in.unwindInfo = makeUnwindInfoSection(); 1137 1138 // This section contains space for just a single word, and will be used by 1139 // dyld to cache an address to the image loader it uses. 1140 uint8_t *arr = bAlloc.Allocate<uint8_t>(target->wordSize); 1141 memset(arr, 0, target->wordSize); 1142 in.imageLoaderCache = make<ConcatInputSection>( 1143 segment_names::data, section_names::data, /*file=*/nullptr, 1144 ArrayRef<uint8_t>{arr, target->wordSize}, 1145 /*align=*/target->wordSize, /*flags=*/S_REGULAR); 1146 // References from dyld are not visible to us, so ensure this section is 1147 // always treated as live. 1148 in.imageLoaderCache->live = true; 1149 } 1150 1151 OutputSection *macho::firstTLVDataSection = nullptr; 1152 1153 template void macho::writeResult<LP64>(); 1154 template void macho::writeResult<ILP32>(); 1155