1 //===-- DWARFDebugAranges.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 "DWARFDebugAranges.h"
10 
11 #include <assert.h>
12 #include <stdio.h>
13 
14 #include <algorithm>
15 
16 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/Stream.h"
18 #include "lldb/Utility/Timer.h"
19 
20 #include "DWARFUnit.h"
21 #include "DWARFDebugInfo.h"
22 #include "SymbolFileDWARF.h"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 //----------------------------------------------------------------------
28 // Constructor
29 //----------------------------------------------------------------------
30 DWARFDebugAranges::DWARFDebugAranges() : m_aranges() {}
31 
32 //----------------------------------------------------------------------
33 // CountArangeDescriptors
34 //----------------------------------------------------------------------
35 class CountArangeDescriptors {
36 public:
37   CountArangeDescriptors(uint32_t &count_ref) : count(count_ref) {
38     //      printf("constructor CountArangeDescriptors()\n");
39   }
40   void operator()(const DWARFDebugArangeSet &set) {
41     count += set.NumDescriptors();
42   }
43   uint32_t &count;
44 };
45 
46 //----------------------------------------------------------------------
47 // Extract
48 //----------------------------------------------------------------------
49 llvm::Error
50 DWARFDebugAranges::extract(const DWARFDataExtractor &debug_aranges_data) {
51   assert(debug_aranges_data.ValidOffset(0));
52 
53   lldb::offset_t offset = 0;
54 
55   DWARFDebugArangeSet set;
56   Range range;
57   while (debug_aranges_data.ValidOffset(offset)) {
58     llvm::Error error = set.extract(debug_aranges_data, &offset);
59     if (!error)
60       return error;
61 
62     const uint32_t num_descriptors = set.NumDescriptors();
63     if (num_descriptors > 0) {
64       const dw_offset_t cu_offset = set.GetHeader().cu_offset;
65 
66       for (uint32_t i = 0; i < num_descriptors; ++i) {
67         const DWARFDebugArangeSet::Descriptor &descriptor =
68             set.GetDescriptorRef(i);
69         m_aranges.Append(RangeToDIE::Entry(descriptor.address,
70                                            descriptor.length, cu_offset));
71       }
72     }
73     set.Clear();
74     }
75     return llvm::ErrorSuccess();
76 }
77 
78 void DWARFDebugAranges::Dump(Log *log) const {
79   if (log == NULL)
80     return;
81 
82   const size_t num_entries = m_aranges.GetSize();
83   for (size_t i = 0; i < num_entries; ++i) {
84     const RangeToDIE::Entry *entry = m_aranges.GetEntryAtIndex(i);
85     if (entry)
86       log->Printf("0x%8.8x: [0x%" PRIx64 " - 0x%" PRIx64 ")", entry->data,
87                   entry->GetRangeBase(), entry->GetRangeEnd());
88   }
89 }
90 
91 void DWARFDebugAranges::AppendRange(dw_offset_t offset, dw_addr_t low_pc,
92                                     dw_addr_t high_pc) {
93   if (high_pc > low_pc)
94     m_aranges.Append(RangeToDIE::Entry(low_pc, high_pc - low_pc, offset));
95 }
96 
97 void DWARFDebugAranges::Sort(bool minimize) {
98   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
99   Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
100                      static_cast<void *>(this));
101 
102   m_aranges.Sort();
103   m_aranges.CombineConsecutiveEntriesWithEqualData();
104 }
105 
106 //----------------------------------------------------------------------
107 // FindAddress
108 //----------------------------------------------------------------------
109 dw_offset_t DWARFDebugAranges::FindAddress(dw_addr_t address) const {
110   const RangeToDIE::Entry *entry = m_aranges.FindEntryThatContains(address);
111   if (entry)
112     return entry->data;
113   return DW_INVALID_OFFSET;
114 }
115