authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-06-18 17:50:51+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2023-06-26 20:00:57+02:00
log381937116326400c1162a710c16496152b542443
tree21e841fedeae8a5a1dac655cec5a2cab724e7175
parent7322aa118376a635ab077ca833dd152639953337
signaturelock-open Commit is signed but in an unrecognized format.

wasm-ld: implement `--export-memory` flag

This flag allows the user to force export the memory to the host environment. This is useful when the memory is imported from the host but must also be exported. This is (currently) required to pass the memory validation for runtimes when using threads. In this future this may become an error instead.

4 files changed, 16 insertions(+), 0 deletions(-)

src/Compilation.zig+3
......@@ -557,6 +557,7 @@ pub const InitOptions = struct {
557557 linker_allow_shlib_undefined: ?bool = null,
558558 linker_bind_global_refs_locally: ?bool = null,
559559 linker_import_memory: ?bool = null,
560 linker_export_memory: ?bool = null,
560561 linker_import_symbols: bool = false,
561562 linker_import_table: bool = false,
562563 linker_export_table: bool = false,
......@@ -1463,6 +1464,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
14631464 .module_definition_file = options.linker_module_definition_file,
14641465 .sort_section = options.linker_sort_section,
14651466 .import_memory = options.linker_import_memory orelse false,
1467 .export_memory = options.linker_export_memory orelse !(options.linker_import_memory orelse false),
14661468 .import_symbols = options.linker_import_symbols,
14671469 .import_table = options.linker_import_table,
14681470 .export_table = options.linker_export_table,
......@@ -2324,6 +2326,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
23242326
23252327 // WASM specific stuff
23262328 man.hash.add(comp.bin_file.options.import_memory);
2329 man.hash.add(comp.bin_file.options.export_memory);
23272330 man.hash.addOptional(comp.bin_file.options.initial_memory);
23282331 man.hash.addOptional(comp.bin_file.options.max_memory);
23292332 man.hash.add(comp.bin_file.options.shared_memory);
src/link.zig+1
......@@ -133,6 +133,7 @@ pub const Options = struct {
133133 compress_debug_sections: CompressDebugSections,
134134 bind_global_refs_locally: bool,
135135 import_memory: bool,
136 export_memory: bool,
136137 import_symbols: bool,
137138 import_table: bool,
138139 export_table: bool,
src/link/Wasm.zig+5
......@@ -4251,6 +4251,7 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
42514251 man.hash.addOptional(wasm.base.options.stack_size_override);
42524252 man.hash.add(wasm.base.options.build_id);
42534253 man.hash.add(wasm.base.options.import_memory);
4254 man.hash.add(wasm.base.options.export_memory);
42544255 man.hash.add(wasm.base.options.import_table);
42554256 man.hash.add(wasm.base.options.export_table);
42564257 man.hash.addOptional(wasm.base.options.initial_memory);
......@@ -4338,6 +4339,10 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
43384339 try argv.append("--import-memory");
43394340 }
43404341
4342 if (wasm.base.options.export_memory) {
4343 try argv.append("--export-memory");
4344 }
4345
43414346 if (wasm.base.options.import_table) {
43424347 assert(!wasm.base.options.export_table);
43434348 try argv.append("--import-table");
src/main.zig+7
......@@ -544,6 +544,7 @@ const usage_build_generic =
544544 \\ -dead_strip (Darwin) remove functions and data that are unreachable by the entry point or exported symbols
545545 \\ -dead_strip_dylibs (Darwin) remove dylibs that are unreachable by the entry point or exported symbols
546546 \\ --import-memory (WebAssembly) import memory from the environment
547 \\ --export-memory (WebAssembly) export memory to the host (Default unless --import-memory used)
547548 \\ --import-symbols (WebAssembly) import missing symbols from the host environment
548549 \\ --import-table (WebAssembly) import function table from the host environment
549550 \\ --export-table (WebAssembly) export function table to the host environment
......@@ -787,6 +788,7 @@ fn buildOutputType(
787788 var linker_allow_shlib_undefined: ?bool = null;
788789 var linker_bind_global_refs_locally: ?bool = null;
789790 var linker_import_memory: ?bool = null;
791 var linker_export_memory: ?bool = null;
790792 var linker_import_symbols: bool = false;
791793 var linker_import_table: bool = false;
792794 var linker_export_table: bool = false;
......@@ -1419,6 +1421,8 @@ fn buildOutputType(
14191421 }
14201422 } else if (mem.eql(u8, arg, "--import-memory")) {
14211423 linker_import_memory = true;
1424 } else if (mem.eql(u8, arg, "--export-memory")) {
1425 linker_export_memory = true;
14221426 } else if (mem.eql(u8, arg, "--import-symbols")) {
14231427 linker_import_symbols = true;
14241428 } else if (mem.eql(u8, arg, "--import-table")) {
......@@ -1982,6 +1986,8 @@ fn buildOutputType(
19821986 linker_bind_global_refs_locally = true;
19831987 } else if (mem.eql(u8, arg, "--import-memory")) {
19841988 linker_import_memory = true;
1989 } else if (mem.eql(u8, arg, "--export-memory")) {
1990 linker_export_memory = true;
19851991 } else if (mem.eql(u8, arg, "--import-symbols")) {
19861992 linker_import_symbols = true;
19871993 } else if (mem.eql(u8, arg, "--import-table")) {
......@@ -3113,6 +3119,7 @@ fn buildOutputType(
31133119 .linker_allow_shlib_undefined = linker_allow_shlib_undefined,
31143120 .linker_bind_global_refs_locally = linker_bind_global_refs_locally,
31153121 .linker_import_memory = linker_import_memory,
3122 .linker_export_memory = linker_export_memory,
31163123 .linker_import_symbols = linker_import_symbols,
31173124 .linker_import_table = linker_import_table,
31183125 .linker_export_table = linker_export_table,