[lld] [lld][WebAssembly] Use writePtrConst helper function (PR #166228)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 3 12:19:39 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lld
Author: Sam Clegg (sbc100)
<details>
<summary>Changes</summary>
This is especially important for writing i32 values larger than 2gb which need to be encoded as negative SLEB vales in the binary.
Without this change offsets over 2gb are wrongly encoded and cause validation errors.
Fixes: https://github.com/emscripten-core/emscripten/issues/25706
---
Full diff: https://github.com/llvm/llvm-project/pull/166228.diff
2 Files Affected:
- (modified) lld/wasm/InputChunks.cpp (+3-10)
- (modified) lld/wasm/SyntheticSections.cpp (+2-6)
``````````diff
diff --git a/lld/wasm/InputChunks.cpp b/lld/wasm/InputChunks.cpp
index 44927e7a432bc..a08e80dcae1c8 100644
--- a/lld/wasm/InputChunks.cpp
+++ b/lld/wasm/InputChunks.cpp
@@ -423,8 +423,6 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
bool is64 = ctx.arg.is64.value_or(false);
bool generated = false;
- unsigned opcode_ptr_const = is64 ? WASM_OPCODE_I64_CONST
- : WASM_OPCODE_I32_CONST;
unsigned opcode_ptr_add = is64 ? WASM_OPCODE_I64_ADD
: WASM_OPCODE_I32_ADD;
@@ -451,8 +449,7 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
<< " output offset=" << offset << "\n");
// Calculate the address at which to apply the relocation
- writeU8(os, opcode_ptr_const, "CONST");
- writeSleb128(os, offset, "offset");
+ writePtrConst(os, offset, is64, "offset");
// In PIC mode we need to add the __memory_base
if (ctx.isPic) {
@@ -466,8 +463,6 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
// Now figure out what we want to store at this location
bool is64 = relocIs64(rel.Type);
- unsigned opcode_reloc_const =
- is64 ? WASM_OPCODE_I64_CONST : WASM_OPCODE_I32_CONST;
unsigned opcode_reloc_add =
is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD;
unsigned opcode_reloc_store =
@@ -477,8 +472,7 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
writeUleb128(os, sym->getGOTIndex(), "global index");
if (rel.Addend) {
- writeU8(os, opcode_reloc_const, "CONST");
- writeSleb128(os, rel.Addend, "addend");
+ writePtrConst(os, rel.Addend, is64, "addend");
writeU8(os, opcode_reloc_add, "ADD");
}
} else {
@@ -491,8 +485,7 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
baseSymbol = ctx.sym.tlsBase;
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
writeUleb128(os, baseSymbol->getGlobalIndex(), "base");
- writeU8(os, opcode_reloc_const, "CONST");
- writeSleb128(os, file->calcNewValue(rel, tombstone, this), "offset");
+ writePtrConst(os, file->calcNewValue(rel, tombstone, this), is64, "offset");
writeU8(os, opcode_reloc_add, "ADD");
}
diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp
index e1192706ea913..399a5084e6595 100644
--- a/lld/wasm/SyntheticSections.cpp
+++ b/lld/wasm/SyntheticSections.cpp
@@ -434,8 +434,6 @@ void GlobalSection::addInternalGOTEntry(Symbol *sym) {
void GlobalSection::generateRelocationCode(raw_ostream &os, bool TLS) const {
assert(!ctx.arg.extendedConst);
bool is64 = ctx.arg.is64.value_or(false);
- unsigned opcode_ptr_const = is64 ? WASM_OPCODE_I64_CONST
- : WASM_OPCODE_I32_CONST;
unsigned opcode_ptr_add = is64 ? WASM_OPCODE_I64_ADD
: WASM_OPCODE_I32_ADD;
@@ -452,8 +450,7 @@ void GlobalSection::generateRelocationCode(raw_ostream &os, bool TLS) const {
writeUleb128(os, ctx.sym.memoryBase->getGlobalIndex(), "__memory_base");
// Add the virtual address of the data symbol
- writeU8(os, opcode_ptr_const, "CONST");
- writeSleb128(os, d->getVA(), "offset");
+ writePtrConst(os, d->getVA(), is64, "offset");
} else if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
if (f->isStub)
continue;
@@ -462,8 +459,7 @@ void GlobalSection::generateRelocationCode(raw_ostream &os, bool TLS) const {
writeUleb128(os, ctx.sym.tableBase->getGlobalIndex(), "__table_base");
// Add the table index to __table_base
- writeU8(os, opcode_ptr_const, "CONST");
- writeSleb128(os, f->getTableIndex(), "offset");
+ writePtrConst(os, f->getTableIndex(), is64, "offset");
} else {
assert(isa<UndefinedData>(sym) || isa<SharedData>(sym));
continue;
``````````
</details>
https://github.com/llvm/llvm-project/pull/166228
More information about the llvm-commits
mailing list