github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/link/internal/ld/deadcode.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ld
     6  
     7  import (
     8  	"github.com/gagliardetto/golang-go/cmd/internal/objabi"
     9  	"github.com/gagliardetto/golang-go/cmd/internal/sys"
    10  	"github.com/gagliardetto/golang-go/cmd/link/internal/sym"
    11  	"fmt"
    12  	"strings"
    13  	"unicode"
    14  )
    15  
    16  // deadcode marks all reachable symbols.
    17  //
    18  // The basis of the dead code elimination is a flood fill of symbols,
    19  // following their relocations, beginning at *flagEntrySymbol.
    20  //
    21  // This flood fill is wrapped in logic for pruning unused methods.
    22  // All methods are mentioned by relocations on their receiver's *rtype.
    23  // These relocations are specially defined as R_METHODOFF by the compiler
    24  // so we can detect and manipulated them here.
    25  //
    26  // There are three ways a method of a reachable type can be invoked:
    27  //
    28  //	1. direct call
    29  //	2. through a reachable interface type
    30  //	3. reflect.Value.Call, .Method, or reflect.Method.Func
    31  //
    32  // The first case is handled by the flood fill, a directly called method
    33  // is marked as reachable.
    34  //
    35  // The second case is handled by decomposing all reachable interface
    36  // types into method signatures. Each encountered method is compared
    37  // against the interface method signatures, if it matches it is marked
    38  // as reachable. This is extremely conservative, but easy and correct.
    39  //
    40  // The third case is handled by looking to see if any of:
    41  //	- reflect.Value.Call is reachable
    42  //	- reflect.Value.Method is reachable
    43  // 	- reflect.Type.Method or MethodByName is called.
    44  // If any of these happen, all bets are off and all exported methods
    45  // of reachable types are marked reachable.
    46  //
    47  // Any unreached text symbols are removed from ctxt.Textp.
    48  func deadcode(ctxt *Link) {
    49  	if ctxt.Debugvlog != 0 {
    50  		ctxt.Logf("deadcode\n")
    51  	}
    52  
    53  	if *flagNewobj {
    54  		deadcode2(ctxt)
    55  		return
    56  	}
    57  
    58  	d := &deadcodepass{
    59  		ctxt:        ctxt,
    60  		ifaceMethod: make(map[methodsig]bool),
    61  	}
    62  
    63  	// First, flood fill any symbols directly reachable in the call
    64  	// graph from *flagEntrySymbol. Ignore all methods not directly called.
    65  	d.init()
    66  	d.flood()
    67  
    68  	callSym := ctxt.Syms.ROLookup("reflect.Value.Call", sym.SymVerABIInternal)
    69  	methSym := ctxt.Syms.ROLookup("reflect.Value.Method", sym.SymVerABIInternal)
    70  	reflectSeen := false
    71  
    72  	if ctxt.DynlinkingGo() {
    73  		// Exported methods may satisfy interfaces we don't know
    74  		// about yet when dynamically linking.
    75  		reflectSeen = true
    76  	}
    77  
    78  	for {
    79  		if !reflectSeen {
    80  			if d.reflectMethod || (callSym != nil && callSym.Attr.Reachable()) || (methSym != nil && methSym.Attr.Reachable()) {
    81  				// Methods might be called via reflection. Give up on
    82  				// static analysis, mark all exported methods of
    83  				// all reachable types as reachable.
    84  				reflectSeen = true
    85  			}
    86  		}
    87  
    88  		// Mark all methods that could satisfy a discovered
    89  		// interface as reachable. We recheck old marked interfaces
    90  		// as new types (with new methods) may have been discovered
    91  		// in the last pass.
    92  		var rem []methodref
    93  		for _, m := range d.markableMethods {
    94  			if (reflectSeen && m.isExported()) || d.ifaceMethod[m.m] {
    95  				d.markMethod(m)
    96  			} else {
    97  				rem = append(rem, m)
    98  			}
    99  		}
   100  		d.markableMethods = rem
   101  
   102  		if len(d.markQueue) == 0 {
   103  			// No new work was discovered. Done.
   104  			break
   105  		}
   106  		d.flood()
   107  	}
   108  
   109  	// Remove all remaining unreached R_METHODOFF relocations.
   110  	for _, m := range d.markableMethods {
   111  		for _, r := range m.r {
   112  			d.cleanupReloc(r)
   113  		}
   114  	}
   115  
   116  	if ctxt.BuildMode != BuildModeShared {
   117  		// Keep a itablink if the symbol it points at is being kept.
   118  		// (When BuildModeShared, always keep itablinks.)
   119  		for _, s := range ctxt.Syms.Allsym {
   120  			if strings.HasPrefix(s.Name, "go.itablink.") {
   121  				s.Attr.Set(sym.AttrReachable, len(s.R) == 1 && s.R[0].Sym.Attr.Reachable())
   122  			}
   123  		}
   124  	}
   125  
   126  	addToTextp(ctxt)
   127  }
   128  
   129  func addToTextp(ctxt *Link) {
   130  	// Remove dead text but keep file information (z symbols).
   131  	textp := []*sym.Symbol{}
   132  	for _, s := range ctxt.Textp {
   133  		if s.Attr.Reachable() {
   134  			textp = append(textp, s)
   135  		}
   136  	}
   137  
   138  	// Put reachable text symbols into Textp.
   139  	// do it in postorder so that packages are laid down in dependency order
   140  	// internal first, then everything else
   141  	ctxt.Library = postorder(ctxt.Library)
   142  	for _, doInternal := range [2]bool{true, false} {
   143  		for _, lib := range ctxt.Library {
   144  			if isRuntimeDepPkg(lib.Pkg) != doInternal {
   145  				continue
   146  			}
   147  			libtextp := lib.Textp[:0]
   148  			for _, s := range lib.Textp {
   149  				if s.Attr.Reachable() {
   150  					textp = append(textp, s)
   151  					libtextp = append(libtextp, s)
   152  					if s.Unit != nil {
   153  						s.Unit.Textp = append(s.Unit.Textp, s)
   154  					}
   155  				}
   156  			}
   157  			for _, s := range lib.DupTextSyms {
   158  				if s.Attr.Reachable() && !s.Attr.OnList() {
   159  					textp = append(textp, s)
   160  					libtextp = append(libtextp, s)
   161  					if s.Unit != nil {
   162  						s.Unit.Textp = append(s.Unit.Textp, s)
   163  					}
   164  					s.Attr |= sym.AttrOnList
   165  					// dupok symbols may be defined in multiple packages. its
   166  					// associated package is chosen sort of arbitrarily (the
   167  					// first containing package that the linker loads). canonicalize
   168  					// it here to the package with which it will be laid down
   169  					// in text.
   170  					s.File = objabi.PathToPrefix(lib.Pkg)
   171  				}
   172  			}
   173  			lib.Textp = libtextp
   174  		}
   175  	}
   176  	ctxt.Textp = textp
   177  
   178  	if len(ctxt.Shlibs) > 0 {
   179  		// We might have overwritten some functions above (this tends to happen for the
   180  		// autogenerated type equality/hashing functions) and we don't want to generated
   181  		// pcln table entries for these any more so remove them from Textp.
   182  		textp := make([]*sym.Symbol, 0, len(ctxt.Textp))
   183  		for _, s := range ctxt.Textp {
   184  			if s.Type != sym.SDYNIMPORT {
   185  				textp = append(textp, s)
   186  			}
   187  		}
   188  		ctxt.Textp = textp
   189  	}
   190  }
   191  
   192  // methodref holds the relocations from a receiver type symbol to its
   193  // method. There are three relocations, one for each of the fields in
   194  // the reflect.method struct: mtyp, ifn, and tfn.
   195  type methodref struct {
   196  	m   methodsig
   197  	src *sym.Symbol   // receiver type symbol
   198  	r   [3]*sym.Reloc // R_METHODOFF relocations to fields of runtime.method
   199  }
   200  
   201  func (m methodref) ifn() *sym.Symbol { return m.r[1].Sym }
   202  
   203  func (m methodref) isExported() bool {
   204  	for _, r := range m.m {
   205  		return unicode.IsUpper(r)
   206  	}
   207  	panic("methodref has no signature")
   208  }
   209  
   210  // deadcodepass holds state for the deadcode flood fill.
   211  type deadcodepass struct {
   212  	ctxt            *Link
   213  	markQueue       []*sym.Symbol      // symbols to flood fill in next pass
   214  	ifaceMethod     map[methodsig]bool // methods declared in reached interfaces
   215  	markableMethods []methodref        // methods of reached types
   216  	reflectMethod   bool
   217  }
   218  
   219  func (d *deadcodepass) cleanupReloc(r *sym.Reloc) {
   220  	if r.Sym.Attr.Reachable() {
   221  		r.Type = objabi.R_ADDROFF
   222  	} else {
   223  		if d.ctxt.Debugvlog > 1 {
   224  			d.ctxt.Logf("removing method %s\n", r.Sym.Name)
   225  		}
   226  		r.Sym = nil
   227  		r.Siz = 0
   228  	}
   229  }
   230  
   231  // mark appends a symbol to the mark queue for flood filling.
   232  func (d *deadcodepass) mark(s, parent *sym.Symbol) {
   233  	if s == nil || s.Attr.Reachable() {
   234  		return
   235  	}
   236  	if s.Attr.ReflectMethod() {
   237  		d.reflectMethod = true
   238  	}
   239  	if *flagDumpDep {
   240  		p := "_"
   241  		if parent != nil {
   242  			p = parent.Name
   243  		}
   244  		fmt.Printf("%s -> %s\n", p, s.Name)
   245  	}
   246  	s.Attr |= sym.AttrReachable
   247  	if d.ctxt.Reachparent != nil {
   248  		d.ctxt.Reachparent[s] = parent
   249  	}
   250  	d.markQueue = append(d.markQueue, s)
   251  }
   252  
   253  // markMethod marks a method as reachable.
   254  func (d *deadcodepass) markMethod(m methodref) {
   255  	for _, r := range m.r {
   256  		d.mark(r.Sym, m.src)
   257  		r.Type = objabi.R_ADDROFF
   258  	}
   259  }
   260  
   261  // init marks all initial symbols as reachable.
   262  // In a typical binary, this is *flagEntrySymbol.
   263  func (d *deadcodepass) init() {
   264  	var names []string
   265  
   266  	if d.ctxt.BuildMode == BuildModeShared {
   267  		// Mark all symbols defined in this library as reachable when
   268  		// building a shared library.
   269  		for _, s := range d.ctxt.Syms.Allsym {
   270  			if s.Type != 0 && s.Type != sym.SDYNIMPORT {
   271  				d.mark(s, nil)
   272  			}
   273  		}
   274  	} else {
   275  		// In a normal binary, start at main.main and the init
   276  		// functions and mark what is reachable from there.
   277  
   278  		if d.ctxt.linkShared && (d.ctxt.BuildMode == BuildModeExe || d.ctxt.BuildMode == BuildModePIE) {
   279  			names = append(names, "main.main", "main..inittask")
   280  		} else {
   281  			// The external linker refers main symbol directly.
   282  			if d.ctxt.LinkMode == LinkExternal && (d.ctxt.BuildMode == BuildModeExe || d.ctxt.BuildMode == BuildModePIE) {
   283  				if d.ctxt.HeadType == objabi.Hwindows && d.ctxt.Arch.Family == sys.I386 {
   284  					*flagEntrySymbol = "_main"
   285  				} else {
   286  					*flagEntrySymbol = "main"
   287  				}
   288  			}
   289  			names = append(names, *flagEntrySymbol)
   290  			if d.ctxt.BuildMode == BuildModePlugin {
   291  				names = append(names, objabi.PathToPrefix(*flagPluginPath)+"..inittask", objabi.PathToPrefix(*flagPluginPath)+".main", "go.plugin.tabs")
   292  
   293  				// We don't keep the go.plugin.exports symbol,
   294  				// but we do keep the symbols it refers to.
   295  				exports := d.ctxt.Syms.ROLookup("go.plugin.exports", 0)
   296  				if exports != nil {
   297  					for i := range exports.R {
   298  						d.mark(exports.R[i].Sym, nil)
   299  					}
   300  				}
   301  			}
   302  		}
   303  		for _, s := range dynexp {
   304  			d.mark(s, nil)
   305  		}
   306  	}
   307  
   308  	for _, name := range names {
   309  		// Mark symbol as a data/ABI0 symbol.
   310  		d.mark(d.ctxt.Syms.ROLookup(name, 0), nil)
   311  		// Also mark any Go functions (internal ABI).
   312  		d.mark(d.ctxt.Syms.ROLookup(name, sym.SymVerABIInternal), nil)
   313  	}
   314  }
   315  
   316  // flood fills symbols reachable from the markQueue symbols.
   317  // As it goes, it collects methodref and interface method declarations.
   318  func (d *deadcodepass) flood() {
   319  	for len(d.markQueue) > 0 {
   320  		s := d.markQueue[0]
   321  		d.markQueue = d.markQueue[1:]
   322  		if s.Type == sym.STEXT {
   323  			if d.ctxt.Debugvlog > 1 {
   324  				d.ctxt.Logf("marktext %s\n", s.Name)
   325  			}
   326  		}
   327  
   328  		if strings.HasPrefix(s.Name, "type.") && s.Name[5] != '.' {
   329  			if len(s.P) == 0 {
   330  				// Probably a bug. The undefined symbol check
   331  				// later will give a better error than deadcode.
   332  				continue
   333  			}
   334  			if decodetypeKind(d.ctxt.Arch, s.P)&kindMask == kindInterface {
   335  				for _, sig := range decodeIfaceMethods(d.ctxt.Arch, s) {
   336  					if d.ctxt.Debugvlog > 1 {
   337  						d.ctxt.Logf("reached iface method: %s\n", sig)
   338  					}
   339  					d.ifaceMethod[sig] = true
   340  				}
   341  			}
   342  		}
   343  
   344  		mpos := 0 // 0-3, the R_METHODOFF relocs of runtime.uncommontype
   345  		var methods []methodref
   346  		for i := range s.R {
   347  			r := &s.R[i]
   348  			if r.Sym == nil {
   349  				continue
   350  			}
   351  			if r.Type == objabi.R_WEAKADDROFF {
   352  				// An R_WEAKADDROFF relocation is not reason
   353  				// enough to mark the pointed-to symbol as
   354  				// reachable.
   355  				continue
   356  			}
   357  			if r.Sym.Type == sym.SABIALIAS {
   358  				// Patch this relocation through the
   359  				// ABI alias before marking.
   360  				r.Sym = resolveABIAlias(r.Sym)
   361  			}
   362  			if r.Type != objabi.R_METHODOFF {
   363  				d.mark(r.Sym, s)
   364  				continue
   365  			}
   366  			// Collect rtype pointers to methods for
   367  			// later processing in deadcode.
   368  			if mpos == 0 {
   369  				m := methodref{src: s}
   370  				m.r[0] = r
   371  				methods = append(methods, m)
   372  			} else {
   373  				methods[len(methods)-1].r[mpos] = r
   374  			}
   375  			mpos++
   376  			if mpos == len(methodref{}.r) {
   377  				mpos = 0
   378  			}
   379  		}
   380  		if len(methods) > 0 {
   381  			// Decode runtime type information for type methods
   382  			// to help work out which methods can be called
   383  			// dynamically via interfaces.
   384  			methodsigs := decodetypeMethods(d.ctxt.Arch, s)
   385  			if len(methods) != len(methodsigs) {
   386  				panic(fmt.Sprintf("%q has %d method relocations for %d methods", s.Name, len(methods), len(methodsigs)))
   387  			}
   388  			for i, m := range methodsigs {
   389  				name := string(m)
   390  				name = name[:strings.Index(name, "(")]
   391  				if !strings.HasSuffix(methods[i].ifn().Name, name) {
   392  					panic(fmt.Sprintf("%q relocation for %q does not match method %q", s.Name, methods[i].ifn().Name, name))
   393  				}
   394  				methods[i].m = m
   395  			}
   396  			d.markableMethods = append(d.markableMethods, methods...)
   397  		}
   398  
   399  		if s.FuncInfo != nil {
   400  			for i := range s.FuncInfo.Funcdata {
   401  				d.mark(s.FuncInfo.Funcdata[i], s)
   402  			}
   403  		}
   404  		d.mark(s.Gotype, s)
   405  		d.mark(s.Sub, s)
   406  		d.mark(s.Outer, s)
   407  	}
   408  }