Fix zig cc/c++ mishandling of -u (undefined symbol) linker flag#242
Open
electronjoe wants to merge 1 commit intouber:mainfrom
Open
Fix zig cc/c++ mishandling of -u (undefined symbol) linker flag#242electronjoe wants to merge 1 commit intouber:mainfrom
electronjoe wants to merge 1 commit intouber:mainfrom
Conversation
The `-u SYMBOL` flag tells the linker to treat SYMBOL as undefined, forcing it to be resolved from archives or shared libraries. This is a standard linker feature used to: - Force inclusion of specific archive members (e.g., static initializers, plugin registration, constructor functions) - Pull in coverage/profiling runtime symbols (e.g., __llvm_profile_runtime) - Ensure weak symbol overrides are linked - Implement plugin architectures that require specific entry points Currently, Zig's cc/c++ driver misinterprets `-u SYMBOL` - it attempts to open SYMBOL as a filename rather than passing it to the linker. This breaks any toolchain or build system that relies on this flag. This is a known Zig bug tracked at: https://codeberg.org/ziglang/zig/issues/30613 The fix (https://codeberg.org/ziglang/zig/pulls/30749) is pending merge. This patch works around the issue by transforming `-u SYMBOL` into `-Wl,-u,SYMBOL`, which explicitly passes the flag through to the linker. Discovered while debugging `bazel coverage` failures in a mixed C++/Rust repository (https://github.com/electronjoe/bazel-cpp-rust-codecov), where rustc passes `-u __llvm_profile_runtime` for coverage instrumentation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #243
The
-u SYMBOLflag tells the linker to treat SYMBOL as undefined, forcing it to be resolved from archives or shared libraries. This is a standard linker feature used to:Currently, Zig's cc/c++ driver misinterprets
-u SYMBOL- it attempts to open SYMBOL as a filename rather than passing it to the linker. This breaks any toolchain or build system that relies on this flag.This is a known Zig bug tracked at:
https://codeberg.org/ziglang/zig/issues/30613
The fix (https://codeberg.org/ziglang/zig/pulls/30749) is pending merge. I'll post on the issue and merge request at Zig just to +1 the need for a fix - and I'm not sure whether you want to integrate a work around here in
hermetic_cc_toolchainor not vs waiting for an upstream fix.This patch works around the issue by transforming
-u SYMBOLinto-Wl,-u,SYMBOL, which explicitly passes the flag through to the linker.Discovered while debugging
bazel coveragefailures in a mixed C++/Rust repository (minimal reproduction at https://github.com/electronjoe/bazel-cpp-rust-codecov). This repo useshermetic_cc_compilerandrules_rust-> rustc passes-u __llvm_profile_runtimefor coverage instrumentation. If you would like to reproduce the original issue, you can open a codespace from the repo,bazel coverage //..., observe success - then roll back this patch in the repo and try again to observe the failure mode.