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