cmd/go: always use --whole-archive for gccgo packages

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>
This commit is contained in:
Ian Lance Taylor 2015-11-10 11:25:15 -08:00
parent 07a6cbf58a
commit 2fdff9586b
6 changed files with 50 additions and 10 deletions

View 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) }

17
misc/cgo/test/gcc68255.go Normal file
View File

@ -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")
}
}

View File

@ -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
}

View File

@ -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;

View File

@ -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);

View 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", "")...)