unsupported linker arg: `-exported_symbols_list`
Zig Version
0.12.0
Steps to Reproduce and Observed Behavior
I am using a toolchain in Bazel which uses Zig to compile Rust. I am running into a problem on MacOS Arm64 only. It works fine on Linux.
This is because Rust only adds that option on Darwin like systems: https://github.com/rust-lang/rust/blob/c23f07d8c56c51b5e634bda55daca2b073306340/compiler/rustc_codegen_ssa/src/back/linker.rs#L811
From inspecting the command line generated by Bazel for the Rust compilation, these options are passed to Zig:
"-Wl,-exported_symbols_list"
"-Wl,/path/to/list"
Zig fails with the following error:
= note: error: unsupported linker arg: -exported_symbols_list
I checked main.zig and indeed it looks like the support for this option is missing.
Expected Behavior
Zig works without failing
It also doesn't seem to support -Wl,--dynamic-list=/path/to/file on Linux. ld.lld installed from LLVM appears to support it (albeit with warnings that local symbols can't be turned global).
Hello people, we are in the exact same use case where we use a toolchain in Bazel which uses Zig to compile Rust. Did you manage to workaround it @guw?
@ArchangelX360 I disabled Zig (hermetic_cc_toolchain) on MacOS. We are using the Bazel default there (non-hermetic).
I use a zig-ld.lld wrapper that replaces the flag with -Wl,--export-dynamic.
It's not great, because it just bloats the resulting binary unnecessarily by turning every symbol dynamic.
Ok, thank you both! I hope the support for it will happen soon 🤞🏼
Using zig cc for Rust cross-compiling like suggested at https://actually.fyi/posts/zig-makes-rust-cross-compilation-just-work/ seemed too good to be true.
Encountered the error with https://github.com/anttiharju/compare-changes/tree/2cb94de6c18ada42a33a8f1426d352dc57a83ad5 when trying to
cargo add clap --features derive
followed by
TARGET=aarch64-apple-darwin CC="./.release/zcc.sh" cargo build --target "$TARGET"
but interestingly
cargo add clap
appears to be fine.
error output
Compiling proc-macro2 v1.0.103
Compiling unicode-ident v1.0.22
Compiling quote v1.0.42
Compiling heck v0.5.0
Compiling syn v2.0.111
Compiling clap_derive v4.5.49
error: linking with `/Users/antti/anttiharju/compare-changes/./.release/zcc.sh` failed: exit status: 1
|
= note: "/Users/antti/anttiharju/compare-changes/./.release/zcc.sh" "-Wl,-exported_symbols_list" "-Wl,/var/folders/cs/w139gbxx41n01gt_j2w3g52r0000gn/T/rustcWUmtiL/list" "/var/folders/cs/w139gbxx41n01gt_j2w3g52r0000gn/T/rustcWUmtiL/symbols.o" "<16 object files omitted>" "/var/folders/cs/w139gbxx41n01gt_j2w3g52r0000gn/T/rustcWUmtiL/rmeta.o" "<1 object files omitted>" "/Users/antti/anttiharju/compare-changes/target/debug/deps/{libheck-b604e8bf463e779e,libsyn-15ddb0e80a0d3a0b,libquote-0b952226778a52fa,libproc_macro2-64f84243080c59a5,libunicode_ident-a509f239f93a2f1f}.rlib" "<sysroot>/lib/rustlib/aarch64-apple-darwin/lib/{libproc_macro-*,librustc_literal_escaper-*,librustc_std_workspace_std-*,libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,libcfg_if-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,liblibc-*,librustc_std_workspace_core-*,liballoc-*,libcore-*,libcompiler_builtins-*}.rlib" "-lSystem" "-lc" "-lm" "-arch" "arm64" "-mmacosx-version-min=11.3.0" "-o" "/Users/antti/anttiharju/compare-changes/target/debug/deps/libclap_derive-87d62b7bd9a4bf8f.dylib" "-Wl,-dead_strip" "-dynamiclib" "-nodefaultlibs"
= note: some arguments are omitted. use `--verbose` to show all linker arguments
= note: error: unsupported linker arg: -exported_symbols_list
error: could not compile `clap_derive` (lib) due to 1 previous error
$ zig version
0.14.1
@guw may be worth adding labels os-macos and zig cc for issue discoverability
Just a note on how this stuff is intended to work:
Zig does not support arbitrary linker arguments; there is no separate linker. The compiler and linker are one and the same.
So, it is the job of the "zig cc" frontend to translate C-compatible command line arguments into configuration that the frontend understands. When this mechanism encounters "linker args", it interprets them and configures the frontend. They are not sent in an opaque form to the linker.
In this case, we already have this concept available, and it's already hooked up to --export=foobar. The CLI parsing code can simply append the extra symbols to this array in src/main.zig:
linker_export_symbol_names.append(arena, foobar);
So this is in fact a contributor-friendly issue.