SymbolCache.cpp 22 KB

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