Skip to content

cmd/go: allow -buildmode=pie with -race on macOS #62318

@dan-smetana

Description

@dan-smetana

What version of Go are you using (go version)?

$ go version
go version go1.21.0 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/daniel/Library/Caches/go-build'
GOENV='/Users/daniel/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/daniel/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/daniel/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/daniel/Downloads/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/daniel/Downloads/go/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.21.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/f_/bzx32trj6ld4gd6_dcqlp1jh0000gn/T/go-build2514726154=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

go build -buildmode=pie -race something.go

What did you expect to see?

The command to work without issues.

What did you see instead?

-buildmode=pie not supported when -race is enabled

Some more context:

On darwin/arm64 -buildmode=pie is fact required and is used by default. This means that even if I do go build -race something.go I still get a PIE binary. (see the code here: https://github.com/golang/go/blob/master/src/internal/platform/supported.go#L236)

It used to be that on macOS/amd64 I would not get a PIE binary, but as of 3 weeks ago that is not the case anymore: f7b4f02

This means that you always get a PIE binary when targeting macOS, even with the -race flag, yet if you explicitly specify that you want a PIE binary, it prints out an error. This is very confusing and should be fixed.

I suggest doing something like:

index cd99a33a21..a017b20e6b 100644
--- a/src/cmd/go/internal/work/init.go
+++ b/src/cmd/go/internal/work/init.go
@@ -264,8 +264,8 @@ func buildModeInit() {
 			pkgsFilter = oneMainPkg
 		}
 	case "pie":
-		if cfg.BuildRace {
-			base.Fatalf("-buildmode=pie not supported when -race is enabled")
+		if cfg.BuildRace && cfg.Goos != "darwin" {
+			base.Fatalf("-buildmode=pie not supported when -race is enabled (except on macOS)")
 		}
 		if gccgo {
 			codegenArg = "-fPIE"

(note that the patch above is for Go 1.20 so it may be slightly different for the latest version)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions