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 uint32_t DWARFDebugInfo::FindUnitIndex(DIERef::Section section,
120                                        dw_offset_t offset) {
121   ParseUnitHeadersIfNeeded();
122 
123   // llvm::lower_bound is not used as for DIE offsets it would still return
124   // index +1 and GetOffset() returning index itself would be a special case.
125   auto pos = llvm::upper_bound(
126       m_units, std::make_pair(section, offset),
127       [](const std::pair<DIERef::Section, dw_offset_t> &lhs,
128          const DWARFUnitSP &rhs) {
129         return lhs < std::make_pair(rhs->GetDebugSection(), rhs->GetOffset());
130       });
131   uint32_t idx = std::distance(m_units.begin(), pos);
132   if (idx == 0)
133     return DW_INVALID_OFFSET;
134   return idx - 1;
135 }
136 
137 DWARFUnit *DWARFDebugInfo::GetUnitAtOffset(DIERef::Section section,
138                                            dw_offset_t cu_offset,
139                                            uint32_t *idx_ptr) {
140   uint32_t idx = FindUnitIndex(section, cu_offset);
141   DWARFUnit *result = GetUnitAtIndex(idx);
142   if (result && result->GetOffset() != cu_offset) {
143     result = nullptr;
144     idx = DW_INVALID_INDEX;
145   }
146   if (idx_ptr)
147     *idx_ptr = idx;
148   return result;
149 }
150 
151 DWARFUnit *DWARFDebugInfo::GetUnit(const DIERef &die_ref) {
152   if (die_ref.cu_offset == DW_INVALID_OFFSET)
153     return GetUnitContainingDIEOffset(die_ref.section, die_ref.die_offset);
154   else
155     return GetUnitAtOffset(die_ref.section, die_ref.cu_offset);
156 }
157 
158 DWARFUnit *
159 DWARFDebugInfo::GetUnitContainingDIEOffset(DIERef::Section section,
160                                            dw_offset_t die_offset) {
161   uint32_t idx = FindUnitIndex(section, die_offset);
162   DWARFUnit *result = GetUnitAtIndex(idx);
163   if (result && !result->ContainsDIEOffset(die_offset))
164     return nullptr;
165   return result;
166 }
167 
168 DWARFDIE
169 DWARFDebugInfo::GetDIEForDIEOffset(DIERef::Section section,
170                                    dw_offset_t die_offset) {
171   DWARFUnit *cu = GetUnitContainingDIEOffset(section, die_offset);
172   if (cu)
173     return cu->GetDIE(die_offset);
174   return DWARFDIE();
175 }
176 
177 // GetDIE()
178 //
179 // Get the DIE (Debug Information Entry) with the specified offset.
180 DWARFDIE
181 DWARFDebugInfo::GetDIE(const DIERef &die_ref) {
182   DWARFUnit *cu = GetUnit(die_ref);
183   if (cu)
184     return cu->GetDIE(die_ref.die_offset);
185   return DWARFDIE(); // Not found
186 }
187 
188