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