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

zig build-exe fails to link objects properly and provides no way of disabling -dead_strip #13729

Open
fschutt opened this issue Dec 1, 2022 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior linking
Milestone

Comments

@fschutt
Copy link

fschutt commented Dec 1, 2022

Zig Version

0.10.0

Steps to Reproduce and Observed Behavior

The wasmer CLI is currently using zig build-exe to cross-compile .wasm files to .exe files across platforms. We support multiple compilers: llvm, cranelift and our "singlepass" compiler.

For some reason, wasmer / zig build-exe fails to link the files together, but only with llvm-compiled files on -target x86_64-macos-none and -target x86_64-windows-gnu (cross-compiling from my Mac on aarch64-apple-darwin).

I've attached a minimum testcase here:

zigbug.zip

cd zigbug
./test.sh

error(link): undefined reference to symbol 'wasmer_vm_raise_trap'
error(link):   first referenced in '/Users/fs/Desktop/zigbug/python.o'
error(link): undefined reference to symbol 'wasmer_vm_f64_floor'
error(link):   first referenced in '/Users/fs/Desktop/zigbug/python.o'
error(link): undefined reference to symbol 'wasmer_vm_f64_ceil'
error(link):   first referenced in '/Users/fs/Desktop/zigbug/python.o'
error: UndefinedSymbolReference

The object files are the unpacked version of libwasmer.a, I just unpacked the .a file into a folder and the test script links all object files in the folder.

The disassembly (in disassembly.txt) shows that wasmer_vm_raise_trap is in the object file and the script does link the object file. The problem is now: why can't the linker find these functions, even if the object file is given as an input?

This issue does only seem to happen when the python.o file gets compiled with LLVM, with other compilers it works.

Our assumption is that zig ld (called internally) passes some flags to ld, especially enabling -dead_strip (there is no -no_dead_strip in zig). In general: can there be some way to manually pass linker flags? If someone wants to use more experimental linker flags, then you'd first have to wait for zig to add support, which isn't great.

Issue first appeared here: wasmerio/wasmer#3374 (comment)

Expected Behavior

The script should output a ./output file, which should be a Python CLI on target x86_64-macos-none.

@fschutt fschutt added the bug Observed behavior contradicts documented or intended behavior label Dec 1, 2022
@Vexu Vexu added the linking label Dec 5, 2022
@Vexu Vexu added this to the 0.11.0 milestone Dec 5, 2022
@kubkon
Copy link
Member

kubkon commented Apr 12, 2023

I am not sure if this is a solution to your issue but you can disable dead code stripping in build.zig with exe.link_gc_sections = false where exe: *std.Build.CompileStep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior linking
Projects
None yet
Development

No branches or pull requests

4 participants