forked from Happy-Ferret/sym
-
Notifications
You must be signed in to change notification settings - Fork 2
/
symbol.go
586 lines (502 loc) · 15.7 KB
/
symbol.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package sym
import (
"encoding/binary"
"fmt"
"io"
"strconv"
"strings"
"github.com/lunixbochs/struc"
"github.com/pkg/errors"
)
// A Symbol is a PS1 symbol.
type Symbol struct {
// Symbol header.
Hdr *SymbolHeader
// Symbol body.
Body SymbolBody
}
// String returns the string representation of the symbol.
func (sym *Symbol) String() string {
return fmt.Sprintf("%v %v", sym.Hdr, sym.Body)
}
// Size returns the size of the symbol in bytes.
func (sym *Symbol) Size() int {
hdrSize := binary.Size(*sym.Hdr)
return hdrSize + sym.Body.BodySize()
}
// A SymbolHeader is a PS1 symbol header.
type SymbolHeader struct {
// Address or value of symbol.
Value uint32 `struc:"uint32,little"`
// Symbol kind; specifies type of symbol body.
Kind Kind `struc:"uint8"`
}
// String returns the string representation of the symbol header.
func (hdr *SymbolHeader) String() string {
return fmt.Sprintf("$%08x %v", hdr.Value, hdr.Kind)
}
// SymbolBody is the sum-type of all symbol bodies.
type SymbolBody interface {
fmt.Stringer
// BodySize returns the size of the symbol body in bytes.
BodySize() int
}
// parseSymbol parses and returns a PS1 symbol.
func parseSymbol(r io.Reader) (*Symbol, error) {
// Parse symbol header.
sym := &Symbol{}
hdr, err := parseSymbolHeader(r)
if err != nil {
return nil, errors.WithStack(err)
}
sym.Hdr = hdr
// Parse symbol body.
body, err := parseSymbolBody(r, hdr.Kind)
if err != nil {
return sym, errors.WithStack(err)
}
sym.Body = body
return sym, nil
}
// parseSymbolHeader parses and returns a PS1 symbol header.
func parseSymbolHeader(r io.Reader) (*SymbolHeader, error) {
hdr := &SymbolHeader{}
if err := struc.Unpack(r, hdr); err != nil {
return nil, errors.WithStack(err)
}
return hdr, nil
}
// parseSymbolBody parses and returns a PS1 symbol body.
func parseSymbolBody(r io.Reader, kind Kind) (SymbolBody, error) {
parse := func(body SymbolBody) (SymbolBody, error) {
if err := struc.Unpack(r, body); err != nil {
return nil, errors.WithStack(err)
}
return body, nil
}
switch kind {
case KindName1:
return parse(&Name1{})
case KindName2:
return parse(&Name2{})
case KindName5:
return parse(&Name2{})
case KindName6:
return parse(&Name2{})
case KindIncSLD:
// empty body.
return &IncSLD{}, nil
case KindIncSLDByte:
return parse(&IncSLDByte{})
case KindIncSLDWord:
return parse(&IncSLDWord{})
case KindSetSLD:
return parse(&SetSLD{})
case KindSetSLD2:
return parse(&SetSLD2{})
case KindEndSLD:
// empty body.
return &EndSLD{}, nil
case KindFuncStart:
return parse(&FuncStart{})
case KindFuncEnd:
return parse(&FuncEnd{})
case KindBlockStart:
return parse(&BlockStart{})
case KindBlockEnd:
return parse(&BlockEnd{})
case KindDef:
return parse(&Def{})
case KindDef2:
return parse(&Def2{})
case KindOverlay:
return parse(&Overlay{})
case KindSetOverlay:
// empty body.
return &SetOverlay{}, nil
default:
return nil, errors.Errorf("support for symbol kind 0x%02X not yet implemented", uint8(kind))
}
}
// --- [ 0x01 ] ----------------------------------------------------------------
// A Name1 symbol specifies the name of a symbol.
//
// Value of the symbol header specifies the associated address.
type Name1 struct {
// Name length.
NameLen uint8 `struc:"uint8,sizeof=Name"`
// Symbol name,
Name string
}
// String returns the string representation of the name symbol.
func (body *Name1) String() string {
// $00000000 1 __RHS2_data_size
return body.Name
}
// BodySize returns the size of the symbol body in bytes.
func (body *Name1) BodySize() int {
return 1 + int(body.NameLen)
}
// --- [ 0x02 ] ----------------------------------------------------------------
// A Name2 symbol specifies the name of a symbol.
//
// Value of the symbol header specifies the associated address.
type Name2 struct {
// Name length.
NameLen uint8 `struc:"uint8,sizeof=Name"`
// Symbol name,
Name string
}
// String returns the string representation of the name symbol.
func (body *Name2) String() string {
// $80010000 2 printattribute
return body.Name
}
// BodySize returns the size of the symbol body in bytes.
func (body *Name2) BodySize() int {
return 1 + int(body.NameLen)
}
// --- [ 0x05 ] ----------------------------------------------------------------
// A Name5 symbol specifies the name of a symbol.
//
// Value of the symbol header specifies the associated address.
type Name5 struct {
// Name length.
NameLen uint8 `struc:"uint8,sizeof=Name"`
// Symbol name,
Name string
}
// String returns the string representation of the name symbol.
func (body *Name5) String() string {
// $00000000 5 m
return body.Name
}
// BodySize returns the size of the symbol body in bytes.
func (body *Name5) BodySize() int {
return 1 + int(body.NameLen)
}
// --- [ 0x06 ] ----------------------------------------------------------------
// A Name6 symbol specifies the name of a symbol.
//
// Value of the symbol header specifies the associated address.
type Name6 struct {
// Name length.
NameLen uint8 `struc:"uint8,sizeof=Name"`
// Symbol name,
Name string
}
// String returns the string representation of the name symbol.
func (body *Name6) String() string {
// $00010604 6 DoTitle
return body.Name
}
// BodySize returns the size of the symbol body in bytes.
func (body *Name6) BodySize() int {
return 1 + int(body.NameLen)
}
// --- [ 0x80 ] ----------------------------------------------------------------
// An IncSLD symbol increments the current line number.
//
// Value of the symbol header specifies the associated address.
type IncSLD struct {
}
// String returns the string representation of the line number increment symbol.
func (body *IncSLD) String() string {
// $80010004 80 Inc SLD linenum (to 116)
return "Inc SLD linenum"
}
// BodySize returns the size of the symbol body in bytes.
func (body *IncSLD) BodySize() int {
return 0
}
// --- [ 0x82 ] ----------------------------------------------------------------
// An IncSLDByte symbol specifies the increment of the current line number.
//
// Value of the symbol header specifies the associated address.
type IncSLDByte struct {
Inc uint8 `struc:"uint8"`
}
// String returns the string representation of the line number increment symbol.
func (body *IncSLDByte) String() string {
// $80010008 82 Inc SLD linenum by byte 2 (to 118)
return fmt.Sprintf("Inc SLD linenum by byte %d", body.Inc)
}
// BodySize returns the size of the symbol body in bytes.
func (body *IncSLDByte) BodySize() int {
return 1
}
// --- [ 0x84 ] ----------------------------------------------------------------
// An IncSLDWord symbol specifies the increment of the current line number.
//
// Value of the symbol header specifies the associated address.
type IncSLDWord struct {
Inc uint16 `struc:"uint16,little"`
}
// String returns the string representation of the line number increment symbol.
func (body *IncSLDWord) String() string {
// $80025d38 84 Inc SLD linenum by word 276 (to 479)
return fmt.Sprintf("Inc SLD linenum by word %d", body.Inc)
}
// BodySize returns the size of the symbol body in bytes.
func (body *IncSLDWord) BodySize() int {
return 2
}
// --- [ 0x86 ] ----------------------------------------------------------------
// A SetSLD symbol specifies the current line number.
//
// Value of the symbol header specifies the associated address.
type SetSLD struct {
// Line number.
Line uint32 `struc:"uint32,little"`
}
// String returns the string representation of the set line number symbol.
func (body *SetSLD) String() string {
// $8001ff08 86 Set SLD linenum to 88
return fmt.Sprintf("Set SLD linenum to %d", body.Line)
}
// BodySize returns the size of the symbol body in bytes.
func (body *SetSLD) BodySize() int {
return 4
}
// --- [ 0x88 ] ----------------------------------------------------------------
// A SetSLD2 symbol specifies the current line number and source file.
//
// Value of the symbol header specifies the associated address.
type SetSLD2 struct {
// Line number.
Line uint32 `struc:"uint32,little"`
// Path length.
PathLen uint8 `struc:"uint8,sizeof=Path"`
// Source file,
Path string
}
// String returns the string representation of the set line number symbol.
func (body *SetSLD2) String() string {
// $80010000 88 Set SLD to line 115 of file D:\LIB\PSX\NULLFUNC.ASM
return fmt.Sprintf("Set SLD to line %d of file %s", body.Line, body.Path)
}
// BodySize returns the size of the symbol body in bytes.
func (body *SetSLD2) BodySize() int {
return 4 + 1 + int(body.PathLen)
}
// --- [ 0x8A ] ----------------------------------------------------------------
// An EndSLD symbol indicates the end of a line number specifier.
//
// Value of the symbol header specifies the associated address.
type EndSLD struct {
}
// String returns the string representation of the end of line number symbol.
func (body *EndSLD) String() string {
// $80020ffc 8a End SLD info
return "End SLD info"
}
// BodySize returns the size of the symbol body in bytes.
func (body *EndSLD) BodySize() int {
return 0
}
// --- [ 0x8C ] ----------------------------------------------------------------
// A FuncStart symbol specifies the start of a function.
//
// Value of the symbol header specifies the associated address.
type FuncStart struct {
// Frame pointer register.
FP uint16 `struc:"uint16,little"`
// Function size.
FSize uint32 `struc:"uint32,little"`
// Return address register.
RetReg uint16 `struc:"uint16,little"`
// Mask.
Mask uint32 `struc:"uint32,little"`
// Mask offset.
MaskOffset int32 `struc:"int32,little"`
// Line number.
Line uint32 `struc:"uint32,little"`
// Path length.
PathLen uint8 `struc:"uint8,sizeof=Path"`
// Source file.
Path string
// Name length.
NameLen uint8 `struc:"uint8,sizeof=Name"`
// Symbol name.
Name string
}
// String returns the string representation of the function start symbol.
func (body *FuncStart) String() string {
// $8001fefc 8c Function_start
// fp = 29
// fsize = 24
// retreg = 31
// mask = $80000000
// maskoffs = -8
// line = 88
// file = C:\DIABPSX\GLIBDEV\SOURCE\TASKER.C
// name = DoEpi
const format = `Function_start
fp = %d
fsize = %d
retreg = %d
mask = $%08x
maskoffs = %d
line = %d
file = %s
name = %s`
return fmt.Sprintf(format, body.FP, body.FSize, body.RetReg, body.Mask, body.MaskOffset, body.Line, body.Path, body.Name)
}
// BodySize returns the size of the symbol body in bytes.
func (body *FuncStart) BodySize() int {
return 2 + 4 + 2 + 4 + 4 + 4 + 1 + int(body.PathLen) + 1 + int(body.NameLen)
}
// --- [ 0x8E ] ----------------------------------------------------------------
// A FuncEnd symbol specifies the end of a function.
//
// Value of the symbol header specifies the associated address.
type FuncEnd struct {
// Line number.
Line uint32 `struc:"uint32,little"`
}
// String returns the string representation of the function end symbol.
func (body *FuncEnd) String() string {
// $8001ff4c 8e Function_end line 91
return fmt.Sprintf("Function_end line %d", body.Line)
}
// BodySize returns the size of the symbol body in bytes.
func (body *FuncEnd) BodySize() int {
return 4
}
// --- [ 0x90 ] ----------------------------------------------------------------
// A BlockStart symbol specifies the start of a block.
//
// Value of the symbol header specifies the associated address.
type BlockStart struct {
// Line number.
Line uint32 `struc:"uint32,little"`
}
// String returns the string representation of the block start symbol.
func (body *BlockStart) String() string {
// $8003017c 90 Block_start line = 1
return fmt.Sprintf("Block_start line = %d", body.Line)
}
// BodySize returns the size of the symbol body in bytes.
func (body *BlockStart) BodySize() int {
return 4
}
// --- [ 0x92 ] ----------------------------------------------------------------
// A BlockEnd symbol specifies the end of a block.
//
// Value of the symbol header specifies the associated address.
type BlockEnd struct {
// Line number.
Line uint32 `struc:"uint32,little"`
}
// String returns the string representation of the block end symbol.
func (body *BlockEnd) String() string {
// $800301ac 92 Block_end line = 4
return fmt.Sprintf("Block_end line = %d", body.Line)
}
// BodySize returns the size of the symbol body in bytes.
func (body *BlockEnd) BodySize() int {
return 4
}
// --- [ 0x94 ] ----------------------------------------------------------------
// A Def symbol specifies the class, type, size and name of a definition.
//
// Value of the symbol header specifies the associated address.
type Def struct {
// Definition class.
Class Class `struc:"uint16,little"`
// Definition type.
Type Type `struc:"uint16,little"`
// Definition size.
Size uint32 `struc:"uint32,little"`
// Name length.
NameLen uint8 `struc:"uint8,sizeof=Name"`
// Definition name,
Name string
}
// String returns the string representation of the definition symbol.
func (body *Def) String() string {
// $00000000 94 Def class TPDEF type UCHAR size 0 name u_char
return fmt.Sprintf("Def class %v type %v size %v name %v", body.Class, body.Type, body.Size, body.Name)
}
// BodySize returns the size of the symbol body in bytes.
func (body *Def) BodySize() int {
return 2 + 2 + 4 + 1 + int(body.NameLen)
}
// --- [ 0x96 ] ----------------------------------------------------------------
// A Def2 symbol specifies the class, type, size, dimensions, tag and name of a
// definition.
//
// Value of the symbol header specifies the associated address.
type Def2 struct {
// Definition class.
Class Class `struc:"uint16,little"`
// Definition type.
Type Type `struc:"uint16,little"`
// Definition size.
Size uint32 `struc:"uint32,little"`
// Dimensions length.
DimsLen uint16 `struc:"uint16,little,sizeof=Dims"`
// Dimensions.
Dims []uint32 `struc:"[]uint32,little"`
// Tag length.
TagLen uint8 `struc:"uint8,sizeof=Tag"`
// Definition tag,
Tag string
// Name length.
NameLen uint8 `struc:"uint8,sizeof=Name"`
// Definition name,
Name string
}
// String returns the string representation of the definition symbol.
func (body *Def2) String() string {
// $00000000 96 Def2 class MOS type ARY INT size 4 dims 1 1 tag name r
dd := make([]string, len(body.Dims))
for i, dim := range body.Dims {
dd[i] = strconv.Itoa(int(dim))
}
dims := fmt.Sprintf("%d %s", body.DimsLen, strings.Join(dd, " "))
if body.DimsLen == 0 {
dims = "0"
}
return fmt.Sprintf("Def2 class %v type %v size %v dims %s tag %v name %v", body.Class, body.Type, body.Size, dims, body.Tag, body.Name)
}
// BodySize returns the size of the symbol body in bytes.
func (body *Def2) BodySize() int {
return 2 + 2 + 4 + 2 + int(4*body.DimsLen) + 1 + int(body.TagLen) + 1 + int(body.NameLen)
}
// --- [ 0x98 ] ----------------------------------------------------------------
// An Overlay symbol specifies the length and id of a file overlay (e.g. a
// shared library).
//
// Value of the symbol header specifies the base address at which the overlay is
// loaded.
type Overlay struct {
// Overlay length in bytes.
Length uint32 `struc:"uint32,little"`
// Overlay ID.
ID uint32 `struc:"uint32,little"`
}
// String returns the string representation of the overlay symbol.
func (body *Overlay) String() string {
// $800b031c overlay length $000009e4 id $4
return fmt.Sprintf("length $%08x id $%x", body.Length, body.ID)
}
// BodySize returns the size of the symbol body in bytes.
func (body *Overlay) BodySize() int {
return 4 + 4
}
// --- [ 0x9A ] ----------------------------------------------------------------
// A SetOverlay specifies the active overlay.
//
// Value of the symbol header specifies the active overlay ID.
type SetOverlay struct {
}
// String returns the string representation of the set overlay symbol.
func (body *SetOverlay) String() string {
// $00000004 set overlay
return ""
}
// BodySize returns the size of the symbol body in bytes.
func (body *SetOverlay) BodySize() int {
return 0
}