1 //===-- DWARFDebugInfo.cpp ------------------------------------------------===//
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 "SymbolFileDWARF.h"
10
11 #include <algorithm>
12 #include <set>
13
14 #include "lldb/Host/PosixApi.h"
15 #include "lldb/Symbol/ObjectFile.h"
16 #include "lldb/Utility/RegularExpression.h"
17 #include "lldb/Utility/Stream.h"
18 #include "llvm/Support/Casting.h"
19
20 #include "DWARFCompileUnit.h"
21 #include "DWARFContext.h"
22 #include "DWARFDebugAranges.h"
23 #include "DWARFDebugInfo.h"
24 #include "DWARFDebugInfoEntry.h"
25 #include "DWARFFormValue.h"
26 #include "DWARFTypeUnit.h"
27
28 using namespace lldb;
29 using namespace lldb_private;
30 using namespace std;
31
32 // Constructor
DWARFDebugInfo(SymbolFileDWARF & dwarf,lldb_private::DWARFContext & context)33 DWARFDebugInfo::DWARFDebugInfo(SymbolFileDWARF &dwarf,
34 lldb_private::DWARFContext &context)
35 : m_dwarf(dwarf), m_context(context), m_units(), m_cu_aranges_up() {}
36
GetCompileUnitAranges()37 const DWARFDebugAranges &DWARFDebugInfo::GetCompileUnitAranges() {
38 if (m_cu_aranges_up)
39 return *m_cu_aranges_up;
40
41 m_cu_aranges_up = std::make_unique<DWARFDebugAranges>();
42 const DWARFDataExtractor &debug_aranges_data =
43 m_context.getOrLoadArangesData();
44
45 // Extract what we can from the .debug_aranges first.
46 m_cu_aranges_up->extract(debug_aranges_data);
47
48 // Make a list of all CUs represented by the .debug_aranges data.
49 std::set<dw_offset_t> cus_with_data;
50 for (size_t n = 0; n < m_cu_aranges_up->GetNumRanges(); n++) {
51 dw_offset_t offset = m_cu_aranges_up->OffsetAtIndex(n);
52 if (offset != DW_INVALID_OFFSET)
53 cus_with_data.insert(offset);
54 }
55
56 // Manually build arange data for everything that wasn't in .debug_aranges.
57 const size_t num_units = GetNumUnits();
58 for (size_t idx = 0; idx < num_units; ++idx) {
59 DWARFUnit *cu = GetUnitAtIndex(idx);
60
61 dw_offset_t offset = cu->GetOffset();
62 if (cus_with_data.find(offset) == cus_with_data.end())
63 cu->BuildAddressRangeTable(m_cu_aranges_up.get());
64 }
65
66 const bool minimize = true;
67 m_cu_aranges_up->Sort(minimize);
68 return *m_cu_aranges_up;
69 }
70
ParseUnitsFor(DIERef::Section section)71 void DWARFDebugInfo::ParseUnitsFor(DIERef::Section section) {
72 DWARFDataExtractor data = section == DIERef::Section::DebugTypes
73 ? m_context.getOrLoadDebugTypesData()
74 : m_context.getOrLoadDebugInfoData();
75 lldb::offset_t offset = 0;
76 while (data.ValidOffset(offset)) {
77 llvm::Expected<DWARFUnitSP> unit_sp = DWARFUnit::extract(
78 m_dwarf, m_units.size(), data, section, &offset);
79
80 if (!unit_sp) {
81 // FIXME: Propagate this error up.
82 llvm::consumeError(unit_sp.takeError());
83 return;
84 }
85
86 // If it didn't return an error, then it should be returning a valid Unit.
87 assert(*unit_sp);
88 m_units.push_back(*unit_sp);
89 offset = (*unit_sp)->GetNextUnitOffset();
90
91 if (auto *type_unit = llvm::dyn_cast<DWARFTypeUnit>(unit_sp->get())) {
92 m_type_hash_to_unit_index.emplace_back(type_unit->GetTypeHash(),
93 unit_sp.get()->GetID());
94 }
95 }
96 }
97
ParseUnitHeadersIfNeeded()98 void DWARFDebugInfo::ParseUnitHeadersIfNeeded() {
99 llvm::call_once(m_units_once_flag, [&] {
100 ParseUnitsFor(DIERef::Section::DebugInfo);
101 ParseUnitsFor(DIERef::Section::DebugTypes);
102 llvm::sort(m_type_hash_to_unit_index, llvm::less_first());
103 });
104 }
105
GetNumUnits()106 size_t DWARFDebugInfo::GetNumUnits() {
107 ParseUnitHeadersIfNeeded();
108 return m_units.size();
109 }
110
GetUnitAtIndex(size_t idx)111 DWARFUnit *DWARFDebugInfo::GetUnitAtIndex(size_t idx) {
112 DWARFUnit *cu = nullptr;
113 if (idx < GetNumUnits())
114 cu = m_units[idx].get();
115 return cu;
116 }
117
FindUnitIndex(DIERef::Section section,dw_offset_t offset)118 uint32_t DWARFDebugInfo::FindUnitIndex(DIERef::Section section,
119 dw_offset_t offset) {
120 ParseUnitHeadersIfNeeded();
121
122 // llvm::lower_bound is not used as for DIE offsets it would still return
123 // index +1 and GetOffset() returning index itself would be a special case.
124 auto pos = llvm::upper_bound(
125 m_units, std::make_pair(section, offset),
126 [](const std::pair<DIERef::Section, dw_offset_t> &lhs,
127 const DWARFUnitSP &rhs) {
128 return lhs < std::make_pair(rhs->GetDebugSection(), rhs->GetOffset());
129 });
130 uint32_t idx = std::distance(m_units.begin(), pos);
131 if (idx == 0)
132 return DW_INVALID_OFFSET;
133 return idx - 1;
134 }
135
GetUnitAtOffset(DIERef::Section section,dw_offset_t cu_offset,uint32_t * idx_ptr)136 DWARFUnit *DWARFDebugInfo::GetUnitAtOffset(DIERef::Section section,
137 dw_offset_t cu_offset,
138 uint32_t *idx_ptr) {
139 uint32_t idx = FindUnitIndex(section, cu_offset);
140 DWARFUnit *result = GetUnitAtIndex(idx);
141 if (result && result->GetOffset() != cu_offset) {
142 result = nullptr;
143 idx = DW_INVALID_INDEX;
144 }
145 if (idx_ptr)
146 *idx_ptr = idx;
147 return result;
148 }
149
GetUnit(const DIERef & die_ref)150 DWARFUnit *DWARFDebugInfo::GetUnit(const DIERef &die_ref) {
151 return GetUnitContainingDIEOffset(die_ref.section(), die_ref.die_offset());
152 }
153
154 DWARFUnit *
GetUnitContainingDIEOffset(DIERef::Section section,dw_offset_t die_offset)155 DWARFDebugInfo::GetUnitContainingDIEOffset(DIERef::Section section,
156 dw_offset_t die_offset) {
157 uint32_t idx = FindUnitIndex(section, die_offset);
158 DWARFUnit *result = GetUnitAtIndex(idx);
159 if (result && !result->ContainsDIEOffset(die_offset))
160 return nullptr;
161 return result;
162 }
163
GetTypeUnitForHash(uint64_t hash)164 DWARFTypeUnit *DWARFDebugInfo::GetTypeUnitForHash(uint64_t hash) {
165 auto pos = llvm::lower_bound(m_type_hash_to_unit_index,
166 std::make_pair(hash, 0u), llvm::less_first());
167 if (pos == m_type_hash_to_unit_index.end() || pos->first != hash)
168 return nullptr;
169 return llvm::cast<DWARFTypeUnit>(GetUnitAtIndex(pos->second));
170 }
171
ContainsTypeUnits()172 bool DWARFDebugInfo::ContainsTypeUnits() {
173 ParseUnitHeadersIfNeeded();
174 return !m_type_hash_to_unit_index.empty();
175 }
176
177 // GetDIE()
178 //
179 // Get the DIE (Debug Information Entry) with the specified offset.
180 DWARFDIE
GetDIE(const DIERef & die_ref)181 DWARFDebugInfo::GetDIE(const DIERef &die_ref) {
182 DWARFUnit *cu = GetUnit(die_ref);
183 if (cu)
184 return cu->GetNonSkeletonUnit().GetDIE(die_ref.die_offset());
185 return DWARFDIE(); // Not found
186 }
187