1 #include "llvm/DebugInfo/PDB/Native/SymbolCache.h"
2 
3 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
4 #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
5 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
6 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
7 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
8 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
9 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
10 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
11 #include "llvm/DebugInfo/CodeView/TypeRecordHelpers.h"
12 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
13 #include "llvm/DebugInfo/PDB/Native/DbiModuleList.h"
14 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
15 #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
16 #include "llvm/DebugInfo/PDB/Native/NativeCompilandSymbol.h"
17 #include "llvm/DebugInfo/PDB/Native/NativeEnumGlobals.h"
18 #include "llvm/DebugInfo/PDB/Native/NativeEnumLineNumbers.h"
19 #include "llvm/DebugInfo/PDB/Native/NativeEnumTypes.h"
20 #include "llvm/DebugInfo/PDB/Native/NativeFunctionSymbol.h"
21 #include "llvm/DebugInfo/PDB/Native/NativeInlineSiteSymbol.h"
22 #include "llvm/DebugInfo/PDB/Native/NativeLineNumber.h"
23 #include "llvm/DebugInfo/PDB/Native/NativePublicSymbol.h"
24 #include "llvm/DebugInfo/PDB/Native/NativeRawSymbol.h"
25 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
26 #include "llvm/DebugInfo/PDB/Native/NativeTypeArray.h"
27 #include "llvm/DebugInfo/PDB/Native/NativeTypeBuiltin.h"
28 #include "llvm/DebugInfo/PDB/Native/NativeTypeEnum.h"
29 #include "llvm/DebugInfo/PDB/Native/NativeTypeFunctionSig.h"
30 #include "llvm/DebugInfo/PDB/Native/NativeTypePointer.h"
31 #include "llvm/DebugInfo/PDB/Native/NativeTypeTypedef.h"
32 #include "llvm/DebugInfo/PDB/Native/NativeTypeUDT.h"
33 #include "llvm/DebugInfo/PDB/Native/NativeTypeVTShape.h"
34 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
35 #include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
36 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
37 #include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
38 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
39 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
40 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
41 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
42 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
43 
44 using namespace llvm;
45 using namespace llvm::codeview;
46 using namespace llvm::pdb;
47 
48 // Maps codeview::SimpleTypeKind of a built-in type to the parameters necessary
49 // to instantiate a NativeBuiltinSymbol for that type.
50 static const struct BuiltinTypeEntry {
51   codeview::SimpleTypeKind Kind;
52   PDB_BuiltinType Type;
53   uint32_t Size;
54 } BuiltinTypes[] = {
55     {codeview::SimpleTypeKind::None, PDB_BuiltinType::None, 0},
56     {codeview::SimpleTypeKind::Void, PDB_BuiltinType::Void, 0},
57     {codeview::SimpleTypeKind::HResult, PDB_BuiltinType::HResult, 4},
58     {codeview::SimpleTypeKind::Int16Short, PDB_BuiltinType::Int, 2},
59     {codeview::SimpleTypeKind::UInt16Short, PDB_BuiltinType::UInt, 2},
60     {codeview::SimpleTypeKind::Int32, PDB_BuiltinType::Int, 4},
61     {codeview::SimpleTypeKind::UInt32, PDB_BuiltinType::UInt, 4},
62     {codeview::SimpleTypeKind::Int32Long, PDB_BuiltinType::Int, 4},
63     {codeview::SimpleTypeKind::UInt32Long, PDB_BuiltinType::UInt, 4},
64     {codeview::SimpleTypeKind::Int64Quad, PDB_BuiltinType::Int, 8},
65     {codeview::SimpleTypeKind::UInt64Quad, PDB_BuiltinType::UInt, 8},
66     {codeview::SimpleTypeKind::NarrowCharacter, PDB_BuiltinType::Char, 1},
67     {codeview::SimpleTypeKind::WideCharacter, PDB_BuiltinType::WCharT, 2},
68     {codeview::SimpleTypeKind::Character16, PDB_BuiltinType::Char16, 2},
69     {codeview::SimpleTypeKind::Character32, PDB_BuiltinType::Char32, 4},
70     {codeview::SimpleTypeKind::SignedCharacter, PDB_BuiltinType::Char, 1},
71     {codeview::SimpleTypeKind::UnsignedCharacter, PDB_BuiltinType::UInt, 1},
72     {codeview::SimpleTypeKind::Float32, PDB_BuiltinType::Float, 4},
73     {codeview::SimpleTypeKind::Float64, PDB_BuiltinType::Float, 8},
74     {codeview::SimpleTypeKind::Float80, PDB_BuiltinType::Float, 10},
75     {codeview::SimpleTypeKind::Boolean8, PDB_BuiltinType::Bool, 1},
76     // This table can be grown as necessary, but these are the only types we've
77     // needed so far.
78 };
79 
80 SymbolCache::SymbolCache(NativeSession &Session, DbiStream *Dbi)
81     : Session(Session), Dbi(Dbi) {
82   // Id 0 is reserved for the invalid symbol.
83   Cache.push_back(nullptr);
84   SourceFiles.push_back(nullptr);
85 
86   if (Dbi)
87     Compilands.resize(Dbi->modules().getModuleCount());
88 }
89 
90 std::unique_ptr<IPDBEnumSymbols>
91 SymbolCache::createTypeEnumerator(TypeLeafKind Kind) {
92   return createTypeEnumerator(std::vector<TypeLeafKind>{Kind});
93 }
94 
95 std::unique_ptr<IPDBEnumSymbols>
96 SymbolCache::createTypeEnumerator(std::vector<TypeLeafKind> Kinds) {
97   auto Tpi = Session.getPDBFile().getPDBTpiStream();
98   if (!Tpi) {
99     consumeError(Tpi.takeError());
100     return nullptr;
101   }
102   auto &Types = Tpi->typeCollection();
103   return std::unique_ptr<IPDBEnumSymbols>(
104       new NativeEnumTypes(Session, Types, std::move(Kinds)));
105 }
106 
107 std::unique_ptr<IPDBEnumSymbols>
108 SymbolCache::createGlobalsEnumerator(codeview::SymbolKind Kind) {
109   return std::unique_ptr<IPDBEnumSymbols>(
110       new NativeEnumGlobals(Session, {Kind}));
111 }
112 
113 SymIndexId SymbolCache::createSimpleType(TypeIndex Index,
114                                          ModifierOptions Mods) const {
115   if (Index.getSimpleMode() != codeview::SimpleTypeMode::Direct)
116     return createSymbol<NativeTypePointer>(Index);
117 
118   const auto Kind = Index.getSimpleKind();
119   const auto It =
120       llvm::find_if(BuiltinTypes, [Kind](const BuiltinTypeEntry &Builtin) {
121         return Builtin.Kind == Kind;
122       });
123   if (It == std::end(BuiltinTypes))
124     return 0;
125   return createSymbol<NativeTypeBuiltin>(Mods, It->Type, It->Size);
126 }
127 
128 SymIndexId
129 SymbolCache::createSymbolForModifiedType(codeview::TypeIndex ModifierTI,
130                                          codeview::CVType CVT) const {
131   ModifierRecord Record;
132   if (auto EC = TypeDeserializer::deserializeAs<ModifierRecord>(CVT, Record)) {
133     consumeError(std::move(EC));
134     return 0;
135   }
136 
137   if (Record.ModifiedType.isSimple())
138     return createSimpleType(Record.ModifiedType, Record.Modifiers);
139 
140   // Make sure we create and cache a record for the unmodified type.
141   SymIndexId UnmodifiedId = findSymbolByTypeIndex(Record.ModifiedType);
142   NativeRawSymbol &UnmodifiedNRS = *Cache[UnmodifiedId];
143 
144   switch (UnmodifiedNRS.getSymTag()) {
145   case PDB_SymType::Enum:
146     return createSymbol<NativeTypeEnum>(
147         static_cast<NativeTypeEnum &>(UnmodifiedNRS), std::move(Record));
148   case PDB_SymType::UDT:
149     return createSymbol<NativeTypeUDT>(
150         static_cast<NativeTypeUDT &>(UnmodifiedNRS), std::move(Record));
151   default:
152     // No other types can be modified.  (LF_POINTER, for example, records
153     // its modifiers a different way.
154     assert(false && "Invalid LF_MODIFIER record");
155     break;
156   }
157   return 0;
158 }
159 
160 SymIndexId SymbolCache::findSymbolByTypeIndex(codeview::TypeIndex Index) const {
161   // First see if it's already in our cache.
162   const auto Entry = TypeIndexToSymbolId.find(Index);
163   if (Entry != TypeIndexToSymbolId.end())
164     return Entry->second;
165 
166   // Symbols for built-in types are created on the fly.
167   if (Index.isSimple()) {
168     SymIndexId Result = createSimpleType(Index, ModifierOptions::None);
169     assert(TypeIndexToSymbolId.count(Index) == 0);
170     TypeIndexToSymbolId[Index] = Result;
171     return Result;
172   }
173 
174   // We need to instantiate and cache the desired type symbol.
175   auto Tpi = Session.getPDBFile().getPDBTpiStream();
176   if (!Tpi) {
177     consumeError(Tpi.takeError());
178     return 0;
179   }
180   codeview::LazyRandomTypeCollection &Types = Tpi->typeCollection();
181   codeview::CVType CVT = Types.getType(Index);
182 
183   if (isUdtForwardRef(CVT)) {
184     Expected<TypeIndex> EFD = Tpi->findFullDeclForForwardRef(Index);
185 
186     if (!EFD)
187       consumeError(EFD.takeError());
188     else if (*EFD != Index) {
189       assert(!isUdtForwardRef(Types.getType(*EFD)));
190       SymIndexId Result = findSymbolByTypeIndex(*EFD);
191       // Record a mapping from ForwardRef -> SymIndex of complete type so that
192       // we'll take the fast path next time.
193       assert(TypeIndexToSymbolId.count(Index) == 0);
194       TypeIndexToSymbolId[Index] = Result;
195       return Result;
196     }
197   }
198 
199   // At this point if we still have a forward ref udt it means the full decl was
200   // not in the PDB.  We just have to deal with it and use the forward ref.
201   SymIndexId Id = 0;
202   switch (CVT.kind()) {
203   case codeview::LF_ENUM:
204     Id = createSymbolForType<NativeTypeEnum, EnumRecord>(Index, std::move(CVT));
205     break;
206   case codeview::LF_ARRAY:
207     Id = createSymbolForType<NativeTypeArray, ArrayRecord>(Index,
208                                                            std::move(CVT));
209     break;
210   case codeview::LF_CLASS:
211   case codeview::LF_STRUCTURE:
212   case codeview::LF_INTERFACE:
213     Id = createSymbolForType<NativeTypeUDT, ClassRecord>(Index, std::move(CVT));
214     break;
215   case codeview::LF_UNION:
216     Id = createSymbolForType<NativeTypeUDT, UnionRecord>(Index, std::move(CVT));
217     break;
218   case codeview::LF_POINTER:
219     Id = createSymbolForType<NativeTypePointer, PointerRecord>(Index,
220                                                                std::move(CVT));
221     break;
222   case codeview::LF_MODIFIER:
223     Id = createSymbolForModifiedType(Index, std::move(CVT));
224     break;
225   case codeview::LF_PROCEDURE:
226     Id = createSymbolForType<NativeTypeFunctionSig, ProcedureRecord>(
227         Index, std::move(CVT));
228     break;
229   case codeview::LF_MFUNCTION:
230     Id = createSymbolForType<NativeTypeFunctionSig, MemberFunctionRecord>(
231         Index, std::move(CVT));
232     break;
233   case codeview::LF_VTSHAPE:
234     Id = createSymbolForType<NativeTypeVTShape, VFTableShapeRecord>(
235         Index, std::move(CVT));
236     break;
237   default:
238     Id = createSymbolPlaceholder();
239     break;
240   }
241   if (Id != 0) {
242     assert(TypeIndexToSymbolId.count(Index) == 0);
243     TypeIndexToSymbolId[Index] = Id;
244   }
245   return Id;
246 }
247 
248 std::unique_ptr<PDBSymbol>
249 SymbolCache::getSymbolById(SymIndexId SymbolId) const {
250   assert(SymbolId < Cache.size());
251 
252   // Id 0 is reserved.
253   if (SymbolId == 0 || SymbolId >= Cache.size())
254     return nullptr;
255 
256   // Make sure to handle the case where we've inserted a placeholder symbol
257   // for types we don't yet support.
258   NativeRawSymbol *NRS = Cache[SymbolId].get();
259   if (!NRS)
260     return nullptr;
261 
262   return PDBSymbol::create(Session, *NRS);
263 }
264 
265 NativeRawSymbol &SymbolCache::getNativeSymbolById(SymIndexId SymbolId) const {
266   return *Cache[SymbolId];
267 }
268 
269 uint32_t SymbolCache::getNumCompilands() const {
270   if (!Dbi)
271     return 0;
272 
273   return Dbi->modules().getModuleCount();
274 }
275 
276 SymIndexId SymbolCache::getOrCreateGlobalSymbolByOffset(uint32_t Offset) {
277   auto Iter = GlobalOffsetToSymbolId.find(Offset);
278   if (Iter != GlobalOffsetToSymbolId.end())
279     return Iter->second;
280 
281   SymbolStream &SS = cantFail(Session.getPDBFile().getPDBSymbolStream());
282   CVSymbol CVS = SS.readRecord(Offset);
283   SymIndexId Id = 0;
284   switch (CVS.kind()) {
285   case SymbolKind::S_UDT: {
286     UDTSym US = cantFail(SymbolDeserializer::deserializeAs<UDTSym>(CVS));
287     Id = createSymbol<NativeTypeTypedef>(std::move(US));
288     break;
289   }
290   default:
291     Id = createSymbolPlaceholder();
292     break;
293   }
294   if (Id != 0) {
295     assert(GlobalOffsetToSymbolId.count(Offset) == 0);
296     GlobalOffsetToSymbolId[Offset] = Id;
297   }
298 
299   return Id;
300 }
301 
302 SymIndexId SymbolCache::getOrCreateInlineSymbol(InlineSiteSym Sym,
303                                                 uint64_t ParentAddr,
304                                                 uint16_t Modi,
305                                                 uint32_t RecordOffset) const {
306   auto Iter = SymTabOffsetToSymbolId.find({Modi, RecordOffset});
307   if (Iter != SymTabOffsetToSymbolId.end())
308     return Iter->second;
309 
310   SymIndexId Id = createSymbol<NativeInlineSiteSymbol>(Sym, ParentAddr);
311   SymTabOffsetToSymbolId.insert({{Modi, RecordOffset}, Id});
312   return Id;
313 }
314 
315 std::unique_ptr<PDBSymbol>
316 SymbolCache::findSymbolBySectOffset(uint32_t Sect, uint32_t Offset,
317                                     PDB_SymType Type) {
318   switch (Type) {
319   case PDB_SymType::Function:
320     return findFunctionSymbolBySectOffset(Sect, Offset);
321   case PDB_SymType::PublicSymbol:
322     return findPublicSymbolBySectOffset(Sect, Offset);
323   case PDB_SymType::Compiland: {
324     uint16_t Modi;
325     if (!Session.moduleIndexForSectOffset(Sect, Offset, Modi))
326       return nullptr;
327     return getOrCreateCompiland(Modi);
328   }
329   case PDB_SymType::None: {
330     // FIXME: Implement for PDB_SymType::Data. The symbolizer calls this but
331     // only uses it to find the symbol length.
332     if (auto Sym = findFunctionSymbolBySectOffset(Sect, Offset))
333       return Sym;
334     return nullptr;
335   }
336   default:
337     return nullptr;
338   }
339 }
340 
341 std::unique_ptr<PDBSymbol>
342 SymbolCache::findFunctionSymbolBySectOffset(uint32_t Sect, uint32_t Offset) {
343   auto Iter = AddressToSymbolId.find({Sect, Offset});
344   if (Iter != AddressToSymbolId.end())
345     return getSymbolById(Iter->second);
346 
347   if (!Dbi)
348     return nullptr;
349 
350   uint16_t Modi;
351   if (!Session.moduleIndexForSectOffset(Sect, Offset, Modi))
352     return nullptr;
353 
354   Expected<ModuleDebugStreamRef> ExpectedModS =
355       Session.getModuleDebugStream(Modi);
356   if (!ExpectedModS) {
357     consumeError(ExpectedModS.takeError());
358     return nullptr;
359   }
360   CVSymbolArray Syms = ExpectedModS->getSymbolArray();
361 
362   // Search for the symbol in this module.
363   for (auto I = Syms.begin(), E = Syms.end(); I != E; ++I) {
364     if (I->kind() != S_LPROC32 && I->kind() != S_GPROC32)
365       continue;
366     auto PS = cantFail(SymbolDeserializer::deserializeAs<ProcSym>(*I));
367     if (Sect == PS.Segment && Offset >= PS.CodeOffset &&
368         Offset < PS.CodeOffset + PS.CodeSize) {
369       // Check if the symbol is already cached.
370       auto Found = AddressToSymbolId.find({PS.Segment, PS.CodeOffset});
371       if (Found != AddressToSymbolId.end())
372         return getSymbolById(Found->second);
373 
374       // Otherwise, create a new symbol.
375       SymIndexId Id = createSymbol<NativeFunctionSymbol>(PS, I.offset());
376       AddressToSymbolId.insert({{PS.Segment, PS.CodeOffset}, Id});
377       return getSymbolById(Id);
378     }
379 
380     // Jump to the end of this ProcSym.
381     I = Syms.at(PS.End);
382   }
383   return nullptr;
384 }
385 
386 std::unique_ptr<PDBSymbol>
387 SymbolCache::findPublicSymbolBySectOffset(uint32_t Sect, uint32_t Offset) {
388   auto Iter = AddressToPublicSymId.find({Sect, Offset});
389   if (Iter != AddressToPublicSymId.end())
390     return getSymbolById(Iter->second);
391 
392   auto Publics = Session.getPDBFile().getPDBPublicsStream();
393   if (!Publics)
394     return nullptr;
395 
396   auto ExpectedSyms = Session.getPDBFile().getPDBSymbolStream();
397   if (!ExpectedSyms)
398     return nullptr;
399   BinaryStreamRef SymStream =
400       ExpectedSyms->getSymbolArray().getUnderlyingStream();
401 
402   // Use binary search to find the first public symbol with an address greater
403   // than or equal to Sect, Offset.
404   auto AddrMap = Publics->getAddressMap();
405   auto First = AddrMap.begin();
406   auto It = AddrMap.begin();
407   size_t Count = AddrMap.size();
408   size_t Half;
409   while (Count > 0) {
410     It = First;
411     Half = Count / 2;
412     It += Half;
413     Expected<CVSymbol> Sym = readSymbolFromStream(SymStream, *It);
414     if (!Sym) {
415       consumeError(Sym.takeError());
416       return nullptr;
417     }
418 
419     auto PS =
420         cantFail(SymbolDeserializer::deserializeAs<PublicSym32>(Sym.get()));
421     if (PS.Segment < Sect || (PS.Segment == Sect && PS.Offset <= Offset)) {
422       First = ++It;
423       Count -= Half + 1;
424     } else
425       Count = Half;
426   }
427   if (It == AddrMap.begin())
428     return nullptr;
429   --It;
430 
431   Expected<CVSymbol> Sym = readSymbolFromStream(SymStream, *It);
432   if (!Sym) {
433     consumeError(Sym.takeError());
434     return nullptr;
435   }
436 
437   // Check if the symbol is already cached.
438   auto PS = cantFail(SymbolDeserializer::deserializeAs<PublicSym32>(Sym.get()));
439   auto Found = AddressToPublicSymId.find({PS.Segment, PS.Offset});
440   if (Found != AddressToPublicSymId.end())
441     return getSymbolById(Found->second);
442 
443   // Otherwise, create a new symbol.
444   SymIndexId Id = createSymbol<NativePublicSymbol>(PS);
445   AddressToPublicSymId.insert({{PS.Segment, PS.Offset}, Id});
446   return getSymbolById(Id);
447 }
448 
449 std::vector<SymbolCache::LineTableEntry>
450 SymbolCache::findLineTable(uint16_t Modi) const {
451   // Check if this module has already been added.
452   auto LineTableIter = LineTable.find(Modi);
453   if (LineTableIter != LineTable.end())
454     return LineTableIter->second;
455 
456   std::vector<LineTableEntry> &ModuleLineTable = LineTable[Modi];
457 
458   // If there is an error or there are no lines, just return the
459   // empty vector.
460   Expected<ModuleDebugStreamRef> ExpectedModS =
461       Session.getModuleDebugStream(Modi);
462   if (!ExpectedModS) {
463     consumeError(ExpectedModS.takeError());
464     return ModuleLineTable;
465   }
466 
467   std::vector<std::vector<LineTableEntry>> EntryList;
468   for (const auto &SS : ExpectedModS->getSubsectionsArray()) {
469     if (SS.kind() != DebugSubsectionKind::Lines)
470       continue;
471 
472     DebugLinesSubsectionRef Lines;
473     BinaryStreamReader Reader(SS.getRecordData());
474     if (auto EC = Lines.initialize(Reader)) {
475       consumeError(std::move(EC));
476       continue;
477     }
478 
479     uint32_t RelocSegment = Lines.header()->RelocSegment;
480     uint32_t RelocOffset = Lines.header()->RelocOffset;
481     for (const LineColumnEntry &Group : Lines) {
482       if (Group.LineNumbers.empty())
483         continue;
484 
485       std::vector<LineTableEntry> Entries;
486 
487       // If there are column numbers, then they should be in a parallel stream
488       // to the line numbers.
489       auto ColIt = Group.Columns.begin();
490       auto ColsEnd = Group.Columns.end();
491 
492       // Add a line to mark the beginning of this section.
493       uint64_t StartAddr =
494           Session.getVAFromSectOffset(RelocSegment, RelocOffset);
495       LineInfo FirstLine(Group.LineNumbers.front().Flags);
496       uint32_t ColNum =
497           (Lines.hasColumnInfo()) ? Group.Columns.front().StartColumn : 0;
498       Entries.push_back({StartAddr, FirstLine, ColNum, Group.NameIndex, false});
499 
500       for (const LineNumberEntry &LN : Group.LineNumbers) {
501         uint64_t VA =
502             Session.getVAFromSectOffset(RelocSegment, RelocOffset + LN.Offset);
503         LineInfo Line(LN.Flags);
504         ColNum = 0;
505 
506         if (Lines.hasColumnInfo() && ColIt != ColsEnd) {
507           ColNum = ColIt->StartColumn;
508           ++ColIt;
509         }
510         Entries.push_back({VA, Line, ColNum, Group.NameIndex, false});
511       }
512 
513       // Add a terminal entry line to mark the end of this subsection.
514       uint64_t EndAddr = StartAddr + Lines.header()->CodeSize;
515       LineInfo LastLine(Group.LineNumbers.back().Flags);
516       ColNum = (Lines.hasColumnInfo()) ? Group.Columns.back().StartColumn : 0;
517       Entries.push_back({EndAddr, LastLine, ColNum, Group.NameIndex, true});
518 
519       EntryList.push_back(Entries);
520     }
521   }
522 
523   // Sort EntryList, and add flattened contents to the line table.
524   llvm::sort(EntryList, [](const std::vector<LineTableEntry> &LHS,
525                            const std::vector<LineTableEntry> &RHS) {
526     return LHS[0].Addr < RHS[0].Addr;
527   });
528   for (std::vector<LineTableEntry> &I : EntryList)
529     llvm::append_range(ModuleLineTable, I);
530 
531   return ModuleLineTable;
532 }
533 
534 std::unique_ptr<IPDBEnumLineNumbers>
535 SymbolCache::findLineNumbersByVA(uint64_t VA, uint32_t Length) const {
536   uint16_t Modi;
537   if (!Session.moduleIndexForVA(VA, Modi))
538     return nullptr;
539 
540   std::vector<LineTableEntry> Lines = findLineTable(Modi);
541   if (Lines.empty())
542     return nullptr;
543 
544   // Find the first line in the line table whose address is not greater than
545   // the one we are searching for.
546   auto LineIter = llvm::partition_point(Lines, [&](const LineTableEntry &E) {
547     return (E.Addr < VA || (E.Addr == VA && E.IsTerminalEntry));
548   });
549 
550   // Try to back up if we've gone too far.
551   if (LineIter == Lines.end() || LineIter->Addr > VA) {
552     if (LineIter == Lines.begin() || std::prev(LineIter)->IsTerminalEntry)
553       return nullptr;
554     --LineIter;
555   }
556 
557   Expected<ModuleDebugStreamRef> ExpectedModS =
558       Session.getModuleDebugStream(Modi);
559   if (!ExpectedModS) {
560     consumeError(ExpectedModS.takeError());
561     return nullptr;
562   }
563   Expected<DebugChecksumsSubsectionRef> ExpectedChecksums =
564       ExpectedModS->findChecksumsSubsection();
565   if (!ExpectedChecksums) {
566     consumeError(ExpectedChecksums.takeError());
567     return nullptr;
568   }
569 
570   // Populate a vector of NativeLineNumbers that have addresses in the given
571   // address range.
572   std::vector<NativeLineNumber> LineNumbers;
573   while (LineIter != Lines.end()) {
574     if (LineIter->IsTerminalEntry) {
575       ++LineIter;
576       continue;
577     }
578 
579     // If the line is still within the address range, create a NativeLineNumber
580     // and add to the list.
581     if (LineIter->Addr > VA + Length)
582       break;
583 
584     uint32_t LineSect, LineOff;
585     Session.addressForVA(LineIter->Addr, LineSect, LineOff);
586     uint32_t LineLength = std::next(LineIter)->Addr - LineIter->Addr;
587     auto ChecksumIter =
588         ExpectedChecksums->getArray().at(LineIter->FileNameIndex);
589     uint32_t SrcFileId = getOrCreateSourceFile(*ChecksumIter);
590     NativeLineNumber LineNum(Session, LineIter->Line, LineIter->ColumnNumber,
591                              LineSect, LineOff, LineLength, SrcFileId, Modi);
592     LineNumbers.push_back(LineNum);
593     ++LineIter;
594   }
595   return std::make_unique<NativeEnumLineNumbers>(std::move(LineNumbers));
596 }
597 
598 std::unique_ptr<PDBSymbolCompiland>
599 SymbolCache::getOrCreateCompiland(uint32_t Index) {
600   if (!Dbi)
601     return nullptr;
602 
603   if (Index >= Compilands.size())
604     return nullptr;
605 
606   if (Compilands[Index] == 0) {
607     const DbiModuleList &Modules = Dbi->modules();
608     Compilands[Index] =
609         createSymbol<NativeCompilandSymbol>(Modules.getModuleDescriptor(Index));
610   }
611 
612   return Session.getConcreteSymbolById<PDBSymbolCompiland>(Compilands[Index]);
613 }
614 
615 std::unique_ptr<IPDBSourceFile>
616 SymbolCache::getSourceFileById(SymIndexId FileId) const {
617   assert(FileId < SourceFiles.size());
618 
619   // Id 0 is reserved.
620   if (FileId == 0)
621     return nullptr;
622 
623   return std::unique_ptr<NativeSourceFile>(
624       new NativeSourceFile(*SourceFiles[FileId].get()));
625 }
626 
627 SymIndexId
628 SymbolCache::getOrCreateSourceFile(const FileChecksumEntry &Checksums) const {
629   auto Iter = FileNameOffsetToId.find(Checksums.FileNameOffset);
630   if (Iter != FileNameOffsetToId.end())
631     return Iter->second;
632 
633   SymIndexId Id = SourceFiles.size();
634   auto SrcFile = std::make_unique<NativeSourceFile>(Session, Id, Checksums);
635   SourceFiles.push_back(std::move(SrcFile));
636   FileNameOffsetToId[Checksums.FileNameOffset] = Id;
637   return Id;
638 }
639 
640 
641