From 38db2df02dbe1edbdb586118d16ebdbf20180a95 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Mon, 28 Aug 2023 20:40:51 +0800 Subject: [PATCH] cmd/go: allow -buildmode=pie with -race on darwin Fixes #62318 Change-Id: I6f1b22c9a4523461aa4c7fcd12e72dbc4430812d Reviewed-on: https://go-review.googlesource.com/c/go/+/523475 Run-TryBot: Andy Pan Auto-Submit: Bryan Mills Reviewed-by: Bryan Mills Reviewed-by: Cherry Mui TryBot-Result: Gopher Robot --- src/cmd/go/internal/work/init.go | 4 +-- src/cmd/go/testdata/script/build_pie_race.txt | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/cmd/go/testdata/script/build_pie_race.txt diff --git a/src/cmd/go/internal/work/init.go b/src/cmd/go/internal/work/init.go index 29116cb90ce..7d0921f0cc9 100644 --- a/src/cmd/go/internal/work/init.go +++ b/src/cmd/go/internal/work/init.go @@ -246,8 +246,8 @@ func buildModeInit() { pkgsFilter = oneMainPkg } case "pie": - if cfg.BuildRace { - base.Fatalf("-buildmode=pie not supported when -race is enabled") + if cfg.BuildRace && !platform.DefaultPIE(cfg.Goos, cfg.Goarch, cfg.BuildRace) { + base.Fatalf("-buildmode=pie not supported when -race is enabled on %s/%s", cfg.Goos, cfg.Goarch) } if gccgo { codegenArg = "-fPIE" diff --git a/src/cmd/go/testdata/script/build_pie_race.txt b/src/cmd/go/testdata/script/build_pie_race.txt new file mode 100644 index 00000000000..39bea0521fc --- /dev/null +++ b/src/cmd/go/testdata/script/build_pie_race.txt @@ -0,0 +1,30 @@ +# go build -buildmode=pie -race main.go on Darwin should work without errors + +[!race] skip 'test requires race detector support' + +[!GOOS:darwin] ! go build -buildmode=pie -race +[!GOOS:darwin] stderr '^-buildmode=pie not supported when -race is enabled on '$GOOS'/'$GOARCH'$' +[!GOOS:darwin] stop 'not testing -buildmode=pie -race on platform that does not support it' + +go build -buildmode=pie -race bytes +! stderr . + +[short] stop 'not linking a binary in -short mode' + +go build -buildmode=pie -race main.go +! stderr . +exec ./main +stdout 'Hello, 世界' + +-- go.mod -- +module m + +go 1.21 +-- main.go -- +package main + +import "fmt" + +func main() { + fmt.Println("Hello, 世界") +}