From 3a72d626a8bae104c658f361d97f992f609d91e7 Mon Sep 17 00:00:00 2001 From: Michael Hudson-Doyle Date: Tue, 26 Apr 2016 21:37:49 +1200 Subject: [PATCH] cmd/link: pass -no-pie (if supported) when creating a race-enabled executable. Fixes #15443 Change-Id: Ia3593104fc1a4255926ae5675c25390604b44b7b Reviewed-on: https://go-review.googlesource.com/22453 Run-TryBot: Michael Hudson-Doyle TryBot-Result: Gobot Gobot Reviewed-by: Dmitry Vyukov Reviewed-by: Ian Lance Taylor --- src/cmd/link/internal/ld/lib.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go index f7b9b79c2f7..53428bb1c6f 100644 --- a/src/cmd/link/internal/ld/lib.go +++ b/src/cmd/link/internal/ld/lib.go @@ -1204,6 +1204,24 @@ func hostlink() { argv = append(argv, ldflag...) + if flag_race != 0 { + // On a system where the toolchain creates position independent + // executables by default, tsan initialization can fail. So we pass + // -no-pie here, but support for that flag is quite new and we test + // for its support first. + src := filepath.Join(tmpdir, "trivial.c") + if err := ioutil.WriteFile(src, []byte{}, 0666); err != nil { + Ctxt.Diag("WriteFile trivial.c failed: %v", err) + } + cmd := exec.Command(argv[0], "-no-pie", "trivial.c") + cmd.Dir = tmpdir + out, err := cmd.CombinedOutput() + supported := err == nil && !bytes.Contains(out, []byte("unrecognized")) + if supported { + argv = append(argv, "-no-pie") + } + } + for _, p := range strings.Fields(extldflags) { argv = append(argv, p)