cmd/release: update checkRelocations for Go 1.16

The plan in https://golang.org/issue/40561#issuecomment-731482962
involved updating the linux-amd64 target from Debian Jessie to
Debian Stretch, which in turn comes with a newer version of GCC.

checkRelocations was added in CL 171317 in response to a bad minor
release that was accidentally issued without the intended fix, and
needs to be updated for Go 1.16 and newer releases. It's not clear
if we want to maintain it indefinitely for future Go versions, but
keep it for a bit longer. Add a note to make it clear that it can
be removed as needed in the future.

For golang/go#40561.
Updates golang/go#31293.

Change-Id: I2da419eff6379575eb2648787f0dac6bba07b304
Reviewed-on: https://go-review.googlesource.com/c/build/+/278357
Trust: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alexander Rakoczy <alex@golang.org>
Reviewed-by: Carlos Amedee <carlos@golang.org>
diff --git a/cmd/release/release.go b/cmd/release/release.go
index 3838068..59bb402 100644
--- a/cmd/release/release.go
+++ b/cmd/release/release.go
@@ -857,7 +857,8 @@
 }
 
 // checkRelocations runs readelf on pkg/linux_amd64/runtime/cgo.a and makes sure
-// we don't see R_X86_64_REX_GOTPCRELX. See golang.org/issue/31293.
+// we don't see R_X86_64_REX_GOTPCRELX in new Go 1.15 and Go 1.14 minor releases.
+// See golang.org/issue/31293 and golang.org/issue/40561#issuecomment-731482962.
 func (b *Build) checkRelocations(client *buildlet.Client) error {
 	if b.OS != "linux" || b.Arch != "amd64" || b.TestOnly {
 		// This check is only applicable to linux/amd64 builds.
@@ -876,11 +877,25 @@
 		return fmt.Errorf("failed to run readelf: %v", err)
 	}
 	got := out.String()
-	if strings.Contains(got, "R_X86_64_REX_GOTPCRELX") {
-		return fmt.Errorf("%s contained a R_X86_64_REX_GOTPCRELX relocation", file)
-	}
-	if !strings.Contains(got, "R_X86_64_GOTPCREL") {
-		return fmt.Errorf("%s did not contain a R_X86_64_GOTPCREL relocation; remoteErr=%v, %s", file, remoteErr, got)
+	switch {
+	default: // Go 1.16 and newer.
+		// Note: This check was kept and updated for Go 1.16, since it wasn't hard.
+		// Remove it at some point in the future if it becomes no longer useful or
+		// overly expensive to maintain.
+		if strings.Contains(got, "R_X86_64_GOTPCREL") {
+			return fmt.Errorf("%s contained a R_X86_64_GOTPCREL relocation", file)
+		}
+		if !strings.Contains(got, "R_X86_64_REX_GOTPCRELX") {
+			return fmt.Errorf("%s did not contain a R_X86_64_REX_GOTPCRELX relocation; remoteErr=%v, %s", file, remoteErr, got)
+		}
+	case strings.HasPrefix(*version, "go1.15"),
+		strings.HasPrefix(*version, "go1.14"):
+		if strings.Contains(got, "R_X86_64_REX_GOTPCRELX") {
+			return fmt.Errorf("%s contained a R_X86_64_REX_GOTPCRELX relocation", file)
+		}
+		if !strings.Contains(got, "R_X86_64_GOTPCREL") {
+			return fmt.Errorf("%s did not contain a R_X86_64_GOTPCREL relocation; remoteErr=%v, %s", file, remoteErr, got)
+		}
 	}
 	return nil
 }