[dev.link] cmd/link: fix up ctxt.Textp2 handling in AssignTextSymbolOrder

Change the loader method AssignTextSymbolOrder to return a slice of
all reachable textp symbols, since it will be needed in second-phase
DWARF gen.

Change-Id: Iaf16ee9cf0d5266aeb0d3df596e8117263b35d8e
Reviewed-on: https://go-review.googlesource.com/c/go/+/220985
Run-TryBot: Than McIntosh <thanm@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
This commit is contained in:
Than McIntosh 2020-02-25 09:07:39 -05:00
parent 7d8aef689d
commit 85d62a91ee
4 changed files with 75 additions and 83 deletions

View file

@ -45,80 +45,56 @@ func deadcode(ctxt *Link) {
deadcode2(ctxt)
}
// addToTextp populates the context Textp slice (needed in various places
// in the linker) and also the unit Textp slices (needed by the "old"
// phase 2 DWARF generation).
func addToTextp(ctxt *Link) {
// Remove dead text but keep file information (z symbols).
textp := []*sym.Symbol{}
for _, s := range ctxt.Textp {
if s.Attr.Reachable() {
textp = append(textp, s)
}
}
// Append the sym.Symbol's that correspond to the reachable
// loader.Sym's created by the new host object loader.
// FIXME: is this the right way to do this? Or should there be
// some way to associated a given host object symbol with the Go
// package that refers to it?
for _, s := range ctxt.Textp2 {
if !ctxt.loader.AttrReachable(s) {
// First set up ctxt.Textp, based on ctxt.Textp2.
textp := make([]*sym.Symbol, 0, len(ctxt.Textp2))
haveshlibs := len(ctxt.Shlibs) > 0
for _, tsym := range ctxt.Textp2 {
sp := ctxt.loader.Syms[tsym]
if sp == nil || !ctxt.loader.AttrReachable(tsym) {
panic("should never happen")
}
if haveshlibs && sp.Type == sym.SDYNIMPORT {
continue
}
textp = append(textp, ctxt.loader.Syms[s])
textp = append(textp, sp)
}
ctxt.Textp = textp
// Put reachable text symbols into Textp.
// do it in postorder so that packages are laid down in dependency order
// internal first, then everything else. This also populates lib and
// unit Textp slices, which are needed for DWARF
// FIXME: once DWARF is completely converted to using loader.Sym,
// we can remove the *sym.Symbol Textp slices.
ctxt.Library = postorder(ctxt.Library)
// Dupok symbols may be defined in multiple packages; the
// associated package for a dupok sym is chosen sort of
// arbitrarily (the first containing package that the linker
// loads). The loop below canonicalizes the File to the package
// with which it will be laid down in text. Assumes that
// ctxt.Library is already in postorder.
for _, doInternal := range [2]bool{true, false} {
for _, lib := range ctxt.Library {
if isRuntimeDepPkg(lib.Pkg) != doInternal {
continue
}
libtextp := lib.Textp[:0]
for _, s := range lib.Textp {
if s.Attr.Reachable() {
textp = append(textp, s)
libtextp = append(libtextp, s)
if s.Unit != nil {
s.Unit.Textp = append(s.Unit.Textp, s)
}
for _, dsym := range lib.DupTextSyms2 {
tsp := ctxt.loader.Syms[dsym]
if !tsp.Attr.OnList() {
tsp.Attr |= sym.AttrOnList
tsp.File = objabi.PathToPrefix(lib.Pkg)
}
}
for _, s := range lib.DupTextSyms {
if s.Attr.Reachable() && !s.Attr.OnList() {
textp = append(textp, s)
libtextp = append(libtextp, s)
if s.Unit != nil {
s.Unit.Textp = append(s.Unit.Textp, s)
}
s.Attr |= sym.AttrOnList
// dupok symbols may be defined in multiple packages. its
// associated package is chosen sort of arbitrarily (the
// first containing package that the linker loads). canonicalize
// it here to the package with which it will be laid down
// in text.
s.File = objabi.PathToPrefix(lib.Pkg)
}
}
lib.Textp = libtextp
}
}
ctxt.Textp = textp
if len(ctxt.Shlibs) > 0 {
// We might have overwritten some functions above (this tends to happen for the
// autogenerated type equality/hashing functions) and we don't want to generated
// pcln table entries for these any more so remove them from Textp.
textp := make([]*sym.Symbol, 0, len(ctxt.Textp))
for _, s := range ctxt.Textp {
if s.Type != sym.SDYNIMPORT {
textp = append(textp, s)
// Finally, set up compilation unit Textp slices. Can be removed
// once loader-Sym DWARF-gen phase 2 is always enabled.
for _, lib := range ctxt.Library {
for _, unit := range lib.Units {
for _, usym := range unit.Textp2 {
usp := ctxt.loader.Syms[usym]
usp.Attr |= sym.AttrOnList
unit.Textp = append(unit.Textp, usp)
}
}
ctxt.Textp = textp
}
}