cmd/internal/ld: add -buildmode=c-shared as an alternative to -shared

The linker currently (on some platforms) takes a -shared flag, which means
approximately what -buildmode=c-shared means in the in the proposed "Go
Execution Modes" document. As part of implementing other modes, the term
"shared" becomes horribly overloaded, so this replaces -shared with a
-buildmode argument instead (which currently only handles -buildmode=c-shared
and the default -buildmode=exe -- no new behaviour here).

As the linker support for -shared was in 1.4 this retains it as an alias.

Change-Id: Id2ebb8e05ee07f46208a554bc2622d0e67b47082
Reviewed-on: https://go-review.googlesource.com/8304
Reviewed-by: David Crawshaw <crawshaw@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Michael Hudson-Doyle 2015-03-27 02:48:27 +00:00 committed by Ian Lance Taylor
parent 47746f10fe
commit 2a5f88d850
8 changed files with 67 additions and 15 deletions

View file

@ -33,6 +33,7 @@ package ld
import (
"bytes"
"cmd/internal/obj"
"errors"
"fmt"
"io/ioutil"
"log"
@ -160,7 +161,7 @@ var (
elfglobalsymndx int
flag_installsuffix string
flag_race int
Flag_shared int
Buildmode BuildMode
tracksym string
interpreter string
tmpdir string
@ -234,6 +235,44 @@ func Lflag(arg string) {
Ctxt.Libdir = append(Ctxt.Libdir, arg)
}
// A BuildMode indicates the sort of object we are building:
// "exe": build a main package and everything it imports into an executable.
// "c-shared": build a main package, plus all packages that it imports, into a
// single C shared library. The only callable symbols will be those functions
// marked as exported.
type BuildMode uint8
const (
BuildmodeExe BuildMode = iota
BuildmodeCShared
)
func (mode *BuildMode) Set(s string) error {
switch s {
default:
return errors.New("invalid mode")
case "exe":
*mode = BuildmodeExe
case "c-shared":
goarch := obj.Getgoarch()
if goarch != "amd64" && goarch != "arm" {
return fmt.Errorf("not supported on %s", goarch)
}
*mode = BuildmodeCShared
}
return nil
}
func (mode *BuildMode) String() string {
switch *mode {
case BuildmodeExe:
return "exe"
case BuildmodeCShared:
return "c-shared"
}
return fmt.Sprintf("BuildMode(%d)", uint8(*mode))
}
/*
* Unix doesn't like it when we write to a running (or, sometimes,
* recently run) binary, so remove the output file before writing it.
@ -276,10 +315,13 @@ func libinit() {
coutbuf = *Binitw(f)
if INITENTRY == "" {
if Flag_shared == 0 {
INITENTRY = fmt.Sprintf("_rt0_%s_%s", goarch, goos)
} else {
switch Buildmode {
case BuildmodeCShared:
INITENTRY = fmt.Sprintf("_rt0_%s_%s_lib", goarch, goos)
case BuildmodeExe:
INITENTRY = fmt.Sprintf("_rt0_%s_%s", goarch, goos)
default:
Diag("unknown INITENTRY for buildmode %v", Buildmode)
}
}
@ -324,7 +366,7 @@ func loadinternal(name string) {
}
func loadlib() {
if Flag_shared != 0 {
if Buildmode == BuildmodeCShared {
s := Linklookup(Ctxt, "runtime.islibrary", 0)
s.Dupok = 1
Adduint8(Ctxt, s, 1)
@ -454,7 +496,7 @@ func loadlib() {
// binaries, so leave it enabled on OS X (Mach-O) binaries.
// Also leave it enabled on Solaris which doesn't support
// statically linked binaries.
if Flag_shared == 0 && havedynamic == 0 && HEADTYPE != Hdarwin && HEADTYPE != Hsolaris {
if Buildmode == BuildmodeExe && havedynamic == 0 && HEADTYPE != Hdarwin && HEADTYPE != Hsolaris {
Debug['d'] = 1
}
@ -746,7 +788,7 @@ func hostlink() {
argv = append(argv, "-Wl,--rosegment")
}
if Flag_shared != 0 {
if Buildmode == BuildmodeCShared {
argv = append(argv, "-Wl,-Bsymbolic")
argv = append(argv, "-shared")
}