121473f7bSZachary Turner //===- PDBSymbolData.cpp - PDB data (e.g. variable) accessors ---*- C++ -*-===//
221473f7bSZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
621473f7bSZachary Turner //
721473f7bSZachary Turner //===----------------------------------------------------------------------===//
821473f7bSZachary Turner 
952c9f881SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
10*eb4c8608Sserge-sans-paille #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
111af50bcfSAaron Smith #include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
1229c69105SZachary Turner #include "llvm/DebugInfo/PDB/IPDBSession.h"
139a818ad1SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymDumper.h"
14a5549178SZachary Turner 
1521473f7bSZachary Turner using namespace llvm;
16ec28fc34SZachary Turner using namespace llvm::pdb;
1721473f7bSZachary Turner 
dump(PDBSymDumper & Dumper) const18b52d08d9SZachary Turner void PDBSymbolData::dump(PDBSymDumper &Dumper) const { Dumper.dump(*this); }
191af50bcfSAaron Smith 
getLineNumbers() const201af50bcfSAaron Smith std::unique_ptr<IPDBEnumLineNumbers> PDBSymbolData::getLineNumbers() const {
211af50bcfSAaron Smith   auto Len = RawSymbol->getLength();
221af50bcfSAaron Smith   Len = Len ? Len : 1;
231af50bcfSAaron Smith   if (auto RVA = RawSymbol->getRelativeVirtualAddress())
241af50bcfSAaron Smith     return Session.findLineNumbersByRVA(RVA, Len);
251af50bcfSAaron Smith 
261af50bcfSAaron Smith   if (auto Section = RawSymbol->getAddressSection())
271af50bcfSAaron Smith     return Session.findLineNumbersBySectOffset(
281af50bcfSAaron Smith         Section, RawSymbol->getAddressOffset(), Len);
291af50bcfSAaron Smith 
301af50bcfSAaron Smith   return nullptr;
311af50bcfSAaron Smith }
321af50bcfSAaron Smith 
getCompilandId() const331af50bcfSAaron Smith uint32_t PDBSymbolData::getCompilandId() const {
341af50bcfSAaron Smith   if (auto Lines = getLineNumbers()) {
351af50bcfSAaron Smith     if (auto FirstLine = Lines->getNext())
361af50bcfSAaron Smith       return FirstLine->getCompilandId();
371af50bcfSAaron Smith   }
381af50bcfSAaron Smith 
391af50bcfSAaron Smith   uint32_t DataSection = RawSymbol->getAddressSection();
401af50bcfSAaron Smith   uint32_t DataOffset = RawSymbol->getAddressOffset();
411af50bcfSAaron Smith   if (DataSection == 0) {
421af50bcfSAaron Smith     if (auto RVA = RawSymbol->getRelativeVirtualAddress())
431af50bcfSAaron Smith       Session.addressForRVA(RVA, DataSection, DataOffset);
441af50bcfSAaron Smith   }
451af50bcfSAaron Smith 
461af50bcfSAaron Smith   if (DataSection) {
471af50bcfSAaron Smith     if (auto SecContribs = Session.getSectionContribs()) {
481af50bcfSAaron Smith       while (auto Section = SecContribs->getNext()) {
491af50bcfSAaron Smith         if (Section->getAddressSection() == DataSection &&
501af50bcfSAaron Smith             Section->getAddressOffset() <= DataOffset &&
511af50bcfSAaron Smith             (Section->getAddressOffset() + Section->getLength()) > DataOffset)
521af50bcfSAaron Smith           return Section->getCompilandId();
531af50bcfSAaron Smith       }
541af50bcfSAaron Smith     }
551af50bcfSAaron Smith   } else {
561af50bcfSAaron Smith     auto LexParentId = RawSymbol->getLexicalParentId();
571af50bcfSAaron Smith     while (auto LexParent = Session.getSymbolById(LexParentId)) {
581af50bcfSAaron Smith       if (LexParent->getSymTag() == PDB_SymType::Exe)
591af50bcfSAaron Smith         break;
601af50bcfSAaron Smith       if (LexParent->getSymTag() == PDB_SymType::Compiland)
611af50bcfSAaron Smith         return LexParentId;
621af50bcfSAaron Smith       LexParentId = LexParent->getRawSymbol().getLexicalParentId();
631af50bcfSAaron Smith     }
641af50bcfSAaron Smith   }
651af50bcfSAaron Smith 
661af50bcfSAaron Smith   return 0;
671af50bcfSAaron Smith }
68