1 //===-- DWARFDebugInfo.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 "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 
19 #include "DWARFCompileUnit.h"
20 #include "DWARFContext.h"
21 #include "DWARFDebugAranges.h"
22 #include "DWARFDebugInfo.h"
23 #include "DWARFDebugInfoEntry.h"
24 #include "DWARFFormValue.h"
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 using namespace std;
29 
30 // Constructor
31 DWARFDebugInfo::DWARFDebugInfo(lldb_private::DWARFContext &context)
32     : m_dwarf2Data(NULL), m_context(context), m_units(), m_cu_aranges_up() {}
33 
34 // SetDwarfData
35 void DWARFDebugInfo::SetDwarfData(SymbolFileDWARF *dwarf2Data) {
36   m_dwarf2Data = dwarf2Data;
37   m_units.clear();
38 }
39 
40 llvm::Expected<DWARFDebugAranges &> DWARFDebugInfo::GetCompileUnitAranges() {
41   if (m_cu_aranges_up)
42     return *m_cu_aranges_up;
43 
44   assert(m_dwarf2Data);
45 
46   m_cu_aranges_up = llvm::make_unique<DWARFDebugAranges>();
47   const DWARFDataExtractor *debug_aranges_data =
48       m_context.getOrLoadArangesData();
49   if (debug_aranges_data) {
50     llvm::Error error = m_cu_aranges_up->extract(*debug_aranges_data);
51     if (error)
52       return std::move(error);
53   }
54 
55   // Make a list of all CUs represented by the arange data in the file.
56   std::set<dw_offset_t> cus_with_data;
57   for (size_t n = 0; n < m_cu_aranges_up->GetNumRanges(); n++) {
58     dw_offset_t offset = m_cu_aranges_up->OffsetAtIndex(n);
59     if (offset != DW_INVALID_OFFSET)
60       cus_with_data.insert(offset);
61   }
62 
63   // Manually build arange data for everything that wasn't in the
64   // .debug_aranges table.
65   const size_t num_units = GetNumUnits();
66   for (size_t idx = 0; idx < num_units; ++idx) {
67     DWARFUnit *cu = GetUnitAtIndex(idx);
68 
69     dw_offset_t offset = cu->GetOffset();
70     if (cus_with_data.find(offset) == cus_with_data.end())
71       cu->BuildAddressRangeTable(m_dwarf2Data, m_cu_aranges_up.get());
72   }
73 
74   const bool minimize = true;
75   m_cu_aranges_up->Sort(minimize);
76   return *m_cu_aranges_up;
77 }
78 
79 void DWARFDebugInfo::ParseUnitHeadersIfNeeded() {
80   if (!m_units.empty())
81     return;
82   if (!m_dwarf2Data)
83     return;
84 
85   lldb::offset_t offset = 0;
86   const auto &debug_info_data = m_dwarf2Data->get_debug_info_data();
87 
88   while (debug_info_data.ValidOffset(offset)) {
89     llvm::Expected<DWARFUnitSP> cu_sp = DWARFCompileUnit::extract(
90         m_dwarf2Data, m_units.size(), debug_info_data, &offset);
91 
92     if (!cu_sp) {
93       // FIXME: Propagate this error up.
94       llvm::consumeError(cu_sp.takeError());
95       return;
96     }
97 
98     // If it didn't return an error, then it should be returning a valid Unit.
99     assert(*cu_sp);
100 
101     m_units.push_back(*cu_sp);
102 
103     offset = (*cu_sp)->GetNextUnitOffset();
104   }
105 }
106 
107 size_t DWARFDebugInfo::GetNumUnits() {
108   ParseUnitHeadersIfNeeded();
109   return m_units.size();
110 }
111 
112 DWARFUnit *DWARFDebugInfo::GetUnitAtIndex(user_id_t idx) {
113   DWARFUnit *cu = NULL;
114   if (idx < GetNumUnits())
115     cu = m_units[idx].get();
116   return cu;
117 }
118 
119 bool DWARFDebugInfo::OffsetLessThanUnitOffset(dw_offset_t offset,
120                                               const DWARFUnitSP &cu_sp) {
121   return offset < cu_sp->GetOffset();
122 }
123 
124 uint32_t DWARFDebugInfo::FindUnitIndex(dw_offset_t offset) {
125   ParseUnitHeadersIfNeeded();
126 
127   // llvm::lower_bound is not used as for DIE offsets it would still return
128   // index +1 and GetOffset() returning index itself would be a special case.
129   auto pos = llvm::upper_bound(m_units, offset, OffsetLessThanUnitOffset);
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 
136 DWARFUnit *DWARFDebugInfo::GetUnitAtOffset(dw_offset_t cu_offset,
137                                            uint32_t *idx_ptr) {
138   uint32_t idx = FindUnitIndex(cu_offset);
139   DWARFUnit *result = GetUnitAtIndex(idx);
140   if (result && result->GetOffset() != cu_offset) {
141     result = nullptr;
142     idx = DW_INVALID_INDEX;
143   }
144   if (idx_ptr)
145     *idx_ptr = idx;
146   return result;
147 }
148 
149 DWARFUnit *DWARFDebugInfo::GetUnit(const DIERef &die_ref) {
150   if (die_ref.cu_offset == DW_INVALID_OFFSET)
151     return GetUnitContainingDIEOffset(die_ref.die_offset);
152   else
153     return GetUnitAtOffset(die_ref.cu_offset);
154 }
155 
156 DWARFUnit *DWARFDebugInfo::GetUnitContainingDIEOffset(dw_offset_t die_offset) {
157   uint32_t idx = FindUnitIndex(die_offset);
158   DWARFUnit *result = GetUnitAtIndex(idx);
159   if (result && !result->ContainsDIEOffset(die_offset))
160     return nullptr;
161   return result;
162 }
163 
164 DWARFDIE
165 DWARFDebugInfo::GetDIEForDIEOffset(dw_offset_t die_offset) {
166   DWARFUnit *cu = GetUnitContainingDIEOffset(die_offset);
167   if (cu)
168     return cu->GetDIE(die_offset);
169   return DWARFDIE();
170 }
171 
172 // GetDIE()
173 //
174 // Get the DIE (Debug Information Entry) with the specified offset.
175 DWARFDIE
176 DWARFDebugInfo::GetDIE(const DIERef &die_ref) {
177   DWARFUnit *cu = GetUnit(die_ref);
178   if (cu)
179     return cu->GetDIE(die_ref.die_offset);
180   return DWARFDIE(); // Not found
181 }
182 
183