[JITLink][ELF][x86-64] Add support for R_X86_64_GOTPC32 relocation.
authorLang Hames <lhames@gmail.com>
Mon, 17 Apr 2023 01:22:52 +0000 (01:22 +0000)
committerLang Hames <lhames@gmail.com>
Mon, 17 Apr 2023 01:45:01 +0000 (01:45 +0000)
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.

llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
llvm/test/ExecutionEngine/JITLink/x86-64/ELF_R_X86_64_GOTPC32.s [new file with mode: 0644]
llvm/tools/llvm-jitlink/llvm-jitlink-elf.cpp

index cab181f..e3cfe70 100644 (file)
@@ -155,6 +155,7 @@ private:
 
     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 @@ private:
                                 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 (file)
index 0000000..241bbb7
--- /dev/null
@@ -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",@progbits
+       .globl  main
+       .p2align        4, 0x90
+       .type   main,@function
+main:
+       leal    _GLOBAL_OFFSET_TABLE_(%rip), %ebx
+       xorl    %eax, %eax
+       retq
+.Lfunc_end0:
+       .size   main, .Lfunc_end0-main
+
index 2b798f9..5200dbc 100644 (file)
@@ -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()) -