]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.link] cmd/link: add symbols to Textp after deadcode pass
authorCherry Zhang <cherryyz@google.com>
Tue, 24 Sep 2019 22:12:55 +0000 (18:12 -0400)
committerCherry Zhang <cherryyz@google.com>
Tue, 8 Oct 2019 19:15:46 +0000 (19:15 +0000)
Currently we add all text symbols to ctxt.Textp at load time,
then the deadcode pass filters out unreachable ones. This CL
delays adding symbols to ctxt.Textp to the end of the deadcode
pass, where we only add reachable ones.

Change-Id: Ie83b2958f915c5aaa004b8c5ed1f1bc275f4d1db
Reviewed-on: https://go-review.googlesource.com/c/go/+/197257
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
src/cmd/link/internal/ld/deadcode.go
src/cmd/link/internal/ld/lib.go

index c880c0da01ac9d66d53399e27ead558237a5b95b..cadb92b43cdc2a263bf28f8f56532af4b5478a33 100644 (file)
@@ -118,22 +118,66 @@ func deadcode(ctxt *Link) {
                }
        }
 
-       for _, lib := range ctxt.Library {
-               lib.Textp = lib.Textp[:0]
-       }
-
        // Remove dead text but keep file information (z symbols).
-       textp := make([]*sym.Symbol, 0, len(ctxt.Textp))
+       textp := []*sym.Symbol{}
        for _, s := range ctxt.Textp {
                if s.Attr.Reachable() {
-                       if s.Unit != nil {
-                               s.Unit.Lib.Textp = append(s.Unit.Lib.Textp, s)
-                               s.Unit.Textp = append(s.Unit.Textp, s)
-                       }
                        textp = append(textp, s)
                }
        }
+
+       // Put reachable text symbols into Textp.
+       // do it in postorder so that packages are laid down in dependency order
+       // internal first, then everything else
+       ctxt.Library = postorder(ctxt.Library)
+       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 _, 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)
+                       }
+               }
+               ctxt.Textp = textp
+       }
 }
 
 // methodref holds the relocations from a receiver type symbol to its
index 5ab43cca705d802d24e759311d26c34ec36b9c39..f11adbcfb6a98ae3c878a0095c50b582ded63ef3 100644 (file)
@@ -538,44 +538,6 @@ func (ctxt *Link) loadlib() {
        }
 
        importcycles()
-
-       // put symbols into Textp
-       // do it in postorder so that packages are laid down in dependency order
-       // internal first, then everything else
-       ctxt.Library = postorder(ctxt.Library)
-       for _, doInternal := range [2]bool{true, false} {
-               for _, lib := range ctxt.Library {
-                       if isRuntimeDepPkg(lib.Pkg) != doInternal {
-                               continue
-                       }
-                       ctxt.Textp = append(ctxt.Textp, lib.Textp...)
-                       for _, s := range lib.DupTextSyms {
-                               if !s.Attr.OnList() {
-                                       ctxt.Textp = append(ctxt.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)
-                               }
-                       }
-               }
-       }
-
-       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)
-                       }
-               }
-               ctxt.Textp = textp
-       }
 }
 
 // Set up flags and special symbols depending on the platform build mode.