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