diff options
author | Than McIntosh <thanm@google.com> | 2023-09-06 10:15:37 -0400 |
---|---|---|
committer | Than McIntosh <thanm@google.com> | 2023-09-07 13:18:51 +0000 |
commit | 660620dd45dc11f2d889add79bedf2dc771c7d04 (patch) | |
tree | 56b5d8b4830866fb6ea5125292e14f836ee37121 /src/cmd/link/internal/ld/deadcode.go | |
parent | 3466e57354974cd2d17ce902df02e0ac58b8ded5 (diff) | |
download | go-660620dd45dc11f2d889add79bedf2dc771c7d04.tar.gz go-660620dd45dc11f2d889add79bedf2dc771c7d04.zip |
cmd/link: avoid deadcode of global map vars for programs using plugins
If a program imports the plugin package, the mechanisms in place for
detecting and deleting unused global map variables are no longer safe,
since it's possibly for a given global map var to be unreferenced in
the main program but referenced by a plugin. This patch changes the
linker to test for plugin use and to avoid removing any unused global
map variables if the main program could possibly load up a plugin.
Fixes #62430.
Change-Id: Ie00b18b681cb0d259e3c859ac947ade5778cd6c8
Reviewed-on: https://go-review.googlesource.com/c/go/+/526115
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/cmd/link/internal/ld/deadcode.go')
-rw-r--r-- | src/cmd/link/internal/ld/deadcode.go | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/cmd/link/internal/ld/deadcode.go b/src/cmd/link/internal/ld/deadcode.go index b365a3b39e..a051e43401 100644 --- a/src/cmd/link/internal/ld/deadcode.go +++ b/src/cmd/link/internal/ld/deadcode.go @@ -143,10 +143,26 @@ func (d *deadcodePass) flood() { methods = methods[:0] for i := 0; i < relocs.Count(); i++ { r := relocs.At(i) - // When build with "-linkshared", we can't tell if the interface - // method in itab will be used or not. Ignore the weak attribute. - if r.Weak() && !(d.ctxt.linkShared && d.ldr.IsItab(symIdx)) { - continue + if r.Weak() { + convertWeakToStrong := false + // When build with "-linkshared", we can't tell if the + // interface method in itab will be used or not. + // Ignore the weak attribute. + if d.ctxt.linkShared && d.ldr.IsItab(symIdx) { + convertWeakToStrong = true + } + // If the program uses plugins, we can no longer treat + // relocs from pkg init functions to outlined map init + // fragments as weak, since doing so can cause package + // init clashes between the main program and the + // plugin. See #62430 for more details. + if d.ctxt.canUsePlugins && r.Type().IsDirectCall() { + convertWeakToStrong = true + } + if !convertWeakToStrong { + // skip this reloc + continue + } } t := r.Type() switch t { |