import (
"cmd/internal/obj"
"fmt"
+ "io/ioutil"
"os"
"path"
"strconv"
}
var pname string
+ isshlib := false
if (ctxt.Windows == 0 && strings.HasPrefix(name, "/")) || (ctxt.Windows != 0 && len(name) >= 2 && name[1] == ':') {
pname = name
} else {
// try dot, -L "libdir", and then goroot.
for _, dir := range ctxt.Libdir {
+ if Linkshared {
+ pname = dir + "/" + pkg + ".shlibname"
+ if _, err := os.Stat(pname); err == nil {
+ isshlib = true
+ break
+ }
+ }
pname = dir + "/" + name
if _, err := os.Stat(pname); err == nil {
break
pname = path.Clean(pname)
if ctxt.Debugvlog > 1 && ctxt.Bso != nil {
- fmt.Fprintf(ctxt.Bso, "%5.2f addlib: %s %s pulls in %s\n", elapsed(), obj, src, pname)
+ fmt.Fprintf(ctxt.Bso, "%5.2f addlib: %s %s pulls in %s isshlib %v\n", elapsed(), obj, src, pname, isshlib)
}
- addlibpath(ctxt, src, obj, pname, pkg)
+ if isshlib {
+ addlibpath(ctxt, src, obj, "", pkg, pname)
+ } else {
+ addlibpath(ctxt, src, obj, pname, pkg, "")
+ }
}
/*
* file: object file, e.g., /home/rsc/go/pkg/container/vector.a
* pkg: package import path, e.g. container/vector
*/
-func addlibpath(ctxt *Link, srcref string, objref string, file string, pkg string) {
+func addlibpath(ctxt *Link, srcref string, objref string, file string, pkg string, shlibnamefile string) {
for i := 0; i < len(ctxt.Library); i++ {
- if file == ctxt.Library[i].File {
+ if pkg == ctxt.Library[i].Pkg {
return
}
}
if ctxt.Debugvlog > 1 && ctxt.Bso != nil {
- fmt.Fprintf(ctxt.Bso, "%5.2f addlibpath: srcref: %s objref: %s file: %s pkg: %s\n", obj.Cputime(), srcref, objref, file, pkg)
+ fmt.Fprintf(ctxt.Bso, "%5.2f addlibpath: srcref: %s objref: %s file: %s pkg: %s shlibnamefile: %s\n", obj.Cputime(), srcref, objref, file, pkg, shlibnamefile)
}
ctxt.Library = append(ctxt.Library, Library{})
l.Srcref = srcref
l.File = file
l.Pkg = pkg
+ if shlibnamefile != "" {
+ shlibbytes, err := ioutil.ReadFile(shlibnamefile)
+ if err != nil {
+ Diag("cannot read %s: %v", shlibnamefile, err)
+ }
+ l.Shlib = strings.TrimSpace(string(shlibbytes))
+ }
}
func atolwhex(s string) int64 {
import (
"bytes"
"cmd/internal/obj"
+ "debug/elf"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
+ "path/filepath"
"strings"
)
// DynlinkingGo returns whether we are producing Go code that can live
// in separate shared libraries linked together at runtime.
func DynlinkingGo() bool {
- // TODO(mwhudson): This is a bit silly for now, but it will need to have
- // "|| Linkshared" appended when a subsequent change adds that flag.
- return Buildmode == BuildmodeShared
+ return Buildmode == BuildmodeShared || Linkshared
}
var (
flag_installsuffix string
flag_race int
Buildmode BuildMode
+ Linkshared bool
tracksym string
interpreter string
tmpdir string
}
func loadinternal(name string) {
- var pname string
-
found := 0
for i := 0; i < len(Ctxt.Libdir); i++ {
- pname = fmt.Sprintf("%s/%s.a", Ctxt.Libdir[i], name)
+ if Linkshared {
+ shlibname := fmt.Sprintf("%s/%s.shlibname", Ctxt.Libdir[i], name)
+ if Debug['v'] != 0 {
+ fmt.Fprintf(&Bso, "searching for %s.a in %s\n", name, shlibname)
+ }
+ if obj.Access(shlibname, obj.AEXIST) >= 0 {
+ addlibpath(Ctxt, "internal", "internal", "", name, shlibname)
+ found = 1
+ break
+ }
+ }
+ pname := fmt.Sprintf("%s/%s.a", Ctxt.Libdir[i], name)
if Debug['v'] != 0 {
fmt.Fprintf(&Bso, "searching for %s.a in %s\n", name, pname)
}
if obj.Access(pname, obj.AEXIST) >= 0 {
- addlibpath(Ctxt, "internal", "internal", pname, name)
+ addlibpath(Ctxt, "internal", "internal", pname, name, "")
found = 1
break
}
fmt.Fprintf(&Bso, "%5.2f autolib: %s (from %s)\n", obj.Cputime(), Ctxt.Library[i].File, Ctxt.Library[i].Objref)
}
iscgo = iscgo || Ctxt.Library[i].Pkg == "runtime/cgo"
- objfile(Ctxt.Library[i].File, Ctxt.Library[i].Pkg)
+ if Ctxt.Library[i].Shlib != "" {
+ ldshlibsyms(Ctxt.Library[i].Shlib)
+ } else {
+ objfile(Ctxt.Library[i].File, Ctxt.Library[i].Pkg)
+ }
}
if Linkmode == LinkAuto {
loadinternal("runtime/cgo")
if i < len(Ctxt.Library) {
- objfile(Ctxt.Library[i].File, Ctxt.Library[i].Pkg)
+ if Ctxt.Library[i].Shlib != "" {
+ ldshlibsyms(Ctxt.Library[i].Shlib)
+ } else {
+ objfile(Ctxt.Library[i].File, Ctxt.Library[i].Pkg)
+ }
}
}
// TODO(crawshaw): android should require leaving the tlsg->type
// alone (as the runtime-provided SNOPTRBSS) just like darwin/arm.
// But some other part of the linker is expecting STLSBSS.
- if goos != "darwin" || Thearch.Thechar != '5' {
+ if tlsg.Type != SDYNIMPORT && (goos != "darwin" || Thearch.Thechar != '5') {
tlsg.Type = STLSBSS
}
tlsg.Size = int64(Thearch.Ptrsize)
argv = append(argv, "-shared")
}
+ if Linkshared && Iself {
+ // We force all symbol resolution to be done at program startup
+ // because lazy PLT resolution can use large amounts of stack at
+ // times we cannot allow it to do so.
+ argv = append(argv, "-znow")
+ }
+
argv = append(argv, "-o")
argv = append(argv, outfile)
}
argv = append(argv, fmt.Sprintf("%s/go.o", tmpdir))
+
+ if Linkshared {
+ for _, shlib := range Ctxt.Shlibs {
+ dir, base := filepath.Split(shlib)
+ argv = append(argv, "-L"+dir)
+ argv = append(argv, "-Wl,-rpath="+dir)
+ base = strings.TrimSuffix(base, ".so")
+ base = strings.TrimPrefix(base, "lib")
+ argv = append(argv, "-l"+base)
+ }
+ }
+
argv = append(argv, ldflag...)
for _, p := range strings.Fields(extldflags) {
Diag("truncated object file: %s", pn)
}
+func ldshlibsyms(shlib string) {
+ found := false
+ libpath := ""
+ for _, libdir := range Ctxt.Libdir {
+ libpath = filepath.Join(libdir, shlib)
+ if _, err := os.Stat(libpath); err == nil {
+ found = true
+ break
+ }
+ }
+ if !found {
+ Diag("cannot find shared library: %s", shlib)
+ return
+ }
+ for _, processedname := range Ctxt.Shlibs {
+ if processedname == libpath {
+ return
+ }
+ }
+ if Ctxt.Debugvlog > 1 && Ctxt.Bso != nil {
+ fmt.Fprintf(Ctxt.Bso, "%5.2f ldshlibsyms: found library with name %s at %s\n", obj.Cputime(), shlib, libpath)
+ Bflush(Ctxt.Bso)
+ }
+
+ f, err := elf.Open(libpath)
+ if err != nil {
+ Diag("cannot open shared library: %s", libpath)
+ return
+ }
+ defer f.Close()
+ syms, err := f.DynamicSymbols()
+ if err != nil {
+ Diag("cannot read symbols from shared library: %s", libpath)
+ return
+ }
+ for _, s := range syms {
+ if elf.ST_TYPE(s.Info) == elf.STT_NOTYPE || elf.ST_TYPE(s.Info) == elf.STT_SECTION {
+ continue
+ }
+ if s.Section == elf.SHN_UNDEF {
+ continue
+ }
+ if strings.HasPrefix(s.Name, "_") {
+ continue
+ }
+ lsym := Linklookup(Ctxt, s.Name, 0)
+ if lsym.Type != 0 && lsym.Dupok == 0 {
+ Diag(
+ "Found duplicate symbol %s reading from %s, first found in %s",
+ s.Name, shlib, lsym.File)
+ }
+ lsym.Type = SDYNIMPORT
+ lsym.File = libpath
+ }
+
+ // We might have overwritten some functions above (this tends to happen for the
+ // autogenerated type equality/hashing functions) and we don't want to generated
+ // pcln table entries for these any more so unstitch them from the Textp linked
+ // list.
+ var last *LSym
+
+ for s := Ctxt.Textp; s != nil; s = s.Next {
+ if s.Type == SDYNIMPORT {
+ continue
+ }
+
+ if last == nil {
+ Ctxt.Textp = s
+ } else {
+ last.Next = s
+ }
+ last = s
+ }
+
+ if last == nil {
+ Ctxt.Textp = nil
+ Ctxt.Etextp = nil
+ } else {
+ last.Next = nil
+ Ctxt.Etextp = last
+ }
+
+ Ctxt.Shlibs = append(Ctxt.Shlibs, libpath)
+}
+
func mywhatsys() {
goroot = obj.Getgoroot()
goos = obj.Getgoos()
obj.Flagstr("installsuffix", "suffix: pkg directory suffix", &flag_installsuffix)
obj.Flagstr("k", "sym: set field tracking symbol", &tracksym)
obj.Flagfn1("linkmode", "mode: set link mode (internal, external, auto)", setlinkmode)
+ flag.BoolVar(&Linkshared, "linkshared", false, "link against installed Go shared libraries")
obj.Flagcount("n", "dump symbol table", &Debug['n'])
obj.Flagstr("o", "outfile: set output file", &outfile)
obj.Flagstr("r", "dir1:dir2:...: set ELF dynamic linker search path", &rpath)
Thearch.Archinit()
+ if Linkshared && !Iself {
+ Diag("-linkshared can only be used on elf systems")
+ Errorexit()
+ }
+
if Debug['v'] != 0 {
fmt.Fprintf(&Bso, "HEADER = -H%d -T0x%x -D0x%x -R0x%x\n", HEADTYPE, uint64(INITTEXT), uint64(INITDAT), uint32(INITRND))
}
} else {
pkgpath, file = parts[0], parts[1]
}
- addlibpath(Ctxt, "command line", "command line", file, pkgpath)
+ addlibpath(Ctxt, "command line", "command line", file, pkgpath, "")
}
} else {
- addlibpath(Ctxt, "command line", "command line", flag.Arg(0), "main")
+ addlibpath(Ctxt, "command line", "command line", flag.Arg(0), "main", "")
}
loadlib()