[dev.link] cmd/link: create sym.Symbols after deadcode in newobj mode

With the new object files, now we can run the deadcode pass on
indices instead of Symbol structs, so we can delay creating
Symbols after the deadcode pass. Then we only need to create
reachable symbols.

Not create Symbols in LoadNew and LoadRefs, and recombine
LoadReloc into LoadFull.

Split loadcgo into two parts: the first finds root symbols, the
second create Symbols and sets attributes. The first runs before
the deadcode pass, while the second runs after.

TODO: currently there are still symbols that are not marked
reachable but still used. This includes DWARF symbols, file
symbols, and type symbols that are referenced by DWARF symbols.
We still need to create them (conservatively).

Change-Id: I695779c9312be9d49ab1683957ac3e72e1f65a1e
Reviewed-on: https://go-review.googlesource.com/c/go/+/199643
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
This commit is contained in:
Cherry Zhang 2019-10-04 22:05:41 -04:00
parent 90fe9c7c5c
commit cab29ebd84
6 changed files with 217 additions and 238 deletions

View file

@ -379,6 +379,9 @@ func (ctxt *Link) loadlib() {
ctxt.loader = objfile.NewLoader()
}
ctxt.cgo_export_static = make(map[string]bool)
ctxt.cgo_export_dynamic = make(map[string]bool)
loadinternal(ctxt, "runtime")
if ctxt.Arch.Family == sys.ARM {
loadinternal(ctxt, "math")
@ -402,60 +405,16 @@ func (ctxt *Link) loadlib() {
}
if *flagNewobj {
// Add references of externally defined symbols.
objfile.LoadRefs(ctxt.loader, ctxt.Arch, ctxt.Syms)
// Load cgo directives.
for _, p := range ctxt.cgodata {
loadcgo(ctxt, p[0], p[1], p[2])
}
iscgo = ctxt.loader.Lookup("x_cgo_init", 0) != 0
ctxt.canUsePlugins = ctxt.loader.Lookup("plugin.Open", sym.SymVerABIInternal) != 0
} else {
iscgo = ctxt.Syms.ROLookup("x_cgo_init", 0) != nil
ctxt.canUsePlugins = ctxt.Syms.ROLookup("plugin.Open", sym.SymVerABIInternal) != nil
}
iscgo = ctxt.Lookup("x_cgo_init", 0) != nil
// Record whether we can use plugins.
ctxt.canUsePlugins = (ctxt.Lookup("plugin.Open", sym.SymVerABIInternal) != nil)
// We now have enough information to determine the link mode.
determineLinkMode(ctxt)
// Now that we know the link mode, trim the dynexp list.
x := sym.AttrCgoExportDynamic
if ctxt.LinkMode == LinkExternal {
x = sym.AttrCgoExportStatic
}
w := 0
for i := range dynexp {
if dynexp[i].Attr&x != 0 {
dynexp[w] = dynexp[i]
w++
}
}
dynexp = dynexp[:w]
// Resolve ABI aliases in the list of cgo-exported functions.
// This is necessary because we load the ABI0 symbol for all
// cgo exports.
for i, s := range dynexp {
if s.Type != sym.SABIALIAS {
continue
}
t := resolveABIAlias(s)
t.Attr |= s.Attr
t.SetExtname(s.Extname())
dynexp[i] = t
}
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 ctxt.LinkMode == LinkExternal && !iscgo && ctxt.LibraryByPkg["runtime/cgo"] == nil && !(objabi.GOOS == "darwin" && (ctxt.Arch.Family == sys.AMD64 || ctxt.Arch.Family == sys.I386)) {
// This indicates a user requested -linkmode=external.
// The startup code uses an import of runtime/cgo to decide
@ -473,6 +432,25 @@ func (ctxt *Link) loadlib() {
}
}
if *flagNewobj {
// Add references of externally defined symbols.
objfile.LoadRefs(ctxt.loader, ctxt.Arch, ctxt.Syms)
}
// Now that we know the link mode, set the dynexp list.
if !*flagNewobj { // set this later in newobj mode
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.
@ -545,6 +523,35 @@ func (ctxt *Link) loadlib() {
importcycles()
}
// Set up dynexp list.
func setupdynexp(ctxt *Link) {
dynexpMap := ctxt.cgo_export_dynamic
if ctxt.LinkMode == LinkExternal {
dynexpMap = ctxt.cgo_export_static
}
dynexp = make([]*sym.Symbol, 0, len(dynexpMap))
for exp := range dynexpMap {
s := ctxt.Syms.Lookup(exp, 0)
dynexp = append(dynexp, s)
}
// Resolve ABI aliases in the list of cgo-exported functions.
// This is necessary because we load the ABI0 symbol for all
// cgo exports.
for i, s := range dynexp {
if s.Type != sym.SABIALIAS {
continue
}
t := resolveABIAlias(s)
t.Attr |= s.Attr
t.SetExtname(s.Extname())
dynexp[i] = t
}
ctxt.cgo_export_static = nil
ctxt.cgo_export_dynamic = nil
}
// Set up flags and special symbols depending on the platform build mode.
func (ctxt *Link) linksetup() {
switch ctxt.BuildMode {
@ -1923,7 +1930,7 @@ func ldshlibsyms(ctxt *Link, shlib string) {
ver = sym.SymVerABIInternal
}
lsym := ctxt.LookupOrCreate(elfsym.Name, ver)
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
@ -2543,8 +2550,7 @@ func dfs(lib *sym.Library, mark map[*sym.Library]markKind, order *[]*sym.Library
func (ctxt *Link) loadlibfull() {
// Load full symbol contents, resolve indexed references.
objfile.LoadReloc(ctxt.loader)
objfile.LoadFull(ctxt.loader)
objfile.LoadFull(ctxt.loader, ctxt.Arch, ctxt.Syms)
// For now, add all symbols to ctxt.Syms.
for _, s := range ctxt.loader.Syms {
@ -2553,8 +2559,16 @@ func (ctxt *Link) loadlibfull() {
}
}
// Load cgo directives.
for _, d := range ctxt.cgodata {
setCgoAttr(ctxt, d.file, d.pkg, d.directives)
}
setupdynexp(ctxt)
// Drop the reference.
ctxt.loader = nil
ctxt.cgodata = nil
addToTextp(ctxt)
}