authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-14 19:03:10-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:36-08:00
logf7f88787165e8417fa524f0781f6cd139864f12b
treedfcf260df3c87edb9173d8c7009930c0ceb63531
parent9143575ec309526f350f7f7969d76b4869a54fc7

wasm linker: correct export visibility logic

exports are hidden unless protected or rdynamic or explicitly asked for, matching master branch

2 files changed, 26 insertions(+), 9 deletions(-)

src/link/Wasm.zig+13
...@@ -4674,3 +4674,16 @@ fn resolveFunctionSynthetic(...@@ -4674,3 +4674,16 @@ fn resolveFunctionSynthetic(
4674 });4674 });
4675 }4675 }
4676}4676}
4677
4678pub fn addFunction(
4679 wasm: *Wasm,
4680 resolution: FunctionImport.Resolution,
4681 params: []const std.wasm.Valtype,
4682 returns: []const std.wasm.Valtype,
4683) Allocator.Error!void {
4684 wasm.functions.putAssumeCapacity(resolution, {});
4685 _ = try wasm.addFuncType(.{
4686 .params = try wasm.internValtypeList(params),
4687 .returns = try wasm.internValtypeList(returns),
4688 });
4689}
src/link/Wasm/Flush.zig+13-9
...@@ -168,11 +168,17 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {...@@ -168,11 +168,17 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
168 if (ip.isFunctionType(ip.getNav(nav_export.nav_index).typeOf(ip))) {168 if (ip.isFunctionType(ip.getNav(nav_export.nav_index).typeOf(ip))) {
169 log.debug("flush export '{s}' nav={d}", .{ nav_export.name.slice(wasm), nav_export.nav_index });169 log.debug("flush export '{s}' nav={d}", .{ nav_export.name.slice(wasm), nav_export.nav_index });
170 const function_index = Wasm.FunctionIndex.fromIpNav(wasm, nav_export.nav_index).?;170 const function_index = Wasm.FunctionIndex.fromIpNav(wasm, nav_export.nav_index).?;
171 switch (export_index.ptr(zcu).opts.visibility) {171 const explicit = f.missing_exports.swapRemove(nav_export.name);
172 .default, .protected => try wasm.function_exports.put(gpa, nav_export.name, function_index),172 const is_hidden = !explicit and switch (export_index.ptr(zcu).opts.visibility) {
173 .hidden => try wasm.hidden_function_exports.put(gpa, nav_export.name, function_index),173 .protected => false,
174 .hidden => true,
175 .default => !comp.config.rdynamic,
176 };
177 if (is_hidden) {
178 try wasm.hidden_function_exports.put(gpa, nav_export.name, function_index);
179 } else {
180 try wasm.function_exports.put(gpa, nav_export.name, function_index);
174 }181 }
175 _ = f.missing_exports.swapRemove(nav_export.name);
176 _ = f.function_imports.swapRemove(nav_export.name);182 _ = f.function_imports.swapRemove(nav_export.name);
177183
178 if (nav_export.name.toOptional() == entry_name)184 if (nav_export.name.toOptional() == entry_name)
...@@ -281,9 +287,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {...@@ -281,9 +287,7 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
281 // We also initialize bss segments (using memory.fill) as part of this287 // We also initialize bss segments (using memory.fill) as part of this
282 // function.288 // function.
283 if (wasm.any_passive_inits) {289 if (wasm.any_passive_inits) {
284 wasm.functions.putAssumeCapacity(.__wasm_init_memory, {});290 try wasm.addFunction(.__wasm_init_memory, &.{}, &.{});
285 const empty = try wasm.internValtypeList(&.{});
286 _ = try wasm.addFuncType(.{ .params = empty, .returns = empty });
287 }291 }
288292
289 // When we have TLS GOT entries and shared memory is enabled,293 // When we have TLS GOT entries and shared memory is enabled,
...@@ -291,8 +295,8 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {...@@ -291,8 +295,8 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
291 if (shared_memory) {295 if (shared_memory) {
292 // This logic that checks `any_tls_relocs` is missing the part where it296 // This logic that checks `any_tls_relocs` is missing the part where it
293 // also notices threadlocal globals from Zcu code.297 // also notices threadlocal globals from Zcu code.
294 if (wasm.any_tls_relocs) wasm.functions.putAssumeCapacity(.__wasm_apply_global_tls_relocs, {});298 if (wasm.any_tls_relocs) try wasm.addFunction(.__wasm_apply_global_tls_relocs, &.{}, &.{});
295 wasm.functions.putAssumeCapacity(.__wasm_init_tls, {});299 try wasm.addFunction(.__wasm_init_tls, &.{.i32}, &.{});
296 }300 }
297301
298 try wasm.tables.ensureUnusedCapacity(gpa, 1);302 try wasm.tables.ensureUnusedCapacity(gpa, 1);