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