authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-10 19:31:14-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:36-08:00
log7b255235d60cb2891ec628506f4d5e8ca811a865
tree5184e5f869d1cc51b218df74c1b18f0ce7f85dae
parente44bafe5fff4a80bb4d69fa88f0242ffecf2a252

wasm linker: fix off-by-one in function table indexes


2 files changed, 11 insertions(+), 7 deletions(-)

src/arch/wasm/Emit.zig+1-1
......@@ -81,7 +81,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
8181 if (is_obj) {
8282 @panic("TODO");
8383 } else {
84 leb.writeUleb128(code.fixedWriter(), @intFromEnum(datas[inst].indirect_function_table_index)) catch unreachable;
84 leb.writeUleb128(code.fixedWriter(), 1 + @intFromEnum(datas[inst].indirect_function_table_index)) catch unreachable;
8585 }
8686 inst += 1;
8787 continue :loop tags[inst];
src/link/Wasm/Flush.zig+10-6
......@@ -59,6 +59,10 @@ const IndirectFunctionTableIndex = enum(u32) {
5959 // These are the same since those are added to the table first.
6060 return @enumFromInt(@intFromEnum(i));
6161 }
62
63 fn toAbi(i: IndirectFunctionTableIndex) u32 {
64 return @intFromEnum(i) + 1;
65 }
6266};
6367
6468const DataSegmentGroup = struct {
......@@ -777,9 +781,9 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
777781 for (wasm.func_table_fixups.items) |fixup| {
778782 const table_index: IndirectFunctionTableIndex = .fromZcuIndirectFunctionSetIndex(fixup.table_index);
779783 if (!is64) {
780 mem.writeInt(u32, wasm.string_bytes.items[fixup.offset..][0..4], @intFromEnum(table_index), .little);
784 mem.writeInt(u32, wasm.string_bytes.items[fixup.offset..][0..4], table_index.toAbi(), .little);
781785 } else {
782 mem.writeInt(u64, wasm.string_bytes.items[fixup.offset..][0..8], @intFromEnum(table_index), .little);
786 mem.writeInt(u64, wasm.string_bytes.items[fixup.offset..][0..8], table_index.toAbi(), .little);
783787 }
784788 }
785789 }
......@@ -1582,19 +1586,19 @@ fn applyRelocs(code: []u8, code_offset: u32, relocs: Wasm.ObjectRelocation.Itera
15821586}
15831587
15841588fn reloc_u32_table_index(code: []u8, i: IndirectFunctionTableIndex) void {
1585 mem.writeInt(u32, code[0..4], @intFromEnum(i), .little);
1589 mem.writeInt(u32, code[0..4], i.toAbi(), .little);
15861590}
15871591
15881592fn reloc_u64_table_index(code: []u8, i: IndirectFunctionTableIndex) void {
1589 mem.writeInt(u64, code[0..8], @intFromEnum(i), .little);
1593 mem.writeInt(u64, code[0..8], i.toAbi(), .little);
15901594}
15911595
15921596fn reloc_sleb_table_index(code: []u8, i: IndirectFunctionTableIndex) void {
1593 leb.writeSignedFixed(5, code[0..5], @intFromEnum(i));
1597 leb.writeSignedFixed(5, code[0..5], i.toAbi());
15941598}
15951599
15961600fn reloc_sleb64_table_index(code: []u8, i: IndirectFunctionTableIndex) void {
1597 leb.writeSignedFixed(11, code[0..11], @intFromEnum(i));
1601 leb.writeSignedFixed(11, code[0..11], i.toAbi());
15981602}
15991603
16001604fn reloc_u32_function(code: []u8, function: Wasm.OutputFunctionIndex) void {