aboutsummaryrefslogtreecommitdiff
path: root/src/arch
diff options
context:
space:
mode:
authorLuuk de Gram <luuk@degram.dev>2024-02-05 17:17:07 +0100
committerLuuk de Gram <luuk@degram.dev>2024-02-29 15:23:05 +0100
commit5aec88fa4102e87295bf60971209d114c6ae6733 (patch)
treeaa4a533cf831d6bb93f7b647b5d253059e20a29e /src/arch
parent5a0f2af7e4aa01f861d86bfe9fb457ffde3d335e (diff)
downloadzig-5aec88fa4102e87295bf60971209d114c6ae6733.tar.gz
zig-5aec88fa4102e87295bf60971209d114c6ae6733.zip
wasm: correctly generate relocations for type index
Previously we could directly write the type index because we used the index that was known in the final binary. However, as we now process the Zig module as its own relocatable object file, we must ensure to generate a relocation for type indexes. This also ensures that we can later link the relocatable object file as a standalone also. This also fixes generating indirect function table entries for ZigObject as it now correctly points to the relocation symbol index rather than the symbol index that owns the relocation.
Diffstat (limited to 'src/arch')
-rw-r--r--src/arch/wasm/Emit.zig14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/arch/wasm/Emit.zig b/src/arch/wasm/Emit.zig
index 31c15ce0ef..ded960daf1 100644
--- a/src/arch/wasm/Emit.zig
+++ b/src/arch/wasm/Emit.zig
@@ -385,7 +385,19 @@ fn emitCallIndirect(emit: *Emit, inst: Mir.Inst.Index) !void {
try emit.code.append(std.wasm.opcode(.call_indirect));
// NOTE: If we remove unused function types in the future for incremental
// linking, we must also emit a relocation for this `type_index`
- try leb128.writeULEB128(emit.code.writer(), type_index);
+ const call_offset = emit.offset();
+ var buf: [5]u8 = undefined;
+ leb128.writeUnsignedFixed(5, &buf, type_index);
+ try emit.code.appendSlice(&buf);
+ if (type_index != 0) {
+ const atom_index = emit.bin_file.zigObjectPtr().?.decls_map.get(emit.decl_index).?.atom;
+ const atom = emit.bin_file.getAtomPtr(atom_index);
+ try atom.relocs.append(emit.bin_file.base.comp.gpa, .{
+ .offset = call_offset,
+ .index = type_index,
+ .relocation_type = .R_WASM_TYPE_INDEX_LEB,
+ });
+ }
try leb128.writeULEB128(emit.code.writer(), @as(u32, 0)); // TODO: Emit relocation for table index
}