[dev.link] cmd/link: add basic shared library support in newobj mode

This CL adds basic shared library support in newobj mode. This is
not complete -- there are still tests in misc/cgo/testshared
failing. But at least a simple program works, and some tests
there pass.

Add the mechanism of loading external symbols with contents.
(Before, external symbols are always contentless.) This may
potentially be also used for other host objects.

Change-Id: I68dbf71e7949cc01ebf37ea159084e798ae16925
Reviewed-on: https://go-review.googlesource.com/c/go/+/201537
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
This commit is contained in:
Cherry Zhang 2019-10-16 15:44:04 -04:00
parent 5caac2f73e
commit 2bbf2e0233
3 changed files with 155 additions and 24 deletions

View file

@ -433,6 +433,15 @@ func (ctxt *Link) loadlib() {
}
}
for _, lib := range ctxt.Library {
if lib.Shlib != "" {
if ctxt.Debugvlog > 1 {
ctxt.Logf("%5.2f autolib: %s (from %s)\n", Cputime(), lib.Shlib, lib.Objref)
}
ldshlibsyms(ctxt, lib.Shlib)
}
}
if *flagNewobj {
// Add references of externally defined symbols.
ctxt.loader.LoadRefs(ctxt.Arch, ctxt.Syms)
@ -443,15 +452,6 @@ func (ctxt *Link) loadlib() {
setupdynexp(ctxt)
}
for _, lib := range ctxt.Library {
if lib.Shlib != "" {
if ctxt.Debugvlog > 1 {
ctxt.Logf("%5.2f autolib: %s (from %s)\n", Cputime(), lib.Shlib, lib.Objref)
}
ldshlibsyms(ctxt, lib.Shlib)
}
}
// In internal link mode, read the host object files.
if ctxt.LinkMode == LinkInternal && len(hostobj) != 0 {
// Drop all the cgo_import_static declarations.
@ -1931,7 +1931,17 @@ func ldshlibsyms(ctxt *Link, shlib string) {
ver = sym.SymVerABIInternal
}
lsym := ctxt.Syms.Lookup(elfsym.Name, ver)
var lsym *sym.Symbol
if *flagNewobj {
i := ctxt.loader.AddExtSym(elfsym.Name, ver)
if i == 0 {
continue
}
lsym = ctxt.Syms.Newsym(elfsym.Name, ver)
ctxt.loader.Syms[i] = lsym
} else {
lsym = ctxt.Syms.Lookup(elfsym.Name, ver)
}
// Because loadlib above loads all .a files before loading any shared
// libraries, any non-dynimport symbols we find that duplicate symbols
// already loaded should be ignored (the symbols from the .a files
@ -1960,7 +1970,17 @@ func ldshlibsyms(ctxt *Link, shlib string) {
// mangle Go function names in the .so to include the
// ABI.
if elf.ST_TYPE(elfsym.Info) == elf.STT_FUNC && ver == 0 {
alias := ctxt.Syms.Lookup(elfsym.Name, sym.SymVerABIInternal)
var alias *sym.Symbol
if *flagNewobj {
i := ctxt.loader.AddExtSym(elfsym.Name, sym.SymVerABIInternal)
if i == 0 {
continue
}
alias = ctxt.Syms.Newsym(elfsym.Name, sym.SymVerABIInternal)
ctxt.loader.Syms[i] = alias
} else {
alias = ctxt.Syms.Lookup(elfsym.Name, sym.SymVerABIInternal)
}
if alias.Type != 0 {
continue
}