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 {...@@ -557,6 +557,7 @@ pub const InitOptions = struct {
557 linker_allow_shlib_undefined: ?bool = null,557 linker_allow_shlib_undefined: ?bool = null,
558 linker_bind_global_refs_locally: ?bool = null,558 linker_bind_global_refs_locally: ?bool = null,
559 linker_import_memory: ?bool = null,559 linker_import_memory: ?bool = null,
560 linker_export_memory: ?bool = null,
560 linker_import_symbols: bool = false,561 linker_import_symbols: bool = false,
561 linker_import_table: bool = false,562 linker_import_table: bool = false,
562 linker_export_table: bool = false,563 linker_export_table: bool = false,
...@@ -1463,6 +1464,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1463,6 +1464,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1463 .module_definition_file = options.linker_module_definition_file,1464 .module_definition_file = options.linker_module_definition_file,
1464 .sort_section = options.linker_sort_section,1465 .sort_section = options.linker_sort_section,
1465 .import_memory = options.linker_import_memory orelse false,1466 .import_memory = options.linker_import_memory orelse false,
1467 .export_memory = options.linker_export_memory orelse !(options.linker_import_memory orelse false),
1466 .import_symbols = options.linker_import_symbols,1468 .import_symbols = options.linker_import_symbols,
1467 .import_table = options.linker_import_table,1469 .import_table = options.linker_import_table,
1468 .export_table = options.linker_export_table,1470 .export_table = options.linker_export_table,
...@@ -2324,6 +2326,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes...@@ -2324,6 +2326,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
23242326
2325 // WASM specific stuff2327 // WASM specific stuff
2326 man.hash.add(comp.bin_file.options.import_memory);2328 man.hash.add(comp.bin_file.options.import_memory);
2329 man.hash.add(comp.bin_file.options.export_memory);
2327 man.hash.addOptional(comp.bin_file.options.initial_memory);2330 man.hash.addOptional(comp.bin_file.options.initial_memory);
2328 man.hash.addOptional(comp.bin_file.options.max_memory);2331 man.hash.addOptional(comp.bin_file.options.max_memory);
2329 man.hash.add(comp.bin_file.options.shared_memory);2332 man.hash.add(comp.bin_file.options.shared_memory);
src/link.zig+1
...@@ -133,6 +133,7 @@ pub const Options = struct {...@@ -133,6 +133,7 @@ pub const Options = struct {
133 compress_debug_sections: CompressDebugSections,133 compress_debug_sections: CompressDebugSections,
134 bind_global_refs_locally: bool,134 bind_global_refs_locally: bool,
135 import_memory: bool,135 import_memory: bool,
136 export_memory: bool,
136 import_symbols: bool,137 import_symbols: bool,
137 import_table: bool,138 import_table: bool,
138 export_table: bool,139 export_table: bool,
src/link/Wasm.zig+5
...@@ -4251,6 +4251,7 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !...@@ -4251,6 +4251,7 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
4251 man.hash.addOptional(wasm.base.options.stack_size_override);4251 man.hash.addOptional(wasm.base.options.stack_size_override);
4252 man.hash.add(wasm.base.options.build_id);4252 man.hash.add(wasm.base.options.build_id);
4253 man.hash.add(wasm.base.options.import_memory);4253 man.hash.add(wasm.base.options.import_memory);
4254 man.hash.add(wasm.base.options.export_memory);
4254 man.hash.add(wasm.base.options.import_table);4255 man.hash.add(wasm.base.options.import_table);
4255 man.hash.add(wasm.base.options.export_table);4256 man.hash.add(wasm.base.options.export_table);
4256 man.hash.addOptional(wasm.base.options.initial_memory);4257 man.hash.addOptional(wasm.base.options.initial_memory);
...@@ -4338,6 +4339,10 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !...@@ -4338,6 +4339,10 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
4338 try argv.append("--import-memory");4339 try argv.append("--import-memory");
4339 }4340 }
43404341
4342 if (wasm.base.options.export_memory) {
4343 try argv.append("--export-memory");
4344 }
4345
4341 if (wasm.base.options.import_table) {4346 if (wasm.base.options.import_table) {
4342 assert(!wasm.base.options.export_table);4347 assert(!wasm.base.options.export_table);
4343 try argv.append("--import-table");4348 try argv.append("--import-table");
src/main.zig+7
...@@ -544,6 +544,7 @@ const usage_build_generic =...@@ -544,6 +544,7 @@ const usage_build_generic =
544 \\ -dead_strip (Darwin) remove functions and data that are unreachable by the entry point or exported symbols544 \\ -dead_strip (Darwin) remove functions and data that are unreachable by the entry point or exported symbols
545 \\ -dead_strip_dylibs (Darwin) remove dylibs that are unreachable by the entry point or exported symbols545 \\ -dead_strip_dylibs (Darwin) remove dylibs that are unreachable by the entry point or exported symbols
546 \\ --import-memory (WebAssembly) import memory from the environment546 \\ --import-memory (WebAssembly) import memory from the environment
547 \\ --export-memory (WebAssembly) export memory to the host (Default unless --import-memory used)
547 \\ --import-symbols (WebAssembly) import missing symbols from the host environment548 \\ --import-symbols (WebAssembly) import missing symbols from the host environment
548 \\ --import-table (WebAssembly) import function table from the host environment549 \\ --import-table (WebAssembly) import function table from the host environment
549 \\ --export-table (WebAssembly) export function table to the host environment550 \\ --export-table (WebAssembly) export function table to the host environment
...@@ -787,6 +788,7 @@ fn buildOutputType(...@@ -787,6 +788,7 @@ fn buildOutputType(
787 var linker_allow_shlib_undefined: ?bool = null;788 var linker_allow_shlib_undefined: ?bool = null;
788 var linker_bind_global_refs_locally: ?bool = null;789 var linker_bind_global_refs_locally: ?bool = null;
789 var linker_import_memory: ?bool = null;790 var linker_import_memory: ?bool = null;
791 var linker_export_memory: ?bool = null;
790 var linker_import_symbols: bool = false;792 var linker_import_symbols: bool = false;
791 var linker_import_table: bool = false;793 var linker_import_table: bool = false;
792 var linker_export_table: bool = false;794 var linker_export_table: bool = false;
...@@ -1419,6 +1421,8 @@ fn buildOutputType(...@@ -1419,6 +1421,8 @@ fn buildOutputType(
1419 }1421 }
1420 } else if (mem.eql(u8, arg, "--import-memory")) {1422 } else if (mem.eql(u8, arg, "--import-memory")) {
1421 linker_import_memory = true;1423 linker_import_memory = true;
1424 } else if (mem.eql(u8, arg, "--export-memory")) {
1425 linker_export_memory = true;
1422 } else if (mem.eql(u8, arg, "--import-symbols")) {1426 } else if (mem.eql(u8, arg, "--import-symbols")) {
1423 linker_import_symbols = true;1427 linker_import_symbols = true;
1424 } else if (mem.eql(u8, arg, "--import-table")) {1428 } else if (mem.eql(u8, arg, "--import-table")) {
...@@ -1982,6 +1986,8 @@ fn buildOutputType(...@@ -1982,6 +1986,8 @@ fn buildOutputType(
1982 linker_bind_global_refs_locally = true;1986 linker_bind_global_refs_locally = true;
1983 } else if (mem.eql(u8, arg, "--import-memory")) {1987 } else if (mem.eql(u8, arg, "--import-memory")) {
1984 linker_import_memory = true;1988 linker_import_memory = true;
1989 } else if (mem.eql(u8, arg, "--export-memory")) {
1990 linker_export_memory = true;
1985 } else if (mem.eql(u8, arg, "--import-symbols")) {1991 } else if (mem.eql(u8, arg, "--import-symbols")) {
1986 linker_import_symbols = true;1992 linker_import_symbols = true;
1987 } else if (mem.eql(u8, arg, "--import-table")) {1993 } else if (mem.eql(u8, arg, "--import-table")) {
...@@ -3113,6 +3119,7 @@ fn buildOutputType(...@@ -3113,6 +3119,7 @@ fn buildOutputType(
3113 .linker_allow_shlib_undefined = linker_allow_shlib_undefined,3119 .linker_allow_shlib_undefined = linker_allow_shlib_undefined,
3114 .linker_bind_global_refs_locally = linker_bind_global_refs_locally,3120 .linker_bind_global_refs_locally = linker_bind_global_refs_locally,
3115 .linker_import_memory = linker_import_memory,3121 .linker_import_memory = linker_import_memory,
3122 .linker_export_memory = linker_export_memory,
3116 .linker_import_symbols = linker_import_symbols,3123 .linker_import_symbols = linker_import_symbols,
3117 .linker_import_table = linker_import_table,3124 .linker_import_table = linker_import_table,
3118 .linker_export_table = linker_export_table,3125 .linker_export_table = linker_export_table,