15ffd83dbSDimitry Andric //===- NativeFunctionSymbol.cpp - info about function symbols----*- C++ -*-===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric 
95ffd83dbSDimitry Andric #include "llvm/DebugInfo/PDB/Native/NativeFunctionSymbol.h"
105ffd83dbSDimitry Andric 
11*af732203SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
125ffd83dbSDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
13*af732203SDimitry Andric #include "llvm/DebugInfo/PDB/Native/NativeEnumSymbols.h"
145ffd83dbSDimitry Andric #include "llvm/DebugInfo/PDB/Native/NativeTypeBuiltin.h"
155ffd83dbSDimitry Andric #include "llvm/DebugInfo/PDB/Native/NativeTypeEnum.h"
165ffd83dbSDimitry Andric 
175ffd83dbSDimitry Andric using namespace llvm;
185ffd83dbSDimitry Andric using namespace llvm::codeview;
195ffd83dbSDimitry Andric using namespace llvm::pdb;
205ffd83dbSDimitry Andric 
NativeFunctionSymbol(NativeSession & Session,SymIndexId Id,const codeview::ProcSym & Sym,uint32_t Offset)215ffd83dbSDimitry Andric NativeFunctionSymbol::NativeFunctionSymbol(NativeSession &Session,
225ffd83dbSDimitry Andric                                            SymIndexId Id,
23*af732203SDimitry Andric                                            const codeview::ProcSym &Sym,
24*af732203SDimitry Andric                                            uint32_t Offset)
25*af732203SDimitry Andric     : NativeRawSymbol(Session, PDB_SymType::Function, Id), Sym(Sym),
26*af732203SDimitry Andric       RecordOffset(Offset) {}
275ffd83dbSDimitry Andric 
~NativeFunctionSymbol()285ffd83dbSDimitry Andric NativeFunctionSymbol::~NativeFunctionSymbol() {}
295ffd83dbSDimitry Andric 
dump(raw_ostream & OS,int Indent,PdbSymbolIdField ShowIdFields,PdbSymbolIdField RecurseIdFields) const305ffd83dbSDimitry Andric void NativeFunctionSymbol::dump(raw_ostream &OS, int Indent,
315ffd83dbSDimitry Andric                                 PdbSymbolIdField ShowIdFields,
325ffd83dbSDimitry Andric                                 PdbSymbolIdField RecurseIdFields) const {
335ffd83dbSDimitry Andric   NativeRawSymbol::dump(OS, Indent, ShowIdFields, RecurseIdFields);
345ffd83dbSDimitry Andric   dumpSymbolField(OS, "name", getName(), Indent);
355ffd83dbSDimitry Andric   dumpSymbolField(OS, "length", getLength(), Indent);
365ffd83dbSDimitry Andric   dumpSymbolField(OS, "offset", getAddressOffset(), Indent);
375ffd83dbSDimitry Andric   dumpSymbolField(OS, "section", getAddressSection(), Indent);
385ffd83dbSDimitry Andric }
395ffd83dbSDimitry Andric 
getAddressOffset() const405ffd83dbSDimitry Andric uint32_t NativeFunctionSymbol::getAddressOffset() const {
415ffd83dbSDimitry Andric   return Sym.CodeOffset;
425ffd83dbSDimitry Andric }
435ffd83dbSDimitry Andric 
getAddressSection() const445ffd83dbSDimitry Andric uint32_t NativeFunctionSymbol::getAddressSection() const { return Sym.Segment; }
getName() const455ffd83dbSDimitry Andric std::string NativeFunctionSymbol::getName() const {
465ffd83dbSDimitry Andric   return std::string(Sym.Name);
475ffd83dbSDimitry Andric }
485ffd83dbSDimitry Andric 
getLength() const495ffd83dbSDimitry Andric uint64_t NativeFunctionSymbol::getLength() const { return Sym.CodeSize; }
505ffd83dbSDimitry Andric 
getRelativeVirtualAddress() const515ffd83dbSDimitry Andric uint32_t NativeFunctionSymbol::getRelativeVirtualAddress() const {
525ffd83dbSDimitry Andric   return Session.getRVAFromSectOffset(Sym.Segment, Sym.CodeOffset);
535ffd83dbSDimitry Andric }
545ffd83dbSDimitry Andric 
getVirtualAddress() const555ffd83dbSDimitry Andric uint64_t NativeFunctionSymbol::getVirtualAddress() const {
565ffd83dbSDimitry Andric   return Session.getVAFromSectOffset(Sym.Segment, Sym.CodeOffset);
575ffd83dbSDimitry Andric }
58*af732203SDimitry Andric 
inlineSiteContainsAddress(InlineSiteSym & IS,uint32_t OffsetInFunc)59*af732203SDimitry Andric static bool inlineSiteContainsAddress(InlineSiteSym &IS,
60*af732203SDimitry Andric                                       uint32_t OffsetInFunc) {
61*af732203SDimitry Andric   // Returns true if inline site contains the offset.
62*af732203SDimitry Andric   bool Found = false;
63*af732203SDimitry Andric   uint32_t CodeOffset = 0;
64*af732203SDimitry Andric   for (auto &Annot : IS.annotations()) {
65*af732203SDimitry Andric     switch (Annot.OpCode) {
66*af732203SDimitry Andric     case BinaryAnnotationsOpCode::CodeOffset:
67*af732203SDimitry Andric     case BinaryAnnotationsOpCode::ChangeCodeOffset:
68*af732203SDimitry Andric     case BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset:
69*af732203SDimitry Andric       CodeOffset += Annot.U1;
70*af732203SDimitry Andric       if (OffsetInFunc >= CodeOffset)
71*af732203SDimitry Andric         Found = true;
72*af732203SDimitry Andric       break;
73*af732203SDimitry Andric     case BinaryAnnotationsOpCode::ChangeCodeLength:
74*af732203SDimitry Andric       CodeOffset += Annot.U1;
75*af732203SDimitry Andric       if (Found && OffsetInFunc < CodeOffset)
76*af732203SDimitry Andric         return true;
77*af732203SDimitry Andric       Found = false;
78*af732203SDimitry Andric       break;
79*af732203SDimitry Andric     case BinaryAnnotationsOpCode::ChangeCodeLengthAndCodeOffset:
80*af732203SDimitry Andric       CodeOffset += Annot.U2;
81*af732203SDimitry Andric       if (OffsetInFunc >= CodeOffset && OffsetInFunc < CodeOffset + Annot.U1)
82*af732203SDimitry Andric         return true;
83*af732203SDimitry Andric       Found = false;
84*af732203SDimitry Andric       break;
85*af732203SDimitry Andric     default:
86*af732203SDimitry Andric       break;
87*af732203SDimitry Andric     }
88*af732203SDimitry Andric   }
89*af732203SDimitry Andric   return false;
90*af732203SDimitry Andric }
91*af732203SDimitry Andric 
92*af732203SDimitry Andric std::unique_ptr<IPDBEnumSymbols>
findInlineFramesByVA(uint64_t VA) const93*af732203SDimitry Andric NativeFunctionSymbol::findInlineFramesByVA(uint64_t VA) const {
94*af732203SDimitry Andric   uint16_t Modi;
95*af732203SDimitry Andric   if (!Session.moduleIndexForVA(VA, Modi))
96*af732203SDimitry Andric     return nullptr;
97*af732203SDimitry Andric 
98*af732203SDimitry Andric   Expected<ModuleDebugStreamRef> ModS = Session.getModuleDebugStream(Modi);
99*af732203SDimitry Andric   if (!ModS) {
100*af732203SDimitry Andric     consumeError(ModS.takeError());
101*af732203SDimitry Andric     return nullptr;
102*af732203SDimitry Andric   }
103*af732203SDimitry Andric   CVSymbolArray Syms = ModS->getSymbolArray();
104*af732203SDimitry Andric 
105*af732203SDimitry Andric   // Search for inline sites. There should be one matching top level inline
106*af732203SDimitry Andric   // site. Then search in its nested inline sites.
107*af732203SDimitry Andric   std::vector<SymIndexId> Frames;
108*af732203SDimitry Andric   uint32_t CodeOffset = VA - getVirtualAddress();
109*af732203SDimitry Andric   auto Start = Syms.at(RecordOffset);
110*af732203SDimitry Andric   auto End = Syms.at(Sym.End);
111*af732203SDimitry Andric   while (Start != End) {
112*af732203SDimitry Andric     bool Found = false;
113*af732203SDimitry Andric     // Find matching inline site within Start and End.
114*af732203SDimitry Andric     for (; Start != End; ++Start) {
115*af732203SDimitry Andric       if (Start->kind() != S_INLINESITE)
116*af732203SDimitry Andric         continue;
117*af732203SDimitry Andric 
118*af732203SDimitry Andric       InlineSiteSym IS =
119*af732203SDimitry Andric           cantFail(SymbolDeserializer::deserializeAs<InlineSiteSym>(*Start));
120*af732203SDimitry Andric       if (inlineSiteContainsAddress(IS, CodeOffset)) {
121*af732203SDimitry Andric         // Insert frames in reverse order.
122*af732203SDimitry Andric         SymIndexId Id = Session.getSymbolCache().getOrCreateInlineSymbol(
123*af732203SDimitry Andric             IS, getVirtualAddress(), Modi, Start.offset());
124*af732203SDimitry Andric         Frames.insert(Frames.begin(), Id);
125*af732203SDimitry Andric 
126*af732203SDimitry Andric         // Update offsets to search within this inline site.
127*af732203SDimitry Andric         ++Start;
128*af732203SDimitry Andric         End = Syms.at(IS.End);
129*af732203SDimitry Andric         Found = true;
130*af732203SDimitry Andric         break;
131*af732203SDimitry Andric       }
132*af732203SDimitry Andric 
133*af732203SDimitry Andric       Start = Syms.at(IS.End);
134*af732203SDimitry Andric       if (Start == End)
135*af732203SDimitry Andric         break;
136*af732203SDimitry Andric     }
137*af732203SDimitry Andric 
138*af732203SDimitry Andric     if (!Found)
139*af732203SDimitry Andric       break;
140*af732203SDimitry Andric   }
141*af732203SDimitry Andric 
142*af732203SDimitry Andric   return std::make_unique<NativeEnumSymbols>(Session, std::move(Frames));
143*af732203SDimitry Andric }
144