From 6d951aff7e32b1b0252d341e66517a9a9ee98a2d Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Wed, 5 Jan 2022 20:47:04 +0100 Subject: wasm-linker: Only export symbols notated as such This exposes a function from stage2 to stage1 to append symbols to automatically export them. This happends under the following conditions: - Target is wasm - User has not provided --export/--rdynamic flags themselves. --- src/stage1/codegen.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/stage1/codegen.cpp') diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index 0cf8024995..7b3640dae6 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -9905,6 +9905,18 @@ void codegen_build_object(CodeGen *g) { codegen_add_time_event(g, "Done"); codegen_switch_sub_prog_node(g, nullptr); + + // append all export symbols to stage2 so we can provide them to the linker + if (target_is_wasm(g->zig_target)){ + Error err; + auto export_it = g->exported_symbol_names.entry_iterator(); + decltype(g->exported_symbol_names)::Entry *curr_entry = nullptr; + while ((curr_entry = export_it.next()) != nullptr) { + if ((err = stage2_append_symbol(&g->stage1, buf_ptr(curr_entry->key)))) { + fprintf(stderr, "Unable to export symbol '%s': %s\n", buf_ptr(curr_entry->key), err_str(err)); + } + } + } } ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path, -- cgit v1.2.3 From 247b638ccf8bfd5e0c4729935d230022726f97aa Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Thu, 6 Jan 2022 20:18:24 +0100 Subject: Pass `--allow-unknown-exports` to wasmtime Also skip exporting non-function symbols when we're building a WASI command using the stage2 llvm backend. --- lib/std/build.zig | 2 ++ src/Compilation.zig | 4 ++-- src/link/Wasm.zig | 7 +++++++ src/stage1.zig | 10 +++------- src/stage1/codegen.cpp | 2 +- src/stage1/stage2.h | 2 +- src/stage1/zig0.cpp | 2 +- 7 files changed, 17 insertions(+), 12 deletions(-) (limited to 'src/stage1/codegen.cpp') diff --git a/lib/std/build.zig b/lib/std/build.zig index 631ca43f38..ecc09bfc13 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -2659,6 +2659,8 @@ pub const LibExeObjStep = struct { try zig_args.append(bin_name); try zig_args.append("--test-cmd"); try zig_args.append("--dir=."); + try zig_args.append("--test-cmd"); + try zig_args.append("--allow-unknown-exports"); // TODO: Remove when stage2 is default compiler try zig_args.append("--test-cmd-bin"); } else { try zig_args.append("--test-no-exec"); diff --git a/src/Compilation.zig b/src/Compilation.zig index cca07c2a34..7d648d4649 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1882,9 +1882,9 @@ pub fn destroy(self: *Compilation) void { self.astgen_wait_group.deinit(); for (self.export_symbol_names.items) |symbol_name| { - self.gpa.free(symbol_name); + gpa.free(symbol_name); } - self.export_symbol_names.deinit(self.gpa); + self.export_symbol_names.deinit(gpa); // This destroys `self`. self.arena_state.promote(gpa).deinit(); diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 2a00b35bd9..f1dca0afe0 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -1256,8 +1256,15 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { try argv.append(try std.fmt.allocPrint(arena, "--export={s}", .{symbol_name})); } } else { + const skip_export_non_fn = target.os.tag == .wasi and + self.base.options.wasi_exec_model == .command; for (module.decl_exports.values()) |exports| { for (exports) |exprt| { + if (skip_export_non_fn and exprt.exported_decl.ty.zigTypeTag() != .Fn) { + // skip exporting symbols when we're building a WASI command + // and the symbol is not a function + continue; + } const symbol_name = exprt.exported_decl.name; const arg = try std.fmt.allocPrint(arena, "--export={s}", .{symbol_name}); try argv.append(arg); diff --git a/src/stage1.zig b/src/stage1.zig index c7967ae32d..5447c7c4b2 100644 --- a/src/stage1.zig +++ b/src/stage1.zig @@ -468,13 +468,9 @@ export fn stage2_fetch_file( return contents.ptr; } -export fn stage2_append_symbol(stage1: *Module, name_ptr: ?[*:0]const u8) Error { +export fn stage2_append_symbol(stage1: *Module, name_ptr: [*c]const u8, name_len: usize) Error { + if (name_len == 0) return Error.None; const comp = @intToPtr(*Compilation, stage1.userdata); - - if (name_ptr) |unwrapped_name| { - const symbol_name = std.mem.sliceTo(unwrapped_name, 0); - if (symbol_name.len == 0) return Error.None; - comp.export_symbol_names.append(comp.gpa, symbol_name) catch return Error.OutOfMemory; - } + comp.export_symbol_names.append(comp.gpa, name_ptr[0..name_len]) catch return Error.OutOfMemory; return Error.None; } diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index 7b3640dae6..cfd455e815 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -9912,7 +9912,7 @@ void codegen_build_object(CodeGen *g) { auto export_it = g->exported_symbol_names.entry_iterator(); decltype(g->exported_symbol_names)::Entry *curr_entry = nullptr; while ((curr_entry = export_it.next()) != nullptr) { - if ((err = stage2_append_symbol(&g->stage1, buf_ptr(curr_entry->key)))) { + if ((err = stage2_append_symbol(&g->stage1, buf_ptr(curr_entry->key), buf_len(curr_entry->key)))) { fprintf(stderr, "Unable to export symbol '%s': %s\n", buf_ptr(curr_entry->key), err_str(err)); } } diff --git a/src/stage1/stage2.h b/src/stage1/stage2.h index 40f142d66e..58b3a7687f 100644 --- a/src/stage1/stage2.h +++ b/src/stage1/stage2.h @@ -183,6 +183,6 @@ ZIG_EXTERN_C const char *stage2_add_link_lib(struct ZigStage1 *stage1, const char *symbol_name_ptr, size_t symbol_name_len); // ABI warning -ZIG_EXTERN_C enum Error stage2_append_symbol(struct ZigStage1 *stage1, const char *name_ptr); +ZIG_EXTERN_C enum Error stage2_append_symbol(struct ZigStage1 *stage1, const char *name_ptr, size_t name_len); #endif diff --git a/src/stage1/zig0.cpp b/src/stage1/zig0.cpp index d3b7560a4f..439f2bd1d4 100644 --- a/src/stage1/zig0.cpp +++ b/src/stage1/zig0.cpp @@ -555,7 +555,7 @@ struct Stage2SemVer stage2_version(void) { return {ZIG_VERSION_MAJOR, ZIG_VERSION_MINOR, ZIG_VERSION_PATCH}; } -Error stage2_append_symbol(struct ZigStage1 *stage1, const char *name_ptr) +Error stage2_append_symbol(struct ZigStage1 *stage1, const char *name_ptr, size_t name_len) { return ErrorNone; } -- cgit v1.2.3