1 // Copyright 2013 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 obj 6 7 import ( 8 "fmt" 9 "log" 10 ) 11 12 func addvarint(ctxt *Link, d *Pcdata, val uint32) { 13 var v uint32 14 for v = val; v >= 0x80; v >>= 7 { 15 d.P = append(d.P, uint8(v|0x80)) 16 } 17 d.P = append(d.P, uint8(v)) 18 } 19 20 // funcpctab writes to dst a pc-value table mapping the code in func to the values 21 // returned by valfunc parameterized by arg. The invocation of valfunc to update the 22 // current value is, for each p, 23 // 24 // val = valfunc(func, val, p, 0, arg); 25 // record val as value at p->pc; 26 // val = valfunc(func, val, p, 1, arg); 27 // 28 // where func is the function, val is the current value, p is the instruction being 29 // considered, and arg can be used to further parameterize valfunc. 30 func funcpctab(ctxt *Link, dst *Pcdata, func_ *LSym, desc string, valfunc func(*Link, *LSym, int32, *Prog, int32, interface{}) int32, arg interface{}) { 31 // To debug a specific function, uncomment second line and change name. 32 dbg := 0 33 34 //dbg = strcmp(func->name, "main.main") == 0; 35 //dbg = strcmp(desc, "pctofile") == 0; 36 37 ctxt.Debugpcln += int32(dbg) 38 39 dst.P = dst.P[:0] 40 41 if ctxt.Debugpcln != 0 { 42 fmt.Fprintf(ctxt.Bso, "funcpctab %s [valfunc=%s]\n", func_.Name, desc) 43 } 44 45 val := int32(-1) 46 oldval := val 47 if func_.Text == nil { 48 ctxt.Debugpcln -= int32(dbg) 49 return 50 } 51 52 pc := func_.Text.Pc 53 54 if ctxt.Debugpcln != 0 { 55 fmt.Fprintf(ctxt.Bso, "%6x %6d %v\n", uint64(pc), val, func_.Text) 56 } 57 58 started := int32(0) 59 var delta uint32 60 for p := func_.Text; p != nil; p = p.Link { 61 // Update val. If it's not changing, keep going. 62 val = valfunc(ctxt, func_, val, p, 0, arg) 63 64 if val == oldval && started != 0 { 65 val = valfunc(ctxt, func_, val, p, 1, arg) 66 if ctxt.Debugpcln != 0 { 67 fmt.Fprintf(ctxt.Bso, "%6x %6s %v\n", uint64(int64(p.Pc)), "", p) 68 } 69 continue 70 } 71 72 // If the pc of the next instruction is the same as the 73 // pc of this instruction, this instruction is not a real 74 // instruction. Keep going, so that we only emit a delta 75 // for a true instruction boundary in the program. 76 if p.Link != nil && p.Link.Pc == p.Pc { 77 val = valfunc(ctxt, func_, val, p, 1, arg) 78 if ctxt.Debugpcln != 0 { 79 fmt.Fprintf(ctxt.Bso, "%6x %6s %v\n", uint64(int64(p.Pc)), "", p) 80 } 81 continue 82 } 83 84 // The table is a sequence of (value, pc) pairs, where each 85 // pair states that the given value is in effect from the current position 86 // up to the given pc, which becomes the new current position. 87 // To generate the table as we scan over the program instructions, 88 // we emit a "(value" when pc == func->value, and then 89 // each time we observe a change in value we emit ", pc) (value". 90 // When the scan is over, we emit the closing ", pc)". 91 // 92 // The table is delta-encoded. The value deltas are signed and 93 // transmitted in zig-zag form, where a complement bit is placed in bit 0, 94 // and the pc deltas are unsigned. Both kinds of deltas are sent 95 // as variable-length little-endian base-128 integers, 96 // where the 0x80 bit indicates that the integer continues. 97 98 if ctxt.Debugpcln != 0 { 99 fmt.Fprintf(ctxt.Bso, "%6x %6d %v\n", uint64(int64(p.Pc)), val, p) 100 } 101 102 if started != 0 { 103 addvarint(ctxt, dst, uint32((p.Pc-pc)/int64(ctxt.Arch.Minlc))) 104 pc = p.Pc 105 } 106 107 delta = uint32(val) - uint32(oldval) 108 if delta>>31 != 0 { 109 delta = 1 | ^(delta << 1) 110 } else { 111 delta <<= 1 112 } 113 addvarint(ctxt, dst, delta) 114 oldval = val 115 started = 1 116 val = valfunc(ctxt, func_, val, p, 1, arg) 117 } 118 119 if started != 0 { 120 if ctxt.Debugpcln != 0 { 121 fmt.Fprintf(ctxt.Bso, "%6x done\n", uint64(int64(func_.Text.Pc)+func_.Size)) 122 } 123 addvarint(ctxt, dst, uint32((func_.Value+func_.Size-pc)/int64(ctxt.Arch.Minlc))) 124 addvarint(ctxt, dst, 0) // terminator 125 } 126 127 if ctxt.Debugpcln != 0 { 128 fmt.Fprintf(ctxt.Bso, "wrote %d bytes to %p\n", len(dst.P), dst) 129 for i := 0; i < len(dst.P); i++ { 130 fmt.Fprintf(ctxt.Bso, " %02x", dst.P[i]) 131 } 132 fmt.Fprintf(ctxt.Bso, "\n") 133 } 134 135 ctxt.Debugpcln -= int32(dbg) 136 } 137 138 // pctofileline computes either the file number (arg == 0) 139 // or the line number (arg == 1) to use at p. 140 // Because p->lineno applies to p, phase == 0 (before p) 141 // takes care of the update. 142 func pctofileline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 { 143 if p.As == ATEXT || p.As == ANOP || p.As == AUSEFIELD || p.Lineno == 0 || phase == 1 { 144 return oldval 145 } 146 var l int32 147 var f *LSym 148 linkgetline(ctxt, p.Lineno, &f, &l) 149 if f == nil { 150 // print("getline failed for %s %v\n", ctxt->cursym->name, p); 151 return oldval 152 } 153 154 if arg == nil { 155 return l 156 } 157 pcln := arg.(*Pcln) 158 159 if f == pcln.Lastfile { 160 return int32(pcln.Lastindex) 161 } 162 163 var i int32 164 for i = 0; i < int32(len(pcln.File)); i++ { 165 file := pcln.File[i] 166 if file == f { 167 pcln.Lastfile = f 168 pcln.Lastindex = int(i) 169 return int32(i) 170 } 171 } 172 pcln.File = append(pcln.File, f) 173 pcln.Lastfile = f 174 pcln.Lastindex = int(i) 175 return i 176 } 177 178 // pctospadj computes the sp adjustment in effect. 179 // It is oldval plus any adjustment made by p itself. 180 // The adjustment by p takes effect only after p, so we 181 // apply the change during phase == 1. 182 func pctospadj(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 { 183 if oldval == -1 { // starting 184 oldval = 0 185 } 186 if phase == 0 { 187 return oldval 188 } 189 if oldval+p.Spadj < -10000 || oldval+p.Spadj > 1100000000 { 190 ctxt.Diag("overflow in spadj: %d + %d = %d", oldval, p.Spadj, oldval+p.Spadj) 191 log.Fatalf("bad code") 192 } 193 194 return oldval + p.Spadj 195 } 196 197 // pctopcdata computes the pcdata value in effect at p. 198 // A PCDATA instruction sets the value in effect at future 199 // non-PCDATA instructions. 200 // Since PCDATA instructions have no width in the final code, 201 // it does not matter which phase we use for the update. 202 func pctopcdata(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 { 203 if phase == 0 || p.As != APCDATA || p.From.Offset != int64(arg.(uint32)) { 204 return oldval 205 } 206 if int64(int32(p.To.Offset)) != p.To.Offset { 207 ctxt.Diag("overflow in PCDATA instruction: %v", p) 208 log.Fatalf("bad code") 209 } 210 211 return int32(p.To.Offset) 212 } 213 214 func linkpcln(ctxt *Link, cursym *LSym) { 215 ctxt.Cursym = cursym 216 217 pcln := new(Pcln) 218 cursym.Pcln = pcln 219 220 npcdata := 0 221 nfuncdata := 0 222 for p := cursym.Text; p != nil; p = p.Link { 223 if p.As == APCDATA && p.From.Offset >= int64(npcdata) { 224 npcdata = int(p.From.Offset + 1) 225 } 226 if p.As == AFUNCDATA && p.From.Offset >= int64(nfuncdata) { 227 nfuncdata = int(p.From.Offset + 1) 228 } 229 } 230 231 pcln.Pcdata = make([]Pcdata, npcdata) 232 pcln.Pcdata = pcln.Pcdata[:npcdata] 233 pcln.Funcdata = make([]*LSym, nfuncdata) 234 pcln.Funcdataoff = make([]int64, nfuncdata) 235 pcln.Funcdataoff = pcln.Funcdataoff[:nfuncdata] 236 237 funcpctab(ctxt, &pcln.Pcsp, cursym, "pctospadj", pctospadj, nil) 238 funcpctab(ctxt, &pcln.Pcfile, cursym, "pctofile", pctofileline, pcln) 239 funcpctab(ctxt, &pcln.Pcline, cursym, "pctoline", pctofileline, nil) 240 241 // tabulate which pc and func data we have. 242 havepc := make([]uint32, (npcdata+31)/32) 243 havefunc := make([]uint32, (nfuncdata+31)/32) 244 for p := cursym.Text; p != nil; p = p.Link { 245 if p.As == AFUNCDATA { 246 if (havefunc[p.From.Offset/32]>>uint64(p.From.Offset%32))&1 != 0 { 247 ctxt.Diag("multiple definitions for FUNCDATA $%d", p.From.Offset) 248 } 249 havefunc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32) 250 } 251 252 if p.As == APCDATA { 253 havepc[p.From.Offset/32] |= 1 << uint64(p.From.Offset%32) 254 } 255 } 256 257 // pcdata. 258 for i := 0; i < npcdata; i++ { 259 if (havepc[i/32]>>uint(i%32))&1 == 0 { 260 continue 261 } 262 funcpctab(ctxt, &pcln.Pcdata[i], cursym, "pctopcdata", pctopcdata, interface{}(uint32(i))) 263 } 264 265 // funcdata 266 if nfuncdata > 0 { 267 var i int 268 for p := cursym.Text; p != nil; p = p.Link { 269 if p.As == AFUNCDATA { 270 i = int(p.From.Offset) 271 pcln.Funcdataoff[i] = p.To.Offset 272 if p.To.Type != TYPE_CONST { 273 // TODO: Dedup. 274 //funcdata_bytes += p->to.sym->size; 275 pcln.Funcdata[i] = p.To.Sym 276 } 277 } 278 } 279 } 280 } 281 282 // iteration over encoded pcdata tables. 283 284 func getvarint(pp *[]byte) uint32 { 285 v := uint32(0) 286 p := *pp 287 for shift := 0; ; shift += 7 { 288 v |= uint32(p[0]&0x7F) << uint(shift) 289 tmp7 := p 290 p = p[1:] 291 if tmp7[0]&0x80 == 0 { 292 break 293 } 294 } 295 296 *pp = p 297 return v 298 } 299 300 func pciternext(it *Pciter) { 301 it.pc = it.nextpc 302 if it.done != 0 { 303 return 304 } 305 if -cap(it.p) >= -cap(it.d.P[len(it.d.P):]) { 306 it.done = 1 307 return 308 } 309 310 // value delta 311 v := getvarint(&it.p) 312 313 if v == 0 && it.start == 0 { 314 it.done = 1 315 return 316 } 317 318 it.start = 0 319 dv := int32(v>>1) ^ (int32(v<<31) >> 31) 320 it.value += dv 321 322 // pc delta 323 v = getvarint(&it.p) 324 325 it.nextpc = it.pc + v*it.pcscale 326 } 327 328 func pciterinit(ctxt *Link, it *Pciter, d *Pcdata) { 329 it.d = *d 330 it.p = it.d.P 331 it.pc = 0 332 it.nextpc = 0 333 it.value = -1 334 it.start = 1 335 it.done = 0 336 it.pcscale = uint32(ctxt.Arch.Minlc) 337 pciternext(it) 338 } 339