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 ad0d4f1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions go/private/mode.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ 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]
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 +255,4 @@ 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]
return ["-extld=" + go.cgo_tools.ld_executable_path]
2 changes: 2 additions & 0 deletions go/tools/builders/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ 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.
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 ad0d4f1

Please sign in to comment.