MachO_arm64.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. #include "llvm/ExecutionEngine/JITLink/MachO_arm64.h"
  13. #include "llvm/ExecutionEngine/JITLink/DWARFRecordSectionSplitter.h"
  14. #include "llvm/ExecutionEngine/JITLink/aarch64.h"
  15. #include "MachOLinkGraphBuilder.h"
  16. #define DEBUG_TYPE "jitlink"
  17. using namespace llvm;
  18. using namespace llvm::jitlink;
  19. namespace {
  20. class MachOLinkGraphBuilder_arm64 : public MachOLinkGraphBuilder {
  21. public:
  22. MachOLinkGraphBuilder_arm64(const object::MachOObjectFile &Obj)
  23. : MachOLinkGraphBuilder(Obj, Triple("arm64-apple-darwin"),
  24. aarch64::getEdgeKindName),
  25. NumSymbols(Obj.getSymtabLoadCommand().nsyms) {}
  26. private:
  27. enum MachOARM64RelocationKind : Edge::Kind {
  28. MachOBranch26 = Edge::FirstRelocation,
  29. MachOPointer32,
  30. MachOPointer64,
  31. MachOPointer64Anon,
  32. MachOPage21,
  33. MachOPageOffset12,
  34. MachOGOTPage21,
  35. MachOGOTPageOffset12,
  36. MachOTLVPage21,
  37. MachOTLVPageOffset12,
  38. MachOPointerToGOT,
  39. MachOPairedAddend,
  40. MachOLDRLiteral19,
  41. MachODelta32,
  42. MachODelta64,
  43. MachONegDelta32,
  44. MachONegDelta64,
  45. };
  46. static Expected<MachOARM64RelocationKind>
  47. getRelocationKind(const MachO::relocation_info &RI) {
  48. switch (RI.r_type) {
  49. case MachO::ARM64_RELOC_UNSIGNED:
  50. if (!RI.r_pcrel) {
  51. if (RI.r_length == 3)
  52. return RI.r_extern ? MachOPointer64 : MachOPointer64Anon;
  53. else if (RI.r_length == 2)
  54. return MachOPointer32;
  55. }
  56. break;
  57. case MachO::ARM64_RELOC_SUBTRACTOR:
  58. // SUBTRACTOR must be non-pc-rel, extern, with length 2 or 3.
  59. // Initially represent SUBTRACTOR relocations with 'Delta<W>'.
  60. // They may be turned into NegDelta<W> by parsePairRelocation.
  61. if (!RI.r_pcrel && RI.r_extern) {
  62. if (RI.r_length == 2)
  63. return MachODelta32;
  64. else if (RI.r_length == 3)
  65. return MachODelta64;
  66. }
  67. break;
  68. case MachO::ARM64_RELOC_BRANCH26:
  69. if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
  70. return MachOBranch26;
  71. break;
  72. case MachO::ARM64_RELOC_PAGE21:
  73. if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
  74. return MachOPage21;
  75. break;
  76. case MachO::ARM64_RELOC_PAGEOFF12:
  77. if (!RI.r_pcrel && RI.r_extern && RI.r_length == 2)
  78. return MachOPageOffset12;
  79. break;
  80. case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
  81. if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
  82. return MachOGOTPage21;
  83. break;
  84. case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
  85. if (!RI.r_pcrel && RI.r_extern && RI.r_length == 2)
  86. return MachOGOTPageOffset12;
  87. break;
  88. case MachO::ARM64_RELOC_POINTER_TO_GOT:
  89. if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
  90. return MachOPointerToGOT;
  91. break;
  92. case MachO::ARM64_RELOC_ADDEND:
  93. if (!RI.r_pcrel && !RI.r_extern && RI.r_length == 2)
  94. return MachOPairedAddend;
  95. break;
  96. case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
  97. if (RI.r_pcrel && RI.r_extern && RI.r_length == 2)
  98. return MachOTLVPage21;
  99. break;
  100. case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
  101. if (!RI.r_pcrel && RI.r_extern && RI.r_length == 2)
  102. return MachOTLVPageOffset12;
  103. break;
  104. }
  105. return make_error<JITLinkError>(
  106. "Unsupported arm64 relocation: address=" +
  107. formatv("{0:x8}", RI.r_address) +
  108. ", symbolnum=" + formatv("{0:x6}", RI.r_symbolnum) +
  109. ", kind=" + formatv("{0:x1}", RI.r_type) +
  110. ", pc_rel=" + (RI.r_pcrel ? "true" : "false") +
  111. ", extern=" + (RI.r_extern ? "true" : "false") +
  112. ", length=" + formatv("{0:d}", RI.r_length));
  113. }
  114. using PairRelocInfo = std::tuple<Edge::Kind, Symbol *, uint64_t>;
  115. // Parses paired SUBTRACTOR/UNSIGNED relocations and, on success,
  116. // returns the edge kind and addend to be used.
  117. Expected<PairRelocInfo>
  118. parsePairRelocation(Block &BlockToFix, Edge::Kind SubtractorKind,
  119. const MachO::relocation_info &SubRI,
  120. orc::ExecutorAddr FixupAddress, const char *FixupContent,
  121. object::relocation_iterator &UnsignedRelItr,
  122. object::relocation_iterator &RelEnd) {
  123. using namespace support;
  124. assert(((SubtractorKind == MachODelta32 && SubRI.r_length == 2) ||
  125. (SubtractorKind == MachODelta64 && SubRI.r_length == 3)) &&
  126. "Subtractor kind should match length");
  127. assert(SubRI.r_extern && "SUBTRACTOR reloc symbol should be extern");
  128. assert(!SubRI.r_pcrel && "SUBTRACTOR reloc should not be PCRel");
  129. if (UnsignedRelItr == RelEnd)
  130. return make_error<JITLinkError>("arm64 SUBTRACTOR without paired "
  131. "UNSIGNED relocation");
  132. auto UnsignedRI = getRelocationInfo(UnsignedRelItr);
  133. if (SubRI.r_address != UnsignedRI.r_address)
  134. return make_error<JITLinkError>("arm64 SUBTRACTOR and paired UNSIGNED "
  135. "point to different addresses");
  136. if (SubRI.r_length != UnsignedRI.r_length)
  137. return make_error<JITLinkError>("length of arm64 SUBTRACTOR and paired "
  138. "UNSIGNED reloc must match");
  139. Symbol *FromSymbol;
  140. if (auto FromSymbolOrErr = findSymbolByIndex(SubRI.r_symbolnum))
  141. FromSymbol = FromSymbolOrErr->GraphSymbol;
  142. else
  143. return FromSymbolOrErr.takeError();
  144. // Read the current fixup value.
  145. uint64_t FixupValue = 0;
  146. if (SubRI.r_length == 3)
  147. FixupValue = *(const little64_t *)FixupContent;
  148. else
  149. FixupValue = *(const little32_t *)FixupContent;
  150. // Find 'ToSymbol' using symbol number or address, depending on whether the
  151. // paired UNSIGNED relocation is extern.
  152. Symbol *ToSymbol = nullptr;
  153. if (UnsignedRI.r_extern) {
  154. // Find target symbol by symbol index.
  155. if (auto ToSymbolOrErr = findSymbolByIndex(UnsignedRI.r_symbolnum))
  156. ToSymbol = ToSymbolOrErr->GraphSymbol;
  157. else
  158. return ToSymbolOrErr.takeError();
  159. } else {
  160. auto ToSymbolSec = findSectionByIndex(UnsignedRI.r_symbolnum - 1);
  161. if (!ToSymbolSec)
  162. return ToSymbolSec.takeError();
  163. ToSymbol = getSymbolByAddress(*ToSymbolSec, ToSymbolSec->Address);
  164. assert(ToSymbol && "No symbol for section");
  165. FixupValue -= ToSymbol->getAddress().getValue();
  166. }
  167. Edge::Kind DeltaKind;
  168. Symbol *TargetSymbol;
  169. uint64_t Addend;
  170. if (&BlockToFix == &FromSymbol->getAddressable()) {
  171. TargetSymbol = ToSymbol;
  172. DeltaKind = (SubRI.r_length == 3) ? aarch64::Delta64 : aarch64::Delta32;
  173. Addend = FixupValue + (FixupAddress - FromSymbol->getAddress());
  174. // FIXME: handle extern 'from'.
  175. } else if (&BlockToFix == &ToSymbol->getAddressable()) {
  176. TargetSymbol = &*FromSymbol;
  177. DeltaKind =
  178. (SubRI.r_length == 3) ? aarch64::NegDelta64 : aarch64::NegDelta32;
  179. Addend = FixupValue - (FixupAddress - ToSymbol->getAddress());
  180. } else {
  181. // BlockToFix was neither FromSymbol nor ToSymbol.
  182. return make_error<JITLinkError>("SUBTRACTOR relocation must fix up "
  183. "either 'A' or 'B' (or a symbol in one "
  184. "of their alt-entry groups)");
  185. }
  186. return PairRelocInfo(DeltaKind, TargetSymbol, Addend);
  187. }
  188. Error addRelocations() override {
  189. using namespace support;
  190. auto &Obj = getObject();
  191. LLVM_DEBUG(dbgs() << "Processing relocations:\n");
  192. for (auto &S : Obj.sections()) {
  193. orc::ExecutorAddr SectionAddress(S.getAddress());
  194. // Skip relocations virtual sections.
  195. if (S.isVirtual()) {
  196. if (S.relocation_begin() != S.relocation_end())
  197. return make_error<JITLinkError>("Virtual section contains "
  198. "relocations");
  199. continue;
  200. }
  201. auto NSec =
  202. findSectionByIndex(Obj.getSectionIndex(S.getRawDataRefImpl()));
  203. if (!NSec)
  204. return NSec.takeError();
  205. // Skip relocations for MachO sections without corresponding graph
  206. // sections.
  207. {
  208. if (!NSec->GraphSection) {
  209. LLVM_DEBUG({
  210. dbgs() << " Skipping relocations for MachO section "
  211. << NSec->SegName << "/" << NSec->SectName
  212. << " which has no associated graph section\n";
  213. });
  214. continue;
  215. }
  216. }
  217. for (auto RelItr = S.relocation_begin(), RelEnd = S.relocation_end();
  218. RelItr != RelEnd; ++RelItr) {
  219. MachO::relocation_info RI = getRelocationInfo(RelItr);
  220. // Validate the relocation kind.
  221. auto MachORelocKind = getRelocationKind(RI);
  222. if (!MachORelocKind)
  223. return MachORelocKind.takeError();
  224. // Find the address of the value to fix up.
  225. orc::ExecutorAddr FixupAddress =
  226. SectionAddress + (uint32_t)RI.r_address;
  227. LLVM_DEBUG({
  228. dbgs() << " " << NSec->SectName << " + "
  229. << formatv("{0:x8}", RI.r_address) << ":\n";
  230. });
  231. // Find the block that the fixup points to.
  232. Block *BlockToFix = nullptr;
  233. {
  234. auto SymbolToFixOrErr = findSymbolByAddress(*NSec, FixupAddress);
  235. if (!SymbolToFixOrErr)
  236. return SymbolToFixOrErr.takeError();
  237. BlockToFix = &SymbolToFixOrErr->getBlock();
  238. }
  239. if (FixupAddress + orc::ExecutorAddrDiff(1ULL << RI.r_length) >
  240. BlockToFix->getAddress() + BlockToFix->getContent().size())
  241. return make_error<JITLinkError>(
  242. "Relocation content extends past end of fixup block");
  243. Edge::Kind Kind = Edge::Invalid;
  244. // Get a pointer to the fixup content.
  245. const char *FixupContent = BlockToFix->getContent().data() +
  246. (FixupAddress - BlockToFix->getAddress());
  247. // The target symbol and addend will be populated by the switch below.
  248. Symbol *TargetSymbol = nullptr;
  249. uint64_t Addend = 0;
  250. if (*MachORelocKind == MachOPairedAddend) {
  251. // If this is an Addend relocation then process it and move to the
  252. // paired reloc.
  253. Addend = SignExtend64(RI.r_symbolnum, 24);
  254. if (RelItr == RelEnd)
  255. return make_error<JITLinkError>("Unpaired Addend reloc at " +
  256. formatv("{0:x16}", FixupAddress));
  257. ++RelItr;
  258. RI = getRelocationInfo(RelItr);
  259. MachORelocKind = getRelocationKind(RI);
  260. if (!MachORelocKind)
  261. return MachORelocKind.takeError();
  262. if (*MachORelocKind != MachOBranch26 &&
  263. *MachORelocKind != MachOPage21 &&
  264. *MachORelocKind != MachOPageOffset12)
  265. return make_error<JITLinkError>(
  266. "Invalid relocation pair: Addend + " +
  267. StringRef(getMachOARM64RelocationKindName(*MachORelocKind)));
  268. LLVM_DEBUG({
  269. dbgs() << " Addend: value = " << formatv("{0:x6}", Addend)
  270. << ", pair is "
  271. << getMachOARM64RelocationKindName(*MachORelocKind) << "\n";
  272. });
  273. // Find the address of the value to fix up.
  274. orc::ExecutorAddr PairedFixupAddress =
  275. SectionAddress + (uint32_t)RI.r_address;
  276. if (PairedFixupAddress != FixupAddress)
  277. return make_error<JITLinkError>("Paired relocation points at "
  278. "different target");
  279. }
  280. switch (*MachORelocKind) {
  281. case MachOBranch26: {
  282. if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
  283. TargetSymbol = TargetSymbolOrErr->GraphSymbol;
  284. else
  285. return TargetSymbolOrErr.takeError();
  286. uint32_t Instr = *(const ulittle32_t *)FixupContent;
  287. if ((Instr & 0x7fffffff) != 0x14000000)
  288. return make_error<JITLinkError>("BRANCH26 target is not a B or BL "
  289. "instruction with a zero addend");
  290. Kind = aarch64::Branch26PCRel;
  291. break;
  292. }
  293. case MachOPointer32:
  294. if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
  295. TargetSymbol = TargetSymbolOrErr->GraphSymbol;
  296. else
  297. return TargetSymbolOrErr.takeError();
  298. Addend = *(const ulittle32_t *)FixupContent;
  299. Kind = aarch64::Pointer32;
  300. break;
  301. case MachOPointer64:
  302. if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
  303. TargetSymbol = TargetSymbolOrErr->GraphSymbol;
  304. else
  305. return TargetSymbolOrErr.takeError();
  306. Addend = *(const ulittle64_t *)FixupContent;
  307. Kind = aarch64::Pointer64;
  308. break;
  309. case MachOPointer64Anon: {
  310. orc::ExecutorAddr TargetAddress(*(const ulittle64_t *)FixupContent);
  311. auto TargetNSec = findSectionByIndex(RI.r_symbolnum - 1);
  312. if (!TargetNSec)
  313. return TargetNSec.takeError();
  314. if (auto TargetSymbolOrErr =
  315. findSymbolByAddress(*TargetNSec, TargetAddress))
  316. TargetSymbol = &*TargetSymbolOrErr;
  317. else
  318. return TargetSymbolOrErr.takeError();
  319. Addend = TargetAddress - TargetSymbol->getAddress();
  320. Kind = aarch64::Pointer64;
  321. break;
  322. }
  323. case MachOPage21:
  324. case MachOGOTPage21:
  325. case MachOTLVPage21: {
  326. if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
  327. TargetSymbol = TargetSymbolOrErr->GraphSymbol;
  328. else
  329. return TargetSymbolOrErr.takeError();
  330. uint32_t Instr = *(const ulittle32_t *)FixupContent;
  331. if ((Instr & 0xffffffe0) != 0x90000000)
  332. return make_error<JITLinkError>("PAGE21/GOTPAGE21 target is not an "
  333. "ADRP instruction with a zero "
  334. "addend");
  335. if (*MachORelocKind == MachOPage21) {
  336. Kind = aarch64::Page21;
  337. } else if (*MachORelocKind == MachOGOTPage21) {
  338. Kind = aarch64::RequestGOTAndTransformToPage21;
  339. } else if (*MachORelocKind == MachOTLVPage21) {
  340. Kind = aarch64::RequestTLVPAndTransformToPage21;
  341. }
  342. break;
  343. }
  344. case MachOPageOffset12: {
  345. if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
  346. TargetSymbol = TargetSymbolOrErr->GraphSymbol;
  347. else
  348. return TargetSymbolOrErr.takeError();
  349. uint32_t Instr = *(const ulittle32_t *)FixupContent;
  350. uint32_t EncodedAddend = (Instr & 0x003FFC00) >> 10;
  351. if (EncodedAddend != 0)
  352. return make_error<JITLinkError>("GOTPAGEOFF12 target has non-zero "
  353. "encoded addend");
  354. Kind = aarch64::PageOffset12;
  355. break;
  356. }
  357. case MachOGOTPageOffset12:
  358. case MachOTLVPageOffset12: {
  359. if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
  360. TargetSymbol = TargetSymbolOrErr->GraphSymbol;
  361. else
  362. return TargetSymbolOrErr.takeError();
  363. uint32_t Instr = *(const ulittle32_t *)FixupContent;
  364. if ((Instr & 0xfffffc00) != 0xf9400000)
  365. return make_error<JITLinkError>("GOTPAGEOFF12 target is not an LDR "
  366. "immediate instruction with a zero "
  367. "addend");
  368. if (*MachORelocKind == MachOGOTPageOffset12) {
  369. Kind = aarch64::RequestGOTAndTransformToPageOffset12;
  370. } else if (*MachORelocKind == MachOTLVPageOffset12) {
  371. Kind = aarch64::RequestTLVPAndTransformToPageOffset12;
  372. }
  373. break;
  374. }
  375. case MachOPointerToGOT:
  376. if (auto TargetSymbolOrErr = findSymbolByIndex(RI.r_symbolnum))
  377. TargetSymbol = TargetSymbolOrErr->GraphSymbol;
  378. else
  379. return TargetSymbolOrErr.takeError();
  380. Kind = aarch64::RequestGOTAndTransformToDelta32;
  381. break;
  382. case MachODelta32:
  383. case MachODelta64: {
  384. // We use Delta32/Delta64 to represent SUBTRACTOR relocations.
  385. // parsePairRelocation handles the paired reloc, and returns the
  386. // edge kind to be used (either Delta32/Delta64, or
  387. // NegDelta32/NegDelta64, depending on the direction of the
  388. // subtraction) along with the addend.
  389. auto PairInfo =
  390. parsePairRelocation(*BlockToFix, *MachORelocKind, RI,
  391. FixupAddress, FixupContent, ++RelItr, RelEnd);
  392. if (!PairInfo)
  393. return PairInfo.takeError();
  394. std::tie(Kind, TargetSymbol, Addend) = *PairInfo;
  395. assert(TargetSymbol && "No target symbol from parsePairRelocation?");
  396. break;
  397. }
  398. default:
  399. llvm_unreachable("Special relocation kind should not appear in "
  400. "mach-o file");
  401. }
  402. LLVM_DEBUG({
  403. dbgs() << " ";
  404. Edge GE(Kind, FixupAddress - BlockToFix->getAddress(), *TargetSymbol,
  405. Addend);
  406. printEdge(dbgs(), *BlockToFix, GE, aarch64::getEdgeKindName(Kind));
  407. dbgs() << "\n";
  408. });
  409. BlockToFix->addEdge(Kind, FixupAddress - BlockToFix->getAddress(),
  410. *TargetSymbol, Addend);
  411. }
  412. }
  413. return Error::success();
  414. }
  415. /// Return the string name of the given MachO arm64 edge kind.
  416. const char *getMachOARM64RelocationKindName(Edge::Kind R) {
  417. switch (R) {
  418. case MachOBranch26:
  419. return "MachOBranch26";
  420. case MachOPointer64:
  421. return "MachOPointer64";
  422. case MachOPointer64Anon:
  423. return "MachOPointer64Anon";
  424. case MachOPage21:
  425. return "MachOPage21";
  426. case MachOPageOffset12:
  427. return "MachOPageOffset12";
  428. case MachOGOTPage21:
  429. return "MachOGOTPage21";
  430. case MachOGOTPageOffset12:
  431. return "MachOGOTPageOffset12";
  432. case MachOTLVPage21:
  433. return "MachOTLVPage21";
  434. case MachOTLVPageOffset12:
  435. return "MachOTLVPageOffset12";
  436. case MachOPointerToGOT:
  437. return "MachOPointerToGOT";
  438. case MachOPairedAddend:
  439. return "MachOPairedAddend";
  440. case MachOLDRLiteral19:
  441. return "MachOLDRLiteral19";
  442. case MachODelta32:
  443. return "MachODelta32";
  444. case MachODelta64:
  445. return "MachODelta64";
  446. case MachONegDelta32:
  447. return "MachONegDelta32";
  448. case MachONegDelta64:
  449. return "MachONegDelta64";
  450. default:
  451. return getGenericEdgeKindName(static_cast<Edge::Kind>(R));
  452. }
  453. }
  454. unsigned NumSymbols = 0;
  455. };
  456. } // namespace
  457. namespace llvm {
  458. namespace jitlink {
  459. Error buildTables_MachO_arm64(LinkGraph &G) {
  460. LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
  461. aarch64::GOTTableManager GOT;
  462. aarch64::PLTTableManager PLT(GOT);
  463. visitExistingEdges(G, GOT, PLT);
  464. return Error::success();
  465. }
  466. class MachOJITLinker_arm64 : public JITLinker<MachOJITLinker_arm64> {
  467. friend class JITLinker<MachOJITLinker_arm64>;
  468. public:
  469. MachOJITLinker_arm64(std::unique_ptr<JITLinkContext> Ctx,
  470. std::unique_ptr<LinkGraph> G,
  471. PassConfiguration PassConfig)
  472. : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {}
  473. private:
  474. Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
  475. return aarch64::applyFixup(G, B, E);
  476. }
  477. uint64_t NullValue = 0;
  478. };
  479. Expected<std::unique_ptr<LinkGraph>>
  480. createLinkGraphFromMachOObject_arm64(MemoryBufferRef ObjectBuffer) {
  481. auto MachOObj = object::ObjectFile::createMachOObjectFile(ObjectBuffer);
  482. if (!MachOObj)
  483. return MachOObj.takeError();
  484. return MachOLinkGraphBuilder_arm64(**MachOObj).buildGraph();
  485. }
  486. void link_MachO_arm64(std::unique_ptr<LinkGraph> G,
  487. std::unique_ptr<JITLinkContext> Ctx) {
  488. PassConfiguration Config;
  489. if (Ctx->shouldAddDefaultTargetPasses(G->getTargetTriple())) {
  490. // Add a mark-live pass.
  491. if (auto MarkLive = Ctx->getMarkLivePass(G->getTargetTriple()))
  492. Config.PrePrunePasses.push_back(std::move(MarkLive));
  493. else
  494. Config.PrePrunePasses.push_back(markAllSymbolsLive);
  495. // Add compact unwind splitter pass.
  496. Config.PrePrunePasses.push_back(
  497. CompactUnwindSplitter("__LD,__compact_unwind"));
  498. // Add eh-frame passses.
  499. // FIXME: Prune eh-frames for which compact-unwind is available once
  500. // we support compact-unwind registration with libunwind.
  501. Config.PrePrunePasses.push_back(createEHFrameSplitterPass_MachO_arm64());
  502. Config.PrePrunePasses.push_back(createEHFrameEdgeFixerPass_MachO_arm64());
  503. // Add an in-place GOT/Stubs pass.
  504. Config.PostPrunePasses.push_back(buildTables_MachO_arm64);
  505. }
  506. if (auto Err = Ctx->modifyPassConfig(*G, Config))
  507. return Ctx->notifyFailed(std::move(Err));
  508. // Construct a JITLinker and run the link function.
  509. MachOJITLinker_arm64::link(std::move(Ctx), std::move(G), std::move(Config));
  510. }
  511. LinkGraphPassFunction createEHFrameSplitterPass_MachO_arm64() {
  512. return DWARFRecordSectionSplitter("__TEXT,__eh_frame");
  513. }
  514. LinkGraphPassFunction createEHFrameEdgeFixerPass_MachO_arm64() {
  515. return EHFrameEdgeFixer("__TEXT,__eh_frame", aarch64::PointerSize,
  516. aarch64::Pointer32, aarch64::Pointer64,
  517. aarch64::Delta32, aarch64::Delta64,
  518. aarch64::NegDelta32);
  519. }
  520. } // end namespace jitlink
  521. } // end namespace llvm