PdbIndex.cpp revision 360660
1//===-- PdbIndex.cpp --------------------------------------------*- 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 "PdbIndex.h"
10#include "PdbUtil.h"
11
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"
22
23#include "lldb/Utility/LLDBAssert.h"
24#include "lldb/lldb-defines.h"
25
26using namespace lldb_private;
27using namespace lldb_private::npdb;
28using namespace llvm::codeview;
29using namespace llvm::pdb;
30
31PdbIndex::PdbIndex() : m_cus(*this), m_va_to_modi(m_allocator) {}
32
33#define ASSIGN_PTR_OR_RETURN(result_ptr, expr)                                 \
34  {                                                                            \
35    auto expected_result = expr;                                               \
36    if (!expected_result)                                                      \
37      return expected_result.takeError();                                      \
38    result_ptr = &expected_result.get();                                       \
39  }
40
41llvm::Expected<std::unique_ptr<PdbIndex>>
42PdbIndex::create(std::unique_ptr<llvm::pdb::PDBFile> file) {
43  lldbassert(file);
44
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());
53
54  result->m_tpi->buildHashMap();
55
56  result->m_file = std::move(file);
57
58  return std::move(result);
59}
60
61lldb::addr_t PdbIndex::MakeVirtualAddress(uint16_t segment,
62                                          uint32_t offset) const {
63  // Segment indices are 1-based.
64  lldbassert(segment > 0);
65
66  uint32_t max_section = dbi().getSectionHeaders().size();
67  lldbassert(segment <= max_section + 1);
68
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;
73
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);
77}
78
79lldb::addr_t PdbIndex::MakeVirtualAddress(const SegmentOffset &so) const {
80  return MakeVirtualAddress(so.segment, so.offset);
81}
82
83llvm::Optional<uint16_t>
84PdbIndex::GetModuleIndexForAddr(uint16_t segment, uint32_t offset) const {
85  return GetModuleIndexForVa(MakeVirtualAddress(segment, offset));
86}
87
88llvm::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())
91    return llvm::None;
92
93  return iter.value();
94}
95
96void PdbIndex::ParseSectionContribs() {
97  class Visitor : public ISectionContribVisitor {
98    PdbIndex &m_ctx;
99    llvm::IntervalMap<uint64_t, uint16_t> &m_imap;
100
101  public:
102    Visitor(PdbIndex &ctx, llvm::IntervalMap<uint64_t, uint16_t> &imap)
103        : m_ctx(ctx), m_imap(imap) {}
104
105    void visit(const SectionContrib &C) override {
106      if (C.Size == 0)
107        return;
108
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);
114    }
115    void visit(const SectionContrib2 &C) override { visit(C.Base); }
116  };
117  Visitor v(*this, m_va_to_modi);
118  dbi().visitSectionContributions(v);
119}
120
121void 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))
128      continue;
129
130    SegmentOffset so = GetSegmentAndOffset(*iter);
131    lldb::addr_t va = MakeVirtualAddress(so);
132
133    PdbCompilandSymId cu_sym_id(modi, iter.offset());
134
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)));
138  }
139}
140
141std::vector<SymbolAndUid> PdbIndex::FindSymbolsByVa(lldb::addr_t va) {
142  std::vector<SymbolAndUid> result;
143
144  llvm::Optional<uint16_t> modi = GetModuleIndexForVa(va);
145  if (!modi)
146    return result;
147
148  CompilandIndexItem &cci = compilands().GetOrCreateCompiland(*modi);
149  if (cci.m_symbols_by_va.empty())
150    BuildAddrToSymbolMap(cci);
151
152  // The map is sorted by starting address of the symbol.  So for example
153  // we could (in theory) have this situation
154  //
155  // [------------------]
156  //    [----------]
157  //      [-----------]
158  //          [-------------]
159  //            [----]
160  //               [-----]
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
164  // searching for.
165
166  auto ub = cci.m_symbols_by_va.upper_bound(va);
167
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);
171
172    SegmentOffsetLength sol;
173    if (SymbolIsCode(sym))
174      sol = GetSegmentOffsetAndLength(sym);
175    else
176      sol.so = GetSegmentAndOffset(sym);
177
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});
182  }
183
184  return result;
185}
186
187CVSymbol 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());
191  return *iter;
192}
193
194CVSymbol PdbIndex::ReadSymbolRecord(PdbGlobalSymId global) const {
195  return symrecords().readRecord(global.offset);
196}
197