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