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_context.getOrLoadDebugInfoData(); 84 while (debug_info_data.ValidOffset(offset)) { 85 llvm::Expected<DWARFUnitSP> unit_sp = DWARFUnit::extract( 86 m_dwarf2Data, m_units.size(), debug_info_data, &offset); 87 88 if (!unit_sp) { 89 // FIXME: Propagate this error up. 90 llvm::consumeError(unit_sp.takeError()); 91 return; 92 } 93 94 // If it didn't return an error, then it should be returning a valid Unit. 95 assert(*unit_sp); 96 97 m_units.push_back(*unit_sp); 98 99 offset = (*unit_sp)->GetNextUnitOffset(); 100 } 101 } 102 103 size_t DWARFDebugInfo::GetNumUnits() { 104 ParseUnitHeadersIfNeeded(); 105 return m_units.size(); 106 } 107 108 DWARFUnit *DWARFDebugInfo::GetUnitAtIndex(user_id_t idx) { 109 DWARFUnit *cu = NULL; 110 if (idx < GetNumUnits()) 111 cu = m_units[idx].get(); 112 return cu; 113 } 114 115 uint32_t DWARFDebugInfo::FindUnitIndex(DIERef::Section section, 116 dw_offset_t offset) { 117 ParseUnitHeadersIfNeeded(); 118 119 // llvm::lower_bound is not used as for DIE offsets it would still return 120 // index +1 and GetOffset() returning index itself would be a special case. 121 auto pos = llvm::upper_bound( 122 m_units, std::make_pair(section, offset), 123 [](const std::pair<DIERef::Section, dw_offset_t> &lhs, 124 const DWARFUnitSP &rhs) { 125 return lhs < std::make_pair(rhs->GetDebugSection(), rhs->GetOffset()); 126 }); 127 uint32_t idx = std::distance(m_units.begin(), pos); 128 if (idx == 0) 129 return DW_INVALID_OFFSET; 130 return idx - 1; 131 } 132 133 DWARFUnit *DWARFDebugInfo::GetUnitAtOffset(DIERef::Section section, 134 dw_offset_t cu_offset, 135 uint32_t *idx_ptr) { 136 uint32_t idx = FindUnitIndex(section, cu_offset); 137 DWARFUnit *result = GetUnitAtIndex(idx); 138 if (result && result->GetOffset() != cu_offset) { 139 result = nullptr; 140 idx = DW_INVALID_INDEX; 141 } 142 if (idx_ptr) 143 *idx_ptr = idx; 144 return result; 145 } 146 147 DWARFUnit *DWARFDebugInfo::GetUnit(const DIERef &die_ref) { 148 if (die_ref.cu_offset == DW_INVALID_OFFSET) 149 return GetUnitContainingDIEOffset(die_ref.section, die_ref.die_offset); 150 else 151 return GetUnitAtOffset(die_ref.section, die_ref.cu_offset); 152 } 153 154 DWARFUnit * 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 164 DWARFDIE 165 DWARFDebugInfo::GetDIEForDIEOffset(DIERef::Section section, 166 dw_offset_t die_offset) { 167 DWARFUnit *cu = GetUnitContainingDIEOffset(section, die_offset); 168 if (cu) 169 return cu->GetDIE(die_offset); 170 return DWARFDIE(); 171 } 172 173 // GetDIE() 174 // 175 // Get the DIE (Debug Information Entry) with the specified offset. 176 DWARFDIE 177 DWARFDebugInfo::GetDIE(const DIERef &die_ref) { 178 DWARFUnit *cu = GetUnit(die_ref); 179 if (cu) 180 return cu->GetDIE(die_ref.die_offset); 181 return DWARFDIE(); // Not found 182 } 183 184