[lld] [lld][WebAssembly] Error on unexptected relocation types in `-pie/`-shared` data sections (PR #162117)

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 6 10:04:03 PDT 2025


https://github.com/sbc100 updated https://github.com/llvm/llvm-project/pull/162117

>From 978d10928833d9fbb9dbbb0235917811d6b93d04 Mon Sep 17 00:00:00 2001
From: Sam Clegg <sbc at chromium.org>
Date: Mon, 6 Oct 2025 09:33:18 -0700
Subject: [PATCH] [lld][WebAssembly] Error on unexptected relocation types in
 `-pie/`-shared` data sections

Most likely we do want to support R_WASM_FUNCTION_INDEX_I32 at some
point but this relocation types (along with many others) is not
currently supported by `InputChunk::generateRelocationCode`.

See #146923
---
 lld/test/wasm/bad-data-relocs.s | 27 +++++++++++++++++++++++++++
 lld/wasm/InputChunks.cpp        | 15 +++++++++++++--
 2 files changed, 40 insertions(+), 2 deletions(-)
 create mode 100644 lld/test/wasm/bad-data-relocs.s

diff --git a/lld/test/wasm/bad-data-relocs.s b/lld/test/wasm/bad-data-relocs.s
new file mode 100644
index 0000000000000..7e2ef3e1dc3b7
--- /dev/null
+++ b/lld/test/wasm/bad-data-relocs.s
@@ -0,0 +1,27 @@
+## Certain relocations types are not supported by runtime relocation code
+## generated in `-shared/`-pie` binaries.
+
+# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
+# RUN: not wasm-ld -pie --experimental-pic %t.o -o %t.wasm 2>&1 | FileCheck %s
+
+# CHECK: wasm-ld: error: invalid runtime relocation type in data section: R_WASM_FUNCTION_INDEX_I32
+
+foo:
+  .functype foo (i32) -> ()
+  end_function
+
+.globl _start
+_start:
+  .functype _start () -> ()
+  i32.const bar at GOT
+  call foo at GOT
+  end_function
+
+# data section containing relocation type that is not valid in a data section
+.section .data,"",@
+.globl bar
+bar:
+  .int32 0
+  .size  bar, 4
+
+.reloc bar, R_WASM_FUNCTION_INDEX_I32, foo
diff --git a/lld/wasm/InputChunks.cpp b/lld/wasm/InputChunks.cpp
index 181221a77b106..0e0c3b44e9eef 100644
--- a/lld/wasm/InputChunks.cpp
+++ b/lld/wasm/InputChunks.cpp
@@ -406,6 +406,15 @@ uint64_t InputChunk::getVA(uint64_t offset) const {
   return (outputSeg ? outputSeg->startVA : 0) + getChunkOffset(offset);
 }
 
+bool isValidRuntimeRelocation(WasmRelocType type) {
+  // TODO(https://github.com/llvm/llvm-project/issues/146923): Add
+  // R_WASM_FUNCTION_INDEX_I32 to this list
+  return type == R_WASM_TABLE_INDEX_I32 ||
+         type == R_WASM_TABLE_INDEX_I64 ||
+         type == R_WASM_MEMORY_ADDR_I32 ||
+         type == R_WASM_MEMORY_ADDR_I64;
+}
+
 // Generate code to apply relocations to the data section at runtime.
 // This is only called when generating shared libraries (PIC) where address are
 // not known at static link time.
@@ -424,8 +433,6 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
   // TODO(sbc): Encode the relocations in the data section and write a loop
   // here to apply them.
   for (const WasmRelocation &rel : relocations) {
-    uint64_t offset = getVA(rel.Offset) - getInputSectionOffset();
-
     Symbol *sym = file->getSymbol(rel);
     // Runtime relocations are needed when we don't know the address of
     // a symbol statically.
@@ -433,6 +440,10 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
     if (!requiresRuntimeReloc)
       continue;
 
+    if (!isValidRuntimeRelocation(rel.getType()))
+      error("invalid runtime relocation type in data section: " + relocTypetoString(rel.Type));
+
+    uint64_t offset = getVA(rel.Offset) - getInputSectionOffset();
     LLVM_DEBUG(dbgs() << "gen reloc: type=" << relocTypeToString(rel.Type)
                       << " addend=" << rel.Addend << " index=" << rel.Index
                       << " output offset=" << offset << "\n");



More information about the llvm-commits mailing list