[lld] [lld][WebAssembly] Handle locally bound relative data relocations in PIC (PR #230805)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 10 08:27:49 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lld
Author: Anutosh Bhat (anutosh491)
<details>
<summary>Changes</summary>
I found this while working on [WasmBolt](https://discourse.llvm.org/t/rfc-embeddable-llvm-tool-drivers-for-long-lived-hosts/91754)
Here's a small C++ example using relative vtables:
```cpp
struct Base {
virtual int value();
};
int Base::value() { return 42; }
Base instance;
```
```bash
clang++ --target=wasm32-unknown-unknown -fPIC \
-fexperimental-relative-c++-abi-vtables -c test.cpp -o test.o
wasm-ld -shared --experimental-pic --allow-undefined \
--no-gc-sections test.o -o test.wasm
```
This fails with:
```text
invalid runtime relocation type in data section: R_WASM_MEMORY_ADDR_LOCREL_I32
```
The vtable stores an offset to its RTTI proxy. Both addresses move by the same amount when the module is loaded, so their difference stays the same. However, LLD treats this as needing a runtime relocation and rejects it.
This keeps locally bound relative data references resolved at link time, with wasm32 and wasm64 regression coverage.
---
Full diff: https://github.com/llvm/llvm-project/pull/230805.diff
2 Files Affected:
- (added) lld/test/wasm/reloc-relative-pic.s (+70)
- (modified) lld/wasm/InputChunks.cpp (+9)
``````````diff
diff --git a/lld/test/wasm/reloc-relative-pic.s b/lld/test/wasm/reloc-relative-pic.s
new file mode 100644
index 0000000000000..927d35e11b798
--- /dev/null
+++ b/lld/test/wasm/reloc-relative-pic.s
@@ -0,0 +1,70 @@
+# RUN: sed -e '/^\.globl target/d' -e '/^\.hidden target/d' %s | llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.local32.o
+# RUN: sed -e '/^\.globl target/d' -e '/^\.hidden target/d' %s | llvm-mc -filetype=obj -triple=wasm64-unknown-unknown -o %t.local64.o
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %s -o %t.hidden32.o
+# RUN: llvm-mc -filetype=obj -triple=wasm64-unknown-unknown %s -o %t.hidden64.o
+# RUN: sed '/^\.hidden target/d' %s | llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.global32.o
+# RUN: sed '/^\.hidden target/d' %s | llvm-mc -filetype=obj -triple=wasm64-unknown-unknown -o %t.global64.o
+# RUN: wasm-ld -shared --experimental-pic --no-gc-sections %t.local32.o -o %t.local32.wasm
+# RUN: wasm-ld -shared --experimental-pic --no-gc-sections -mwasm64 %t.local64.o -o %t.local64.wasm
+# RUN: wasm-ld -shared --experimental-pic --no-gc-sections %t.hidden32.o -o %t.hidden32.wasm
+# RUN: wasm-ld -shared --experimental-pic --no-gc-sections -mwasm64 %t.hidden64.o -o %t.hidden64.wasm
+# RUN: wasm-ld -shared -Bsymbolic --experimental-pic --no-gc-sections %t.global32.o -o %t.global32.wasm
+# RUN: wasm-ld -shared -Bsymbolic --experimental-pic --no-gc-sections -mwasm64 %t.global64.o -o %t.global64.wasm
+# RUN: wasm-ld -pie --no-entry --experimental-pic --no-gc-sections %t.global32.o -o %t.pie32.wasm
+# RUN: wasm-ld -pie --no-entry --experimental-pic --no-gc-sections -mwasm64 %t.global64.o -o %t.pie64.wasm
+# RUN: obj2yaml %t.local32.wasm | FileCheck %s
+# RUN: obj2yaml %t.local64.wasm | FileCheck %s
+# RUN: obj2yaml %t.hidden32.wasm | FileCheck %s
+# RUN: obj2yaml %t.hidden64.wasm | FileCheck %s
+# RUN: obj2yaml %t.global32.wasm | FileCheck %s
+# RUN: obj2yaml %t.global64.wasm | FileCheck %s
+# RUN: obj2yaml %t.pie32.wasm | FileCheck %s
+# RUN: obj2yaml %t.pie64.wasm | FileCheck %s
+# RUN: llvm-objdump -d %t.hidden32.wasm | FileCheck %s --check-prefix=NO-RUNTIME
+# RUN: llvm-objdump -d %t.hidden64.wasm | FileCheck %s --check-prefix=NO-RUNTIME
+
+## A preemptible target may live in another module, so its load base does not
+## necessarily cancel. Keep rejecting unsupported runtime relocations.
+# RUN: not wasm-ld -shared --experimental-pic --no-gc-sections %t.global32.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERROR
+# RUN: not wasm-ld -shared --experimental-pic --no-gc-sections -mwasm64 %t.global64.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERROR
+
+## Absolute symbols do not share the module's load base either.
+# RUN: sed 's/target-relative/__wasm_first_page_end-relative/g' %s | llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.absolute32.o
+# RUN: sed 's/target-relative/__wasm_first_page_end-relative/g' %s | llvm-mc -filetype=obj -triple=wasm64-unknown-unknown -o %t.absolute64.o
+# RUN: not wasm-ld -shared --experimental-pic --no-gc-sections %t.absolute32.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERROR
+# RUN: not wasm-ld -shared --experimental-pic --no-gc-sections -mwasm64 %t.absolute64.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERROR
+
+.section .data.target,"",@
+.globl target
+.hidden target
+target:
+ .int32 42
+.size target, 4
+
+.section .data.relative,"",@
+.globl relative32
+relative32:
+ .int32 target-relative32
+.size relative32, 4
+.globl relative64
+relative64:
+ .int64 target-relative64
+.size relative64, 8
+.globl relative32_addend
+relative32_addend:
+ .int32 target-relative32_addend+8
+.size relative32_addend, 4
+.globl relative64_addend
+relative64_addend:
+ .int64 target-relative64_addend+16
+.size relative64_addend, 8
+
+## The target and both references share the same load base. Their differences
+## (-4 and -8), including nonzero addends, are resolved statically for both
+## wasm32 and wasm64.
+# CHECK: - Type: DATA
+# CHECK: Content: {{'?}}2A000000FCFFFFFFF8FFFFFFFFFFFFFFF8FFFFFFFCFFFFFFFFFFFFFF{{'?}}
+# NO-RUNTIME-NOT: i32.store
+# NO-RUNTIME-NOT: i64.store
+# ERROR: invalid runtime relocation type in data section: R_WASM_MEMORY_ADDR_LOCREL_I32
+# ERROR: invalid runtime relocation type in data section: R_WASM_MEMORY_ADDR_LOCREL_I64
diff --git a/lld/wasm/InputChunks.cpp b/lld/wasm/InputChunks.cpp
index 605d49f08fde7..2114217179550 100644
--- a/lld/wasm/InputChunks.cpp
+++ b/lld/wasm/InputChunks.cpp
@@ -450,6 +450,15 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
// here to apply them.
for (const WasmRelocation &rel : relocations) {
Symbol *sym = file->getSymbol(rel);
+ // For locally bound location-relative references, the load base cancels
+ // between the symbol and the relocation location. The static relocation
+ // already contains their difference.
+ if ((rel.Type == R_WASM_MEMORY_ADDR_LOCREL_I32 ||
+ rel.Type == R_WASM_MEMORY_ADDR_LOCREL_I64) &&
+ sym->isDefined() && sym->getChunk() && !sym->isTLS() && !isTLS() &&
+ (sym->isHidden() || sym->isLocal() || !ctx.arg.shared ||
+ ctx.arg.bsymbolic))
+ continue;
// Runtime relocations are needed when we don't know the address of
// a symbol statically.
bool requiresRuntimeReloc = ctx.isPic || sym->hasGOTIndex();
``````````
</details>
https://github.com/llvm/llvm-project/pull/230805
More information about the llvm-commits
mailing list