Skip to content

Commit 76742b4

Browse files
hunshcnclaude
andcommitted
refactor: move -z,now filtering to _LINKER_OPTIONS_DENYLIST
Instead of a custom _remove_bind_now function in link.bzl, add the flags to the existing _LINKER_OPTIONS_DENYLIST in context.bzl. This is consistent with how -Wl,--gc-sections and -pie are already handled. The Go toolchain (go build) does not pass -z,relro or -z,now either, so filtering the compound flag entirely aligns with go build behavior. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 64476fd commit 76742b4

2 files changed

Lines changed: 2 additions & 36 deletions

File tree

go/private/actions/link.bzl

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ def emit_link(
5757
# in archives via CGO_LDFLAGS if it's needed.
5858
extldflags = [f for f in extldflags_from_cc_toolchain(go) if f not in ("-lstdc++", "-lc++", "-static")]
5959

60-
# Remove -z,now (BIND_NOW) from CC toolchain flags. The Go toolchain
61-
# (go build) does not pass this flag to the linker. BIND_NOW forces the
62-
# dynamic linker to resolve all undefined symbols at load time, which
63-
# breaks Go libraries that use dlopen/dlsym to load symbols at runtime
64-
# (e.g., NVIDIA's go-nvml). See #4377.
65-
extldflags = _remove_bind_now(extldflags)
66-
6760
if go.coverage_enabled:
6861
extldflags.append("--coverage")
6962
gc_linkopts = gc_linkopts + go.mode.gc_linkopts
@@ -241,32 +234,3 @@ def _extract_extldflags(gc_linkopts, extldflags):
241234
else:
242235
filtered_gc_linkopts.append(opt)
243236
return filtered_gc_linkopts, extldflags
244-
245-
def _remove_bind_now(extldflags):
246-
"""Removes -z,now (BIND_NOW) from linker flags while preserving other flags.
247-
248-
The CC toolchain typically includes -Wl,-z,relro,-z,now for security
249-
hardening. While -z,relro is safe to keep, -z,now forces all dynamic
250-
symbols to be resolved at load time, which is incompatible with Go
251-
libraries that load symbols at runtime via dlopen/dlsym (e.g., go-nvml).
252-
The standard Go toolchain (go build) does not set -z,now.
253-
254-
Args:
255-
extldflags: a list of flags to be passed to the external linker.
256-
257-
Return:
258-
A new list with -z,now removed from any compound or standalone flags.
259-
"""
260-
result = []
261-
for f in extldflags:
262-
if ",-z,now" in f:
263-
# Remove -z,now from compound flags like -Wl,-z,relro,-z,now
264-
cleaned = f.replace(",-z,now", "")
265-
if cleaned and cleaned != "-Wl":
266-
result.append(cleaned)
267-
elif f in ("-Wl,-z,now", "-z,now"):
268-
# Skip standalone -z,now flags
269-
continue
270-
else:
271-
result.append(f)
272-
return result

go/private/context.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ _COMPILER_OPTIONS_DENYLIST = dict({
131131
_LINKER_OPTIONS_DENYLIST = {
132132
"-Wl,--gc-sections": None,
133133
"-pie": None, # See https://github.com/bazelbuild/rules_go/issues/3691.
134+
"-Wl,-z,relro,-z,now": None, # See https://github.com/bazel-contrib/rules_go/issues/4377.
135+
"-Wl,-z,now": None, # See https://github.com/bazel-contrib/rules_go/issues/4377.
134136
}
135137

136138
_UNSUPPORTED_FEATURES = [

0 commit comments

Comments
 (0)