Skip to content

Commit

Permalink
Fix hermetic toolchain linking before Go 1.21
Browse files Browse the repository at this point in the history
Go toolchain checks which linker flags are available by running the
linker on a temporary file. Before Go 1.21, this command was run in a
temporary directory, but this has now been fixed upstream.
golang/go#59952

This change will not be needed for Go versions 1.21 and above, but will
be beneficial for users on lower versions and using a hermetic
toolchain.

See bazel-contrib/toolchains_llvm#183 for some more
context.
  • Loading branch information
siddharthab committed Sep 14, 2023
1 parent f03a723 commit 65c35ec
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions go/private/mode.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ def extld_from_cc_toolchain(go):
if not go.cgo_tools:
return []
elif go.mode.link in (LINKMODE_SHARED, LINKMODE_PLUGIN, LINKMODE_C_SHARED, LINKMODE_PIE):
return ["-extld", go.cgo_tools.ld_dynamic_lib_path]
# NOTE: Use '=' to help distinguish between `extld` and `extldflags` in
# cgoAbsLinkFlags.
return ["-extld=" + go.cgo_tools.ld_dynamic_lib_path]
elif go.mode.link == LINKMODE_C_ARCHIVE:
if go.mode.goos in ["darwin", "ios"]:
# TODO(jayconrod): on macOS, set -extar. At this time, wrapped_ar is
Expand All @@ -255,4 +257,6 @@ def extld_from_cc_toolchain(go):
# on macOS, Bazel returns wrapped_ar, which is not executable.
# /usr/bin/ar (the default) should be visible though, and we have a
# hack in link.go to strip out non-reproducible stuff.
return ["-extld", go.cgo_tools.ld_executable_path]
# NOTE: Use '=' to help distinguish between `extld` and `extldflags` in
# cgoAbsLinkFlags.
return ["-extld=" + go.cgo_tools.ld_executable_path]
5 changes: 5 additions & 0 deletions go/tools/builders/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ var (
cgoEnvVars = []string{"CGO_CFLAGS", "CGO_CXXFLAGS", "CGO_CPPFLAGS", "CGO_LDFLAGS"}
// cgoAbsEnvFlags are all the flags that need absolute path in cgoEnvVars
cgoAbsEnvFlags = []string{"-I", "-L", "-isysroot", "-isystem", "-iquote", "-include", "-gcc-toolchain", "--sysroot", "-resource-dir", "-fsanitize-blacklist", "-fsanitize-ignorelist"}
// cgoAbsLinkFlags are all the flags in the generated link command that need absolute paths.
// Need absolute path for -extld because of https://github.com/golang/go/issues/59952.
// Can be reverted after dropping support for Go versions older than 1.21.0.
// Also see note for `-extld=` vs `-extld` in go/private/mode.bzl.
cgoAbsLinkFlags = []string{"-extld="}
)

// env holds a small amount of Go environment and toolchain information
Expand Down
1 change: 1 addition & 0 deletions go/tools/builders/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func link(args []string) error {
if err != nil {
return err
}
absArgs(args, cgoAbsLinkFlags)
builderArgs, toolArgs := splitArgs(args)
stamps := multiFlag{}
xdefs := multiFlag{}
Expand Down

0 comments on commit 65c35ec

Please sign in to comment.