[llvm] [PowerPC][JITLink] Support R_PPC64_GOT_PCREL34 (PR #68658)

Kai Luo via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 9 21:37:17 PDT 2023


https://github.com/bzEq created https://github.com/llvm/llvm-project/pull/68658

`R_PPC64_GOT_PCREL34` is generated for pwr10+.

>From f2ab75d423da56407e50227a41bd974ba5e20396 Mon Sep 17 00:00:00 2001
From: Kai Luo <lkail at cn.ibm.com>
Date: Tue, 10 Oct 2023 12:34:27 +0800
Subject: [PATCH] Support R_PPC64_GOT_PCREL34

---
 llvm/include/llvm/ExecutionEngine/JITLink/ppc64.h |  5 +++++
 llvm/lib/ExecutionEngine/JITLink/ELF_ppc64.cpp    |  6 ++++++
 llvm/lib/ExecutionEngine/JITLink/ppc64.cpp        |  2 ++
 .../JITLink/ppc64/ELF_ppc64_relocations.s         | 15 +++++++++++++++
 4 files changed, 28 insertions(+)

diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/ppc64.h b/llvm/include/llvm/ExecutionEngine/JITLink/ppc64.h
index 88af15d61e714e1..6d27841bff611e2 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/ppc64.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/ppc64.h
@@ -51,6 +51,7 @@ enum EdgeKind_ppc64 : Edge::Kind {
   TOCDelta16HI,
   TOCDelta16LO,
   TOCDelta16LODS,
+  RequestGOTAndTransformToDelta34,
   CallBranchDelta,
   // Need to restore r2 after the bl, suggesting the bl is followed by a nop.
   CallBranchDeltaRestoreTOC,
@@ -170,6 +171,10 @@ class TOCTableManager : public TableManager<TOCTableManager<Endianness>> {
       // Create TOC section if TOC relocation, PLT or GOT is used.
       getOrCreateTOCSection(G);
       return false;
+    case RequestGOTAndTransformToDelta34:
+      E.setKind(ppc64::Delta34);
+      E.setTarget(createEntry(G, E.getTarget()));
+      return true;
     default:
       return false;
     }
diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_ppc64.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_ppc64.cpp
index 5ce7a5bda840239..4818837e7cb796c 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF_ppc64.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_ppc64.cpp
@@ -234,6 +234,9 @@ class ELFLinkGraphBuilder_ppc64
     if (ELFReloc == ELF::R_PPC64_TLSLD)
       return make_error<StringError>("Local-dynamic TLS model is not supported",
                                      inconvertibleErrorCode());
+    if (ELFReloc == ELF::R_PPC64_PCREL_OPT)
+      // TODO: Support PCREL optimization, now ignore it.
+      return Error::success();
 
     auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
     if (!ObjSymbol)
@@ -360,6 +363,9 @@ class ELFLinkGraphBuilder_ppc64
     case ELF::R_PPC64_PCREL34:
       Kind = ppc64::Delta34;
       break;
+    case ELF::R_PPC64_GOT_PCREL34:
+      Kind = ppc64::RequestGOTAndTransformToDelta34;
+      break;
     case ELF::R_PPC64_GOT_TLSGD16_HA:
       Kind = ppc64::RequestTLSDescInGOTAndTransformToTOCDelta16HA;
       break;
diff --git a/llvm/lib/ExecutionEngine/JITLink/ppc64.cpp b/llvm/lib/ExecutionEngine/JITLink/ppc64.cpp
index b147ffc8dac2175..ac4a62a503919e2 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ppc64.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ppc64.cpp
@@ -120,6 +120,8 @@ const char *getEdgeKindName(Edge::Kind K) {
     return "TOCDelta16LO";
   case TOCDelta16LODS:
     return "TOCDelta16LODS";
+  case RequestGOTAndTransformToDelta34:
+    return "RequestGOTAndTransformToDelta34";
   case CallBranchDelta:
     return "CallBranchDelta";
   case CallBranchDeltaRestoreTOC:
diff --git a/llvm/test/ExecutionEngine/JITLink/ppc64/ELF_ppc64_relocations.s b/llvm/test/ExecutionEngine/JITLink/ppc64/ELF_ppc64_relocations.s
index 7e39a20ef6ab8b0..bcee29d1d34f60c 100644
--- a/llvm/test/ExecutionEngine/JITLink/ppc64/ELF_ppc64_relocations.s
+++ b/llvm/test/ExecutionEngine/JITLink/ppc64/ELF_ppc64_relocations.s
@@ -8,6 +8,7 @@
 # RUN:              --abs external_addr14_func=0x0880 \
 # RUN:              --abs external_addr16_data=0x6000 \
 # RUN:              --abs external_addr32_data=0x36668840 \
+# RUN:              --abs pcrel_external_var=0x36668860 \
 # RUN:              --check %s %t/elf_reloc.o
 # RUN: llvm-mc --triple=powerpc64-unknown-linux-gnu --filetype=obj -o \
 # RUN:   %t/elf_reloc.o %s
@@ -18,6 +19,7 @@
 # RUN:              --abs external_addr14_func=0x0880 \
 # RUN:              --abs external_addr16_data=0x6000 \
 # RUN:              --abs external_addr32_data=0x36668840 \
+# RUN:              --abs pcrel_external_var=0x36668860 \
 # RUN:              --check %s %t/elf_reloc.o
 
 # jitlink-check: section_addr(elf_reloc.o, $__GOT) + 0x8000 = __TOC__
@@ -240,6 +242,19 @@ reloc_rel16:
   blr
   .size reloc_rel16, .-reloc_rel16
 
+# Check R_PPC64_GOT_PCREL34
+# jitlink-check: (got_addr(elf_reloc.o, pcrel_external_var) - reloc_got_pcrel34)[33:0] = \
+# jitlink-check:   ((((*{4}(reloc_got_pcrel34)) & 0x3ffff) << 16) | ((*{4}(reloc_got_pcrel34 + 4)) & 0xffff))[33:0]
+  .global reloc_got_pcrel34
+  .p2align 4
+  .type reloc_got_pcrel34, at function
+reloc_got_pcrel34:
+  pld 3,pcrel_external_var at got@pcrel(0),1
+.Lpcrel0:
+  .reloc .Lpcrel0-8,R_PPC64_PCREL_OPT,.-(.Lpcrel0-8)
+  blr
+  .size reloc_got_pcrel34,.-reloc_got_pcrel34
+
   .type	.L.str, at object
 	.section	.rodata.str1.1,"aMS", at progbits,1
 .L.str:



More information about the llvm-commits mailing list