Commit 2fdff958 authored by Ian Lance Taylor's avatar Ian Lance Taylor

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: 's avatarRuss Cox <rsc@golang.org>
parent 07a6cbf5
...@@ -66,5 +66,6 @@ func Test9557(t *testing.T) { test9557(t) } ...@@ -66,5 +66,6 @@ func Test9557(t *testing.T) { test9557(t) }
func Test10303(t *testing.T) { test10303(t, 10) } func Test10303(t *testing.T) { test10303(t, 10) }
func Test11925(t *testing.T) { test11925(t) } func Test11925(t *testing.T) { test11925(t) }
func Test12030(t *testing.T) { test12030(t) } func Test12030(t *testing.T) { test12030(t) }
func TestGCC68255(t *testing.T) { testGCC68255(t) }
func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) } func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
// 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")
}
}
// 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
}
// 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;
// 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);
...@@ -2599,17 +2599,9 @@ func (tools gccgoToolchain) ld(b *builder, root *action, out string, allactions ...@@ -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...) 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, cgoldflags...)
ldflags = append(ldflags, envList("CGO_LDFLAGS", "")...) ldflags = append(ldflags, envList("CGO_LDFLAGS", "")...)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment