authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-09 18:40:01-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:36-08:00
log1c4b4fb51604f388bb4355e566aac4ea9fda8960
tree65ec97db826dce98ce9bfa63287691ff913c6310
parentd9d49ce995c555286ba4f6041bcca12477f37296

implement indirect function table for object functions


4 files changed, 91 insertions(+), 27 deletions(-)

src/arch/wasm/CodeGen.zig+2-2
......@@ -1027,7 +1027,7 @@ fn emitWValue(cg: *CodeGen, value: WValue) InnerError!void {
10271027 const ip = &zcu.intern_pool;
10281028 if (ip.getNav(nav_ref.nav_index).isExternOrFn(ip)) {
10291029 assert(nav_ref.offset == 0);
1030 const gop = try wasm.indirect_function_table.getOrPut(comp.gpa, nav_ref.nav_index);
1030 const gop = try wasm.zcu_indirect_function_set.getOrPut(comp.gpa, nav_ref.nav_index);
10311031 if (!gop.found_existing) gop.value_ptr.* = {};
10321032 try cg.addInst(.{
10331033 .tag = .func_ref,
......@@ -1056,7 +1056,7 @@ fn emitWValue(cg: *CodeGen, value: WValue) InnerError!void {
10561056 if (ip.isFunctionType(ip.typeOf(uav.ip_index))) {
10571057 assert(uav.offset == 0);
10581058 const owner_nav = ip.toFunc(uav.ip_index).owner_nav;
1059 const gop = try wasm.indirect_function_table.getOrPut(comp.gpa, owner_nav);
1059 const gop = try wasm.zcu_indirect_function_set.getOrPut(comp.gpa, owner_nav);
10601060 if (!gop.found_existing) gop.value_ptr.* = {};
10611061 try cg.addInst(.{
10621062 .tag = .func_ref,
src/arch/wasm/Mir.zig+1-1
......@@ -615,7 +615,7 @@ pub const Inst = struct {
615615 intrinsic: Intrinsic,
616616 uav_obj: Wasm.UavsObjIndex,
617617 uav_exe: Wasm.UavsExeIndex,
618 indirect_function_table_index: Wasm.IndirectFunctionTableIndex,
618 indirect_function_table_index: Wasm.ZcuIndirectFunctionSetIndex,
619619
620620 comptime {
621621 switch (builtin.mode) {
src/link/Wasm.zig+24-7
......@@ -260,7 +260,9 @@ table_imports: std.AutoArrayHashMapUnmanaged(String, TableImport.Index) = .empty
260260
261261/// All functions that have had their address taken and therefore might be
262262/// called via a `call_indirect` function.
263indirect_function_table: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, void) = .empty,
263zcu_indirect_function_set: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, void) = .empty,
264object_indirect_function_import_set: std.AutoArrayHashMapUnmanaged(String, void) = .empty,
265object_indirect_function_set: std.AutoArrayHashMapUnmanaged(ObjectFunctionIndex, void) = .empty,
264266
265267error_name_table_ref_count: u32 = 0,
266268
......@@ -288,8 +290,8 @@ error_name_bytes: std.ArrayListUnmanaged(u8) = .empty,
288290/// is stored. No need to serialize; trivially reconstructed.
289291error_name_offs: std.ArrayListUnmanaged(u32) = .empty,
290292
291/// Index into `Wasm.indirect_function_table`.
292pub const IndirectFunctionTableIndex = enum(u32) {
293/// Index into `Wasm.zcu_indirect_function_set`.
294pub const ZcuIndirectFunctionSetIndex = enum(u32) {
293295 _,
294296};
295297
......@@ -1312,8 +1314,8 @@ pub const TableImport = extern struct {
13121314 .unresolved => unreachable,
13131315 .__indirect_function_table => .{
13141316 .flags = .{ .has_max = true, .is_shared = false },
1315 .min = @intCast(wasm.indirect_function_table.entries.len + 1),
1316 .max = @intCast(wasm.indirect_function_table.entries.len + 1),
1317 .min = @intCast(wasm.flush_buffer.indirect_function_table.entries.len + 1),
1318 .max = @intCast(wasm.flush_buffer.indirect_function_table.entries.len + 1),
13171319 },
13181320 .object_table => |i| i.ptr(wasm).limits(),
13191321 };
......@@ -3025,7 +3027,10 @@ pub fn deinit(wasm: *Wasm) void {
30253027 wasm.out_relocs.deinit(gpa);
30263028 wasm.uav_fixups.deinit(gpa);
30273029 wasm.nav_fixups.deinit(gpa);
3028 wasm.indirect_function_table.deinit(gpa);
3030
3031 wasm.zcu_indirect_function_set.deinit(gpa);
3032 wasm.object_indirect_function_import_set.deinit(gpa);
3033 wasm.object_indirect_function_set.deinit(gpa);
30293034
30303035 wasm.string_bytes.deinit(gpa);
30313036 wasm.string_table.deinit(gpa);
......@@ -3515,6 +3520,7 @@ fn markDataImport(
35153520}
35163521
35173522fn markRelocations(wasm: *Wasm, relocs: ObjectRelocation.IterableSlice) link.File.FlushError!void {
3523 const gpa = wasm.base.comp.gpa;
35183524 for (relocs.slice.tags(wasm), relocs.slice.pointees(wasm), relocs.slice.offsets(wasm)) |tag, pointee, offset| {
35193525 if (offset >= relocs.end) break;
35203526 switch (tag) {
......@@ -3522,6 +3528,11 @@ fn markRelocations(wasm: *Wasm, relocs: ObjectRelocation.IterableSlice) link.Fil
35223528 .function_import_index_i32,
35233529 .function_import_offset_i32,
35243530 .function_import_offset_i64,
3531 => {
3532 const name = pointee.symbol_name;
3533 const i: FunctionImport.Index = @enumFromInt(wasm.object_function_imports.getIndex(name).?);
3534 try markFunctionImport(wasm, name, i.value(wasm), i);
3535 },
35253536 .table_import_index_sleb,
35263537 .table_import_index_i32,
35273538 .table_import_index_sleb64,
......@@ -3530,6 +3541,7 @@ fn markRelocations(wasm: *Wasm, relocs: ObjectRelocation.IterableSlice) link.Fil
35303541 .table_import_index_rel_sleb64,
35313542 => {
35323543 const name = pointee.symbol_name;
3544 try wasm.object_indirect_function_import_set.put(gpa, name, {});
35333545 const i: FunctionImport.Index = @enumFromInt(wasm.object_function_imports.getIndex(name).?);
35343546 try markFunctionImport(wasm, name, i.value(wasm), i);
35353547 },
......@@ -3564,13 +3576,18 @@ fn markRelocations(wasm: *Wasm, relocs: ObjectRelocation.IterableSlice) link.Fil
35643576 .function_index_i32,
35653577 .function_offset_i32,
35663578 .function_offset_i64,
3579 => try markFunction(wasm, pointee.function.chaseWeak(wasm)),
35673580 .table_index_sleb,
35683581 .table_index_i32,
35693582 .table_index_sleb64,
35703583 .table_index_i64,
35713584 .table_index_rel_sleb,
35723585 .table_index_rel_sleb64,
3573 => try markFunction(wasm, pointee.function.chaseWeak(wasm)),
3586 => {
3587 const function = pointee.function;
3588 try wasm.object_indirect_function_set.put(gpa, function, {});
3589 try markFunction(wasm, function.chaseWeak(wasm));
3590 },
35743591 .global_index_leb,
35753592 .global_index_i32,
35763593 => try markGlobal(wasm, pointee.global.chaseWeak(wasm)),
src/link/Wasm/Flush.zig+64-17
......@@ -34,9 +34,28 @@ function_imports: std.AutoArrayHashMapUnmanaged(String, Wasm.FunctionImportId) =
3434global_imports: std.AutoArrayHashMapUnmanaged(String, Wasm.GlobalImportId) = .empty,
3535data_imports: std.AutoArrayHashMapUnmanaged(String, Wasm.DataImportId) = .empty,
3636
37indirect_function_table: std.AutoArrayHashMapUnmanaged(Wasm.OutputFunctionIndex, void) = .empty,
38
3739/// For debug purposes only.
3840memory_layout_finished: bool = false,
3941
42/// Index into `indirect_function_table`.
43const IndirectFunctionTableIndex = enum(u32) {
44 _,
45
46 fn fromObjectFunctionHandlingWeak(wasm: *const Wasm, index: Wasm.ObjectFunctionIndex) IndirectFunctionTableIndex {
47 return fromOutputFunctionIndex(&wasm.flush_buffer, .fromObjectFunctionHandlingWeak(wasm, index));
48 }
49
50 fn fromSymbolName(wasm: *const Wasm, name: String) IndirectFunctionTableIndex {
51 return fromOutputFunctionIndex(&wasm.flush_buffer, .fromSymbolName(wasm, name));
52 }
53
54 fn fromOutputFunctionIndex(f: *const Flush, i: Wasm.OutputFunctionIndex) IndirectFunctionTableIndex {
55 return @enumFromInt(f.indirect_function_table.getIndex(i).?);
56 }
57};
58
4059const DataSegmentGroup = struct {
4160 first_segment: Wasm.DataSegmentId,
4261 end_addr: u32,
......@@ -46,6 +65,7 @@ pub fn clear(f: *Flush) void {
4665 f.data_segments.clearRetainingCapacity();
4766 f.data_segment_groups.clearRetainingCapacity();
4867 f.binary_bytes.clearRetainingCapacity();
68 f.indirect_function_table.clearRetainingCapacity();
4969 f.memory_layout_finished = false;
5070}
5171
......@@ -57,6 +77,7 @@ pub fn deinit(f: *Flush, gpa: Allocator) void {
5777 f.function_imports.deinit(gpa);
5878 f.global_imports.deinit(gpa);
5979 f.data_imports.deinit(gpa);
80 f.indirect_function_table.deinit(gpa);
6081 f.* = undefined;
6182}
6283
......@@ -156,6 +177,17 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
156177
157178 if (diags.hasErrors()) return error.LinkFailure;
158179
180 // Merge indirect function tables.
181 try f.indirect_function_table.ensureUnusedCapacity(gpa, wasm.zcu_indirect_function_set.entries.len +
182 wasm.object_indirect_function_import_set.entries.len + wasm.object_indirect_function_set.entries.len);
183 // This one goes first so the indexes can be stable for MIR lowering.
184 for (wasm.zcu_indirect_function_set.keys()) |nav_index|
185 f.indirect_function_table.putAssumeCapacity(.fromIpNav(wasm, nav_index), {});
186 for (wasm.object_indirect_function_import_set.keys()) |symbol_name|
187 f.indirect_function_table.putAssumeCapacity(.fromSymbolName(wasm, symbol_name), {});
188 for (wasm.object_indirect_function_set.keys()) |object_function_index|
189 f.indirect_function_table.putAssumeCapacity(.fromObjectFunction(wasm, object_function_index), {});
190
159191 // TODO only include init functions for objects with must_link=true or
160192 // which have any alive functions inside them.
161193 if (wasm.object_init_funcs.items.len > 0) {
......@@ -213,7 +245,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
213245
214246 try wasm.tables.ensureUnusedCapacity(gpa, 1);
215247
216 if (wasm.indirect_function_table.entries.len > 0) {
248 if (f.indirect_function_table.entries.len > 0) {
217249 wasm.tables.putAssumeCapacity(.__indirect_function_table, {});
218250 }
219251
......@@ -634,7 +666,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
634666 }
635667
636668 // element section
637 if (wasm.indirect_function_table.entries.len > 0) {
669 if (f.indirect_function_table.entries.len > 0) {
638670 const header_offset = try reserveVecSectionHeader(gpa, binary_bytes);
639671
640672 // indirect function table elements
......@@ -650,9 +682,8 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
650682 if (flags == 0x02) {
651683 try leb.writeUleb128(binary_writer, @as(u8, 0)); // represents funcref
652684 }
653 try leb.writeUleb128(binary_writer, @as(u32, @intCast(wasm.indirect_function_table.entries.len)));
654 for (wasm.indirect_function_table.keys()) |nav_index| {
655 const func_index: Wasm.OutputFunctionIndex = .fromIpNav(wasm, nav_index);
685 try leb.writeUleb128(binary_writer, @as(u32, @intCast(f.indirect_function_table.entries.len)));
686 for (f.indirect_function_table.keys()) |func_index| {
656687 try leb.writeUleb128(binary_writer, @intFromEnum(func_index));
657688 }
658689
......@@ -1459,23 +1490,23 @@ fn applyRelocs(code: []u8, code_offset: u32, relocs: Wasm.ObjectRelocation.Itera
14591490 .function_index_leb => reloc_leb_function(sliced_code, .fromObjectFunctionHandlingWeak(wasm, pointee.function)),
14601491 .function_offset_i32 => @panic("TODO this value is not known yet"),
14611492 .function_offset_i64 => @panic("TODO this value is not known yet"),
1462 .table_index_i32 => @panic("TODO indirect function table needs to support object functions too"),
1463 .table_index_i64 => @panic("TODO indirect function table needs to support object functions too"),
1464 .table_index_rel_sleb => @panic("TODO indirect function table needs to support object functions too"),
1465 .table_index_rel_sleb64 => @panic("TODO indirect function table needs to support object functions too"),
1466 .table_index_sleb => @panic("TODO indirect function table needs to support object functions too"),
1467 .table_index_sleb64 => @panic("TODO indirect function table needs to support object functions too"),
1493 .table_index_i32 => reloc_u32_table_index(sliced_code, .fromObjectFunctionHandlingWeak(wasm, pointee.function)),
1494 .table_index_i64 => reloc_u64_table_index(sliced_code, .fromObjectFunctionHandlingWeak(wasm, pointee.function)),
1495 .table_index_rel_sleb => @panic("TODO what does this reloc tag mean?"),
1496 .table_index_rel_sleb64 => @panic("TODO what does this reloc tag mean?"),
1497 .table_index_sleb => reloc_sleb_table_index(sliced_code, .fromObjectFunctionHandlingWeak(wasm, pointee.function)),
1498 .table_index_sleb64 => reloc_sleb64_table_index(sliced_code, .fromObjectFunctionHandlingWeak(wasm, pointee.function)),
14681499
14691500 .function_import_index_i32 => reloc_u32_function(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)),
14701501 .function_import_index_leb => reloc_leb_function(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)),
14711502 .function_import_offset_i32 => @panic("TODO this value is not known yet"),
14721503 .function_import_offset_i64 => @panic("TODO this value is not known yet"),
1473 .table_import_index_i32 => @panic("TODO indirect function table needs to support object functions too"),
1474 .table_import_index_i64 => @panic("TODO indirect function table needs to support object functions too"),
1475 .table_import_index_rel_sleb => @panic("TODO indirect function table needs to support object functions too"),
1476 .table_import_index_rel_sleb64 => @panic("TODO indirect function table needs to support object functions too"),
1477 .table_import_index_sleb => @panic("TODO indirect function table needs to support object functions too"),
1478 .table_import_index_sleb64 => @panic("TODO indirect function table needs to support object functions too"),
1504 .table_import_index_i32 => reloc_u32_table_index(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)),
1505 .table_import_index_i64 => reloc_u64_table_index(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)),
1506 .table_import_index_rel_sleb => @panic("TODO what does this reloc tag mean?"),
1507 .table_import_index_rel_sleb64 => @panic("TODO what does this reloc tag mean?"),
1508 .table_import_index_sleb => reloc_sleb_table_index(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)),
1509 .table_import_index_sleb64 => reloc_sleb64_table_index(sliced_code, .fromSymbolName(wasm, pointee.symbol_name)),
14791510
14801511 .global_index_i32 => reloc_u32_global(sliced_code, .fromObjectGlobalHandlingWeak(wasm, pointee.global)),
14811512 .global_index_leb => reloc_leb_global(sliced_code, .fromObjectGlobalHandlingWeak(wasm, pointee.global)),
......@@ -1517,6 +1548,22 @@ fn applyRelocs(code: []u8, code_offset: u32, relocs: Wasm.ObjectRelocation.Itera
15171548 }
15181549}
15191550
1551fn reloc_u32_table_index(code: []u8, i: IndirectFunctionTableIndex) void {
1552 mem.writeInt(u32, code[0..4], @intFromEnum(i), .little);
1553}
1554
1555fn reloc_u64_table_index(code: []u8, i: IndirectFunctionTableIndex) void {
1556 mem.writeInt(u64, code[0..8], @intFromEnum(i), .little);
1557}
1558
1559fn reloc_sleb_table_index(code: []u8, i: IndirectFunctionTableIndex) void {
1560 leb.writeSignedFixed(5, code[0..5], @intFromEnum(i));
1561}
1562
1563fn reloc_sleb64_table_index(code: []u8, i: IndirectFunctionTableIndex) void {
1564 leb.writeSignedFixed(11, code[0..11], @intFromEnum(i));
1565}
1566
15201567fn reloc_u32_function(code: []u8, function: Wasm.OutputFunctionIndex) void {
15211568 mem.writeInt(u32, code[0..4], @intFromEnum(function), .little);
15221569}