]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: always use --whole-archive for gccgo packages
authorIan Lance Taylor <iant@golang.org>
Tue, 10 Nov 2015 19:25:15 +0000 (11:25 -0800)
committerIan Lance Taylor <iant@golang.org>
Tue, 10 Nov 2015 20:22:13 +0000 (20:22 +0000)
This is, in effect, what the gc toolchain does.  It fixes cases where Go
code refers to a C global variable; without this, if the global variable
was the only thing visible in the C code, the generated cgo file might
not get pulled in from the archive, leaving the Go variable
uninitialized.

This was reported against gccgo as https://gcc.gnu.org/PR68255 .

Change-Id: I3e769dd174f64050ebbff268fbbf5e6fab1e2a1b
Reviewed-on: https://go-review.googlesource.com/16775
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
misc/cgo/test/cgo_test.go
misc/cgo/test/gcc68255.go [new file with mode: 0644]
misc/cgo/test/gcc68255/a.go [new file with mode: 0644]
misc/cgo/test/gcc68255/c.c [new file with mode: 0644]
misc/cgo/test/gcc68255/c.h [new file with mode: 0644]
src/cmd/go/build.go

index 4060338b654f298816600d786b2aa79f93a84915..948a0eab648a944d6c4f49d8de58b5c9f2a6d640 100644 (file)
@@ -66,5 +66,6 @@ func Test9557(t *testing.T)                  { test9557(t) }
 func Test10303(t *testing.T)                 { test10303(t, 10) }
 func Test11925(t *testing.T)                 { test11925(t) }
 func Test12030(t *testing.T)                 { test12030(t) }
+func TestGCC68255(t *testing.T)              { testGCC68255(t) }
 
 func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
diff --git a/misc/cgo/test/gcc68255.go b/misc/cgo/test/gcc68255.go
new file mode 100644 (file)
index 0000000..2c4f931
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2015 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cgotest
+
+import (
+       "testing"
+
+       "./gcc68255"
+)
+
+func testGCC68255(t *testing.T) {
+       if !gcc68255.F() {
+               t.Error("C global variable was not initialized")
+       }
+}
diff --git a/misc/cgo/test/gcc68255/a.go b/misc/cgo/test/gcc68255/a.go
new file mode 100644 (file)
index 0000000..02e5494
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2015 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test that it's OK to have C code that does nothing other than
+// initialize a global variable.  This used to fail with gccgo.
+
+package gcc68255
+
+/*
+#include "c.h"
+*/
+import "C"
+
+func F() bool {
+       return C.v != nil
+}
diff --git a/misc/cgo/test/gcc68255/c.c b/misc/cgo/test/gcc68255/c.c
new file mode 100644 (file)
index 0000000..28cfe72
--- /dev/null
@@ -0,0 +1,8 @@
+// Copyright 2015 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+static void f(void) {
+}
+
+void (*v)(void) = f;
diff --git a/misc/cgo/test/gcc68255/c.h b/misc/cgo/test/gcc68255/c.h
new file mode 100644 (file)
index 0000000..644003e
--- /dev/null
@@ -0,0 +1,5 @@
+// Copyright 2015 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+extern void (*v)(void);
index ea27ae1dd12c1a2ef4251055130070d3223df48d..7e48c17f6ce09703c9e40d07299d3e4f90281284 100644 (file)
@@ -2599,17 +2599,9 @@ func (tools gccgoToolchain) ld(b *builder, root *action, out string, allactions
                }
        }
 
-       switch ldBuildmode {
-       case "c-archive", "c-shared":
-               ldflags = append(ldflags, "-Wl,--whole-archive")
-       }
-
+       ldflags = append(ldflags, "-Wl,--whole-archive")
        ldflags = append(ldflags, afiles...)
-
-       switch ldBuildmode {
-       case "c-archive", "c-shared":
-               ldflags = append(ldflags, "-Wl,--no-whole-archive")
-       }
+       ldflags = append(ldflags, "-Wl,--no-whole-archive")
 
        ldflags = append(ldflags, cgoldflags...)
        ldflags = append(ldflags, envList("CGO_LDFLAGS", "")...)