authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-12-28 14:41:27+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-12-28 14:41:27+01:00
log4aab8118a771e37566c0c3b1c40c175ce1e98285
tree904bafeb49f697ef01d20390e3586378f078916a
parent7802c26449657b205dcaa47bb6575bb26171c024
signaturelock-open Commit is signed but in an unrecognized format.

WebAssembly: don't append `--export` for functions

No longer automatically append the `--export` flag for each exported function unconditionally. This was essentially a hack to prevent binary bloat caused by compiler-rt symbols being always included in the final binary as they were exported and therefore not garbage- collected. This is no longer needed as we now support the ability to set the visibility of exports. This essentially reverts 6d951aff7e32b1b0252d341e66517a9a9ee98a2d

2 files changed, 6 insertions(+), 34 deletions(-)

src/Compilation.zig-9
...@@ -181,10 +181,6 @@ emit_docs: ?EmitLoc,...@@ -181,10 +181,6 @@ emit_docs: ?EmitLoc,
181work_queue_wait_group: WaitGroup = .{},181work_queue_wait_group: WaitGroup = .{},
182astgen_wait_group: WaitGroup = .{},182astgen_wait_group: WaitGroup = .{},
183183
184/// Exported symbol names. This is only for when the target is wasm.
185/// TODO: Remove this when Stage2 becomes the default compiler as it will already have this information.
186export_symbol_names: std.ArrayListUnmanaged([]const u8) = .{},
187
188pub const default_stack_protector_buffer_size = 4;184pub const default_stack_protector_buffer_size = 4;
189pub const SemaError = Module.SemaError;185pub const SemaError = Module.SemaError;
190186
...@@ -2168,11 +2164,6 @@ pub fn destroy(self: *Compilation) void {...@@ -2168,11 +2164,6 @@ pub fn destroy(self: *Compilation) void {
2168 self.cache_parent.manifest_dir.close();2164 self.cache_parent.manifest_dir.close();
2169 if (self.owned_link_dir) |*dir| dir.close();2165 if (self.owned_link_dir) |*dir| dir.close();
21702166
2171 for (self.export_symbol_names.items) |symbol_name| {
2172 gpa.free(symbol_name);
2173 }
2174 self.export_symbol_names.deinit(gpa);
2175
2176 // This destroys `self`.2167 // This destroys `self`.
2177 self.arena_state.promote(gpa).deinit();2168 self.arena_state.promote(gpa).deinit();
2178}2169}
src/link/Wasm.zig+6-25
...@@ -3406,39 +3406,14 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !...@@ -3406,39 +3406,14 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
3406 try argv.append("--stack-first");3406 try argv.append("--stack-first");
3407 }3407 }
34083408
3409 var auto_export_symbols = true;
3410 // Users are allowed to specify which symbols they want to export to the wasm host.3409 // Users are allowed to specify which symbols they want to export to the wasm host.
3411 for (wasm.base.options.export_symbol_names) |symbol_name| {3410 for (wasm.base.options.export_symbol_names) |symbol_name| {
3412 const arg = try std.fmt.allocPrint(arena, "--export={s}", .{symbol_name});3411 const arg = try std.fmt.allocPrint(arena, "--export={s}", .{symbol_name});
3413 try argv.append(arg);3412 try argv.append(arg);
3414 auto_export_symbols = false;
3415 }3413 }
34163414
3417 if (wasm.base.options.rdynamic) {3415 if (wasm.base.options.rdynamic) {
3418 try argv.append("--export-dynamic");3416 try argv.append("--export-dynamic");
3419 auto_export_symbols = false;
3420 }
3421
3422 if (auto_export_symbols) {
3423 if (wasm.base.options.module) |mod| {
3424 // when we use stage1, we use the exports that stage1 provided us.
3425 // For stage2, we can directly retrieve them from the module.
3426 const skip_export_non_fn = target.os.tag == .wasi and
3427 wasm.base.options.wasi_exec_model == .command;
3428 for (mod.decl_exports.values()) |exports| {
3429 for (exports.items) |exprt| {
3430 const exported_decl = mod.declPtr(exprt.exported_decl);
3431 if (skip_export_non_fn and exported_decl.ty.zigTypeTag() != .Fn) {
3432 // skip exporting symbols when we're building a WASI command
3433 // and the symbol is not a function
3434 continue;
3435 }
3436 const symbol_name = exported_decl.name;
3437 const arg = try std.fmt.allocPrint(arena, "--export={s}", .{symbol_name});
3438 try argv.append(arg);
3439 }
3440 }
3441 }
3442 }3417 }
34433418
3444 if (wasm.base.options.entry) |entry| {3419 if (wasm.base.options.entry) |entry| {
...@@ -3457,6 +3432,12 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !...@@ -3457,6 +3432,12 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
3457 if (wasm.base.options.wasi_exec_model == .reactor) {3432 if (wasm.base.options.wasi_exec_model == .reactor) {
3458 // Reactor execution model does not have _start so lld doesn't look for it.3433 // Reactor execution model does not have _start so lld doesn't look for it.
3459 try argv.append("--no-entry");3434 try argv.append("--no-entry");
3435 // Make sure "_initialize" and other used-defined functions are exported if this is WASI reactor.
3436 // If rdynamic is true, it will already be appended, so only verify if the user did not specify
3437 // the flag in which case, we ensure `--export-dynamic` is called.
3438 if (!wasm.base.options.rdynamic) {
3439 try argv.append("--export-dynamic");
3440 }
3460 }3441 }
3461 } else if (wasm.base.options.entry == null) {3442 } else if (wasm.base.options.entry == null) {
3462 try argv.append("--no-entry"); // So lld doesn't look for _start.3443 try argv.append("--no-entry"); // So lld doesn't look for _start.