xref: /freebsd/contrib/llvm-project/llvm/lib/DebugInfo/PDB/PDBSymbolData.cpp (revision 257e70f1d5ee61037c8c59b116538d3b6b1427a2)
1 //===- PDBSymbolData.cpp - PDB data (e.g. variable) accessors ---*- 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 
9 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
10 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
11 #include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
12 #include "llvm/DebugInfo/PDB/IPDBSession.h"
13 #include "llvm/DebugInfo/PDB/PDBSymDumper.h"
14 
15 using namespace llvm;
16 using namespace llvm::pdb;
17 
18 void PDBSymbolData::dump(PDBSymDumper &Dumper) const { Dumper.dump(*this); }
19 
20 std::unique_ptr<IPDBEnumLineNumbers> PDBSymbolData::getLineNumbers() const {
21   auto Len = RawSymbol->getLength();
22   Len = Len ? Len : 1;
23   if (auto RVA = RawSymbol->getRelativeVirtualAddress())
24     return Session.findLineNumbersByRVA(RVA, Len);
25 
26   if (auto Section = RawSymbol->getAddressSection())
27     return Session.findLineNumbersBySectOffset(
28         Section, RawSymbol->getAddressOffset(), Len);
29 
30   return nullptr;
31 }
32 
33 uint32_t PDBSymbolData::getCompilandId() const {
34   if (auto Lines = getLineNumbers()) {
35     if (auto FirstLine = Lines->getNext())
36       return FirstLine->getCompilandId();
37   }
38 
39   uint32_t DataSection = RawSymbol->getAddressSection();
40   uint32_t DataOffset = RawSymbol->getAddressOffset();
41   if (DataSection == 0) {
42     if (auto RVA = RawSymbol->getRelativeVirtualAddress())
43       Session.addressForRVA(RVA, DataSection, DataOffset);
44   }
45 
46   if (DataSection) {
47     if (auto SecContribs = Session.getSectionContribs()) {
48       while (auto Section = SecContribs->getNext()) {
49         if (Section->getAddressSection() == DataSection &&
50             Section->getAddressOffset() <= DataOffset &&
51             (Section->getAddressOffset() + Section->getLength()) > DataOffset)
52           return Section->getCompilandId();
53       }
54     }
55   } else {
56     auto LexParentId = RawSymbol->getLexicalParentId();
57     while (auto LexParent = Session.getSymbolById(LexParentId)) {
58       if (LexParent->getSymTag() == PDB_SymType::Exe)
59         break;
60       if (LexParent->getSymTag() == PDB_SymType::Compiland)
61         return LexParentId;
62       LexParentId = LexParent->getRawSymbol().getLexicalParentId();
63     }
64   }
65 
66   return 0;
67 }
68