diff options
| author | Luuk de Gram <luuk@degram.dev> | 2023-08-31 17:12:12 +0200 |
|---|---|---|
| committer | Luuk de Gram <luuk@degram.dev> | 2023-11-03 12:48:52 +0100 |
| commit | 58618afaee7c14feedad4f115e9a4468bac4c529 (patch) | |
| tree | 90a4a16f016817ec171c49b4f5e1b42dc504e0bd | |
| parent | 1cb7a01b25349c42dbe207b68f9e19b92890b3d2 (diff) | |
| download | zig-58618afaee7c14feedad4f115e9a4468bac4c529.tar.gz zig-58618afaee7c14feedad4f115e9a4468bac4c529.zip | |
wasm-linker: correctly pass --shared and --pie
When linking a WebAssembly binary using wasm-ld, we will now correctly pass
--shared when building a dynamic library and --pie when `-fpic` is given.
This is a breaking change and users who currently build dynamic libraries
for WebAssembly but wish to have an executable without a main, should use
build-exe instead while supplying `-fno-entry`.
We also verify the user does not supply --export-memory when -dynamic is
enabled. By default we set this flag now to 'false' when `-dynamic` is used
to ensure we do not enable it as it's enabled by default for regular
WebAssembly binaries.
| -rw-r--r-- | src/link/Wasm.zig | 7 | ||||
| -rw-r--r-- | src/main.zig | 10 |
2 files changed, 17 insertions, 0 deletions
diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 135803007a..ad9f8266f0 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -4548,6 +4548,13 @@ fn linkWithLLD(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) ! try argv.append("--no-entry"); } + if (wasm.base.options.output_mode == .Lib and wasm.base.options.link_mode == .Dynamic) { + try argv.append("--shared"); + } + if (wasm.base.options.pie) { + try argv.append("--pie"); + } + // XXX - TODO: add when wasm-ld supports --build-id. // if (wasm.base.options.build_id) { // try argv.append("--build-id=tree"); diff --git a/src/main.zig b/src/main.zig index fc7e43b16f..95174ae344 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2627,6 +2627,16 @@ fn buildOutputType( if (single_threaded == null) { single_threaded = true; } + if (link_mode) |mode| { + if (mode == .Dynamic) { + if (linker_export_memory != null and linker_export_memory.?) { + fatal("flags '-dynamic' and '--export-memory' are incompatible", .{}); + } + // User did not supply `--export-memory` which is incompatible with -dynamic, therefore + // set the flag to false to ensure it does not get enabled by default. + linker_export_memory = false; + } + } if (wasi_exec_model) |model| { if (model == .reactor) { if (linker_no_entry != null and !linker_no_entry.?) { |
