[clang] [Clang][Interpreter] Force GOT access for external data references (PR #201286)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 4 01:41:26 PDT 2026


aokblast wrote:

> > In clang-repl, R_*_32 relocations are emitted as direct 32-bit PC-relative references. A normal static linker can resolve references to distinct symbol through the GOT or Copy relocation, but clang-repl directly mmaps shared objects into memory and bypasses the static linker.
> 
> Can you be more specific about the relocations that you're seeing?
> 
> CodeGen usually requests a GOT entry (by selecting one of the GOT relocations and producing the necessary instruction sequence), and the linker just builds the requested GOT entry.
> 
> The usual caveat is hidden symbols:
> 
> ```c
> extern int __attribute__((visibility("hidden"))) X;
> int getX() { return X; } // <- will typically emit 32-bit PC-relative reference
> ```
> 
> The problem in this case is that ClangRepl probably isn't doing slab allocation by default, in which case switching to a slab-based allocator is the best answer.

GOT is only generated for relocatable code when -fPIC is enabled. This is not the default in ClangRepl. I previously tried building relocatable objects in ClangRepl with -fPIC, but encountered a test failure.

The issue is that ClangRepl can consume PCH (precompiled headers) where the headers are compiled normally (i.e., without -fPIC). This leads to a mismatch in PIC levels between objects, causing assertion failures.

I can share the failing test case that triggered this patch on FreeBSD.

Here is my experiment:

```
blast at linux-dev ~/p/cpp> cvat ^C
blast at linux-dev ~/p/cpp> cat test.cpp
extern int X;
int getX() { return X; } 
blast at linux-dev ~/p/cpp> cc test.cpp -c -o test.o
blast at linux-dev ~/p/cpp> readelf --relocs test.o

Relocation section '.rela.text' at offset 0x168 contains 1 entry:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
00000000000a  000400000002 R_X86_64_PC32     0000000000000000 X - 4

Relocation section '.rela.eh_frame' at offset 0x180 contains 1 entry:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000000020  000200000002 R_X86_64_PC32     0000000000000000 .text + 0
blast at linux-dev ~/p/cpp> cc -fPIC test.cpp -c -o test.o
blast at linux-dev ~/p/cpp> readelf --relocs test.o

Relocation section '.rela.text' at offset 0x1a0 contains 1 entry:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
00000000000b  00050000002a R_X86_64_REX_GOTP 0000000000000000 X - 4

Relocation section '.rela.eh_frame' at offset 0x1b8 contains 1 entry:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000000020  000200000002 R_X86_64_PC32     0000000000000000 .text + 0
blast at linux-dev ~/p/cpp> uname -a
Linux linux-dev 6.8.12-13-pve #1 SMP PREEMPT_DYNAMIC PMX 6.8.12-13 (2025-07-22T10:00Z) x86_64 x86_64 x86_64 GNU/Linux
``` 


https://github.com/llvm/llvm-project/pull/201286


More information about the cfe-commits mailing list