/
sourcemap.go
511 lines (435 loc) · 10.2 KB
/
sourcemap.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// Copyright 2014-2018 Brett Vickers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package asm
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"sort"
)
// A SourceMap describes the mapping between source code line numbers and
// assembly code addresses.
type SourceMap struct {
Origin uint16
Size uint32
CRC uint32
Files []string
Lines []SourceLine
Exports []Export
}
// A SourceLine represents a mapping between a machine code address and
// the source code file and line number used to generate it.
type SourceLine struct {
Address int // Machine code address
FileIndex int // Source code file index
Line int // Source code line number
}
// Encoding flags
const (
continued byte = 1 << 7
negative byte = 1 << 6
fileIndexChanged byte = 1 << 5
)
// NewSourceMap creates an empty source map.
func NewSourceMap() *SourceMap {
return &SourceMap{
Files: []string{},
Lines: []SourceLine{},
Exports: []Export{},
}
}
// Find searches the source map for a source code line corresponding to the
// requested address.
func (s *SourceMap) Find(addr int) (filename string, line int, err error) {
i := sort.Search(len(s.Lines), func(i int) bool {
return s.Lines[i].Address >= addr
})
if i < len(s.Lines) && s.Lines[i].Address == addr {
return s.Files[s.Lines[i].FileIndex], s.Lines[i].Line, nil
}
return "", 0, fmt.Errorf("address $%04X not found in source file", addr)
}
// ClearRange clears portions of the source map that reference the
// address range between `origin` and `origin+size`.
func (s *SourceMap) ClearRange(origin, size int) {
min := uint16(origin)
max := uint16(origin + size)
// Filter out original exports covered by the new map's address range.
exports := make([]Export, 0, len(s.Exports))
for _, e := range s.Exports {
if e.Address < min || e.Address > max {
exports = append(exports, e)
}
}
// Filter out original source lines covered by the new map's address
// range. Track only the files that remain referenced.
fileCount := 0
fileMap := make(map[string]int) // filename -> file index
lines := make([]SourceLine, 0, len(s.Lines))
for _, l := range s.Lines {
if uint16(l.Address) < min || uint16(l.Address) >= max {
filename := s.Files[l.FileIndex]
if fileIndex, ok := fileMap[filename]; ok {
l.FileIndex = fileIndex
} else {
fileIndex = fileCount
fileMap[filename] = fileIndex
l.FileIndex = fileIndex
fileCount++
}
lines = append(lines, l)
}
}
// Build the files array from the file map.
files := make([]string, len(fileMap))
for f, i := range fileMap {
files[i] = f
}
s.Files = files
s.Lines = lines
s.Exports = exports
}
type bySLAddr []SourceLine
func (a bySLAddr) Len() int { return len(a) }
func (a bySLAddr) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a bySLAddr) Less(i, j int) bool { return a[i].Address < a[j].Address }
type byEAddr []Export
func (a byEAddr) Len() int { return len(a) }
func (a byEAddr) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byEAddr) Less(i, j int) bool { return a[i].Address < a[j].Address }
// Merge merges another source map (s2) into this source map.
func (s *SourceMap) Merge(s2 *SourceMap) {
// Clear the portion of the original source map that references addresses
// in the new map's range.
s.ClearRange(int(s2.Origin), int(s2.Size))
// Add exports from the new map.
s.Exports = append(s.Exports, s2.Exports...)
// Sort exports by address.
sort.Sort(byEAddr(s.Exports))
// Build a mapping from filename to file index.
fileCount := 0
fileMap := make(map[string]int)
for i, f := range s.Files {
fileMap[f] = i
}
// Add source lines from the new map.
for _, l := range s2.Lines {
filename := s2.Files[l.FileIndex]
if fileIndex, ok := fileMap[filename]; ok {
l.FileIndex = fileIndex
} else {
fileIndex = fileCount
fileMap[filename] = fileIndex
l.FileIndex = fileIndex
fileCount++
}
s.Lines = append(s.Lines, l)
}
// Sort lines by address.
sort.Sort(bySLAddr(s.Lines))
// Build the files array from the file map.
s.Files = make([]string, len(fileMap))
for f, i := range fileMap {
s.Files[i] = f
}
}
// ReadFrom reads the contents of an assembly source map.
func (s *SourceMap) ReadFrom(r io.Reader) (n int64, err error) {
rr := bufio.NewReader(r)
b := make([]byte, 26)
nn, err := io.ReadFull(rr, b)
n += int64(nn)
if err != nil {
return n, err
}
if len(b) < 16 || !bytes.Equal(b[0:4], []byte(sourceMapSignature)) {
return n, errors.New("invalid source map format")
}
if b[4] != versionMajor || b[5] != versionMinor {
return n, errors.New("invalid source map version")
}
s.Origin = binary.LittleEndian.Uint16(b[6:8])
s.Size = binary.LittleEndian.Uint32(b[8:12])
s.CRC = binary.LittleEndian.Uint32(b[12:16])
fileCount := int(binary.LittleEndian.Uint16(b[16:18]))
lineCount := int(binary.LittleEndian.Uint32(b[18:22]))
exportCount := int(binary.LittleEndian.Uint32(b[22:26]))
s.Files = make([]string, fileCount)
for i := 0; i < fileCount; i++ {
file, err := rr.ReadString(0)
n += int64(len(file))
if err != nil {
return n, err
}
s.Files[i] = file[:len(file)-1]
}
s.Lines = make([]SourceLine, 0, lineCount)
if lineCount > 0 {
var line SourceLine
for i := 0; i < lineCount; i++ {
var nn int
line, nn, err = decodeSourceLine(rr, line)
n += int64(nn)
if err != nil {
return n, err
}
s.Lines = append(s.Lines, line)
}
}
s.Exports = make([]Export, exportCount)
for i := 0; i < exportCount; i++ {
label, err := rr.ReadString(0)
n += int64(len(label))
if err != nil {
return n, err
}
s.Exports[i].Label = label[:len(label)-1]
nn, err = io.ReadFull(rr, b[:2])
n += int64(nn)
if err != nil {
return n, err
}
s.Exports[i].Address = binary.LittleEndian.Uint16(b[0:2])
}
return n, nil
}
// WriteTo writes the contents of an assembly source map to an output
// stream.
func (s *SourceMap) WriteTo(w io.Writer) (n int64, err error) {
fileCount := uint16(len(s.Files))
lineCount := uint32(len(s.Lines))
exportCount := uint32(len(s.Exports))
ww := bufio.NewWriter(w)
var hdr [26]byte
copy(hdr[:], []byte(sourceMapSignature))
hdr[4] = versionMajor
hdr[5] = versionMinor
binary.LittleEndian.PutUint16(hdr[6:8], s.Origin)
binary.LittleEndian.PutUint32(hdr[8:12], s.Size)
binary.LittleEndian.PutUint32(hdr[12:16], s.CRC)
binary.LittleEndian.PutUint16(hdr[16:18], fileCount)
binary.LittleEndian.PutUint32(hdr[18:22], lineCount)
binary.LittleEndian.PutUint32(hdr[22:26], exportCount)
nn, err := ww.Write(hdr[:])
n += int64(nn)
if err != nil {
return n, err
}
for _, f := range s.Files {
nn, err = ww.WriteString(f)
n += int64(nn)
if err != nil {
return n, err
}
err = ww.WriteByte(0)
if err != nil {
return 0, err
}
n++
}
if len(s.Lines) > 0 {
var prev SourceLine
for _, line := range s.Lines {
nn, err = encodeSourceLine(ww, prev, line)
n += int64(nn)
if err != nil {
return n, err
}
prev = line
}
}
for _, e := range s.Exports {
nn, err = ww.WriteString(e.Label)
n += int64(nn)
if err != nil {
return n, err
}
ww.WriteByte(0)
if err != nil {
return n, err
}
n++
var b [2]byte
binary.LittleEndian.PutUint16(b[:], e.Address)
nn, err = ww.Write(b[:])
n += int64(nn)
if err != nil {
return n, err
}
}
ww.Flush()
return n, nil
}
func decodeSourceLine(r *bufio.Reader, prev SourceLine) (line SourceLine, n int, err error) {
da, nn, err := decode67(r)
n += nn
if err != nil {
return line, n, err
}
var dl int
var f bool
dl, f, nn, err = decode57(r)
n += nn
if err != nil {
return line, n, err
}
var df int
if f {
df, nn, err = decode67(r)
n += nn
if err != nil {
return line, n, err
}
}
line.Address = prev.Address + da
line.FileIndex = prev.FileIndex + df
line.Line = prev.Line + dl
return line, n, nil
}
func decode7(r *bufio.Reader) (value int, n int, err error) {
var shift uint
for {
var b byte
b, err = r.ReadByte()
if err != nil {
return 0, n, err
}
n++
value |= (int(b&0x7f) << shift)
shift += 7
if (b & continued) == 0 {
break
}
}
return value, n, nil
}
func decode57(r *bufio.Reader) (value int, f bool, n int, err error) {
var b byte
b, err = r.ReadByte()
if err != nil {
return 0, f, n, err
}
n++
value = int(b & 0x1f)
f = (b & fileIndexChanged) != 0
neg := (b & negative) != 0
if (b & continued) != 0 {
var vl, nn int
vl, nn, err = decode7(r)
n += nn
if err != nil {
return 0, f, n, err
}
value |= vl << 5
}
if neg {
value = -value
}
return value, f, n, nil
}
func decode67(r *bufio.Reader) (value int, n int, err error) {
var b byte
b, err = r.ReadByte()
if err != nil {
return 0, n, err
}
n++
value = int(b & 0x3f)
neg := (b & negative) != 0
if (b & continued) != 0 {
var vl, nn int
vl, nn, err = decode7(r)
n += nn
if err != nil {
return 0, n, err
}
value |= vl << 6
}
if neg {
value = -value
}
return value, n, nil
}
func encodeSourceLine(w *bufio.Writer, l0, l1 SourceLine) (n int, err error) {
da := l1.Address - l0.Address
df := l1.FileIndex - l0.FileIndex
dl := l1.Line - l0.Line
nn, err := encode67(w, da)
n += nn
if err != nil {
return n, err
}
nn, err = encode57(w, dl, df != 0)
n += nn
if err != nil {
return n, err
}
if df != 0 {
nn, err = encode67(w, df)
n += nn
}
return n, err
}
func encode7(w *bufio.Writer, v int) (n int, err error) {
for v != 0 {
var b byte
if v >= 0x80 {
b |= continued
}
b |= (byte(v) & 0x7f)
err = w.WriteByte(b)
if err != nil {
return n, err
}
n++
v >>= 7
}
return n, nil
}
func encode57(w *bufio.Writer, v int, f bool) (n int, err error) {
var b byte
if f {
b |= fileIndexChanged
}
if v < 0 {
b |= negative
v = -v
}
if v >= 0x20 {
b |= continued
}
b |= (byte(v) & 0x1f)
err = w.WriteByte(b)
if err != nil {
return n, err
}
n++
v >>= 5
nn, err := encode7(w, v)
n += nn
return n, err
}
func encode67(w *bufio.Writer, v int) (n int, err error) {
var b byte
if v < 0 {
b |= negative
v = -v
}
if v >= 0x40 {
b |= continued
}
b |= (byte(v) & 0x3f)
err = w.WriteByte(b)
if err != nil {
return n, err
}
n++
v >>= 6
nn, err := encode7(w, v)
n += nn
return n, err
}