/
typeinfo.go
126 lines (110 loc) · 3.11 KB
/
typeinfo.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
package typeinfo
import (
"fmt"
"reflect"
"regexp"
"strconv"
)
type Relocate struct {
Addr int
Width int
Reloctype int
Name string
NameOffset int
}
type TypeInfo struct {
TypeName string
TypeKind reflect.Kind
Data []byte
RelocateMap map[string]*Relocate
}
func NewTypeInfo(name string, Content []string) *TypeInfo {
// get size
sizeString := typeSizeRegex.FindStringSubmatch(Content[0])[1]
size, err := strconv.Atoi(sizeString)
if err != nil {
panic(err)
}
//fmt.Println(size)
// get binary data
data := make([]byte, 0, size)
relocates := make([]*Relocate, 0, 10)
relocateMap := make(map[string]*Relocate, 10) // key: Relocate.addr+Relocate.width, example 0+8
//relocates := make([]Relocate, 0, 10)
for i := 1; i < len(Content); i++ {
switch {
case typeBinaryDataHeaderRegex.MatchString(Content[i]):
bs := typeBinaryDataRegex.FindStringSubmatch(Content[i])
//fmt.Println(bs)
for j := 0; j < len(bs[1]); j += 3 {
d, err := strconv.ParseUint(bs[1][j:j+2], 16, 8)
if err != nil {
panic(err)
}
data = append(data, byte(d))
}
case typeRelocateHeaderRegex.MatchString(Content[i]):
rs := typeRelocateRegex.FindStringSubmatch(Content[i])
//fmt.Println(Content[i], rs)
r := &Relocate{
Addr: stringToInt(rs[1]),
Width: stringToInt(rs[2]),
Reloctype: stringToInt(rs[3]),
Name: rs[4],
NameOffset: stringToInt(rs[5]),
}
relocates = append(relocates, r)
position := fmt.Sprintf("%d+%d", r.Addr, r.Width)
relocateMap[position] = r
}
}
//fmt.Println(data)
//fmt.Println(relocates)
typeKind := getTypeKindFromTypeData(data)
ti := &TypeInfo{TypeName: name, TypeKind: typeKind, Data: data, RelocateMap: relocateMap}
return ti
}
func (ti *TypeInfo) getRelocate(addr int, width int) *Relocate {
return ti.RelocateMap[fmt.Sprintf("%d+%d", addr, width)]
}
func (ti *TypeInfo) getRelocateFullName(addr int, width int) string {
if r, ok := ti.RelocateMap[fmt.Sprintf("%d+%d", addr, width)]; ok {
return fmt.Sprintf("%s+%d", r.Name, r.NameOffset)
} else {
return "unknown"
}
}
func (ti *TypeInfo) getUintFromData(offset, length int) uint {
return byteToInt(ti.Data[offset : offset+length])
}
func (ti *TypeInfo) Display() {
fmt.Printf("\n%s is a %s, and type size is %d\n", ti.TypeName, ti.TypeKind, len(ti.Data))
d := NewDisplayer(ti)
d.display()
}
func getTypeKindFromTypeData(data []byte) reflect.Kind {
return reflect.Kind(data[23] & 0b11111)
}
func byteToInt(s []byte) (u uint) {
if len(s) == 0 {
return
}
lastIndex := len(s) - 1
u = uint(s[lastIndex])
for i := lastIndex - 1; i >= 0; i-- {
u = u<<8 + uint(s[i])
}
return u
}
func stringToInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}
var typeSizeRegex = regexp.MustCompile(`^\S.+?size=(\d+)`)
var typeBinaryDataHeaderRegex = regexp.MustCompile(`^\s+0x`)
var typeBinaryDataRegex = regexp.MustCompile(`0x[abcdef\d]+\s((?:[abcdef\d][abcdef\d]\s)+)\s`)
var typeRelocateHeaderRegex = regexp.MustCompile(`^\s+rel`)
var typeRelocateRegex = regexp.MustCompile(`^\s+rel\s(\d+)\+(\d+)\st=(-?\d+)\s(.+)\+(\d+)`)