1 //===---- MachO_arm64.cpp - JIT linker implementation for MachO/arm64 -----===// 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 // MachO/arm64 jit-link implementation. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ExecutionEngine/JITLink/MachO_arm64.h" 14 15 #include "MachOLinkGraphBuilder.h" 16 #include "PerGraphGOTAndPLTStubsBuilder.h" 17 18 #define DEBUG_TYPE "jitlink" 19 20 using namespace llvm; 21 using namespace llvm::jitlink; 22 using namespace llvm::jitlink::MachO_arm64_Edges; 23 24 namespace { 25 26 class MachOLinkGraphBuilder_arm64 : public MachOLinkGraphBuilder { 27 public: 28 MachOLinkGraphBuilder_arm64(const object::MachOObjectFile &Obj) 29 : MachOLinkGraphBuilder(Obj, Triple("arm64-apple-darwin"), 30 getMachOARM64RelocationKindName), 31 NumSymbols(Obj.getSymtabLoadCommand().nsyms) {} 32 33 private: 34 static Expected<MachOARM64RelocationKind> 35 getRelocationKind(const MachO::relocation_info &RI) { 36 switch (RI.r_type) { 37 case MachO::ARM64_RELOC_UNSIGNED: 38 if (!RI.r_pcrel) { 39 if (RI.r_length == 3) 40 return RI.r_extern ? Pointer64 : Pointer64Anon; 41 else if (RI.r_length == 2) 42 return Pointer32; 43 } 44 break; 45 case MachO::ARM64_RELOC_SUBTRACTOR: 46 // SUBTRACTOR must be non-pc-rel, extern, with length 2 or 3. 47 // Initially represent SUBTRACTOR relocations with 'Delta<W>'. 48 // They may be turned into NegDelta<W> by parsePairRelocation. 49 if (!RI.r_pcrel && RI.r_extern) { 50 if (RI.r_length == 2) 51 return Delta32; 52 else if (RI.r_length == 3) 53 return Delta64; 54 } 55 break; 56 case MachO::ARM64_RELOC_BRANCH26: 57 if (RI.r_pcrel && RI.r_extern && RI.r_length == 2) 58 return Branch26; 59 break; 60 case MachO::ARM64_RELOC_PAGE21: 61 if (RI.r_pcrel && RI.r_extern && RI.r_length == 2) 62 return Page21; 63 break; 64 case MachO::ARM64_RELOC_PAGEOFF12: 65 if (!RI.r_pcrel && RI.r_extern && RI.r_length == 2) 66 return PageOffset12; 67 break; 68 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: 69 if (RI.r_pcrel && RI.r_extern && RI.r_length == 2) 70 return GOTPage21; 71 break; 72 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: 73 if (!RI.r_pcrel && RI.r_extern && RI.r_length == 2) 74 return GOTPageOffset12; 75 break; 76 case MachO::ARM64_RELOC_POINTER_TO_GOT: 77 if (RI.r_pcrel && RI.r_extern && RI.r_length == 2) 78 return PointerToGOT; 79 break; 80 case MachO::ARM64_RELOC_ADDEND: 81 if (!RI.r_pcrel && !RI.r_extern && RI.r_length == 2) 82 return PairedAddend; 83 break; 84 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21: 85 if (RI.r_pcrel && RI.r_extern && RI.r_length == 2) 86 return TLVPage21; 87 break; 88 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12: 89 if (!RI.r_pcrel && RI.r_extern && RI.r_length == 2) 90 return TLVPageOffset12; 91 break; 92 } 93 94 return make_error<JITLinkError>( 95 "Unsupported arm64 relocation: address=" + 96 formatv("{0:x8}", RI.r_address) + 97 ", symbolnum=" + formatv("{0:x6}", RI.r_symbolnum) + 98 ", kind=" + formatv("{0:x1}", RI.r_type) + 99 ", pc_rel=" + (RI.r_pcrel ? "true" : "false") + 100 ", extern=" + (RI.r_extern ? "true" : "false") + 101 ", length=" + formatv("{0:d}", RI.r_length)); 102 } 103 104 using PairRelocInfo = 105 std::tuple<MachOARM64RelocationKind, Symbol *, uint64_t>; 106 107 // Parses paired SUBTRACTOR/UNSIGNED relocations and, on success, 108 // returns the edge kind and addend to be used. 109 Expected<PairRelocInfo> 110 parsePairRelocation(Block &BlockToFix, Edge::Kind SubtractorKind, 111 const MachO::relocation_info &SubRI, 112 JITTargetAddress FixupAddress, const char *FixupContent, 113 object::relocation_iterator &UnsignedRelItr, 114 object::relocation_iterator &RelEnd) { 115 using namespace support; 116 117 assert(((SubtractorKind == Delta32 && SubRI.r_length == 2) || 118 (SubtractorKind == Delta64 && SubRI.r_length == 3)) && 119 "Subtractor kind should match length"); 120 assert(SubRI.r_extern && "SUBTRACTOR reloc symbol should be extern"); 121 assert(!SubRI.r_pcrel && "SUBTRACTOR reloc should not be PCRel"); 122 123 if (UnsignedRelItr == RelEnd) 124 return make_error<JITLinkError>("arm64 SUBTRACTOR without paired " 125 "UNSIGNED relocation"); 126 127 auto UnsignedRI = getRelocationInfo(UnsignedRelItr); 128 129 if (SubRI.r_address != UnsignedRI.r_address) 130 return make_error<JITLinkError>("arm64 SUBTRACTOR and paired UNSIGNED " 131 "point to different addresses"); 132 133 if (SubRI.r_length != UnsignedRI.r_length) 134 return make_error<JITLinkError>("length of arm64 SUBTRACTOR and paired " 135 "UNSIGNED reloc must match"); 136 137 Symbol *FromSymbol; 138 if (auto FromSymbolOrErr = findSymbolByIndex(SubRI.r_symbolnum)) 139 FromSymbol = FromSymbolOrErr->GraphSymbol; 140 else 141 return FromSymbolOrErr.takeError(); 142 143 // Read the current fixup value. 144 uint64_t FixupValue = 0; 145 if (SubRI.r_length == 3) 146 FixupValue = *(const little64_t *)FixupContent; 147 else 148 FixupValue = *(const little32_t *)FixupContent; 149 150 // Find 'ToSymbol' using symbol number or address, depending on whether the 151 // paired UNSIGNED relocation is extern. 152 Symbol *ToSymbol = nullptr; 153 if (UnsignedRI.r_extern) { 154 // Find target symbol by symbol index. 155 if (auto ToSymbolOrErr = findSymbolByIndex(UnsignedRI.r_symbolnum)) 156 ToSymbol = ToSymbolOrErr->GraphSymbol; 157 else 158 return ToSymbolOrErr.takeError(); 159 } else { 160 auto ToSymbolSec = findSectionByIndex(UnsignedRI.r_symbolnum - 1); 161 if (!ToSymbolSec) 162 return ToSymbolSec.takeError(); 163 ToSymbol = getSymbolByAddress(*ToSymbolSec, ToSymbolSec->Address); 164 assert(ToSymbol && "No symbol for section"); 165 FixupValue -= ToSymbol->getAddress(); 166 } 167 168 MachOARM64RelocationKind DeltaKind; 169 Symbol *TargetSymbol; 170 uint64_t Addend; 171 if (&BlockToFix == &FromSymbol->getAddressable()) { 172 TargetSymbol = ToSymbol; 173 DeltaKind = (SubRI.r_length == 3) ? Delta64 : Delta32; 174 Addend = FixupValue + (FixupAddress - FromSymbol->getAddress()); 175 // FIXME: handle extern 'from'. 176 } else if (&BlockToFix == &ToSymbol->getAddressable()) { 177 TargetSymbol = &*FromSymbol; 178 DeltaKind = (SubRI.r_length == 3) ? NegDelta64 : NegDelta32; 179 Addend = FixupValue - (FixupAddress - ToSymbol->getAddress()); 180 } else { 181 // BlockToFix was neither FromSymbol nor ToSymbol. 182 return make_error<JITLinkError>("SUBTRACTOR relocation must fix up " 183 "either 'A' or 'B' (or a symbol in one " 184 "of their alt-entry groups)"); 185 } 186 187 return PairRelocInfo(DeltaKind, TargetSymbol, Addend); 188 } 189 190 Error addRelocations() override { 191 using namespace support; 192 auto &Obj = getObject(); 193 194 LLVM_DEBUG(dbgs() << "Processing relocations:\n"); 195 196 for (auto &S : Obj.sections()) { 197 198 JITTargetAddress SectionAddress = S.getAddress(); 199 200 // Skip relocations virtual sections. 201 if (S.isVirtual()) { 202 if (S.relocation_begin() != S.relocation_end()) 203 return make_error<JITLinkError>("Virtual section contains " 204 "relocations"); 205 continue; 206 } 207 208 auto NSec = 209 findSectionByIndex(Obj.getSectionIndex(S.getRawDataRefImpl())); 210 if (!NSec) 211 return NSec.takeError(); 212 213 // Skip relocations for MachO sections without corresponding graph 214 // sections. 215 { 216 if (!NSec->GraphSection) { 217 LLVM_DEBUG({ 218 dbgs() << " Skipping relocations for MachO section " 219 << NSec->SegName << "/" << NSec->SectName 220 << " which has no associated graph section\n"; 221 }); 222 continue; 223 } 224 } 225 226 for (auto RelItr = S.relocation_begin(), RelEnd = S.relocation_end(); 227 RelItr != RelEnd; ++RelItr) { 228 229 MachO::relocation_info RI = getRelocationInfo(RelItr); 230 231 // Validate the relocation kind. 232 auto Kind = getRelocationKind(RI); 233 if (!Kind) 234 return Kind.takeError(); 235 236 // Find the address of the value to fix up. 237 JITTargetAddress FixupAddress = SectionAddress + (uint32_t)RI.r_address; 238 LLVM_DEBUG({ 239 dbgs() << " " << NSec->SectName << " + " 240 << formatv("{0:x8}", RI.r_address) << ":\n"; 241 }); 242 243 // Find the block that the fixup points to. 244 Block *BlockToFix = nullptr; 245 { 246 auto SymbolToFixOrErr = findSymbolByAddress(*NSec, FixupAddress); 247 if (!SymbolToFixOrErr) 248 return SymbolToFixOrErr.takeError(); 249 BlockToFix = &SymbolToFixOrErr->getBlock(); 250 } 251 252 if (FixupAddress + static_cast<JITTargetAddress>(1ULL << RI.r_length) > 253 BlockToFix->getAddress() + BlockToFix->getContent().size()) 254 return make_error<JITLinkError>( 255 "Relocation content extends past end of fixup block"); 256 257 // Get a pointer to the fixup content. 258 const char *FixupContent = BlockToFix->getContent().data() + 259 (FixupAddress - BlockToFix->getAddress()); 260 261 // The target symbol and addend will be populated by the switch below. 262 Symbol *TargetSymbol = nullptr; 263 uint64_t Addend = 0; 264 265 if (*Kind == PairedAddend) { 266 // If this is an Addend relocation then process it and move to the 267 // paired reloc. 268 269 Addend = SignExtend64(RI.r_symbolnum, 24); 270 271 if (RelItr == RelEnd) 272 return make_error<JITLinkError>("Unpaired Addend reloc at " + 273 formatv("{0:x16}", FixupAddress)); 274 ++RelItr; 275 RI = getRelocationInfo(RelItr); 276 277 Kind = getRelocationKind(RI); 278 if (!Kind) 279 return Kind.takeError(); 280 281 if (*Kind != Branch26 && *Kind != Page21 && *Kind != PageOffset12) 282 return make_error<JITLinkError>( 283 "Invalid relocation pair: Addend + " + 284 StringRef(getMachOARM64RelocationKindName(*Kind))); 285 286 LLVM_DEBUG({ 287 dbgs() << " Addend: value = " << formatv("{0:x6}", Addend) 288 << ", pair is " << getMachOARM64RelocationKindName(*Kind) 289 << "\n"; 290 }); 291 292 // Find the address of the value to fix up. 293 JITTargetAddress PairedFixupAddress = 294 SectionAddress + (uint32_t)RI.r_address; 295 if (PairedFixupAddress != FixupAddress) 296 return make_error<JITLinkError>("Paired relocation points at " 297 "different target"); 298 } 299 300 switch (*Kind) { 301 case Branch26: { 302 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum)) 303 TargetSymbol = TargetSymbolOrErr->GraphSymbol; 304 else 305 return TargetSymbolOrErr.takeError(); 306 uint32_t Instr = *(const ulittle32_t *)FixupContent; 307 if ((Instr & 0x7fffffff) != 0x14000000) 308 return make_error<JITLinkError>("BRANCH26 target is not a B or BL " 309 "instruction with a zero addend"); 310 break; 311 } 312 case Pointer32: 313 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum)) 314 TargetSymbol = TargetSymbolOrErr->GraphSymbol; 315 else 316 return TargetSymbolOrErr.takeError(); 317 Addend = *(const ulittle32_t *)FixupContent; 318 break; 319 case Pointer64: 320 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum)) 321 TargetSymbol = TargetSymbolOrErr->GraphSymbol; 322 else 323 return TargetSymbolOrErr.takeError(); 324 Addend = *(const ulittle64_t *)FixupContent; 325 break; 326 case Pointer64Anon: { 327 JITTargetAddress TargetAddress = *(const ulittle64_t *)FixupContent; 328 auto TargetNSec = findSectionByIndex(RI.r_symbolnum - 1); 329 if (!TargetNSec) 330 return TargetNSec.takeError(); 331 if (auto TargetSymbolOrErr = 332 findSymbolByAddress(*TargetNSec, TargetAddress)) 333 TargetSymbol = &*TargetSymbolOrErr; 334 else 335 return TargetSymbolOrErr.takeError(); 336 Addend = TargetAddress - TargetSymbol->getAddress(); 337 break; 338 } 339 case Page21: 340 case TLVPage21: 341 case GOTPage21: { 342 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum)) 343 TargetSymbol = TargetSymbolOrErr->GraphSymbol; 344 else 345 return TargetSymbolOrErr.takeError(); 346 uint32_t Instr = *(const ulittle32_t *)FixupContent; 347 if ((Instr & 0xffffffe0) != 0x90000000) 348 return make_error<JITLinkError>("PAGE21/GOTPAGE21 target is not an " 349 "ADRP instruction with a zero " 350 "addend"); 351 break; 352 } 353 case PageOffset12: { 354 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum)) 355 TargetSymbol = TargetSymbolOrErr->GraphSymbol; 356 else 357 return TargetSymbolOrErr.takeError(); 358 uint32_t Instr = *(const ulittle32_t *)FixupContent; 359 uint32_t EncodedAddend = (Instr & 0x003FFC00) >> 10; 360 if (EncodedAddend != 0) 361 return make_error<JITLinkError>("GOTPAGEOFF12 target has non-zero " 362 "encoded addend"); 363 break; 364 } 365 case TLVPageOffset12: 366 case GOTPageOffset12: { 367 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum)) 368 TargetSymbol = TargetSymbolOrErr->GraphSymbol; 369 else 370 return TargetSymbolOrErr.takeError(); 371 uint32_t Instr = *(const ulittle32_t *)FixupContent; 372 if ((Instr & 0xfffffc00) != 0xf9400000) 373 return make_error<JITLinkError>("GOTPAGEOFF12 target is not an LDR " 374 "immediate instruction with a zero " 375 "addend"); 376 break; 377 } 378 case PointerToGOT: 379 if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum)) 380 TargetSymbol = TargetSymbolOrErr->GraphSymbol; 381 else 382 return TargetSymbolOrErr.takeError(); 383 break; 384 case Delta32: 385 case Delta64: { 386 // We use Delta32/Delta64 to represent SUBTRACTOR relocations. 387 // parsePairRelocation handles the paired reloc, and returns the 388 // edge kind to be used (either Delta32/Delta64, or 389 // NegDelta32/NegDelta64, depending on the direction of the 390 // subtraction) along with the addend. 391 auto PairInfo = 392 parsePairRelocation(*BlockToFix, *Kind, RI, FixupAddress, 393 FixupContent, ++RelItr, RelEnd); 394 if (!PairInfo) 395 return PairInfo.takeError(); 396 std::tie(*Kind, TargetSymbol, Addend) = *PairInfo; 397 assert(TargetSymbol && "No target symbol from parsePairRelocation?"); 398 break; 399 } 400 default: 401 llvm_unreachable("Special relocation kind should not appear in " 402 "mach-o file"); 403 } 404 405 LLVM_DEBUG({ 406 dbgs() << " "; 407 Edge GE(*Kind, FixupAddress - BlockToFix->getAddress(), *TargetSymbol, 408 Addend); 409 printEdge(dbgs(), *BlockToFix, GE, 410 getMachOARM64RelocationKindName(*Kind)); 411 dbgs() << "\n"; 412 }); 413 BlockToFix->addEdge(*Kind, FixupAddress - BlockToFix->getAddress(), 414 *TargetSymbol, Addend); 415 } 416 } 417 return Error::success(); 418 } 419 420 unsigned NumSymbols = 0; 421 }; 422 423 class PerGraphGOTAndPLTStubsBuilder_MachO_arm64 424 : public PerGraphGOTAndPLTStubsBuilder< 425 PerGraphGOTAndPLTStubsBuilder_MachO_arm64> { 426 public: 427 using PerGraphGOTAndPLTStubsBuilder< 428 PerGraphGOTAndPLTStubsBuilder_MachO_arm64>::PerGraphGOTAndPLTStubsBuilder; 429 430 bool isGOTEdgeToFix(Edge &E) const { 431 return E.getKind() == GOTPage21 || E.getKind() == GOTPageOffset12 || 432 E.getKind() == TLVPage21 || E.getKind() == TLVPageOffset12 || 433 E.getKind() == PointerToGOT; 434 } 435 436 Symbol &createGOTEntry(Symbol &Target) { 437 auto &GOTEntryBlock = G.createContentBlock( 438 getGOTSection(), getGOTEntryBlockContent(), 0, 8, 0); 439 GOTEntryBlock.addEdge(Pointer64, 0, Target, 0); 440 return G.addAnonymousSymbol(GOTEntryBlock, 0, 8, false, false); 441 } 442 443 void fixGOTEdge(Edge &E, Symbol &GOTEntry) { 444 if (E.getKind() == GOTPage21 || E.getKind() == GOTPageOffset12 || 445 E.getKind() == TLVPage21 || E.getKind() == TLVPageOffset12) { 446 // Update the target, but leave the edge addend as-is. 447 E.setTarget(GOTEntry); 448 } else if (E.getKind() == PointerToGOT) { 449 E.setTarget(GOTEntry); 450 E.setKind(Delta32); 451 } else 452 llvm_unreachable("Not a GOT edge?"); 453 } 454 455 bool isExternalBranchEdge(Edge &E) { 456 return E.getKind() == Branch26 && !E.getTarget().isDefined(); 457 } 458 459 Symbol &createPLTStub(Symbol &Target) { 460 auto &StubContentBlock = 461 G.createContentBlock(getStubsSection(), getStubBlockContent(), 0, 1, 0); 462 // Re-use GOT entries for stub targets. 463 auto &GOTEntrySymbol = getGOTEntry(Target); 464 StubContentBlock.addEdge(LDRLiteral19, 0, GOTEntrySymbol, 0); 465 return G.addAnonymousSymbol(StubContentBlock, 0, 8, true, false); 466 } 467 468 void fixPLTEdge(Edge &E, Symbol &Stub) { 469 assert(E.getKind() == Branch26 && "Not a Branch32 edge?"); 470 assert(E.getAddend() == 0 && "Branch32 edge has non-zero addend?"); 471 E.setTarget(Stub); 472 } 473 474 private: 475 Section &getGOTSection() { 476 if (!GOTSection) 477 GOTSection = &G.createSection("$__GOT", MemProt::Read); 478 return *GOTSection; 479 } 480 481 Section &getStubsSection() { 482 if (!StubsSection) 483 StubsSection = 484 &G.createSection("$__STUBS", MemProt::Read | MemProt::Exec); 485 return *StubsSection; 486 } 487 488 ArrayRef<char> getGOTEntryBlockContent() { 489 return {reinterpret_cast<const char *>(NullGOTEntryContent), 490 sizeof(NullGOTEntryContent)}; 491 } 492 493 ArrayRef<char> getStubBlockContent() { 494 return {reinterpret_cast<const char *>(StubContent), sizeof(StubContent)}; 495 } 496 497 static const uint8_t NullGOTEntryContent[8]; 498 static const uint8_t StubContent[8]; 499 Section *GOTSection = nullptr; 500 Section *StubsSection = nullptr; 501 }; 502 503 const uint8_t 504 PerGraphGOTAndPLTStubsBuilder_MachO_arm64::NullGOTEntryContent[8] = { 505 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 506 const uint8_t PerGraphGOTAndPLTStubsBuilder_MachO_arm64::StubContent[8] = { 507 0x10, 0x00, 0x00, 0x58, // LDR x16, <literal> 508 0x00, 0x02, 0x1f, 0xd6 // BR x16 509 }; 510 511 } // namespace 512 513 namespace llvm { 514 namespace jitlink { 515 516 class MachOJITLinker_arm64 : public JITLinker<MachOJITLinker_arm64> { 517 friend class JITLinker<MachOJITLinker_arm64>; 518 519 public: 520 MachOJITLinker_arm64(std::unique_ptr<JITLinkContext> Ctx, 521 std::unique_ptr<LinkGraph> G, 522 PassConfiguration PassConfig) 523 : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {} 524 525 private: 526 527 static unsigned getPageOffset12Shift(uint32_t Instr) { 528 constexpr uint32_t LoadStoreImm12Mask = 0x3b000000; 529 constexpr uint32_t Vec128Mask = 0x04800000; 530 531 if ((Instr & LoadStoreImm12Mask) == 0x39000000) { 532 uint32_t ImplicitShift = Instr >> 30; 533 if (ImplicitShift == 0) 534 if ((Instr & Vec128Mask) == Vec128Mask) 535 ImplicitShift = 4; 536 537 return ImplicitShift; 538 } 539 540 return 0; 541 } 542 543 Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const { 544 using namespace support; 545 546 char *BlockWorkingMem = B.getAlreadyMutableContent().data(); 547 char *FixupPtr = BlockWorkingMem + E.getOffset(); 548 JITTargetAddress FixupAddress = B.getAddress() + E.getOffset(); 549 550 switch (E.getKind()) { 551 case Branch26: { 552 assert((FixupAddress & 0x3) == 0 && "Branch-inst is not 32-bit aligned"); 553 554 int64_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend(); 555 556 if (static_cast<uint64_t>(Value) & 0x3) 557 return make_error<JITLinkError>("Branch26 target is not 32-bit " 558 "aligned"); 559 560 if (Value < -(1 << 27) || Value > ((1 << 27) - 1)) 561 return makeTargetOutOfRangeError(G, B, E); 562 563 uint32_t RawInstr = *(little32_t *)FixupPtr; 564 assert((RawInstr & 0x7fffffff) == 0x14000000 && 565 "RawInstr isn't a B or BR immediate instruction"); 566 uint32_t Imm = (static_cast<uint32_t>(Value) & ((1 << 28) - 1)) >> 2; 567 uint32_t FixedInstr = RawInstr | Imm; 568 *(little32_t *)FixupPtr = FixedInstr; 569 break; 570 } 571 case Pointer32: { 572 uint64_t Value = E.getTarget().getAddress() + E.getAddend(); 573 if (Value > std::numeric_limits<uint32_t>::max()) 574 return makeTargetOutOfRangeError(G, B, E); 575 *(ulittle32_t *)FixupPtr = Value; 576 break; 577 } 578 case Pointer64: 579 case Pointer64Anon: { 580 uint64_t Value = E.getTarget().getAddress() + E.getAddend(); 581 *(ulittle64_t *)FixupPtr = Value; 582 break; 583 } 584 case Page21: 585 case TLVPage21: 586 case GOTPage21: { 587 assert((E.getKind() != GOTPage21 || E.getAddend() == 0) && 588 "GOTPAGE21 with non-zero addend"); 589 uint64_t TargetPage = 590 (E.getTarget().getAddress() + E.getAddend()) & 591 ~static_cast<uint64_t>(4096 - 1); 592 uint64_t PCPage = FixupAddress & ~static_cast<uint64_t>(4096 - 1); 593 594 int64_t PageDelta = TargetPage - PCPage; 595 if (PageDelta < -(1 << 30) || PageDelta > ((1 << 30) - 1)) 596 return makeTargetOutOfRangeError(G, B, E); 597 598 uint32_t RawInstr = *(ulittle32_t *)FixupPtr; 599 assert((RawInstr & 0xffffffe0) == 0x90000000 && 600 "RawInstr isn't an ADRP instruction"); 601 uint32_t ImmLo = (static_cast<uint64_t>(PageDelta) >> 12) & 0x3; 602 uint32_t ImmHi = (static_cast<uint64_t>(PageDelta) >> 14) & 0x7ffff; 603 uint32_t FixedInstr = RawInstr | (ImmLo << 29) | (ImmHi << 5); 604 *(ulittle32_t *)FixupPtr = FixedInstr; 605 break; 606 } 607 case PageOffset12: { 608 uint64_t TargetOffset = 609 (E.getTarget().getAddress() + E.getAddend()) & 0xfff; 610 611 uint32_t RawInstr = *(ulittle32_t *)FixupPtr; 612 unsigned ImmShift = getPageOffset12Shift(RawInstr); 613 614 if (TargetOffset & ((1 << ImmShift) - 1)) 615 return make_error<JITLinkError>("PAGEOFF12 target is not aligned"); 616 617 uint32_t EncodedImm = (TargetOffset >> ImmShift) << 10; 618 uint32_t FixedInstr = RawInstr | EncodedImm; 619 *(ulittle32_t *)FixupPtr = FixedInstr; 620 break; 621 } 622 case TLVPageOffset12: 623 case GOTPageOffset12: { 624 assert(E.getAddend() == 0 && "GOTPAGEOF12 with non-zero addend"); 625 626 uint32_t RawInstr = *(ulittle32_t *)FixupPtr; 627 assert((RawInstr & 0xfffffc00) == 0xf9400000 && 628 "RawInstr isn't a 64-bit LDR immediate"); 629 630 uint32_t TargetOffset = E.getTarget().getAddress() & 0xfff; 631 assert((TargetOffset & 0x7) == 0 && "GOT entry is not 8-byte aligned"); 632 uint32_t EncodedImm = (TargetOffset >> 3) << 10; 633 uint32_t FixedInstr = RawInstr | EncodedImm; 634 *(ulittle32_t *)FixupPtr = FixedInstr; 635 break; 636 } 637 case LDRLiteral19: { 638 assert((FixupAddress & 0x3) == 0 && "LDR is not 32-bit aligned"); 639 assert(E.getAddend() == 0 && "LDRLiteral19 with non-zero addend"); 640 uint32_t RawInstr = *(ulittle32_t *)FixupPtr; 641 assert(RawInstr == 0x58000010 && "RawInstr isn't a 64-bit LDR literal"); 642 int64_t Delta = E.getTarget().getAddress() - FixupAddress; 643 if (Delta & 0x3) 644 return make_error<JITLinkError>("LDR literal target is not 32-bit " 645 "aligned"); 646 if (Delta < -(1 << 20) || Delta > ((1 << 20) - 1)) 647 return makeTargetOutOfRangeError(G, B, E); 648 649 uint32_t EncodedImm = 650 ((static_cast<uint32_t>(Delta) >> 2) & 0x7ffff) << 5; 651 uint32_t FixedInstr = RawInstr | EncodedImm; 652 *(ulittle32_t *)FixupPtr = FixedInstr; 653 break; 654 } 655 case Delta32: 656 case Delta64: 657 case NegDelta32: 658 case NegDelta64: { 659 int64_t Value; 660 if (E.getKind() == Delta32 || E.getKind() == Delta64) 661 Value = E.getTarget().getAddress() - FixupAddress + E.getAddend(); 662 else 663 Value = FixupAddress - E.getTarget().getAddress() + E.getAddend(); 664 665 if (E.getKind() == Delta32 || E.getKind() == NegDelta32) { 666 if (Value < std::numeric_limits<int32_t>::min() || 667 Value > std::numeric_limits<int32_t>::max()) 668 return makeTargetOutOfRangeError(G, B, E); 669 *(little32_t *)FixupPtr = Value; 670 } else 671 *(little64_t *)FixupPtr = Value; 672 break; 673 } 674 default: 675 llvm_unreachable("Unrecognized edge kind"); 676 } 677 678 return Error::success(); 679 } 680 681 uint64_t NullValue = 0; 682 }; 683 684 Expected<std::unique_ptr<LinkGraph>> 685 createLinkGraphFromMachOObject_arm64(MemoryBufferRef ObjectBuffer) { 686 auto MachOObj = object::ObjectFile::createMachOObjectFile(ObjectBuffer); 687 if (!MachOObj) 688 return MachOObj.takeError(); 689 return MachOLinkGraphBuilder_arm64(**MachOObj).buildGraph(); 690 } 691 692 void link_MachO_arm64(std::unique_ptr<LinkGraph> G, 693 std::unique_ptr<JITLinkContext> Ctx) { 694 695 PassConfiguration Config; 696 697 if (Ctx->shouldAddDefaultTargetPasses(G->getTargetTriple())) { 698 // Add a mark-live pass. 699 if (auto MarkLive = Ctx->getMarkLivePass(G->getTargetTriple())) 700 Config.PrePrunePasses.push_back(std::move(MarkLive)); 701 else 702 Config.PrePrunePasses.push_back(markAllSymbolsLive); 703 704 // Add compact unwind splitter pass. 705 Config.PrePrunePasses.push_back( 706 CompactUnwindSplitter("__LD,__compact_unwind")); 707 708 // Add an in-place GOT/Stubs pass. 709 Config.PostPrunePasses.push_back( 710 PerGraphGOTAndPLTStubsBuilder_MachO_arm64::asPass); 711 } 712 713 if (auto Err = Ctx->modifyPassConfig(*G, Config)) 714 return Ctx->notifyFailed(std::move(Err)); 715 716 // Construct a JITLinker and run the link function. 717 MachOJITLinker_arm64::link(std::move(Ctx), std::move(G), std::move(Config)); 718 } 719 720 const char *getMachOARM64RelocationKindName(Edge::Kind R) { 721 switch (R) { 722 case Branch26: 723 return "Branch26"; 724 case Pointer64: 725 return "Pointer64"; 726 case Pointer64Anon: 727 return "Pointer64Anon"; 728 case Page21: 729 return "Page21"; 730 case PageOffset12: 731 return "PageOffset12"; 732 case GOTPage21: 733 return "GOTPage21"; 734 case GOTPageOffset12: 735 return "GOTPageOffset12"; 736 case TLVPage21: 737 return "TLVPage21"; 738 case TLVPageOffset12: 739 return "TLVPageOffset12"; 740 case PointerToGOT: 741 return "PointerToGOT"; 742 case PairedAddend: 743 return "PairedAddend"; 744 case LDRLiteral19: 745 return "LDRLiteral19"; 746 case Delta32: 747 return "Delta32"; 748 case Delta64: 749 return "Delta64"; 750 case NegDelta32: 751 return "NegDelta32"; 752 case NegDelta64: 753 return "NegDelta64"; 754 default: 755 return getGenericEdgeKindName(static_cast<Edge::Kind>(R)); 756 } 757 } 758 759 } // end namespace jitlink 760 } // end namespace llvm 761