SymbolCache.cpp 22 KB

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