github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/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 "cmd/internal/obj" 9 "cmd/internal/sys" 10 "fmt" 11 "strings" 12 "unicode" 13 ) 14 15 // deadcode marks all reachable symbols. 16 // 17 // The basis of the dead code elimination is a flood fill of symbols, 18 // following their relocations, beginning at INITENTRY. 19 // 20 // This flood fill is wrapped in logic for pruning unused methods. 21 // All methods are mentioned by relocations on their receiver's *rtype. 22 // These relocations are specially defined as R_METHODOFF by the compiler 23 // so we can detect and manipulated them here. 24 // 25 // There are three ways a method of a reachable type can be invoked: 26 // 27 // 1. direct call 28 // 2. through a reachable interface type 29 // 3. reflect.Value.Call, .Method, or reflect.Method.Func 30 // 31 // The first case is handled by the flood fill, a directly called method 32 // is marked as reachable. 33 // 34 // The second case is handled by decomposing all reachable interface 35 // types into method signatures. Each encountered method is compared 36 // against the interface method signatures, if it matches it is marked 37 // as reachable. This is extremely conservative, but easy and correct. 38 // 39 // The third case is handled by looking to see if any of: 40 // - reflect.Value.Call is reachable 41 // - reflect.Value.Method is reachable 42 // - reflect.Type.Method or MethodByName is called. 43 // If any of these happen, all bets are off and all exported methods 44 // of reachable types are marked reachable. 45 // 46 // Any unreached text symbols are removed from ctxt.Textp. 47 func deadcode(ctxt *Link) { 48 if Debug['v'] != 0 { 49 fmt.Fprintf(ctxt.Bso, "%5.2f deadcode\n", obj.Cputime()) 50 } 51 52 d := &deadcodepass{ 53 ctxt: ctxt, 54 ifaceMethod: make(map[methodsig]bool), 55 } 56 57 // First, flood fill any symbols directly reachable in the call 58 // graph from INITENTRY. Ignore all methods not directly called. 59 d.init() 60 d.flood() 61 62 callSym := Linkrlookup(ctxt, "reflect.Value.Call", 0) 63 methSym := Linkrlookup(ctxt, "reflect.Value.Method", 0) 64 reflectSeen := false 65 66 if DynlinkingGo() { 67 // Exported methods may satisfy interfaces we don't know 68 // about yet when dynamically linking. 69 reflectSeen = true 70 } 71 72 for { 73 if !reflectSeen { 74 if d.reflectMethod || (callSym != nil && callSym.Attr.Reachable()) || (methSym != nil && methSym.Attr.Reachable()) { 75 // Methods might be called via reflection. Give up on 76 // static analysis, mark all exported methods of 77 // all reachable types as reachable. 78 reflectSeen = true 79 } 80 } 81 82 // Mark all methods that could satisfy a discovered 83 // interface as reachable. We recheck old marked interfaces 84 // as new types (with new methods) may have been discovered 85 // in the last pass. 86 var rem []methodref 87 for _, m := range d.markableMethods { 88 if (reflectSeen && m.isExported()) || d.ifaceMethod[m.m] { 89 d.markMethod(m) 90 } else { 91 rem = append(rem, m) 92 } 93 } 94 d.markableMethods = rem 95 96 if len(d.markQueue) == 0 { 97 // No new work was discovered. Done. 98 break 99 } 100 d.flood() 101 } 102 103 // Remove all remaining unreached R_METHODOFF relocations. 104 for _, m := range d.markableMethods { 105 for _, r := range m.r { 106 d.cleanupReloc(r) 107 } 108 } 109 110 if Buildmode != BuildmodeShared { 111 // Keep a typelink or itablink if the symbol it points at is being kept. 112 // (When BuildmodeShared, always keep typelinks and itablinks.) 113 for _, s := range ctxt.Allsym { 114 if strings.HasPrefix(s.Name, "go.typelink.") || 115 strings.HasPrefix(s.Name, "go.itablink.") { 116 s.Attr.Set(AttrReachable, len(s.R) == 1 && s.R[0].Sym.Attr.Reachable()) 117 } 118 } 119 } 120 121 // Remove dead text but keep file information (z symbols). 122 textp := make([]*LSym, 0, len(ctxt.Textp)) 123 for _, s := range ctxt.Textp { 124 if s.Attr.Reachable() { 125 textp = append(textp, s) 126 } 127 } 128 ctxt.Textp = textp 129 } 130 131 var markextra = []string{ 132 "runtime.morestack", 133 "runtime.morestackx", 134 "runtime.morestack00", 135 "runtime.morestack10", 136 "runtime.morestack01", 137 "runtime.morestack11", 138 "runtime.morestack8", 139 "runtime.morestack16", 140 "runtime.morestack24", 141 "runtime.morestack32", 142 "runtime.morestack40", 143 "runtime.morestack48", 144 145 // on arm, lock in the div/mod helpers too 146 "_div", 147 "_divu", 148 "_mod", 149 "_modu", 150 } 151 152 // methodref holds the relocations from a receiver type symbol to its 153 // method. There are three relocations, one for each of the fields in 154 // the reflect.method struct: mtyp, ifn, and tfn. 155 type methodref struct { 156 m methodsig 157 src *LSym // receiver type symbol 158 r [3]*Reloc // R_METHODOFF relocations to fields of runtime.method 159 } 160 161 func (m methodref) ifn() *LSym { return m.r[1].Sym } 162 163 func (m methodref) isExported() bool { 164 for _, r := range m.m { 165 return unicode.IsUpper(r) 166 } 167 panic("methodref has no signature") 168 } 169 170 // deadcodepass holds state for the deadcode flood fill. 171 type deadcodepass struct { 172 ctxt *Link 173 markQueue []*LSym // symbols to flood fill in next pass 174 ifaceMethod map[methodsig]bool // methods declared in reached interfaces 175 markableMethods []methodref // methods of reached types 176 reflectMethod bool 177 } 178 179 func (d *deadcodepass) cleanupReloc(r *Reloc) { 180 if r.Sym.Attr.Reachable() { 181 r.Type = obj.R_ADDROFF 182 } else { 183 if Debug['v'] > 1 { 184 fmt.Fprintf(d.ctxt.Bso, "removing method %s\n", r.Sym.Name) 185 } 186 r.Sym = nil 187 r.Siz = 0 188 } 189 } 190 191 // mark appends a symbol to the mark queue for flood filling. 192 func (d *deadcodepass) mark(s, parent *LSym) { 193 if s == nil || s.Attr.Reachable() { 194 return 195 } 196 if s.Attr.ReflectMethod() { 197 d.reflectMethod = true 198 } 199 if flag_dumpdep { 200 p := "_" 201 if parent != nil { 202 p = parent.Name 203 } 204 fmt.Printf("%s -> %s\n", p, s.Name) 205 } 206 s.Attr |= AttrReachable 207 s.Reachparent = parent 208 d.markQueue = append(d.markQueue, s) 209 } 210 211 // markMethod marks a method as reachable. 212 func (d *deadcodepass) markMethod(m methodref) { 213 for _, r := range m.r { 214 d.mark(r.Sym, m.src) 215 r.Type = obj.R_ADDROFF 216 } 217 } 218 219 // init marks all initial symbols as reachable. 220 // In a typical binary, this is INITENTRY. 221 func (d *deadcodepass) init() { 222 var names []string 223 224 if SysArch.Family == sys.ARM { 225 // mark some functions that are only referenced after linker code editing 226 if d.ctxt.Goarm == 5 { 227 names = append(names, "_sfloat") 228 } 229 names = append(names, "runtime.read_tls_fallback") 230 } 231 232 if Buildmode == BuildmodeShared { 233 // Mark all symbols defined in this library as reachable when 234 // building a shared library. 235 for _, s := range d.ctxt.Allsym { 236 if s.Type != 0 && s.Type != obj.SDYNIMPORT { 237 d.mark(s, nil) 238 } 239 } 240 } else { 241 // In a normal binary, start at main.main and the init 242 // functions and mark what is reachable from there. 243 names = append(names, INITENTRY) 244 if Linkshared && Buildmode == BuildmodeExe { 245 names = append(names, "main.main", "main.init") 246 } 247 for _, name := range markextra { 248 names = append(names, name) 249 } 250 for _, s := range dynexp { 251 d.mark(s, nil) 252 } 253 } 254 255 for _, name := range names { 256 d.mark(Linkrlookup(d.ctxt, name, 0), nil) 257 } 258 } 259 260 // flood flood fills symbols reachable from the markQueue symbols. 261 // As it goes, it collects methodref and interface method declarations. 262 func (d *deadcodepass) flood() { 263 for len(d.markQueue) > 0 { 264 s := d.markQueue[0] 265 d.markQueue = d.markQueue[1:] 266 if s.Type == obj.STEXT { 267 if Debug['v'] > 1 { 268 fmt.Fprintf(d.ctxt.Bso, "marktext %s\n", s.Name) 269 } 270 if s.FuncInfo != nil { 271 for _, a := range s.FuncInfo.Autom { 272 d.mark(a.Gotype, s) 273 } 274 } 275 276 } 277 278 if strings.HasPrefix(s.Name, "type.") && s.Name[5] != '.' { 279 if decodetype_kind(s)&kindMask == kindInterface { 280 for _, sig := range decodetype_ifacemethods(s) { 281 if Debug['v'] > 1 { 282 fmt.Fprintf(d.ctxt.Bso, "reached iface method: %s\n", sig) 283 } 284 d.ifaceMethod[sig] = true 285 } 286 } 287 } 288 289 mpos := 0 // 0-3, the R_METHODOFF relocs of runtime.uncommontype 290 var methods []methodref 291 for i := 0; i < len(s.R); i++ { 292 r := &s.R[i] 293 if r.Sym == nil { 294 continue 295 } 296 if r.Type != obj.R_METHODOFF { 297 d.mark(r.Sym, s) 298 continue 299 } 300 // Collect rtype pointers to methods for 301 // later processing in deadcode. 302 if mpos == 0 { 303 m := methodref{src: s} 304 m.r[0] = r 305 methods = append(methods, m) 306 } else { 307 methods[len(methods)-1].r[mpos] = r 308 } 309 mpos++ 310 if mpos == len(methodref{}.r) { 311 mpos = 0 312 } 313 } 314 if len(methods) > 0 { 315 // Decode runtime type information for type methods 316 // to help work out which methods can be called 317 // dynamically via interfaces. 318 methodsigs := decodetype_methods(s) 319 if len(methods) != len(methodsigs) { 320 panic(fmt.Sprintf("%q has %d method relocations for %d methods", s.Name, len(methods), len(methodsigs))) 321 } 322 for i, m := range methodsigs { 323 name := string(m) 324 name = name[:strings.Index(name, "(")] 325 if !strings.HasSuffix(methods[i].ifn().Name, name) { 326 panic(fmt.Sprintf("%q relocation for %q does not match method %q", s.Name, methods[i].ifn().Name, name)) 327 } 328 methods[i].m = m 329 } 330 d.markableMethods = append(d.markableMethods, methods...) 331 } 332 333 if s.FuncInfo != nil { 334 for i := range s.FuncInfo.Funcdata { 335 d.mark(s.FuncInfo.Funcdata[i], s) 336 } 337 } 338 d.mark(s.Gotype, s) 339 d.mark(s.Sub, s) 340 d.mark(s.Outer, s) 341 } 342 }