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