NativeFunctionSymbol.cpp 4.8 KB

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