Source file src/cmd/link/internal/ld/deadcode.go

     1  // Copyright 2019 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  	"cmd/internal/goobj"
     9  	"cmd/internal/objabi"
    10  	"cmd/internal/sys"
    11  	"cmd/link/internal/loader"
    12  	"cmd/link/internal/sym"
    13  	"fmt"
    14  	"internal/buildcfg"
    15  	"unicode"
    16  )
    17  
    18  var _ = fmt.Print
    19  
    20  type deadcodePass struct {
    21  	ctxt *Link
    22  	ldr  *loader.Loader
    23  	wq   heap // work queue, using min-heap for better locality
    24  
    25  	ifaceMethod     map[methodsig]bool // methods declared in reached interfaces
    26  	markableMethods []methodref        // methods of reached types
    27  	reflectSeen     bool               // whether we have seen a reflect method call
    28  	dynlink         bool
    29  
    30  	methodsigstmp []methodsig // scratch buffer for decoding method signatures
    31  }
    32  
    33  func (d *deadcodePass) init() {
    34  	d.ldr.InitReachable()
    35  	d.ifaceMethod = make(map[methodsig]bool)
    36  	if buildcfg.Experiment.FieldTrack {
    37  		d.ldr.Reachparent = make([]loader.Sym, d.ldr.NSym())
    38  	}
    39  	d.dynlink = d.ctxt.DynlinkingGo()
    40  
    41  	if d.ctxt.BuildMode == BuildModeShared {
    42  		// Mark all symbols defined in this library as reachable when
    43  		// building a shared library.
    44  		n := d.ldr.NDef()
    45  		for i := 1; i < n; i++ {
    46  			s := loader.Sym(i)
    47  			d.mark(s, 0)
    48  		}
    49  		return
    50  	}
    51  
    52  	var names []string
    53  
    54  	// In a normal binary, start at main.main and the init
    55  	// functions and mark what is reachable from there.
    56  	if d.ctxt.linkShared && (d.ctxt.BuildMode == BuildModeExe || d.ctxt.BuildMode == BuildModePIE) {
    57  		names = append(names, "main.main", "main..inittask")
    58  	} else {
    59  		// The external linker refers main symbol directly.
    60  		if d.ctxt.LinkMode == LinkExternal && (d.ctxt.BuildMode == BuildModeExe || d.ctxt.BuildMode == BuildModePIE) {
    61  			if d.ctxt.HeadType == objabi.Hwindows && d.ctxt.Arch.Family == sys.I386 {
    62  				*flagEntrySymbol = "_main"
    63  			} else {
    64  				*flagEntrySymbol = "main"
    65  			}
    66  		}
    67  		names = append(names, *flagEntrySymbol)
    68  	}
    69  	// runtime.unreachableMethod is a function that will throw if called.
    70  	// We redirect unreachable methods to it.
    71  	names = append(names, "runtime.unreachableMethod")
    72  	if !d.ctxt.linkShared && d.ctxt.BuildMode != BuildModePlugin {
    73  		// runtime.buildVersion and runtime.modinfo are referenced in .go.buildinfo section
    74  		// (see function buildinfo in data.go). They should normally be reachable from the
    75  		// runtime. Just make it explicit, in case.
    76  		names = append(names, "runtime.buildVersion", "runtime.modinfo")
    77  	}
    78  	if d.ctxt.BuildMode == BuildModePlugin {
    79  		names = append(names, objabi.PathToPrefix(*flagPluginPath)+"..inittask", objabi.PathToPrefix(*flagPluginPath)+".main", "go.plugin.tabs")
    80  
    81  		// We don't keep the go.plugin.exports symbol,
    82  		// but we do keep the symbols it refers to.
    83  		exportsIdx := d.ldr.Lookup("go.plugin.exports", 0)
    84  		if exportsIdx != 0 {
    85  			relocs := d.ldr.Relocs(exportsIdx)
    86  			for i := 0; i < relocs.Count(); i++ {
    87  				d.mark(relocs.At(i).Sym(), 0)
    88  			}
    89  		}
    90  	}
    91  
    92  	if d.ctxt.Debugvlog > 1 {
    93  		d.ctxt.Logf("deadcode start names: %v\n", names)
    94  	}
    95  
    96  	for _, name := range names {
    97  		// Mark symbol as a data/ABI0 symbol.
    98  		d.mark(d.ldr.Lookup(name, 0), 0)
    99  		// Also mark any Go functions (internal ABI).
   100  		d.mark(d.ldr.Lookup(name, sym.SymVerABIInternal), 0)
   101  	}
   102  
   103  	// All dynamic exports are roots.
   104  	for _, s := range d.ctxt.dynexp {
   105  		if d.ctxt.Debugvlog > 1 {
   106  			d.ctxt.Logf("deadcode start dynexp: %s<%d>\n", d.ldr.SymName(s), d.ldr.SymVersion(s))
   107  		}
   108  		d.mark(s, 0)
   109  	}
   110  }
   111  
   112  func (d *deadcodePass) flood() {
   113  	var methods []methodref
   114  	for !d.wq.empty() {
   115  		symIdx := d.wq.pop()
   116  
   117  		d.reflectSeen = d.reflectSeen || d.ldr.IsReflectMethod(symIdx)
   118  
   119  		isgotype := d.ldr.IsGoType(symIdx)
   120  		relocs := d.ldr.Relocs(symIdx)
   121  		var usedInIface bool
   122  
   123  		if isgotype {
   124  			if d.dynlink {
   125  				// When dynamic linking, a type may be passed across DSO
   126  				// boundary and get converted to interface at the other side.
   127  				d.ldr.SetAttrUsedInIface(symIdx, true)
   128  			}
   129  			usedInIface = d.ldr.AttrUsedInIface(symIdx)
   130  		}
   131  
   132  		methods = methods[:0]
   133  		for i := 0; i < relocs.Count(); i++ {
   134  			r := relocs.At(i)
   135  			// When build with "-linkshared", we can't tell if the interface
   136  			// method in itab will be used or not. Ignore the weak attribute.
   137  			if r.Weak() && !(d.ctxt.linkShared && d.ldr.IsItab(symIdx)) {
   138  				continue
   139  			}
   140  			t := r.Type()
   141  			switch t {
   142  			case objabi.R_METHODOFF:
   143  				if i+2 >= relocs.Count() {
   144  					panic("expect three consecutive R_METHODOFF relocs")
   145  				}
   146  				if usedInIface {
   147  					methods = append(methods, methodref{src: symIdx, r: i})
   148  					// The method descriptor is itself a type descriptor, and
   149  					// it can be used to reach other types, e.g. by using
   150  					// reflect.Type.Method(i).Type.In(j). We need to traverse
   151  					// its child types with UsedInIface set. (See also the
   152  					// comment below.)
   153  					rs := r.Sym()
   154  					if !d.ldr.AttrUsedInIface(rs) {
   155  						d.ldr.SetAttrUsedInIface(rs, true)
   156  						if d.ldr.AttrReachable(rs) {
   157  							d.ldr.SetAttrReachable(rs, false)
   158  							d.mark(rs, symIdx)
   159  						}
   160  					}
   161  				}
   162  				i += 2
   163  				continue
   164  			case objabi.R_USETYPE:
   165  				// type symbol used for DWARF. we need to load the symbol but it may not
   166  				// be otherwise reachable in the program.
   167  				// do nothing for now as we still load all type symbols.
   168  				continue
   169  			case objabi.R_USEIFACE:
   170  				// R_USEIFACE is a marker relocation that tells the linker the type is
   171  				// converted to an interface, i.e. should have UsedInIface set. See the
   172  				// comment below for why we need to unset the Reachable bit and re-mark it.
   173  				rs := r.Sym()
   174  				if !d.ldr.AttrUsedInIface(rs) {
   175  					d.ldr.SetAttrUsedInIface(rs, true)
   176  					if d.ldr.AttrReachable(rs) {
   177  						d.ldr.SetAttrReachable(rs, false)
   178  						d.mark(rs, symIdx)
   179  					}
   180  				}
   181  				continue
   182  			case objabi.R_USEIFACEMETHOD:
   183  				// R_USEIFACEMETHOD is a marker relocation that marks an interface
   184  				// method as used.
   185  				rs := r.Sym()
   186  				if d.ctxt.linkShared && (d.ldr.SymType(rs) == sym.SDYNIMPORT || d.ldr.SymType(rs) == sym.Sxxx) {
   187  					// Don't decode symbol from shared library (we'll mark all exported methods anyway).
   188  					// We check for both SDYNIMPORT and Sxxx because name-mangled symbols haven't
   189  					// been resolved at this point.
   190  					continue
   191  				}
   192  				m := d.decodeIfaceMethod(d.ldr, d.ctxt.Arch, rs, r.Add())
   193  				if d.ctxt.Debugvlog > 1 {
   194  					d.ctxt.Logf("reached iface method: %v\n", m)
   195  				}
   196  				d.ifaceMethod[m] = true
   197  				continue
   198  			}
   199  			rs := r.Sym()
   200  			if isgotype && usedInIface && d.ldr.IsGoType(rs) && !d.ldr.AttrUsedInIface(rs) {
   201  				// If a type is converted to an interface, it is possible to obtain an
   202  				// interface with a "child" type of it using reflection (e.g. obtain an
   203  				// interface of T from []chan T). We need to traverse its "child" types
   204  				// with UsedInIface attribute set.
   205  				// When visiting the child type (chan T in the example above), it will
   206  				// have UsedInIface set, so it in turn will mark and (re)visit its children
   207  				// (e.g. T above).
   208  				// We unset the reachable bit here, so if the child type is already visited,
   209  				// it will be visited again.
   210  				// Note that a type symbol can be visited at most twice, one without
   211  				// UsedInIface and one with. So termination is still guaranteed.
   212  				d.ldr.SetAttrUsedInIface(rs, true)
   213  				d.ldr.SetAttrReachable(rs, false)
   214  			}
   215  			d.mark(rs, symIdx)
   216  		}
   217  		naux := d.ldr.NAux(symIdx)
   218  		for i := 0; i < naux; i++ {
   219  			a := d.ldr.Aux(symIdx, i)
   220  			if a.Type() == goobj.AuxGotype {
   221  				// A symbol being reachable doesn't imply we need its
   222  				// type descriptor. Don't mark it.
   223  				continue
   224  			}
   225  			d.mark(a.Sym(), symIdx)
   226  		}
   227  		// Some host object symbols have an outer object, which acts like a
   228  		// "carrier" symbol, or it holds all the symbols for a particular
   229  		// section. We need to mark all "referenced" symbols from that carrier,
   230  		// so we make sure we're pulling in all outer symbols, and their sub
   231  		// symbols. This is not ideal, and these carrier/section symbols could
   232  		// be removed.
   233  		if d.ldr.IsExternal(symIdx) {
   234  			d.mark(d.ldr.OuterSym(symIdx), symIdx)
   235  			d.mark(d.ldr.SubSym(symIdx), symIdx)
   236  		}
   237  
   238  		if len(methods) != 0 {
   239  			if !isgotype {
   240  				panic("method found on non-type symbol")
   241  			}
   242  			// Decode runtime type information for type methods
   243  			// to help work out which methods can be called
   244  			// dynamically via interfaces.
   245  			methodsigs := d.decodetypeMethods(d.ldr, d.ctxt.Arch, symIdx, &relocs)
   246  			if len(methods) != len(methodsigs) {
   247  				panic(fmt.Sprintf("%q has %d method relocations for %d methods", d.ldr.SymName(symIdx), len(methods), len(methodsigs)))
   248  			}
   249  			for i, m := range methodsigs {
   250  				methods[i].m = m
   251  				if d.ctxt.Debugvlog > 1 {
   252  					d.ctxt.Logf("markable method: %v of sym %v %s\n", m, symIdx, d.ldr.SymName(symIdx))
   253  				}
   254  			}
   255  			d.markableMethods = append(d.markableMethods, methods...)
   256  		}
   257  	}
   258  }
   259  
   260  func (d *deadcodePass) mark(symIdx, parent loader.Sym) {
   261  	if symIdx != 0 && !d.ldr.AttrReachable(symIdx) {
   262  		d.wq.push(symIdx)
   263  		d.ldr.SetAttrReachable(symIdx, true)
   264  		if buildcfg.Experiment.FieldTrack && d.ldr.Reachparent[symIdx] == 0 {
   265  			d.ldr.Reachparent[symIdx] = parent
   266  		}
   267  		if *flagDumpDep {
   268  			to := d.ldr.SymName(symIdx)
   269  			if to != "" {
   270  				if d.ldr.AttrUsedInIface(symIdx) {
   271  					to += " <UsedInIface>"
   272  				}
   273  				from := "_"
   274  				if parent != 0 {
   275  					from = d.ldr.SymName(parent)
   276  					if d.ldr.AttrUsedInIface(parent) {
   277  						from += " <UsedInIface>"
   278  					}
   279  				}
   280  				fmt.Printf("%s -> %s\n", from, to)
   281  			}
   282  		}
   283  	}
   284  }
   285  
   286  func (d *deadcodePass) markMethod(m methodref) {
   287  	relocs := d.ldr.Relocs(m.src)
   288  	d.mark(relocs.At(m.r).Sym(), m.src)
   289  	d.mark(relocs.At(m.r+1).Sym(), m.src)
   290  	d.mark(relocs.At(m.r+2).Sym(), m.src)
   291  }
   292  
   293  // deadcode marks all reachable symbols.
   294  //
   295  // The basis of the dead code elimination is a flood fill of symbols,
   296  // following their relocations, beginning at *flagEntrySymbol.
   297  //
   298  // This flood fill is wrapped in logic for pruning unused methods.
   299  // All methods are mentioned by relocations on their receiver's *rtype.
   300  // These relocations are specially defined as R_METHODOFF by the compiler
   301  // so we can detect and manipulated them here.
   302  //
   303  // There are three ways a method of a reachable type can be invoked:
   304  //
   305  //	1. direct call
   306  //	2. through a reachable interface type
   307  //	3. reflect.Value.Method (or MethodByName), or reflect.Type.Method
   308  //	   (or MethodByName)
   309  //
   310  // The first case is handled by the flood fill, a directly called method
   311  // is marked as reachable.
   312  //
   313  // The second case is handled by decomposing all reachable interface
   314  // types into method signatures. Each encountered method is compared
   315  // against the interface method signatures, if it matches it is marked
   316  // as reachable. This is extremely conservative, but easy and correct.
   317  //
   318  // The third case is handled by looking to see if any of:
   319  //	- reflect.Value.Method or MethodByName is reachable
   320  // 	- reflect.Type.Method or MethodByName is called (through the
   321  // 	  REFLECTMETHOD attribute marked by the compiler).
   322  // If any of these happen, all bets are off and all exported methods
   323  // of reachable types are marked reachable.
   324  //
   325  // Any unreached text symbols are removed from ctxt.Textp.
   326  func deadcode(ctxt *Link) {
   327  	ldr := ctxt.loader
   328  	d := deadcodePass{ctxt: ctxt, ldr: ldr}
   329  	d.init()
   330  	d.flood()
   331  
   332  	methSym := ldr.Lookup("reflect.Value.Method", sym.SymVerABIInternal)
   333  	methByNameSym := ldr.Lookup("reflect.Value.MethodByName", sym.SymVerABIInternal)
   334  
   335  	if ctxt.DynlinkingGo() {
   336  		// Exported methods may satisfy interfaces we don't know
   337  		// about yet when dynamically linking.
   338  		d.reflectSeen = true
   339  	}
   340  
   341  	for {
   342  		// Methods might be called via reflection. Give up on
   343  		// static analysis, mark all exported methods of
   344  		// all reachable types as reachable.
   345  		d.reflectSeen = d.reflectSeen || (methSym != 0 && ldr.AttrReachable(methSym)) || (methByNameSym != 0 && ldr.AttrReachable(methByNameSym))
   346  
   347  		// Mark all methods that could satisfy a discovered
   348  		// interface as reachable. We recheck old marked interfaces
   349  		// as new types (with new methods) may have been discovered
   350  		// in the last pass.
   351  		rem := d.markableMethods[:0]
   352  		for _, m := range d.markableMethods {
   353  			if (d.reflectSeen && m.isExported()) || d.ifaceMethod[m.m] {
   354  				d.markMethod(m)
   355  			} else {
   356  				rem = append(rem, m)
   357  			}
   358  		}
   359  		d.markableMethods = rem
   360  
   361  		if d.wq.empty() {
   362  			// No new work was discovered. Done.
   363  			break
   364  		}
   365  		d.flood()
   366  	}
   367  }
   368  
   369  // methodsig is a typed method signature (name + type).
   370  type methodsig struct {
   371  	name string
   372  	typ  loader.Sym // type descriptor symbol of the function
   373  }
   374  
   375  // methodref holds the relocations from a receiver type symbol to its
   376  // method. There are three relocations, one for each of the fields in
   377  // the reflect.method struct: mtyp, ifn, and tfn.
   378  type methodref struct {
   379  	m   methodsig
   380  	src loader.Sym // receiver type symbol
   381  	r   int        // the index of R_METHODOFF relocations
   382  }
   383  
   384  func (m methodref) isExported() bool {
   385  	for _, r := range m.m.name {
   386  		return unicode.IsUpper(r)
   387  	}
   388  	panic("methodref has no signature")
   389  }
   390  
   391  // decodeMethodSig decodes an array of method signature information.
   392  // Each element of the array is size bytes. The first 4 bytes is a
   393  // nameOff for the method name, and the next 4 bytes is a typeOff for
   394  // the function type.
   395  //
   396  // Conveniently this is the layout of both runtime.method and runtime.imethod.
   397  func (d *deadcodePass) decodeMethodSig(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, relocs *loader.Relocs, off, size, count int) []methodsig {
   398  	if cap(d.methodsigstmp) < count {
   399  		d.methodsigstmp = append(d.methodsigstmp[:0], make([]methodsig, count)...)
   400  	}
   401  	var methods = d.methodsigstmp[:count]
   402  	for i := 0; i < count; i++ {
   403  		methods[i].name = decodetypeName(ldr, symIdx, relocs, off)
   404  		methods[i].typ = decodeRelocSym(ldr, symIdx, relocs, int32(off+4))
   405  		off += size
   406  	}
   407  	return methods
   408  }
   409  
   410  // Decode the method of interface type symbol symIdx at offset off.
   411  func (d *deadcodePass) decodeIfaceMethod(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, off int64) methodsig {
   412  	p := ldr.Data(symIdx)
   413  	if decodetypeKind(arch, p)&kindMask != kindInterface {
   414  		panic(fmt.Sprintf("symbol %q is not an interface", ldr.SymName(symIdx)))
   415  	}
   416  	relocs := ldr.Relocs(symIdx)
   417  	var m methodsig
   418  	m.name = decodetypeName(ldr, symIdx, &relocs, int(off))
   419  	m.typ = decodeRelocSym(ldr, symIdx, &relocs, int32(off+4))
   420  	return m
   421  }
   422  
   423  func (d *deadcodePass) decodetypeMethods(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, relocs *loader.Relocs) []methodsig {
   424  	p := ldr.Data(symIdx)
   425  	if !decodetypeHasUncommon(arch, p) {
   426  		panic(fmt.Sprintf("no methods on %q", ldr.SymName(symIdx)))
   427  	}
   428  	off := commonsize(arch) // reflect.rtype
   429  	switch decodetypeKind(arch, p) & kindMask {
   430  	case kindStruct: // reflect.structType
   431  		off += 4 * arch.PtrSize
   432  	case kindPtr: // reflect.ptrType
   433  		off += arch.PtrSize
   434  	case kindFunc: // reflect.funcType
   435  		off += arch.PtrSize // 4 bytes, pointer aligned
   436  	case kindSlice: // reflect.sliceType
   437  		off += arch.PtrSize
   438  	case kindArray: // reflect.arrayType
   439  		off += 3 * arch.PtrSize
   440  	case kindChan: // reflect.chanType
   441  		off += 2 * arch.PtrSize
   442  	case kindMap: // reflect.mapType
   443  		off += 4*arch.PtrSize + 8
   444  	case kindInterface: // reflect.interfaceType
   445  		off += 3 * arch.PtrSize
   446  	default:
   447  		// just Sizeof(rtype)
   448  	}
   449  
   450  	mcount := int(decodeInuxi(arch, p[off+4:], 2))
   451  	moff := int(decodeInuxi(arch, p[off+4+2+2:], 4))
   452  	off += moff                // offset to array of reflect.method values
   453  	const sizeofMethod = 4 * 4 // sizeof reflect.method in program
   454  	return d.decodeMethodSig(ldr, arch, symIdx, relocs, off, sizeofMethod, mcount)
   455  }
   456  

View as plain text