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