cmd/link: memoize/cache whether plugin.Open symbol available

Perform a single lookup of "plugin.Open" at the point where we set the
loaded flag for the context, then cache whether the result is nil, so
that we can consult this cached value later on (instead of having to
look up the symbol each time). This helps speed up the DynLinkingGo()
context method, which is called from within some very hot loops in the
linker (when linking 'hyperkube' from kubernetes, reduces total calls
to "sym.(*Symbols).ROLookup" from 6.5M to 4.3M)

Change-Id: I92a2ea2b21d24f67aec0a7afeef4acc77c095adf
Reviewed-on: https://go-review.googlesource.com/c/go/+/193260
Reviewed-by: Jeremy Faller <jeremy@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
Than McIntosh 2019-09-04 11:25:06 -04:00
parent c7dc5e92dd
commit b1a20253fe
2 changed files with 9 additions and 2 deletions

View file

@ -173,12 +173,15 @@ func (ctxt *Link) DynlinkingGo() bool {
if !ctxt.Loaded {
panic("DynlinkingGo called before all symbols loaded")
}
return ctxt.BuildMode == BuildModeShared || ctxt.linkShared || ctxt.BuildMode == BuildModePlugin || ctxt.CanUsePlugins()
return ctxt.BuildMode == BuildModeShared || ctxt.linkShared || ctxt.BuildMode == BuildModePlugin || ctxt.canUsePlugins
}
// CanUsePlugins reports whether a plugins can be used
func (ctxt *Link) CanUsePlugins() bool {
return ctxt.Syms.ROLookup("plugin.Open", sym.SymVerABIInternal) != nil
if !ctxt.Loaded {
panic("CanUsePlugins called before all symbols loaded")
}
return ctxt.canUsePlugins
}
// UseRelro reports whether to make use of "read only relocations" aka
@ -595,6 +598,9 @@ func (ctxt *Link) loadlib() {
// We've loaded all the code now.
ctxt.Loaded = true
// Record whether we can use plugins.
ctxt.canUsePlugins = (ctxt.Syms.ROLookup("plugin.Open", sym.SymVerABIInternal) != nil)
// If there are no dynamic libraries needed, gcc disables dynamic linking.
// Because of this, glibc's dynamic ELF loader occasionally (like in version 2.13)
// assumes that a dynamic binary always refers to at least one dynamic library.

View file

@ -67,6 +67,7 @@ type Link struct {
linkShared bool // link against installed Go shared libraries
LinkMode LinkMode
BuildMode BuildMode
canUsePlugins bool // initialized when Loaded is set to true
compressDWARF bool
Tlsg *sym.Symbol