Debian Codegraph @

external/cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFunction.cs

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 using System;
5 using System.Collections;
6 using System.Collections.Generic;
7
8 #pragma warning disable 1591 // TODO: doc comments
9
10 namespace Microsoft.Cci.Pdb {
11 internal class PdbFunction {
12 static internal readonly Guid msilMetaData = new Guid(0xc6ea3fc9, 0x59b3, 0x49d6, 0xbc, 0x25,
13 0x09, 0x02, 0xbb, 0xab, 0xb4, 0x60);
14 static internal readonly IComparer byAddress = new PdbFunctionsByAddress();
15 static internal readonly IComparer byAddressAndToken = new PdbFunctionsByAddressAndToken();
16 //static internal readonly IComparer byToken = new PdbFunctionsByToken();
17
18 internal uint token;
19 internal uint slotToken;
20 internal uint tokenOfMethodWhoseUsingInfoAppliesToThisMethod;
21 //internal string name;
22 //internal string module;
23 //internal ushort flags;
24
25 internal uint segment;
26 internal uint address;
27 internal uint length;
28
29 //internal byte[] metadata;
30 internal PdbScope[] scopes;
31 internal PdbSlot[] slots;
32 internal PdbConstant[] constants;
33 internal string[] usedNamespaces;
34 internal PdbLines[] lines;
35 internal ushort[]/*?*/ usingCounts;
36 internal IEnumerable<INamespaceScope>/*?*/ namespaceScopes;
37 internal string/*?*/ iteratorClass;
38 internal List<ILocalScope>/*?*/ iteratorScopes;
39 internal PdbSynchronizationInformation/*?*/ synchronizationInformation;
40
41 /// <summary>
42 /// Flag saying whether the method has been identified as a product of VB compilation using
43 /// the legacy Windows PDB symbol format, in which case scope ends need to be shifted by 1
44 /// due to different semantics of scope limits in VB and C# compilers.
45 /// </summary>
46 private bool visualBasicScopesAdjusted = false;
47
48 private static string StripNamespace(string module) {
49 int li = module.LastIndexOf('.');
50 if (li > 0) {
51 return module.Substring(li + 1);
52 }
53 return module;
54 }
55
56 /// <summary>
57 /// When the Windows PDB reader identifies a PdbFunction as having 'Basic' as its source language,
58 /// it calls this method which adjusts all scopes by adding 1 to their lengths to compensate
59 /// for different behavior of VB vs. the C# compiler w.r.t. emission of scope info.
60 /// </summary>
61 internal void AdjustVisualBasicScopes()
62 {
63 if (!visualBasicScopesAdjusted)
64 {
65 visualBasicScopesAdjusted = true;
66
67 // Don't adjust root scope as that one is correct
68 foreach (PdbScope scope in scopes)
69 {
70 AdjustVisualBasicScopes(scope.scopes);
71 }
72 }
73 }
74
75 /// <summary>
76 /// Recursively update the entire scope tree by adding 1 to the length of each scope.
77 /// </summary>
78 private void AdjustVisualBasicScopes(PdbScope[] scopes)
79 {
80 foreach (PdbScope scope in scopes)
81 {
82 scope.length++;
83 AdjustVisualBasicScopes(scope.scopes);
84 }
85 }
86
87 internal static PdbFunction[] LoadManagedFunctions(/*string module,*/
88 BitAccess bits, uint limit,
89 bool readStrings) {
90 //string mod = StripNamespace(module);
91 int begin = bits.Position;
92 int count = 0;
93
94 while (bits.Position < limit) {
95 ushort siz;
96 ushort rec;
97
98 bits.ReadUInt16(out siz);
99 int star = bits.Position;
100 int stop = bits.Position + siz;
101 bits.Position = star;
102 bits.ReadUInt16(out rec);
103
104 switch ((SYM)rec) {
105 case SYM.S_GMANPROC:
106 case SYM.S_LMANPROC:
107 ManProcSym proc;
108 bits.ReadUInt32(out proc.parent);
109 bits.ReadUInt32(out proc.end);
110 bits.Position = (int)proc.end;
111 count++;
112 break;
113
114 case SYM.S_END:
115 bits.Position = stop;
116 break;
117
118 default:
119 //Console.WriteLine("{0,6}: {1:x2} {2}",
120 // bits.Position, rec, (SYM)rec);
121 bits.Position = stop;
122 break;
123 }
124 }
125 if (count == 0) {
126 return null;
127 }
128
129 bits.Position = begin;
130 PdbFunction[] funcs = new PdbFunction[count];
131 int func = 0;
132
133 while (bits.Position < limit) {
134 ushort siz;
135 ushort rec;
136
137 bits.ReadUInt16(out siz);
138 int star = bits.Position;
139 int stop = bits.Position + siz;
140 bits.ReadUInt16(out rec);
141
142 switch ((SYM)rec) {
143
144 case SYM.S_GMANPROC:
145 case SYM.S_LMANPROC:
146 ManProcSym proc;
147 //int offset = bits.Position;
148
149 bits.ReadUInt32(out proc.parent);
150 bits.ReadUInt32(out proc.end);
151 bits.ReadUInt32(out proc.next);
152 bits.ReadUInt32(out proc.len);
153 bits.ReadUInt32(out proc.dbgStart);
154 bits.ReadUInt32(out proc.dbgEnd);
155 bits.ReadUInt32(out proc.token);
156 bits.ReadUInt32(out proc.off);
157 bits.ReadUInt16(out proc.seg);
158 bits.ReadUInt8(out proc.flags);
159 bits.ReadUInt16(out proc.retReg);
160 if (readStrings) {
161 bits.ReadCString(out proc.name);
162 } else {
163 bits.SkipCString(out proc.name);
164 }
165 //Console.WriteLine("token={0:X8} [{1}::{2}]", proc.token, module, proc.name);
166
167 bits.Position = stop;
168 funcs[func++] = new PdbFunction(/*module,*/ proc, bits);
169 break;
170
171 default: {
172 //throw new PdbDebugException("Unknown SYMREC {0}", (SYM)rec);
173 bits.Position = stop;
174 break;
175 }
176 }
177 }
178 return funcs;
179 }
180
181 internal static void CountScopesAndSlots(BitAccess bits, uint limit,
182 out int constants, out int scopes, out int slots, out int usedNamespaces) {
183 int pos = bits.Position;
184 BlockSym32 block;
185 constants = 0;
186 slots = 0;
187 scopes = 0;
188 usedNamespaces = 0;
189
190 while (bits.Position < limit) {
191 ushort siz;
192 ushort rec;
193
194 bits.ReadUInt16(out siz);
195 int star = bits.Position;
196 int stop = bits.Position + siz;
197 bits.Position = star;
198 bits.ReadUInt16(out rec);
199
200 switch ((SYM)rec) {
201 case SYM.S_BLOCK32: {
202 bits.ReadUInt32(out block.parent);
203 bits.ReadUInt32(out block.end);
204
205 scopes++;
206 bits.Position = (int)block.end;
207 break;
208 }
209
210 case SYM.S_MANSLOT:
211 slots++;
212 bits.Position = stop;
213 break;
214
215 case SYM.S_UNAMESPACE:
216 usedNamespaces++;
217 bits.Position = stop;
218 break;
219
220 case SYM.S_MANCONSTANT:
221 constants++;
222 bits.Position = stop;
223 break;
224
225 default:
226 bits.Position = stop;
227 break;
228 }
229 }
230 bits.Position = pos;
231 }
232
233 internal PdbFunction() {
234 }
235
236 internal PdbFunction(/*string module, */ManProcSym proc, BitAccess bits) {
237 this.token = proc.token;
238 //this.module = module;
239 //this.name = proc.name;
240 //this.flags = proc.flags;
241 this.segment = proc.seg;
242 this.address = proc.off;
243 this.length = proc.len;
244
245 if (proc.seg != 1) {
246 throw new PdbDebugException("Segment is {0}, not 1.", proc.seg);
247 }
248 if (proc.parent != 0 || proc.next != 0) {
249 throw new PdbDebugException("Warning parent={0}, next={1}",
250 proc.parent, proc.next);
251 }
252 //if (proc.dbgStart != 0 || proc.dbgEnd != 0) {
253 // throw new PdbDebugException("Warning DBG start={0}, end={1}",
254 // proc.dbgStart, proc.dbgEnd);
255 //}
256
257 int constantCount;
258 int scopeCount;
259 int slotCount;
260 int usedNamespacesCount;
261 CountScopesAndSlots(bits, proc.end, out constantCount, out scopeCount, out slotCount, out usedNamespacesCount);
262 int scope = constantCount > 0 || slotCount > 0 || usedNamespacesCount > 0 ? 1 : 0;
263 int slot = 0;
264 int constant = 0;
265 int usedNs = 0;
266 scopes = new PdbScope[scopeCount+scope];
267 slots = new PdbSlot[slotCount];
268 constants = new PdbConstant[constantCount];
269 usedNamespaces = new string[usedNamespacesCount];
270
271 if (scope > 0)
272 scopes[0] = new PdbScope(this.address, proc.len, slots, constants, usedNamespaces);
273
274 while (bits.Position < proc.end) {
275 ushort siz;
276 ushort rec;
277
278 bits.ReadUInt16(out siz);
279 int star = bits.Position;
280 int stop = bits.Position + siz;
281 bits.Position = star;
282 bits.ReadUInt16(out rec);
283
284 switch ((SYM)rec) {
285 case SYM.S_OEM: { // 0x0404
286 OemSymbol oem;
287
288 bits.ReadGuid(out oem.idOem);
289 bits.ReadUInt32(out oem.typind);
290 // internal byte[] rgl; // user data, force 4-byte alignment
291
292 if (oem.idOem == msilMetaData) {
293 string name = bits.ReadString();
294 if (name == "MD2") {
295 ReadMD2CustomMetadata(bits);
296 } else if (name == "asyncMethodInfo") {
297 this.synchronizationInformation = new PdbSynchronizationInformation(bits);
298 }
299 bits.Position = stop;
300 break;
301 } else {
302 throw new PdbDebugException("OEM section: guid={0} ti={1}",
303 oem.idOem, oem.typind);
304 // bits.Position = stop;
305 }
306 }
307
308 case SYM.S_BLOCK32: {
309 BlockSym32 block = new BlockSym32();
310
311 bits.ReadUInt32(out block.parent);
312 bits.ReadUInt32(out block.end);
313 bits.ReadUInt32(out block.len);
314 bits.ReadUInt32(out block.off);
315 bits.ReadUInt16(out block.seg);
316 bits.SkipCString(out block.name);
317 bits.Position = stop;
318
319 scopes[scope++] = new PdbScope(this.address, block, bits, out slotToken);
320 bits.Position = (int)block.end;
321 break;
322 }
323
324 case SYM.S_MANSLOT:
325 slots[slot++] = new PdbSlot(bits);
326 bits.Position = stop;
327 break;
328
329 case SYM.S_MANCONSTANT:
330 constants[constant++] = new PdbConstant(bits);
331 bits.Position = stop;
332 break;
333
334 case SYM.S_UNAMESPACE:
335 bits.ReadCString(out usedNamespaces[usedNs++]);
336 bits.Position = stop;
337 break;
338
339 case SYM.S_END:
340 bits.Position = stop;
341 break;
342
343 default: {
344 //throw new PdbDebugException("Unknown SYM: {0}", (SYM)rec);
345 bits.Position = stop;
346 break;
347 }
348 }
349 }
350
351 if (bits.Position != proc.end) {
352 throw new PdbDebugException("Not at S_END");
353 }
354
355 ushort esiz;
356 ushort erec;
357 bits.ReadUInt16(out esiz);
358 bits.ReadUInt16(out erec);
359
360 if (erec != (ushort)SYM.S_END) {
361 throw new PdbDebugException("Missing S_END");
362 }
363 }
364
365 internal void ReadMD2CustomMetadata(BitAccess bits)
366 {
367 byte version;
368 bits.ReadUInt8(out version);
369 if (version == 4) {
370 byte count;
371 bits.ReadUInt8(out count);
372 bits.Align(4);
373 while (count-- > 0)
374 this.ReadCustomMetadata(bits);
375 }
376 }
377
378 private void ReadCustomMetadata(BitAccess bits)
379 {
380 int savedPosition = bits.Position;
381 byte version;
382 bits.ReadUInt8(out version);
383 byte kind;
384 bits.ReadUInt8(out kind);
385 bits.Position += 2; // 2-bytes padding
386 uint numberOfBytesInItem;
387 bits.ReadUInt32(out numberOfBytesInItem);
388 if (version == 4)
389 {
390 switch (kind)
391 {
392 case 0: this.ReadUsingInfo(bits); break;
393 case 1: this.ReadForwardInfo(bits); break;
394 case 2: break; // this.ReadForwardedToModuleInfo(bits); break;
395 case 3: this.ReadIteratorLocals(bits); break;
396 case 4: this.ReadForwardIterator(bits); break;
397 case 5: break; // dynamic locals - see http://index/#Microsoft.VisualStudio.LanguageServices/Shared/CustomDebugInfoReader.cs,a3031f7681d76e93
398 case 6: break; // EnC data
399 case 7: break; // EnC data for lambdas and closures
400 // ignore any other unknown record types that may be added in future, instead of throwing an exception
401 // see more details here: https://github.com/tmat/roslyn/blob/portable-pdb/docs/specs/PortablePdb-Metadata.md
402 default: break; // throw new PdbDebugException("Unknown custom metadata item kind: {0}", kind);
403 }
404 }
405 bits.Position = savedPosition + (int)numberOfBytesInItem;
406 }
407
408 private void ReadForwardIterator(BitAccess bits) {
409 this.iteratorClass = bits.ReadString();
410 }
411
412 private void ReadIteratorLocals(BitAccess bits) {
413 uint numberOfLocals;
414 bits.ReadUInt32(out numberOfLocals);
415 this.iteratorScopes = new List<ILocalScope>((int)numberOfLocals);
416 while (numberOfLocals-- > 0) {
417 uint ilStartOffset;
418 uint ilEndOffset;
419 bits.ReadUInt32(out ilStartOffset);
420 bits.ReadUInt32(out ilEndOffset);
421 this.iteratorScopes.Add(new PdbIteratorScope(ilStartOffset, ilEndOffset-ilStartOffset));
422 }
423 }
424
425 //private void ReadForwardedToModuleInfo(BitAccess bits) {
426 //}
427
428 private void ReadForwardInfo(BitAccess bits) {
429 bits.ReadUInt32(out this.tokenOfMethodWhoseUsingInfoAppliesToThisMethod);
430 }
431
432 private void ReadUsingInfo(BitAccess bits) {
433 ushort numberOfNamespaces;
434 bits.ReadUInt16(out numberOfNamespaces);
435 this.usingCounts = new ushort[numberOfNamespaces];
436 for (ushort i = 0; i < numberOfNamespaces; i++) {
437 bits.ReadUInt16(out this.usingCounts[i]);
438 }
439 }
440
441 internal class PdbFunctionsByAddress : IComparer {
442 public int Compare(Object x, Object y) {
443 PdbFunction fx = (PdbFunction)x;
444 PdbFunction fy = (PdbFunction)y;
445
446 if (fx.segment < fy.segment) {
447 return -1;
448 } else if (fx.segment > fy.segment) {
449 return 1;
450 } else if (fx.address < fy.address) {
451 return -1;
452 } else if (fx.address > fy.address) {
453 return 1;
454 } else {
455 return 0;
456 }
457 }
458 }
459
460 internal class PdbFunctionsByAddressAndToken : IComparer {
461 public int Compare(Object x, Object y) {
462 PdbFunction fx = (PdbFunction)x;
463 PdbFunction fy = (PdbFunction)y;
464
465 if (fx.segment < fy.segment) {
466 return -1;
467 } else if (fx.segment > fy.segment) {
468 return 1;
469 } else if (fx.address < fy.address) {
470 return -1;
471 } else if (fx.address > fy.address) {
472 return 1;
473 } else {
474 if (fx.token < fy.token)
475 return -1;
476 else if (fx.token > fy.token)
477 return 1;
478 else
479 return 0;
480 }
481 }
482 }
483
484 //internal class PdbFunctionsByToken : IComparer {
485 // public int Compare(Object x, Object y) {
486 // PdbFunction fx = (PdbFunction)x;
487 // PdbFunction fy = (PdbFunction)y;
488
489 // if (fx.token < fy.token) {
490 // return -1;
491 // } else if (fx.token > fy.token) {
492 // return 1;
493 // } else {
494 // return 0;
495 // }
496 // }
497
498 //}
499 }
500
501 internal class PdbSynchronizationInformation {
502 internal uint kickoffMethodToken;
503 internal uint generatedCatchHandlerIlOffset;
504 internal PdbSynchronizationPoint[] synchronizationPoints;
505
506 internal PdbSynchronizationInformation(BitAccess bits) {
507 uint asyncStepInfoCount;
508 bits.ReadUInt32(out this.kickoffMethodToken);
509 bits.ReadUInt32(out this.generatedCatchHandlerIlOffset);
510 bits.ReadUInt32(out asyncStepInfoCount);
511 this.synchronizationPoints = new PdbSynchronizationPoint[asyncStepInfoCount];
512 for (uint i = 0; i < asyncStepInfoCount; i += 1) {
513 this.synchronizationPoints[i] = new PdbSynchronizationPoint(bits);
514 }
515 }
516
517 public uint GeneratedCatchHandlerOffset {
518 get { return this.generatedCatchHandlerIlOffset; }
519 }
520 }
521
522 internal class PdbSynchronizationPoint {
523 internal uint synchronizeOffset;
524 internal uint continuationMethodToken;
525 internal uint continuationOffset;
526
527 internal PdbSynchronizationPoint(BitAccess bits) {
528 bits.ReadUInt32(out this.synchronizeOffset);
529 bits.ReadUInt32(out this.continuationMethodToken);
530 bits.ReadUInt32(out this.continuationOffset);
531 }
532
533 public uint SynchronizeOffset {
534 get { return this.synchronizeOffset; }
535 }
536
537 public uint ContinuationOffset {
538 get { return this.continuationOffset; }
539 }
540 }
541
542 }