[llvm] e4b6e68 - [JITLink][ELF][x86-64] Add support for R_X86_64_GOTPC32 relocation.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 16 18:45:08 PDT 2023


Author: Lang Hames
Date: 2023-04-17T01:45:01Z
New Revision: e4b6e686b70529db2c6424bd4094545836fdcd99

URL: https://github.com/llvm/llvm-project/commit/e4b6e686b70529db2c6424bd4094545836fdcd99
DIFF: https://github.com/llvm/llvm-project/commit/e4b6e686b70529db2c6424bd4094545836fdcd99.diff

LOG: [JITLink][ELF][x86-64] Add support for R_X86_64_GOTPC32 relocation.

Adds support for the R_X86_64_GOTPC32 relocation, which is a 32-bit delta to
the global offset table.

Since the delta to the GOT doesn't actually require any GOT entries to exist
this commit adds an extra fallback path to the getOrCreateGOTSymbol function:
If the symbol is in the extenal symbols list but no entry exists then the
symbol is turned into an absolute symbol pointing to an arbitrary address in
the current graph's allocation (accessing this address via the symbol would be
illegal, but any access should have triggered creation of a GOT entry which
would prevent this fallback path from being taken in the first place).

This commit also updates the llvm-jitlink tool to scrape the addresses of the
absolute symbols in the graph so that the testcase can see the now-absolute
_GLOBAL_OFFSET_TABLE_ symbol.

Added: 
    llvm/test/ExecutionEngine/JITLink/x86-64/ELF_R_X86_64_GOTPC32.s

Modified: 
    llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
    llvm/tools/llvm-jitlink/llvm-jitlink-elf.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
index cab181f2f30dd..e3cfe7040a13c 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
@@ -155,6 +155,7 @@ class ELFLinkGraphBuilder_x86_64 : public ELFLinkGraphBuilder<object::ELF64LE> {
 
     switch (ELFReloc) {
     case ELF::R_X86_64_PC32:
+    case ELF::R_X86_64_GOTPC32:
       Kind = x86_64::Delta32;
       break;
     case ELF::R_X86_64_PC64:
@@ -293,6 +294,22 @@ class ELFJITLinker_x86_64 : public JITLinker<ELFJITLinker_x86_64> {
                                 Linkage::Strong, Scope::Local, false, true);
     }
 
+    // If we still haven't found a GOT symbol then double check the externals.
+    // We may have a GOT-relative reference but no GOT section, in which case
+    // we just need to point the GOT symbol at some address in this graph.
+    if (!GOTSymbol) {
+      for (auto *Sym : G.external_symbols()) {
+        if (Sym->getName() == ELFGOTSymbolName) {
+          auto Blocks = G.blocks();
+          if (!Blocks.empty()) {
+            G.makeAbsolute(*Sym, (*Blocks.begin())->getAddress());
+            GOTSymbol = Sym;
+            break;
+          }
+        }
+      }
+    }
+
     return Error::success();
   }
 

diff  --git a/llvm/test/ExecutionEngine/JITLink/x86-64/ELF_R_X86_64_GOTPC32.s b/llvm/test/ExecutionEngine/JITLink/x86-64/ELF_R_X86_64_GOTPC32.s
new file mode 100644
index 0000000000000..241bbb7b85187
--- /dev/null
+++ b/llvm/test/ExecutionEngine/JITLink/x86-64/ELF_R_X86_64_GOTPC32.s
@@ -0,0 +1,18 @@
+# RUN: llvm-mc -triple=x86_64-unknown-linux -position-independent \
+# RUN:     -filetype=obj -o %t.o %s
+# RUN: llvm-jitlink -noexec -check=%s %t.o
+
+# jitlink-check: decode_operand(main, 4) = _GLOBAL_OFFSET_TABLE_ - next_pc(main)
+
+	.text
+	.section	.text.main,"ax", at progbits
+	.globl	main
+	.p2align	4, 0x90
+	.type	main, at function
+main:
+	leal    _GLOBAL_OFFSET_TABLE_(%rip), %ebx
+	xorl	%eax, %eax
+	retq
+.Lfunc_end0:
+	.size	main, .Lfunc_end0-main
+

diff  --git a/llvm/tools/llvm-jitlink/llvm-jitlink-elf.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink-elf.cpp
index 2b798f95edb25..5200dbcf90366 100644
--- a/llvm/tools/llvm-jitlink/llvm-jitlink-elf.cpp
+++ b/llvm/tools/llvm-jitlink/llvm-jitlink-elf.cpp
@@ -151,6 +151,11 @@ Error registerELFGraphInfo(Session &S, LinkGraph &G) {
       }
     }
 
+    // Add symbol info for absolute symbols.
+    for (auto *Sym : G.absolute_symbols())
+      S.SymbolInfos[Sym->getName()] = {Sym->getSize(),
+                                       Sym->getAddress().getValue()};
+
     auto SecAddr = FirstSym->getAddress();
     auto SecSize =
         (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -


        


More information about the llvm-commits mailing list