1 //===-- DWARFDebugAranges.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DWARFDebugAranges.h"
11 #include "DWARFCompileUnit.h"
12 #include "DWARFContext.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/raw_ostream.h"
19 void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
20 if (!DebugArangesData.isValidOffset(0))
23 typedef std::vector<DWARFDebugArangeSet> RangeSetColl;
25 DWARFDebugArangeSet Set;
26 uint32_t TotalRanges = 0;
28 while (Set.extract(DebugArangesData, &Offset)) {
30 TotalRanges += Set.getNumDescriptors();
35 Aranges.reserve(TotalRanges);
36 for (const auto &I : Sets) {
37 uint32_t CUOffset = I.getCompileUnitDIEOffset();
39 for (const auto &Desc : I.descriptors()) {
40 uint64_t LowPC = Desc.Address;
41 uint64_t HighPC = Desc.getEndAddress();
42 appendRange(CUOffset, LowPC, HighPC);
47 void DWARFDebugAranges::generate(DWARFContext *CTX) {
52 // Extract aranges from .debug_aranges section.
53 DataExtractor ArangesData(CTX->getARangeSection(), CTX->isLittleEndian(), 0);
56 // Generate aranges from DIEs: even if .debug_aranges section is present,
57 // it may describe only a small subset of compilation units, so we need to
58 // manually build aranges for the rest of them.
59 for (const auto &CU : CTX->compile_units()) {
60 uint32_t CUOffset = CU->getOffset();
61 if (ParsedCUOffsets.insert(CUOffset).second)
62 CU->buildAddressRangeTable(this, true, CUOffset);
68 void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
70 if (!Aranges.empty()) {
71 if (Aranges.back().CUOffset == CUOffset &&
72 Aranges.back().HighPC() == LowPC) {
73 Aranges.back().setHighPC(HighPC);
77 Aranges.push_back(Range(LowPC, HighPC, CUOffset));
80 void DWARFDebugAranges::sortAndMinimize() {
81 const size_t orig_arange_size = Aranges.size();
82 // Size of one? If so, no sorting is needed
83 if (orig_arange_size <= 1)
85 // Sort our address range entries
86 std::stable_sort(Aranges.begin(), Aranges.end());
88 // Most address ranges are contiguous from function to function
89 // so our new ranges will likely be smaller. We calculate the size
90 // of the new ranges since although std::vector objects can be resized,
91 // the will never reduce their allocated block size and free any excesss
92 // memory, so we might as well start a brand new collection so it is as
95 // First calculate the size of the new minimal arange vector
96 // so we don't have to do a bunch of re-allocations as we
97 // copy the new minimal stuff over to the new collection.
98 size_t minimal_size = 1;
99 for (size_t i = 1; i < orig_arange_size; ++i) {
100 if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i]))
104 // If the sizes are the same, then no consecutive aranges can be
105 // combined, we are done.
106 if (minimal_size == orig_arange_size)
109 // Else, make a new RangeColl that _only_ contains what we need.
110 RangeColl minimal_aranges;
111 minimal_aranges.resize(minimal_size);
113 minimal_aranges[j] = Aranges[0];
114 for (size_t i = 1; i < orig_arange_size; ++i) {
115 if (Range::SortedOverlapCheck(minimal_aranges[j], Aranges[i])) {
116 minimal_aranges[j].setHighPC(Aranges[i].HighPC());
118 // Only increment j if we aren't merging
119 minimal_aranges[++j] = Aranges[i];
122 assert(j+1 == minimal_size);
124 // Now swap our new minimal aranges into place. The local
125 // minimal_aranges will then contian the old big collection
126 // which will get freed.
127 minimal_aranges.swap(Aranges);
130 uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
131 if (!Aranges.empty()) {
132 Range range(Address);
133 RangeCollIterator begin = Aranges.begin();
134 RangeCollIterator end = Aranges.end();
135 RangeCollIterator pos =
136 std::lower_bound(begin, end, range);
138 if (pos != end && pos->containsAddress(Address)) {
139 return pos->CUOffset;
140 } else if (pos != begin) {
142 if (pos->containsAddress(Address))
143 return pos->CUOffset;