-
Notifications
You must be signed in to change notification settings - Fork 10
/
SymbolInfo.cs
493 lines (447 loc) · 17.3 KB
/
SymbolInfo.cs
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
using Dia2Lib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace CruncherSharp
{
public class SymbolInfo
{
public string Name { get; private set; }
public string TypeName { get; set; }
public ulong Size { get; set; }
public ulong NewSize { get; set; }
public ulong EndPadding { get; set; }
public ulong Padding => (ulong)((long)EndPadding + Members.Sum(info => (long)info.PaddingBefore)); // This is the local (intrinsic) padding
public ulong PaddingZonesCount => (ulong)((EndPadding > 0 ? 1 : 0) + Members.Sum(info => info.PaddingBefore > 0 ? 1 : 0));
public ulong? TotalPadding { get; set; } // Includes padding from base classes and members
public ulong NumInstances { get; set; }
public ulong TotalCount { get; set; }
public bool IsAbstract { get; set; }
public bool IsTemplate { get; set; }
public List<SymbolMemberInfo> Members { get; set; }
public List<SymbolFunctionInfo> Functions { get; set; }
public List<SymbolInfo> DerivedClasses { get; set; }
private const string PaddingMarker = "****Padding";
public SymbolInfo(string name, string typeName, ulong size)
{
Name = name;
TypeName = typeName;
Size = size;
EndPadding = 0;
TotalPadding = null;
Members = new List<SymbolMemberInfo>();
Functions = new List<SymbolFunctionInfo>();
IsAbstract = false;
if (Name.Contains("<") && Name.Contains(">"))
IsTemplate = true;
}
private void AddMember(SymbolMemberInfo member)
{
Members.Add(member);
}
private void AddFunction(SymbolFunctionInfo function)
{
Functions.Add(function);
}
private bool ComputeOffsetCollision(int index)
{
return index > 0 && Members[index].Offset == Members[index - 1].Offset;
}
public bool HasVtable
{
get
{
foreach (var member in Members)
{
if (member.Category == SymbolMemberInfo.MemberCategory.VTable)
{
return true;
}
}
return false;
}
}
public bool HasBaseClass
{
get
{
foreach (var member in Members)
{
if (member.Category == SymbolMemberInfo.MemberCategory.Base)
{
return true;
}
}
return false;
}
}
public bool HasMSVCExtraPadding
{
get
{
if (HasBaseClass)
return false;
if (!HasVtable)
return false;
if (Members.Count < 2)
return false;
return Members[1].Size == 8 && Members[1].Offset == 16;
}
}
public bool HasMSVCEmptyBaseClass
{
get
{
var n = 1;
while (n + 2 < Members.Count)
{
if (Members[n - 1].IsBase && Members[n].IsBase && Members[n].Size == 1 && Members[n].Offset > Members[n - 1].Offset && Members[n + 1].Offset > Members[n].Offset)
return true;
n++;
}
return false;
}
}
private ulong ComputePadding(int index)
{
if (index < 1 || index > Members.Count)
{
return 0;
}
if (index < Members.Count && Members[index].AlignWithPrevious)
{
return 0;
}
int previousIndex = index - 1;
ulong biggestSize = Members[previousIndex].Size;
while (Members[previousIndex].AlignWithPrevious && previousIndex > 0)
{
previousIndex--;
if (biggestSize < Members[previousIndex].Size)
biggestSize = Members[previousIndex].Size;
}
ulong currentOffset = index > Members.Count - 1 ? Size : Members[index].Offset;
ulong previousEnd = Members[previousIndex].Offset + biggestSize;
return currentOffset > previousEnd ? currentOffset - previousEnd : 0;
}
private ulong ComputeBitPadding(int index)
{
if (index > Members.Count)
{
return 0;
}
if (!Members[index].BitField)
{
return 0;
}
if (index + 1 < Members.Count)
{
if (Members[index + 1].BitField && Members[index + 1].Offset == Members[index].Offset)
return 0;
}
return (8 * Members[index].Size) - (Members[index].BitPosition + Members[index].BitSize);
}
private ulong ComputeEndPadding()
{
return ComputePadding(Members.Count);
}
public ulong ComputeTotalPadding(SymbolAnalyzer symbolAnalyzer)
{
if (TotalPadding.HasValue)
{
return TotalPadding.Value;
}
TotalPadding = (ulong)((long)Padding + Members.Sum(info =>
{
if (info.AlignWithPrevious)
return 0;
if (info.Category == SymbolMemberInfo.MemberCategory.Member)
return 0;
if (info.TypeName == Name)
return 0; // avoid infinite loops
var referencedInfo = symbolAnalyzer.FindSymbolInfo(info.TypeName);
if (referencedInfo == null)
{
return 0;
}
return (long)referencedInfo.ComputeTotalPadding(symbolAnalyzer);
}));
return TotalPadding.Value;
}
public void UpdateBaseClass(SymbolAnalyzer symbolAnalyzer)
{
foreach (var member in Members)
{
if (member.Category == SymbolMemberInfo.MemberCategory.Base)
{
var referencedInfo = symbolAnalyzer.FindSymbolInfo(member.Name);
if (referencedInfo != null)
{
if (referencedInfo.DerivedClasses == null)
referencedInfo.DerivedClasses = new List<SymbolInfo>();
referencedInfo.DerivedClasses.Add(this);
}
}
}
}
public void CheckOverride()
{
foreach (var function in Functions)
{
if (function.Virtual && function.Category == SymbolFunctionInfo.FunctionCategory.Function && DerivedClasses != null)
{
foreach(var derivedClass in DerivedClasses)
{
if (derivedClass.IsOverloadingFunction(function))
{
function.IsOverloaded = true;
break;
}
}
}
}
}
public void CheckMasking()
{
foreach (var function in Functions)
{
if (function.Virtual == false && function.Category == SymbolFunctionInfo.FunctionCategory.Function && DerivedClasses != null)
{
foreach (var derivedClass in DerivedClasses)
{
derivedClass.CheckMasking(function);
}
}
}
}
private void CheckMasking(SymbolFunctionInfo func)
{
foreach (var function in Functions)
{
if (function.Virtual == false && function.Name == func.Name)
{
function.IsMasking = true;
if (DerivedClasses != null)
{
foreach (var derivedClass in DerivedClasses)
{
derivedClass.CheckMasking(func);
}
}
}
}
}
private bool IsOverloadingFunction(SymbolFunctionInfo func)
{
foreach (var function in Functions)
{
if (function.Name == func.Name)
return true;
}
if (DerivedClasses != null)
{
foreach (var derivedClass in DerivedClasses)
{
if (derivedClass.IsOverloadingFunction(func))
return true;
}
}
return false;
}
public void UpdateTotalCount(SymbolAnalyzer symbolAnalyzer, ulong count)
{
foreach (var member in Members)
{
if (member.Category != SymbolMemberInfo.MemberCategory.VTable)
{
var referencedInfo = symbolAnalyzer.FindSymbolInfo(member.TypeName);
if (referencedInfo != null)
{
referencedInfo.TotalCount += count;
referencedInfo.UpdateTotalCount(symbolAnalyzer,count);
}
}
}
}
public void ProcessChildren(IDiaSymbol symbol)
{
symbol.findChildren(SymTagEnum.SymTagNull, null, 0, out var children);
if (children == null)
return;
foreach (IDiaSymbol child in children)
{
if (child.symTag == (uint)SymTagEnum.SymTagFunction)
{
var functionInfo = ProcessFunction(child);
if (functionInfo != null)
{
AddFunction(functionInfo);
if (functionInfo.IsPure)
IsAbstract = true;
}
}
else
{
var memberInfo = ProcessMember(child);
if (memberInfo != null)
{
AddMember(memberInfo);
}
}
}
// Sort members by offset, recompute padding.
// Sorting is usually not needed (for data fields), but sometimes base class order is wrong.
Members.Sort(SymbolMemberInfo.CompareOffsets);
for (int i = 0; i < Members.Count; ++i)
{
var member = Members[i];
member.AlignWithPrevious = ComputeOffsetCollision(i);
member.PaddingBefore = ComputePadding(i);
member.BitPaddingAfter = ComputeBitPadding(i);
}
EndPadding = ComputeEndPadding();
}
public SymbolMemberInfo ProcessMember(IDiaSymbol symbol)
{
if (symbol.symTag == (uint)SymTagEnum.SymTagVTable)
{
return new SymbolMemberInfo(SymbolMemberInfo.MemberCategory.VTable, string.Empty, string.Empty, 8, 0, (ulong)symbol.offset, symbol.bitPosition);
}
if (symbol.isStatic != 0 || (symbol.symTag != (uint)SymTagEnum.SymTagData && symbol.symTag != (uint)SymTagEnum.SymTagBaseClass))
{
return null;
}
// LocIsThisRel || LocIsNull || LocIsBitField
if (symbol.locationType != 4 && symbol.locationType != 0 && symbol.locationType != 6)
{
return null;
}
var typeSymbol = symbol.type;
var typeName = GetType(typeSymbol);
var symbolName = symbol.name;
var category = SymbolMemberInfo.MemberCategory.Member;
if ((SymTagEnum)symbol.symTag == SymTagEnum.SymTagBaseClass)
category = SymbolMemberInfo.MemberCategory.Base;
else if ((SymTagEnum)typeSymbol.symTag == SymTagEnum.SymTagUDT)
category = SymbolMemberInfo.MemberCategory.UDT;
else if ((SymTagEnum)typeSymbol.symTag == SymTagEnum.SymTagPointerType)
category = SymbolMemberInfo.MemberCategory.Pointer;
var info = new SymbolMemberInfo(category, symbolName, typeName, typeSymbol.length, symbol.length, (ulong)symbol.offset, symbol.bitPosition);
if (typeSymbol.volatileType == 1)
info.Volatile = true;
if (symbol.locationType == 6)
info.BitField = true;
return info;
}
public SymbolFunctionInfo ProcessFunction(IDiaSymbol symbol)
{
if (symbol.compilerGenerated > 0)
return null;
var info = new SymbolFunctionInfo();
if (symbol.isStatic > 0)
{
info.Category = SymbolFunctionInfo.FunctionCategory.StaticFunction;
}
else if (symbol.classParent.name.EndsWith(symbol.name))
{
info.Category = SymbolFunctionInfo.FunctionCategory.Constructor;
}
else if (symbol.name.StartsWith("~"))
{
info.Category = SymbolFunctionInfo.FunctionCategory.Destructor;
}
else
{
info.Category = SymbolFunctionInfo.FunctionCategory.Function;
}
info.Virtual = (symbol.@virtual == 1);
info.IsOverride = info.Virtual && (symbol.intro == 0);
info.IsPure = info.Virtual && (symbol.pure != 0);
info.IsConst = symbol.constType != 0;
if (symbol.wasInlined == 0 && symbol.inlSpec != 0)
info.WasInlineRemoved = true;
info.Name = GetType(symbol.type.type) + " " + symbol.name;
symbol.type.findChildren(SymTagEnum.SymTagFunctionArgType, null, 0, out var syms);
if (syms.count == 0)
{
info.Name += "(void)";
}
else
{
var parameters = new List<string>();
foreach (IDiaSymbol argSym in syms)
{
parameters.Add(GetType(argSym.type));
}
info.Name += "(" + string.Join(",", parameters) + ")";
}
return info;
}
private string GetType(IDiaSymbol typeSymbol)
{
switch ((SymTagEnum)typeSymbol.symTag)
{
case SymTagEnum.SymTagFunctionType:
var returnType = GetType(typeSymbol.type);
typeSymbol.findChildren(SymTagEnum.SymTagFunctionArgType, null, 0, out var syms);
if (syms.count == 0)
{
returnType += "(void)";
}
else
{
var parameters = new List<string>();
foreach (IDiaSymbol argSym in syms)
{
parameters.Add(GetType(argSym.type));
}
returnType += "(" + string.Join(",",parameters) + ")";
}
return returnType;
case SymTagEnum.SymTagPointerType:
return typeSymbol.reference != 0 ? $"{GetType(typeSymbol.type)}&" : $"{GetType(typeSymbol.type)}*";
case SymTagEnum.SymTagBaseType:
if (typeSymbol.constType != 0)
return "const " + SymbolMemberInfo.GetBaseType(typeSymbol);
return SymbolMemberInfo.GetBaseType(typeSymbol);
case SymTagEnum.SymTagArrayType:
// get array dimension:
var dimension = typeSymbol.count.ToString();
return $"{GetType(typeSymbol.type)}[{dimension}]";
case SymTagEnum.SymTagUDT:
return typeSymbol.name;
case SymTagEnum.SymTagEnum:
return $"enum {typeSymbol.name}";
default:
return string.Empty;
}
}
public override string ToString()
{
var sw = new StringWriter();
sw.WriteLine($"Symbol: {Name}");
sw.WriteLine($"TypeName: {TypeName}");
sw.WriteLine($"Size: {Size}");
sw.WriteLine($"Padding: {Padding}");
sw.WriteLine($"Total padding: {TotalPadding}");
sw.WriteLine("Members:");
sw.WriteLine("-------");
foreach (var member in Members)
{
if (member.PaddingBefore > 0)
{
var paddingOffset = member.Offset - member.PaddingBefore;
sw.WriteLine($"{PaddingMarker,-40} {paddingOffset,5} {member.PaddingBefore,5}");
}
sw.WriteLine($"{member.DisplayName,-40} {member.Offset,5} {member.Size,5}");
}
if (EndPadding > 0)
{
var endPaddingOffset = Size - EndPadding;
sw.WriteLine($"{PaddingMarker,-40} {endPaddingOffset,5} {EndPadding,5}");
}
return sw.ToString();
}
}
}