forked from pkujhd/goloader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtype.go
413 lines (383 loc) · 12.1 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
package goloader
import (
"cmd/objfile/goobj"
"cmd/objfile/obj"
"cmd/objfile/objabi"
"fmt"
"reflect"
"runtime"
"strings"
"unsafe"
)
type tflag uint8
// See runtime/type.go _typePair
type _typePair struct {
t1 *_type
t2 *_type
}
// See reflect/value.go emptyInterface
type emptyInterface struct {
_type *_type
data unsafe.Pointer
}
type nonEmptyInterface struct {
// see ../runtime/iface.go:/Itab
itab *itab
word unsafe.Pointer
}
func efaceOf(ep *interface{}) *emptyInterface {
return (*emptyInterface)(unsafe.Pointer(ep))
}
// See reflect/value.go sliceHeader
type sliceHeader struct {
Data uintptr
Len int
Cap int
}
// Method on non-interface type
type method struct {
name nameOff // name of method
mtyp typeOff // method type (without receiver)
ifn textOff // fn used in interface call (one-word receiver)
tfn textOff // fn used for normal method call
}
type imethod struct {
name nameOff
ityp typeOff
}
type interfacetype struct {
typ _type
pkgpath name
mhdr []imethod
}
type name struct {
bytes *byte
}
func (t *_type) uncommon() *uncommonType { return _uncommon(t) }
func (t *_type) nameOff(off nameOff) name { return _nameOff(t, off) }
func (t *_type) typeOff(off typeOff) *_type { return _typeOff(t, off) }
func (n name) name() string { return _name(n) }
func (n name) pkgPath() string { return _pkgPath(n) }
func (n name) isExported() bool { return _isExported(n) }
func (t *uncommonType) methods() []method { return _methods(t) }
func (t *_type) Kind() reflect.Kind { return _Kind(t) }
func (t *_type) Elem() *_type { return fromRType(_Elem(t)) }
// This replaces local package names with import paths, including where the package name doesn't match the last part of the import path e.g.
//
// import "github.com/org/somepackage/v4" + somepackage.SomeStruct
// => github.com/org/somepackage/v4.SomeStruct
func resolveFullyQualifiedSymbolName(t *_type) string {
typ := AsRType(t)
// go.shape is a special builtin package whose name shouldn't be escaped
pkgPath := unescapeGoShapePkg(objabi.PathToPrefix(t.PkgPath()))
name := typ.Name()
if name == "" {
name = nameFromTypeString(t)
}
var maybeStar string
if typ.String()[0] == '*' {
maybeStar = "*"
}
if pkgPath != "" && name != "" {
if strings.HasPrefix(pkgPath, "go.shape") { // go.shape Name()s don't necessarily match the String()
return typ.String()
}
return maybeStar + pkgPath + "." + name
}
switch t.Kind() {
case reflect.Ptr:
return "*" + resolveFullyQualifiedSymbolName(fromRType(typ.Elem()))
case reflect.Struct:
if typ.NumField() == 0 {
return typ.String()
}
fields := make([]string, typ.NumField())
for i := 0; i < typ.NumField(); i++ {
fieldName := typ.Field(i).Name + " "
if typ.Field(i).Anonymous {
fieldName = ""
}
fieldPkgPath := ""
if typ.Field(i).PkgPath != "" && !typ.Field(i).Anonymous {
fieldPkgPath = unescapeGoShapePkg(objabi.PathToPrefix(typ.Field(i).PkgPath)) + "."
}
fieldStructTag := ""
if typ.Field(i).Tag != "" {
fieldStructTag = fmt.Sprintf(" %q", string(typ.Field(i).Tag))
}
fields[i] = fmt.Sprintf("%s%s%s%s", fieldPkgPath, fieldName, resolveFullyQualifiedSymbolName(fromRType(typ.Field(i).Type)), fieldStructTag)
}
return fmt.Sprintf("struct { %s }", strings.Join(fields, "; "))
case reflect.Map:
return fmt.Sprintf("map[%s]%s", resolveFullyQualifiedSymbolName(fromRType(typ.Key())), resolveFullyQualifiedSymbolName(fromRType(typ.Elem())))
case reflect.Chan:
switch reflect.ChanDir(typ.ChanDir()) {
case reflect.BothDir:
return fmt.Sprintf("chan %s", resolveFullyQualifiedSymbolName(fromRType(typ.Elem())))
case reflect.RecvDir:
return fmt.Sprintf("<-chan %s", resolveFullyQualifiedSymbolName(fromRType(typ.Elem())))
case reflect.SendDir:
return fmt.Sprintf("chan<- %s", resolveFullyQualifiedSymbolName(fromRType(typ.Elem())))
}
case reflect.Slice:
return fmt.Sprintf("[]%s", resolveFullyQualifiedSymbolName(fromRType(typ.Elem())))
case reflect.Array:
return fmt.Sprintf("[%d]%s", typ.Len(), resolveFullyQualifiedSymbolName(fromRType(typ.Elem())))
case reflect.Func:
ins := make([]string, typ.NumIn())
outs := make([]string, typ.NumOut())
for i := 0; i < typ.NumIn(); i++ {
ins[i] = resolveFullyQualifiedSymbolName(fromRType(typ.In(i)))
if i == typ.NumIn()-1 && typ.IsVariadic() {
ins[i] = "..." + resolveFullyQualifiedSymbolName(fromRType(typ.In(i).Elem()))
}
}
for i := 0; i < typ.NumOut(); i++ {
outs[i] = resolveFullyQualifiedSymbolName(fromRType(typ.Out(i)))
}
funcName := "func(" + strings.Join(ins, ", ") + ")"
if len(outs) > 0 {
funcName += " "
}
if len(outs) > 1 {
funcName += "("
}
funcName += strings.Join(outs, ", ")
if len(outs) > 1 {
funcName += ")"
}
return funcName
case reflect.Interface:
if goobj.BuiltinIdx(TypePrefix+typ.String(), int(obj.ABI0)) != -1 {
// must be a builtin,
return typ.String()
}
if typ.NumMethod() == 0 {
return typ.String()
}
methods := make([]string, typ.NumMethod())
ifaceT := (*interfacetype)(unsafe.Pointer(t))
for i := 0; i < typ.NumMethod(); i++ {
methodType := _typeOff(t, ifaceT.mhdr[i].ityp)
methodName := _nameOff(t, ifaceT.mhdr[i].name).name()
methods[i] = fmt.Sprintf("%s(%s", methodName, strings.TrimPrefix(resolveFullyQualifiedSymbolName(methodType), "func("))
}
reflect.TypeOf(0)
return fmt.Sprintf("interface { %s }", strings.Join(methods, "; "))
default:
if goobj.BuiltinIdx(TypePrefix+typ.String(), int(obj.ABI0)) != -1 {
// must be a builtin,
return typ.String()
}
switch typ.String() {
case "int", "uint", "struct {}", "interface {}":
return typ.String()
}
panic("unexpected builtin type: " + typ.String())
}
return ""
}
func nameFromTypeString(t *_type) string {
typ := AsRType(t)
s := typ.String()
i := len(s) - 1
sqBrackets := 0
for i >= 0 && (s[i] != '.' || sqBrackets != 0) {
switch s[i] {
case ']':
sqBrackets++
case '[':
sqBrackets--
}
i--
}
return s[i+1:]
}
func fullyQualifiedMethodName(t *_type, method method) string {
methodName := t.nameOff(method.name).name()
// t.PkgPath() will always return a path, whereas AsRType(t).PkgPath() will only return if flag TNamed is set
pkgPath := unescapeGoShapePkg(objabi.PathToPrefix(t.PkgPath()))
name := nameFromTypeString(t)
if name == "" {
panic("method on type with no name")
}
if t.Kind() == reflect.Pointer {
return pkgPath + ".(*" + name + ")." + methodName
}
return pkgPath + "." + name + "." + methodName
}
func unescapeGoShapePkg(pkgPath string) string {
if pkgPath == "go%2eshape" {
return "go.shape"
}
return pkgPath
}
func symbolIsVariant(name string) (string, bool) {
const dot = "·"
const noAlgPrefix = TypePrefix + "noalg."
if strings.HasPrefix(name, TypePrefix+"struct {") || strings.HasPrefix(name, TypePrefix+"*struct {") {
// Anonymous structs might embed variant types, so these will need parsing first
ptr := false
if strings.HasPrefix(name, TypePrefix+"*struct {") {
ptr = true
}
fieldsStr := strings.TrimPrefix(name, TypePrefix+"struct { ")
fieldsStr = strings.TrimPrefix(name, TypePrefix+"*struct { ")
fieldsStr = strings.TrimSuffix(fieldsStr, " }")
fields := strings.Split(fieldsStr, "; ")
isVariant := false
for j, field := range fields {
var typeName string
var typeNameIndex int
fieldTypeTag := strings.SplitN(field, " ", 3)
// could be anonymous, or tagless, or both - we want to operate on the type
switch len(fieldTypeTag) {
case 1:
// Anonymous, tagless - just a type
typeName = fieldTypeTag[0]
case 2:
// could be a name + type, or type + tag
if strings.HasPrefix(fieldTypeTag[1], "\"") || strings.HasPrefix(fieldTypeTag[1], "`") {
// type + tag
typeName = fieldTypeTag[0]
} else {
// name + type
typeName = fieldTypeTag[1]
typeNameIndex = 1
}
case 3:
// Name + type + tag
typeName = fieldTypeTag[1]
typeNameIndex = 1
}
i := len(typeName)
for i > 0 && typeName[i-1] >= '0' && typeName[i-1] <= '9' {
i--
}
if i >= len(dot) && typeName[i-len(dot):i] == dot {
isVariant = true
fieldTypeTag[typeNameIndex] = typeName[:i-len(dot)]
fields[j] = strings.Join(fieldTypeTag, " ")
}
}
if isVariant {
if ptr {
return TypePrefix + "*struct { " + strings.Join(fields, "; ") + " }", true
}
return TypePrefix + "struct { " + strings.Join(fields, "; ") + " }", true
}
return "", false
} else {
// need to double check for function scoped types which get a ·N suffix added, and also type.noalg.* variants
i := len(name)
for i > 0 && name[i-1] >= '0' && name[i-1] <= '9' {
i--
}
if i >= len(dot) && name[i-len(dot):i] == dot {
return name[:i-len(dot)], true
} else if strings.HasPrefix(name, noAlgPrefix) {
return TypePrefix + strings.TrimPrefix(name, noAlgPrefix), true
}
return "", false
}
}
func funcPkgPath(funcName string) string {
funcName = strings.TrimPrefix(funcName, TypeDoubleDotPrefix+"eq.")
// Anonymous struct methods can't have a package
if strings.HasPrefix(funcName, "go"+ObjSymbolSeparator+"struct {") || strings.HasPrefix(funcName, "go"+ObjSymbolSeparator+"(*struct {") {
return ""
}
lastSlash := strings.LastIndexByte(funcName, '/')
if lastSlash == -1 {
lastSlash = 0
}
// Generic dictionaries
firstDict := strings.Index(funcName, "..dict")
if firstDict > 0 {
return funcName[:firstDict]
} else {
// Methods on structs embedding structs from other packages look funny, e.g.:
// regexp.(*onePassInst).regexp/syntax.op
firstBracket := strings.LastIndex(funcName, ".(")
if firstBracket > 0 && lastSlash > firstBracket {
lastSlash = firstBracket
}
}
dot := lastSlash
for ; dot < len(funcName) && funcName[dot] != '.' && funcName[dot] != '(' && funcName[dot] != '['; dot++ {
}
pkgPath := funcName[:dot]
return strings.TrimPrefix(strings.TrimPrefix(pkgPath, TypePrefix+".eq."), "[...]")
}
func (t *_type) PkgPath() string {
ut := t.uncommon()
if ut == nil {
return EmptyString
}
return t.nameOff(ut.pkgpath).name()
}
func RegTypes(symPtr map[string]uintptr, interfaces ...interface{}) {
for _, inter := range interfaces {
v := reflect.ValueOf(inter)
regType(symPtr, v)
if v.Kind() == reflect.Ptr {
regType(symPtr, v.Elem())
}
}
}
func regType(symPtr map[string]uintptr, v reflect.Value) {
inter := v.Interface()
if v.Kind() == reflect.Func && getFunctionPtr(inter) != 0 {
symPtr[runtime.FuncForPC(v.Pointer()).Name()] = getFunctionPtr(inter)
} else {
header := (*emptyInterface)(unsafe.Pointer(&inter))
t := header._type
registerType(t, symPtr, map[string]struct{}{})
}
}
func buildModuleTypeHash(module *moduledata, typeHash map[uint32][]*_type) {
for _, tl := range module.typelinks {
var t *_type
t = (*_type)(adduintptr(module.types, int(tl)))
registerTypeHash(t, typeHash)
}
}
func registerTypeHash(t *_type, typeHash map[uint32][]*_type) {
if t.Kind() == reflect.Invalid {
panic("Unexpected invalid kind during registration!")
}
tlist := typeHash[t.hash]
for _, tcur := range tlist {
if tcur == t {
return
}
}
typeHash[t.hash] = append(tlist, t)
switch t.Kind() {
case reflect.Ptr, reflect.Chan, reflect.Array, reflect.Slice:
// Indirect pointers + elems
element := t.Elem()
registerTypeHash(element, typeHash)
case reflect.Func:
typ := AsType(t)
for i := 0; i < typ.NumIn(); i++ {
registerTypeHash(toType(typ.In(i)), typeHash)
}
for i := 0; i < typ.NumOut(); i++ {
registerTypeHash(toType(typ.Out(i)), typeHash)
}
case reflect.Struct:
typ := AsType(t)
for i := 0; i < typ.NumField(); i++ {
registerTypeHash(toType(typ.Field(i).Type), typeHash)
}
case reflect.Map:
mt := (*mapType)(unsafe.Pointer(t))
registerTypeHash(mt.key, typeHash)
registerTypeHash(mt.elem, typeHash)
case reflect.Bool, reflect.Int, reflect.Uint, reflect.Int64, reflect.Uint64, reflect.Int32, reflect.Uint32, reflect.Int16, reflect.Uint16, reflect.Int8, reflect.Uint8, reflect.Float64, reflect.Float32, reflect.String, reflect.UnsafePointer, reflect.Uintptr, reflect.Complex64, reflect.Complex128, reflect.Interface:
// Already added above
default:
panic(fmt.Sprintf("registerTypeHash found unexpected type (kind %s): ", t.Kind()))
}
}