From a6df299e89f6ba56d71571f694fb7327294ae72e Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 22 Jun 2017 16:11:10 -0400 Subject: [PATCH] cmd/go: detect Go assembly before assembling with gcc Avoids confusing errors from the GNU assembler processing Go assembly source code. Fixes #19448. Change-Id: Ic2c68b2521847cca5a3d078a092e5c60ec340840 Reviewed-on: https://go-review.googlesource.com/46423 Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/cmd/go/go_test.go | 13 +++++++++++++ src/cmd/go/internal/work/build.go | 10 ++++++++++ src/cmd/go/testdata/src/cgoasm/p.go | 8 ++++++++ src/cmd/go/testdata/src/cgoasm/p.s | 2 ++ 4 files changed, 33 insertions(+) create mode 100644 src/cmd/go/testdata/src/cgoasm/p.go create mode 100644 src/cmd/go/testdata/src/cgoasm/p.s diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index e7fc5fc103..71b34b6ec4 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -2310,6 +2310,19 @@ func TestCoverageWithCgo(t *testing.T) { } } +func TestCgoAsmError(t *testing.T) { + if !canCgo { + t.Skip("skipping because cgo not enabled") + } + + tg := testgo(t) + tg.parallel() + defer tg.cleanup() + tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata")) + tg.runFail("build", "cgoasm") + tg.grepBoth("package using cgo has Go assembly file", "did not detect Go assembly file") +} + func TestCgoDependsOnSyscall(t *testing.T) { if testing.Short() { t.Skip("skipping test that removes $GOROOT/pkg/*_race in short mode") diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index a7949b6ff3..d03ad3e139 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -1333,6 +1333,16 @@ func (b *Builder) build(a *Action) (err error) { } sfiles, gccfiles = filter(sfiles, sfiles[:0], gccfiles) } else { + for _, sfile := range sfiles { + data, err := ioutil.ReadFile(filepath.Join(a.Package.Dir, sfile)) + if err == nil { + if bytes.HasPrefix(data, []byte("TEXT")) || bytes.Contains(data, []byte("\nTEXT")) || + bytes.HasPrefix(data, []byte("DATA")) || bytes.Contains(data, []byte("\nDATA")) || + bytes.HasPrefix(data, []byte("GLOBL")) || bytes.Contains(data, []byte("\nGLOBL")) { + return fmt.Errorf("package using cgo has Go assembly file %s", sfile) + } + } + } gccfiles = append(gccfiles, sfiles...) sfiles = nil } diff --git a/src/cmd/go/testdata/src/cgoasm/p.go b/src/cmd/go/testdata/src/cgoasm/p.go new file mode 100644 index 0000000000..148b47f6a5 --- /dev/null +++ b/src/cmd/go/testdata/src/cgoasm/p.go @@ -0,0 +1,8 @@ +package p + +/* +// hi +*/ +import "C" + +func F() {} diff --git a/src/cmd/go/testdata/src/cgoasm/p.s b/src/cmd/go/testdata/src/cgoasm/p.s new file mode 100644 index 0000000000..aaade03a43 --- /dev/null +++ b/src/cmd/go/testdata/src/cgoasm/p.s @@ -0,0 +1,2 @@ +TEXT asm(SB),$0 + RET