DWARF: Port support for parsing .debug_aranges section from LLDB and wire it up to...
[oota-llvm.git] / lib / DebugInfo / DWARFDebugAranges.cpp
1 //===-- DWARFDebugAranges.cpp -----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
15 #include <algorithm>
16 #include <cassert>
17 using namespace llvm;
18
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;
23 }
24
25 namespace {
26   class CountArangeDescriptors {
27   public:
28     CountArangeDescriptors(uint32_t &count_ref) : Count(count_ref) {}
29     void operator()(const DWARFDebugArangeSet &set) {
30       Count += set.getNumDescriptors();
31     }
32     uint32_t &Count;
33   };
34
35   class AddArangeDescriptors {
36   public:
37     AddArangeDescriptors(DWARFDebugAranges::RangeColl &ranges)
38       : RangeCollection(ranges) {}
39     void operator()(const DWARFDebugArangeSet& set) {
40       const DWARFDebugArangeSet::Descriptor* arange_desc_ptr;
41       DWARFDebugAranges::Range range;
42       range.Offset = set.getCompileUnitDIEOffset();
43
44       for (uint32_t i=0; (arange_desc_ptr = set.getDescriptor(i)) != NULL; ++i){
45         range.LoPC = arange_desc_ptr->Address;
46         range.Length = arange_desc_ptr->Length;
47
48         // Insert each item in increasing address order so binary searching
49         // can later be done!
50         DWARFDebugAranges::RangeColl::iterator insert_pos =
51           std::lower_bound(RangeCollection.begin(), RangeCollection.end(),
52                            range, RangeLessThan);
53         RangeCollection.insert(insert_pos, range);
54       }
55     }
56     DWARFDebugAranges::RangeColl& RangeCollection;
57   };
58 }
59
60 bool DWARFDebugAranges::extract(DataExtractor debug_aranges_data) {
61   if (debug_aranges_data.isValidOffset(0)) {
62     uint32_t offset = 0;
63
64     typedef std::vector<DWARFDebugArangeSet> SetCollection;
65     typedef SetCollection::const_iterator SetCollectionIter;
66     SetCollection sets;
67
68     DWARFDebugArangeSet set;
69     Range range;
70     while (set.extract(debug_aranges_data, &offset))
71       sets.push_back(set);
72
73     uint32_t count = 0;
74
75     std::for_each(sets.begin(), sets.end(), CountArangeDescriptors(count));
76
77     if (count > 0) {
78       Aranges.reserve(count);
79       AddArangeDescriptors range_adder(Aranges);
80       std::for_each(sets.begin(), sets.end(), range_adder);
81     }
82   }
83   return false;
84 }
85
86 void DWARFDebugAranges::dump(raw_ostream &OS) const {
87   const uint32_t num_ranges = getNumRanges();
88   for (uint32_t i = 0; i < num_ranges; ++i) {
89     const Range &range = Aranges[i];
90     OS << format("0x%8.8x: [0x%8.8llx - 0x%8.8llx)", range.Offset,
91                  (uint64_t)range.LoPC, (uint64_t)range.HiPC());
92   }
93 }
94
95 void DWARFDebugAranges::Range::dump(raw_ostream &OS) const {
96   OS << format("{0x%8.8x}: [0x%8.8llx - 0x%8.8llx)\n", Offset, LoPC, HiPC());
97 }
98
99 void DWARFDebugAranges::appendRange(uint32_t offset, uint64_t low_pc,
100                                     uint64_t high_pc) {
101   if (!Aranges.empty()) {
102     if (Aranges.back().Offset == offset && Aranges.back().HiPC() == low_pc) {
103       Aranges.back().setHiPC(high_pc);
104       return;
105     }
106   }
107   Aranges.push_back(Range(low_pc, high_pc, offset));
108 }
109
110 void DWARFDebugAranges::sort(bool minimize, uint32_t n) {
111   const size_t orig_arange_size = Aranges.size();
112   // Size of one? If so, no sorting is needed
113   if (orig_arange_size <= 1)
114     return;
115   // Sort our address range entries
116   std::stable_sort(Aranges.begin(), Aranges.end(), RangeLessThan);
117
118   if (!minimize)
119     return;
120
121   // Most address ranges are contiguous from function to function
122   // so our new ranges will likely be smaller. We calculate the size
123   // of the new ranges since although std::vector objects can be resized,
124   // the will never reduce their allocated block size and free any excesss
125   // memory, so we might as well start a brand new collection so it is as
126   // small as possible.
127
128   // First calculate the size of the new minimal arange vector
129   // so we don't have to do a bunch of re-allocations as we
130   // copy the new minimal stuff over to the new collection.
131   size_t minimal_size = 1;
132   for (size_t i = 1; i < orig_arange_size; ++i) {
133     if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i], n))
134       ++minimal_size;
135   }
136
137   // If the sizes are the same, then no consecutive aranges can be
138   // combined, we are done.
139   if (minimal_size == orig_arange_size)
140     return;
141
142   // Else, make a new RangeColl that _only_ contains what we need.
143   RangeColl minimal_aranges;
144   minimal_aranges.resize(minimal_size);
145   uint32_t j = 0;
146   minimal_aranges[j] = Aranges[0];
147   for (size_t i = 1; i < orig_arange_size; ++i) {
148     if(Range::SortedOverlapCheck (minimal_aranges[j], Aranges[i], n)) {
149       minimal_aranges[j].setHiPC (Aranges[i].HiPC());
150     } else {
151       // Only increment j if we aren't merging
152       minimal_aranges[++j] = Aranges[i];
153     }
154   }
155   assert (j+1 == minimal_size);
156
157   // Now swap our new minimal aranges into place. The local
158   // minimal_aranges will then contian the old big collection
159   // which will get freed.
160   minimal_aranges.swap(Aranges);
161 }
162
163 uint32_t DWARFDebugAranges::findAddress(uint64_t address) const {
164   if (!Aranges.empty()) {
165     Range range(address);
166     RangeCollIterator begin = Aranges.begin();
167     RangeCollIterator end = Aranges.end();
168     RangeCollIterator pos = lower_bound(begin, end, range, RangeLessThan);
169
170     if (pos != end && pos->LoPC <= address && address < pos->HiPC()) {
171       return pos->Offset;
172     } else if (pos != begin) {
173       --pos;
174       if (pos->LoPC <= address && address < pos->HiPC())
175         return (*pos).Offset;
176     }
177   }
178   return -1U;
179 }
180
181 bool
182 DWARFDebugAranges::allRangesAreContiguous(uint64_t &LoPC, uint64_t &HiPC) const{
183   if (Aranges.empty())
184     return false;
185
186   uint64_t next_addr = 0;
187   RangeCollIterator begin = Aranges.begin();
188   for (RangeCollIterator pos = begin, end = Aranges.end(); pos != end;
189        ++pos) {
190     if (pos != begin && pos->LoPC != next_addr)
191       return false;
192     next_addr = pos->HiPC();
193   }
194   // We checked for empty at the start of function so front() will be valid.
195   LoPC = Aranges.front().LoPC;
196   // We checked for empty at the start of function so back() will be valid.
197   HiPC = Aranges.back().HiPC();
198   return true;
199 }
200
201 bool DWARFDebugAranges::getMaxRange(uint64_t &LoPC, uint64_t &HiPC) const {
202   if (Aranges.empty())
203     return false;
204   // We checked for empty at the start of function so front() will be valid.
205   LoPC = Aranges.front().LoPC;
206   // We checked for empty at the start of function so back() will be valid.
207   HiPC = Aranges.back().HiPC();
208   return true;
209 }
210