blob: 08864a06021832be10228650e32b96987c1472bd [file] [log] [blame]
Benjamin Kramera6002fd2011-09-14 01:09:52 +00001//===-- 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>
17using namespace llvm;
18
Alexey Samsonov0c9b7252013-10-01 15:48:10 +000019void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
20 if (!DebugArangesData.isValidOffset(0))
21 return;
Alexey Samsonovad4bf3d2013-10-01 16:52:46 +000022 uint32_t Offset = 0;
23 typedef std::vector<DWARFDebugArangeSet> RangeSetColl;
24 RangeSetColl Sets;
25 DWARFDebugArangeSet Set;
26 uint32_t TotalRanges = 0;
Benjamin Kramera6002fd2011-09-14 01:09:52 +000027
Alexey Samsonovad4bf3d2013-10-01 16:52:46 +000028 while (Set.extract(DebugArangesData, &Offset)) {
29 Sets.push_back(Set);
30 TotalRanges += Set.getNumDescriptors();
Benjamin Kramera6002fd2011-09-14 01:09:52 +000031 }
Alexey Samsonovad4bf3d2013-10-01 16:52:46 +000032 if (TotalRanges == 0)
33 return;
34
35 Aranges.reserve(TotalRanges);
Alexey Samsonov1eabf982014-03-13 07:52:54 +000036 for (const auto &I : Sets) {
37 uint32_t CUOffset = I.getCompileUnitDIEOffset();
Alexey Samsonovad4bf3d2013-10-01 16:52:46 +000038
Alexey Samsonov1eabf982014-03-13 07:52:54 +000039 for (const auto &Desc : I.descriptors()) {
40 uint64_t LowPC = Desc.Address;
41 uint64_t HighPC = Desc.getEndAddress();
Alexey Samsonovad4bf3d2013-10-01 16:52:46 +000042 appendRange(CUOffset, LowPC, HighPC);
43 }
44 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000045}
46
Alexey Samsonov0c9b7252013-10-01 15:48:10 +000047void DWARFDebugAranges::generate(DWARFContext *CTX) {
Alexey Samsonov15a23352013-10-02 07:12:47 +000048 clear();
49 if (!CTX)
50 return;
51
52 // Extract aranges from .debug_aranges section.
53 DataExtractor ArangesData(CTX->getARangeSection(), CTX->isLittleEndian(), 0);
54 extract(ArangesData);
55
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.
Alexey Samsonov1eabf982014-03-13 07:52:54 +000059 for (const auto &CU : CTX->compile_units()) {
60 uint32_t CUOffset = CU->getOffset();
Alexey Samsonov762343d2014-04-18 17:25:46 +000061 if (ParsedCUOffsets.insert(CUOffset).second) {
62 DWARFAddressRangesVector CURanges;
63 CU->collectAddressRanges(CURanges);
64 for (const auto &R : CURanges) {
65 appendRange(CUOffset, R.first, R.second);
66 }
67 }
Benjamin Kramer32664932011-09-14 20:52:27 +000068 }
Alexey Samsonov15a23352013-10-02 07:12:47 +000069
Alexey Samsonov97e8a872013-10-01 16:25:14 +000070 sortAndMinimize();
Benjamin Kramer32664932011-09-14 20:52:27 +000071}
72
Alexey Samsonov0c9b7252013-10-01 15:48:10 +000073void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
74 uint64_t HighPC) {
Benjamin Kramera6002fd2011-09-14 01:09:52 +000075 if (!Aranges.empty()) {
Alexey Samsonov97e8a872013-10-01 16:25:14 +000076 if (Aranges.back().CUOffset == CUOffset &&
77 Aranges.back().HighPC() == LowPC) {
78 Aranges.back().setHighPC(HighPC);
Benjamin Kramera6002fd2011-09-14 01:09:52 +000079 return;
80 }
81 }
Alexey Samsonov0c9b7252013-10-01 15:48:10 +000082 Aranges.push_back(Range(LowPC, HighPC, CUOffset));
Benjamin Kramera6002fd2011-09-14 01:09:52 +000083}
84
Alexey Samsonov97e8a872013-10-01 16:25:14 +000085void DWARFDebugAranges::sortAndMinimize() {
Benjamin Kramera6002fd2011-09-14 01:09:52 +000086 const size_t orig_arange_size = Aranges.size();
87 // Size of one? If so, no sorting is needed
88 if (orig_arange_size <= 1)
89 return;
90 // Sort our address range entries
Alexey Samsonov97e8a872013-10-01 16:25:14 +000091 std::stable_sort(Aranges.begin(), Aranges.end());
Benjamin Kramera6002fd2011-09-14 01:09:52 +000092
93 // Most address ranges are contiguous from function to function
94 // so our new ranges will likely be smaller. We calculate the size
95 // of the new ranges since although std::vector objects can be resized,
96 // the will never reduce their allocated block size and free any excesss
97 // memory, so we might as well start a brand new collection so it is as
98 // small as possible.
99
100 // First calculate the size of the new minimal arange vector
101 // so we don't have to do a bunch of re-allocations as we
102 // copy the new minimal stuff over to the new collection.
103 size_t minimal_size = 1;
104 for (size_t i = 1; i < orig_arange_size; ++i) {
Alexey Samsonov97e8a872013-10-01 16:25:14 +0000105 if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i]))
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000106 ++minimal_size;
107 }
108
109 // If the sizes are the same, then no consecutive aranges can be
110 // combined, we are done.
111 if (minimal_size == orig_arange_size)
112 return;
113
114 // Else, make a new RangeColl that _only_ contains what we need.
115 RangeColl minimal_aranges;
116 minimal_aranges.resize(minimal_size);
117 uint32_t j = 0;
118 minimal_aranges[j] = Aranges[0];
119 for (size_t i = 1; i < orig_arange_size; ++i) {
Alexey Samsonov97e8a872013-10-01 16:25:14 +0000120 if (Range::SortedOverlapCheck(minimal_aranges[j], Aranges[i])) {
121 minimal_aranges[j].setHighPC(Aranges[i].HighPC());
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000122 } else {
123 // Only increment j if we aren't merging
124 minimal_aranges[++j] = Aranges[i];
125 }
126 }
Alexey Samsonov97e8a872013-10-01 16:25:14 +0000127 assert(j+1 == minimal_size);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000128
129 // Now swap our new minimal aranges into place. The local
130 // minimal_aranges will then contian the old big collection
131 // which will get freed.
132 minimal_aranges.swap(Aranges);
133}
134
Alexey Samsonov0c9b7252013-10-01 15:48:10 +0000135uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000136 if (!Aranges.empty()) {
Alexey Samsonov0c9b7252013-10-01 15:48:10 +0000137 Range range(Address);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000138 RangeCollIterator begin = Aranges.begin();
139 RangeCollIterator end = Aranges.end();
Alexey Samsonov97e8a872013-10-01 16:25:14 +0000140 RangeCollIterator pos =
141 std::lower_bound(begin, end, range);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000142
Alexey Samsonov97e8a872013-10-01 16:25:14 +0000143 if (pos != end && pos->containsAddress(Address)) {
144 return pos->CUOffset;
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000145 } else if (pos != begin) {
146 --pos;
Alexey Samsonov97e8a872013-10-01 16:25:14 +0000147 if (pos->containsAddress(Address))
148 return pos->CUOffset;
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000149 }
150 }
151 return -1U;
152}