aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-12-29 17:59:44 -0500
committerGitHub <noreply@github.com>2022-12-29 17:59:44 -0500
commit40ba4d4a89cc30ac5f5cbb560a2f8d75f0b3145e (patch)
tree7e54829c07e67957f02ae28b626149bd3a893f35 /src
parent0d83487dd098f9498d907f06535e645cc8bcf3de (diff)
parent8403612adc67d6398b3664b20eb453475688bd46 (diff)
downloadzig-40ba4d4a89cc30ac5f5cbb560a2f8d75f0b3145e.tar.gz
zig-40ba4d4a89cc30ac5f5cbb560a2f8d75f0b3145e.zip
Merge pull request #14102 from Luukdegram/wasm-undefined-symbols
WebAssembly: remove unconditional --allow-undefined flag
Diffstat (limited to 'src')
-rw-r--r--src/Compilation.zig11
-rw-r--r--src/link.zig1
-rw-r--r--src/link/Wasm.zig35
-rw-r--r--src/main.zig7
4 files changed, 19 insertions, 35 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig
index b385fa5f72..5b80e5c7a7 100644
--- a/src/Compilation.zig
+++ b/src/Compilation.zig
@@ -181,10 +181,6 @@ emit_docs: ?EmitLoc,
work_queue_wait_group: WaitGroup = .{},
astgen_wait_group: WaitGroup = .{},
-/// Exported symbol names. This is only for when the target is wasm.
-/// TODO: Remove this when Stage2 becomes the default compiler as it will already have this information.
-export_symbol_names: std.ArrayListUnmanaged([]const u8) = .{},
-
pub const default_stack_protector_buffer_size = 4;
pub const SemaError = Module.SemaError;
@@ -954,6 +950,7 @@ pub const InitOptions = struct {
linker_allow_shlib_undefined: ?bool = null,
linker_bind_global_refs_locally: ?bool = null,
linker_import_memory: ?bool = null,
+ linker_import_symbols: bool = false,
linker_import_table: bool = false,
linker_export_table: bool = false,
linker_initial_memory: ?u64 = null,
@@ -1811,6 +1808,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
.bind_global_refs_locally = options.linker_bind_global_refs_locally orelse false,
.compress_debug_sections = options.linker_compress_debug_sections orelse .none,
.import_memory = options.linker_import_memory orelse false,
+ .import_symbols = options.linker_import_symbols,
.import_table = options.linker_import_table,
.export_table = options.linker_export_table,
.initial_memory = options.linker_initial_memory,
@@ -2166,11 +2164,6 @@ pub fn destroy(self: *Compilation) void {
self.cache_parent.manifest_dir.close();
if (self.owned_link_dir) |*dir| dir.close();
- for (self.export_symbol_names.items) |symbol_name| {
- gpa.free(symbol_name);
- }
- self.export_symbol_names.deinit(gpa);
-
// This destroys `self`.
self.arena_state.promote(gpa).deinit();
}
diff --git a/src/link.zig b/src/link.zig
index 001cc709e4..33c5feb727 100644
--- a/src/link.zig
+++ b/src/link.zig
@@ -128,6 +128,7 @@ pub const Options = struct {
compress_debug_sections: CompressDebugSections,
bind_global_refs_locally: bool,
import_memory: bool,
+ import_symbols: bool,
import_table: bool,
export_table: bool,
initial_memory: ?u64,
diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig
index 346c92ebbe..9beb40e418 100644
--- a/src/link/Wasm.zig
+++ b/src/link/Wasm.zig
@@ -3406,39 +3406,14 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
try argv.append("--stack-first");
}
- var auto_export_symbols = true;
// Users are allowed to specify which symbols they want to export to the wasm host.
for (wasm.base.options.export_symbol_names) |symbol_name| {
const arg = try std.fmt.allocPrint(arena, "--export={s}", .{symbol_name});
try argv.append(arg);
- auto_export_symbols = false;
}
if (wasm.base.options.rdynamic) {
try argv.append("--export-dynamic");
- auto_export_symbols = false;
- }
-
- if (auto_export_symbols) {
- if (wasm.base.options.module) |mod| {
- // when we use stage1, we use the exports that stage1 provided us.
- // For stage2, we can directly retrieve them from the module.
- const skip_export_non_fn = target.os.tag == .wasi and
- wasm.base.options.wasi_exec_model == .command;
- for (mod.decl_exports.values()) |exports| {
- for (exports.items) |exprt| {
- const exported_decl = mod.declPtr(exprt.exported_decl);
- if (skip_export_non_fn and 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 = exported_decl.name;
- const arg = try std.fmt.allocPrint(arena, "--export={s}", .{symbol_name});
- try argv.append(arg);
- }
- }
- }
}
if (wasm.base.options.entry) |entry| {
@@ -3457,12 +3432,20 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
if (wasm.base.options.wasi_exec_model == .reactor) {
// Reactor execution model does not have _start so lld doesn't look for it.
try argv.append("--no-entry");
+ // Make sure "_initialize" and other used-defined functions are exported if this is WASI reactor.
+ // If rdynamic is true, it will already be appended, so only verify if the user did not specify
+ // the flag in which case, we ensure `--export-dynamic` is called.
+ if (!wasm.base.options.rdynamic) {
+ try argv.append("--export-dynamic");
+ }
}
} else if (wasm.base.options.entry == null) {
try argv.append("--no-entry"); // So lld doesn't look for _start.
}
+ if (wasm.base.options.import_symbols) {
+ try argv.append("--allow-undefined");
+ }
try argv.appendSlice(&[_][]const u8{
- "--allow-undefined",
"-o",
full_out_path,
});
diff --git a/src/main.zig b/src/main.zig
index 397bead30c..ffb65b43aa 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -517,6 +517,7 @@ const usage_build_generic =
\\ -dead_strip (Darwin) remove functions and data that are unreachable by the entry point or exported symbols
\\ -dead_strip_dylibs (Darwin) remove dylibs that are unreachable by the entry point or exported symbols
\\ --import-memory (WebAssembly) import memory from the environment
+ \\ --import-symbols (WebAssembly) import missing symbols from the host environment
\\ --import-table (WebAssembly) import function table from the host environment
\\ --export-table (WebAssembly) export function table to the host environment
\\ --initial-memory=[bytes] (WebAssembly) initial size of the linear memory
@@ -718,6 +719,7 @@ fn buildOutputType(
var linker_allow_shlib_undefined: ?bool = null;
var linker_bind_global_refs_locally: ?bool = null;
var linker_import_memory: ?bool = null;
+ var linker_import_symbols: bool = false;
var linker_import_table: bool = false;
var linker_export_table: bool = false;
var linker_initial_memory: ?u64 = null;
@@ -1316,6 +1318,8 @@ fn buildOutputType(
}
} else if (mem.eql(u8, arg, "--import-memory")) {
linker_import_memory = true;
+ } else if (mem.eql(u8, arg, "--import-symbols")) {
+ linker_import_symbols = true;
} else if (mem.eql(u8, arg, "--import-table")) {
linker_import_table = true;
} else if (mem.eql(u8, arg, "--export-table")) {
@@ -1839,6 +1843,8 @@ fn buildOutputType(
linker_bind_global_refs_locally = true;
} else if (mem.eql(u8, arg, "--import-memory")) {
linker_import_memory = true;
+ } else if (mem.eql(u8, arg, "--import-symbols")) {
+ linker_import_symbols = true;
} else if (mem.eql(u8, arg, "--import-table")) {
linker_import_table = true;
} else if (mem.eql(u8, arg, "--export-table")) {
@@ -2981,6 +2987,7 @@ fn buildOutputType(
.linker_allow_shlib_undefined = linker_allow_shlib_undefined,
.linker_bind_global_refs_locally = linker_bind_global_refs_locally,
.linker_import_memory = linker_import_memory,
+ .linker_import_symbols = linker_import_symbols,
.linker_import_table = linker_import_table,
.linker_export_table = linker_export_table,
.linker_initial_memory = linker_initial_memory,