Commit 09c6d13a authored by Ian Lance Taylor's avatar Ian Lance Taylor

cmd/cgo: only declare real function in gccgo exported header file

When exporting a function using gccgo, we generate two functions: a Go
function with a leading Cgoexp_ prefix, and a C function that calls the
Go function.  The Go function has a name that can not be represented in
C, so the C code needs a declaration with an __asm__ qualifier giving
the name of the Go function.

Before this CL we put that declaration in the exported header file.
Because code would sometimes #include "_cgo_export.h", we added a macro
definition for the C function giving it the name of the declaration.  We
then added a macro undefine in the actual C code, so that we could
declare the C function we wanted.

This rounadabout process worked OK until we started exporting the header
file for use with -buildmode=c-archive and c-shared.  Doing that caused
the code to see the define and thus call the Go function rather than the
C function.  That often works fine, but the C function calls
_cgo_wait_runtime_init_done before calling the Go function, and that
sometimes matters.  This didn't show up in tests because we don't test
using gccgo.  That is something we should fix, but not now.

Fix that by simplifying the code to declare the C function in the header
file as one would expect, and move the __asm__ declaration to the C
code.

Change-Id: I33547e028152ff98e332630994b4f33285feec32
Reviewed-on: https://go-review.googlesource.com/15043Reviewed-by: default avatarMinux Ma <minux@golang.org>
parent 82a9d90e
...@@ -933,23 +933,15 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) { ...@@ -933,23 +933,15 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
fmt.Fprintf(fgcch, "\n%s", exp.Doc) fmt.Fprintf(fgcch, "\n%s", exp.Doc)
} }
fmt.Fprintf(fgcch, "extern %s %s %s;\n", cRet, exp.ExpName, cParams)
// We need to use a name that will be exported by the // We need to use a name that will be exported by the
// Go code; otherwise gccgo will make it static and we // Go code; otherwise gccgo will make it static and we
// will not be able to link against it from the C // will not be able to link against it from the C
// code. // code.
goName := "Cgoexp_" + exp.ExpName goName := "Cgoexp_" + exp.ExpName
fmt.Fprintf(fgcch, `extern %s %s %s __asm__("%s.%s");`, cRet, goName, cParams, gccgoSymbolPrefix, goName) fmt.Fprintf(fgcc, `extern %s %s %s __asm__("%s.%s");`, cRet, goName, cParams, gccgoSymbolPrefix, goName)
fmt.Fprint(fgcch, "\n") fmt.Fprint(fgcc, "\n")
// Use a #define so that the C code that includes
// cgo_export.h will be able to refer to the Go
// function using the expected name.
fmt.Fprintf(fgcch, "#define %s %s\n", exp.ExpName, goName)
// Use a #undef in _cgo_export.c so that we ignore the
// #define from cgo_export.h, since here we are
// defining the real function.
fmt.Fprintf(fgcc, "#undef %s\n", exp.ExpName)
fmt.Fprint(fgcc, "\n") fmt.Fprint(fgcc, "\n")
fmt.Fprintf(fgcc, "%s %s %s {\n", cRet, exp.ExpName, cParams) fmt.Fprintf(fgcc, "%s %s %s {\n", cRet, exp.ExpName, cParams)
......
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