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 // Compare function DWARFDebugAranges::Range structures
20 static bool RangeLessThan(const DWARFDebugAranges::Range &range1,
21 const DWARFDebugAranges::Range &range2) {
22 return range1.LoPC < range2.LoPC;
26 class CountArangeDescriptors {
28 CountArangeDescriptors(uint32_t &count_ref) : Count(count_ref) {}
29 void operator()(const DWARFDebugArangeSet &Set) {
30 Count += Set.getNumDescriptors();
35 class AddArangeDescriptors {
37 AddArangeDescriptors(DWARFDebugAranges::RangeColl &Ranges,
38 DWARFDebugAranges::ParsedCUOffsetColl &CUOffsets)
39 : RangeCollection(Ranges),
40 CUOffsetCollection(CUOffsets) {}
41 void operator()(const DWARFDebugArangeSet &Set) {
42 DWARFDebugAranges::Range Range;
43 Range.Offset = Set.getCompileUnitDIEOffset();
44 CUOffsetCollection.insert(Range.Offset);
46 for (uint32_t i = 0, n = Set.getNumDescriptors(); i < n; ++i) {
47 const DWARFDebugArangeSet::Descriptor *ArangeDescPtr =
49 Range.LoPC = ArangeDescPtr->Address;
50 Range.Length = ArangeDescPtr->Length;
52 // Insert each item in increasing address order so binary searching
54 DWARFDebugAranges::RangeColl::iterator InsertPos =
55 std::lower_bound(RangeCollection.begin(), RangeCollection.end(),
56 Range, RangeLessThan);
57 RangeCollection.insert(InsertPos, Range);
61 DWARFDebugAranges::RangeColl &RangeCollection;
62 DWARFDebugAranges::ParsedCUOffsetColl &CUOffsetCollection;
66 void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
67 if (!DebugArangesData.isValidOffset(0))
71 typedef std::vector<DWARFDebugArangeSet> SetCollection;
74 DWARFDebugArangeSet set;
76 while (set.extract(DebugArangesData, &offset))
81 std::for_each(sets.begin(), sets.end(), CountArangeDescriptors(count));
84 Aranges.reserve(count);
85 AddArangeDescriptors range_adder(Aranges, ParsedCUOffsets);
86 std::for_each(sets.begin(), sets.end(), range_adder);
90 void DWARFDebugAranges::generate(DWARFContext *CTX) {
92 const uint32_t num_compile_units = CTX->getNumCompileUnits();
93 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
94 if (DWARFCompileUnit *cu = CTX->getCompileUnitAtIndex(cu_idx)) {
95 uint32_t CUOffset = cu->getOffset();
96 if (ParsedCUOffsets.insert(CUOffset).second)
97 cu->buildAddressRangeTable(this, true, CUOffset);
101 sort(true, /* overlap size */ 0);
104 void DWARFDebugAranges::dump(raw_ostream &OS) const {
105 for (RangeCollIterator I = Aranges.begin(), E = Aranges.end(); I != E; ++I) {
110 void DWARFDebugAranges::Range::dump(raw_ostream &OS) const {
111 OS << format("{0x%8.8x}: [0x%8.8" PRIx64 " - 0x%8.8" PRIx64 ")\n",
112 Offset, LoPC, HiPC());
115 void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
117 if (!Aranges.empty()) {
118 if (Aranges.back().Offset == CUOffset && Aranges.back().HiPC() == LowPC) {
119 Aranges.back().setHiPC(HighPC);
123 Aranges.push_back(Range(LowPC, HighPC, CUOffset));
126 void DWARFDebugAranges::sort(bool Minimize, uint32_t OverlapSize) {
127 const size_t orig_arange_size = Aranges.size();
128 // Size of one? If so, no sorting is needed
129 if (orig_arange_size <= 1)
131 // Sort our address range entries
132 std::stable_sort(Aranges.begin(), Aranges.end(), RangeLessThan);
137 // Most address ranges are contiguous from function to function
138 // so our new ranges will likely be smaller. We calculate the size
139 // of the new ranges since although std::vector objects can be resized,
140 // the will never reduce their allocated block size and free any excesss
141 // memory, so we might as well start a brand new collection so it is as
142 // small as possible.
144 // First calculate the size of the new minimal arange vector
145 // so we don't have to do a bunch of re-allocations as we
146 // copy the new minimal stuff over to the new collection.
147 size_t minimal_size = 1;
148 for (size_t i = 1; i < orig_arange_size; ++i) {
149 if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i], OverlapSize))
153 // If the sizes are the same, then no consecutive aranges can be
154 // combined, we are done.
155 if (minimal_size == orig_arange_size)
158 // Else, make a new RangeColl that _only_ contains what we need.
159 RangeColl minimal_aranges;
160 minimal_aranges.resize(minimal_size);
162 minimal_aranges[j] = Aranges[0];
163 for (size_t i = 1; i < orig_arange_size; ++i) {
164 if (Range::SortedOverlapCheck(minimal_aranges[j], Aranges[i],
166 minimal_aranges[j].setHiPC (Aranges[i].HiPC());
168 // Only increment j if we aren't merging
169 minimal_aranges[++j] = Aranges[i];
172 assert (j+1 == minimal_size);
174 // Now swap our new minimal aranges into place. The local
175 // minimal_aranges will then contian the old big collection
176 // which will get freed.
177 minimal_aranges.swap(Aranges);
180 uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
181 if (!Aranges.empty()) {
182 Range range(Address);
183 RangeCollIterator begin = Aranges.begin();
184 RangeCollIterator end = Aranges.end();
185 RangeCollIterator pos = std::lower_bound(begin, end, range, RangeLessThan);
187 if (pos != end && pos->LoPC <= Address && Address < pos->HiPC()) {
189 } else if (pos != begin) {
191 if (pos->LoPC <= Address && Address < pos->HiPC())
192 return (*pos).Offset;