NativeFunctionSymbol.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //===- NativeFunctionSymbol.cpp - info about function symbols----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/DebugInfo/PDB/Native/NativeFunctionSymbol.h"
  9. #include "llvm/DebugInfo/CodeView/CVRecord.h"
  10. #include "llvm/DebugInfo/CodeView/CodeView.h"
  11. #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
  12. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  13. #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
  14. #include "llvm/DebugInfo/PDB/Native/NativeEnumSymbols.h"
  15. #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
  16. #include "llvm/DebugInfo/PDB/Native/SymbolCache.h"
  17. #include "llvm/DebugInfo/PDB/PDBExtras.h"
  18. using namespace llvm;
  19. using namespace llvm::codeview;
  20. using namespace llvm::pdb;
  21. NativeFunctionSymbol::NativeFunctionSymbol(NativeSession &Session,
  22. SymIndexId Id,
  23. const codeview::ProcSym &Sym,
  24. uint32_t Offset)
  25. : NativeRawSymbol(Session, PDB_SymType::Function, Id), Sym(Sym),
  26. RecordOffset(Offset) {}
  27. NativeFunctionSymbol::~NativeFunctionSymbol() = default;
  28. void NativeFunctionSymbol::dump(raw_ostream &OS, int Indent,
  29. PdbSymbolIdField ShowIdFields,
  30. PdbSymbolIdField RecurseIdFields) const {
  31. NativeRawSymbol::dump(OS, Indent, ShowIdFields, RecurseIdFields);
  32. dumpSymbolField(OS, "name", getName(), Indent);
  33. dumpSymbolField(OS, "length", getLength(), Indent);
  34. dumpSymbolField(OS, "offset", getAddressOffset(), Indent);
  35. dumpSymbolField(OS, "section", getAddressSection(), Indent);
  36. }
  37. uint32_t NativeFunctionSymbol::getAddressOffset() const {
  38. return Sym.CodeOffset;
  39. }
  40. uint32_t NativeFunctionSymbol::getAddressSection() const { return Sym.Segment; }
  41. std::string NativeFunctionSymbol::getName() const {
  42. return std::string(Sym.Name);
  43. }
  44. uint64_t NativeFunctionSymbol::getLength() const { return Sym.CodeSize; }
  45. uint32_t NativeFunctionSymbol::getRelativeVirtualAddress() const {
  46. return Session.getRVAFromSectOffset(Sym.Segment, Sym.CodeOffset);
  47. }
  48. uint64_t NativeFunctionSymbol::getVirtualAddress() const {
  49. return Session.getVAFromSectOffset(Sym.Segment, Sym.CodeOffset);
  50. }
  51. static bool inlineSiteContainsAddress(InlineSiteSym &IS,
  52. uint32_t OffsetInFunc) {
  53. // Returns true if inline site contains the offset.
  54. bool Found = false;
  55. uint32_t CodeOffset = 0;
  56. for (auto &Annot : IS.annotations()) {
  57. switch (Annot.OpCode) {
  58. case BinaryAnnotationsOpCode::CodeOffset:
  59. case BinaryAnnotationsOpCode::ChangeCodeOffset:
  60. case BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset:
  61. CodeOffset += Annot.U1;
  62. if (OffsetInFunc >= CodeOffset)
  63. Found = true;
  64. break;
  65. case BinaryAnnotationsOpCode::ChangeCodeLength:
  66. CodeOffset += Annot.U1;
  67. if (Found && OffsetInFunc < CodeOffset)
  68. return true;
  69. Found = false;
  70. break;
  71. case BinaryAnnotationsOpCode::ChangeCodeLengthAndCodeOffset:
  72. CodeOffset += Annot.U2;
  73. if (OffsetInFunc >= CodeOffset && OffsetInFunc < CodeOffset + Annot.U1)
  74. return true;
  75. Found = false;
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. return false;
  82. }
  83. std::unique_ptr<IPDBEnumSymbols>
  84. NativeFunctionSymbol::findInlineFramesByVA(uint64_t VA) const {
  85. uint16_t Modi;
  86. if (!Session.moduleIndexForVA(VA, Modi))
  87. return nullptr;
  88. Expected<ModuleDebugStreamRef> ModS = Session.getModuleDebugStream(Modi);
  89. if (!ModS) {
  90. consumeError(ModS.takeError());
  91. return nullptr;
  92. }
  93. CVSymbolArray Syms = ModS->getSymbolArray();
  94. // Search for inline sites. There should be one matching top level inline
  95. // site. Then search in its nested inline sites.
  96. std::vector<SymIndexId> Frames;
  97. uint32_t CodeOffset = VA - getVirtualAddress();
  98. auto Start = Syms.at(RecordOffset);
  99. auto End = Syms.at(Sym.End);
  100. while (Start != End) {
  101. bool Found = false;
  102. // Find matching inline site within Start and End.
  103. for (; Start != End; ++Start) {
  104. if (Start->kind() != S_INLINESITE)
  105. continue;
  106. InlineSiteSym IS =
  107. cantFail(SymbolDeserializer::deserializeAs<InlineSiteSym>(*Start));
  108. if (inlineSiteContainsAddress(IS, CodeOffset)) {
  109. // Insert frames in reverse order.
  110. SymIndexId Id = Session.getSymbolCache().getOrCreateInlineSymbol(
  111. IS, getVirtualAddress(), Modi, Start.offset());
  112. Frames.insert(Frames.begin(), Id);
  113. // Update offsets to search within this inline site.
  114. ++Start;
  115. End = Syms.at(IS.End);
  116. Found = true;
  117. break;
  118. }
  119. Start = Syms.at(IS.End);
  120. if (Start == End)
  121. break;
  122. }
  123. if (!Found)
  124. break;
  125. }
  126. return std::make_unique<NativeEnumSymbols>(Session, std::move(Frames));
  127. }