Commit 42bb59a3 authored by Ian Lance Taylor's avatar Ian Lance Taylor

cmd/go, cmd/cgo: support -buildmode=c-archive for gccgo

This extends the cgo changes in http://golang.org/cl/8094 to gccgo.
It also adds support for setting runtime_iscgo correctly for gccgo;
the gc runtime bases the variable on the runtime/cgo package, but
gccgo has no equivalent to that package.

The go tool supports -buildmode=c-archive for gccgo by linking all the
Go objects together using -r.  For convenience this object is then put
into an archive file.

The go tool now passes -fsplit-stack when building C code for gccgo on
386 and amd64.  This is required for using -r and will also cut down
on unnecessary stack splits.

The go tool no longer applies standard package cgo LDFLAGS when using
gccgo.  This is mainly to avoid getting confused by the LDFLAGS in the
runtime/cgo package that gccgo does not use.

Change-Id: I1d0865b2a362818a033ca9e9e901d0ce250784e7
Reviewed-on: https://go-review.googlesource.com/9511Reviewed-by: default avatarDavid Crawshaw <crawshaw@golang.org>
parent f4e3e5ea
...@@ -840,6 +840,8 @@ func (p *Package) writeGccgoExports(fgo2, fm io.Writer) { ...@@ -840,6 +840,8 @@ func (p *Package) writeGccgoExports(fgo2, fm io.Writer) {
fmt.Fprintf(fgcc, "/* Created by cgo - DO NOT EDIT. */\n") fmt.Fprintf(fgcc, "/* Created by cgo - DO NOT EDIT. */\n")
fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n") fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n")
fmt.Fprintf(fgcc, "%s\n", gccgoExportFileProlog)
for _, exp := range p.ExpFunc { for _, exp := range p.ExpFunc {
fn := exp.Func fn := exp.Func
fntype := fn.Type fntype := fn.Type
...@@ -908,6 +910,8 @@ func (p *Package) writeGccgoExports(fgo2, fm io.Writer) { ...@@ -908,6 +910,8 @@ func (p *Package) writeGccgoExports(fgo2, fm io.Writer) {
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)
fmt.Fprintf(fgcc, "\tif(_cgo_wait_runtime_init_done)\n")
fmt.Fprintf(fgcc, "\t\t_cgo_wait_runtime_init_done();\n")
fmt.Fprint(fgcc, "\t") fmt.Fprint(fgcc, "\t")
if resultCount > 0 { if resultCount > 0 {
fmt.Fprint(fgcc, "return ") fmt.Fprint(fgcc, "return ")
...@@ -1324,3 +1328,19 @@ typedef void *GoChan; ...@@ -1324,3 +1328,19 @@ typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface; typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
` `
// gccgoExportFileProlog is written to the _cgo_export.c file when
// using gccgo.
// We use weak declarations, and test the addresses, so that this code
// works with older versions of gccgo.
const gccgoExportFileProlog = `
extern _Bool runtime_iscgo __attribute__ ((weak));
static void GoInit(void) __attribute__ ((constructor));
static void GoInit(void) {
if(&runtime_iscgo)
runtime_iscgo = 1;
}
extern void _cgo_wait_runtime_init_done() __attribute__ ((weak));
`
...@@ -2223,7 +2223,13 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions [] ...@@ -2223,7 +2223,13 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
afiles = append(xfiles, afiles...) afiles = append(xfiles, afiles...)
for _, a := range allactions { for _, a := range allactions {
cgoldflags = append(cgoldflags, a.p.CgoLDFLAGS...) // Gather CgoLDFLAGS, but not from standard packages.
// The go tool can dig up runtime/cgo from GOROOT and
// think that it should use its CgoLDFLAGS, but gccgo
// doesn't use runtime/cgo.
if !a.p.Standard {
cgoldflags = append(cgoldflags, a.p.CgoLDFLAGS...)
}
if len(a.p.CgoFiles) > 0 { if len(a.p.CgoFiles) > 0 {
usesCgo = true usesCgo = true
} }
...@@ -2237,20 +2243,75 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions [] ...@@ -2237,20 +2243,75 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
objc = true objc = true
} }
} }
if ldBuildmode == "c-archive" {
ldflags = append(ldflags, "-Wl,--whole-archive")
}
ldflags = append(ldflags, afiles...) ldflags = append(ldflags, afiles...)
if ldBuildmode == "c-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", "")...)
ldflags = append(ldflags, p.CgoLDFLAGS...) ldflags = append(ldflags, p.CgoLDFLAGS...)
if usesCgo && goos == "linux" {
ldflags = append(ldflags, "-Wl,-E") ldflags = stringList("-Wl,-(", ldflags, "-Wl,-)")
var realOut string
switch ldBuildmode {
case "exe":
if usesCgo && goos == "linux" {
ldflags = append(ldflags, "-Wl,-E")
}
if cxx {
ldflags = append(ldflags, "-lstdc++")
}
if objc {
ldflags = append(ldflags, "-lobjc")
}
case "c-archive":
// Link the Go files into a single .o, and also link
// in -lgolibbegin.
//
// We need to use --whole-archive with -lgolibbegin
// because it doesn't define any symbols that will
// cause the contents to be pulled in; it's just
// initialization code.
//
// The user remains responsible for linking against
// -lgo -lpthread -lm in the final link. We can't use
// -r to pick them up because we can't combine
// split-stack and non-split-stack code in a single -r
// link, and libgo picks up non-split-stack code from
// libffi.
ldflags = append(ldflags, "-Wl,-r", "-nostdlib", "-Wl,--whole-archive", "-lgolibbegin", "-Wl,--no-whole-archive")
// We are creating an object file, so we don't want a build ID.
ldflags = b.disableBuildID(ldflags)
realOut = out
out = out + ".o"
default:
fatalf("-buildmode=%s not supported for gccgo", ldBuildmode)
} }
if cxx {
ldflags = append(ldflags, "-lstdc++") if err := b.run(".", p.ImportPath, nil, tools.linker(), "-o", out, ofiles, ldflags, buildGccgoflags); err != nil {
return err
} }
if objc {
ldflags = append(ldflags, "-lobjc") switch ldBuildmode {
case "c-archive":
if err := b.run(".", p.ImportPath, nil, "ar", "rc", realOut, out); err != nil {
return err
}
} }
return b.run(".", p.ImportPath, nil, tools.linker(), "-o", out, ofiles, "-Wl,-(", ldflags, "-Wl,-)", buildGccgoflags)
return nil
} }
func (gccgoToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) error { func (gccgoToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) error {
...@@ -2261,6 +2322,10 @@ func (gccgoToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) er ...@@ -2261,6 +2322,10 @@ func (gccgoToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) er
if pkgpath := gccgoCleanPkgpath(p); pkgpath != "" { if pkgpath := gccgoCleanPkgpath(p); pkgpath != "" {
defs = append(defs, `-D`, `GOPKGPATH="`+pkgpath+`"`) defs = append(defs, `-D`, `GOPKGPATH="`+pkgpath+`"`)
} }
switch goarch {
case "386", "amd64":
defs = append(defs, "-fsplit-stack")
}
return b.run(p.Dir, p.ImportPath, nil, envList("CC", defaultCC), "-Wall", "-g", return b.run(p.Dir, p.ImportPath, nil, envList("CC", defaultCC), "-Wall", "-g",
"-I", objdir, "-I", inc, "-o", ofile, defs, "-c", cfile) "-I", objdir, "-I", inc, "-o", ofile, defs, "-c", cfile)
} }
...@@ -2504,6 +2569,10 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi ...@@ -2504,6 +2569,10 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
} }
if _, ok := buildToolchain.(gccgoToolchain); ok { if _, ok := buildToolchain.(gccgoToolchain); ok {
switch goarch {
case "386", "amd64":
cgoCFLAGS = append(cgoCFLAGS, "-fsplit-stack")
}
cgoflags = append(cgoflags, "-gccgo") cgoflags = append(cgoflags, "-gccgo")
if pkgpath := gccgoPkgpath(p); pkgpath != "" { if pkgpath := gccgoPkgpath(p); pkgpath != "" {
cgoflags = append(cgoflags, "-gccgopkgpath="+pkgpath) cgoflags = append(cgoflags, "-gccgopkgpath="+pkgpath)
...@@ -2657,19 +2726,8 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi ...@@ -2657,19 +2726,8 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
} }
ldflags := stringList(bareLDFLAGS, "-Wl,-r", "-nostdlib", staticLibs) ldflags := stringList(bareLDFLAGS, "-Wl,-r", "-nostdlib", staticLibs)
// Some systems, such as Ubuntu, always add --build-id to // We are creating an object file, so we don't want a build ID.
// every link, but we don't want a build ID since we are ldflags = b.disableBuildID(ldflags)
// producing an object file. On some of those system a plain
// -r (not -Wl,-r) will turn off --build-id, but clang 3.0
// doesn't support a plain -r. I don't know how to turn off
// --build-id when using clang other than passing a trailing
// --build-id=none. So that is what we do, but only on
// systems likely to support it, which is to say, systems that
// normally use gold or the GNU linker.
switch goos {
case "android", "dragonfly", "linux", "netbsd":
ldflags = append(ldflags, "-Wl,--build-id=none")
}
if err := b.gccld(p, ofile, ldflags, gccObjs); err != nil { if err := b.gccld(p, ofile, ldflags, gccObjs); err != nil {
return nil, nil, err return nil, nil, err
...@@ -2883,6 +2941,24 @@ func (b *builder) swigOne(p *Package, file, obj string, pcCFLAGS []string, cxx b ...@@ -2883,6 +2941,24 @@ func (b *builder) swigOne(p *Package, file, obj string, pcCFLAGS []string, cxx b
return obj + goFile, obj + gccBase + gccExt, nil return obj + goFile, obj + gccBase + gccExt, nil
} }
// disableBuildID adjusts a linker command line to avoid creating a
// build ID when creating an object file rather than an executable or
// shared library. Some systems, such as Ubuntu, always add
// --build-id to every link, but we don't want a build ID when we are
// producing an object file. On some of those system a plain -r (not
// -Wl,-r) will turn off --build-id, but clang 3.0 doesn't support a
// plain -r. I don't know how to turn off --build-id when using clang
// other than passing a trailing --build-id=none. So that is what we
// do, but only on systems likely to support it, which is to say,
// systems that normally use gold or the GNU linker.
func (b *builder) disableBuildID(ldflags []string) []string {
switch goos {
case "android", "dragonfly", "linux", "netbsd":
ldflags = append(ldflags, "-Wl,--build-id=none")
}
return ldflags
}
// An actionQueue is a priority queue of actions. // An actionQueue is a priority queue of actions.
type actionQueue []*action type actionQueue []*action
......
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