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

via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 4 00:25:54 PDT 2026


================
@@ -133,6 +133,17 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {
   // times, reusing the same AST.
   Clang->getCodeGenOpts().ClearASTBeforeBackend = false;
 
+  // Clang emits direct PC-relative accesses for external data (e.g. C++
+  // type-info used by exception handling). As clang-repl directly mmap()s
+  // shared objects into memory, the target symbol may be more than 2GB away
+  // from the generated code, resulting in an out-of-range Delta32/PC32
+  // relocation. Force GOT-based access instead so the relocation remains
+  // within range.
+  //
+  // Unlike -fPIC, this does not define __PIC__ and remains compatible with
+  // precompiled headers.
+  Clang->getCodeGenOpts().DirectAccessExternalData = false;
----------------
aokblast wrote:

My understanding is that it uses the following pattern:

```
mov foo at GOTPCREL(%rip), %rax
mov (%rax), %eax
```

The origianl PC relative is:
```
lea foo(%rip), %rax
```

It definitely introduces some performance difference, but it is the correct way to handle R_*_32 relocations. In a static linker, simple cases are resolved directly, while others are handled using R_*_COPY or R_*_GLOB_DAT relocations, which can target symbols beyond the 2GB range.

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


More information about the cfe-commits mailing list