-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
symbols.go
433 lines (353 loc) · 8.7 KB
/
symbols.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
package emu
// Parse ca65's debugging symbols file
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
type SymbolRecord struct {
Name string
Size uint16 // size of the data (the '.res #' value)
AddrSize int // size of the address itself. either zero page or absolute
Value uint16
Type string
Defined *lineRecord
References []*lineRecord
}
type fileRecord struct {
Id int
ModuleId int
Name string
Size int
Mtime int // what is the format of this?
}
type Symbols struct {
Version string
files map[int]*fileRecord
lines map[int]*lineRecord
// indexed by scope name. "" is default
// "SomeLabel"
// "SomeScope::SomeLabel"
// "ParentLabel@localLabel"
sym map[string]*SymbolRecord
// Each address may have more than one symbol attached to it
symAddr map[uint16][]*SymbolRecord
segments map[int]*segmentRecord
}
type segmentRecord struct {
Id int
Name string
Start int
Size uint16
Type string
OutputName string
OutputOffset int
}
func (s *Symbols) AllLabels() []string {
l := []string{}
for key, _ := range s.sym {
l = append(l, key)
}
return l
}
// Returns a list of labels that occupy a given address.
// Returns nil if none found.
func (s *Symbols) LabelsAt(address uint16) ([]string) {
if list, ok := s.symAddr[address]; ok {
ret := []string{}
for _, rec := range list {
ret = append(ret, rec.Name)
}
return ret
}
return nil
}
// GetAddress("SomeLabel")
// GetAddress("SomeScope::SomeLabel")
// GetAddress("ParentLabel@localLabel")
func (s *Symbols) GetAddress(name string) (uint16, error) {
sym, err := s.GetSymbol(name)
if err != nil {
return 0, err
}
return sym.Value, nil
}
// Returns the full symbol object
func (s *Symbols) GetSymbol(name string) (*SymbolRecord, error) {
val, ok := s.sym[name]
if ok {
return val, nil
}
return nil, fmt.Errorf("Address symbol %q does not exist", name)
}
// Get the last defined label before the given address
func (s *Symbols) GetLastLabel(address uint16) string {
labelList := s.LabelsAt(address)
if len(labelList) > 0 {
return fmt.Sprintf("%s ($%04X)", labelList[0], address)
}
var last *SymbolRecord
for addr, sym := range s.symAddr {
if addr > address {
continue
}
if last == nil || last.Value < addr {
last = sym[0]
}
}
if last != nil {
return fmt.Sprintf("%s ($%04X)", last.Name, last.Value)
}
return "<none>"
}
type lineRecord struct {
Id int
File *fileRecord
Line int
Span *spanRecord
}
type spanRecord struct {
}
func NewSymbols(filename string) (*Symbols, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
scopes := map[int]map[string]string{}
symbols := map[int]map[string]string{}
lines := map[int]map[string]string{}
sym := &Symbols{
sym: map[string]*SymbolRecord{},
symAddr: map[uint16][]*SymbolRecord{},
Version: "",
files: map[int]*fileRecord{},
lines: map[int]*lineRecord{},
}
// pass one
reader := bufio.NewReader(file)
var readerr error
for readerr == nil {
var line string
line, readerr = reader.ReadString('\n')
if readerr != nil && readerr != io.EOF {
return nil, readerr
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
idx := strings.Index(line, "\t")
if idx == -1 {
return nil, fmt.Errorf("Invalid line: %q", line)
}
t := line[:idx]
vals := strings.Split(line[idx+1:], ",")
m := map[string]string{}
for _, kv := range vals {
data := strings.Split(kv, "=")
if len(data) != 2 {
return nil, fmt.Errorf("Invalid key/value pair: %q", kv)
}
m[data[0]] = strings.Trim(data[1], `"`)
}
idStr, ok := m["id"]
if !ok {
continue
}
id, err := strconv.ParseInt(idStr, 10, 32)
if err != nil {
return nil, err
}
switch t {
case "scope":
scopes[int(id)] = m
case "sym":
symbols[int(id)] = m
case "line":
lines[int(id)] = m
case "version":
sym.Version = m["major"] + "." + m["minor"]
case "seg":
id, err := strconv.Atoi(m["id"])
if err != nil {
return nil, fmt.Errorf("Unable to parse id value for segment %q: %w", m["id"], err)
}
start, err := strconv.ParseInt(m["start"], 0, 32)
if err != nil {
return nil, fmt.Errorf("Unable to parse start value for segment %q: %w", m["start"], err)
}
size, err := strconv.ParseUint(m["size"], 0, 16)
if err != nil {
return nil, fmt.Errorf("Unable to parse size value for segment %q: %w", m["size"], err)
}
seg := &segmentRecord{
Id: id,
Name: m["name"],
Start: int(start),
Size: uint16(size),
Type: m["type"],
}
var oname string
if val, ok := m["oname"]; ok {
oname = val
offset, err := strconv.Atoi(m["ooffs"])
if err != nil {
return nil, fmt.Errorf("Unable to parse ooffs value for segment %q: %w", m["ooffs"], err)
}
seg.OutputName = oname
seg.OutputOffset = offset
}
case "file":
mtime, err := strconv.ParseInt(m["mtime"], 0, 32)
if err != nil {
return nil, fmt.Errorf("Unable to parse mtime value of %q for file record: %w", m["mtime"], err)
}
size , err := strconv.ParseInt(m["size"], 10, 32)
if err != nil {
return nil, fmt.Errorf("Unable to parse size value of %q for file record: %w", m["size"], err)
}
sym.files[int(id)] = &fileRecord{
Id: int(id),
Name: m["name"],
Mtime: int(mtime),
Size: int(size),
}
default:
// all others not implemented yet
continue
}
}
// second passes
for _, line := range lines {
id, err := strconv.Atoi(line["id"])
if err != nil {
return nil, fmt.Errorf("Unable to parse line ID %q: %w", err)
}
fileId, err := strconv.Atoi(line["file"])
if err != nil {
return nil, fmt.Errorf("Unable to parse line fileId %q: %w", err)
}
lineNum, err := strconv.Atoi(line["line"])
if err != nil {
return nil, fmt.Errorf("Unable to parse line number %q: %w", err)
}
f, ok := sym.files[fileId]
if !ok {
return nil, fmt.Errorf("Cannot find file with ID %d", fileId)
}
sym.lines[id] = &lineRecord{
Id: id,
Line: lineNum,
File: f,
// TODO: span
}
}
for _, symbol := range symbols {
prefix := ""
var parent map[string]string
var parentOk bool = false
parentIdStr, ok := symbol["parent"]
if ok {
parentId, err := strconv.ParseInt(parentIdStr, 10, 32)
if err != nil {
return nil, err
}
parent, parentOk = symbols[int(parentId)]
}
var scopeIdStr string
if parentOk {
scopeIdStr = parent["scope"]
prefix = parent["name"]
} else {
scopeIdStr = symbol["scope"]
}
scopeId, err := strconv.ParseInt(scopeIdStr, 10, 32)
if err != nil {
return nil, err
}
scope, ok := scopes[int(scopeId)]
if !ok {
fmt.Printf("Scope with ID %d not found", scopeId)
continue
}
if scope["name"] != "" {
prefix = scope["name"] + "::" + prefix
}
var addrSize int = 0
switch symbol["addrsize"] {
case "absolute":
addrSize = 2
case "zeropage":
addrSize = 1
default:
fmt.Println("unknown addrsize:", symbol["addrsize"])
continue
}
size := uint16(1)
if val, ok := symbol["size"]; ok {
s, err := strconv.ParseUint(val, 0, 16)
if err != nil {
return nil, fmt.Errorf("Unable to parse size value %q: %w", val, err)
}
size = uint16(s)
}
addrValue, err := strconv.ParseUint(symbol["val"], 0, 16)
if err != nil {
return nil, fmt.Errorf("Unable to parse address value %q: %w", symbol["val"], err)
}
name := symbol["name"]
if prefix != "" {
name = prefix+name
}
def := symbol["def"]
plusIdx := strings.LastIndex(def, "+")
if plusIdx > -1 {
def = def[plusIdx:]
}
definedId, err := strconv.Atoi(def)
if err != nil {
return nil, fmt.Errorf("Unable to parse def value for symbol %q: %w", symbol["def"], err)
}
defined, ok := sym.lines[definedId]
if !ok {
return nil, fmt.Errorf("No line with id %d", definedId)
}
record := &SymbolRecord{
Name: name,
AddrSize: addrSize,
Size: size,
Value: uint16(addrValue),
Type: scope["type"],
Defined: defined,
References: []*lineRecord{},
}
sym.sym[name] = record
if references, ok := symbol["ref"]; ok {
refList := strings.Split(references, "+")
for _, lineIdStr := range refList {
lineId, err := strconv.Atoi(lineIdStr)
if err != nil {
return nil, fmt.Errorf("Unable to parse reference id %q: %w", lineIdStr, err)
}
if line, lok := sym.lines[lineId]; lok {
record.References = append(record.References, line)
}
}
}
// Add a reference for every address this label occupies
for i := uint16(0); i < size; i++ {
if _, ok := sym.symAddr[uint16(addrValue)+i]; !ok {
sym.symAddr[uint16(addrValue)+i] = []*SymbolRecord{}
}
sym.symAddr[uint16(addrValue)+i] = append(sym.symAddr[uint16(addrValue)+i], record)
}
}
//for name, _ := range sym.sym {
// fmt.Println(name)
//}
return sym, nil
}