1 //===-- PdbIndex.cpp --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
12 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
13 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
14 #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
15 #include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h"
16 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
17 #include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
18 #include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
19 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
20 #include "llvm/Object/COFF.h"
21 #include "llvm/Support/Error.h"
23 #include "lldb/Utility/LLDBAssert.h"
24 #include "lldb/lldb-defines.h"
26 using namespace lldb_private;
27 using namespace lldb_private::npdb;
28 using namespace llvm::codeview;
29 using namespace llvm::pdb;
31 PdbIndex::PdbIndex() : m_cus(*this), m_va_to_modi(m_allocator) {}
33 #define ASSIGN_PTR_OR_RETURN(result_ptr, expr) \
35 auto expected_result = expr; \
36 if (!expected_result) \
37 return expected_result.takeError(); \
38 result_ptr = &expected_result.get(); \
41 llvm::Expected<std::unique_ptr<PdbIndex>>
42 PdbIndex::create(std::unique_ptr<llvm::pdb::PDBFile> file) {
45 std::unique_ptr<PdbIndex> result(new PdbIndex());
46 ASSIGN_PTR_OR_RETURN(result->m_dbi, file->getPDBDbiStream());
47 ASSIGN_PTR_OR_RETURN(result->m_tpi, file->getPDBTpiStream());
48 ASSIGN_PTR_OR_RETURN(result->m_ipi, file->getPDBIpiStream());
49 ASSIGN_PTR_OR_RETURN(result->m_info, file->getPDBInfoStream());
50 ASSIGN_PTR_OR_RETURN(result->m_publics, file->getPDBPublicsStream());
51 ASSIGN_PTR_OR_RETURN(result->m_globals, file->getPDBGlobalsStream());
52 ASSIGN_PTR_OR_RETURN(result->m_symrecords, file->getPDBSymbolStream());
54 result->m_tpi->buildHashMap();
56 result->m_file = std::move(file);
58 return std::move(result);
61 lldb::addr_t PdbIndex::MakeVirtualAddress(uint16_t segment,
62 uint32_t offset) const {
63 // Segment indices are 1-based.
64 lldbassert(segment > 0);
66 uint32_t max_section = dbi().getSectionHeaders().size();
67 lldbassert(segment <= max_section + 1);
69 // If this is an absolute symbol, it's indicated by the magic section index
70 // |max_section+1|. In this case, the offset is meaningless, so just return.
71 if (segment == max_section + 1)
72 return LLDB_INVALID_ADDRESS;
74 const llvm::object::coff_section &cs = dbi().getSectionHeaders()[segment - 1];
75 return m_load_address + static_cast<lldb::addr_t>(cs.VirtualAddress) +
76 static_cast<lldb::addr_t>(offset);
79 lldb::addr_t PdbIndex::MakeVirtualAddress(const SegmentOffset &so) const {
80 return MakeVirtualAddress(so.segment, so.offset);
83 llvm::Optional<uint16_t>
84 PdbIndex::GetModuleIndexForAddr(uint16_t segment, uint32_t offset) const {
85 return GetModuleIndexForVa(MakeVirtualAddress(segment, offset));
88 llvm::Optional<uint16_t> PdbIndex::GetModuleIndexForVa(lldb::addr_t va) const {
89 auto iter = m_va_to_modi.find(va);
90 if (iter == m_va_to_modi.end())
96 void PdbIndex::ParseSectionContribs() {
97 class Visitor : public ISectionContribVisitor {
99 llvm::IntervalMap<uint64_t, uint16_t> &m_imap;
102 Visitor(PdbIndex &ctx, llvm::IntervalMap<uint64_t, uint16_t> &imap)
103 : m_ctx(ctx), m_imap(imap) {}
105 void visit(const SectionContrib &C) override {
109 uint64_t va = m_ctx.MakeVirtualAddress(C.ISect, C.Off);
110 uint64_t end = va + C.Size;
111 // IntervalMap's start and end represent a closed range, not a half-open
112 // range, so we have to subtract 1.
113 m_imap.insert(va, end - 1, C.Imod);
115 void visit(const SectionContrib2 &C) override { visit(C.Base); }
117 Visitor v(*this, m_va_to_modi);
118 dbi().visitSectionContributions(v);
121 void PdbIndex::BuildAddrToSymbolMap(CompilandIndexItem &cci) {
122 lldbassert(cci.m_symbols_by_va.empty() &&
123 "Addr to symbol map is already built!");
124 uint16_t modi = cci.m_id.modi;
125 const CVSymbolArray &syms = cci.m_debug_stream.getSymbolArray();
126 for (auto iter = syms.begin(); iter != syms.end(); ++iter) {
127 if (!SymbolHasAddress(*iter))
130 SegmentOffset so = GetSegmentAndOffset(*iter);
131 lldb::addr_t va = MakeVirtualAddress(so);
133 PdbCompilandSymId cu_sym_id(modi, iter.offset());
135 // It's rare, but we could have multiple symbols with the same address
136 // because of identical comdat folding. Right now, the first one will win.
137 cci.m_symbols_by_va.insert(std::make_pair(va, PdbSymUid(cu_sym_id)));
141 std::vector<SymbolAndUid> PdbIndex::FindSymbolsByVa(lldb::addr_t va) {
142 std::vector<SymbolAndUid> result;
144 llvm::Optional<uint16_t> modi = GetModuleIndexForVa(va);
148 CompilandIndexItem &cci = compilands().GetOrCreateCompiland(*modi);
149 if (cci.m_symbols_by_va.empty())
150 BuildAddrToSymbolMap(cci);
152 // The map is sorted by starting address of the symbol. So for example
153 // we could (in theory) have this situation
155 // [------------------]
161 // ^ Address we're searching for
162 // In order to find this, we use the upper_bound of the key value which would
163 // be the first symbol whose starting address is higher than the element we're
166 auto ub = cci.m_symbols_by_va.upper_bound(va);
168 for (auto iter = cci.m_symbols_by_va.begin(); iter != ub; ++iter) {
169 PdbCompilandSymId cu_sym_id = iter->second.asCompilandSym();
170 CVSymbol sym = ReadSymbolRecord(cu_sym_id);
172 SegmentOffsetLength sol;
173 if (SymbolIsCode(sym))
174 sol = GetSegmentOffsetAndLength(sym);
176 sol.so = GetSegmentAndOffset(sym);
178 lldb::addr_t start = MakeVirtualAddress(sol.so);
179 lldb::addr_t end = start + sol.length;
180 if (va >= start && va < end)
181 result.push_back({std::move(sym), iter->second});
187 CVSymbol PdbIndex::ReadSymbolRecord(PdbCompilandSymId cu_sym) const {
188 const CompilandIndexItem *cci = compilands().GetCompiland(cu_sym.modi);
189 auto iter = cci->m_debug_stream.getSymbolArray().at(cu_sym.offset);
190 lldbassert(iter != cci->m_debug_stream.getSymbolArray().end());
194 CVSymbol PdbIndex::ReadSymbolRecord(PdbGlobalSymId global) const {
195 return symrecords().readRecord(global.offset);