cmd/link: fix -s with external linking

This code used to only be run for ELF, with the predictable
result that using -s with external linking broke on Windows and OS X.
Moving it here should fix Windows and does fix OS X.

CL 10835 also claims to fix the crash on Windows.
I don't know whether it does so correctly, but regardless,
this CL should make that one a no-op.

Fixes #10254.

Change-Id: I2e7b45ab0c28568ddbb1b50581dcc157ae0e7ffe
Reviewed-on: https://go-review.googlesource.com/11695
Reviewed-by: David Crawshaw <crawshaw@golang.org>
diff --git a/src/cmd/dist/test.go b/src/cmd/dist/test.go
index 802631d..ff35c6c 100644
--- a/src/cmd/dist/test.go
+++ b/src/cmd/dist/test.go
@@ -613,21 +613,22 @@
 
 	pair := t.gohostos + "-" + t.goarch
 	switch pair {
-	case "openbsd-386", "openbsd-amd64":
+	case "darwin-386", "darwin-amd64",
+		"openbsd-386", "openbsd-amd64",
+		"windows-386", "windows-amd64":
 		// test linkmode=external, but __thread not supported, so skip testtls.
+		if !t.extLink() {
+			break
+		}
 		cmd := t.dirCmd("misc/cgo/test", "go", "test", "-ldflags", "-linkmode=external")
 		cmd.Env = env
 		if err := cmd.Run(); err != nil {
 			return err
 		}
-	case "darwin-386", "darwin-amd64",
-		"windows-386", "windows-amd64":
-		if t.extLink() {
-			cmd := t.dirCmd("misc/cgo/test", "go", "test", "-ldflags", "-linkmode=external")
-			cmd.Env = env
-			if err := cmd.Run(); err != nil {
-				return err
-			}
+		cmd = t.dirCmd("misc/cgo/test", "go", "test", "-ldflags", "-linkmode=external -s")
+		cmd.Env = env
+		if err := cmd.Run(); err != nil {
+			return err
 		}
 	case "android-arm",
 		"dragonfly-386", "dragonfly-amd64",
diff --git a/src/cmd/link/internal/ld/elf.go b/src/cmd/link/internal/ld/elf.go
index 785b1cb..7864d1a 100644
--- a/src/cmd/link/internal/ld/elf.go
+++ b/src/cmd/link/internal/ld/elf.go
@@ -1693,8 +1693,6 @@
 	Addstring(shstrtab, ".gopclntab")
 
 	if Linkmode == LinkExternal {
-		debug_s = Debug['s']
-		Debug['s'] = 0
 		Debug['d'] = 1
 
 		switch Thearch.Thechar {
diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go
index ac28439..866eb67 100644
--- a/src/cmd/link/internal/ld/lib.go
+++ b/src/cmd/link/internal/ld/lib.go
@@ -818,6 +818,12 @@
 		return
 	}
 
+	// For external link, record that we need to tell the external linker -s,
+	// and turn off -s internally: the external linker needs the symbol
+	// information for its final link.
+	debug_s = Debug['s']
+	Debug['s'] = 0
+
 	// create temporary directory and arrange cleanup
 	if tmpdir == "" {
 		dir, err := ioutil.TempDir("", "go-link-")