blob: ae4a8038ccd7eea5a4875af3cf30ef571c54b431 [file] [log] [blame]
Zachary Turner21473f72015-02-08 00:29:29 +00001//===- PDBSymbolData.cpp - PDB data (e.g. variable) accessors ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Zachary Turner52c9f882015-02-14 03:53:56 +000010#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Aaron Smith1af50bc2018-03-26 22:17:12 +000011#include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
Zachary Turner29c69102015-02-23 05:58:34 +000012#include "llvm/DebugInfo/PDB/IPDBSession.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000013#include "llvm/DebugInfo/PDB/PDBSymDumper.h"
Zachary Turnera5549172015-02-10 22:43:25 +000014
Zachary Turner52c9f882015-02-14 03:53:56 +000015#include <utility>
16
Zachary Turner21473f72015-02-08 00:29:29 +000017using namespace llvm;
Zachary Turnerec28fc32016-05-04 20:32:13 +000018using namespace llvm::pdb;
Zachary Turner21473f72015-02-08 00:29:29 +000019
Zachary Turner98571ed2015-02-08 22:53:53 +000020PDBSymbolData::PDBSymbolData(const IPDBSession &PDBSession,
Zachary Turnerbae16b32015-02-08 20:58:09 +000021 std::unique_ptr<IPDBRawSymbol> DataSymbol)
Zachary Turner1b1a70f2017-04-10 06:14:09 +000022 : PDBSymbol(PDBSession, std::move(DataSymbol)) {
23 assert(RawSymbol->getSymTag() == PDB_SymType::Data);
Zachary Turner29c69102015-02-23 05:58:34 +000024}
25
Jim Grosbach4c98cf72015-05-15 19:13:31 +000026void PDBSymbolData::dump(PDBSymDumper &Dumper) const { Dumper.dump(*this); }
Aaron Smith1af50bc2018-03-26 22:17:12 +000027
28std::unique_ptr<IPDBEnumLineNumbers> PDBSymbolData::getLineNumbers() const {
29 auto Len = RawSymbol->getLength();
30 Len = Len ? Len : 1;
31 if (auto RVA = RawSymbol->getRelativeVirtualAddress())
32 return Session.findLineNumbersByRVA(RVA, Len);
33
34 if (auto Section = RawSymbol->getAddressSection())
35 return Session.findLineNumbersBySectOffset(
36 Section, RawSymbol->getAddressOffset(), Len);
37
38 return nullptr;
39}
40
41uint32_t PDBSymbolData::getCompilandId() const {
42 if (auto Lines = getLineNumbers()) {
43 if (auto FirstLine = Lines->getNext())
44 return FirstLine->getCompilandId();
45 }
46
47 uint32_t DataSection = RawSymbol->getAddressSection();
48 uint32_t DataOffset = RawSymbol->getAddressOffset();
49 if (DataSection == 0) {
50 if (auto RVA = RawSymbol->getRelativeVirtualAddress())
51 Session.addressForRVA(RVA, DataSection, DataOffset);
52 }
53
54 if (DataSection) {
55 if (auto SecContribs = Session.getSectionContribs()) {
56 while (auto Section = SecContribs->getNext()) {
57 if (Section->getAddressSection() == DataSection &&
58 Section->getAddressOffset() <= DataOffset &&
59 (Section->getAddressOffset() + Section->getLength()) > DataOffset)
60 return Section->getCompilandId();
61 }
62 }
63 } else {
64 auto LexParentId = RawSymbol->getLexicalParentId();
65 while (auto LexParent = Session.getSymbolById(LexParentId)) {
66 if (LexParent->getSymTag() == PDB_SymType::Exe)
67 break;
68 if (LexParent->getSymTag() == PDB_SymType::Compiland)
69 return LexParentId;
70 LexParentId = LexParent->getRawSymbol().getLexicalParentId();
71 }
72 }
73
74 return 0;
75}