Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:50

0001 //= loongarch.h - Generic JITLink loongarch edge kinds, utilities -*- C++ -*-=//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Generic utilities for graphs representing loongarch objects.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_EXECUTIONENGINE_JITLINK_LOONGARCH_H
0014 #define LLVM_EXECUTIONENGINE_JITLINK_LOONGARCH_H
0015 
0016 #include "TableManager.h"
0017 #include "llvm/ADT/StringExtras.h"
0018 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
0019 #include "llvm/ExecutionEngine/Orc/Shared/MemoryFlags.h"
0020 #include "llvm/Support/LEB128.h"
0021 
0022 namespace llvm {
0023 namespace jitlink {
0024 namespace loongarch {
0025 
0026 /// Represents loongarch fixups.
0027 enum EdgeKind_loongarch : Edge::Kind {
0028   /// A plain 64-bit pointer value relocation.
0029   ///
0030   /// Fixup expression:
0031   ///   Fixup <- Target + Addend : uint64
0032   ///
0033   Pointer64 = Edge::FirstRelocation,
0034 
0035   /// A plain 32-bit pointer value relocation.
0036   ///
0037   /// Fixup expression:
0038   ///   Fixup <- Target + Addend : uint32
0039   ///
0040   /// Errors:
0041   ///   - The target must reside in the low 32-bits of the address space,
0042   ///     otherwise an out-of-range error will be returned.
0043   ///
0044   Pointer32,
0045 
0046   /// A 16-bit PC-relative branch.
0047   ///
0048   /// Represents a PC-relative branch to a target within +/-128Kb. The target
0049   /// must be 4-byte aligned.
0050   ///
0051   /// Fixup expression:
0052   ///   Fixup <- (Target - Fixup + Addend) >> 2 : int16
0053   ///
0054   /// Notes:
0055   ///   The '16' in the name refers to the number operand bits and follows the
0056   /// naming convention used by the corresponding ELF relocations. Since the low
0057   /// two bits must be zero (because of the 4-byte alignment of the target) the
0058   /// operand is effectively a signed 18-bit number.
0059   ///
0060   /// Errors:
0061   ///   - The result of the unshifted part of the fixup expression must be
0062   ///     4-byte aligned otherwise an alignment error will be returned.
0063   ///   - The result of the fixup expression must fit into an int16 otherwise an
0064   ///     out-of-range error will be returned.
0065   ///
0066   Branch16PCRel,
0067 
0068   /// A 21-bit PC-relative branch.
0069   ///
0070   /// Represents a PC-relative branch to a target within +/-4Mb. The Target must
0071   /// be 4-byte aligned.
0072   ///
0073   /// Fixup expression:
0074   ///   Fixup <- (Target - Fixup + Addend) >> 2 : int21
0075   ///
0076   /// Notes:
0077   ///   The '21' in the name refers to the number operand bits and follows the
0078   /// naming convention used by the corresponding ELF relocations. Since the low
0079   /// two bits must be zero (because of the 4-byte alignment of the target) the
0080   /// operand is effectively a signed 23-bit number.
0081   ///
0082   /// Errors:
0083   ///   - The result of the unshifted part of the fixup expression must be
0084   ///     4-byte aligned otherwise an alignment error will be returned.
0085   ///   - The result of the fixup expression must fit into an int21 otherwise an
0086   ///     out-of-range error will be returned.
0087   ///
0088   Branch21PCRel,
0089 
0090   /// A 26-bit PC-relative branch.
0091   ///
0092   /// Represents a PC-relative call or branch to a target within +/-128Mb. The
0093   /// target must be 4-byte aligned.
0094   ///
0095   /// Fixup expression:
0096   ///   Fixup <- (Target - Fixup + Addend) >> 2 : int26
0097   ///
0098   /// Notes:
0099   ///   The '26' in the name refers to the number operand bits and follows the
0100   /// naming convention used by the corresponding ELF relocations. Since the low
0101   /// two bits must be zero (because of the 4-byte alignment of the target) the
0102   /// operand is effectively a signed 28-bit number.
0103   ///
0104   /// Errors:
0105   ///   - The result of the unshifted part of the fixup expression must be
0106   ///     4-byte aligned otherwise an alignment error will be returned.
0107   ///   - The result of the fixup expression must fit into an int26 otherwise an
0108   ///     out-of-range error will be returned.
0109   ///
0110   Branch26PCRel,
0111 
0112   /// A 32-bit delta.
0113   ///
0114   /// Delta from the fixup to the target.
0115   ///
0116   /// Fixup expression:
0117   ///   Fixup <- Target - Fixup + Addend : int32
0118   ///
0119   /// Errors:
0120   ///   - The result of the fixup expression must fit into an int32, otherwise
0121   ///     an out-of-range error will be returned.
0122   ///
0123   Delta32,
0124 
0125   /// A 32-bit negative delta.
0126   ///
0127   /// Delta from the target back to the fixup.
0128   ///
0129   /// Fixup expression:
0130   ///   Fixup <- Fixup - Target + Addend : int32
0131   ///
0132   /// Errors:
0133   ///   - The result of the fixup expression must fit into an int32, otherwise
0134   ///     an out-of-range error will be returned.
0135   ///
0136   NegDelta32,
0137 
0138   /// A 64-bit delta.
0139   ///
0140   /// Delta from the fixup to the target.
0141   ///
0142   /// Fixup expression:
0143   ///   Fixup <- Target - Fixup + Addend : int64
0144   ///
0145   Delta64,
0146 
0147   /// The signed 20-bit delta from the fixup page to the page containing the
0148   /// target.
0149   ///
0150   /// Fixup expression:
0151   ///   Fixup <- (((Target + Addend + ((Target + Addend) & 0x800)) & ~0xfff)
0152   //              - (Fixup & ~0xfff)) >> 12 : int20
0153   ///
0154   /// Notes:
0155   ///   For PCALAU12I fixups.
0156   ///
0157   /// Errors:
0158   ///   - The result of the fixup expression must fit into an int20 otherwise an
0159   ///     out-of-range error will be returned.
0160   ///
0161   Page20,
0162 
0163   /// The 12-bit offset of the target within its page.
0164   ///
0165   /// Typically used to fix up ADDI/LD_W/LD_D immediates.
0166   ///
0167   /// Fixup expression:
0168   ///   Fixup <- ((Target + Addend) >> Shift) & 0xfff : int12
0169   ///
0170   PageOffset12,
0171 
0172   /// A GOT entry getter/constructor, transformed to Page20 pointing at the GOT
0173   /// entry for the original target.
0174   ///
0175   /// Indicates that this edge should be transformed into a Page20 targeting
0176   /// the GOT entry for the edge's current target, maintaining the same addend.
0177   /// A GOT entry for the target should be created if one does not already
0178   /// exist.
0179   ///
0180   /// Edges of this kind are usually handled by a GOT/PLT builder pass inserted
0181   /// by default.
0182   ///
0183   /// Fixup expression:
0184   ///   NONE
0185   ///
0186   /// Errors:
0187   ///   - *ASSERTION* Failure to handle edges of this kind prior to the fixup
0188   ///     phase will result in an assert/unreachable during the fixup phase.
0189   ///
0190   RequestGOTAndTransformToPage20,
0191 
0192   /// A GOT entry getter/constructor, transformed to Pageoffset12 pointing at
0193   /// the GOT entry for the original target.
0194   ///
0195   /// Indicates that this edge should be transformed into a PageOffset12
0196   /// targeting the GOT entry for the edge's current target, maintaining the
0197   /// same addend. A GOT entry for the target should be created if one does not
0198   /// already exist.
0199   ///
0200   /// Edges of this kind are usually handled by a GOT/PLT builder pass inserted
0201   /// by default.
0202   ///
0203   /// Fixup expression:
0204   ///   NONE
0205   ///
0206   RequestGOTAndTransformToPageOffset12,
0207 
0208   /// A 36-bit PC-relative call.
0209   ///
0210   /// Represents a PC-relative call to a target within [-128G - 0x20000, +128G
0211   /// - 0x20000). The target must be 4-byte aligned. For adjacent pcaddu18i+jirl
0212   /// instruction pairs.
0213   ///
0214   /// Fixup expression:
0215   ///   Fixup <- (Target - Fixup + Addend) >> 2 : int36
0216   ///
0217   /// Notes:
0218   ///   The '36' in the name refers to the number operand bits and follows the
0219   /// naming convention used by the corresponding ELF relocations. Since the low
0220   /// two bits must be zero (because of the 4-byte alignment of the target) the
0221   /// operand is effectively a signed 38-bit number.
0222   ///
0223   /// Errors:
0224   ///   - The result of the unshifted part of the fixup expression must be
0225   ///     4-byte aligned otherwise an alignment error will be returned.
0226   ///   - The result of the fixup expression must fit into an int36 otherwise an
0227   ///     out-of-range error will be returned.
0228   ///
0229   Call36PCRel,
0230 
0231   /// low 6 bits label addition
0232   ///
0233   /// Fixup expression:
0234   ///   Fixup <- (*{1}Fixup + (Target + Addend) & 0x3f) : int8
0235   ///
0236   Add6,
0237 
0238   /// 8 bits label addition
0239   ///
0240   /// Fixup expression:
0241   ///   Fixup <- (*{1}Fixup + Target + Addend) : int8
0242   ///
0243   Add8,
0244 
0245   /// 16 bits label addition
0246   ///
0247   /// Fixup expression:
0248   ///   Fixup <- (*{2}Fixup + Target + Addend) : int16
0249   ///
0250   Add16,
0251 
0252   /// 32 bits label addition
0253   ///
0254   /// Fixup expression:
0255   ///   Fixup <- (*{4}Fixup + Target + Addend) : int32
0256   ///
0257   Add32,
0258 
0259   /// 64 bits label addition
0260   ///
0261   /// Fixup expression:
0262   ///   Fixup <- (*{8}Fixup + Target + Addend) : int64
0263   ///
0264   Add64,
0265 
0266   /// ULEB128 bits label addition
0267   ///
0268   /// Fixup expression:
0269   ///   Fixup <- (Fixup + Target + Addend) : uleb128
0270   ///
0271   AddUleb128,
0272 
0273   /// low 6 bits label subtraction
0274   ///
0275   /// Fixup expression:
0276   ///   Fixup <- (*{1}Fixup - (Target + Addend) & 0x3f) : int8
0277   ///
0278   Sub6,
0279 
0280   /// 8 bits label subtraction
0281   ///
0282   /// Fixup expression:
0283   ///   Fixup <- (*{1}Fixup - Target - Addend) : int8
0284   ///
0285   Sub8,
0286 
0287   /// 16 bits label subtraction
0288   ///
0289   /// Fixup expression:
0290   ///   Fixup <- (*{2}Fixup - Target - Addend) : int16
0291   ///
0292   Sub16,
0293 
0294   /// 32 bits label subtraction
0295   ///
0296   /// Fixup expression:
0297   ///   Fixup <- (*{4}Fixup - Target - Addend) : int32
0298   ///
0299   Sub32,
0300 
0301   /// 64 bits label subtraction
0302   ///
0303   /// Fixup expression:
0304   ///   Fixup <- (*{8}Fixup - Target - Addend) : int64
0305   ///
0306   Sub64,
0307 
0308   /// ULEB128 bits label subtraction
0309   ///
0310   /// Fixup expression:
0311   ///   Fixup <- (Fixup - Target - Addend) : uleb128
0312   ///
0313   SubUleb128,
0314 
0315   /// Alignment requirement used by linker relaxation.
0316   ///
0317   /// Linker relaxation will use this to ensure all code sequences are properly
0318   /// aligned and then remove these edges from the graph.
0319   ///
0320   AlignRelaxable,
0321 };
0322 
0323 /// Returns a string name for the given loongarch edge. For debugging purposes
0324 /// only.
0325 const char *getEdgeKindName(Edge::Kind K);
0326 
0327 // Returns extract bits Val[Hi:Lo].
0328 inline uint32_t extractBits(uint64_t Val, unsigned Hi, unsigned Lo) {
0329   return Hi == 63 ? Val >> Lo : (Val & ((((uint64_t)1 << (Hi + 1)) - 1))) >> Lo;
0330 }
0331 
0332 /// Apply fixup expression for edge to block content.
0333 inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E) {
0334   using namespace support;
0335 
0336   char *BlockWorkingMem = B.getAlreadyMutableContent().data();
0337   char *FixupPtr = BlockWorkingMem + E.getOffset();
0338   uint64_t FixupAddress = (B.getAddress() + E.getOffset()).getValue();
0339   uint64_t TargetAddress = E.getTarget().getAddress().getValue();
0340   int64_t Addend = E.getAddend();
0341 
0342   switch (E.getKind()) {
0343   case Pointer64:
0344     *(ulittle64_t *)FixupPtr = TargetAddress + Addend;
0345     break;
0346   case Pointer32: {
0347     uint64_t Value = TargetAddress + Addend;
0348     if (Value > std::numeric_limits<uint32_t>::max())
0349       return makeTargetOutOfRangeError(G, B, E);
0350     *(ulittle32_t *)FixupPtr = Value;
0351     break;
0352   }
0353   case Branch16PCRel: {
0354     int64_t Value = TargetAddress - FixupAddress + Addend;
0355 
0356     if (!isInt<18>(Value))
0357       return makeTargetOutOfRangeError(G, B, E);
0358 
0359     if (!isShiftedInt<16, 2>(Value))
0360       return makeAlignmentError(orc::ExecutorAddr(FixupAddress), Value, 4, E);
0361 
0362     uint32_t RawInstr = *(little32_t *)FixupPtr;
0363     uint32_t Imm = static_cast<uint32_t>(Value >> 2);
0364     uint32_t Imm15_0 = extractBits(Imm, /*Hi=*/15, /*Lo=*/0) << 10;
0365     *(little32_t *)FixupPtr = RawInstr | Imm15_0;
0366     break;
0367   }
0368   case Branch21PCRel: {
0369     int64_t Value = TargetAddress - FixupAddress + Addend;
0370 
0371     if (!isInt<23>(Value))
0372       return makeTargetOutOfRangeError(G, B, E);
0373 
0374     if (!isShiftedInt<21, 2>(Value))
0375       return makeAlignmentError(orc::ExecutorAddr(FixupAddress), Value, 4, E);
0376 
0377     uint32_t RawInstr = *(little32_t *)FixupPtr;
0378     uint32_t Imm = static_cast<uint32_t>(Value >> 2);
0379     uint32_t Imm15_0 = extractBits(Imm, /*Hi=*/15, /*Lo=*/0) << 10;
0380     uint32_t Imm20_16 = extractBits(Imm, /*Hi=*/20, /*Lo=*/16);
0381     *(little32_t *)FixupPtr = RawInstr | Imm15_0 | Imm20_16;
0382     break;
0383   }
0384   case Branch26PCRel: {
0385     int64_t Value = TargetAddress - FixupAddress + Addend;
0386 
0387     if (!isInt<28>(Value))
0388       return makeTargetOutOfRangeError(G, B, E);
0389 
0390     if (!isShiftedInt<26, 2>(Value))
0391       return makeAlignmentError(orc::ExecutorAddr(FixupAddress), Value, 4, E);
0392 
0393     uint32_t RawInstr = *(little32_t *)FixupPtr;
0394     uint32_t Imm = static_cast<uint32_t>(Value >> 2);
0395     uint32_t Imm15_0 = extractBits(Imm, /*Hi=*/15, /*Lo=*/0) << 10;
0396     uint32_t Imm25_16 = extractBits(Imm, /*Hi=*/25, /*Lo=*/16);
0397     *(little32_t *)FixupPtr = RawInstr | Imm15_0 | Imm25_16;
0398     break;
0399   }
0400   case Delta32: {
0401     int64_t Value = TargetAddress - FixupAddress + Addend;
0402 
0403     if (!isInt<32>(Value))
0404       return makeTargetOutOfRangeError(G, B, E);
0405     *(little32_t *)FixupPtr = Value;
0406     break;
0407   }
0408   case NegDelta32: {
0409     int64_t Value = FixupAddress - TargetAddress + Addend;
0410     if (!isInt<32>(Value))
0411       return makeTargetOutOfRangeError(G, B, E);
0412     *(little32_t *)FixupPtr = Value;
0413     break;
0414   }
0415   case Delta64:
0416     *(little64_t *)FixupPtr = TargetAddress - FixupAddress + Addend;
0417     break;
0418   case Page20: {
0419     uint64_t Target = TargetAddress + Addend;
0420     uint64_t TargetPage =
0421         (Target + (Target & 0x800)) & ~static_cast<uint64_t>(0xfff);
0422     uint64_t PCPage = FixupAddress & ~static_cast<uint64_t>(0xfff);
0423 
0424     int64_t PageDelta = TargetPage - PCPage;
0425     if (!isInt<32>(PageDelta))
0426       return makeTargetOutOfRangeError(G, B, E);
0427 
0428     uint32_t RawInstr = *(little32_t *)FixupPtr;
0429     uint32_t Imm31_12 = extractBits(PageDelta, /*Hi=*/31, /*Lo=*/12) << 5;
0430     *(little32_t *)FixupPtr = RawInstr | Imm31_12;
0431     break;
0432   }
0433   case PageOffset12: {
0434     uint64_t TargetOffset = (TargetAddress + Addend) & 0xfff;
0435 
0436     uint32_t RawInstr = *(ulittle32_t *)FixupPtr;
0437     uint32_t Imm11_0 = TargetOffset << 10;
0438     *(ulittle32_t *)FixupPtr = RawInstr | Imm11_0;
0439     break;
0440   }
0441   case Call36PCRel: {
0442     int64_t Value = TargetAddress - FixupAddress + Addend;
0443 
0444     if ((Value + 0x20000) != llvm::SignExtend64(Value + 0x20000, 38))
0445       return makeTargetOutOfRangeError(G, B, E);
0446 
0447     if (!isShiftedInt<36, 2>(Value))
0448       return makeAlignmentError(orc::ExecutorAddr(FixupAddress), Value, 4, E);
0449 
0450     uint32_t Pcaddu18i = *(little32_t *)FixupPtr;
0451     uint32_t Hi20 = extractBits(Value + (1 << 17), /*Hi=*/37, /*Lo=*/18) << 5;
0452     *(little32_t *)FixupPtr = Pcaddu18i | Hi20;
0453     uint32_t Jirl = *(little32_t *)(FixupPtr + 4);
0454     uint32_t Lo16 = extractBits(Value, /*Hi=*/17, /*Lo=*/2) << 10;
0455     *(little32_t *)(FixupPtr + 4) = Jirl | Lo16;
0456     break;
0457   }
0458   case Add6: {
0459     int64_t Value = *(reinterpret_cast<const int8_t *>(FixupPtr));
0460     Value += ((TargetAddress + Addend) & 0x3f);
0461     *FixupPtr = (*FixupPtr & 0xc0) | (static_cast<int8_t>(Value) & 0x3f);
0462     break;
0463   }
0464   case Add8: {
0465     int64_t Value =
0466         TargetAddress + *(reinterpret_cast<const int8_t *>(FixupPtr)) + Addend;
0467     *FixupPtr = static_cast<int8_t>(Value);
0468     break;
0469   }
0470   case Add16: {
0471     int64_t Value =
0472         TargetAddress + support::endian::read16le(FixupPtr) + Addend;
0473     *(little16_t *)FixupPtr = static_cast<int16_t>(Value);
0474     break;
0475   }
0476   case Add32: {
0477     int64_t Value =
0478         TargetAddress + support::endian::read32le(FixupPtr) + Addend;
0479     *(little32_t *)FixupPtr = static_cast<int32_t>(Value);
0480     break;
0481   }
0482   case Add64: {
0483     int64_t Value =
0484         TargetAddress + support::endian::read64le(FixupPtr) + Addend;
0485     *(little64_t *)FixupPtr = static_cast<int64_t>(Value);
0486     break;
0487   }
0488   case AddUleb128: {
0489     const uint32_t Maxcount = 1 + 64 / 7;
0490     uint32_t Count;
0491     const char *Error = nullptr;
0492     uint64_t Orig = decodeULEB128((reinterpret_cast<const uint8_t *>(FixupPtr)),
0493                                   &Count, nullptr, &Error);
0494 
0495     if (Count > Maxcount || (Count == Maxcount && Error))
0496       return make_error<JITLinkError>(
0497           "0x" + llvm::utohexstr(orc::ExecutorAddr(FixupAddress).getValue()) +
0498           ": extra space for uleb128");
0499 
0500     uint64_t Mask = Count < Maxcount ? (1ULL << 7 * Count) - 1 : -1ULL;
0501     encodeULEB128((Orig + TargetAddress + Addend) & Mask,
0502                   (reinterpret_cast<uint8_t *>(FixupPtr)), Count);
0503     break;
0504   }
0505   case Sub6: {
0506     int64_t Value = *(reinterpret_cast<const int8_t *>(FixupPtr));
0507     Value -= ((TargetAddress + Addend) & 0x3f);
0508     *FixupPtr = (*FixupPtr & 0xc0) | (static_cast<int8_t>(Value) & 0x3f);
0509     break;
0510   }
0511   case Sub8: {
0512     int64_t Value =
0513         *(reinterpret_cast<const int8_t *>(FixupPtr)) - TargetAddress - Addend;
0514     *FixupPtr = static_cast<int8_t>(Value);
0515     break;
0516   }
0517   case Sub16: {
0518     int64_t Value =
0519         support::endian::read16le(FixupPtr) - TargetAddress - Addend;
0520     *(little16_t *)FixupPtr = static_cast<int16_t>(Value);
0521     break;
0522   }
0523   case Sub32: {
0524     int64_t Value =
0525         support::endian::read32le(FixupPtr) - TargetAddress - Addend;
0526     *(little32_t *)FixupPtr = static_cast<int32_t>(Value);
0527     break;
0528   }
0529   case Sub64: {
0530     int64_t Value =
0531         support::endian::read64le(FixupPtr) - TargetAddress - Addend;
0532     *(little64_t *)FixupPtr = static_cast<int64_t>(Value);
0533     break;
0534   }
0535   case SubUleb128: {
0536     const uint32_t Maxcount = 1 + 64 / 7;
0537     uint32_t Count;
0538     const char *Error = nullptr;
0539     uint64_t Orig = decodeULEB128((reinterpret_cast<const uint8_t *>(FixupPtr)),
0540                                   &Count, nullptr, &Error);
0541 
0542     if (Count > Maxcount || (Count == Maxcount && Error))
0543       return make_error<JITLinkError>(
0544           "0x" + llvm::utohexstr(orc::ExecutorAddr(FixupAddress).getValue()) +
0545           ": extra space for uleb128");
0546 
0547     uint64_t Mask = Count < Maxcount ? (1ULL << 7 * Count) - 1 : -1ULL;
0548     encodeULEB128((Orig - TargetAddress - Addend) & Mask,
0549                   (reinterpret_cast<uint8_t *>(FixupPtr)), Count);
0550     break;
0551   }
0552   case AlignRelaxable:
0553     // Ignore when the relaxation pass did not run
0554     break;
0555   default:
0556     return make_error<JITLinkError>(
0557         "In graph " + G.getName() + ", section " + B.getSection().getName() +
0558         " unsupported edge kind " + getEdgeKindName(E.getKind()));
0559   }
0560 
0561   return Error::success();
0562 }
0563 
0564 /// loongarch null pointer content.
0565 extern const char NullPointerContent[8];
0566 inline ArrayRef<char> getGOTEntryBlockContent(LinkGraph &G) {
0567   return {reinterpret_cast<const char *>(NullPointerContent),
0568           G.getPointerSize()};
0569 }
0570 
0571 /// loongarch stub content.
0572 ///
0573 /// Contains the instruction sequence for an indirect jump via an in-memory
0574 /// pointer:
0575 ///   pcalau12i $t8, %page20(ptr)
0576 ///   ld.[w/d]  $t8, %pageoff12(ptr)
0577 ///   jr        $t8
0578 constexpr size_t StubEntrySize = 12;
0579 extern const uint8_t LA64StubContent[StubEntrySize];
0580 extern const uint8_t LA32StubContent[StubEntrySize];
0581 inline ArrayRef<char> getStubBlockContent(LinkGraph &G) {
0582   auto StubContent =
0583       G.getPointerSize() == 8 ? LA64StubContent : LA32StubContent;
0584   return {reinterpret_cast<const char *>(StubContent), StubEntrySize};
0585 }
0586 
0587 /// Creates a new pointer block in the given section and returns an
0588 /// Anonymous symbol pointing to it.
0589 ///
0590 /// If InitialTarget is given then an Pointer64 relocation will be added to the
0591 /// block pointing at InitialTarget.
0592 ///
0593 /// The pointer block will have the following default values:
0594 ///   alignment: PointerSize
0595 ///   alignment-offset: 0
0596 inline Symbol &createAnonymousPointer(LinkGraph &G, Section &PointerSection,
0597                                       Symbol *InitialTarget = nullptr,
0598                                       uint64_t InitialAddend = 0) {
0599   auto &B = G.createContentBlock(PointerSection, getGOTEntryBlockContent(G),
0600                                  orc::ExecutorAddr(), G.getPointerSize(), 0);
0601   if (InitialTarget)
0602     B.addEdge(G.getPointerSize() == 8 ? Pointer64 : Pointer32, 0,
0603               *InitialTarget, InitialAddend);
0604   return G.addAnonymousSymbol(B, 0, G.getPointerSize(), false, false);
0605 }
0606 
0607 /// Create a jump stub that jumps via the pointer at the given symbol and
0608 /// an anonymous symbol pointing to it. Return the anonymous symbol.
0609 inline Symbol &createAnonymousPointerJumpStub(LinkGraph &G,
0610                                               Section &StubSection,
0611                                               Symbol &PointerSymbol) {
0612   Block &StubContentBlock = G.createContentBlock(
0613       StubSection, getStubBlockContent(G), orc::ExecutorAddr(), 4, 0);
0614   StubContentBlock.addEdge(Page20, 0, PointerSymbol, 0);
0615   StubContentBlock.addEdge(PageOffset12, 4, PointerSymbol, 0);
0616   return G.addAnonymousSymbol(StubContentBlock, 0, StubEntrySize, true, false);
0617 }
0618 
0619 /// Global Offset Table Builder.
0620 class GOTTableManager : public TableManager<GOTTableManager> {
0621 public:
0622   static StringRef getSectionName() { return "$__GOT"; }
0623 
0624   bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
0625     Edge::Kind KindToSet = Edge::Invalid;
0626     switch (E.getKind()) {
0627     case RequestGOTAndTransformToPage20:
0628       KindToSet = Page20;
0629       break;
0630     case RequestGOTAndTransformToPageOffset12:
0631       KindToSet = PageOffset12;
0632       break;
0633     default:
0634       return false;
0635     }
0636     assert(KindToSet != Edge::Invalid &&
0637            "Fell through switch, but no new kind to set");
0638     DEBUG_WITH_TYPE("jitlink", {
0639       dbgs() << "  Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
0640              << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
0641              << formatv("{0:x}", E.getOffset()) << ")\n";
0642     });
0643     E.setKind(KindToSet);
0644     E.setTarget(getEntryForTarget(G, E.getTarget()));
0645     return true;
0646   }
0647 
0648   Symbol &createEntry(LinkGraph &G, Symbol &Target) {
0649     return createAnonymousPointer(G, getGOTSection(G), &Target);
0650   }
0651 
0652 private:
0653   Section &getGOTSection(LinkGraph &G) {
0654     if (!GOTSection)
0655       GOTSection = &G.createSection(getSectionName(),
0656                                     orc::MemProt::Read | orc::MemProt::Exec);
0657     return *GOTSection;
0658   }
0659 
0660   Section *GOTSection = nullptr;
0661 };
0662 
0663 /// Procedure Linkage Table Builder.
0664 class PLTTableManager : public TableManager<PLTTableManager> {
0665 public:
0666   PLTTableManager(GOTTableManager &GOT) : GOT(GOT) {}
0667 
0668   static StringRef getSectionName() { return "$__STUBS"; }
0669 
0670   bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
0671     if ((E.getKind() == Branch26PCRel || E.getKind() == Call36PCRel) &&
0672         !E.getTarget().isDefined()) {
0673       DEBUG_WITH_TYPE("jitlink", {
0674         dbgs() << "  Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
0675                << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
0676                << formatv("{0:x}", E.getOffset()) << ")\n";
0677       });
0678       E.setTarget(getEntryForTarget(G, E.getTarget()));
0679       return true;
0680     }
0681     return false;
0682   }
0683 
0684   Symbol &createEntry(LinkGraph &G, Symbol &Target) {
0685     return createAnonymousPointerJumpStub(G, getStubsSection(G),
0686                                           GOT.getEntryForTarget(G, Target));
0687   }
0688 
0689 public:
0690   Section &getStubsSection(LinkGraph &G) {
0691     if (!StubsSection)
0692       StubsSection = &G.createSection(getSectionName(),
0693                                       orc::MemProt::Read | orc::MemProt::Exec);
0694     return *StubsSection;
0695   }
0696 
0697   GOTTableManager &GOT;
0698   Section *StubsSection = nullptr;
0699 };
0700 
0701 } // namespace loongarch
0702 } // namespace jitlink
0703 } // namespace llvm
0704 
0705 #endif