diff --git a/docs/go/core/rules.md b/docs/go/core/rules.md
index 52507673af..22e08a2434 100644
--- a/docs/go/core/rules.md
+++ b/docs/go/core/rules.md
@@ -391,7 +391,7 @@ This builds a set of tests that can be run with `bazel test`.
| goos | Forces a binary to be cross-compiled for a specific operating system. It's usually better to control this on the command line with --platforms
.
This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set pure
= off
.
See [Cross compilation] for more information. | String | optional | "auto" |
| gotags | Enables a list of build tags when evaluating [build constraints]. Useful for conditional compilation. | List of strings | optional | [] |
| importpath | The import path of this test. Tests can't actually be imported, but this may be used by [go_path] and other tools to report the location of source files. This may be inferred from embedded libraries. | String | optional | "" |
-| linkmode | Determines how the binary should be built and linked. This accepts some of the same values as `go build -buildmode` and works the same way.
- `normal`: Builds a normal executable with position-dependent code.
- `pie`: Builds a position-independent executable.
- `plugin`: Builds a shared library that can be loaded as a Go plugin. Only supported on platforms that support plugins.
- `c-shared`: Builds a shared library that can be linked into a C program.
- `c-archive`: Builds an archive that can be linked into a C program.
| String | optional | "normal" |
+| linkmode | Determines how the binary should be built and linked. This accepts some of the same values as `go build -buildmode` and works the same way.
- `auto` (default): Controlled by `//go/config:linkmode`, which defaults to `normal`.
- `normal`: Builds a normal executable with position-dependent code.
- `pie`: Builds a position-independent executable.
- `plugin`: Builds a shared library that can be loaded as a Go plugin. Only supported on platforms that support plugins.
- `c-shared`: Builds a shared library that can be linked into a C program.
- `c-archive`: Builds an archive that can be linked into a C program.
| String | optional | "auto" |
| msan | Controls whether code is instrumented for memory sanitization. May be one of on
, off
, or auto
. Not available when cgo is disabled. In most cases, it's better to control this on the command line with --@io_bazel_rules_go//go/config:msan
. See [mode attributes], specifically [msan]. | String | optional | "auto" |
| pure | Controls whether cgo source code and dependencies are compiled and linked, similar to setting CGO_ENABLED
. May be one of on
, off
, or auto
. If auto
, pure mode is enabled when no C/C++ toolchain is configured or when cross-compiling. It's usually better to control this on the command line with --@io_bazel_rules_go//go/config:pure
. See [mode attributes], specifically [pure]. | String | optional | "auto" |
| race | Controls whether code is instrumented for race detection. May be one of on
, off
, or auto
. Not available when cgo is disabled. In most cases, it's better to control this on the command line with --@io_bazel_rules_go//go/config:race
. See [mode attributes], specifically [race]. | String | optional | "auto" |
diff --git a/go/private/rules/test.bzl b/go/private/rules/test.bzl
index 413a19da43..7710b38919 100644
--- a/go/private/rules/test.bzl
+++ b/go/private/rules/test.bzl
@@ -46,7 +46,7 @@ load(
)
load(
"//go/private:mode.bzl",
- "LINKMODE_NORMAL",
+ "LINKMODES",
)
load(
"@bazel_skylib//lib:structs.bzl",
@@ -292,11 +292,13 @@ _go_test_kwargs = {
""",
),
"linkmode": attr.string(
- default = LINKMODE_NORMAL,
+ default = "auto",
+ values = ["auto"] + LINKMODES,
doc = """Determines how the binary should be built and linked. This accepts some of
the same values as `go build -buildmode` and works the same way.
+ - `auto` (default): Controlled by `//go/config:linkmode`, which defaults to `normal`.
- `normal`: Builds a normal executable with position-dependent code.
- `pie`: Builds a position-independent executable.
- `plugin`: Builds a shared library that can be loaded as a Go plugin. Only supported on platforms that support plugins.
diff --git a/tests/core/go_binary/BUILD.bazel b/tests/core/go_binary/BUILD.bazel
index 89c211b7ac..74d4e04b5b 100644
--- a/tests/core/go_binary/BUILD.bazel
+++ b/tests/core/go_binary/BUILD.bazel
@@ -108,6 +108,20 @@ linkmode_pie_wrapper(
target = ":hello_nopie_bin",
)
+go_test(
+ name = "hello_nopie_test_bin",
+ srcs = ["hello.go"],
+ cgo = True,
+ tags = ["manual"],
+)
+
+linkmode_pie_wrapper(
+ name = "hello_pie_setting_test_bin",
+ testonly = True,
+ tags = ["manual"],
+ target = ":hello_nopie_test_bin",
+)
+
go_test(
name = "pie_test",
srcs = [
@@ -120,11 +134,13 @@ go_test(
":hello_nopie_bin",
":hello_pie_bin",
":hello_pie_setting_bin",
+ ":hello_pie_setting_test_bin",
],
"@io_bazel_rules_go//go/platform:linux": [
":hello_nopie_bin",
":hello_pie_bin",
":hello_pie_setting_bin",
+ ":hello_pie_setting_test_bin",
],
"//conditions:default": [],
}),
diff --git a/tests/core/go_binary/pie_darwin_test.go b/tests/core/go_binary/pie_darwin_test.go
index 2f45b87881..bea2f2fdd1 100644
--- a/tests/core/go_binary/pie_darwin_test.go
+++ b/tests/core/go_binary/pie_darwin_test.go
@@ -30,6 +30,28 @@ func TestPIE(t *testing.T) {
}
if m.Flags&macho.FlagPIE == 0 {
- t.Error("ELF binary is not position-independent.")
+ t.Error("MachO binary is not position-independent.")
+ }
+}
+
+func TestPIESetting(t *testing.T) {
+ m, err := openMachO("tests/core/go_binary", "hello_pie_setting_bin")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if m.Flags&macho.FlagPIE == 0 {
+ t.Error("MachO binary is not position-independent.")
+ }
+}
+
+func TestPIESettingTest(t *testing.T) {
+ m, err := openMachO("tests/core/go_binary", "hello_pie_setting_test_bin")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if m.Flags&macho.FlagPIE == 0 {
+ t.Error("MachO binary is not position-independent.")
}
}
diff --git a/tests/core/go_binary/pie_linux_test.go b/tests/core/go_binary/pie_linux_test.go
index 9248575c21..fdef236499 100644
--- a/tests/core/go_binary/pie_linux_test.go
+++ b/tests/core/go_binary/pie_linux_test.go
@@ -35,6 +35,18 @@ func TestPIESetting(t *testing.T) {
}
}
+func TestPIESettingTest(t *testing.T) {
+ e, err := openELF("tests/core/go_binary", "hello_pie_setting_test_bin")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // PIE binaries are implemented as shared libraries.
+ if e.Type != elf.ET_DYN {
+ t.Error("ELF binary is not position-independent.")
+ }
+}
+
func TestPIE(t *testing.T) {
e, err := openELF("tests/core/go_binary", "hello_pie_bin")
if err != nil {