From e4b6e686b70529db2c6424bd4094545836fdcd99 Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Mon, 17 Apr 2023 01:22:52 +0000 Subject: [PATCH] [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. --- llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp | 17 +++++++++++++++++ .../JITLink/x86-64/ELF_R_X86_64_GOTPC32.s | 18 ++++++++++++++++++ llvm/tools/llvm-jitlink/llvm-jitlink-elf.cpp | 5 +++++ 3 files changed, 40 insertions(+) create mode 100644 llvm/test/ExecutionEngine/JITLink/x86-64/ELF_R_X86_64_GOTPC32.s diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp index cab181f..e3cfe70 100644 --- a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp @@ -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 index 0000000..241bbb7 --- /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",@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 + diff --git a/llvm/tools/llvm-jitlink/llvm-jitlink-elf.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink-elf.cpp index 2b798f9..5200dbc 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()) - -- 2.7.4