Skip to content

Commit

Permalink
[JITLink][ELF][x86-64] Add support for R_X86_64_GOTPC32 relocation.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lhames committed Apr 17, 2023
1 parent b63b77a commit e4b6e68
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
17 changes: 17 additions & 0 deletions llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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();
}

Expand Down
18 changes: 18 additions & 0 deletions llvm/test/ExecutionEngine/JITLink/x86-64/ELF_R_X86_64_GOTPC32.s
Original file line number Diff line number Diff line change
@@ -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

5 changes: 5 additions & 0 deletions llvm/tools/llvm-jitlink/llvm-jitlink-elf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) -
Expand Down

0 comments on commit e4b6e68

Please sign in to comment.