1 //===- lib/MC/MachObjectWriter.cpp - Mach-O File Writer -------------------===// 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 "llvm/ADT/DenseMap.h" 10 #include "llvm/ADT/Twine.h" 11 #include "llvm/ADT/iterator_range.h" 12 #include "llvm/BinaryFormat/MachO.h" 13 #include "llvm/MC/MCAsmBackend.h" 14 #include "llvm/MC/MCAsmLayout.h" 15 #include "llvm/MC/MCAsmInfoDarwin.h" 16 #include "llvm/MC/MCAssembler.h" 17 #include "llvm/MC/MCContext.h" 18 #include "llvm/MC/MCDirectives.h" 19 #include "llvm/MC/MCExpr.h" 20 #include "llvm/MC/MCFixupKindInfo.h" 21 #include "llvm/MC/MCFragment.h" 22 #include "llvm/MC/MCMachObjectWriter.h" 23 #include "llvm/MC/MCObjectFileInfo.h" 24 #include "llvm/MC/MCObjectWriter.h" 25 #include "llvm/MC/MCSection.h" 26 #include "llvm/MC/MCSectionMachO.h" 27 #include "llvm/MC/MCSymbol.h" 28 #include "llvm/MC/MCSymbolMachO.h" 29 #include "llvm/MC/MCValue.h" 30 #include "llvm/Support/Alignment.h" 31 #include "llvm/Support/Casting.h" 32 #include "llvm/Support/Debug.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/LEB128.h" 35 #include "llvm/Support/MathExtras.h" 36 #include "llvm/Support/raw_ostream.h" 37 #include <algorithm> 38 #include <cassert> 39 #include <cstdint> 40 #include <string> 41 #include <utility> 42 #include <vector> 43 44 using namespace llvm; 45 46 #define DEBUG_TYPE "mc" 47 48 void MachObjectWriter::reset() { 49 Relocations.clear(); 50 IndirectSymBase.clear(); 51 StringTable.clear(); 52 LocalSymbolData.clear(); 53 ExternalSymbolData.clear(); 54 UndefinedSymbolData.clear(); 55 MCObjectWriter::reset(); 56 } 57 58 bool MachObjectWriter::doesSymbolRequireExternRelocation(const MCSymbol &S) { 59 // Undefined symbols are always extern. 60 if (S.isUndefined()) 61 return true; 62 63 // References to weak definitions require external relocation entries; the 64 // definition may not always be the one in the same object file. 65 if (cast<MCSymbolMachO>(S).isWeakDefinition()) 66 return true; 67 68 // Otherwise, we can use an internal relocation. 69 return false; 70 } 71 72 bool MachObjectWriter:: 73 MachSymbolData::operator<(const MachSymbolData &RHS) const { 74 return Symbol->getName() < RHS.Symbol->getName(); 75 } 76 77 bool MachObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) { 78 const MCFixupKindInfo &FKI = Asm.getBackend().getFixupKindInfo( 79 (MCFixupKind) Kind); 80 81 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel; 82 } 83 84 uint64_t MachObjectWriter::getFragmentAddress(const MCFragment *Fragment, 85 const MCAsmLayout &Layout) const { 86 return getSectionAddress(Fragment->getParent()) + 87 Layout.getFragmentOffset(Fragment); 88 } 89 90 uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S, 91 const MCAsmLayout &Layout) const { 92 // If this is a variable, then recursively evaluate now. 93 if (S.isVariable()) { 94 if (const MCConstantExpr *C = 95 dyn_cast<const MCConstantExpr>(S.getVariableValue())) 96 return C->getValue(); 97 98 MCValue Target; 99 if (!S.getVariableValue()->evaluateAsRelocatable(Target, &Layout, nullptr)) 100 report_fatal_error("unable to evaluate offset for variable '" + 101 S.getName() + "'"); 102 103 // Verify that any used symbols are defined. 104 if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined()) 105 report_fatal_error("unable to evaluate offset to undefined symbol '" + 106 Target.getSymA()->getSymbol().getName() + "'"); 107 if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined()) 108 report_fatal_error("unable to evaluate offset to undefined symbol '" + 109 Target.getSymB()->getSymbol().getName() + "'"); 110 111 uint64_t Address = Target.getConstant(); 112 if (Target.getSymA()) 113 Address += getSymbolAddress(Target.getSymA()->getSymbol(), Layout); 114 if (Target.getSymB()) 115 Address += getSymbolAddress(Target.getSymB()->getSymbol(), Layout); 116 return Address; 117 } 118 119 return getSectionAddress(S.getFragment()->getParent()) + 120 Layout.getSymbolOffset(S); 121 } 122 123 uint64_t MachObjectWriter::getPaddingSize(const MCSection *Sec, 124 const MCAsmLayout &Layout) const { 125 uint64_t EndAddr = getSectionAddress(Sec) + Layout.getSectionAddressSize(Sec); 126 unsigned Next = Sec->getLayoutOrder() + 1; 127 if (Next >= Layout.getSectionOrder().size()) 128 return 0; 129 130 const MCSection &NextSec = *Layout.getSectionOrder()[Next]; 131 if (NextSec.isVirtualSection()) 132 return 0; 133 return offsetToAlignment(EndAddr, NextSec.getAlign()); 134 } 135 136 static bool isSymbolLinkerVisible(const MCSymbol &Symbol) { 137 // Non-temporary labels should always be visible to the linker. 138 if (!Symbol.isTemporary()) 139 return true; 140 141 if (Symbol.isUsedInReloc()) 142 return true; 143 144 return false; 145 } 146 147 const MCSymbol *MachObjectWriter::getAtom(const MCSymbol &S) const { 148 // Linker visible symbols define atoms. 149 if (isSymbolLinkerVisible(S)) 150 return &S; 151 152 // Absolute and undefined symbols have no defining atom. 153 if (!S.isInSection()) 154 return nullptr; 155 156 // Non-linker visible symbols in sections which can't be atomized have no 157 // defining atom. 158 if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols( 159 *S.getFragment()->getParent())) 160 return nullptr; 161 162 // Otherwise, return the atom for the containing fragment. 163 return S.getFragment()->getAtom(); 164 } 165 166 void MachObjectWriter::writeHeader(MachO::HeaderFileType Type, 167 unsigned NumLoadCommands, 168 unsigned LoadCommandsSize, 169 bool SubsectionsViaSymbols) { 170 uint32_t Flags = 0; 171 172 if (SubsectionsViaSymbols) 173 Flags |= MachO::MH_SUBSECTIONS_VIA_SYMBOLS; 174 175 // struct mach_header (28 bytes) or 176 // struct mach_header_64 (32 bytes) 177 178 uint64_t Start = W.OS.tell(); 179 (void) Start; 180 181 W.write<uint32_t>(is64Bit() ? MachO::MH_MAGIC_64 : MachO::MH_MAGIC); 182 183 W.write<uint32_t>(TargetObjectWriter->getCPUType()); 184 W.write<uint32_t>(TargetObjectWriter->getCPUSubtype()); 185 186 W.write<uint32_t>(Type); 187 W.write<uint32_t>(NumLoadCommands); 188 W.write<uint32_t>(LoadCommandsSize); 189 W.write<uint32_t>(Flags); 190 if (is64Bit()) 191 W.write<uint32_t>(0); // reserved 192 193 assert(W.OS.tell() - Start == (is64Bit() ? sizeof(MachO::mach_header_64) 194 : sizeof(MachO::mach_header))); 195 } 196 197 void MachObjectWriter::writeWithPadding(StringRef Str, uint64_t Size) { 198 assert(Size >= Str.size()); 199 W.OS << Str; 200 W.OS.write_zeros(Size - Str.size()); 201 } 202 203 /// writeSegmentLoadCommand - Write a segment load command. 204 /// 205 /// \param NumSections The number of sections in this segment. 206 /// \param SectionDataSize The total size of the sections. 207 void MachObjectWriter::writeSegmentLoadCommand( 208 StringRef Name, unsigned NumSections, uint64_t VMAddr, uint64_t VMSize, 209 uint64_t SectionDataStartOffset, uint64_t SectionDataSize, uint32_t MaxProt, 210 uint32_t InitProt) { 211 // struct segment_command (56 bytes) or 212 // struct segment_command_64 (72 bytes) 213 214 uint64_t Start = W.OS.tell(); 215 (void) Start; 216 217 unsigned SegmentLoadCommandSize = 218 is64Bit() ? sizeof(MachO::segment_command_64): 219 sizeof(MachO::segment_command); 220 W.write<uint32_t>(is64Bit() ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT); 221 W.write<uint32_t>(SegmentLoadCommandSize + 222 NumSections * (is64Bit() ? sizeof(MachO::section_64) : 223 sizeof(MachO::section))); 224 225 writeWithPadding(Name, 16); 226 if (is64Bit()) { 227 W.write<uint64_t>(VMAddr); // vmaddr 228 W.write<uint64_t>(VMSize); // vmsize 229 W.write<uint64_t>(SectionDataStartOffset); // file offset 230 W.write<uint64_t>(SectionDataSize); // file size 231 } else { 232 W.write<uint32_t>(VMAddr); // vmaddr 233 W.write<uint32_t>(VMSize); // vmsize 234 W.write<uint32_t>(SectionDataStartOffset); // file offset 235 W.write<uint32_t>(SectionDataSize); // file size 236 } 237 // maxprot 238 W.write<uint32_t>(MaxProt); 239 // initprot 240 W.write<uint32_t>(InitProt); 241 W.write<uint32_t>(NumSections); 242 W.write<uint32_t>(0); // flags 243 244 assert(W.OS.tell() - Start == SegmentLoadCommandSize); 245 } 246 247 void MachObjectWriter::writeSection(const MCAsmLayout &Layout, 248 const MCSection &Sec, uint64_t VMAddr, 249 uint64_t FileOffset, unsigned Flags, 250 uint64_t RelocationsStart, 251 unsigned NumRelocations) { 252 uint64_t SectionSize = Layout.getSectionAddressSize(&Sec); 253 const MCSectionMachO &Section = cast<MCSectionMachO>(Sec); 254 255 // The offset is unused for virtual sections. 256 if (Section.isVirtualSection()) { 257 assert(Layout.getSectionFileSize(&Sec) == 0 && "Invalid file size!"); 258 FileOffset = 0; 259 } 260 261 // struct section (68 bytes) or 262 // struct section_64 (80 bytes) 263 264 uint64_t Start = W.OS.tell(); 265 (void) Start; 266 267 writeWithPadding(Section.getName(), 16); 268 writeWithPadding(Section.getSegmentName(), 16); 269 if (is64Bit()) { 270 W.write<uint64_t>(VMAddr); // address 271 W.write<uint64_t>(SectionSize); // size 272 } else { 273 W.write<uint32_t>(VMAddr); // address 274 W.write<uint32_t>(SectionSize); // size 275 } 276 W.write<uint32_t>(FileOffset); 277 278 W.write<uint32_t>(Log2(Section.getAlign())); 279 W.write<uint32_t>(NumRelocations ? RelocationsStart : 0); 280 W.write<uint32_t>(NumRelocations); 281 W.write<uint32_t>(Flags); 282 W.write<uint32_t>(IndirectSymBase.lookup(&Sec)); // reserved1 283 W.write<uint32_t>(Section.getStubSize()); // reserved2 284 if (is64Bit()) 285 W.write<uint32_t>(0); // reserved3 286 287 assert(W.OS.tell() - Start == 288 (is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section))); 289 } 290 291 void MachObjectWriter::writeSymtabLoadCommand(uint32_t SymbolOffset, 292 uint32_t NumSymbols, 293 uint32_t StringTableOffset, 294 uint32_t StringTableSize) { 295 // struct symtab_command (24 bytes) 296 297 uint64_t Start = W.OS.tell(); 298 (void) Start; 299 300 W.write<uint32_t>(MachO::LC_SYMTAB); 301 W.write<uint32_t>(sizeof(MachO::symtab_command)); 302 W.write<uint32_t>(SymbolOffset); 303 W.write<uint32_t>(NumSymbols); 304 W.write<uint32_t>(StringTableOffset); 305 W.write<uint32_t>(StringTableSize); 306 307 assert(W.OS.tell() - Start == sizeof(MachO::symtab_command)); 308 } 309 310 void MachObjectWriter::writeDysymtabLoadCommand(uint32_t FirstLocalSymbol, 311 uint32_t NumLocalSymbols, 312 uint32_t FirstExternalSymbol, 313 uint32_t NumExternalSymbols, 314 uint32_t FirstUndefinedSymbol, 315 uint32_t NumUndefinedSymbols, 316 uint32_t IndirectSymbolOffset, 317 uint32_t NumIndirectSymbols) { 318 // struct dysymtab_command (80 bytes) 319 320 uint64_t Start = W.OS.tell(); 321 (void) Start; 322 323 W.write<uint32_t>(MachO::LC_DYSYMTAB); 324 W.write<uint32_t>(sizeof(MachO::dysymtab_command)); 325 W.write<uint32_t>(FirstLocalSymbol); 326 W.write<uint32_t>(NumLocalSymbols); 327 W.write<uint32_t>(FirstExternalSymbol); 328 W.write<uint32_t>(NumExternalSymbols); 329 W.write<uint32_t>(FirstUndefinedSymbol); 330 W.write<uint32_t>(NumUndefinedSymbols); 331 W.write<uint32_t>(0); // tocoff 332 W.write<uint32_t>(0); // ntoc 333 W.write<uint32_t>(0); // modtaboff 334 W.write<uint32_t>(0); // nmodtab 335 W.write<uint32_t>(0); // extrefsymoff 336 W.write<uint32_t>(0); // nextrefsyms 337 W.write<uint32_t>(IndirectSymbolOffset); 338 W.write<uint32_t>(NumIndirectSymbols); 339 W.write<uint32_t>(0); // extreloff 340 W.write<uint32_t>(0); // nextrel 341 W.write<uint32_t>(0); // locreloff 342 W.write<uint32_t>(0); // nlocrel 343 344 assert(W.OS.tell() - Start == sizeof(MachO::dysymtab_command)); 345 } 346 347 MachObjectWriter::MachSymbolData * 348 MachObjectWriter::findSymbolData(const MCSymbol &Sym) { 349 for (auto *SymbolData : 350 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData}) 351 for (MachSymbolData &Entry : *SymbolData) 352 if (Entry.Symbol == &Sym) 353 return &Entry; 354 355 return nullptr; 356 } 357 358 const MCSymbol &MachObjectWriter::findAliasedSymbol(const MCSymbol &Sym) const { 359 const MCSymbol *S = &Sym; 360 while (S->isVariable()) { 361 const MCExpr *Value = S->getVariableValue(); 362 const auto *Ref = dyn_cast<MCSymbolRefExpr>(Value); 363 if (!Ref) 364 return *S; 365 S = &Ref->getSymbol(); 366 } 367 return *S; 368 } 369 370 void MachObjectWriter::writeNlist(MachSymbolData &MSD, 371 const MCAsmLayout &Layout) { 372 const MCSymbol *Symbol = MSD.Symbol; 373 const MCSymbol &Data = *Symbol; 374 const MCSymbol *AliasedSymbol = &findAliasedSymbol(*Symbol); 375 uint8_t SectionIndex = MSD.SectionIndex; 376 uint8_t Type = 0; 377 uint64_t Address = 0; 378 bool IsAlias = Symbol != AliasedSymbol; 379 380 const MCSymbol &OrigSymbol = *Symbol; 381 MachSymbolData *AliaseeInfo; 382 if (IsAlias) { 383 AliaseeInfo = findSymbolData(*AliasedSymbol); 384 if (AliaseeInfo) 385 SectionIndex = AliaseeInfo->SectionIndex; 386 Symbol = AliasedSymbol; 387 // FIXME: Should this update Data as well? 388 } 389 390 // Set the N_TYPE bits. See <mach-o/nlist.h>. 391 // 392 // FIXME: Are the prebound or indirect fields possible here? 393 if (IsAlias && Symbol->isUndefined()) 394 Type = MachO::N_INDR; 395 else if (Symbol->isUndefined()) 396 Type = MachO::N_UNDF; 397 else if (Symbol->isAbsolute()) 398 Type = MachO::N_ABS; 399 else 400 Type = MachO::N_SECT; 401 402 // FIXME: Set STAB bits. 403 404 if (Data.isPrivateExtern()) 405 Type |= MachO::N_PEXT; 406 407 // Set external bit. 408 if (Data.isExternal() || (!IsAlias && Symbol->isUndefined())) 409 Type |= MachO::N_EXT; 410 411 // Compute the symbol address. 412 if (IsAlias && Symbol->isUndefined()) 413 Address = AliaseeInfo->StringIndex; 414 else if (Symbol->isDefined()) 415 Address = getSymbolAddress(OrigSymbol, Layout); 416 else if (Symbol->isCommon()) { 417 // Common symbols are encoded with the size in the address 418 // field, and their alignment in the flags. 419 Address = Symbol->getCommonSize(); 420 } 421 422 // struct nlist (12 bytes) 423 424 W.write<uint32_t>(MSD.StringIndex); 425 W.OS << char(Type); 426 W.OS << char(SectionIndex); 427 428 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc' 429 // value. 430 bool EncodeAsAltEntry = 431 IsAlias && cast<MCSymbolMachO>(OrigSymbol).isAltEntry(); 432 W.write<uint16_t>(cast<MCSymbolMachO>(Symbol)->getEncodedFlags(EncodeAsAltEntry)); 433 if (is64Bit()) 434 W.write<uint64_t>(Address); 435 else 436 W.write<uint32_t>(Address); 437 } 438 439 void MachObjectWriter::writeLinkeditLoadCommand(uint32_t Type, 440 uint32_t DataOffset, 441 uint32_t DataSize) { 442 uint64_t Start = W.OS.tell(); 443 (void) Start; 444 445 W.write<uint32_t>(Type); 446 W.write<uint32_t>(sizeof(MachO::linkedit_data_command)); 447 W.write<uint32_t>(DataOffset); 448 W.write<uint32_t>(DataSize); 449 450 assert(W.OS.tell() - Start == sizeof(MachO::linkedit_data_command)); 451 } 452 453 static unsigned ComputeLinkerOptionsLoadCommandSize( 454 const std::vector<std::string> &Options, bool is64Bit) 455 { 456 unsigned Size = sizeof(MachO::linker_option_command); 457 for (const std::string &Option : Options) 458 Size += Option.size() + 1; 459 return alignTo(Size, is64Bit ? 8 : 4); 460 } 461 462 void MachObjectWriter::writeLinkerOptionsLoadCommand( 463 const std::vector<std::string> &Options) 464 { 465 unsigned Size = ComputeLinkerOptionsLoadCommandSize(Options, is64Bit()); 466 uint64_t Start = W.OS.tell(); 467 (void) Start; 468 469 W.write<uint32_t>(MachO::LC_LINKER_OPTION); 470 W.write<uint32_t>(Size); 471 W.write<uint32_t>(Options.size()); 472 uint64_t BytesWritten = sizeof(MachO::linker_option_command); 473 for (const std::string &Option : Options) { 474 // Write each string, including the null byte. 475 W.OS << Option << '\0'; 476 BytesWritten += Option.size() + 1; 477 } 478 479 // Pad to a multiple of the pointer size. 480 W.OS.write_zeros( 481 offsetToAlignment(BytesWritten, is64Bit() ? Align(8) : Align(4))); 482 483 assert(W.OS.tell() - Start == Size); 484 } 485 486 static bool isFixupTargetValid(const MCValue &Target) { 487 // Target is (LHS - RHS + cst). 488 // We don't support the form where LHS is null: -RHS + cst 489 if (!Target.getSymA() && Target.getSymB()) 490 return false; 491 return true; 492 } 493 494 void MachObjectWriter::recordRelocation(MCAssembler &Asm, 495 const MCFragment *Fragment, 496 const MCFixup &Fixup, MCValue Target, 497 uint64_t &FixedValue) { 498 if (!isFixupTargetValid(Target)) { 499 Asm.getContext().reportError(Fixup.getLoc(), 500 "unsupported relocation expression"); 501 return; 502 } 503 504 TargetObjectWriter->recordRelocation(this, Asm, Fragment, Fixup, Target, 505 FixedValue); 506 } 507 508 void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) { 509 // This is the point where 'as' creates actual symbols for indirect symbols 510 // (in the following two passes). It would be easier for us to do this sooner 511 // when we see the attribute, but that makes getting the order in the symbol 512 // table much more complicated than it is worth. 513 // 514 // FIXME: Revisit this when the dust settles. 515 516 // Report errors for use of .indirect_symbol not in a symbol pointer section 517 // or stub section. 518 for (IndirectSymbolData &ISD : llvm::make_range(Asm.indirect_symbol_begin(), 519 Asm.indirect_symbol_end())) { 520 const MCSectionMachO &Section = cast<MCSectionMachO>(*ISD.Section); 521 522 if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS && 523 Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS && 524 Section.getType() != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS && 525 Section.getType() != MachO::S_SYMBOL_STUBS) { 526 MCSymbol &Symbol = *ISD.Symbol; 527 report_fatal_error("indirect symbol '" + Symbol.getName() + 528 "' not in a symbol pointer or stub section"); 529 } 530 } 531 532 // Bind non-lazy symbol pointers first. 533 unsigned IndirectIndex = 0; 534 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), 535 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) { 536 const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section); 537 538 if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS && 539 Section.getType() != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) 540 continue; 541 542 // Initialize the section indirect symbol base, if necessary. 543 IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex)); 544 545 Asm.registerSymbol(*it->Symbol); 546 } 547 548 // Then lazy symbol pointers and symbol stubs. 549 IndirectIndex = 0; 550 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), 551 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) { 552 const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section); 553 554 if (Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS && 555 Section.getType() != MachO::S_SYMBOL_STUBS) 556 continue; 557 558 // Initialize the section indirect symbol base, if necessary. 559 IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex)); 560 561 // Set the symbol type to undefined lazy, but only on construction. 562 // 563 // FIXME: Do not hardcode. 564 if (Asm.registerSymbol(*it->Symbol)) 565 cast<MCSymbolMachO>(it->Symbol)->setReferenceTypeUndefinedLazy(true); 566 } 567 } 568 569 /// computeSymbolTable - Compute the symbol table data 570 void MachObjectWriter::computeSymbolTable( 571 MCAssembler &Asm, std::vector<MachSymbolData> &LocalSymbolData, 572 std::vector<MachSymbolData> &ExternalSymbolData, 573 std::vector<MachSymbolData> &UndefinedSymbolData) { 574 // Build section lookup table. 575 DenseMap<const MCSection*, uint8_t> SectionIndexMap; 576 unsigned Index = 1; 577 for (MCAssembler::iterator it = Asm.begin(), 578 ie = Asm.end(); it != ie; ++it, ++Index) 579 SectionIndexMap[&*it] = Index; 580 assert(Index <= 256 && "Too many sections!"); 581 582 // Build the string table. 583 for (const MCSymbol &Symbol : Asm.symbols()) { 584 if (!Asm.isSymbolLinkerVisible(Symbol)) 585 continue; 586 587 StringTable.add(Symbol.getName()); 588 } 589 StringTable.finalize(); 590 591 // Build the symbol arrays but only for non-local symbols. 592 // 593 // The particular order that we collect and then sort the symbols is chosen to 594 // match 'as'. Even though it doesn't matter for correctness, this is 595 // important for letting us diff .o files. 596 for (const MCSymbol &Symbol : Asm.symbols()) { 597 // Ignore non-linker visible symbols. 598 if (!Asm.isSymbolLinkerVisible(Symbol)) 599 continue; 600 601 if (!Symbol.isExternal() && !Symbol.isUndefined()) 602 continue; 603 604 MachSymbolData MSD; 605 MSD.Symbol = &Symbol; 606 MSD.StringIndex = StringTable.getOffset(Symbol.getName()); 607 608 if (Symbol.isUndefined()) { 609 MSD.SectionIndex = 0; 610 UndefinedSymbolData.push_back(MSD); 611 } else if (Symbol.isAbsolute()) { 612 MSD.SectionIndex = 0; 613 ExternalSymbolData.push_back(MSD); 614 } else { 615 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); 616 assert(MSD.SectionIndex && "Invalid section index!"); 617 ExternalSymbolData.push_back(MSD); 618 } 619 } 620 621 // Now add the data for local symbols. 622 for (const MCSymbol &Symbol : Asm.symbols()) { 623 // Ignore non-linker visible symbols. 624 if (!Asm.isSymbolLinkerVisible(Symbol)) 625 continue; 626 627 if (Symbol.isExternal() || Symbol.isUndefined()) 628 continue; 629 630 MachSymbolData MSD; 631 MSD.Symbol = &Symbol; 632 MSD.StringIndex = StringTable.getOffset(Symbol.getName()); 633 634 if (Symbol.isAbsolute()) { 635 MSD.SectionIndex = 0; 636 LocalSymbolData.push_back(MSD); 637 } else { 638 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); 639 assert(MSD.SectionIndex && "Invalid section index!"); 640 LocalSymbolData.push_back(MSD); 641 } 642 } 643 644 // External and undefined symbols are required to be in lexicographic order. 645 llvm::sort(ExternalSymbolData); 646 llvm::sort(UndefinedSymbolData); 647 648 // Set the symbol indices. 649 Index = 0; 650 for (auto *SymbolData : 651 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData}) 652 for (MachSymbolData &Entry : *SymbolData) 653 Entry.Symbol->setIndex(Index++); 654 655 for (const MCSection &Section : Asm) { 656 for (RelAndSymbol &Rel : Relocations[&Section]) { 657 if (!Rel.Sym) 658 continue; 659 660 // Set the Index and the IsExtern bit. 661 unsigned Index = Rel.Sym->getIndex(); 662 assert(isInt<24>(Index)); 663 if (W.Endian == llvm::endianness::little) 664 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & (~0U << 24)) | Index | (1 << 27); 665 else 666 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & 0xff) | Index << 8 | (1 << 4); 667 } 668 } 669 } 670 671 void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm) { 672 uint64_t StartAddress = 0; 673 for (const MCSection *Sec : Asm.getLayout()->getSectionOrder()) { 674 StartAddress = alignTo(StartAddress, Sec->getAlign()); 675 SectionAddress[Sec] = StartAddress; 676 StartAddress += Asm.getSectionAddressSize(*Sec); 677 678 // Explicitly pad the section to match the alignment requirements of the 679 // following one. This is for 'gas' compatibility, it shouldn't 680 /// strictly be necessary. 681 StartAddress += getPaddingSize(Sec, *Asm.getLayout()); 682 } 683 } 684 685 void MachObjectWriter::executePostLayoutBinding(MCAssembler &Asm) { 686 computeSectionAddresses(Asm); 687 688 // Create symbol data for any indirect symbols. 689 bindIndirectSymbols(Asm); 690 } 691 692 bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( 693 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB, 694 bool InSet, bool IsPCRel) const { 695 if (InSet) 696 return true; 697 698 // The effective address is 699 // addr(atom(A)) + offset(A) 700 // - addr(atom(B)) - offset(B) 701 // and the offsets are not relocatable, so the fixup is fully resolved when 702 // addr(atom(A)) - addr(atom(B)) == 0. 703 const MCSymbol &SA = findAliasedSymbol(SymA); 704 const MCSection &SecA = SA.getSection(); 705 const MCSection &SecB = *FB.getParent(); 706 707 if (IsPCRel) { 708 // The simple (Darwin, except on x86_64) way of dealing with this was to 709 // assume that any reference to a temporary symbol *must* be a temporary 710 // symbol in the same atom, unless the sections differ. Therefore, any PCrel 711 // relocation to a temporary symbol (in the same section) is fully 712 // resolved. This also works in conjunction with absolutized .set, which 713 // requires the compiler to use .set to absolutize the differences between 714 // symbols which the compiler knows to be assembly time constants, so we 715 // don't need to worry about considering symbol differences fully resolved. 716 // 717 // If the file isn't using sub-sections-via-symbols, we can make the 718 // same assumptions about any symbol that we normally make about 719 // assembler locals. 720 721 bool hasReliableSymbolDifference = isX86_64(); 722 if (!hasReliableSymbolDifference) { 723 if (!SA.isInSection() || &SecA != &SecB || 724 (!SA.isTemporary() && FB.getAtom() != SA.getFragment()->getAtom() && 725 Asm.getSubsectionsViaSymbols())) 726 return false; 727 return true; 728 } 729 } 730 731 // If they are not in the same section, we can't compute the diff. 732 if (&SecA != &SecB) 733 return false; 734 735 // If the atoms are the same, they are guaranteed to have the same address. 736 return SA.getFragment()->getAtom() == FB.getAtom(); 737 } 738 739 static MachO::LoadCommandType getLCFromMCVM(MCVersionMinType Type) { 740 switch (Type) { 741 case MCVM_OSXVersionMin: return MachO::LC_VERSION_MIN_MACOSX; 742 case MCVM_IOSVersionMin: return MachO::LC_VERSION_MIN_IPHONEOS; 743 case MCVM_TvOSVersionMin: return MachO::LC_VERSION_MIN_TVOS; 744 case MCVM_WatchOSVersionMin: return MachO::LC_VERSION_MIN_WATCHOS; 745 } 746 llvm_unreachable("Invalid mc version min type"); 747 } 748 749 void MachObjectWriter::populateAddrSigSection(MCAssembler &Asm) { 750 MCSection *AddrSigSection = 751 Asm.getContext().getObjectFileInfo()->getAddrSigSection(); 752 unsigned Log2Size = is64Bit() ? 3 : 2; 753 for (const MCSymbol *S : getAddrsigSyms()) { 754 if (!S->isRegistered()) 755 continue; 756 MachO::any_relocation_info MRE; 757 MRE.r_word0 = 0; 758 MRE.r_word1 = (Log2Size << 25) | (MachO::GENERIC_RELOC_VANILLA << 28); 759 addRelocation(S, AddrSigSection, MRE); 760 } 761 } 762 763 uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) { 764 auto &Layout = *Asm.getLayout(); 765 uint64_t StartOffset = W.OS.tell(); 766 767 populateAddrSigSection(Asm); 768 769 // Compute symbol table information and bind symbol indices. 770 computeSymbolTable(Asm, LocalSymbolData, ExternalSymbolData, 771 UndefinedSymbolData); 772 773 if (!Asm.CGProfile.empty()) { 774 MCSection *CGProfileSection = Asm.getContext().getMachOSection( 775 "__LLVM", "__cg_profile", 0, SectionKind::getMetadata()); 776 auto &Frag = cast<MCDataFragment>(*CGProfileSection->begin()); 777 Frag.getContents().clear(); 778 raw_svector_ostream OS(Frag.getContents()); 779 for (const MCAssembler::CGProfileEntry &CGPE : Asm.CGProfile) { 780 uint32_t FromIndex = CGPE.From->getSymbol().getIndex(); 781 uint32_t ToIndex = CGPE.To->getSymbol().getIndex(); 782 support::endian::write(OS, FromIndex, W.Endian); 783 support::endian::write(OS, ToIndex, W.Endian); 784 support::endian::write(OS, CGPE.Count, W.Endian); 785 } 786 } 787 788 unsigned NumSections = Asm.size(); 789 const MCAssembler::VersionInfoType &VersionInfo = Asm.getVersionInfo(); 790 791 // The section data starts after the header, the segment load command (and 792 // section headers) and the symbol table. 793 unsigned NumLoadCommands = 1; 794 uint64_t LoadCommandsSize = is64Bit() ? 795 sizeof(MachO::segment_command_64) + NumSections * sizeof(MachO::section_64): 796 sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section); 797 798 // Add the deployment target version info load command size, if used. 799 if (VersionInfo.Major != 0) { 800 ++NumLoadCommands; 801 if (VersionInfo.EmitBuildVersion) 802 LoadCommandsSize += sizeof(MachO::build_version_command); 803 else 804 LoadCommandsSize += sizeof(MachO::version_min_command); 805 } 806 807 const MCAssembler::VersionInfoType &TargetVariantVersionInfo = 808 Asm.getDarwinTargetVariantVersionInfo(); 809 810 // Add the target variant version info load command size, if used. 811 if (TargetVariantVersionInfo.Major != 0) { 812 ++NumLoadCommands; 813 assert(TargetVariantVersionInfo.EmitBuildVersion && 814 "target variant should use build version"); 815 LoadCommandsSize += sizeof(MachO::build_version_command); 816 } 817 818 // Add the data-in-code load command size, if used. 819 unsigned NumDataRegions = Asm.getDataRegions().size(); 820 if (NumDataRegions) { 821 ++NumLoadCommands; 822 LoadCommandsSize += sizeof(MachO::linkedit_data_command); 823 } 824 825 // Add the loh load command size, if used. 826 uint64_t LOHRawSize = Asm.getLOHContainer().getEmitSize(*this, Layout); 827 uint64_t LOHSize = alignTo(LOHRawSize, is64Bit() ? 8 : 4); 828 if (LOHSize) { 829 ++NumLoadCommands; 830 LoadCommandsSize += sizeof(MachO::linkedit_data_command); 831 } 832 833 // Add the symbol table load command sizes, if used. 834 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() + 835 UndefinedSymbolData.size(); 836 if (NumSymbols) { 837 NumLoadCommands += 2; 838 LoadCommandsSize += (sizeof(MachO::symtab_command) + 839 sizeof(MachO::dysymtab_command)); 840 } 841 842 // Add the linker option load commands sizes. 843 for (const auto &Option : Asm.getLinkerOptions()) { 844 ++NumLoadCommands; 845 LoadCommandsSize += ComputeLinkerOptionsLoadCommandSize(Option, is64Bit()); 846 } 847 848 // Compute the total size of the section data, as well as its file size and vm 849 // size. 850 uint64_t SectionDataStart = (is64Bit() ? sizeof(MachO::mach_header_64) : 851 sizeof(MachO::mach_header)) + LoadCommandsSize; 852 uint64_t SectionDataSize = 0; 853 uint64_t SectionDataFileSize = 0; 854 uint64_t VMSize = 0; 855 for (const MCSection &Sec : Asm) { 856 uint64_t Address = getSectionAddress(&Sec); 857 uint64_t Size = Asm.getSectionAddressSize(Sec); 858 uint64_t FileSize = Asm.getSectionFileSize(Sec); 859 FileSize += getPaddingSize(&Sec, Layout); 860 861 VMSize = std::max(VMSize, Address + Size); 862 863 if (Sec.isVirtualSection()) 864 continue; 865 866 SectionDataSize = std::max(SectionDataSize, Address + Size); 867 SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize); 868 } 869 870 // The section data is padded to pointer size bytes. 871 // 872 // FIXME: Is this machine dependent? 873 unsigned SectionDataPadding = 874 offsetToAlignment(SectionDataFileSize, is64Bit() ? Align(8) : Align(4)); 875 SectionDataFileSize += SectionDataPadding; 876 877 // Write the prolog, starting with the header and load command... 878 writeHeader(MachO::MH_OBJECT, NumLoadCommands, LoadCommandsSize, 879 Asm.getSubsectionsViaSymbols()); 880 uint32_t Prot = 881 MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE; 882 writeSegmentLoadCommand("", NumSections, 0, VMSize, SectionDataStart, 883 SectionDataSize, Prot, Prot); 884 885 // ... and then the section headers. 886 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize; 887 for (const MCSection &Section : Asm) { 888 const auto &Sec = cast<MCSectionMachO>(Section); 889 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec]; 890 unsigned NumRelocs = Relocs.size(); 891 uint64_t SectionStart = SectionDataStart + getSectionAddress(&Sec); 892 unsigned Flags = Sec.getTypeAndAttributes(); 893 if (Sec.hasInstructions()) 894 Flags |= MachO::S_ATTR_SOME_INSTRUCTIONS; 895 writeSection(Layout, Sec, getSectionAddress(&Sec), SectionStart, Flags, 896 RelocTableEnd, NumRelocs); 897 RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info); 898 } 899 900 // Write out the deployment target information, if it's available. 901 auto EmitDeploymentTargetVersion = 902 [&](const MCAssembler::VersionInfoType &VersionInfo) { 903 auto EncodeVersion = [](VersionTuple V) -> uint32_t { 904 assert(!V.empty() && "empty version"); 905 unsigned Update = V.getSubminor().value_or(0); 906 unsigned Minor = V.getMinor().value_or(0); 907 assert(Update < 256 && "unencodable update target version"); 908 assert(Minor < 256 && "unencodable minor target version"); 909 assert(V.getMajor() < 65536 && "unencodable major target version"); 910 return Update | (Minor << 8) | (V.getMajor() << 16); 911 }; 912 uint32_t EncodedVersion = EncodeVersion(VersionTuple( 913 VersionInfo.Major, VersionInfo.Minor, VersionInfo.Update)); 914 uint32_t SDKVersion = !VersionInfo.SDKVersion.empty() 915 ? EncodeVersion(VersionInfo.SDKVersion) 916 : 0; 917 if (VersionInfo.EmitBuildVersion) { 918 // FIXME: Currently empty tools. Add clang version in the future. 919 W.write<uint32_t>(MachO::LC_BUILD_VERSION); 920 W.write<uint32_t>(sizeof(MachO::build_version_command)); 921 W.write<uint32_t>(VersionInfo.TypeOrPlatform.Platform); 922 W.write<uint32_t>(EncodedVersion); 923 W.write<uint32_t>(SDKVersion); 924 W.write<uint32_t>(0); // Empty tools list. 925 } else { 926 MachO::LoadCommandType LCType = 927 getLCFromMCVM(VersionInfo.TypeOrPlatform.Type); 928 W.write<uint32_t>(LCType); 929 W.write<uint32_t>(sizeof(MachO::version_min_command)); 930 W.write<uint32_t>(EncodedVersion); 931 W.write<uint32_t>(SDKVersion); 932 } 933 }; 934 if (VersionInfo.Major != 0) 935 EmitDeploymentTargetVersion(VersionInfo); 936 if (TargetVariantVersionInfo.Major != 0) 937 EmitDeploymentTargetVersion(TargetVariantVersionInfo); 938 939 // Write the data-in-code load command, if used. 940 uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8; 941 if (NumDataRegions) { 942 uint64_t DataRegionsOffset = RelocTableEnd; 943 uint64_t DataRegionsSize = NumDataRegions * 8; 944 writeLinkeditLoadCommand(MachO::LC_DATA_IN_CODE, DataRegionsOffset, 945 DataRegionsSize); 946 } 947 948 // Write the loh load command, if used. 949 uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize; 950 if (LOHSize) 951 writeLinkeditLoadCommand(MachO::LC_LINKER_OPTIMIZATION_HINT, 952 DataInCodeTableEnd, LOHSize); 953 954 // Write the symbol table load command, if used. 955 if (NumSymbols) { 956 unsigned FirstLocalSymbol = 0; 957 unsigned NumLocalSymbols = LocalSymbolData.size(); 958 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols; 959 unsigned NumExternalSymbols = ExternalSymbolData.size(); 960 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols; 961 unsigned NumUndefinedSymbols = UndefinedSymbolData.size(); 962 unsigned NumIndirectSymbols = Asm.indirect_symbol_size(); 963 unsigned NumSymTabSymbols = 964 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols; 965 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4; 966 uint64_t IndirectSymbolOffset = 0; 967 968 // If used, the indirect symbols are written after the section data. 969 if (NumIndirectSymbols) 970 IndirectSymbolOffset = LOHTableEnd; 971 972 // The symbol table is written after the indirect symbol data. 973 uint64_t SymbolTableOffset = LOHTableEnd + IndirectSymbolSize; 974 975 // The string table is written after symbol table. 976 uint64_t StringTableOffset = 977 SymbolTableOffset + NumSymTabSymbols * (is64Bit() ? 978 sizeof(MachO::nlist_64) : 979 sizeof(MachO::nlist)); 980 writeSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols, 981 StringTableOffset, StringTable.getSize()); 982 983 writeDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols, 984 FirstExternalSymbol, NumExternalSymbols, 985 FirstUndefinedSymbol, NumUndefinedSymbols, 986 IndirectSymbolOffset, NumIndirectSymbols); 987 } 988 989 // Write the linker options load commands. 990 for (const auto &Option : Asm.getLinkerOptions()) 991 writeLinkerOptionsLoadCommand(Option); 992 993 // Write the actual section data. 994 for (const MCSection &Sec : Asm) { 995 Asm.writeSectionData(W.OS, &Sec); 996 997 uint64_t Pad = getPaddingSize(&Sec, Layout); 998 W.OS.write_zeros(Pad); 999 } 1000 1001 // Write the extra padding. 1002 W.OS.write_zeros(SectionDataPadding); 1003 1004 // Write the relocation entries. 1005 for (const MCSection &Sec : Asm) { 1006 // Write the section relocation entries, in reverse order to match 'as' 1007 // (approximately, the exact algorithm is more complicated than this). 1008 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec]; 1009 for (const RelAndSymbol &Rel : llvm::reverse(Relocs)) { 1010 W.write<uint32_t>(Rel.MRE.r_word0); 1011 W.write<uint32_t>(Rel.MRE.r_word1); 1012 } 1013 } 1014 1015 // Write out the data-in-code region payload, if there is one. 1016 for (MCAssembler::const_data_region_iterator 1017 it = Asm.data_region_begin(), ie = Asm.data_region_end(); 1018 it != ie; ++it) { 1019 const DataRegionData *Data = &(*it); 1020 uint64_t Start = getSymbolAddress(*Data->Start, Layout); 1021 uint64_t End; 1022 if (Data->End) 1023 End = getSymbolAddress(*Data->End, Layout); 1024 else 1025 report_fatal_error("Data region not terminated"); 1026 1027 LLVM_DEBUG(dbgs() << "data in code region-- kind: " << Data->Kind 1028 << " start: " << Start << "(" << Data->Start->getName() 1029 << ")" 1030 << " end: " << End << "(" << Data->End->getName() << ")" 1031 << " size: " << End - Start << "\n"); 1032 W.write<uint32_t>(Start); 1033 W.write<uint16_t>(End - Start); 1034 W.write<uint16_t>(Data->Kind); 1035 } 1036 1037 // Write out the loh commands, if there is one. 1038 if (LOHSize) { 1039 #ifndef NDEBUG 1040 unsigned Start = W.OS.tell(); 1041 #endif 1042 Asm.getLOHContainer().emit(*this, Layout); 1043 // Pad to a multiple of the pointer size. 1044 W.OS.write_zeros( 1045 offsetToAlignment(LOHRawSize, is64Bit() ? Align(8) : Align(4))); 1046 assert(W.OS.tell() - Start == LOHSize); 1047 } 1048 1049 // Write the symbol table data, if used. 1050 if (NumSymbols) { 1051 // Write the indirect symbol entries. 1052 for (MCAssembler::const_indirect_symbol_iterator 1053 it = Asm.indirect_symbol_begin(), 1054 ie = Asm.indirect_symbol_end(); it != ie; ++it) { 1055 // Indirect symbols in the non-lazy symbol pointer section have some 1056 // special handling. 1057 const MCSectionMachO &Section = 1058 static_cast<const MCSectionMachO &>(*it->Section); 1059 if (Section.getType() == MachO::S_NON_LAZY_SYMBOL_POINTERS) { 1060 // If this symbol is defined and internal, mark it as such. 1061 if (it->Symbol->isDefined() && !it->Symbol->isExternal()) { 1062 uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL; 1063 if (it->Symbol->isAbsolute()) 1064 Flags |= MachO::INDIRECT_SYMBOL_ABS; 1065 W.write<uint32_t>(Flags); 1066 continue; 1067 } 1068 } 1069 1070 W.write<uint32_t>(it->Symbol->getIndex()); 1071 } 1072 1073 // FIXME: Check that offsets match computed ones. 1074 1075 // Write the symbol table entries. 1076 for (auto *SymbolData : 1077 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData}) 1078 for (MachSymbolData &Entry : *SymbolData) 1079 writeNlist(Entry, Layout); 1080 1081 // Write the string table. 1082 StringTable.write(W.OS); 1083 } 1084 1085 return W.OS.tell() - StartOffset; 1086 } 1087 1088 std::unique_ptr<MCObjectWriter> 1089 llvm::createMachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW, 1090 raw_pwrite_stream &OS, bool IsLittleEndian) { 1091 return std::make_unique<MachObjectWriter>(std::move(MOTW), OS, 1092 IsLittleEndian); 1093 } 1094