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 "DWARFDebugArangeSet.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/raw_ostream.h"
20 void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
21 if (!DebugArangesData.isValidOffset(0))
24 DWARFDebugArangeSet Set;
26 while (Set.extract(DebugArangesData, &Offset)) {
27 uint32_t CUOffset = Set.getCompileUnitDIEOffset();
28 for (const auto &Desc : Set.descriptors()) {
29 uint64_t LowPC = Desc.Address;
30 uint64_t HighPC = Desc.getEndAddress();
31 appendRange(CUOffset, LowPC, HighPC);
36 void DWARFDebugAranges::generate(DWARFContext *CTX) {
41 // Extract aranges from .debug_aranges section.
42 DataExtractor ArangesData(CTX->getARangeSection(), CTX->isLittleEndian(), 0);
45 // Generate aranges from DIEs: even if .debug_aranges section is present,
46 // it may describe only a small subset of compilation units, so we need to
47 // manually build aranges for the rest of them.
48 for (const auto &CU : CTX->compile_units()) {
49 uint32_t CUOffset = CU->getOffset();
50 if (ParsedCUOffsets.insert(CUOffset).second) {
51 DWARFAddressRangesVector CURanges;
52 CU->collectAddressRanges(CURanges);
53 for (const auto &R : CURanges) {
54 appendRange(CUOffset, R.first, R.second);
62 void DWARFDebugAranges::clear() {
64 ParsedCUOffsets.clear();
67 void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
69 if (!Aranges.empty()) {
70 if (Aranges.back().CUOffset == CUOffset &&
71 Aranges.back().HighPC() == LowPC) {
72 Aranges.back().setHighPC(HighPC);
76 Aranges.push_back(Range(LowPC, HighPC, CUOffset));
79 void DWARFDebugAranges::sortAndMinimize() {
80 const size_t orig_arange_size = Aranges.size();
81 // Size of one? If so, no sorting is needed
82 if (orig_arange_size <= 1)
84 // Sort our address range entries
85 std::stable_sort(Aranges.begin(), Aranges.end());
87 // Most address ranges are contiguous from function to function
88 // so our new ranges will likely be smaller. We calculate the size
89 // of the new ranges since although std::vector objects can be resized,
90 // the will never reduce their allocated block size and free any excesss
91 // memory, so we might as well start a brand new collection so it is as
94 // First calculate the size of the new minimal arange vector
95 // so we don't have to do a bunch of re-allocations as we
96 // copy the new minimal stuff over to the new collection.
97 size_t minimal_size = 1;
98 for (size_t i = 1; i < orig_arange_size; ++i) {
99 if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i]))
103 // Else, make a new RangeColl that _only_ contains what we need.
104 RangeColl minimal_aranges;
105 minimal_aranges.resize(minimal_size);
107 minimal_aranges[j] = Aranges[0];
108 for (size_t i = 1; i < orig_arange_size; ++i) {
109 if (Range::SortedOverlapCheck(minimal_aranges[j], Aranges[i])) {
110 minimal_aranges[j].setHighPC(Aranges[i].HighPC());
112 // Only increment j if we aren't merging
113 minimal_aranges[++j] = Aranges[i];
116 assert(j+1 == minimal_size);
118 // Now swap our new minimal aranges into place. The local
119 // minimal_aranges will then contian the old big collection
120 // which will get freed.
121 minimal_aranges.swap(Aranges);
124 uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
125 if (!Aranges.empty()) {
126 Range range(Address);
127 RangeCollIterator begin = Aranges.begin();
128 RangeCollIterator end = Aranges.end();
129 RangeCollIterator pos =
130 std::lower_bound(begin, end, range);
132 if (pos != end && pos->containsAddress(Address)) {
133 return pos->CUOffset;
134 } else if (pos != begin) {
136 if (pos->containsAddress(Address))
137 return pos->CUOffset;