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