Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make this repo build again #20

Open
voigt opened this issue Jun 3, 2022 · 2 comments
Open

Make this repo build again #20

voigt opened this issue Jun 3, 2022 · 2 comments

Comments

@voigt
Copy link

voigt commented Jun 3, 2022

Hey,

first of all: thank you for the great work! Really happy to see a few people taking care of Zigs Wasm capabilities!

Unfortunately, this repo aged a bit, I wasn't able to make it work with zig-0.8.0 as wasm-zig is a little more ahead... Therefore I started to update this repo to make it runnable with zig-master and consequently with a recent version of the wasmtime API.

I anticipated a three-step process:

  1. update zig & gyro dependency, fix existing code (this issue; PR)
  2. update wasmtime API, adjust code to use a recent version (Updating to new (breaking) Wasmtime API #15)
  3. translate additional examples wasmtime examples repo (no issue yet)

I'm currently in phase 1 and working on a PR. While gyro build & gyro build test already work, I'm now about to fix the examples: simple and gcd already work on macos, Linux and Windows examples fail to build entirely.

On Linux I get weird linking errors, which is the main reason I'm reaching out here. As macos build basically works, I see no reason why it shouldn't on Linux. However, I have no experience investigating such errors - my gut feeling tells me I either need an additional compile flag, there is an issue in wasm-zig or even an issue in the nightly of the zig version used.

Here an example of the linking errors:

gyro build run -Dexample=simple --search-prefix "$(pwd)/$WASMTIME"

Click to expand!
$ gyro build run -Dexample=simple --search-prefix "$(pwd)/$WASMTIME"
LLD Link... ld.lld: error: undefined symbol: _Unwind_Resume
>>> referenced by wasmtime.2gagyan6-cgu.13
>>>               wasmtime.wasmtime.2gagyan6-cgu.13.rcgu.o:(_$LT$smallvec..SmallVec$LT$A$GT$$u20$as$u20$core..ops..drop..Drop$GT$::drop::h0c82f3937d4ad6ba) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced by wasmtime.2gagyan6-cgu.13
>>>               wasmtime.wasmtime.2gagyan6-cgu.13.rcgu.o:(_$LT$smallvec..SmallVec$LT$A$GT$$u20$as$u20$core..ops..drop..Drop$GT$::drop::hefb0aab2e138c30b) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced by wasmtime.2gagyan6-cgu.13
>>>               wasmtime.wasmtime.2gagyan6-cgu.13.rcgu.o:(smallvec::SmallVec$LT$A$GT$::from_elem::hbdeff6b68cc78aa3) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced 2136 more times

ld.lld: error: undefined symbol: _Unwind_Backtrace
>>> referenced by libunwind.rs:90 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/std/src/../../backtrace/src/backtrace/libunwind.rs:90)
>>>               std-6f77337c1826707d.std.4qsyoc55-cgu.0.rcgu.o:(_$LT$std..sys_common..backtrace.._print..DisplayBacktrace$u20$as$u20$core..fmt..Display$GT$::fmt::hf092b5883b4b2e50) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced by wasmtime_runtime.594lqgj0-cgu.12
>>>               wasmtime_runtime-5268fe14b5b9cb0e.wasmtime_runtime.594lqgj0-cgu.12.rcgu.o:(backtrace::backtrace::trace::h6cb5f3002488cf98) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced by backtrace.5jl3q0p6-cgu.9
>>>               backtrace-b1b27a0e5161b8ca.backtrace.5jl3q0p6-cgu.9.rcgu.o:(backtrace::backtrace::trace::hca0d710b8f9deb48) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: _Unwind_GetIP
>>> referenced by libunwind.rs:43 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/std/src/../../backtrace/src/backtrace/libunwind.rs:43)
>>>               std-6f77337c1826707d.std.4qsyoc55-cgu.0.rcgu.o:(std::sys_common::backtrace::_print_fmt::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::h76dfbe2a87a43772) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced by libunwind.rs:43 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/std/src/../../backtrace/src/backtrace/libunwind.rs:43)
>>>               std-6f77337c1826707d.std.4qsyoc55-cgu.0.rcgu.o:(std::sys_common::backtrace::_print_fmt::_$u7b$$u7b$closure$u7d$$u7d$::hab3cbf6657151fae) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced by libunwind.rs:43 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/std/src/../../backtrace/src/backtrace/libunwind.rs:43)
>>>               std-6f77337c1826707d.std.4qsyoc55-cgu.0.rcgu.o:(std::backtrace_rs::symbolize::gimli::resolve::hd3226e61f21019de) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced 10 more times

