1 //===-- DWARFDebugRanges.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 "DWARFDebugRanges.h"
10 #include "DWARFUnit.h"
11 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
12 
13 using namespace lldb_private;
14 
15 DWARFDebugRanges::DWARFDebugRanges() : m_range_map() {}
16 
17 void DWARFDebugRanges::Extract(DWARFContext &context) {
18   llvm::DWARFDataExtractor extractor =
19       context.getOrLoadRangesData().GetAsLLVMDWARF();
20   llvm::DWARFDebugRangeList extracted_list;
21   uint64_t current_offset = 0;
22   auto extract_next_list = [&] {
23     if (auto error = extracted_list.extract(extractor, &current_offset)) {
24       consumeError(std::move(error));
25       return false;
26     }
27     return true;
28   };
29 
30   uint64_t previous_offset = current_offset;
31   while (extractor.isValidOffset(current_offset) && extract_next_list()) {
32     DWARFRangeList &lldb_range_list = m_range_map[previous_offset];
33     lldb_range_list.Reserve(extracted_list.getEntries().size());
34     for (auto &range : extracted_list.getEntries())
35       lldb_range_list.Append(range.StartAddress,
36                              range.EndAddress - range.StartAddress);
37     lldb_range_list.Sort();
38     previous_offset = current_offset;
39   }
40 }
41 
42 DWARFRangeList
43 DWARFDebugRanges::FindRanges(const DWARFUnit *cu,
44                              dw_offset_t debug_ranges_offset) const {
45   dw_addr_t debug_ranges_address = cu->GetRangesBase() + debug_ranges_offset;
46   auto pos = m_range_map.find(debug_ranges_address);
47   DWARFRangeList ans =
48       pos == m_range_map.end() ? DWARFRangeList() : pos->second;
49 
50   // All DW_AT_ranges are relative to the base address of the compile
51   // unit. We add the compile unit base address to make sure all the
52   // addresses are properly fixed up.
53   ans.Slide(cu->GetBaseAddress());
54   return ans;
55 }
56