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