ld.lld: error: undefined symbol: _Unwind_GetDataRelBase
>>> referenced by gcc.rs:309 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/panic_unwind/src/gcc.rs:309)
>>>               panic_unwind-e238540f8faf9927.panic_unwind.3kbstr8o-cgu.0.rcgu.o:(core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::h43c444d62f54d802) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced by gcc.rs:309 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/panic_unwind/src/gcc.rs:309)
>>>               panic_unwind-e238540f8faf9927.panic_unwind.3kbstr8o-cgu.0.rcgu.o:(panic_unwind::real_imp::find_eh_action::_$u7b$$u7b$closure$u7d$$u7d$::h4fbbdfaa9a570c9c) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: _Unwind_GetTextRelBase
>>> referenced by gcc.rs:308 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/panic_unwind/src/gcc.rs:308)
>>>               panic_unwind-e238540f8faf9927.panic_unwind.3kbstr8o-cgu.0.rcgu.o:(core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::h46d93eef11d58b07) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced by gcc.rs:308 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/panic_unwind/src/gcc.rs:308)
>>>               panic_unwind-e238540f8faf9927.panic_unwind.3kbstr8o-cgu.0.rcgu.o:(panic_unwind::real_imp::find_eh_action::_$u7b$$u7b$closure$u7d$$u7d$::h47fb3c2732dca9b2) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: _Unwind_DeleteException
>>> referenced by gcc.rs:78 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/panic_unwind/src/gcc.rs:78)
>>>               panic_unwind-e238540f8faf9927.panic_unwind.3kbstr8o-cgu.0.rcgu.o:(__rust_panic_cleanup) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: _Unwind_RaiseException
>>> referenced by gcc.rs:62 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/panic_unwind/src/gcc.rs:62)
>>>               panic_unwind-e238540f8faf9927.panic_unwind.3kbstr8o-cgu.0.rcgu.o:(__rust_start_panic) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: _Unwind_GetLanguageSpecificData
>>> referenced by gcc.rs:300 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/panic_unwind/src/gcc.rs:300)
>>>               panic_unwind-e238540f8faf9927.panic_unwind.3kbstr8o-cgu.0.rcgu.o:(rust_eh_personality) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: _Unwind_GetIPInfo
>>> referenced by gcc.rs:302 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/panic_unwind/src/gcc.rs:302)
>>>               panic_unwind-e238540f8faf9927.panic_unwind.3kbstr8o-cgu.0.rcgu.o:(rust_eh_personality) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: _Unwind_GetRegionStart
>>> referenced by gcc.rs:307 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/panic_unwind/src/gcc.rs:307)
>>>               panic_unwind-e238540f8faf9927.panic_unwind.3kbstr8o-cgu.0.rcgu.o:(rust_eh_personality) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: _Unwind_SetGR
>>> referenced by gcc.rs:251 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/panic_unwind/src/gcc.rs:251)
>>>               panic_unwind-e238540f8faf9927.panic_unwind.3kbstr8o-cgu.0.rcgu.o:(rust_eh_personality) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: _Unwind_SetIP
>>> referenced by gcc.rs:254 (/rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/panic_unwind/src/gcc.rs:254)
>>>               panic_unwind-e238540f8faf9927.panic_unwind.3kbstr8o-cgu.0.rcgu.o:(rust_eh_personality) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: __deregister_frame
>>> referenced by wasmtime_jit.38whm15l-cgu.11
>>>               wasmtime_jit-64b0bceb8bfbac2b.wasmtime_jit.38whm15l-cgu.11.rcgu.o:(_$LT$wasmtime_jit..code_memory..CodeMemoryEntry$u20$as$u20$core..ops..drop..Drop$GT$::drop::hd0cb240d41257406) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: __register_frame
>>> referenced by wasmtime_jit.38whm15l-cgu.11
>>>               wasmtime_jit-64b0bceb8bfbac2b.wasmtime_jit.38whm15l-cgu.11.rcgu.o:(wasmtime_jit::code_memory::CodeMemory::publish::hafc3e987784edd10) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: _Unwind_GetCFA
>>> referenced by backtrace.5jl3q0p6-cgu.9
>>>               backtrace-b1b27a0e5161b8ca.backtrace.5jl3q0p6-cgu.9.rcgu.o:(core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::hca7bc2ec8b552c8a (.llvm.14183395609191761177)) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced by backtrace.5jl3q0p6-cgu.9
>>>               backtrace-b1b27a0e5161b8ca.backtrace.5jl3q0p6-cgu.9.rcgu.o:(backtrace::backtrace::Frame::sp::hb8cf940b60e44949) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced by backtrace.5jl3q0p6-cgu.9
>>>               backtrace-b1b27a0e5161b8ca.backtrace.5jl3q0p6-cgu.9.rcgu.o:(backtrace::capture::Backtrace::create::_$u7b$$u7b$closure$u7d$$u7d$::h0062f0a6638f7a48 (.llvm.14183395609191761177)) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a

