cmd/link: establish dependable package initialization order

As described here:

https://github.com/golang/go/issues/31636#issuecomment-493271830

"Find the lexically earliest package that is not initialized yet,
but has had all its dependencies initialized, initialize that package,
 and repeat."

Simplify the runtime a bit, by just computing the ordering required
in the linker and giving a list to the runtime.

Update #31636
Fixes #57411

RELNOTE=yes

Change-Id: I1e4d3878ebe6e8953527aedb730824971d722cac
Reviewed-on: https://go-review.googlesource.com/c/go/+/462035
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Keith Randall 2023-01-12 20:25:39 -08:00
parent cd6d225bd3
commit ce2a609909
14 changed files with 324 additions and 56 deletions

View file

@ -113,6 +113,9 @@ func (d *deadcodePass) init() {
if d.mapinitnoop == 0 {
panic("could not look up runtime.mapinitnoop")
}
if d.ctxt.mainInittasks != 0 {
d.mark(d.ctxt.mainInittasks, 0)
}
}
func (d *deadcodePass) flood() {
@ -208,6 +211,11 @@ func (d *deadcodePass) flood() {
}
d.genericIfaceMethod[name] = true
continue // don't mark referenced symbol - it is not needed in the final binary.
case objabi.R_INITORDER:
// inittasks has already run, so any R_INITORDER links are now
// superfluous - the only live inittask records are those which are
// in a scheduled list somewhere (e.g. runtime.moduledata.inittasks).
continue
}
rs := r.Sym()
if isgotype && usedInIface && d.ldr.IsGoType(rs) && !d.ldr.AttrUsedInIface(rs) {