aarch64.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //=== aarch64.h - Generic JITLink aarch64 edge kinds, utilities -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Generic utilities for graphs representing aarch64 objects.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_JITLINK_AARCH64_H
  18. #define LLVM_EXECUTIONENGINE_JITLINK_AARCH64_H
  19. #include "TableManager.h"
  20. #include "llvm/ExecutionEngine/JITLink/JITLink.h"
  21. #include "llvm/ExecutionEngine/Orc/Shared/MemoryFlags.h"
  22. namespace llvm {
  23. namespace jitlink {
  24. namespace aarch64 {
  25. /// Represents aarch64 fixups and other aarch64-specific edge kinds.
  26. enum EdgeKind_aarch64 : Edge::Kind {
  27. /// A plain 64-bit pointer value relocation.
  28. ///
  29. /// Fixup expression:
  30. /// Fixup <- Target + Addend : uint64
  31. ///
  32. Pointer64 = Edge::FirstRelocation,
  33. /// A plain 32-bit pointer value relocation.
  34. ///
  35. /// Fixup expression:
  36. /// Fixup <- Target + Addend : uint32
  37. ///
  38. /// Errors:
  39. /// - The target must reside in the low 32-bits of the address space,
  40. /// otherwise an out-of-range error will be returned.
  41. ///
  42. Pointer32,
  43. /// A 64-bit delta.
  44. ///
  45. /// Delta from the fixup to the target.
  46. ///
  47. /// Fixup expression:
  48. /// Fixup <- Target - Fixup + Addend : int64
  49. ///
  50. Delta64,
  51. /// A 32-bit delta.
  52. ///
  53. /// Delta from the fixup to the target.
  54. ///
  55. /// Fixup expression:
  56. /// Fixup <- Target - Fixup + Addend : int64
  57. ///
  58. /// Errors:
  59. /// - The result of the fixup expression must fit into an int32, otherwise
  60. /// an out-of-range error will be returned.
  61. ///
  62. Delta32,
  63. /// A 64-bit negative delta.
  64. ///
  65. /// Delta from target back to the fixup.
  66. ///
  67. /// Fixup expression:
  68. /// Fixup <- Fixup - Target + Addend : int64
  69. ///
  70. NegDelta64,
  71. /// A 32-bit negative delta.
  72. ///
  73. /// Delta from the target back to the fixup.
  74. ///
  75. /// Fixup expression:
  76. /// Fixup <- Fixup - Target + Addend : int32
  77. ///
  78. /// Errors:
  79. /// - The result of the fixup expression must fit into an int32, otherwise
  80. /// an out-of-range error will be returned.
  81. NegDelta32,
  82. /// A 26-bit PC-relative branch.
  83. ///
  84. /// Represents a PC-relative call or branch to a target within +/-128Mb. The
  85. /// target must be 32-bit aligned.
  86. ///
  87. /// Fixup expression:
  88. /// Fixup <- (Target - Fixup + Addend) >> 2 : int26
  89. ///
  90. /// Notes:
  91. /// The '26' in the name refers to the number operand bits and follows the
  92. /// naming convention used by the corresponding ELF and MachO relocations.
  93. /// Since the low two bits must be zero (because of the 32-bit alignment of
  94. /// the target) the operand is effectively a signed 28-bit number.
  95. ///
  96. ///
  97. /// Errors:
  98. /// - The result of the unshifted part of the fixup expression must be
  99. /// 32-bit aligned otherwise an alignment error will be returned.
  100. /// - The result of the fixup expression must fit into an int26 otherwise an
  101. /// out-of-range error will be returned.
  102. Branch26PCRel,
  103. /// A 16-bit slice of the target address (which slice depends on the
  104. /// instruction at the fixup location).
  105. ///
  106. /// Used to fix up MOVK/MOVN/MOVZ instructions.
  107. ///
  108. /// Fixup expression:
  109. ///
  110. /// Fixup <- (Target + Addend) >> Shift : uint16
  111. ///
  112. /// where Shift is encoded in the instruction at the fixup location.
  113. ///
  114. MoveWide16,
  115. /// The signed 21-bit delta from the fixup to the target.
  116. ///
  117. /// Typically used to load a pointers at a PC-relative offset of +/- 1Mb. The
  118. /// target must be 32-bit aligned.
  119. ///
  120. /// Fixup expression:
  121. ///
  122. /// Fixup <- (Target - Fixup) >> 2 : int19
  123. ///
  124. /// Errors:
  125. /// - The result of the unshifted part of the fixup expression must be
  126. /// 32-bit aligned otherwise an alignment error will be returned.
  127. /// - The result of the fixup expression must fit into an an int19 or an
  128. /// out-of-range error will be returned.
  129. LDRLiteral19,
  130. /// The signed 21-bit delta from the fixup page to the page containing the
  131. /// target.
  132. ///
  133. /// Fixup expression:
  134. ///
  135. /// Fixup <- (((Target + Addend) & ~0xfff) - (Fixup & ~0xfff)) >> 12 : int21
  136. ///
  137. /// Notes:
  138. /// For ADRP fixups.
  139. ///
  140. /// Errors:
  141. /// - The result of the fixup expression must fit into an int21 otherwise an
  142. /// out-of-range error will be returned.
  143. Page21,
  144. /// The 12-bit (potentially shifted) offset of the target within its page.
  145. ///
  146. /// Typically used to fix up LDR immediates.
  147. ///
  148. /// Fixup expression:
  149. ///
  150. /// Fixup <- ((Target + Addend) >> Shift) & 0xfff : uint12
  151. ///
  152. /// where Shift is encoded in the size field of the instruction.
  153. ///
  154. /// Errors:
  155. /// - The result of the unshifted part of the fixup expression must be
  156. /// aligned otherwise an alignment error will be returned.
  157. /// - The result of the fixup expression must fit into a uint12 otherwise an
  158. /// out-of-range error will be returned.
  159. PageOffset12,
  160. /// A GOT entry getter/constructor, transformed to Page21 pointing at the GOT
  161. /// entry for the original target.
  162. ///
  163. /// Indicates that this edge should be transformed into a Page21 targeting
  164. /// the GOT entry for the edge's current target, maintaining the same addend.
  165. /// A GOT entry for the target should be created if one does not already
  166. /// exist.
  167. ///
  168. /// Edges of this kind are usually handled by a GOT builder pass inserted by
  169. /// default.
  170. ///
  171. /// Fixup expression:
  172. /// NONE
  173. ///
  174. /// Errors:
  175. /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
  176. /// phase will result in an assert/unreachable during the fixup phase.
  177. ///
  178. RequestGOTAndTransformToPage21,
  179. /// A GOT entry getter/constructor, transformed to Pageoffset12 pointing at
  180. /// the GOT entry for the original target.
  181. ///
  182. /// Indicates that this edge should be transformed into a PageOffset12
  183. /// targeting the GOT entry for the edge's current target, maintaining the
  184. /// same addend. A GOT entry for the target should be created if one does not
  185. /// already exist.
  186. ///
  187. /// Edges of this kind are usually handled by a GOT builder pass inserted by
  188. /// default.
  189. ///
  190. /// Fixup expression:
  191. /// NONE
  192. ///
  193. /// Errors:
  194. /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
  195. /// phase will result in an assert/unreachable during the fixup phase.
  196. ///
  197. RequestGOTAndTransformToPageOffset12,
  198. /// A GOT entry getter/constructor, transformed to Delta32 pointing at the GOT
  199. /// entry for the original target.
  200. ///
  201. /// Indicates that this edge should be transformed into a Delta32/ targeting
  202. /// the GOT entry for the edge's current target, maintaining the same addend.
  203. /// A GOT entry for the target should be created if one does not already
  204. /// exist.
  205. ///
  206. /// Edges of this kind are usually handled by a GOT builder pass inserted by
  207. /// default.
  208. ///
  209. /// Fixup expression:
  210. /// NONE
  211. ///
  212. /// Errors:
  213. /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
  214. /// phase will result in an assert/unreachable during the fixup phase.
  215. ///
  216. RequestGOTAndTransformToDelta32,
  217. /// A TLVP entry getter/constructor, transformed to Page21.
  218. ///
  219. /// Indicates that this edge should be transformed into a Page21 targeting the
  220. /// TLVP entry for the edge's current target. A TLVP entry for the target
  221. /// should be created if one does not already exist.
  222. ///
  223. /// Fixup expression:
  224. /// NONE
  225. ///
  226. /// Errors:
  227. /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
  228. /// phase will result in an assert/unreachable during the fixup phase.
  229. ///
  230. RequestTLVPAndTransformToPage21,
  231. /// A TLVP entry getter/constructor, transformed to PageOffset12.
  232. ///
  233. /// Indicates that this edge should be transformed into a PageOffset12
  234. /// targeting the TLVP entry for the edge's current target. A TLVP entry for
  235. /// the target should be created if one does not already exist.
  236. ///
  237. /// Fixup expression:
  238. /// NONE
  239. ///
  240. /// Errors:
  241. /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
  242. /// phase will result in an assert/unreachable during the fixup phase.
  243. ///
  244. RequestTLVPAndTransformToPageOffset12,
  245. /// A TLSDesc entry getter/constructor, transformed to Page21.
  246. ///
  247. /// Indicates that this edge should be transformed into a Page21 targeting the
  248. /// TLSDesc entry for the edge's current target. A TLSDesc entry for the
  249. /// target should be created if one does not already exist.
  250. ///
  251. /// Fixup expression:
  252. /// NONE
  253. ///
  254. /// Errors:
  255. /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
  256. /// phase will result in an assert/unreachable during the fixup phase.
  257. ///
  258. RequestTLSDescEntryAndTransformToPage21,
  259. /// A TLSDesc entry getter/constructor, transformed to PageOffset12.
  260. ///
  261. /// Indicates that this edge should be transformed into a PageOffset12
  262. /// targeting the TLSDesc entry for the edge's current target. A TLSDesc entry
  263. /// for the target should be created if one does not already exist.
  264. ///
  265. /// Fixup expression:
  266. /// NONE
  267. ///
  268. /// Errors:
  269. /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
  270. /// phase will result in an assert/unreachable during the fixup phase.
  271. ///
  272. RequestTLSDescEntryAndTransformToPageOffset12,
  273. };
  274. /// Returns a string name for the given aarch64 edge. For debugging purposes
  275. /// only
  276. const char *getEdgeKindName(Edge::Kind K);
  277. // Returns whether the Instr is LD/ST (imm12)
  278. inline bool isLoadStoreImm12(uint32_t Instr) {
  279. constexpr uint32_t LoadStoreImm12Mask = 0x3b000000;
  280. return (Instr & LoadStoreImm12Mask) == 0x39000000;
  281. }
  282. // Returns the amount the address operand of LD/ST (imm12)
  283. // should be shifted right by.
  284. //
  285. // The shift value varies by the data size of LD/ST instruction.
  286. // For instance, LDH instructoin needs the address to be shifted
  287. // right by 1.
  288. inline unsigned getPageOffset12Shift(uint32_t Instr) {
  289. constexpr uint32_t Vec128Mask = 0x04800000;
  290. if (isLoadStoreImm12(Instr)) {
  291. uint32_t ImplicitShift = Instr >> 30;
  292. if (ImplicitShift == 0)
  293. if ((Instr & Vec128Mask) == Vec128Mask)
  294. ImplicitShift = 4;
  295. return ImplicitShift;
  296. }
  297. return 0;
  298. }
  299. // Returns whether the Instr is MOVK/MOVZ (imm16) with a zero immediate field
  300. inline bool isMoveWideImm16(uint32_t Instr) {
  301. constexpr uint32_t MoveWideImm16Mask = 0x5f9fffe0;
  302. return (Instr & MoveWideImm16Mask) == 0x52800000;
  303. }
  304. // Returns the amount the address operand of MOVK/MOVZ (imm16)
  305. // should be shifted right by.
  306. //
  307. // The shift value is specfied in the assembly as LSL #<shift>.
  308. inline unsigned getMoveWide16Shift(uint32_t Instr) {
  309. if (isMoveWideImm16(Instr)) {
  310. uint32_t ImplicitShift = (Instr >> 21) & 0b11;
  311. return ImplicitShift << 4;
  312. }
  313. return 0;
  314. }
  315. /// Apply fixup expression for edge to block content.
  316. inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E) {
  317. using namespace support;
  318. char *BlockWorkingMem = B.getAlreadyMutableContent().data();
  319. char *FixupPtr = BlockWorkingMem + E.getOffset();
  320. orc::ExecutorAddr FixupAddress = B.getAddress() + E.getOffset();
  321. switch (E.getKind()) {
  322. case Pointer64: {
  323. uint64_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
  324. *(ulittle64_t *)FixupPtr = Value;
  325. break;
  326. }
  327. case Pointer32: {
  328. uint64_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
  329. if (Value > std::numeric_limits<uint32_t>::max())
  330. return makeTargetOutOfRangeError(G, B, E);
  331. *(ulittle32_t *)FixupPtr = Value;
  332. break;
  333. }
  334. case Delta32:
  335. case Delta64:
  336. case NegDelta32:
  337. case NegDelta64: {
  338. int64_t Value;
  339. if (E.getKind() == Delta32 || E.getKind() == Delta64)
  340. Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
  341. else
  342. Value = FixupAddress - E.getTarget().getAddress() + E.getAddend();
  343. if (E.getKind() == Delta32 || E.getKind() == NegDelta32) {
  344. if (Value < std::numeric_limits<int32_t>::min() ||
  345. Value > std::numeric_limits<int32_t>::max())
  346. return makeTargetOutOfRangeError(G, B, E);
  347. *(little32_t *)FixupPtr = Value;
  348. } else
  349. *(little64_t *)FixupPtr = Value;
  350. break;
  351. }
  352. case Branch26PCRel: {
  353. assert((FixupAddress.getValue() & 0x3) == 0 &&
  354. "Branch-inst is not 32-bit aligned");
  355. int64_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
  356. if (static_cast<uint64_t>(Value) & 0x3)
  357. return make_error<JITLinkError>("BranchPCRel26 target is not 32-bit "
  358. "aligned");
  359. if (Value < -(1 << 27) || Value > ((1 << 27) - 1))
  360. return makeTargetOutOfRangeError(G, B, E);
  361. uint32_t RawInstr = *(little32_t *)FixupPtr;
  362. assert((RawInstr & 0x7fffffff) == 0x14000000 &&
  363. "RawInstr isn't a B or BR immediate instruction");
  364. uint32_t Imm = (static_cast<uint32_t>(Value) & ((1 << 28) - 1)) >> 2;
  365. uint32_t FixedInstr = RawInstr | Imm;
  366. *(little32_t *)FixupPtr = FixedInstr;
  367. break;
  368. }
  369. case MoveWide16: {
  370. uint64_t TargetOffset =
  371. (E.getTarget().getAddress() + E.getAddend()).getValue();
  372. uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
  373. assert(isMoveWideImm16(RawInstr) &&
  374. "RawInstr isn't a MOVK/MOVZ instruction");
  375. unsigned ImmShift = getMoveWide16Shift(RawInstr);
  376. uint32_t Imm = (TargetOffset >> ImmShift) & 0xffff;
  377. uint32_t FixedInstr = RawInstr | (Imm << 5);
  378. *(ulittle32_t *)FixupPtr = FixedInstr;
  379. break;
  380. }
  381. case LDRLiteral19: {
  382. assert((FixupAddress.getValue() & 0x3) == 0 && "LDR is not 32-bit aligned");
  383. assert(E.getAddend() == 0 && "LDRLiteral19 with non-zero addend");
  384. uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
  385. assert(RawInstr == 0x58000010 && "RawInstr isn't a 64-bit LDR literal");
  386. int64_t Delta = E.getTarget().getAddress() - FixupAddress;
  387. if (Delta & 0x3)
  388. return make_error<JITLinkError>("LDR literal target is not 32-bit "
  389. "aligned");
  390. if (Delta < -(1 << 20) || Delta > ((1 << 20) - 1))
  391. return makeTargetOutOfRangeError(G, B, E);
  392. uint32_t EncodedImm = ((static_cast<uint32_t>(Delta) >> 2) & 0x7ffff) << 5;
  393. uint32_t FixedInstr = RawInstr | EncodedImm;
  394. *(ulittle32_t *)FixupPtr = FixedInstr;
  395. break;
  396. }
  397. case Page21: {
  398. uint64_t TargetPage =
  399. (E.getTarget().getAddress().getValue() + E.getAddend()) &
  400. ~static_cast<uint64_t>(4096 - 1);
  401. uint64_t PCPage =
  402. FixupAddress.getValue() & ~static_cast<uint64_t>(4096 - 1);
  403. int64_t PageDelta = TargetPage - PCPage;
  404. if (!isInt<33>(PageDelta))
  405. return makeTargetOutOfRangeError(G, B, E);
  406. uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
  407. assert((RawInstr & 0xffffffe0) == 0x90000000 &&
  408. "RawInstr isn't an ADRP instruction");
  409. uint32_t ImmLo = (static_cast<uint64_t>(PageDelta) >> 12) & 0x3;
  410. uint32_t ImmHi = (static_cast<uint64_t>(PageDelta) >> 14) & 0x7ffff;
  411. uint32_t FixedInstr = RawInstr | (ImmLo << 29) | (ImmHi << 5);
  412. *(ulittle32_t *)FixupPtr = FixedInstr;
  413. break;
  414. }
  415. case PageOffset12: {
  416. uint64_t TargetOffset =
  417. (E.getTarget().getAddress() + E.getAddend()).getValue() & 0xfff;
  418. uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
  419. unsigned ImmShift = getPageOffset12Shift(RawInstr);
  420. if (TargetOffset & ((1 << ImmShift) - 1))
  421. return make_error<JITLinkError>("PAGEOFF12 target is not aligned");
  422. uint32_t EncodedImm = (TargetOffset >> ImmShift) << 10;
  423. uint32_t FixedInstr = RawInstr | EncodedImm;
  424. *(ulittle32_t *)FixupPtr = FixedInstr;
  425. break;
  426. }
  427. default:
  428. return make_error<JITLinkError>(
  429. "In graph " + G.getName() + ", section " + B.getSection().getName() +
  430. " unsupported edge kind " + getEdgeKindName(E.getKind()));
  431. }
  432. return Error::success();
  433. }
  434. /// aarch64 pointer size.
  435. constexpr uint64_t PointerSize = 8;
  436. /// AArch64 null pointer content.
  437. extern const char NullPointerContent[PointerSize];
  438. /// AArch64 pointer jump stub content.
  439. ///
  440. /// Contains the instruction sequence for an indirect jump via an in-memory
  441. /// pointer:
  442. /// ADRP x16, ptr@page21
  443. /// LDR x16, [x16, ptr@pageoff12]
  444. /// BR x16
  445. extern const char PointerJumpStubContent[12];
  446. /// Creates a new pointer block in the given section and returns an
  447. /// Anonymous symobl pointing to it.
  448. ///
  449. /// If InitialTarget is given then an Pointer64 relocation will be added to the
  450. /// block pointing at InitialTarget.
  451. ///
  452. /// The pointer block will have the following default values:
  453. /// alignment: 64-bit
  454. /// alignment-offset: 0
  455. /// address: highest allowable (~7U)
  456. inline Symbol &createAnonymousPointer(LinkGraph &G, Section &PointerSection,
  457. Symbol *InitialTarget = nullptr,
  458. uint64_t InitialAddend = 0) {
  459. auto &B = G.createContentBlock(PointerSection, NullPointerContent,
  460. orc::ExecutorAddr(~uint64_t(7)), 8, 0);
  461. if (InitialTarget)
  462. B.addEdge(Pointer64, 0, *InitialTarget, InitialAddend);
  463. return G.addAnonymousSymbol(B, 0, 8, false, false);
  464. }
  465. /// Create a jump stub block that jumps via the pointer at the given symbol.
  466. ///
  467. /// The stub block will have the following default values:
  468. /// alignment: 32-bit
  469. /// alignment-offset: 0
  470. /// address: highest allowable: (~11U)
  471. inline Block &createPointerJumpStubBlock(LinkGraph &G, Section &StubSection,
  472. Symbol &PointerSymbol) {
  473. auto &B = G.createContentBlock(StubSection, PointerJumpStubContent,
  474. orc::ExecutorAddr(~uint64_t(11)), 1, 0);
  475. B.addEdge(Page21, 0, PointerSymbol, 0);
  476. B.addEdge(PageOffset12, 4, PointerSymbol, 0);
  477. return B;
  478. }
  479. /// Create a jump stub that jumps via the pointer at the given symbol and
  480. /// an anonymous symbol pointing to it. Return the anonymous symbol.
  481. ///
  482. /// The stub block will be created by createPointerJumpStubBlock.
  483. inline Symbol &createAnonymousPointerJumpStub(LinkGraph &G,
  484. Section &StubSection,
  485. Symbol &PointerSymbol) {
  486. return G.addAnonymousSymbol(
  487. createPointerJumpStubBlock(G, StubSection, PointerSymbol), 0,
  488. sizeof(PointerJumpStubContent), true, false);
  489. }
  490. /// Global Offset Table Builder.
  491. class GOTTableManager : public TableManager<GOTTableManager> {
  492. public:
  493. static StringRef getSectionName() { return "$__GOT"; }
  494. bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
  495. Edge::Kind KindToSet = Edge::Invalid;
  496. const char *BlockWorkingMem = B->getContent().data();
  497. const char *FixupPtr = BlockWorkingMem + E.getOffset();
  498. switch (E.getKind()) {
  499. case aarch64::RequestGOTAndTransformToPage21:
  500. case aarch64::RequestTLVPAndTransformToPage21: {
  501. KindToSet = aarch64::Page21;
  502. break;
  503. }
  504. case aarch64::RequestGOTAndTransformToPageOffset12:
  505. case aarch64::RequestTLVPAndTransformToPageOffset12: {
  506. KindToSet = aarch64::PageOffset12;
  507. uint32_t RawInstr = *(const support::ulittle32_t *)FixupPtr;
  508. (void)RawInstr;
  509. assert(E.getAddend() == 0 &&
  510. "GOTPageOffset12/TLVPageOffset12 with non-zero addend");
  511. assert((RawInstr & 0xfffffc00) == 0xf9400000 &&
  512. "RawInstr isn't a 64-bit LDR immediate");
  513. break;
  514. }
  515. case aarch64::RequestGOTAndTransformToDelta32: {
  516. KindToSet = aarch64::Delta32;
  517. break;
  518. }
  519. default:
  520. return false;
  521. }
  522. assert(KindToSet != Edge::Invalid &&
  523. "Fell through switch, but no new kind to set");
  524. DEBUG_WITH_TYPE("jitlink", {
  525. dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
  526. << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
  527. << formatv("{0:x}", E.getOffset()) << ")\n";
  528. });
  529. E.setKind(KindToSet);
  530. E.setTarget(getEntryForTarget(G, E.getTarget()));
  531. return true;
  532. }
  533. Symbol &createEntry(LinkGraph &G, Symbol &Target) {
  534. return createAnonymousPointer(G, getGOTSection(G), &Target);
  535. }
  536. private:
  537. Section &getGOTSection(LinkGraph &G) {
  538. if (!GOTSection)
  539. GOTSection = &G.createSection(getSectionName(),
  540. orc::MemProt::Read | orc::MemProt::Exec);
  541. return *GOTSection;
  542. }
  543. Section *GOTSection = nullptr;
  544. };
  545. /// Procedure Linkage Table Builder.
  546. class PLTTableManager : public TableManager<PLTTableManager> {
  547. public:
  548. PLTTableManager(GOTTableManager &GOT) : GOT(GOT) {}
  549. static StringRef getSectionName() { return "$__STUBS"; }
  550. bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
  551. if (E.getKind() == aarch64::Branch26PCRel && !E.getTarget().isDefined()) {
  552. DEBUG_WITH_TYPE("jitlink", {
  553. dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
  554. << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
  555. << formatv("{0:x}", E.getOffset()) << ")\n";
  556. });
  557. E.setTarget(getEntryForTarget(G, E.getTarget()));
  558. return true;
  559. }
  560. return false;
  561. }
  562. Symbol &createEntry(LinkGraph &G, Symbol &Target) {
  563. return createAnonymousPointerJumpStub(G, getStubsSection(G),
  564. GOT.getEntryForTarget(G, Target));
  565. }
  566. public:
  567. Section &getStubsSection(LinkGraph &G) {
  568. if (!StubsSection)
  569. StubsSection = &G.createSection(getSectionName(),
  570. orc::MemProt::Read | orc::MemProt::Exec);
  571. return *StubsSection;
  572. }
  573. GOTTableManager &GOT;
  574. Section *StubsSection = nullptr;
  575. };
  576. } // namespace aarch64
  577. } // namespace jitlink
  578. } // namespace llvm
  579. #endif // LLVM_EXECUTIONENGINE_JITLINK_AARCH64_H
  580. #ifdef __GNUC__
  581. #pragma GCC diagnostic pop
  582. #endif