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