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,
181181work_queue_wait_group: WaitGroup = .{},
182182astgen_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
188184pub const default_stack_protector_buffer_size = 4;
189185pub const SemaError = Module.SemaError;
190186
......@@ -2168,11 +2164,6 @@ pub fn destroy(self: *Compilation) void {
21682164 self.cache_parent.manifest_dir.close();
21692165 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
21762167 // This destroys `self`.
21772168 self.arena_state.promote(gpa).deinit();
21782169}
src/link/Wasm.zig+6-25
......@@ -3406,39 +3406,14 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
34063406 try argv.append("--stack-first");
34073407 }
34083408
3409 var auto_export_symbols = true;
34103409 // Users are allowed to specify which symbols they want to export to the wasm host.
34113410 for (wasm.base.options.export_symbol_names) |symbol_name| {
34123411 const arg = try std.fmt.allocPrint(arena, "--export={s}", .{symbol_name});
34133412 try argv.append(arg);
3414 auto_export_symbols = false;
34153413 }
34163414
34173415 if (wasm.base.options.rdynamic) {
34183416 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 }
34423417 }
34433418
34443419 if (wasm.base.options.entry) |entry| {
......@@ -3457,6 +3432,12 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
34573432 if (wasm.base.options.wasi_exec_model == .reactor) {
34583433 // Reactor execution model does not have _start so lld doesn't look for it.
34593434 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 }
34603441 }
34613442 } else if (wasm.base.options.entry == null) {
34623443 try argv.append("--no-entry"); // So lld doesn't look for _start.