[llvm] [JITLink][LoongArch] Ignore R_LARCH_RELAX and check R_LARCH_ALIGN (PR #121097)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 25 01:18:10 PST 2024


https://github.com/zhaoqi5 created https://github.com/llvm/llvm-project/pull/121097

Linker relaxation is not implemented for jitlink now (maybe implement in the future). But if relaxation is enabled by clang, R_LARCH_RELAX and R_LARCH_ALIGN relocations will be emitted. So we just ignore linker relaxation and check the alignment now.

If not, interpreting C++ code using clang-repl when relaxation is enabled will occur error: `JIT session error: Unsupported loongarch relocation:102: R_LARCH_ALIGN`.

Similar to: https://github.com/llvm/llvm-project/commit/f5b5398ebf7da1ab54f6793331bca8fbc3340f14.

>From 65847de8c305ca2c9a4a790c0f7887cc183e6e35 Mon Sep 17 00:00:00 2001
From: Qi Zhao <zhaoqi01 at loongson.cn>
Date: Wed, 25 Dec 2024 16:27:31 +0800
Subject: [PATCH] [JITLink][LoongArch] Ignore R_LARCH_RELAX and check
 R_LARCH_ALIGN

Linker relaxation is not implemented for jitlink now (maybe
implement in the future). But if relaxation is enabled by clang,
R_LARCH_RELAX and R_LARCH_ALIGN relocations will be emitted.
So we just ignore linker relaxation and check the alignment now.

If not, interpreting C++ code using clang-repl when relaxation
is enabled will occur error: `JIT session error: Unsupported
loongarch relocation:102: R_LARCH_ALIGN`.

Similar to: https://github.com/llvm/llvm-project/commit/f5b5398ebf7da1ab54f6793331bca8fbc3340f14.
---
 .../ExecutionEngine/JITLink/ELF_loongarch.cpp | 30 +++++++++++++++----
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_loongarch.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_loongarch.cpp
index 56c32aeecf55a8..1f4e34f41ab2b5 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF_loongarch.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_loongarch.cpp
@@ -95,6 +95,30 @@ class ELFLinkGraphBuilder_loongarch : public ELFLinkGraphBuilder<ELFT> {
                             Block &BlockToFix) {
     using Base = ELFLinkGraphBuilder<ELFT>;
 
+    uint32_t Type = Rel.getType(false);
+    // We do not implement linker relaxation for jitlink now, except what is
+    // required for alignment (see below).
+    if (Type == ELF::R_LARCH_RELAX)
+      return Error::success();
+
+    int64_t Addend = Rel.r_addend;
+    if (Type == ELF::R_LARCH_ALIGN) {
+      uint64_t Alignment = PowerOf2Ceil(Addend);
+      // FIXME: Implement support for ensuring alignment together with linker
+      // relaxation. Addend is always 28 in the most common case when
+      // interpreting C++ code in clang-repl.
+      if (Alignment > 32)
+        return make_error<JITLinkError>(
+            formatv("Unsupported relocation R_LARCH_ALIGN with alignment {0} "
+                    "larger than 32 (addend: {1})",
+                    Alignment, Addend));
+      return Error::success();
+    }
+
+    Expected<loongarch::EdgeKind_loongarch> Kind = getRelocationKind(Type);
+    if (!Kind)
+      return Kind.takeError();
+
     uint32_t SymbolIndex = Rel.getSymbol(false);
     auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
     if (!ObjSymbol)
@@ -109,12 +133,6 @@ class ELFLinkGraphBuilder_loongarch : public ELFLinkGraphBuilder<ELFT> {
                   Base::GraphSymbols.size()),
           inconvertibleErrorCode());
 
-    uint32_t Type = Rel.getType(false);
-    Expected<loongarch::EdgeKind_loongarch> Kind = getRelocationKind(Type);
-    if (!Kind)
-      return Kind.takeError();
-
-    int64_t Addend = Rel.r_addend;
     auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset;
     Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
     Edge GE(*Kind, Offset, *GraphSymbol, Addend);



More information about the llvm-commits mailing list