-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
311 lines (258 loc) · 12.8 KB
/
parse.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
package x86_64
import (
"log"
"reflect"
"strings"
"github.com/vsoch/gosmeagle/descriptor"
"github.com/vsoch/gosmeagle/parsers/file"
"github.com/vsoch/gosmeagle/pkg/debug/dwarf"
)
// ParseFunction parses a function parameters
func ParseFunction(f *file.File, symbol file.Symbol, entry *file.DwarfEntry, disasm *file.Disasm, isCallSite bool) descriptor.FunctionDescription {
// Prepare list of function parameters
params := []descriptor.Parameter{}
// Keep a record of names and components we've seen
seen := map[string]file.Component{}
// Create an allocator for the function
allocator := NewRegisterAllocator()
// Data is needed by typedef to look up full struct, class, or union info
data := (*entry).GetData()
// Get the direction for the function
direction := GetDirection(symbol.GetName(), isCallSite)
// A return value will be included here with name "return"
for _, c := range (*entry).GetComponents() {
indirections := int64(0)
// Parse the parameter!
param := ParseParameter(c, data, symbol, &indirections, &seen, allocator, isCallSite)
if param != nil {
params = append(params, param)
}
}
return descriptor.FunctionDescription{Parameters: params, Name: symbol.GetName(), Type: "Function", Direction: direction}
}
// ParseParameter will parse a general parameter
func ParseParameter(c file.Component, d *dwarf.Data, symbol file.Symbol, indirections *int64, seen *map[string]file.Component,
a *RegisterAllocator, isCallSite bool) descriptor.Parameter {
// Parse parameter based on the type
switch c.Class {
case "Pointer":
return ParsePointerType(c, d, symbol, indirections, seen, a, isCallSite)
case "Qualified":
return ParseQualifiedType(c, d, symbol, indirections, seen, a, isCallSite)
case "Basic", "Uint", "Int", "Float", "Char", "Uchar", "Complex", "Bool", "Unspecified", "Address":
return ParseBasicType(c, d, symbol, indirections, seen, a, isCallSite)
case "Enum":
return ParseEnumType(c, symbol, indirections, a, isCallSite)
case "Typedef":
convert := (*d).StructCache[c.Name]
if convert != nil {
return ParseStructure(convert, d, symbol, indirections, seen, a, isCallSite)
}
return ParseTypedef(c, symbol, indirections, seen, isCallSite)
case "Structure":
convert := c.RawType.(*dwarf.StructType)
return ParseStructure(convert, d, symbol, indirections, seen, a, isCallSite)
case "Array":
return ParseArray(c, d, symbol, indirections, seen, a, isCallSite)
// A nested function here appears to be anonymous (e.g., { Function -1 func(*char) void})
case "", "Undefined", "Function":
return nil
default:
log.Fatalf("Unparsed parameter class", c.Class)
}
return nil
}
// ParseTypeDef parses a type definition
func ParseTypedef(c file.Component, symbol file.Symbol, indirections *int64, seen *map[string]file.Component, isCallSite bool) descriptor.Parameter {
convert := c.RawType.(*dwarf.TypedefType)
direction := GetDirection(convert.Name, isCallSite)
return descriptor.BasicParameter{Name: convert.Name, Size: convert.CommonType.Size(), Type: convert.Type.Common().Name,
Direction: direction, Class: "TypeDef"}
}
// ParseEnumType parses an enum type
func ParseEnumType(c file.Component, symbol file.Symbol, indirections *int64, a *RegisterAllocator, isCallSite bool) descriptor.Parameter {
convert := c.RawType.(*dwarf.EnumType)
// Get a list of constants (name and value)
constants := map[string]int64{}
for _, value := range convert.Val {
constants[value.Name] = value.Val
}
// Get the location
enumClass := ClassifyEnum(convert, &c, indirections)
loc := a.GetRegisterString(enumClass.Lo, enumClass.Hi, c.Size, c.Class)
// If it's a return value, it's an export, otherwise import (and TODO we need callsite)
direction := GetDirection(convert.EnumName, isCallSite)
return descriptor.EnumParameter{Name: convert.EnumName, Type: c.Type, Class: c.Class, Location: loc,
Size: convert.CommonType.ByteSize, Length: len(convert.Val), Constants: constants, Direction: direction}
}
// Get direction determines the direction (export or import)
func GetDirection(name string, isCallSite bool) string {
// if is return and not call site, should be export
if name == "return" && !isCallSite {
return "export"
}
// if is return and call site, should be import
if name == "return" && isCallSite {
return "import"
}
// If it's not a return and a callsite, should be export
if isCallSite {
return "export"
}
return "import"
}
// ParsePointerType parses a pointer and returns an abi description for a function parameter
func ParsePointerType(c file.Component, d *dwarf.Data, symbol file.Symbol, indirections *int64, seen *map[string]file.Component,
a *RegisterAllocator, isCallSite bool) descriptor.Parameter {
// Convert Original back to Pointer Type to get underlying type
convert := c.RawType.(*dwarf.PtrType)
// Default will return nil (no underlying type to continue parsing)
underlyingType := ParseParameter(file.Component{}, d, nil, indirections, seen, a, isCallSite)
// Only parse things we haven't seen
seenComponent, ok := (*seen)[convert.Type.Common().Name]
if !ok {
comp := file.Component{Name: convert.Type.Common().Name, Class: file.GetStringType(convert.Type),
Size: convert.Type.Size(), RawType: convert.Type}
// If we've hit another pointer, this is an indirection
(*indirections) += 1
// Mark as seen, and parse the underlying type
(*seen)[convert.Type.Common().Name] = comp
underlyingType = ParseParameter(comp, d, nil, indirections, seen, a, isCallSite)
}
// Default direction for a library symbol is import
// But what about call sites? Why are they all imports?
direction := GetDirection(c.Name, isCallSite)
// On x86, all pointers are the same ABI class
ptrClass := ClassifyPointer(indirections)
// Allocate space for the pointer (NOT the underlying type)
ptrLoc := a.GetRegisterString(ptrClass.Lo, ptrClass.Hi, seenComponent.Size, seenComponent.Class)
return descriptor.PointerParameter{Name: c.Name, Type: c.Type, Class: c.Class, Location: ptrLoc,
Size: c.Size, Direction: direction, UnderlyingType: underlyingType, Indirections: (*indirections)}
}
/* ParseArray parses an array type
1. For formal function parameters, arrays are pointers and have class "Pointer"
2. For fields of a struct/union/class, arrays are real types and have class "Array"
3 .For callsite parameters, arrays are detected as a real type but will be decayed to a pointer for the function call.
TODO: currently just testing with #2 above
*/
func ParseArray(c file.Component, d *dwarf.Data, symbol file.Symbol, indirections *int64, seen *map[string]file.Component,
a *RegisterAllocator, isCallSite bool) descriptor.Parameter {
convert := c.RawType.(*dwarf.ArrayType)
// Default will return nil (no underlying type to continue parsing)
underlyingType := ParseParameter(file.Component{}, d, nil, indirections, seen, a, isCallSite)
// Only parse things we haven't seen
seenComponent, ok := (*seen)[convert.Type.Common().Name]
if !ok {
comp := file.Component{Name: convert.Type.Common().Name, Class: file.GetStringType(convert.Type),
Size: convert.Type.Size(), RawType: convert.Type}
// If we've hit another pointer, this is an indirection
(*indirections) += 1
// Mark as seen, and parse the underlying type
(*seen)[convert.Type.Common().Name] = comp
underlyingType = ParseParameter(comp, d, nil, indirections, seen, a, isCallSite)
}
arrayClass := ClassifyArray(convert, &seenComponent, indirections)
loc := a.GetRegisterString(arrayClass.Lo, arrayClass.Hi, seenComponent.Size, seenComponent.Class)
direction := GetDirection(convert.CommonType.Name, isCallSite)
return descriptor.ArrayParameter{Length: convert.Count, Name: convert.CommonType.Name, Type: convert.Type.String(),
Size: convert.Count * seenComponent.Size, Class: "Array", ItemType: underlyingType, Location: loc, Direction: direction}
}
// ParseStructure parses a structure type
func ParseStructure(convert *dwarf.StructType, d *dwarf.Data, symbol file.Symbol, indirections *int64, seen *map[string]file.Component,
a *RegisterAllocator, isCallSite bool) descriptor.Parameter {
fields := []descriptor.Parameter{}
for _, field := range convert.Field {
c := file.Component{Name: field.Name, Class: file.GetStringType(field.Type),
Size: field.Type.Size(), RawType: field.Type}
newField := ParseParameter(c, d, nil, indirections, seen, a, isCallSite)
if newField != nil {
fields = append(fields, newField)
}
}
direction := GetDirection("", isCallSite)
// Get the location ?
// structClass := ClassifyStruct(convert, &c, indirections)
// loc := a.GetRegisterString(structClass.Lo, structClass.Hi, c.Size, c.Class)
return descriptor.StructureParameter{Fields: fields, Class: strings.Title(convert.Kind), Type: convert.StructName,
Size: convert.CommonType.Size(), Direction: direction}
}
// ParseQualified parses a qualified type (a size and type)
func ParseQualifiedType(c file.Component, d *dwarf.Data, symbol file.Symbol, indirections *int64, seen *map[string]file.Component,
a *RegisterAllocator, isCallSite bool) descriptor.Parameter {
// We can still have a pointer here!
switch c.RawType.(type) {
case *dwarf.PtrType:
return ParsePointerType(c, d, symbol, indirections, seen, a, isCallSite)
case *dwarf.QualType:
convert := c.RawType.(*dwarf.QualType)
direction := GetDirection("", isCallSite)
return descriptor.QualifiedParameter{Size: convert.Type.Size(), Type: convert.Type.String(), Class: "Qual",
Direction: direction}
}
return descriptor.QualifiedParameter{}
}
// ParseBasicType parses a basic type
func ParseBasicType(c file.Component, d *dwarf.Data, symbol file.Symbol, indirections *int64, seen *map[string]file.Component,
a *RegisterAllocator, isCallSite bool) descriptor.Parameter {
direction := GetDirection("", isCallSite)
cls := ClassifyType(&c, indirections)
loc := a.GetRegisterString(cls.Lo, cls.Hi, c.Size, c.Class)
switch c.RawType.(type) {
case *dwarf.IntType:
convert := c.RawType.(*dwarf.IntType)
return descriptor.BasicParameter{Size: convert.CommonType.Size(), Type: convert.CommonType.Name, Class: "Int",
Direction: direction, Name: c.Name, Location: loc}
case *dwarf.FloatType:
convert := c.RawType.(*dwarf.FloatType)
return descriptor.BasicParameter{Size: convert.CommonType.Size(), Type: convert.CommonType.Name, Class: "Float",
Direction: direction, Name: c.Name, Location: loc}
case *dwarf.UintType:
convert := c.RawType.(*dwarf.UintType)
return descriptor.BasicParameter{Size: convert.CommonType.Size(), Type: convert.CommonType.Name, Class: "Uint",
Direction: direction, Name: c.Name, Location: loc}
case *dwarf.UcharType:
convert := c.RawType.(*dwarf.UcharType)
return descriptor.BasicParameter{Size: convert.CommonType.Size(), Type: convert.CommonType.Name, Class: "Uchar",
Direction: direction, Name: c.Name, Location: loc}
case *dwarf.CharType:
convert := c.RawType.(*dwarf.CharType)
return descriptor.BasicParameter{Size: convert.CommonType.Size(), Type: convert.CommonType.Name, Class: "Char",
Direction: direction, Name: c.Name, Location: loc}
case *dwarf.ComplexType:
convert := c.RawType.(*dwarf.ComplexType)
return descriptor.BasicParameter{Size: convert.CommonType.Size(), Type: convert.CommonType.Name, Class: "Complex",
Direction: direction, Name: c.Name, Location: loc}
case *dwarf.BoolType:
convert := c.RawType.(*dwarf.BoolType)
return descriptor.BasicParameter{Size: convert.CommonType.Size(), Type: convert.CommonType.Name, Class: "Bool",
Direction: direction, Name: c.Name, Location: loc}
case *dwarf.UnspecifiedType:
convert := c.RawType.(*dwarf.UnspecifiedType)
return descriptor.BasicParameter{Size: convert.CommonType.Size(), Type: convert.CommonType.Name, Class: "Unspecified",
Direction: direction, Name: c.Name, Location: loc}
case *dwarf.AddrType:
convert := c.RawType.(*dwarf.AddrType)
return descriptor.BasicParameter{Size: convert.CommonType.Size(), Type: convert.CommonType.Name, Class: "Address",
Direction: direction, Name: c.Name, Location: loc}
case *dwarf.PtrType:
return ParsePointerType(c, d, symbol, indirections, seen, a, isCallSite)
case *dwarf.BasicType:
convert := c.RawType.(*dwarf.BasicType)
return descriptor.BasicParameter{Size: convert.CommonType.Size(), Type: convert.CommonType.Name, Direction: direction,
Name: c.Name, Class: c.Class, Location: loc}
default:
log.Fatalf("Type not accounted for:", reflect.TypeOf(c.RawType))
}
return descriptor.BasicParameter{}
}
// ParseVariable parses a global variable
func ParseVariable(f *file.File, symbol file.Symbol, entry *file.DwarfEntry, isCallSite bool) descriptor.VariableDescription {
// We only need one variable
variable := descriptor.VariableDescription{}
// A variable will only have one component for itself
for _, v := range (*entry).GetComponents() {
direction := GetDirection(v.Name, isCallSite)
variable = descriptor.VariableDescription{Name: v.Name, Type: v.Type, Size: v.Size, Direction: direction}
}
return variable
}