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.GetCompileUnitDIEOffset();
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 //----------------------------------------------------------------------
79 // Generate
80 //----------------------------------------------------------------------
81 bool DWARFDebugAranges::Generate(SymbolFileDWARF *dwarf2Data) {
82   Clear();
83   DWARFDebugInfo *debug_info = dwarf2Data->DebugInfo();
84   if (debug_info) {
85     uint32_t cu_idx = 0;
86     const uint32_t num_compile_units = dwarf2Data->GetNumCompileUnits();
87     for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
88       DWARFUnit *cu = debug_info->GetCompileUnitAtIndex(cu_idx);
89       if (cu)
90         cu->BuildAddressRangeTable(dwarf2Data, this);
91     }
92   }
93   return !IsEmpty();
94 }
95 
96 void DWARFDebugAranges::Dump(Log *log) const {
97   if (log == NULL)
98     return;
99 
100   const size_t num_entries = m_aranges.GetSize();
101   for (size_t i = 0; i < num_entries; ++i) {
102     const RangeToDIE::Entry *entry = m_aranges.GetEntryAtIndex(i);
103     if (entry)
104       log->Printf("0x%8.8x: [0x%" PRIx64 " - 0x%" PRIx64 ")", entry->data,
105                   entry->GetRangeBase(), entry->GetRangeEnd());
106   }
107 }
108 
109 void DWARFDebugAranges::AppendRange(dw_offset_t offset, dw_addr_t low_pc,
110                                     dw_addr_t high_pc) {
111   if (high_pc > low_pc)
112     m_aranges.Append(RangeToDIE::Entry(low_pc, high_pc - low_pc, offset));
113 }
114 
115 void DWARFDebugAranges::Sort(bool minimize) {
116   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
117   Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
118                      static_cast<void *>(this));
119 
120   m_aranges.Sort();
121   m_aranges.CombineConsecutiveEntriesWithEqualData();
122 }
123 
124 //----------------------------------------------------------------------
125 // FindAddress
126 //----------------------------------------------------------------------
127 dw_offset_t DWARFDebugAranges::FindAddress(dw_addr_t address) const {
128   const RangeToDIE::Entry *entry = m_aranges.FindEntryThatContains(address);
129   if (entry)
130     return entry->data;
131   return DW_INVALID_OFFSET;
132 }
133