1 //===---- MachO_x86_64.cpp -JIT linker implementation for MachO/x86-64 ----===//
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/x86-64 jit-link implementation.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ExecutionEngine/JITLink/MachO_x86_64.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_x86_64_Edges;
23 
24 namespace {
25 
26 class MachOLinkGraphBuilder_x86_64 : public MachOLinkGraphBuilder {
27 public:
28   MachOLinkGraphBuilder_x86_64(const object::MachOObjectFile &Obj)
29       : MachOLinkGraphBuilder(Obj) {}
30 
31 private:
32   static Expected<MachOX86RelocationKind>
33   getRelocationKind(const MachO::relocation_info &RI) {
34     switch (RI.r_type) {
35     case MachO::X86_64_RELOC_UNSIGNED:
36       if (!RI.r_pcrel) {
37         if (RI.r_length == 3)
38           return RI.r_extern ? Pointer64 : Pointer64Anon;
39         else if (RI.r_extern && RI.r_length == 2)
40           return Pointer32;
41       }
42       break;
43     case MachO::X86_64_RELOC_SIGNED:
44       if (RI.r_pcrel && RI.r_length == 2)
45         return RI.r_extern ? PCRel32 : PCRel32Anon;
46       break;
47     case MachO::X86_64_RELOC_BRANCH:
48       if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
49         return Branch32;
50       break;
51     case MachO::X86_64_RELOC_GOT_LOAD:
52       if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
53         return PCRel32GOTLoad;
54       break;
55     case MachO::X86_64_RELOC_GOT:
56       if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
57         return PCRel32GOT;
58       break;
59     case MachO::X86_64_RELOC_SUBTRACTOR:
60       // SUBTRACTOR must be non-pc-rel, extern, with length 2 or 3.
61       // Initially represent SUBTRACTOR relocations with 'Delta<W>'. They may
62       // be turned into NegDelta<W> by parsePairRelocation.
63       if (!RI.r_pcrel && RI.r_extern) {
64         if (RI.r_length == 2)
65           return Delta32;
66         else if (RI.r_length == 3)
67           return Delta64;
68       }
69       break;
70     case MachO::X86_64_RELOC_SIGNED_1:
71       if (RI.r_pcrel && RI.r_length == 2)
72         return RI.r_extern ? PCRel32Minus1 : PCRel32Minus1Anon;
73       break;
74     case MachO::X86_64_RELOC_SIGNED_2:
75       if (RI.r_pcrel && RI.r_length == 2)
76         return RI.r_extern ? PCRel32Minus2 : PCRel32Minus2Anon;
77       break;
78     case MachO::X86_64_RELOC_SIGNED_4:
79       if (RI.r_pcrel && RI.r_length == 2)
80         return RI.r_extern ? PCRel32Minus4 : PCRel32Minus4Anon;
81       break;
82     case MachO::X86_64_RELOC_TLV:
83       if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
84         return PCRel32TLV;
85       break;
86     }
87 
88     return make_error<JITLinkError>(
89         "Unsupported x86-64 relocation: address=" +
90         formatv("{0:x8}", RI.r_address) +
91         ", symbolnum=" + formatv("{0:x6}", RI.r_symbolnum) +
92         ", kind=" + formatv("{0:x1}", RI.r_type) +
93         ", pc_rel=" + (RI.r_pcrel ? "true" : "false") +
94         ", extern=" + (RI.r_extern ? "true" : "false") +
95         ", length=" + formatv("{0:d}", RI.r_length));
96   }
97 
98   using PairRelocInfo = std::tuple<MachOX86RelocationKind, Symbol *, uint64_t>;
99 
100   // Parses paired SUBTRACTOR/UNSIGNED relocations and, on success,
101   // returns the edge kind and addend to be used.
102   Expected<PairRelocInfo>
103   parsePairRelocation(Block &BlockToFix, Edge::Kind SubtractorKind,
104                       const MachO::relocation_info &SubRI,
105                       JITTargetAddress FixupAddress, const char *FixupContent,
106                       object::relocation_iterator &UnsignedRelItr,
107                       object::relocation_iterator &RelEnd) {
108     using namespace support;
109 
110     assert(((SubtractorKind == Delta32 && SubRI.r_length == 2) ||
111             (SubtractorKind == Delta64 && SubRI.r_length == 3)) &&
112            "Subtractor kind should match length");
113     assert(SubRI.r_extern && "SUBTRACTOR reloc symbol should be extern");
114     assert(!SubRI.r_pcrel && "SUBTRACTOR reloc should not be PCRel");
115 
116     if (UnsignedRelItr == RelEnd)
117       return make_error<JITLinkError>("x86_64 SUBTRACTOR without paired "
118                                       "UNSIGNED relocation");
119 
120     auto UnsignedRI = getRelocationInfo(UnsignedRelItr);
121 
122     if (SubRI.r_address != UnsignedRI.r_address)
123       return make_error<JITLinkError>("x86_64 SUBTRACTOR and paired UNSIGNED "
124                                       "point to different addresses");
125 
126     if (SubRI.r_length != UnsignedRI.r_length)
127       return make_error<JITLinkError>("length of x86_64 SUBTRACTOR and paired "
128                                       "UNSIGNED reloc must match");
129 
130     Symbol *FromSymbol;
131     if (auto FromSymbolOrErr = findSymbolByIndex(SubRI.r_symbolnum))
132       FromSymbol = FromSymbolOrErr->GraphSymbol;
133     else
134       return FromSymbolOrErr.takeError();
135 
136     // Read the current fixup value.
137     uint64_t FixupValue = 0;
138     if (SubRI.r_length == 3)
139       FixupValue = *(const little64_t *)FixupContent;
140     else
141       FixupValue = *(const little32_t *)FixupContent;
142 
143     // Find 'ToSymbol' using symbol number or address, depending on whether the
144     // paired UNSIGNED relocation is extern.
145     Symbol *ToSymbol = nullptr;
146     if (UnsignedRI.r_extern) {
147       // Find target symbol by symbol index.
148       if (auto ToSymbolOrErr = findSymbolByIndex(UnsignedRI.r_symbolnum))
149         ToSymbol = ToSymbolOrErr->GraphSymbol;
150       else
151         return ToSymbolOrErr.takeError();
152     } else {
153       if (auto ToSymbolOrErr = findSymbolByAddress(FixupValue))
154         ToSymbol = &*ToSymbolOrErr;
155       else
156         return ToSymbolOrErr.takeError();
157       FixupValue -= ToSymbol->getAddress();
158     }
159 
160     MachOX86RelocationKind DeltaKind;
161     Symbol *TargetSymbol;
162     uint64_t Addend;
163     if (&BlockToFix == &FromSymbol->getAddressable()) {
164       TargetSymbol = ToSymbol;
165       DeltaKind = (SubRI.r_length == 3) ? Delta64 : Delta32;
166       Addend = FixupValue + (FixupAddress - FromSymbol->getAddress());
167       // FIXME: handle extern 'from'.
168     } else if (&BlockToFix == &ToSymbol->getAddressable()) {
169       TargetSymbol = FromSymbol;
170       DeltaKind = (SubRI.r_length == 3) ? NegDelta64 : NegDelta32;
171       Addend = FixupValue - (FixupAddress - ToSymbol->getAddress());
172     } else {
173       // BlockToFix was neither FromSymbol nor ToSymbol.
174       return make_error<JITLinkError>("SUBTRACTOR relocation must fix up "
175                                       "either 'A' or 'B' (or a symbol in one "
176                                       "of their alt-entry chains)");
177     }
178 
179     return PairRelocInfo(DeltaKind, TargetSymbol, Addend);
180   }
181 
182   Error addRelocations() override {
183     using namespace support;
184     auto &Obj = getObject();
185 
186     for (auto &S : Obj.sections()) {
187 
188       JITTargetAddress SectionAddress = S.getAddress();
189 
190       if (S.isVirtual()) {
191         if (S.relocation_begin() != S.relocation_end())
192           return make_error<JITLinkError>("Virtual section contains "
193                                           "relocations");
194         continue;
195       }
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 relocation at "
212                  << format("0x%016" PRIx64, FixupAddress) << "\n";
213         });
214 
215         // Find the block that the fixup points to.
216         Block *BlockToFix = nullptr;
217         {
218           auto SymbolToFixOrErr = findSymbolByAddress(FixupAddress);
219           if (!SymbolToFixOrErr)
220             return SymbolToFixOrErr.takeError();
221           BlockToFix = &SymbolToFixOrErr->getBlock();
222         }
223 
224         if (FixupAddress + static_cast<JITTargetAddress>(1ULL << RI.r_length) >
225             BlockToFix->getAddress() + BlockToFix->getContent().size())
226           return make_error<JITLinkError>(
227               "Relocation extends past end of fixup block");
228 
229         // Get a pointer to the fixup content.
230         const char *FixupContent = BlockToFix->getContent().data() +
231                                    (FixupAddress - BlockToFix->getAddress());
232 
233         // The target symbol and addend will be populated by the switch below.
234         Symbol *TargetSymbol = nullptr;
235         uint64_t Addend = 0;
236 
237         switch (*Kind) {
238         case Branch32:
239         case PCRel32:
240         case PCRel32GOTLoad:
241         case PCRel32GOT:
242           if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
243             TargetSymbol = TargetSymbolOrErr->GraphSymbol;
244           else
245             return TargetSymbolOrErr.takeError();
246           Addend = *(const little32_t *)FixupContent;
247           break;
248         case Pointer32:
249           if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
250             TargetSymbol = TargetSymbolOrErr->GraphSymbol;
251           else
252             return TargetSymbolOrErr.takeError();
253           Addend = *(const ulittle32_t *)FixupContent;
254           break;
255         case Pointer64:
256           if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
257             TargetSymbol = TargetSymbolOrErr->GraphSymbol;
258           else
259             return TargetSymbolOrErr.takeError();
260           Addend = *(const ulittle64_t *)FixupContent;
261           break;
262         case Pointer64Anon: {
263           JITTargetAddress TargetAddress = *(const ulittle64_t *)FixupContent;
264           if (auto TargetSymbolOrErr = findSymbolByAddress(TargetAddress))
265             TargetSymbol = &*TargetSymbolOrErr;
266           else
267             return TargetSymbolOrErr.takeError();
268           Addend = TargetAddress - TargetSymbol->getAddress();
269           break;
270         }
271         case PCRel32Minus1:
272         case PCRel32Minus2:
273         case PCRel32Minus4:
274           if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
275             TargetSymbol = TargetSymbolOrErr->GraphSymbol;
276           else
277             return TargetSymbolOrErr.takeError();
278           Addend = *(const little32_t *)FixupContent +
279                    (1 << (*Kind - PCRel32Minus1));
280           break;
281         case PCRel32Anon: {
282           JITTargetAddress TargetAddress =
283               FixupAddress + 4 + *(const little32_t *)FixupContent;
284           if (auto TargetSymbolOrErr = findSymbolByAddress(TargetAddress))
285             TargetSymbol = &*TargetSymbolOrErr;
286           else
287             return TargetSymbolOrErr.takeError();
288           Addend = TargetAddress - TargetSymbol->getAddress();
289           break;
290         }
291         case PCRel32Minus1Anon:
292         case PCRel32Minus2Anon:
293         case PCRel32Minus4Anon: {
294           JITTargetAddress Delta =
295               static_cast<JITTargetAddress>(1ULL << (*Kind - PCRel32Minus1Anon));
296           JITTargetAddress TargetAddress =
297               FixupAddress + 4 + Delta + *(const little32_t *)FixupContent;
298           if (auto TargetSymbolOrErr = findSymbolByAddress(TargetAddress))
299             TargetSymbol = &*TargetSymbolOrErr;
300           else
301             return TargetSymbolOrErr.takeError();
302           Addend = TargetAddress - TargetSymbol->getAddress();
303           break;
304         }
305         case Delta32:
306         case Delta64: {
307           // We use Delta32/Delta64 to represent SUBTRACTOR relocations.
308           // parsePairRelocation handles the paired reloc, and returns the
309           // edge kind to be used (either Delta32/Delta64, or
310           // NegDelta32/NegDelta64, depending on the direction of the
311           // subtraction) along with the addend.
312           auto PairInfo =
313               parsePairRelocation(*BlockToFix, *Kind, RI, FixupAddress,
314                                   FixupContent, ++RelItr, RelEnd);
315           if (!PairInfo)
316             return PairInfo.takeError();
317           std::tie(*Kind, TargetSymbol, Addend) = *PairInfo;
318           assert(TargetSymbol && "No target symbol from parsePairRelocation?");
319           break;
320         }
321         default:
322           llvm_unreachable("Special relocation kind should not appear in "
323                            "mach-o file");
324         }
325 
326         LLVM_DEBUG({
327           Edge GE(*Kind, FixupAddress - BlockToFix->getAddress(), *TargetSymbol,
328                   Addend);
329           printEdge(dbgs(), *BlockToFix, GE,
330                     getMachOX86RelocationKindName(*Kind));
331           dbgs() << "\n";
332         });
333         BlockToFix->addEdge(*Kind, FixupAddress - BlockToFix->getAddress(),
334                             *TargetSymbol, Addend);
335       }
336     }
337     return Error::success();
338   }
339 };
340 
341 class MachO_x86_64_GOTAndStubsBuilder
342     : public BasicGOTAndStubsBuilder<MachO_x86_64_GOTAndStubsBuilder> {
343 public:
344   static const uint8_t NullGOTEntryContent[8];
345   static const uint8_t StubContent[6];
346 
347   MachO_x86_64_GOTAndStubsBuilder(LinkGraph &G)
348       : BasicGOTAndStubsBuilder<MachO_x86_64_GOTAndStubsBuilder>(G) {}
349 
350   bool isGOTEdge(Edge &E) const {
351     return E.getKind() == PCRel32GOT || E.getKind() == PCRel32GOTLoad;
352   }
353 
354   Symbol &createGOTEntry(Symbol &Target) {
355     auto &GOTEntryBlock = G.createContentBlock(
356         getGOTSection(), getGOTEntryBlockContent(), 0, 8, 0);
357     GOTEntryBlock.addEdge(Pointer64, 0, Target, 0);
358     return G.addAnonymousSymbol(GOTEntryBlock, 0, 8, false, false);
359   }
360 
361   void fixGOTEdge(Edge &E, Symbol &GOTEntry) {
362     assert((E.getKind() == PCRel32GOT || E.getKind() == PCRel32GOTLoad) &&
363            "Not a GOT edge?");
364     // If this is a PCRel32GOT then change it to an ordinary PCRel32. If it is
365     // a PCRel32GOTLoad then leave it as-is for now. We will use the kind to
366     // check for GOT optimization opportunities in the
367     // optimizeMachO_x86_64_GOTAndStubs pass below.
368     if (E.getKind() == PCRel32GOT)
369       E.setKind(PCRel32);
370 
371     E.setTarget(GOTEntry);
372     // Leave the edge addend as-is.
373   }
374 
375   bool isExternalBranchEdge(Edge &E) {
376     return E.getKind() == Branch32 && !E.getTarget().isDefined();
377   }
378 
379   Symbol &createStub(Symbol &Target) {
380     auto &StubContentBlock =
381         G.createContentBlock(getStubsSection(), getStubBlockContent(), 0, 1, 0);
382     // Re-use GOT entries for stub targets.
383     auto &GOTEntrySymbol = getGOTEntrySymbol(Target);
384     StubContentBlock.addEdge(PCRel32, 2, GOTEntrySymbol, 0);
385     return G.addAnonymousSymbol(StubContentBlock, 0, 6, true, false);
386   }
387 
388   void fixExternalBranchEdge(Edge &E, Symbol &Stub) {
389     assert(E.getKind() == Branch32 && "Not a Branch32 edge?");
390     assert(E.getAddend() == 0 && "Branch32 edge has non-zero addend?");
391 
392     // Set the edge kind to Branch32ToStub. We will use this to check for stub
393     // optimization opportunities in the optimizeMachO_x86_64_GOTAndStubs pass
394     // below.
395     E.setKind(Branch32ToStub);
396     E.setTarget(Stub);
397   }
398 
399 private:
400   Section &getGOTSection() {
401     if (!GOTSection)
402       GOTSection = &G.createSection("$__GOT", sys::Memory::MF_READ);
403     return *GOTSection;
404   }
405 
406   Section &getStubsSection() {
407     if (!StubsSection) {
408       auto StubsProt = static_cast<sys::Memory::ProtectionFlags>(
409           sys::Memory::MF_READ | sys::Memory::MF_EXEC);
410       StubsSection = &G.createSection("$__STUBS", StubsProt);
411     }
412     return *StubsSection;
413   }
414 
415   StringRef getGOTEntryBlockContent() {
416     return StringRef(reinterpret_cast<const char *>(NullGOTEntryContent),
417                      sizeof(NullGOTEntryContent));
418   }
419 
420   StringRef getStubBlockContent() {
421     return StringRef(reinterpret_cast<const char *>(StubContent),
422                      sizeof(StubContent));
423   }
424 
425   Section *GOTSection = nullptr;
426   Section *StubsSection = nullptr;
427 };
428 
429 const uint8_t MachO_x86_64_GOTAndStubsBuilder::NullGOTEntryContent[8] = {
430     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
431 const uint8_t MachO_x86_64_GOTAndStubsBuilder::StubContent[6] = {
432     0xFF, 0x25, 0x00, 0x00, 0x00, 0x00};
433 } // namespace
434 
435 static Error optimizeMachO_x86_64_GOTAndStubs(LinkGraph &G) {
436   LLVM_DEBUG(dbgs() << "Optimizing GOT entries and stubs:\n");
437 
438   for (auto *B : G.blocks())
439     for (auto &E : B->edges())
440       if (E.getKind() == PCRel32GOTLoad) {
441         assert(E.getOffset() >= 3 && "GOT edge occurs too early in block");
442 
443         // Switch the edge kind to PCRel32: Whether we change the edge target
444         // or not this will be the desired kind.
445         E.setKind(PCRel32);
446 
447         // Optimize GOT references.
448         auto &GOTBlock = E.getTarget().getBlock();
449         assert(GOTBlock.getSize() == G.getPointerSize() &&
450                "GOT entry block should be pointer sized");
451         assert(GOTBlock.edges_size() == 1 &&
452                "GOT entry should only have one outgoing edge");
453 
454         auto &GOTTarget = GOTBlock.edges().begin()->getTarget();
455         JITTargetAddress EdgeAddr = B->getAddress() + E.getOffset();
456         JITTargetAddress TargetAddr = GOTTarget.getAddress();
457 
458         // Check that this is a recognized MOV instruction.
459         // FIXME: Can we assume this?
460         constexpr uint8_t MOVQRIPRel[] = {0x48, 0x8b};
461         if (strncmp(B->getContent().data() + E.getOffset() - 3,
462                     reinterpret_cast<const char *>(MOVQRIPRel), 2) != 0)
463           continue;
464 
465         int64_t Displacement = TargetAddr - EdgeAddr + 4;
466         if (Displacement >= std::numeric_limits<int32_t>::min() &&
467             Displacement <= std::numeric_limits<int32_t>::max()) {
468           E.setTarget(GOTTarget);
469           auto *BlockData = reinterpret_cast<uint8_t *>(
470               const_cast<char *>(B->getContent().data()));
471           BlockData[E.getOffset() - 2] = 0x8d;
472           LLVM_DEBUG({
473             dbgs() << "  Replaced GOT load wih LEA:\n    ";
474             printEdge(dbgs(), *B, E,
475                       getMachOX86RelocationKindName(E.getKind()));
476             dbgs() << "\n";
477           });
478         }
479       } else if (E.getKind() == Branch32ToStub) {
480 
481         // Switch the edge kind to PCRel32: Whether we change the edge target
482         // or not this will be the desired kind.
483         E.setKind(Branch32);
484 
485         auto &StubBlock = E.getTarget().getBlock();
486         assert(StubBlock.getSize() ==
487                    sizeof(MachO_x86_64_GOTAndStubsBuilder::StubContent) &&
488                "Stub block should be stub sized");
489         assert(StubBlock.edges_size() == 1 &&
490                "Stub block should only have one outgoing edge");
491 
492         auto &GOTBlock = StubBlock.edges().begin()->getTarget().getBlock();
493         assert(GOTBlock.getSize() == G.getPointerSize() &&
494                "GOT block should be pointer sized");
495         assert(GOTBlock.edges_size() == 1 &&
496                "GOT block should only have one outgoing edge");
497 
498         auto &GOTTarget = GOTBlock.edges().begin()->getTarget();
499         JITTargetAddress EdgeAddr = B->getAddress() + E.getOffset();
500         JITTargetAddress TargetAddr = GOTTarget.getAddress();
501 
502         int64_t Displacement = TargetAddr - EdgeAddr + 4;
503         if (Displacement >= std::numeric_limits<int32_t>::min() &&
504             Displacement <= std::numeric_limits<int32_t>::max()) {
505           E.setTarget(GOTTarget);
506           LLVM_DEBUG({
507             dbgs() << "  Replaced stub branch with direct branch:\n    ";
508             printEdge(dbgs(), *B, E,
509                       getMachOX86RelocationKindName(E.getKind()));
510             dbgs() << "\n";
511           });
512         }
513       }
514 
515   return Error::success();
516 }
517 
518 namespace llvm {
519 namespace jitlink {
520 
521 class MachOJITLinker_x86_64 : public JITLinker<MachOJITLinker_x86_64> {
522   friend class JITLinker<MachOJITLinker_x86_64>;
523 
524 public:
525   MachOJITLinker_x86_64(std::unique_ptr<JITLinkContext> Ctx,
526                         PassConfiguration PassConfig)
527       : JITLinker(std::move(Ctx), std::move(PassConfig)) {}
528 
529 private:
530   StringRef getEdgeKindName(Edge::Kind R) const override {
531     return getMachOX86RelocationKindName(R);
532   }
533 
534   Expected<std::unique_ptr<LinkGraph>>
535   buildGraph(MemoryBufferRef ObjBuffer) override {
536     auto MachOObj = object::ObjectFile::createMachOObjectFile(ObjBuffer);
537     if (!MachOObj)
538       return MachOObj.takeError();
539     return MachOLinkGraphBuilder_x86_64(**MachOObj).buildGraph();
540   }
541 
542   static Error targetOutOfRangeError(const Block &B, const Edge &E) {
543     std::string ErrMsg;
544     {
545       raw_string_ostream ErrStream(ErrMsg);
546       ErrStream << "Relocation target out of range: ";
547       printEdge(ErrStream, B, E, getMachOX86RelocationKindName(E.getKind()));
548       ErrStream << "\n";
549     }
550     return make_error<JITLinkError>(std::move(ErrMsg));
551   }
552 
553   Error applyFixup(Block &B, const Edge &E, char *BlockWorkingMem) const {
554 
555     using namespace support;
556 
557     char *FixupPtr = BlockWorkingMem + E.getOffset();
558     JITTargetAddress FixupAddress = B.getAddress() + E.getOffset();
559 
560     switch (E.getKind()) {
561     case Branch32:
562     case PCRel32:
563     case PCRel32Anon: {
564       int64_t Value =
565           E.getTarget().getAddress() - (FixupAddress + 4) + E.getAddend();
566       if (Value < std::numeric_limits<int32_t>::min() ||
567           Value > std::numeric_limits<int32_t>::max())
568         return targetOutOfRangeError(B, E);
569       *(little32_t *)FixupPtr = Value;
570       break;
571     }
572     case Pointer64:
573     case Pointer64Anon: {
574       uint64_t Value = E.getTarget().getAddress() + E.getAddend();
575       *(ulittle64_t *)FixupPtr = Value;
576       break;
577     }
578     case PCRel32Minus1:
579     case PCRel32Minus2:
580     case PCRel32Minus4: {
581       int Delta = 4 + (1 << (E.getKind() - PCRel32Minus1));
582       int64_t Value =
583           E.getTarget().getAddress() - (FixupAddress + Delta) + E.getAddend();
584       if (Value < std::numeric_limits<int32_t>::min() ||
585           Value > std::numeric_limits<int32_t>::max())
586         return targetOutOfRangeError(B, E);
587       *(little32_t *)FixupPtr = Value;
588       break;
589     }
590     case PCRel32Minus1Anon:
591     case PCRel32Minus2Anon:
592     case PCRel32Minus4Anon: {
593       int Delta = 4 + (1 << (E.getKind() - PCRel32Minus1Anon));
594       int64_t Value =
595           E.getTarget().getAddress() - (FixupAddress + Delta) + E.getAddend();
596       if (Value < std::numeric_limits<int32_t>::min() ||
597           Value > std::numeric_limits<int32_t>::max())
598         return targetOutOfRangeError(B, E);
599       *(little32_t *)FixupPtr = Value;
600       break;
601     }
602     case Delta32:
603     case Delta64:
604     case NegDelta32:
605     case NegDelta64: {
606       int64_t Value;
607       if (E.getKind() == Delta32 || E.getKind() == Delta64)
608         Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
609       else
610         Value = FixupAddress - E.getTarget().getAddress() + E.getAddend();
611 
612       if (E.getKind() == Delta32 || E.getKind() == NegDelta32) {
613         if (Value < std::numeric_limits<int32_t>::min() ||
614             Value > std::numeric_limits<int32_t>::max())
615           return targetOutOfRangeError(B, E);
616         *(little32_t *)FixupPtr = Value;
617       } else
618         *(little64_t *)FixupPtr = Value;
619       break;
620     }
621     case Pointer32: {
622       uint64_t Value = E.getTarget().getAddress() + E.getAddend();
623       if (Value > std::numeric_limits<uint32_t>::max())
624         return targetOutOfRangeError(B, E);
625       *(ulittle32_t *)FixupPtr = Value;
626       break;
627     }
628     default:
629       llvm_unreachable("Unrecognized edge kind");
630     }
631 
632     return Error::success();
633   }
634 
635   uint64_t NullValue = 0;
636 };
637 
638 void jitLink_MachO_x86_64(std::unique_ptr<JITLinkContext> Ctx) {
639   PassConfiguration Config;
640   Triple TT("x86_64-apple-macosx");
641 
642   if (Ctx->shouldAddDefaultTargetPasses(TT)) {
643     // Add eh-frame passses.
644     Config.PrePrunePasses.push_back(EHFrameSplitter("__eh_frame"));
645     Config.PrePrunePasses.push_back(
646         EHFrameEdgeFixer("__eh_frame", NegDelta32, Delta64, Delta64));
647 
648     // Add a mark-live pass.
649     if (auto MarkLive = Ctx->getMarkLivePass(TT))
650       Config.PrePrunePasses.push_back(std::move(MarkLive));
651     else
652       Config.PrePrunePasses.push_back(markAllSymbolsLive);
653 
654     // Add an in-place GOT/Stubs pass.
655     Config.PostPrunePasses.push_back([](LinkGraph &G) -> Error {
656       MachO_x86_64_GOTAndStubsBuilder(G).run();
657       return Error::success();
658     });
659 
660     // Add GOT/Stubs optimizer pass.
661     Config.PostAllocationPasses.push_back(optimizeMachO_x86_64_GOTAndStubs);
662   }
663 
664   if (auto Err = Ctx->modifyPassConfig(TT, Config))
665     return Ctx->notifyFailed(std::move(Err));
666 
667   // Construct a JITLinker and run the link function.
668   MachOJITLinker_x86_64::link(std::move(Ctx), std::move(Config));
669 }
670 
671 StringRef getMachOX86RelocationKindName(Edge::Kind R) {
672   switch (R) {
673   case Branch32:
674     return "Branch32";
675   case Branch32ToStub:
676     return "Branch32ToStub";
677   case Pointer32:
678     return "Pointer32";
679   case Pointer64:
680     return "Pointer64";
681   case Pointer64Anon:
682     return "Pointer64Anon";
683   case PCRel32:
684     return "PCRel32";
685   case PCRel32Minus1:
686     return "PCRel32Minus1";
687   case PCRel32Minus2:
688     return "PCRel32Minus2";
689   case PCRel32Minus4:
690     return "PCRel32Minus4";
691   case PCRel32Anon:
692     return "PCRel32Anon";
693   case PCRel32Minus1Anon:
694     return "PCRel32Minus1Anon";
695   case PCRel32Minus2Anon:
696     return "PCRel32Minus2Anon";
697   case PCRel32Minus4Anon:
698     return "PCRel32Minus4Anon";
699   case PCRel32GOTLoad:
700     return "PCRel32GOTLoad";
701   case PCRel32GOT:
702     return "PCRel32GOT";
703   case PCRel32TLV:
704     return "PCRel32TLV";
705   case Delta32:
706     return "Delta32";
707   case Delta64:
708     return "Delta64";
709   case NegDelta32:
710     return "NegDelta32";
711   case NegDelta64:
712     return "NegDelta64";
713   default:
714     return getGenericEdgeKindName(static_cast<Edge::Kind>(R));
715   }
716 }
717 
718 } // end namespace jitlink
719 } // end namespace llvm
720