ld.lld: error: undefined symbol: _Unwind_FindEnclosingFunction
>>> referenced by backtrace.5jl3q0p6-cgu.9
>>>               backtrace-b1b27a0e5161b8ca.backtrace.5jl3q0p6-cgu.9.rcgu.o:(core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::hca7bc2ec8b552c8a (.llvm.14183395609191761177)) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced by backtrace.5jl3q0p6-cgu.9
>>>               backtrace-b1b27a0e5161b8ca.backtrace.5jl3q0p6-cgu.9.rcgu.o:(core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::hca7bc2ec8b552c8a (.llvm.14183395609191761177)) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced by backtrace.5jl3q0p6-cgu.9
>>>               backtrace-b1b27a0e5161b8ca.backtrace.5jl3q0p6-cgu.9.rcgu.o:(backtrace::capture::Backtrace::create::_$u7b$$u7b$closure$u7d$$u7d$::h0062f0a6638f7a48 (.llvm.14183395609191761177)) in archive /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib/libwasmtime.a
>>> referenced 1 more times
error: LLDReportedFailure
simple...The following command exited with error code 1:
/usr/local/bin/zig build-exe /home/ubuntu/wasmtime-zig/examples/simple.zig -lwasmtime -lc --cache-dir /home/ubuntu/wasmtime-zig/zig-cache --global-cache-dir /root/.cache/zig --name simple --pkg-begin wasmtime /home/ubuntu/wasmtime-zig/src/main.zig --pkg-begin wasm /home/ubuntu/wasmtime-zig/.gyro/wasm-zig-zigwasm-github.com-3a96556d/pkg/src/main.zig --pkg-end --pkg-end -L /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/lib -isystem /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api/include --enable-cache 
The following command exited with error code 1 (expected 0):
cd . && /home/ubuntu/wasmtime-zig/zig-cache/o/c69a7c4ebdb9350db37863b2f992160c/build /usr/local/bin/zig . zig-cache /root/.cache/zig run -Dexample=simple --search-prefix /home/ubuntu/wasmtime-zig/wasmtime-v0.24.0-x86_64-linux-c-api

Windows fails as well, but I didn't spend any time investigating for now.

I'd really appreciate any idea/advice on how I to get Linux (and potentially even Windows ;) ) examples to build!

@kubkon
Copy link
Member

kubkon commented Jun 4, 2022

I took a look and the missing symbols are provided by libunwind (also note that the errors pop up in the first place since Zig/lld will prefer linking against static libs first). Zig ships libunwind and so we only need to explicitly specify that we want to link against in the build.zig script. Here's the diff, feel free to use it and no attribution is necessary:

diff --git a/build.zig b/build.zig
index 0b07c15..9c5ab2b 100644
--- a/build.zig
+++ b/build.zig
@@ -33,10 +33,19 @@ pub fn build(b: *std.build.Builder) !void {
         .source = .{ .path = "src/main.zig" },
         .dependencies = &.{pkgs.wasm},
     });
-    if (builtin.os.tag == .windows) {
-        simple_exe.linkSystemLibrary("wasmtime.dll");
-    } else {
-        simple_exe.linkSystemLibrary("wasmtime");
+    switch (builtin.os.tag) {
+        .windows => {
+            simple_exe.linkSystemLibrary("wasmtime.dll");
+            simple_exe.linkSystemLibrary("unwind");
+        },
+        .linux => {
+            simple_exe.linkSystemLibrary("wasmtime");
+            simple_exe.linkSystemLibrary("unwind");
+        },
+        .macos => {
+            simple_exe.linkSystemLibrary("wasmtime");
+        },
+        else => unreachable,
     }
     simple_exe.linkLibC();
     simple_exe.step.dependOn(b.getInstallStep());

Note that on macOS we always link dynamically and libcand libunwind are provided by the OS and always available so no need to link against it there.

Once you get everything building again, I'd really appreciate a PR with the fixed :-)

@voigt
Copy link
Author

voigt commented Jun 9, 2022

Thanks a lot, @kubkon, for looking into the problem.

I was able to incorporate your changes into build.zig. While Linux finally builds, Windows still complains about "FILENOTFOUND" stuff...

D:\a\wasmtime-zig\wasmtime-zig\zig-windows-x86_64-0.10.0-dev.2473+e498fb155\lib\std\os\windows.zig:1593:32: 0x7ff78d4627eb in td.os.windows.CreateProcessW (build.obj)
            .FILE_NOT_FOUND => return error.FileNotFound,

I'd appreciate it if you could have another look.

Apart from that, I started fixing the examples. This might take a couple of days (currently a bit limited in time)

Once you get everything building again, I'd appreciate a PR with the fixed :-)

This is my goal :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants