forked from goretk/gore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
817 lines (749 loc) · 22 KB
/
type.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
// This file is part of GoRE.
//
// Copyright (C) 2019-2021 GoRE Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package gore
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"reflect"
)
const (
intSize32 = 4
intSize64 = intSize32 * 2
kindMask = (1 << 5) - 1
tflagExtraStar uint8 = 1 << 1
tflagUncommon uint8 = 1 << 0
)
type _typeField uint8
const (
_typeFieldSize = iota
_typeFieldKind
_typeFieldStr
_typeFieldFlag
_typeFieldEnd
)
// ChanDir is a channel direction.
type ChanDir int
const (
// ChanRecv is a receive only chan (<-chan)
ChanRecv ChanDir = 1 << iota
// ChanSend is a send only chan (chan<-)
ChanSend
// ChanBoth is a send and receive chan (chan)
ChanBoth = ChanRecv | ChanSend
)
func getTypes(fileInfo *FileInfo, f fileHandler, md moduledata) (map[uint64]*GoType, error) {
if GoVersionCompare(fileInfo.goversion.Name, "go1.7beta1") < 0 {
return getLegacyTypes(fileInfo, f, md)
}
types, err := md.Types().Data()
if err != nil {
return nil, fmt.Errorf("failed to get types data section: %w", err)
}
typeLink, err := md.TypeLinkData()
if err != nil {
return nil, fmt.Errorf("failed to get type link data: %w", err)
}
// New parser
parser := newTypeParser(types, md.Types().Address, fileInfo)
for _, off := range typeLink {
typ, err := parser.parseType(uint64(off) + parser.base)
if err != nil || typ == nil {
return nil, fmt.Errorf("failed to parse type at offset 0x%x: %w", off, err)
}
}
return parser.parsedTypes(), nil
}
func getLegacyTypes(fileInfo *FileInfo, f fileHandler, md moduledata) (map[uint64]*GoType, error) {
typelinkAddr, typelinkData, err := f.getSectionDataFromAddress(md.TypelinkAddr)
if err != nil {
return nil, fmt.Errorf("no typelink section found: %w", err)
}
r := bytes.NewReader(typelinkData)
_, err = r.Seek(int64(md.TypelinkAddr)-int64(typelinkAddr), io.SeekStart)
if err != nil {
return nil, err
}
goTypes := make(map[uint64]*GoType)
for i := uint64(0); i < md.TypelinkLen; i++ {
// Type offsets are always *_type
address, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil, err
}
baseAddr, baseData, err := f.getSectionDataFromAddress(address)
if err != nil {
continue
}
typ := typeParse(goTypes, fileInfo, address-baseAddr, baseData, baseAddr)
if typ == nil {
continue
}
}
return goTypes, nil
}
// GoType is a representation of all types in Go.
type GoType struct {
// Kind indicates the specific kind of type the GoType
Kind reflect.Kind
// Name is the name of the type.
Name string
// Addr is the virtual address to where the type struct is defined.
Addr uint64
// PtrResolvAddr is the address to where the resolved structure is located
// if the GoType is of pointer kind.
PtrResolvAddr uint64
// PackagePath is the name of the package import path for the GoType.
PackagePath string
// Fields is a slice of the struct fields if the GoType is of kind struct.
Fields []*GoType
// FieldName is the name of the field if the GoType is a struct field.
FieldName string
// FieldTag holds the extracted tag for the field.
FieldTag string
// FieldAnon is true if the field does not have a name and is an embedded type.
FieldAnon bool
// Element is the element type for arrays, slices channels or the resolved type for
// a pointer type. For example int if the slice is a []int.
Element *GoType
// Length is the array or slice length.
Length int
// ChanDir is the channel direction
ChanDir ChanDir
// Key is the key type for a map.
Key *GoType
// FuncArgs holds the argument types for the function if the type is a function kind.
FuncArgs []*GoType
// FuncReturnVals holds the return types for the function if the type is a function kind.
FuncReturnVals []*GoType
// IsVariadic is true if the last argument type is variadic. For example "func(s string, n ...int)"
IsVariadic bool
// Methods holds information of the types methods.
Methods []*TypeMethod
flag uint8
}
// String implements the fmt.Stringer interface.
func (t *GoType) String() string {
switch t.Kind {
case reflect.Slice:
return fmt.Sprintf("[]%s", t.Element)
case reflect.Array:
return fmt.Sprintf("[%d]%s", t.Length, t.Element)
case reflect.Map:
return fmt.Sprintf("map[%s]%s", t.Key, t.Element)
case reflect.Struct:
// Handle empty struct type
if t.Name == "" {
return "struct{}"
}
return t.Name
case reflect.Ptr:
return fmt.Sprintf("*%s", t.Element)
case reflect.Chan:
if t.ChanDir == ChanRecv {
return fmt.Sprintf("<-chan %s", t.Element)
}
if t.ChanDir == ChanSend {
return fmt.Sprintf("chan<- %s", t.Element)
}
return fmt.Sprintf("chan %s", t.Element)
case reflect.Func:
buf := "func("
for i, a := range t.FuncArgs {
if i != 0 {
buf += ", "
}
if a.Kind == reflect.Func && a.Name == t.Name {
buf += a.Name
} else {
buf += a.String()
}
}
if len(t.FuncReturnVals) > 1 {
buf += ") ("
} else if len(t.FuncReturnVals) == 1 {
buf += ") "
} else {
buf += ")"
}
for i, r := range t.FuncReturnVals {
if i != 0 {
buf += ", "
}
if r.Kind == reflect.Func && r.Name == t.Name {
buf += r.Name
} else {
buf += r.String()
}
}
if len(t.FuncReturnVals) > 1 {
buf += ")"
}
return buf
case reflect.Interface:
// Handle empty interface
if t.Name == "" {
return "interface{}"
}
return t.Name
case reflect.Invalid:
return t.Name
default:
return t.Kind.String()
}
}
// StructDef reconstructs the type definition code for the struct.
// If the type is not a struct, an empty string is returned.
func StructDef(typ *GoType) string {
if typ.Kind != reflect.Struct {
return ""
}
buf := fmt.Sprintf("type %s struct{", typ.Name)
for _, f := range typ.Fields {
if f.FieldAnon {
buf += fmt.Sprintf("\n\t%s", f)
} else {
buf += fmt.Sprintf("\n\t%s %s", f.FieldName, f)
}
if f.FieldTag != "" {
buf += "\t`" + f.FieldTag + "`"
}
}
if len(typ.Fields) > 0 {
buf += "\n"
}
return buf + "}"
}
// InterfaceDef reconstructs the type definition code for the interface.
// If the type is not an interface, an empty string is returned.
func InterfaceDef(typ *GoType) string {
if typ.Kind != reflect.Interface {
return ""
}
// Handle interface with no methods defined.
if len(typ.Methods) == 0 {
return "type " + typ.Name + " interface{}"
}
// Remove package from name.
buf := fmt.Sprintf("type %s interface {", typ.Name)
for _, m := range typ.Methods {
buf += fmt.Sprintf("\n\t%s%s", m.Name, m.Type.String()[4:])
}
return buf + "\n}"
}
// MethodDef constructs a string summary of all methods for the type.
// If type information exists for the methods, it is used to determine function parameters.
// If the type does not have any methods, an empty string is returned.
func MethodDef(typ *GoType) string {
if len(typ.Methods) == 0 {
return ""
}
var buf string
for i, m := range typ.Methods {
if i > 0 {
buf += "\n"
}
if m.Type != nil {
buf += fmt.Sprintf("func (%s) %s%s", typ.Name, m.Name, m.Type.String()[4:])
} else {
buf += fmt.Sprintf("func (%s) %s()", typ.Name, m.Name)
}
}
return buf
}
// TypeMethod is description of a method owned by the GoType.
type TypeMethod struct {
// Name is the string name for the method.
Name string
// Type is the specific function type for the method.
// This can be nil. If it is nil, the method is not part of an
// implementation of a interface or it is not exported.
Type *GoType
// IfaceCallOffset is the offset from the beginning of the .text section
// where the function code starts. According to code comments in the
// standard library, it is used for interface calls.
// Can be 0 if the code is not called in the binary and was optimized out
// by the compiler or linker.
IfaceCallOffset uint64
// FuncCallOffset is the offset from the beginning of the .text section
// where the function code starts. According to code comments in the
// standard library, it is used for normal method calls.
// Can be 0 if the code is not called in the binary and was optimized out
// by the compiler or linker.
FuncCallOffset uint64
}
/*
Size: 32 or 48
type _type struct {
size uintptr 4 or 8
ptrdata uintptr 4 or 8
hash uint32 4
tflag tflag 1
align uint8 1
fieldalign uint8 1
kind uint8 1
alg *typeAlg 4 or 8
gcdata *byte 4 or 8
str nameOff 4
ptrToThis typeOff 4
}
*/
func typeParse(types map[uint64]*GoType, fileInfo *FileInfo, offset uint64, sectionData []byte, sectionBaseAddr uint64) *GoType {
typ, ok := types[offset+sectionBaseAddr]
if ok {
return typ
}
typ = new(GoType)
// XXX: This is to catch bad parsing. The current parser does not handle
// uncommon functions correctly. This ensures an out of bounds read does
// not occur.
if offset > uint64(len(sectionData)) {
return nil
}
r := bytes.NewReader(sectionData[offset:])
// Parse size
off := typeOffset(fileInfo, _typeFieldSize)
r.Seek(off, io.SeekStart)
_, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
// Parse kind
off = typeOffset(fileInfo, _typeFieldKind)
// Defined as uint8
var k uint8
r.Seek(off, io.SeekStart)
binary.Read(r, fileInfo.ByteOrder, &k)
typ.Kind = reflect.Kind(k & kindMask)
// Parse flag
off = typeOffset(fileInfo, _typeFieldFlag)
// Defined as uint8
var f uint8
r.Seek(off, io.SeekStart)
binary.Read(r, fileInfo.ByteOrder, &f)
typ.flag = f
// Parse nameOff
off = typeOffset(fileInfo, _typeFieldStr)
r.Seek(off, io.SeekStart)
ptrN, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
if ptrN != 0 {
typ.Name = parseString(fileInfo, ptrN, sectionBaseAddr, sectionData)
}
typ.Addr = offset + sectionBaseAddr
types[typ.Addr] = typ
// Legacy types has a field with a pointer to the uncommonType.
// The flags location is unused, hence 0, so the parsing of the uncommonType
// is skipped below. So instead, if the binary uses legacy types parse it now.
// Pointer is right after the string pointer.
off = typeOffset(fileInfo, _typeFieldStr) + int64(fileInfo.WordSize)
r.Seek(off, io.SeekStart)
ptr, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
if ptr != 0 {
// Since we don't know if the struct is located before or after this type,
// create a new reader.
ur := bytes.NewReader(sectionData)
ur.Seek(int64(ptr-sectionBaseAddr), io.SeekStart)
parseUncommonType(typ, ur, fileInfo, sectionData, sectionBaseAddr, types)
}
// Parse extra fields
off = typeOffset(fileInfo, _typeFieldEnd)
r.Seek(off, io.SeekStart)
switch typ.Kind {
case reflect.Ptr:
ptr, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
typ.PtrResolvAddr = ptr
if ptr != 0 {
c := typeParse(types, fileInfo, ptr-sectionBaseAddr, sectionData, sectionBaseAddr)
typ.Element = c
}
case reflect.Struct:
// Parse struct fields
fieldptr, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
numfield, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
// Eat cap
_, err = readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
// Parse methods
if typ.flag&tflagUncommon != 0 {
parseUncommonType(typ, r, fileInfo, sectionData, sectionBaseAddr, types)
}
// Parse fields
typ.Fields = make([]*GoType, numfield)
secR := bytes.NewReader(sectionData)
for i := 0; i < int(numfield); i++ {
var fieldName string
var tag string
o := int64(fieldptr + uint64(i*5*fileInfo.WordSize) - sectionBaseAddr)
secR.Seek(o, io.SeekStart)
nptr, err := readUIntTo64(secR, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
ppp, err := readUIntTo64(secR, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
if ppp != 0 {
pps := parseString(fileInfo, ppp, sectionBaseAddr, sectionData)
if pps != "" {
typ.PackagePath = pps
}
}
tptr, err := readUIntTo64(secR, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
tagptr, err := readUIntTo64(secR, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
if tagptr != 0 {
tag = parseString(fileInfo, tagptr, sectionBaseAddr, sectionData)
}
uptr, err := readUIntTo64(secR, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
gt := typeParse(types, fileInfo, tptr-sectionBaseAddr, sectionData, sectionBaseAddr)
// Make a copy
field := *gt
fieldName = parseString(fileInfo, nptr, sectionBaseAddr, sectionData)
field.FieldName = fieldName
if tag != "" {
field.FieldTag = tag
}
// Older versions has no field name for anonymous fields. New versions
// uses a bit flag on the offset.
field.FieldAnon = fieldName == "" || uptr&1 != 0
typ.Fields[i] = &field
}
case reflect.Array:
elementAddr, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
if elementAddr != 0 {
e := typeParse(types, fileInfo, elementAddr-sectionBaseAddr, sectionData, sectionBaseAddr)
typ.Element = e
}
// Read and skip slice type
_, err = readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
// Read length
l, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
typ.Length = int(l)
case reflect.Slice:
elementAddr, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
if elementAddr != 0 {
e := typeParse(types, fileInfo, elementAddr-sectionBaseAddr, sectionData, sectionBaseAddr)
typ.Element = e
}
case reflect.Chan:
elementAddr, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
if elementAddr != 0 {
e := typeParse(types, fileInfo, elementAddr-sectionBaseAddr, sectionData, sectionBaseAddr)
typ.Element = e
}
// Direction
d, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
typ.ChanDir = ChanDir(int(d))
case reflect.Map:
keyAddr, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
if keyAddr != 0 {
k := typeParse(types, fileInfo, keyAddr-sectionBaseAddr, sectionData, sectionBaseAddr)
typ.Key = k
}
elementAddr, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
if elementAddr != 0 {
e := typeParse(types, fileInfo, elementAddr-sectionBaseAddr, sectionData, sectionBaseAddr)
typ.Element = e
}
case reflect.Func:
// bool plus padding.
dotdotdot, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
typ.IsVariadic = dotdotdot > uint64(0)
// One for args and one for returns
rtypes := make([]uint64, 2)
typelens := make([]uint64, 2)
for i := 0; i < 2; i++ {
p, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
continue
}
rtypes[i] = p
l, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
continue
}
typelens[i] = l
// Eat cap
_, err = readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
println("Error when reading padding:", err)
return nil
}
}
// Full section reader
sr := bytes.NewReader(sectionData)
// Parse the arg types and result types.
for i := 0; i < 2; i++ {
if rtypes[i] == 0 {
continue
}
_, err = sr.Seek(int64(rtypes[i]-sectionBaseAddr), io.SeekStart)
if err != nil {
continue
}
for j := 0; j < int(typelens[i]); j++ {
p, err := readUIntTo64(sr, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
continue
}
if p == 0 {
continue
}
t := typeParse(types, fileInfo, p-sectionBaseAddr, sectionData, sectionBaseAddr)
if i == 0 {
typ.FuncArgs = append(typ.FuncArgs, t)
} else {
typ.FuncReturnVals = append(typ.FuncReturnVals, t)
}
}
}
case reflect.Interface:
ptrMethods, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
numMethods, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
// Eat cap
_, err = readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
// Parse imethods
secR := bytes.NewReader(sectionData)
imethSize := uint64(2 * intSize32)
int32ptr := true
if GoVersionCompare(fileInfo.goversion.Name, "go1.7beta1") < 0 {
imethSize = uint64(3 * fileInfo.WordSize)
int32ptr = fileInfo.WordSize == intSize32
}
for i := 0; i < int(numMethods); i++ {
meth := new(TypeMethod)
// All fields has the size of int32
secR.Seek(int64(ptrMethods+uint64(i)*imethSize-sectionBaseAddr), io.SeekStart)
nameOff, err := readUIntTo64(secR, fileInfo.ByteOrder, int32ptr)
if err != nil {
continue
}
if nameOff != 0 {
meth.Name = parseString(fileInfo, nameOff, sectionBaseAddr, sectionData)
}
pkgPathPtr, err := readUIntTo64(secR, fileInfo.ByteOrder, int32ptr)
if err != nil {
continue
}
if pkgPathPtr != 0 {
pkgPathStr := parseString(fileInfo, pkgPathPtr, sectionBaseAddr, sectionData)
if pkgPathStr != "" {
typ.PackagePath = pkgPathStr
}
}
typeOff, err := readUIntTo64(secR, fileInfo.ByteOrder, int32ptr)
if err != nil {
continue
}
if typeOff != 0 {
typeOff = typeOff - sectionBaseAddr
meth.Type = typeParse(types, fileInfo, typeOff, sectionData, sectionBaseAddr)
}
typ.Methods = append(typ.Methods, meth)
}
}
return typ
}
func parseString(fileInfo *FileInfo, off, base uint64, baseData []byte) string {
if off == 0 {
return ""
}
r := bytes.NewReader(baseData)
r.Seek(int64(off-base), io.SeekStart)
h, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return ""
}
l, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return ""
}
if h == 0 || l == 0 {
return ""
}
str := string(baseData[h-base : h-base+l])
return str
}
func parseUncommonType(typ *GoType, r *bytes.Reader, fileInfo *FileInfo, sectionData []byte, sectionBaseAddr uint64, types map[uint64]*GoType) {
pname, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return
}
if pname != 0 {
n := parseString(fileInfo, pname, sectionBaseAddr, sectionData)
if typ.Name == "" && n != "" {
typ.Name = n
}
}
ppkg, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return
}
if ppkg != 0 {
p := parseString(fileInfo, ppkg, sectionBaseAddr, sectionData)
if typ.PackagePath == "" && p != "" {
typ.PackagePath = p
}
}
typ.Methods = parseMethods(r, fileInfo, sectionData, sectionBaseAddr, types)
}
// The methods must start at the readers current location.
func parseMethods(r *bytes.Reader, fileInfo *FileInfo, sectionData []byte, sectionBaseAddr uint64, types map[uint64]*GoType) []*TypeMethod {
pdata, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
numMeth, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
methods := make([]*TypeMethod, numMeth)
r.Seek(int64(pdata-sectionBaseAddr), io.SeekStart)
for i := 0; i < int(numMeth); i++ {
m := &TypeMethod{}
p, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
m.Name = parseString(fileInfo, p, sectionBaseAddr, sectionData)
// Eat package path
_, err = readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
// mtyp
mtype, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
if mtype != 0 {
m.Type = typeParse(types, fileInfo, mtype-sectionBaseAddr, sectionData, sectionBaseAddr)
}
// typ
p, err = readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
if p != 0 {
// Parse it so we capture it in the global type list.
typeParse(types, fileInfo, p-sectionBaseAddr, sectionData, sectionBaseAddr)
}
// ifn
ifn, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
m.IfaceCallOffset = ifn
// tfn
tfn, err := readUIntTo64(r, fileInfo.ByteOrder, fileInfo.WordSize == intSize32)
if err != nil {
return nil
}
m.FuncCallOffset = tfn
methods[i] = m
}
return methods
}
func typeOffset(fileInfo *FileInfo, field _typeField) int64 {
intSize := int64(intSize64)
if fileInfo.WordSize == intSize32 {
intSize = intSize32
}
switch field {
case _typeFieldSize:
return 0
case _typeFieldKind:
return 2*intSize + 4 + 3
case _typeFieldStr:
return 4*intSize + 4 + 4
case _typeFieldFlag:
return 2*intSize + 4
case _typeFieldEnd:
if GoVersionCompare(fileInfo.goversion.Name, "go1.6beta1") < 0 {
return 8*intSize + 8
}
if GoVersionCompare(fileInfo.goversion.Name, "go1.7beta1") < 0 {
return 7*intSize + 8
}
return 4*intSize + 16
default:
return -1
}
}