diff options
| author | Luuk de Gram <luuk@degram.dev> | 2021-12-12 16:21:25 +0100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-12-14 14:02:23 -0800 |
| commit | 50201e1c30e0d71bafd643e9804d55eceb7b3542 (patch) | |
| tree | f600a02163b1b3bb52bb9df28413df1035d4672f /src/main.zig | |
| parent | e563b166b2b70975899c84beb425c8739d05ed65 (diff) | |
| download | zig-50201e1c30e0d71bafd643e9804d55eceb7b3542.tar.gz zig-50201e1c30e0d71bafd643e9804d55eceb7b3542.zip | |
wasm-linker: Allow specifying symbols to be exported
Notating a symbol to be exported in code will only tell the linker
where to find this symbol, so other object files can find it. However, this does not mean
said symbol will also be exported to the host environment. Currently, we 'fix' this by force
exporting every single symbol that is visible. This creates bigger binaries and means host environments
have access to symbols that they perhaps shouldn't have. Now, users can tell Zig which symbols
are to be exported, meaning all other symbols that are not specified will not be exported.
Another change is we now support `-rdynamic` in the wasm linker as well, meaning all symbols will
be put in the dynamic symbol table. This is the same behavior as with ELF. This means there's a 3rd strategy
users will have to build their wasm binary.
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig index 761935c897..57342702ee 100644 --- a/src/main.zig +++ b/src/main.zig @@ -434,6 +434,7 @@ const usage_build_generic = \\ --initial-memory=[bytes] (WebAssembly) initial size of the linear memory \\ --max-memory=[bytes] (WebAssembly) maximum size of the linear memory \\ --global-base=[addr] (WebAssembly) where to start to place global data + \\ --export=[value] (WebAssembly) Force a symbol to be exported \\ \\Test Options: \\ --test-filter [text] Skip tests that do not match filter @@ -711,6 +712,9 @@ fn buildOutputType( var test_exec_args = std.ArrayList(?[]const u8).init(gpa); defer test_exec_args.deinit(); + var linker_export_symbol_names = std.ArrayList([]const u8).init(gpa); + defer linker_export_symbol_names.deinit(); + // This package only exists to clean up the code parsing --pkg-begin and // --pkg-end flags. Use dummy values that are safe for the destroy call. var pkg_tree_root: Package = .{ @@ -1175,6 +1179,8 @@ fn buildOutputType( linker_max_memory = parseIntSuffix(arg, "--max-memory=".len); } else if (mem.startsWith(u8, arg, "--global-base=")) { linker_global_base = parseIntSuffix(arg, "--global-base=".len); + } else if (mem.startsWith(u8, arg, "--export=")) { + try linker_export_symbol_names.append(arg["--export=".len..]); } else if (mem.eql(u8, arg, "-Bsymbolic")) { linker_bind_global_refs_locally = true; } else if (mem.eql(u8, arg, "--debug-compile-errors")) { @@ -1554,6 +1560,8 @@ fn buildOutputType( linker_max_memory = parseIntSuffix(arg, "--max-memory=".len); } else if (mem.startsWith(u8, arg, "--global-base=")) { linker_global_base = parseIntSuffix(arg, "--global-base=".len); + } else if (mem.startsWith(u8, arg, "--export=")) { + try linker_export_symbol_names.append(arg["--export=".len..]); } else if (mem.eql(u8, arg, "-z")) { i += 1; if (i >= linker_args.items.len) { @@ -2438,6 +2446,7 @@ fn buildOutputType( .linker_initial_memory = linker_initial_memory, .linker_max_memory = linker_max_memory, .linker_global_base = linker_global_base, + .linker_export_symbol_names = linker_export_symbol_names.items, .linker_z_nodelete = linker_z_nodelete, .linker_z_notext = linker_z_notext, .linker_z_defs = linker_z_defs, |
