Debian Codegraph @

external/cecil/symbols/pdb/Microsoft.Cci.Pdb/PdbFile.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 using System.IO;
8
9 namespace Microsoft.Cci.Pdb {
10
11 internal class PdbFile {
12
13 /// <summary>
14 /// GUID of the Basic source language.
15 /// </summary>
16 private static readonly Guid BasicLanguageGuid = new Guid(974311608, -15764, 4560, 180, 66, 0, 160, 36, 74, 29, 210);
17
18 private PdbFile() // This class can't be instantiated.
19 {
20 }
21
22 static void LoadInjectedSourceInformation(BitAccess bits, out Guid doctype, out Guid language, out Guid vendor, out Guid checksumAlgo, out byte[] checksum) {
23 int checksumSize;
24 int injectedSourceSize;
25 checksum = null;
26
27 bits.ReadGuid(out language);
28 bits.ReadGuid(out vendor);
29 bits.ReadGuid(out doctype);
30 bits.ReadGuid(out checksumAlgo);
31 bits.ReadInt32(out checksumSize);
32 bits.ReadInt32(out injectedSourceSize);
33
34 if (checksumSize > 0) {
35 checksum = new byte[checksumSize];
36 bits.ReadBytes(checksum);
37 }
38 }
39
40 static Dictionary<string, int> LoadNameIndex(BitAccess bits, out int age, out Guid guid) {
41 Dictionary<string, int> result = new Dictionary<string, int>();
42 int ver;
43 int sig;
44 bits.ReadInt32(out ver); // 0..3 Version
45 bits.ReadInt32(out sig); // 4..7 Signature
46 bits.ReadInt32(out age); // 8..11 Age
47 bits.ReadGuid(out guid); // 12..27 GUID
48
49 //if (ver != 20000404) {
50 // throw new PdbDebugException("Unsupported PDB Stream version {0}", ver);
51 //}
52
53 // Read string buffer.
54 int buf;
55 bits.ReadInt32(out buf); // 28..31 Bytes of Strings
56
57 int beg = bits.Position;
58 int nxt = bits.Position + buf;
59
60 bits.Position = nxt;
61
62 // Read map index.
63 int cnt; // n+0..3 hash size.
64 int max; // n+4..7 maximum ni.
65
66 bits.ReadInt32(out cnt);
67 bits.ReadInt32(out max);
68
69 BitSet present = new BitSet(bits);
70 BitSet deleted = new BitSet(bits);
71 if (!deleted.IsEmpty) {
72 throw new PdbDebugException("Unsupported PDB deleted bitset is not empty.");
73 }
74
75 int j = 0;
76 for (int i = 0; i < max; i++) {
77 if (present.IsSet(i)) {
78 int ns;
79 int ni;
80 bits.ReadInt32(out ns);
81 bits.ReadInt32(out ni);
82
83 string name;
84 int saved = bits.Position;
85 bits.Position = beg + ns;
86 bits.ReadCString(out name);
87 bits.Position = saved;
88
89 result.Add(name.ToUpperInvariant(), ni);
90 j++;
91 }
92 }
93 if (j != cnt) {
94 throw new PdbDebugException("Count mismatch. ({0} != {1})", j, cnt);
95 }
96 return result;
97 }
98
99 static IntHashTable LoadNameStream(BitAccess bits) {
100 IntHashTable ht = new IntHashTable();
101
102 uint sig;
103 int ver;
104 bits.ReadUInt32(out sig); // 0..3 Signature
105 bits.ReadInt32(out ver); // 4..7 Version
106
107 // Read (or skip) string buffer.
108 int buf;
109 bits.ReadInt32(out buf); // 8..11 Bytes of Strings
110
111 if (sig != 0xeffeeffe || ver != 1) {
112 throw new PdbDebugException("Unsupported Name Stream version. "+
113 "(sig={0:x8}, ver={1})",
114 sig, ver);
115 }
116 int beg = bits.Position;
117 int nxt = bits.Position + buf;
118 bits.Position = nxt;
119
120 // Read hash table.
121 int siz;
122 bits.ReadInt32(out siz); // n+0..3 Number of hash buckets.
123 nxt = bits.Position;
124
125 for (int i = 0; i < siz; i++) {
126 int ni;
127 string name;
128
129 bits.ReadInt32(out ni);
130
131 if (ni != 0) {
132 int saved = bits.Position;
133 bits.Position = beg + ni;
134 bits.ReadCString(out name);
135 bits.Position = saved;
136
137 ht.Add(ni, name);
138 }
139 }
140 bits.Position = nxt;
141
142 return ht;
143 }
144
145 private static PdbFunction match = new PdbFunction();
146
147 private static int FindFunction(PdbFunction[] funcs, ushort sec, uint off) {
148 match.segment = sec;
149 match.address = off;
150
151 return Array.BinarySearch(funcs, match, PdbFunction.byAddress);
152 }
153
154 static void LoadManagedLines(PdbFunction[] funcs,
155 IntHashTable names,
156 BitAccess bits,
157 MsfDirectory dir,
158 Dictionary<string, int> nameIndex,
159 PdbReader reader,
160 uint limit,
161 Dictionary<string, PdbSource> sourceCache)
162 {
163 Array.Sort(funcs, PdbFunction.byAddressAndToken);
164
165 int begin = bits.Position;
166 IntHashTable checks = ReadSourceFileInfo(bits, limit, names, dir, nameIndex, reader, sourceCache);
167
168 // Read the lines next.
169 bits.Position = begin;
170 while (bits.Position < limit) {
171 int sig;
172 int siz;
173 bits.ReadInt32(out sig);
174 bits.ReadInt32(out siz);
175 int endSym = bits.Position + siz;
176
177 switch ((DEBUG_S_SUBSECTION)sig) {
178 case DEBUG_S_SUBSECTION.LINES: {
179 CV_LineSection sec;
180
181 bits.ReadUInt32(out sec.off);
182 bits.ReadUInt16(out sec.sec);
183 bits.ReadUInt16(out sec.flags);
184 bits.ReadUInt32(out sec.cod);
185 int funcIndex = FindFunction(funcs, sec.sec, sec.off);
186 if (funcIndex < 0) break;
187 var func = funcs[funcIndex];
188 if (func.lines == null) {
189 while (funcIndex > 0) {
190 var f = funcs[funcIndex-1];
191 if (f.lines != null || f.segment != sec.sec || f.address != sec.off) break;
192 func = f;
193 funcIndex--;
194 }
195 } else {
196 while (funcIndex < funcs.Length-1 && func.lines != null) {
197 var f = funcs[funcIndex+1];
198 if (f.segment != sec.sec || f.address != sec.off) break;
199 func = f;
200 funcIndex++;
201 }
202 }
203 if (func.lines != null) break;
204
205 // Count the line blocks.
206 int begSym = bits.Position;
207 int blocks = 0;
208 while (bits.Position < endSym) {
209 CV_SourceFile file;
210 bits.ReadUInt32(out file.index);
211 bits.ReadUInt32(out file.count);
212 bits.ReadUInt32(out file.linsiz); // Size of payload.
213 int linsiz = (int)file.count * (8 + ((sec.flags & 1) != 0 ? 4 : 0));
214 bits.Position += linsiz;
215 blocks++;
216 }
217
218 func.lines = new PdbLines[blocks];
219 int block = 0;
220
221 bits.Position = begSym;
222 while (bits.Position < endSym) {
223 CV_SourceFile file;
224 bits.ReadUInt32(out file.index);
225 bits.ReadUInt32(out file.count);
226 bits.ReadUInt32(out file.linsiz); // Size of payload.
227
228 PdbSource src = (PdbSource)checks[(int)file.index];
229 if (src.language.Equals(BasicLanguageGuid))
230 {
231 func.AdjustVisualBasicScopes();
232 }
233
234 PdbLines tmp = new PdbLines(src, file.count);
235 func.lines[block++] = tmp;
236 PdbLine[] lines = tmp.lines;
237
238 int plin = bits.Position;
239 int pcol = bits.Position + 8 * (int)file.count;
240
241 for (int i = 0; i < file.count; i++) {
242 CV_Line line;
243 CV_Column column = new CV_Column();
244
245 bits.Position = plin + 8 * i;
246 bits.ReadUInt32(out line.offset);
247 bits.ReadUInt32(out line.flags);
248
249 uint lineBegin = line.flags & (uint)CV_Line_Flags.linenumStart;
250 uint delta = (line.flags & (uint)CV_Line_Flags.deltaLineEnd) >> 24;
251 //bool statement = ((line.flags & (uint)CV_Line_Flags.fStatement) == 0);
252 if ((sec.flags & 1) != 0) {
253 bits.Position = pcol + 4 * i;
254 bits.ReadUInt16(out column.offColumnStart);
255 bits.ReadUInt16(out column.offColumnEnd);
256 }
257
258 lines[i] = new PdbLine(line.offset,
259 lineBegin,
260 column.offColumnStart,
261 lineBegin+delta,
262 column.offColumnEnd);
263 }
264 }
265 break;
266 }
267 }
268 bits.Position = endSym;
269 }
270 }
271
272 static void LoadFuncsFromDbiModule(BitAccess bits,
273 DbiModuleInfo info,
274 IntHashTable names,
275 List<PdbFunction> funcList,
276 bool readStrings,
277 MsfDirectory dir,
278 Dictionary<string, int> nameIndex,
279 PdbReader reader,
280 Dictionary<string, PdbSource> sourceCache)
281 {
282 PdbFunction[] funcs = null;
283
284 bits.Position = 0;
285 int sig;
286 bits.ReadInt32(out sig);
287 if (sig != 4) {
288 throw new PdbDebugException("Invalid signature. (sig={0})", sig);
289 }
290
291 bits.Position = 4;
292 // Console.WriteLine("{0}:", info.moduleName);
293 funcs = PdbFunction.LoadManagedFunctions(/*info.moduleName,*/
294 bits, (uint)info.cbSyms,
295 readStrings);
296 if (funcs != null) {
297 bits.Position = info.cbSyms + info.cbOldLines;
298 LoadManagedLines(funcs, names, bits, dir, nameIndex, reader,
299 (uint)(info.cbSyms + info.cbOldLines + info.cbLines),
300 sourceCache);
301
302 for (int i = 0; i < funcs.Length; i++) {
303 funcList.Add(funcs[i]);
304 }
305 }
306 }
307
308 static void LoadDbiStream(BitAccess bits,
309 out DbiModuleInfo[] modules,
310 out DbiDbgHdr header,
311 bool readStrings) {
312 DbiHeader dh = new DbiHeader(bits);
313 header = new DbiDbgHdr();
314
315 //if (dh.sig != -1 || dh.ver != 19990903) {
316 // throw new PdbException("Unsupported DBI Stream version, sig={0}, ver={1}",
317 // dh.sig, dh.ver);
318 //}
319
320 // Read gpmod section.
321 List<DbiModuleInfo> modList = new List<DbiModuleInfo>();
322 int end = bits.Position + dh.gpmodiSize;
323 while (bits.Position < end) {
324 DbiModuleInfo mod = new DbiModuleInfo(bits, readStrings);
325 modList.Add(mod);
326 }
327 if (bits.Position != end) {
328 throw new PdbDebugException("Error reading DBI stream, pos={0} != {1}",
329 bits.Position, end);
330 }
331
332 if (modList.Count > 0) {
333 modules = modList.ToArray();
334 } else {
335 modules = null;
336 }
337
338 // Skip the Section Contribution substream.
339 bits.Position += dh.secconSize;
340
341 // Skip the Section Map substream.
342 bits.Position += dh.secmapSize;
343
344 // Skip the File Info substream.
345 bits.Position += dh.filinfSize;
346
347 // Skip the TSM substream.
348 bits.Position += dh.tsmapSize;
349
350 // Skip the EC substream.
351 bits.Position += dh.ecinfoSize;
352
353 // Read the optional header.
354 end = bits.Position + dh.dbghdrSize;
355 if (dh.dbghdrSize > 0) {
356 header = new DbiDbgHdr(bits);
357 }
358 bits.Position = end;
359 }
360
361 internal static PdbInfo LoadFunctions(Stream read) {
362 PdbInfo pdbInfo = new PdbInfo();
363
364 pdbInfo.TokenToSourceMapping = new Dictionary<uint, PdbTokenLine>();
365 BitAccess bits = new BitAccess(64 * 1024);
366 PdbFileHeader head = new PdbFileHeader(read, bits);
367 PdbReader reader = new PdbReader(read, head.pageSize);
368 MsfDirectory dir = new MsfDirectory(reader, head, bits);
369 DbiModuleInfo[] modules = null;
370 DbiDbgHdr header;
371 Dictionary<string, PdbSource> sourceCache = new Dictionary<string, PdbSource>();
372
373 dir.streams[1].Read(reader, bits);
374 Dictionary<string, int> nameIndex = LoadNameIndex(bits, out pdbInfo.Age, out pdbInfo.Guid);
375 int nameStream;
376 if (!nameIndex.TryGetValue("/NAMES", out nameStream)) {
377 throw new PdbException("Could not find the '/NAMES' stream: the PDB file may be a public symbol file instead of a private symbol file");
378 }
379 dir.streams[nameStream].Read(reader, bits);
380 IntHashTable names = LoadNameStream(bits);
381
382 int srcsrvStream;
383 if (!nameIndex.TryGetValue("SRCSRV", out srcsrvStream))
384 pdbInfo.SourceServerData = string.Empty;
385 else {
386 DataStream dataStream = dir.streams[srcsrvStream];
387 byte[] bytes = new byte[dataStream.contentSize];
388 dataStream.Read(reader, bits);
389 pdbInfo.SourceServerData = bits.ReadBString(bytes.Length);
390 }
391
392 int sourceLinkStream;
393 if (nameIndex.TryGetValue("SOURCELINK", out sourceLinkStream)) {
394 DataStream dataStream = dir.streams[sourceLinkStream];
395 pdbInfo.SourceLinkData = new byte[dataStream.contentSize];
396 dataStream.Read(reader, bits);
397 bits.ReadBytes(pdbInfo.SourceLinkData);
398 }
399
400 dir.streams[3].Read(reader, bits);
401 LoadDbiStream(bits, out modules, out header, true);
402
403 List<PdbFunction> funcList = new List<PdbFunction>();
404
405 if (modules != null) {
406 for (int m = 0; m < modules.Length; m++) {
407 var module = modules[m];
408 if (module.stream > 0) {
409 dir.streams[module.stream].Read(reader, bits);
410 if (module.moduleName == "TokenSourceLineInfo") {
411 LoadTokenToSourceInfo(bits, module, names, dir, nameIndex, reader, pdbInfo.TokenToSourceMapping, sourceCache);
412 continue;
413 }
414 LoadFuncsFromDbiModule(bits, module, names, funcList, true, dir, nameIndex, reader, sourceCache);
415 }
416 }
417 }
418
419 PdbFunction[] funcs = funcList.ToArray();
420
421 // After reading the functions, apply the token remapping table if it exists.
422 if (header.snTokenRidMap != 0 && header.snTokenRidMap != 0xffff) {
423 dir.streams[header.snTokenRidMap].Read(reader, bits);
424 uint[] ridMap = new uint[dir.streams[header.snTokenRidMap].Length / 4];
425 bits.ReadUInt32(ridMap);
426
427 foreach (PdbFunction func in funcs) {
428 func.token = 0x06000000 | ridMap[func.token & 0xffffff];
429 }
430 }
431
432 //
433 Array.Sort(funcs, PdbFunction.byAddressAndToken);
434 //Array.Sort(funcs, PdbFunction.byToken);
435 pdbInfo.Functions = funcs;
436 return pdbInfo;
437 }
438
439 private static void LoadTokenToSourceInfo(BitAccess bits, DbiModuleInfo module, IntHashTable names, MsfDirectory dir,
440 Dictionary<string, int> nameIndex, PdbReader reader, Dictionary<uint, PdbTokenLine> tokenToSourceMapping, Dictionary<string,PdbSource> sourceCache) {
441 bits.Position = 0;
442 int sig;
443 bits.ReadInt32(out sig);
444 if (sig != 4) {
445 throw new PdbDebugException("Invalid signature. (sig={0})", sig);
446 }
447
448 bits.Position = 4;
449
450 while (bits.Position < module.cbSyms) {
451 ushort siz;
452 ushort rec;
453
454 bits.ReadUInt16(out siz);
455 int star = bits.Position;
456 int stop = bits.Position + siz;
457 bits.Position = star;
458 bits.ReadUInt16(out rec);
459
460 switch ((SYM)rec) {
461 case SYM.S_OEM:
462 OemSymbol oem;
463
464 bits.ReadGuid(out oem.idOem);
465 bits.ReadUInt32(out oem.typind);
466 // internal byte[] rgl; // user data, force 4-byte alignment
467
468 if (oem.idOem == PdbFunction.msilMetaData) {
469 string name = bits.ReadString();
470 if (name == "TSLI") {
471 uint token;
472 uint file_id;
473 uint line;
474 uint column;
475 uint endLine;
476 uint endColumn;
477 bits.ReadUInt32(out token);
478 bits.ReadUInt32(out file_id);
479 bits.ReadUInt32(out line);
480 bits.ReadUInt32(out column);
481 bits.ReadUInt32(out endLine);
482 bits.ReadUInt32(out endColumn);
483 PdbTokenLine tokenLine;
484 if (!tokenToSourceMapping.TryGetValue(token, out tokenLine))
485 tokenToSourceMapping.Add(token, new PdbTokenLine(token, file_id, line, column, endLine, endColumn));
486 else {
487 while (tokenLine.nextLine != null) tokenLine = tokenLine.nextLine;
488 tokenLine.nextLine = new PdbTokenLine(token, file_id, line, column, endLine, endColumn);
489 }
490 }
491 bits.Position = stop;
492 break;
493 } else {
494 throw new PdbDebugException("OEM section: guid={0} ti={1}",
495 oem.idOem, oem.typind);
496 // bits.Position = stop;
497 }
498
499 case SYM.S_END:
500 bits.Position = stop;
501 break;
502
503 default:
504 //Console.WriteLine("{0,6}: {1:x2} {2}",
505 // bits.Position, rec, (SYM)rec);
506 bits.Position = stop;
507 break;
508 }
509 }
510
511 bits.Position = module.cbSyms + module.cbOldLines;
512 int limit = module.cbSyms + module.cbOldLines + module.cbLines;
513 IntHashTable sourceFiles = ReadSourceFileInfo(bits, (uint)limit, names, dir, nameIndex, reader, sourceCache);
514 foreach (var tokenLine in tokenToSourceMapping.Values) {
515 tokenLine.sourceFile = (PdbSource)sourceFiles[(int)tokenLine.file_id];
516 }
517
518 }
519
520 public static readonly Guid SymDocumentType_Text = new Guid(1518771467, 26129, 4563, 189, 42, 0, 0, 248, 8, 73, 189);
521
522 private static IntHashTable ReadSourceFileInfo(BitAccess bits, uint limit, IntHashTable names, MsfDirectory dir,
523 Dictionary<string, int> nameIndex, PdbReader reader, Dictionary<string, PdbSource> sourceCache)
524 {
525 IntHashTable checks = new IntHashTable();
526
527 int begin = bits.Position;
528 while (bits.Position < limit) {
529 int sig;
530 int siz;
531 bits.ReadInt32(out sig);
532 bits.ReadInt32(out siz);
533 int place = bits.Position;
534 int endSym = bits.Position + siz;
535
536 switch ((DEBUG_S_SUBSECTION)sig) {
537 case DEBUG_S_SUBSECTION.FILECHKSMS:
538 while (bits.Position < endSym) {
539 CV_FileCheckSum chk;
540
541 int ni = bits.Position - place;
542 bits.ReadUInt32(out chk.name);
543 bits.ReadUInt8(out chk.len);
544 bits.ReadUInt8(out chk.type);
545
546 string name = (string)names[(int)chk.name];
547
548 PdbSource src;
549 if (!sourceCache.TryGetValue(name, out src))
550 {
551 int guidStream;
552 Guid doctypeGuid = SymDocumentType_Text;
553 Guid languageGuid = Guid.Empty;
554 Guid vendorGuid = Guid.Empty;
555 Guid checksumAlgoGuid = Guid.Empty;
556 byte[] checksum = null;
557 if (nameIndex.TryGetValue("/SRC/FILES/" + name.ToUpperInvariant(), out guidStream))
558 {
559 var guidBits = new BitAccess(0x100);
560 dir.streams[guidStream].Read(reader, guidBits);
561 LoadInjectedSourceInformation(guidBits, out doctypeGuid, out languageGuid, out vendorGuid, out checksumAlgoGuid, out checksum);
562 }
563
564 src = new PdbSource(name, doctypeGuid, languageGuid, vendorGuid, checksumAlgoGuid, checksum);
565 sourceCache.Add(name, src);
566 }
567 checks.Add(ni, src);
568 bits.Position += chk.len;
569 bits.Align(4);
570 }
571 bits.Position = endSym;
572 break;
573
574 default:
575 bits.Position = endSym;
576 break;
577 }
578 }
579 return checks;
580 }
581 }
582 }