Home | History | Annotate | Download | only in obj
      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 "log"
      8 
      9 func addvarint(d *Pcdata, v uint32) {
     10 	for ; v >= 0x80; v >>= 7 {
     11 		d.P = append(d.P, uint8(v|0x80))
     12 	}
     13 	d.P = append(d.P, uint8(v))
     14 }
     15 
     16 // funcpctab writes to dst a pc-value table mapping the code in func to the values
     17 // returned by valfunc parameterized by arg. The invocation of valfunc to update the
     18 // current value is, for each p,
     19 //
     20 //	val = valfunc(func, val, p, 0, arg);
     21 //	record val as value at p->pc;
     22 //	val = valfunc(func, val, p, 1, arg);
     23 //
     24 // where func is the function, val is the current value, p is the instruction being
     25 // considered, and arg can be used to further parameterize valfunc.
     26 func funcpctab(ctxt *Link, dst *Pcdata, func_ *LSym, desc string, valfunc func(*Link, *LSym, int32, *Prog, int32, interface{}) int32, arg interface{}) {
     27 	// To debug a specific function, uncomment lines and change name.
     28 	dbg := 0
     29 
     30 	//if func_.Name == "main.main" || desc == "pctospadj" {
     31 	//	dbg = 1
     32 	//}
     33 
     34 	ctxt.Debugpcln += int32(dbg)
     35 
     36 	dst.P = dst.P[:0]
     37 
     38 	if ctxt.Debugpcln != 0 {
     39 		ctxt.Logf("funcpctab %s [valfunc=%s]\n", func_.Name, desc)
     40 	}
     41 
     42 	val := int32(-1)
     43 	oldval := val
     44 	if func_.Text == nil {
     45 		ctxt.Debugpcln -= int32(dbg)
     46 		return
     47 	}
     48 
     49 	pc := func_.Text.Pc
     50 
     51 	if ctxt.Debugpcln != 0 {
     52 		ctxt.Logf("%6x %6d %v\n", uint64(pc), val, func_.Text)
     53 	}
     54 
     55 	started := int32(0)
     56 	var delta uint32
     57 	for p := func_.Text; p != nil; p = p.Link {
     58 		// Update val. If it's not changing, keep going.
     59 		val = valfunc(ctxt, func_, val, p, 0, arg)
     60 
     61 		if val == oldval && started != 0 {
     62 			val = valfunc(ctxt, func_, val, p, 1, arg)
     63 			if ctxt.Debugpcln != 0 {
     64 				ctxt.Logf("%6x %6s %v\n", uint64(p.Pc), "", p)
     65 			}
     66 			continue
     67 		}
     68 
     69 		// If the pc of the next instruction is the same as the
     70 		// pc of this instruction, this instruction is not a real
     71 		// instruction. Keep going, so that we only emit a delta
     72 		// for a true instruction boundary in the program.
     73 		if p.Link != nil && p.Link.Pc == p.Pc {
     74 			val = valfunc(ctxt, func_, val, p, 1, arg)
     75 			if ctxt.Debugpcln != 0 {
     76 				ctxt.Logf("%6x %6s %v\n", uint64(p.Pc), "", p)
     77 			}
     78 			continue
     79 		}
     80 
     81 		// The table is a sequence of (value, pc) pairs, where each
     82 		// pair states that the given value is in effect from the current position
     83 		// up to the given pc, which becomes the new current position.
     84 		// To generate the table as we scan over the program instructions,
     85 		// we emit a "(value" when pc == func->value, and then
     86 		// each time we observe a change in value we emit ", pc) (value".
     87 		// When the scan is over, we emit the closing ", pc)".
     88 		//
     89 		// The table is delta-encoded. The value deltas are signed and
     90 		// transmitted in zig-zag form, where a complement bit is placed in bit 0,
     91 		// and the pc deltas are unsigned. Both kinds of deltas are sent
     92 		// as variable-length little-endian base-128 integers,
     93 		// where the 0x80 bit indicates that the integer continues.
     94 
     95 		if ctxt.Debugpcln != 0 {
     96 			ctxt.Logf("%6x %6d %v\n", uint64(p.Pc), val, p)
     97 		}
     98 
     99 		if started != 0 {
    100 			addvarint(dst, uint32((p.Pc-pc)/int64(ctxt.Arch.MinLC)))
    101 			pc = p.Pc
    102 		}
    103 
    104 		delta = uint32(val) - uint32(oldval)
    105 		if delta>>31 != 0 {
    106 			delta = 1 | ^(delta << 1)
    107 		} else {
    108 			delta <<= 1
    109 		}
    110 		addvarint(dst, delta)
    111 		oldval = val
    112 		started = 1
    113 		val = valfunc(ctxt, func_, val, p, 1, arg)
    114 	}
    115 
    116 	if started != 0 {
    117 		if ctxt.Debugpcln != 0 {
    118 			ctxt.Logf("%6x done\n", uint64(func_.Text.Pc+func_.Size))
    119 		}
    120 		addvarint(dst, uint32((func_.Size-pc)/int64(ctxt.Arch.MinLC)))
    121 		addvarint(dst, 0) // terminator
    122 	}
    123 
    124 	if ctxt.Debugpcln != 0 {
    125 		ctxt.Logf("wrote %d bytes to %p\n", len(dst.P), dst)
    126 		for i := 0; i < len(dst.P); i++ {
    127 			ctxt.Logf(" %02x", dst.P[i])
    128 		}
    129 		ctxt.Logf("\n")
    130 	}
    131 
    132 	ctxt.Debugpcln -= int32(dbg)
    133 }
    134 
    135 // pctofileline computes either the file number (arg == 0)
    136 // or the line number (arg == 1) to use at p.
    137 // Because p->lineno applies to p, phase == 0 (before p)
    138 // takes care of the update.
    139 func pctofileline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 {
    140 	if p.As == ATEXT || p.As == ANOP || p.As == AUSEFIELD || p.Lineno == 0 || phase == 1 {
    141 		return oldval
    142 	}
    143 	f, l := linkgetline(ctxt, p.Lineno)
    144 	if f == nil {
    145 		//	print("getline failed for %s %v\n", ctxt->cursym->name, p);
    146 		return oldval
    147 	}
    148 
    149 	if arg == nil {
    150 		return l
    151 	}
    152 	pcln := arg.(*Pcln)
    153 
    154 	if f == pcln.Lastfile {
    155 		return int32(pcln.Lastindex)
    156 	}
    157 
    158 	for i, file := range pcln.File {
    159 		if file == f {
    160 			pcln.Lastfile = f
    161 			pcln.Lastindex = i
    162 			return int32(i)
    163 		}
    164 	}
    165 	i := len(pcln.File)
    166 	pcln.File = append(pcln.File, f)
    167 	pcln.Lastfile = f
    168 	pcln.Lastindex = i
    169 	return int32(i)
    170 }
    171 
    172 // pctospadj computes the sp adjustment in effect.
    173 // It is oldval plus any adjustment made by p itself.
    174 // The adjustment by p takes effect only after p, so we
    175 // apply the change during phase == 1.
    176 func pctospadj(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 {
    177 	if oldval == -1 { // starting
    178 		oldval = 0
    179 	}
    180 	if phase == 0 {
    181 		return oldval
    182 	}
    183 	if oldval+p.Spadj < -10000 || oldval+p.Spadj > 1100000000 {
    184 		ctxt.Diag("overflow in spadj: %d + %d = %d", oldval, p.Spadj, oldval+p.Spadj)
    185 		log.Fatalf("bad code")
    186 	}
    187 
    188 	return oldval + p.Spadj
    189 }
    190 
    191 // pctopcdata computes the pcdata value in effect at p.
    192 // A PCDATA instruction sets the value in effect at future
    193 // non-PCDATA instructions.
    194 // Since PCDATA instructions have no width in the final code,
    195 // it does not matter which phase we use for the update.
    196 func pctopcdata(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg interface{}) int32 {
    197 	if phase == 0 || p.As != APCDATA || p.From.Offset != int64(arg.(uint32)) {
    198 		return oldval
    199 	}
    200 	if int64(int32(p.To.Offset)) != p.To.Offset {
    201 		ctxt.Diag("overflow in PCDATA instruction: %v", p)
    202 		log.Fatalf("bad code")
    203 	}
    204 
    205 	return int32(p.To.Offset)
    206 }
    207 
    208 func linkpcln(ctxt *Link, cursym *LSym) {
    209 	ctxt.Cursym = cursym
    210 
    211 	pcln := new(Pcln)
    212 	cursym.Pcln = pcln
    213 
    214 	npcdata := 0
    215 	nfuncdata := 0
    216 	for p := cursym.Text; p != nil; p = p.Link {
    217 		// Find the highest ID of any used PCDATA table. This ignores PCDATA table
    218 		// that consist entirely of "-1", since that's the assumed default value.
    219 		//   From.Offset is table ID
    220 		//   To.Offset is data
    221 		if p.As == APCDATA && p.From.Offset >= int64(npcdata) && p.To.Offset != -1 { // ignore -1 as we start at -1, if we only see -1, nothing changed
    222 			npcdata = int(p.From.Offset + 1)
    223 		}
    224 		// Find the highest ID of any FUNCDATA table.
    225 		//   From.Offset is table ID
    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 && p.To.Offset != -1 {
    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