blob: dfab7886b572e257e60ab5ce830dfad88cb03814 [file] [log] [blame]
Benjamin Kramer358f4fd2011-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 Samsonovd1fc0f82013-10-01 15:48:10 +000019void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
20 if (!DebugArangesData.isValidOffset(0))
21 return;
Alexey Samsonov041f7c82013-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 Kramer358f4fd2011-09-14 01:09:52 +000027
Alexey Samsonov041f7c82013-10-01 16:52:46 +000028 while (Set.extract(DebugArangesData, &Offset)) {
29 Sets.push_back(Set);
30 TotalRanges += Set.getNumDescriptors();
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000031 }
Alexey Samsonov041f7c82013-10-01 16:52:46 +000032 if (TotalRanges == 0)
33 return;
34
35 Aranges.reserve(TotalRanges);
Stephen Hines36b56882014-04-23 16:57:46 -070036 for (const auto &I : Sets) {
37 uint32_t CUOffset = I.getCompileUnitDIEOffset();
Alexey Samsonov041f7c82013-10-01 16:52:46 +000038
Stephen Hines36b56882014-04-23 16:57:46 -070039 for (const auto &Desc : I.descriptors()) {
40 uint64_t LowPC = Desc.Address;
41 uint64_t HighPC = Desc.getEndAddress();
Alexey Samsonov041f7c82013-10-01 16:52:46 +000042 appendRange(CUOffset, LowPC, HighPC);
43 }
44 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000045}
46
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000047void DWARFDebugAranges::generate(DWARFContext *CTX) {
Alexey Samsonov3db24f52013-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.
Stephen Hines36b56882014-04-23 16:57:46 -070059 for (const auto &CU : CTX->compile_units()) {
60 uint32_t CUOffset = CU->getOffset();
61 if (ParsedCUOffsets.insert(CUOffset).second)
62 CU->buildAddressRangeTable(this, true, CUOffset);
Benjamin Kramer10df8062011-09-14 20:52:27 +000063 }
Alexey Samsonov3db24f52013-10-02 07:12:47 +000064
Alexey Samsonov17f7d092013-10-01 16:25:14 +000065 sortAndMinimize();
Benjamin Kramer10df8062011-09-14 20:52:27 +000066}
67
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000068void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
69 uint64_t HighPC) {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000070 if (!Aranges.empty()) {
Alexey Samsonov17f7d092013-10-01 16:25:14 +000071 if (Aranges.back().CUOffset == CUOffset &&
72 Aranges.back().HighPC() == LowPC) {
73 Aranges.back().setHighPC(HighPC);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000074 return;
75 }
76 }
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000077 Aranges.push_back(Range(LowPC, HighPC, CUOffset));
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000078}
79
Alexey Samsonov17f7d092013-10-01 16:25:14 +000080void DWARFDebugAranges::sortAndMinimize() {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000081 const size_t orig_arange_size = Aranges.size();
82 // Size of one? If so, no sorting is needed
83 if (orig_arange_size <= 1)
84 return;
85 // Sort our address range entries
Alexey Samsonov17f7d092013-10-01 16:25:14 +000086 std::stable_sort(Aranges.begin(), Aranges.end());
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000087
88 // Most address ranges are contiguous from function to function
89 // so our new ranges will likely be smaller. We calculate the size
90 // of the new ranges since although std::vector objects can be resized,
91 // the will never reduce their allocated block size and free any excesss
92 // memory, so we might as well start a brand new collection so it is as
93 // small as possible.
94
95 // First calculate the size of the new minimal arange vector
96 // so we don't have to do a bunch of re-allocations as we
97 // copy the new minimal stuff over to the new collection.
98 size_t minimal_size = 1;
99 for (size_t i = 1; i < orig_arange_size; ++i) {
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000100 if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i]))
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000101 ++minimal_size;
102 }
103
104 // If the sizes are the same, then no consecutive aranges can be
105 // combined, we are done.
106 if (minimal_size == orig_arange_size)
107 return;
108
109 // Else, make a new RangeColl that _only_ contains what we need.
110 RangeColl minimal_aranges;
111 minimal_aranges.resize(minimal_size);
112 uint32_t j = 0;
113 minimal_aranges[j] = Aranges[0];
114 for (size_t i = 1; i < orig_arange_size; ++i) {
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000115 if (Range::SortedOverlapCheck(minimal_aranges[j], Aranges[i])) {
116 minimal_aranges[j].setHighPC(Aranges[i].HighPC());
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000117 } else {
118 // Only increment j if we aren't merging
119 minimal_aranges[++j] = Aranges[i];
120 }
121 }
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000122 assert(j+1 == minimal_size);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000123
124 // Now swap our new minimal aranges into place. The local
125 // minimal_aranges will then contian the old big collection
126 // which will get freed.
127 minimal_aranges.swap(Aranges);
128}
129
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000130uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000131 if (!Aranges.empty()) {
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000132 Range range(Address);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000133 RangeCollIterator begin = Aranges.begin();
134 RangeCollIterator end = Aranges.end();
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000135 RangeCollIterator pos =
136 std::lower_bound(begin, end, range);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000137
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000138 if (pos != end && pos->containsAddress(Address)) {
139 return pos->CUOffset;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000140 } else if (pos != begin) {
141 --pos;
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000142 if (pos->containsAddress(Address))
143 return pos->CUOffset;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000144 }
145 }
146 return -1U;
147}