xref: /freebsd/contrib/llvm-project/llvm/include/llvm/ExecutionEngine/JITLink/loongarch.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //= loongarch.h - Generic JITLink loongarch edge kinds, utilities -*- C++ -*-=//
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 // Generic utilities for graphs representing loongarch objects.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_JITLINK_LOONGARCH_H
14 #define LLVM_EXECUTIONENGINE_JITLINK_LOONGARCH_H
15 
16 #include "TableManager.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
19 #include "llvm/ExecutionEngine/Orc/Shared/MemoryFlags.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/LEB128.h"
22 
23 namespace llvm {
24 namespace jitlink {
25 namespace loongarch {
26 
27 /// Represents loongarch fixups.
28 enum EdgeKind_loongarch : Edge::Kind {
29   /// A plain 64-bit pointer value relocation.
30   ///
31   /// Fixup expression:
32   ///   Fixup <- Target + Addend : uint64
33   ///
34   Pointer64 = Edge::FirstRelocation,
35 
36   /// A plain 32-bit pointer value relocation.
37   ///
38   /// Fixup expression:
39   ///   Fixup <- Target + Addend : uint32
40   ///
41   /// Errors:
42   ///   - The target must reside in the low 32-bits of the address space,
43   ///     otherwise an out-of-range error will be returned.
44   ///
45   Pointer32,
46 
47   /// A 16-bit PC-relative branch.
48   ///
49   /// Represents a PC-relative branch to a target within +/-128Kb. The target
50   /// must be 4-byte aligned.
51   ///
52   /// Fixup expression:
53   ///   Fixup <- (Target - Fixup + Addend) >> 2 : int16
54   ///
55   /// Notes:
56   ///   The '16' in the name refers to the number operand bits and follows the
57   /// naming convention used by the corresponding ELF relocations. Since the low
58   /// two bits must be zero (because of the 4-byte alignment of the target) the
59   /// operand is effectively a signed 18-bit number.
60   ///
61   /// Errors:
62   ///   - The result of the unshifted part of the fixup expression must be
63   ///     4-byte aligned otherwise an alignment error will be returned.
64   ///   - The result of the fixup expression must fit into an int16 otherwise an
65   ///     out-of-range error will be returned.
66   ///
67   Branch16PCRel,
68 
69   /// A 21-bit PC-relative branch.
70   ///
71   /// Represents a PC-relative branch to a target within +/-4Mb. The Target must
72   /// be 4-byte aligned.
73   ///
74   /// Fixup expression:
75   ///   Fixup <- (Target - Fixup + Addend) >> 2 : int21
76   ///
77   /// Notes:
78   ///   The '21' in the name refers to the number operand bits and follows the
79   /// naming convention used by the corresponding ELF relocations. Since the low
80   /// two bits must be zero (because of the 4-byte alignment of the target) the
81   /// operand is effectively a signed 23-bit number.
82   ///
83   /// Errors:
84   ///   - The result of the unshifted part of the fixup expression must be
85   ///     4-byte aligned otherwise an alignment error will be returned.
86   ///   - The result of the fixup expression must fit into an int21 otherwise an
87   ///     out-of-range error will be returned.
88   ///
89   Branch21PCRel,
90 
91   /// A 26-bit PC-relative branch.
92   ///
93   /// Represents a PC-relative call or branch to a target within +/-128Mb. The
94   /// target must be 4-byte aligned.
95   ///
96   /// Fixup expression:
97   ///   Fixup <- (Target - Fixup + Addend) >> 2 : int26
98   ///
99   /// Notes:
100   ///   The '26' in the name refers to the number operand bits and follows the
101   /// naming convention used by the corresponding ELF relocations. Since the low
102   /// two bits must be zero (because of the 4-byte alignment of the target) the
103   /// operand is effectively a signed 28-bit number.
104   ///
105   /// Errors:
106   ///   - The result of the unshifted part of the fixup expression must be
107   ///     4-byte aligned otherwise an alignment error will be returned.
108   ///   - The result of the fixup expression must fit into an int26 otherwise an
109   ///     out-of-range error will be returned.
110   ///
111   Branch26PCRel,
112 
113   /// A 32-bit delta.
114   ///
115   /// Delta from the fixup to the target.
116   ///
117   /// Fixup expression:
118   ///   Fixup <- Target - Fixup + Addend : int32
119   ///
120   /// Errors:
121   ///   - The result of the fixup expression must fit into an int32, otherwise
122   ///     an out-of-range error will be returned.
123   ///
124   Delta32,
125 
126   /// A 32-bit negative delta.
127   ///
128   /// Delta from the target back to the fixup.
129   ///
130   /// Fixup expression:
131   ///   Fixup <- Fixup - Target + Addend : int32
132   ///
133   /// Errors:
134   ///   - The result of the fixup expression must fit into an int32, otherwise
135   ///     an out-of-range error will be returned.
136   ///
137   NegDelta32,
138 
139   /// A 64-bit delta.
140   ///
141   /// Delta from the fixup to the target.
142   ///
143   /// Fixup expression:
144   ///   Fixup <- Target - Fixup + Addend : int64
145   ///
146   Delta64,
147 
148   /// The signed 20-bit delta from the fixup page to the page containing the
149   /// target.
150   ///
151   /// Fixup expression:
152   ///   Fixup <- (((Target + Addend + ((Target + Addend) & 0x800)) & ~0xfff)
153   //              - (Fixup & ~0xfff)) >> 12 : int20
154   ///
155   /// Notes:
156   ///   For PCALAU12I fixups.
157   ///
158   /// Errors:
159   ///   - The result of the fixup expression must fit into an int20 otherwise an
160   ///     out-of-range error will be returned.
161   ///
162   Page20,
163 
164   /// The 12-bit offset of the target within its page.
165   ///
166   /// Typically used to fix up ADDI/LD_W/LD_D immediates.
167   ///
168   /// Fixup expression:
169   ///   Fixup <- ((Target + Addend) >> Shift) & 0xfff : int12
170   ///
171   PageOffset12,
172 
173   /// A GOT entry getter/constructor, transformed to Page20 pointing at the GOT
174   /// entry for the original target.
175   ///
176   /// Indicates that this edge should be transformed into a Page20 targeting
177   /// the GOT entry for the edge's current target, maintaining the same addend.
178   /// A GOT entry for the target should be created if one does not already
179   /// exist.
180   ///
181   /// Edges of this kind are usually handled by a GOT/PLT builder pass inserted
182   /// by default.
183   ///
184   /// Fixup expression:
185   ///   NONE
186   ///
187   /// Errors:
188   ///   - *ASSERTION* Failure to handle edges of this kind prior to the fixup
189   ///     phase will result in an assert/unreachable during the fixup phase.
190   ///
191   RequestGOTAndTransformToPage20,
192 
193   /// A GOT entry getter/constructor, transformed to Pageoffset12 pointing at
194   /// the GOT entry for the original target.
195   ///
196   /// Indicates that this edge should be transformed into a PageOffset12
197   /// targeting the GOT entry for the edge's current target, maintaining the
198   /// same addend. A GOT entry for the target should be created if one does not
199   /// already exist.
200   ///
201   /// Edges of this kind are usually handled by a GOT/PLT builder pass inserted
202   /// by default.
203   ///
204   /// Fixup expression:
205   ///   NONE
206   ///
207   RequestGOTAndTransformToPageOffset12,
208 
209   /// A 36-bit PC-relative call.
210   ///
211   /// Represents a PC-relative call to a target within [-128G - 0x20000, +128G
212   /// - 0x20000). The target must be 4-byte aligned. For adjacent pcaddu18i+jirl
213   /// instruction pairs.
214   ///
215   /// Fixup expression:
216   ///   Fixup <- (Target - Fixup + Addend) >> 2 : int36
217   ///
218   /// Notes:
219   ///   The '36' in the name refers to the number operand bits and follows the
220   /// naming convention used by the corresponding ELF relocations. Since the low
221   /// two bits must be zero (because of the 4-byte alignment of the target) the
222   /// operand is effectively a signed 38-bit number.
223   ///
224   /// Errors:
225   ///   - The result of the unshifted part of the fixup expression must be
226   ///     4-byte aligned otherwise an alignment error will be returned.
227   ///   - The result of the fixup expression must fit into an int36 otherwise an
228   ///     out-of-range error will be returned.
229   ///
230   Call36PCRel,
231 
232   /// low 6 bits label addition
233   ///
234   /// Fixup expression:
235   ///   Fixup <- (*{1}Fixup + (Target + Addend) & 0x3f) : int8
236   ///
237   Add6,
238 
239   /// 8 bits label addition
240   ///
241   /// Fixup expression:
242   ///   Fixup <- (*{1}Fixup + Target + Addend) : int8
243   ///
244   Add8,
245 
246   /// 16 bits label addition
247   ///
248   /// Fixup expression:
249   ///   Fixup <- (*{2}Fixup + Target + Addend) : int16
250   ///
251   Add16,
252 
253   /// 32 bits label addition
254   ///
255   /// Fixup expression:
256   ///   Fixup <- (*{4}Fixup + Target + Addend) : int32
257   ///
258   Add32,
259 
260   /// 64 bits label addition
261   ///
262   /// Fixup expression:
263   ///   Fixup <- (*{8}Fixup + Target + Addend) : int64
264   ///
265   Add64,
266 
267   /// ULEB128 bits label addition
268   ///
269   /// Fixup expression:
270   ///   Fixup <- (Fixup + Target + Addend) : uleb128
271   ///
272   AddUleb128,
273 
274   /// low 6 bits label subtraction
275   ///
276   /// Fixup expression:
277   ///   Fixup <- (*{1}Fixup - (Target + Addend) & 0x3f) : int8
278   ///
279   Sub6,
280 
281   /// 8 bits label subtraction
282   ///
283   /// Fixup expression:
284   ///   Fixup <- (*{1}Fixup - Target - Addend) : int8
285   ///
286   Sub8,
287 
288   /// 16 bits label subtraction
289   ///
290   /// Fixup expression:
291   ///   Fixup <- (*{2}Fixup - Target - Addend) : int16
292   ///
293   Sub16,
294 
295   /// 32 bits label subtraction
296   ///
297   /// Fixup expression:
298   ///   Fixup <- (*{4}Fixup - Target - Addend) : int32
299   ///
300   Sub32,
301 
302   /// 64 bits label subtraction
303   ///
304   /// Fixup expression:
305   ///   Fixup <- (*{8}Fixup - Target - Addend) : int64
306   ///
307   Sub64,
308 
309   /// ULEB128 bits label subtraction
310   ///
311   /// Fixup expression:
312   ///   Fixup <- (Fixup - Target - Addend) : uleb128
313   ///
314   SubUleb128,
315 
316   /// Alignment requirement used by linker relaxation.
317   ///
318   /// Linker relaxation will use this to ensure all code sequences are properly
319   /// aligned and then remove these edges from the graph.
320   ///
321   AlignRelaxable,
322 };
323 
324 /// Returns a string name for the given loongarch edge. For debugging purposes
325 /// only.
326 LLVM_ABI const char *getEdgeKindName(Edge::Kind K);
327 
328 // Returns extract bits Val[Hi:Lo].
extractBits(uint64_t Val,unsigned Hi,unsigned Lo)329 inline uint32_t extractBits(uint64_t Val, unsigned Hi, unsigned Lo) {
330   return Hi == 63 ? Val >> Lo : (Val & ((((uint64_t)1 << (Hi + 1)) - 1))) >> Lo;
331 }
332 
333 /// Apply fixup expression for edge to block content.
applyFixup(LinkGraph & G,Block & B,const Edge & E)334 inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E) {
335   using namespace support;
336 
337   char *BlockWorkingMem = B.getAlreadyMutableContent().data();
338   char *FixupPtr = BlockWorkingMem + E.getOffset();
339   uint64_t FixupAddress = (B.getAddress() + E.getOffset()).getValue();
340   uint64_t TargetAddress = E.getTarget().getAddress().getValue();
341   int64_t Addend = E.getAddend();
342 
343   switch (E.getKind()) {
344   case Pointer64:
345     *(ulittle64_t *)FixupPtr = TargetAddress + Addend;
346     break;
347   case Pointer32: {
348     uint64_t Value = TargetAddress + Addend;
349     if (Value > std::numeric_limits<uint32_t>::max())
350       return makeTargetOutOfRangeError(G, B, E);
351     *(ulittle32_t *)FixupPtr = Value;
352     break;
353   }
354   case Branch16PCRel: {
355     int64_t Value = TargetAddress - FixupAddress + Addend;
356 
357     if (!isInt<18>(Value))
358       return makeTargetOutOfRangeError(G, B, E);
359 
360     if (!isShiftedInt<16, 2>(Value))
361       return makeAlignmentError(orc::ExecutorAddr(FixupAddress), Value, 4, E);
362 
363     uint32_t RawInstr = *(little32_t *)FixupPtr;
364     uint32_t Imm = static_cast<uint32_t>(Value >> 2);
365     uint32_t Imm15_0 = extractBits(Imm, /*Hi=*/15, /*Lo=*/0) << 10;
366     *(little32_t *)FixupPtr = RawInstr | Imm15_0;
367     break;
368   }
369   case Branch21PCRel: {
370     int64_t Value = TargetAddress - FixupAddress + Addend;
371 
372     if (!isInt<23>(Value))
373       return makeTargetOutOfRangeError(G, B, E);
374 
375     if (!isShiftedInt<21, 2>(Value))
376       return makeAlignmentError(orc::ExecutorAddr(FixupAddress), Value, 4, E);
377 
378     uint32_t RawInstr = *(little32_t *)FixupPtr;
379     uint32_t Imm = static_cast<uint32_t>(Value >> 2);
380     uint32_t Imm15_0 = extractBits(Imm, /*Hi=*/15, /*Lo=*/0) << 10;
381     uint32_t Imm20_16 = extractBits(Imm, /*Hi=*/20, /*Lo=*/16);
382     *(little32_t *)FixupPtr = RawInstr | Imm15_0 | Imm20_16;
383     break;
384   }
385   case Branch26PCRel: {
386     int64_t Value = TargetAddress - FixupAddress + Addend;
387 
388     if (!isInt<28>(Value))
389       return makeTargetOutOfRangeError(G, B, E);
390 
391     if (!isShiftedInt<26, 2>(Value))
392       return makeAlignmentError(orc::ExecutorAddr(FixupAddress), Value, 4, E);
393 
394     uint32_t RawInstr = *(little32_t *)FixupPtr;
395     uint32_t Imm = static_cast<uint32_t>(Value >> 2);
396     uint32_t Imm15_0 = extractBits(Imm, /*Hi=*/15, /*Lo=*/0) << 10;
397     uint32_t Imm25_16 = extractBits(Imm, /*Hi=*/25, /*Lo=*/16);
398     *(little32_t *)FixupPtr = RawInstr | Imm15_0 | Imm25_16;
399     break;
400   }
401   case Delta32: {
402     int64_t Value = TargetAddress - FixupAddress + Addend;
403 
404     if (!isInt<32>(Value))
405       return makeTargetOutOfRangeError(G, B, E);
406     *(little32_t *)FixupPtr = Value;
407     break;
408   }
409   case NegDelta32: {
410     int64_t Value = FixupAddress - TargetAddress + Addend;
411     if (!isInt<32>(Value))
412       return makeTargetOutOfRangeError(G, B, E);
413     *(little32_t *)FixupPtr = Value;
414     break;
415   }
416   case Delta64:
417     *(little64_t *)FixupPtr = TargetAddress - FixupAddress + Addend;
418     break;
419   case Page20: {
420     uint64_t Target = TargetAddress + Addend;
421     uint64_t TargetPage =
422         (Target + (Target & 0x800)) & ~static_cast<uint64_t>(0xfff);
423     uint64_t PCPage = FixupAddress & ~static_cast<uint64_t>(0xfff);
424 
425     int64_t PageDelta = TargetPage - PCPage;
426     if (!isInt<32>(PageDelta))
427       return makeTargetOutOfRangeError(G, B, E);
428 
429     uint32_t RawInstr = *(little32_t *)FixupPtr;
430     uint32_t Imm31_12 = extractBits(PageDelta, /*Hi=*/31, /*Lo=*/12) << 5;
431     *(little32_t *)FixupPtr = RawInstr | Imm31_12;
432     break;
433   }
434   case PageOffset12: {
435     uint64_t TargetOffset = (TargetAddress + Addend) & 0xfff;
436 
437     uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
438     uint32_t Imm11_0 = TargetOffset << 10;
439     *(ulittle32_t *)FixupPtr = RawInstr | Imm11_0;
440     break;
441   }
442   case Call36PCRel: {
443     int64_t Value = TargetAddress - FixupAddress + Addend;
444 
445     if ((Value + 0x20000) != llvm::SignExtend64(Value + 0x20000, 38))
446       return makeTargetOutOfRangeError(G, B, E);
447 
448     if (!isShiftedInt<36, 2>(Value))
449       return makeAlignmentError(orc::ExecutorAddr(FixupAddress), Value, 4, E);
450 
451     uint32_t Pcaddu18i = *(little32_t *)FixupPtr;
452     uint32_t Hi20 = extractBits(Value + (1 << 17), /*Hi=*/37, /*Lo=*/18) << 5;
453     *(little32_t *)FixupPtr = Pcaddu18i | Hi20;
454     uint32_t Jirl = *(little32_t *)(FixupPtr + 4);
455     uint32_t Lo16 = extractBits(Value, /*Hi=*/17, /*Lo=*/2) << 10;
456     *(little32_t *)(FixupPtr + 4) = Jirl | Lo16;
457     break;
458   }
459   case Add6: {
460     int64_t Value = *(reinterpret_cast<const int8_t *>(FixupPtr));
461     Value += ((TargetAddress + Addend) & 0x3f);
462     *FixupPtr = (*FixupPtr & 0xc0) | (static_cast<int8_t>(Value) & 0x3f);
463     break;
464   }
465   case Add8: {
466     int64_t Value =
467         TargetAddress + *(reinterpret_cast<const int8_t *>(FixupPtr)) + Addend;
468     *FixupPtr = static_cast<int8_t>(Value);
469     break;
470   }
471   case Add16: {
472     int64_t Value =
473         TargetAddress + support::endian::read16le(FixupPtr) + Addend;
474     *(little16_t *)FixupPtr = static_cast<int16_t>(Value);
475     break;
476   }
477   case Add32: {
478     int64_t Value =
479         TargetAddress + support::endian::read32le(FixupPtr) + Addend;
480     *(little32_t *)FixupPtr = static_cast<int32_t>(Value);
481     break;
482   }
483   case Add64: {
484     int64_t Value =
485         TargetAddress + support::endian::read64le(FixupPtr) + Addend;
486     *(little64_t *)FixupPtr = static_cast<int64_t>(Value);
487     break;
488   }
489   case AddUleb128: {
490     const uint32_t Maxcount = 1 + 64 / 7;
491     uint32_t Count;
492     const char *Error = nullptr;
493     uint64_t Orig = decodeULEB128((reinterpret_cast<const uint8_t *>(FixupPtr)),
494                                   &Count, nullptr, &Error);
495 
496     if (Count > Maxcount || (Count == Maxcount && Error))
497       return make_error<JITLinkError>(
498           "0x" + llvm::utohexstr(orc::ExecutorAddr(FixupAddress).getValue()) +
499           ": extra space for uleb128");
500 
501     uint64_t Mask = Count < Maxcount ? (1ULL << 7 * Count) - 1 : -1ULL;
502     encodeULEB128((Orig + TargetAddress + Addend) & Mask,
503                   (reinterpret_cast<uint8_t *>(FixupPtr)), Count);
504     break;
505   }
506   case Sub6: {
507     int64_t Value = *(reinterpret_cast<const int8_t *>(FixupPtr));
508     Value -= ((TargetAddress + Addend) & 0x3f);
509     *FixupPtr = (*FixupPtr & 0xc0) | (static_cast<int8_t>(Value) & 0x3f);
510     break;
511   }
512   case Sub8: {
513     int64_t Value =
514         *(reinterpret_cast<const int8_t *>(FixupPtr)) - TargetAddress - Addend;
515     *FixupPtr = static_cast<int8_t>(Value);
516     break;
517   }
518   case Sub16: {
519     int64_t Value =
520         support::endian::read16le(FixupPtr) - TargetAddress - Addend;
521     *(little16_t *)FixupPtr = static_cast<int16_t>(Value);
522     break;
523   }
524   case Sub32: {
525     int64_t Value =
526         support::endian::read32le(FixupPtr) - TargetAddress - Addend;
527     *(little32_t *)FixupPtr = static_cast<int32_t>(Value);
528     break;
529   }
530   case Sub64: {
531     int64_t Value =
532         support::endian::read64le(FixupPtr) - TargetAddress - Addend;
533     *(little64_t *)FixupPtr = static_cast<int64_t>(Value);
534     break;
535   }
536   case SubUleb128: {
537     const uint32_t Maxcount = 1 + 64 / 7;
538     uint32_t Count;
539     const char *Error = nullptr;
540     uint64_t Orig = decodeULEB128((reinterpret_cast<const uint8_t *>(FixupPtr)),
541                                   &Count, nullptr, &Error);
542 
543     if (Count > Maxcount || (Count == Maxcount && Error))
544       return make_error<JITLinkError>(
545           "0x" + llvm::utohexstr(orc::ExecutorAddr(FixupAddress).getValue()) +
546           ": extra space for uleb128");
547 
548     uint64_t Mask = Count < Maxcount ? (1ULL << 7 * Count) - 1 : -1ULL;
549     encodeULEB128((Orig - TargetAddress - Addend) & Mask,
550                   (reinterpret_cast<uint8_t *>(FixupPtr)), Count);
551     break;
552   }
553   case AlignRelaxable:
554     // Ignore when the relaxation pass did not run
555     break;
556   default:
557     return make_error<JITLinkError>(
558         "In graph " + G.getName() + ", section " + B.getSection().getName() +
559         " unsupported edge kind " + getEdgeKindName(E.getKind()));
560   }
561 
562   return Error::success();
563 }
564 
565 /// loongarch null pointer content.
566 LLVM_ABI extern const char NullPointerContent[8];
getGOTEntryBlockContent(LinkGraph & G)567 inline ArrayRef<char> getGOTEntryBlockContent(LinkGraph &G) {
568   return {reinterpret_cast<const char *>(NullPointerContent),
569           G.getPointerSize()};
570 }
571 
572 /// loongarch stub content.
573 ///
574 /// Contains the instruction sequence for an indirect jump via an in-memory
575 /// pointer:
576 ///   pcalau12i $t8, %page20(ptr)
577 ///   ld.[w/d]  $t8, %pageoff12(ptr)
578 ///   jr        $t8
579 constexpr size_t StubEntrySize = 12;
580 LLVM_ABI extern const uint8_t LA64StubContent[StubEntrySize];
581 LLVM_ABI extern const uint8_t LA32StubContent[StubEntrySize];
getStubBlockContent(LinkGraph & G)582 inline ArrayRef<char> getStubBlockContent(LinkGraph &G) {
583   auto StubContent =
584       G.getPointerSize() == 8 ? LA64StubContent : LA32StubContent;
585   return {reinterpret_cast<const char *>(StubContent), StubEntrySize};
586 }
587 
588 /// Creates a new pointer block in the given section and returns an
589 /// Anonymous symbol pointing to it.
590 ///
591 /// If InitialTarget is given then an Pointer64 relocation will be added to the
592 /// block pointing at InitialTarget.
593 ///
594 /// The pointer block will have the following default values:
595 ///   alignment: PointerSize
596 ///   alignment-offset: 0
597 inline Symbol &createAnonymousPointer(LinkGraph &G, Section &PointerSection,
598                                       Symbol *InitialTarget = nullptr,
599                                       uint64_t InitialAddend = 0) {
600   auto &B = G.createContentBlock(PointerSection, getGOTEntryBlockContent(G),
601                                  orc::ExecutorAddr(), G.getPointerSize(), 0);
602   if (InitialTarget)
603     B.addEdge(G.getPointerSize() == 8 ? Pointer64 : Pointer32, 0,
604               *InitialTarget, InitialAddend);
605   return G.addAnonymousSymbol(B, 0, G.getPointerSize(), false, false);
606 }
607 
608 /// Create a jump stub that jumps via the pointer at the given symbol and
609 /// an anonymous symbol pointing to it. Return the anonymous symbol.
createAnonymousPointerJumpStub(LinkGraph & G,Section & StubSection,Symbol & PointerSymbol)610 inline Symbol &createAnonymousPointerJumpStub(LinkGraph &G,
611                                               Section &StubSection,
612                                               Symbol &PointerSymbol) {
613   Block &StubContentBlock = G.createContentBlock(
614       StubSection, getStubBlockContent(G), orc::ExecutorAddr(), 4, 0);
615   StubContentBlock.addEdge(Page20, 0, PointerSymbol, 0);
616   StubContentBlock.addEdge(PageOffset12, 4, PointerSymbol, 0);
617   return G.addAnonymousSymbol(StubContentBlock, 0, StubEntrySize, true, false);
618 }
619 
620 /// Global Offset Table Builder.
621 class GOTTableManager : public TableManager<GOTTableManager> {
622 public:
getSectionName()623   static StringRef getSectionName() { return "$__GOT"; }
624 
visitEdge(LinkGraph & G,Block * B,Edge & E)625   bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
626     Edge::Kind KindToSet = Edge::Invalid;
627     switch (E.getKind()) {
628     case RequestGOTAndTransformToPage20:
629       KindToSet = Page20;
630       break;
631     case RequestGOTAndTransformToPageOffset12:
632       KindToSet = PageOffset12;
633       break;
634     default:
635       return false;
636     }
637     assert(KindToSet != Edge::Invalid &&
638            "Fell through switch, but no new kind to set");
639     DEBUG_WITH_TYPE("jitlink", {
640       dbgs() << "  Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
641              << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
642              << formatv("{0:x}", E.getOffset()) << ")\n";
643     });
644     E.setKind(KindToSet);
645     E.setTarget(getEntryForTarget(G, E.getTarget()));
646     return true;
647   }
648 
createEntry(LinkGraph & G,Symbol & Target)649   Symbol &createEntry(LinkGraph &G, Symbol &Target) {
650     return createAnonymousPointer(G, getGOTSection(G), &Target);
651   }
652 
653 private:
getGOTSection(LinkGraph & G)654   Section &getGOTSection(LinkGraph &G) {
655     if (!GOTSection)
656       GOTSection = &G.createSection(getSectionName(),
657                                     orc::MemProt::Read | orc::MemProt::Exec);
658     return *GOTSection;
659   }
660 
661   Section *GOTSection = nullptr;
662 };
663 
664 /// Procedure Linkage Table Builder.
665 class PLTTableManager : public TableManager<PLTTableManager> {
666 public:
PLTTableManager(GOTTableManager & GOT)667   PLTTableManager(GOTTableManager &GOT) : GOT(GOT) {}
668 
getSectionName()669   static StringRef getSectionName() { return "$__STUBS"; }
670 
visitEdge(LinkGraph & G,Block * B,Edge & E)671   bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
672     if ((E.getKind() == Branch26PCRel || E.getKind() == Call36PCRel) &&
673         !E.getTarget().isDefined()) {
674       DEBUG_WITH_TYPE("jitlink", {
675         dbgs() << "  Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
676                << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
677                << formatv("{0:x}", E.getOffset()) << ")\n";
678       });
679       E.setTarget(getEntryForTarget(G, E.getTarget()));
680       return true;
681     }
682     return false;
683   }
684 
createEntry(LinkGraph & G,Symbol & Target)685   Symbol &createEntry(LinkGraph &G, Symbol &Target) {
686     return createAnonymousPointerJumpStub(G, getStubsSection(G),
687                                           GOT.getEntryForTarget(G, Target));
688   }
689 
690 public:
getStubsSection(LinkGraph & G)691   Section &getStubsSection(LinkGraph &G) {
692     if (!StubsSection)
693       StubsSection = &G.createSection(getSectionName(),
694                                       orc::MemProt::Read | orc::MemProt::Exec);
695     return *StubsSection;
696   }
697 
698   GOTTableManager &GOT;
699   Section *StubsSection = nullptr;
700 };
701 
702 } // namespace loongarch
703 } // namespace jitlink
704 } // namespace llvm
705 
706 #endif
707