github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/link/internal/ld/deadcode2.go (about) 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 "bytes" 9 "github.com/gagliardetto/golang-go/cmd/internal/dwarf" 10 "github.com/gagliardetto/golang-go/cmd/internal/objabi" 11 "github.com/gagliardetto/golang-go/cmd/internal/sys" 12 "github.com/gagliardetto/golang-go/cmd/link/internal/loader" 13 "github.com/gagliardetto/golang-go/cmd/link/internal/sym" 14 "container/heap" 15 "fmt" 16 "unicode" 17 ) 18 19 var _ = fmt.Print 20 21 type workQueue []loader.Sym 22 23 // Implement container/heap.Interface. 24 func (q *workQueue) Len() int { return len(*q) } 25 func (q *workQueue) Less(i, j int) bool { return (*q)[i] < (*q)[j] } 26 func (q *workQueue) Swap(i, j int) { (*q)[i], (*q)[j] = (*q)[j], (*q)[i] } 27 func (q *workQueue) Push(i interface{}) { *q = append(*q, i.(loader.Sym)) } 28 func (q *workQueue) Pop() interface{} { i := (*q)[len(*q)-1]; *q = (*q)[:len(*q)-1]; return i } 29 30 // Functions for deadcode pass to use. 31 // Deadcode pass should call push/pop, not Push/Pop. 32 func (q *workQueue) push(i loader.Sym) { heap.Push(q, i) } 33 func (q *workQueue) pop() loader.Sym { return heap.Pop(q).(loader.Sym) } 34 func (q *workQueue) empty() bool { return len(*q) == 0 } 35 36 type deadcodePass2 struct { 37 ctxt *Link 38 ldr *loader.Loader 39 wq workQueue 40 rtmp []loader.Reloc 41 42 ifaceMethod map[methodsig]bool // methods declared in reached interfaces 43 markableMethods []methodref2 // methods of reached types 44 reflectSeen bool // whether we have seen a reflect method call 45 } 46 47 func (d *deadcodePass2) init() { 48 d.ldr.InitReachable() 49 d.ifaceMethod = make(map[methodsig]bool) 50 if d.ctxt.Reachparent != nil { 51 d.ldr.Reachparent = make([]loader.Sym, d.ldr.NSym()) 52 } 53 heap.Init(&d.wq) 54 55 if d.ctxt.BuildMode == BuildModeShared { 56 // Mark all symbols defined in this library as reachable when 57 // building a shared library. 58 n := d.ldr.NDef() 59 for i := 1; i < n; i++ { 60 s := loader.Sym(i) 61 if !d.ldr.IsDup(s) { 62 d.mark(s, 0) 63 } 64 } 65 return 66 } 67 68 var names []string 69 70 // In a normal binary, start at main.main and the init 71 // functions and mark what is reachable from there. 72 if d.ctxt.linkShared && (d.ctxt.BuildMode == BuildModeExe || d.ctxt.BuildMode == BuildModePIE) { 73 names = append(names, "main.main", "main..inittask") 74 } else { 75 // The external linker refers main symbol directly. 76 if d.ctxt.LinkMode == LinkExternal && (d.ctxt.BuildMode == BuildModeExe || d.ctxt.BuildMode == BuildModePIE) { 77 if d.ctxt.HeadType == objabi.Hwindows && d.ctxt.Arch.Family == sys.I386 { 78 *flagEntrySymbol = "_main" 79 } else { 80 *flagEntrySymbol = "main" 81 } 82 } 83 names = append(names, *flagEntrySymbol) 84 if d.ctxt.BuildMode == BuildModePlugin { 85 names = append(names, objabi.PathToPrefix(*flagPluginPath)+"..inittask", objabi.PathToPrefix(*flagPluginPath)+".main", "go.plugin.tabs") 86 87 // We don't keep the go.plugin.exports symbol, 88 // but we do keep the symbols it refers to. 89 exportsIdx := d.ldr.Lookup("go.plugin.exports", 0) 90 if exportsIdx != 0 { 91 d.ReadRelocs(exportsIdx) 92 for i := 0; i < len(d.rtmp); i++ { 93 d.mark(d.rtmp[i].Sym, 0) 94 } 95 } 96 } 97 } 98 99 dynexpMap := d.ctxt.cgo_export_dynamic 100 if d.ctxt.LinkMode == LinkExternal { 101 dynexpMap = d.ctxt.cgo_export_static 102 } 103 for exp := range dynexpMap { 104 names = append(names, exp) 105 } 106 107 // DWARF constant DIE symbols are not referenced, but needed by 108 // the dwarf pass. 109 if !*FlagW { 110 for _, lib := range d.ctxt.Library { 111 names = append(names, dwarf.ConstInfoPrefix+lib.Pkg) 112 } 113 } 114 115 for _, name := range names { 116 // Mark symbol as a data/ABI0 symbol. 117 d.mark(d.ldr.Lookup(name, 0), 0) 118 // Also mark any Go functions (internal ABI). 119 d.mark(d.ldr.Lookup(name, sym.SymVerABIInternal), 0) 120 } 121 } 122 123 func (d *deadcodePass2) flood() { 124 symRelocs := []loader.Reloc{} 125 auxSyms := []loader.Sym{} 126 for !d.wq.empty() { 127 symIdx := d.wq.pop() 128 129 d.reflectSeen = d.reflectSeen || d.ldr.IsReflectMethod(symIdx) 130 131 relocs := d.ldr.Relocs(symIdx) 132 symRelocs = relocs.ReadAll(symRelocs) 133 134 if d.ldr.IsGoType(symIdx) { 135 p := d.ldr.Data(symIdx) 136 if len(p) != 0 && decodetypeKind(d.ctxt.Arch, p)&kindMask == kindInterface { 137 for _, sig := range d.decodeIfaceMethods2(d.ldr, d.ctxt.Arch, symIdx, symRelocs) { 138 if d.ctxt.Debugvlog > 1 { 139 d.ctxt.Logf("reached iface method: %s\n", sig) 140 } 141 d.ifaceMethod[sig] = true 142 } 143 } 144 } 145 146 var methods []methodref2 147 for i := 0; i < relocs.Count; i++ { 148 r := symRelocs[i] 149 if r.Type == objabi.R_WEAKADDROFF { 150 continue 151 } 152 if r.Type == objabi.R_METHODOFF { 153 if i+2 >= relocs.Count { 154 panic("expect three consecutive R_METHODOFF relocs") 155 } 156 methods = append(methods, methodref2{src: symIdx, r: i}) 157 i += 2 158 continue 159 } 160 if r.Type == objabi.R_USETYPE { 161 // type symbol used for DWARF. we need to load the symbol but it may not 162 // be otherwise reachable in the program. 163 // do nothing for now as we still load all type symbols. 164 continue 165 } 166 d.mark(r.Sym, symIdx) 167 } 168 auxSyms = d.ldr.ReadAuxSyms(symIdx, auxSyms) 169 for i := 0; i < len(auxSyms); i++ { 170 d.mark(auxSyms[i], symIdx) 171 } 172 // Some host object symbols have an outer object, which acts like a 173 // "carrier" symbol, or it holds all the symbols for a particular 174 // section. We need to mark all "referenced" symbols from that carrier, 175 // so we make sure we're pulling in all outer symbols, and their sub 176 // symbols. This is not ideal, and these carrier/section symbols could 177 // be removed. 178 d.mark(d.ldr.OuterSym(symIdx), symIdx) 179 d.mark(d.ldr.SubSym(symIdx), symIdx) 180 181 if len(methods) != 0 { 182 // Decode runtime type information for type methods 183 // to help work out which methods can be called 184 // dynamically via interfaces. 185 methodsigs := d.decodetypeMethods2(d.ldr, d.ctxt.Arch, symIdx, symRelocs) 186 if len(methods) != len(methodsigs) { 187 panic(fmt.Sprintf("%q has %d method relocations for %d methods", d.ldr.SymName(symIdx), len(methods), len(methodsigs))) 188 } 189 for i, m := range methodsigs { 190 methods[i].m = m 191 } 192 d.markableMethods = append(d.markableMethods, methods...) 193 } 194 } 195 } 196 197 func (d *deadcodePass2) mark(symIdx, parent loader.Sym) { 198 if symIdx != 0 && !d.ldr.Reachable.Has(symIdx) { 199 d.wq.push(symIdx) 200 d.ldr.Reachable.Set(symIdx) 201 if d.ctxt.Reachparent != nil { 202 d.ldr.Reachparent[symIdx] = parent 203 } 204 if *flagDumpDep { 205 to := d.ldr.SymName(symIdx) 206 if to != "" { 207 from := "_" 208 if parent != 0 { 209 from = d.ldr.SymName(parent) 210 } 211 fmt.Printf("%s -> %s\n", from, to) 212 } 213 } 214 } 215 } 216 217 func (d *deadcodePass2) markMethod(m methodref2) { 218 d.ReadRelocs(m.src) 219 d.mark(d.rtmp[m.r].Sym, m.src) 220 d.mark(d.rtmp[m.r+1].Sym, m.src) 221 d.mark(d.rtmp[m.r+2].Sym, m.src) 222 } 223 224 func deadcode2(ctxt *Link) { 225 ldr := ctxt.loader 226 d := deadcodePass2{ctxt: ctxt, ldr: ldr} 227 d.init() 228 d.flood() 229 230 callSym := ldr.Lookup("reflect.Value.Call", sym.SymVerABIInternal) 231 methSym := ldr.Lookup("reflect.Value.Method", sym.SymVerABIInternal) 232 if ctxt.DynlinkingGo() { 233 // Exported methods may satisfy interfaces we don't know 234 // about yet when dynamically linking. 235 d.reflectSeen = true 236 } 237 238 for { 239 // Methods might be called via reflection. Give up on 240 // static analysis, mark all exported methods of 241 // all reachable types as reachable. 242 d.reflectSeen = d.reflectSeen || (callSym != 0 && ldr.Reachable.Has(callSym)) || (methSym != 0 && ldr.Reachable.Has(methSym)) 243 244 // Mark all methods that could satisfy a discovered 245 // interface as reachable. We recheck old marked interfaces 246 // as new types (with new methods) may have been discovered 247 // in the last pass. 248 rem := d.markableMethods[:0] 249 for _, m := range d.markableMethods { 250 if (d.reflectSeen && m.isExported()) || d.ifaceMethod[m.m] { 251 d.markMethod(m) 252 } else { 253 rem = append(rem, m) 254 } 255 } 256 d.markableMethods = rem 257 258 if d.wq.empty() { 259 // No new work was discovered. Done. 260 break 261 } 262 d.flood() 263 } 264 265 n := ldr.NSym() 266 267 if ctxt.BuildMode != BuildModeShared { 268 // Keep a itablink if the symbol it points at is being kept. 269 // (When BuildModeShared, always keep itablinks.) 270 for i := 1; i < n; i++ { 271 s := loader.Sym(i) 272 if ldr.IsItabLink(s) { 273 relocs := ldr.Relocs(s) 274 if relocs.Count > 0 && ldr.Reachable.Has(relocs.At(0).Sym) { 275 ldr.Reachable.Set(s) 276 } 277 } 278 } 279 } 280 } 281 282 // methodref2 holds the relocations from a receiver type symbol to its 283 // method. There are three relocations, one for each of the fields in 284 // the reflect.method struct: mtyp, ifn, and tfn. 285 type methodref2 struct { 286 m methodsig 287 src loader.Sym // receiver type symbol 288 r int // the index of R_METHODOFF relocations 289 } 290 291 func (m methodref2) isExported() bool { 292 for _, r := range m.m { 293 return unicode.IsUpper(r) 294 } 295 panic("methodref has no signature") 296 } 297 298 // decodeMethodSig2 decodes an array of method signature information. 299 // Each element of the array is size bytes. The first 4 bytes is a 300 // nameOff for the method name, and the next 4 bytes is a typeOff for 301 // the function type. 302 // 303 // Conveniently this is the layout of both runtime.method and runtime.imethod. 304 func (d *deadcodePass2) decodeMethodSig2(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, symRelocs []loader.Reloc, off, size, count int) []methodsig { 305 var buf bytes.Buffer 306 var methods []methodsig 307 for i := 0; i < count; i++ { 308 buf.WriteString(decodetypeName2(ldr, symIdx, symRelocs, off)) 309 mtypSym := decodeRelocSym2(ldr, symIdx, symRelocs, int32(off+4)) 310 // FIXME: add some sort of caching here, since we may see some of the 311 // same symbols over time for param types. 312 d.ReadRelocs(mtypSym) 313 mp := ldr.Data(mtypSym) 314 315 buf.WriteRune('(') 316 inCount := decodetypeFuncInCount(arch, mp) 317 for i := 0; i < inCount; i++ { 318 if i > 0 { 319 buf.WriteString(", ") 320 } 321 a := d.decodetypeFuncInType2(ldr, arch, mtypSym, d.rtmp, i) 322 buf.WriteString(ldr.SymName(a)) 323 } 324 buf.WriteString(") (") 325 outCount := decodetypeFuncOutCount(arch, mp) 326 for i := 0; i < outCount; i++ { 327 if i > 0 { 328 buf.WriteString(", ") 329 } 330 a := d.decodetypeFuncOutType2(ldr, arch, mtypSym, d.rtmp, i) 331 buf.WriteString(ldr.SymName(a)) 332 } 333 buf.WriteRune(')') 334 335 off += size 336 methods = append(methods, methodsig(buf.String())) 337 buf.Reset() 338 } 339 return methods 340 } 341 342 func (d *deadcodePass2) decodeIfaceMethods2(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, symRelocs []loader.Reloc) []methodsig { 343 p := ldr.Data(symIdx) 344 if decodetypeKind(arch, p)&kindMask != kindInterface { 345 panic(fmt.Sprintf("symbol %q is not an interface", ldr.SymName(symIdx))) 346 } 347 rel := decodeReloc2(ldr, symIdx, symRelocs, int32(commonsize(arch)+arch.PtrSize)) 348 if rel.Sym == 0 { 349 return nil 350 } 351 if rel.Sym != symIdx { 352 panic(fmt.Sprintf("imethod slice pointer in %q leads to a different symbol", ldr.SymName(symIdx))) 353 } 354 off := int(rel.Add) // array of reflect.imethod values 355 numMethods := int(decodetypeIfaceMethodCount(arch, p)) 356 sizeofIMethod := 4 + 4 357 return d.decodeMethodSig2(ldr, arch, symIdx, symRelocs, off, sizeofIMethod, numMethods) 358 } 359 360 func (d *deadcodePass2) decodetypeMethods2(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, symRelocs []loader.Reloc) []methodsig { 361 p := ldr.Data(symIdx) 362 if !decodetypeHasUncommon(arch, p) { 363 panic(fmt.Sprintf("no methods on %q", ldr.SymName(symIdx))) 364 } 365 off := commonsize(arch) // reflect.rtype 366 switch decodetypeKind(arch, p) & kindMask { 367 case kindStruct: // reflect.structType 368 off += 4 * arch.PtrSize 369 case kindPtr: // reflect.ptrType 370 off += arch.PtrSize 371 case kindFunc: // reflect.funcType 372 off += arch.PtrSize // 4 bytes, pointer aligned 373 case kindSlice: // reflect.sliceType 374 off += arch.PtrSize 375 case kindArray: // reflect.arrayType 376 off += 3 * arch.PtrSize 377 case kindChan: // reflect.chanType 378 off += 2 * arch.PtrSize 379 case kindMap: // reflect.mapType 380 off += 4*arch.PtrSize + 8 381 case kindInterface: // reflect.interfaceType 382 off += 3 * arch.PtrSize 383 default: 384 // just Sizeof(rtype) 385 } 386 387 mcount := int(decodeInuxi(arch, p[off+4:], 2)) 388 moff := int(decodeInuxi(arch, p[off+4+2+2:], 4)) 389 off += moff // offset to array of reflect.method values 390 const sizeofMethod = 4 * 4 // sizeof reflect.method in program 391 return d.decodeMethodSig2(ldr, arch, symIdx, symRelocs, off, sizeofMethod, mcount) 392 } 393 394 func decodeReloc2(ldr *loader.Loader, symIdx loader.Sym, symRelocs []loader.Reloc, off int32) loader.Reloc { 395 for j := 0; j < len(symRelocs); j++ { 396 rel := symRelocs[j] 397 if rel.Off == off { 398 return rel 399 } 400 } 401 return loader.Reloc{} 402 } 403 404 func decodeRelocSym2(ldr *loader.Loader, symIdx loader.Sym, symRelocs []loader.Reloc, off int32) loader.Sym { 405 return decodeReloc2(ldr, symIdx, symRelocs, off).Sym 406 } 407 408 // decodetypeName2 decodes the name from a reflect.name. 409 func decodetypeName2(ldr *loader.Loader, symIdx loader.Sym, symRelocs []loader.Reloc, off int) string { 410 r := decodeRelocSym2(ldr, symIdx, symRelocs, int32(off)) 411 if r == 0 { 412 return "" 413 } 414 415 data := ldr.Data(r) 416 namelen := int(uint16(data[1])<<8 | uint16(data[2])) 417 return string(data[3 : 3+namelen]) 418 } 419 420 func (d *deadcodePass2) decodetypeFuncInType2(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, symRelocs []loader.Reloc, i int) loader.Sym { 421 uadd := commonsize(arch) + 4 422 if arch.PtrSize == 8 { 423 uadd += 4 424 } 425 if decodetypeHasUncommon(arch, ldr.Data(symIdx)) { 426 uadd += uncommonSize() 427 } 428 return decodeRelocSym2(ldr, symIdx, symRelocs, int32(uadd+i*arch.PtrSize)) 429 } 430 431 func (d *deadcodePass2) decodetypeFuncOutType2(ldr *loader.Loader, arch *sys.Arch, symIdx loader.Sym, symRelocs []loader.Reloc, i int) loader.Sym { 432 return d.decodetypeFuncInType2(ldr, arch, symIdx, symRelocs, i+decodetypeFuncInCount(arch, ldr.Data(symIdx))) 433 } 434 435 // readRelocs reads the relocations for the specified symbol into the 436 // deadcode relocs work array. Use with care, since the work array 437 // is a singleton. 438 func (d *deadcodePass2) ReadRelocs(symIdx loader.Sym) { 439 relocs := d.ldr.Relocs(symIdx) 440 d.rtmp = relocs.ReadAll(d.rtmp) 441 }