Commit 881d693c authored by Erik Westrup's avatar Erik Westrup Committed by Ian Lance Taylor

cmd/go: Use exported CgoLDFlags when compiler=gccgo

If you compile a program that has cgo LDFLAGS directives, those are exported to an environment variable to be used by subsequent compiler tool invocations. The linking phase when using the gccgo toolchain did not consider the envvar CGO_LDFLAGS's linking directives resulting in undefined references when using cgo+gccgo.

Fixes #7573

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/80780043
parent a6adc19e
...@@ -1905,6 +1905,7 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions [] ...@@ -1905,6 +1905,7 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
ldflags = append(ldflags, afiles...) ldflags = append(ldflags, afiles...)
ldflags = append(ldflags, sfiles...) ldflags = append(ldflags, sfiles...)
ldflags = append(ldflags, cgoldflags...) ldflags = append(ldflags, cgoldflags...)
ldflags = append(ldflags, p.CgoLDFLAGS...)
if usesCgo && goos == "linux" { if usesCgo && goos == "linux" {
ldflags = append(ldflags, "-Wl,-E") ldflags = append(ldflags, "-Wl,-E")
} }
......
...@@ -669,6 +669,31 @@ if ! ./testgo test -c -test.bench=XXX fmt; then ...@@ -669,6 +669,31 @@ if ! ./testgo test -c -test.bench=XXX fmt; then
fi fi
rm -f fmt.test rm -f fmt.test
TEST 'Issue 7573: cmd/cgo: undefined reference when linking a C-library using gccgo'
d=$(mktemp -d -t testgoXXX)
export GOPATH=$d
mkdir -p $d/src/cgoref
ldflags="-L alibpath -lalib"
echo "
package main
// #cgo LDFLAGS: $ldflags
// void f(void) {}
import \"C\"
func main() { C.f() }
" >$d/src/cgoref/cgoref.go
go_cmds="$(./testgo build -n -compiler gccgo cgoref 2>&1 1>/dev/null)"
ldflags_count="$(echo "$go_cmds" | egrep -c "^gccgo.*$(echo $ldflags | sed -e 's/-/\\-/g')" || true)"
if [ "$ldflags_count" -lt 1 ]; then
echo "No Go-inline "#cgo LDFLAGS:" (\"$ldflags\") passed to gccgo linking stage."
ok=false
fi
rm -rf $d
unset ldflags_count
unset go_cmds
unset ldflags
unset GOPATH
# clean up # clean up
if $started; then stop; fi if $started; then stop; fi
rm -rf testdata/bin testdata/bin1 rm -rf testdata/bin testdata/bin1
......
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