Move getBaseSymbol somewhere the COFF writer can use.
[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 "DWARFDebugArangeSet.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include <algorithm>
17 #include <cassert>
18 using namespace llvm;
19
20 void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
21   if (!DebugArangesData.isValidOffset(0))
22     return;
23   uint32_t Offset = 0;
24   DWARFDebugArangeSet Set;
25
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);
32     }
33   }
34 }
35
36 void DWARFDebugAranges::generate(DWARFContext *CTX) {
37   clear();
38   if (!CTX)
39     return;
40
41   // Extract aranges from .debug_aranges section.
42   DataExtractor ArangesData(CTX->getARangeSection(), CTX->isLittleEndian(), 0);
43   extract(ArangesData);
44
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);
55       }
56     }
57   }
58
59   sortAndMinimize();
60 }
61
62 void DWARFDebugAranges::clear() {
63   Aranges.clear();
64   ParsedCUOffsets.clear();
65 }
66
67 void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
68                                     uint64_t HighPC) {
69   if (!Aranges.empty()) {
70     if (Aranges.back().CUOffset == CUOffset &&
71         Aranges.back().HighPC() == LowPC) {
72       Aranges.back().setHighPC(HighPC);
73       return;
74     }
75   }
76   Aranges.push_back(Range(LowPC, HighPC, CUOffset));
77 }
78
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)
83     return;
84   // Sort our address range entries
85   std::stable_sort(Aranges.begin(), Aranges.end());
86
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
92   // small as possible.
93
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]))
100       ++minimal_size;
101   }
102
103   // Else, make a new RangeColl that _only_ contains what we need.
104   RangeColl minimal_aranges;
105   minimal_aranges.resize(minimal_size);
106   uint32_t j = 0;
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());
111     } else {
112       // Only increment j if we aren't merging
113       minimal_aranges[++j] = Aranges[i];
114     }
115   }
116   assert(j+1 == minimal_size);
117
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);
122 }
123
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);
131
132     if (pos != end && pos->containsAddress(Address)) {
133       return pos->CUOffset;
134     } else if (pos != begin) {
135       --pos;
136       if (pos->containsAddress(Address))
137         return pos->CUOffset;
138     }
139   }
140   return -1U;
141 }