blob: 2524adc25c15e50b2a3e04d0aa9c420f85cd4c1f [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"
Stephen Hinesdce4a402014-05-29 02:49:00 -070013#include "DWARFDebugArangeSet.h"
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000014#include "llvm/Support/Format.h"
15#include "llvm/Support/raw_ostream.h"
16#include <algorithm>
17#include <cassert>
18using namespace llvm;
19
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000020void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
21 if (!DebugArangesData.isValidOffset(0))
22 return;
Alexey Samsonov041f7c82013-10-01 16:52:46 +000023 uint32_t Offset = 0;
Alexey Samsonov041f7c82013-10-01 16:52:46 +000024 DWARFDebugArangeSet Set;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000025
Alexey Samsonov041f7c82013-10-01 16:52:46 +000026 while (Set.extract(DebugArangesData, &Offset)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -070027 uint32_t CUOffset = Set.getCompileUnitDIEOffset();
28 for (const auto &Desc : Set.descriptors()) {
Stephen Hines36b56882014-04-23 16:57:46 -070029 uint64_t LowPC = Desc.Address;
30 uint64_t HighPC = Desc.getEndAddress();
Alexey Samsonov041f7c82013-10-01 16:52:46 +000031 appendRange(CUOffset, LowPC, HighPC);
32 }
33 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000034}
35
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000036void DWARFDebugAranges::generate(DWARFContext *CTX) {
Alexey Samsonov3db24f52013-10-02 07:12:47 +000037 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.
Stephen Hines36b56882014-04-23 16:57:46 -070048 for (const auto &CU : CTX->compile_units()) {
49 uint32_t CUOffset = CU->getOffset();
Stephen Hinesdce4a402014-05-29 02:49:00 -070050 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 }
Benjamin Kramer10df8062011-09-14 20:52:27 +000057 }
Alexey Samsonov3db24f52013-10-02 07:12:47 +000058
Alexey Samsonov17f7d092013-10-01 16:25:14 +000059 sortAndMinimize();
Benjamin Kramer10df8062011-09-14 20:52:27 +000060}
61
Stephen Hinesdce4a402014-05-29 02:49:00 -070062void DWARFDebugAranges::clear() {
63 Aranges.clear();
64 ParsedCUOffsets.clear();
65}
66
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000067void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
68 uint64_t HighPC) {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000069 if (!Aranges.empty()) {
Alexey Samsonov17f7d092013-10-01 16:25:14 +000070 if (Aranges.back().CUOffset == CUOffset &&
71 Aranges.back().HighPC() == LowPC) {
72 Aranges.back().setHighPC(HighPC);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000073 return;
74 }
75 }
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000076 Aranges.push_back(Range(LowPC, HighPC, CUOffset));
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000077}
78
Alexey Samsonov17f7d092013-10-01 16:25:14 +000079void DWARFDebugAranges::sortAndMinimize() {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000080 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
Alexey Samsonov17f7d092013-10-01 16:25:14 +000085 std::stable_sort(Aranges.begin(), Aranges.end());
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000086
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) {
Alexey Samsonov17f7d092013-10-01 16:25:14 +000099 if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i]))
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000100 ++minimal_size;
101 }
102
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000103 // 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) {
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000109 if (Range::SortedOverlapCheck(minimal_aranges[j], Aranges[i])) {
110 minimal_aranges[j].setHighPC(Aranges[i].HighPC());
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000111 } else {
112 // Only increment j if we aren't merging
113 minimal_aranges[++j] = Aranges[i];
114 }
115 }
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000116 assert(j+1 == minimal_size);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000117
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
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000124uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000125 if (!Aranges.empty()) {
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000126 Range range(Address);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000127 RangeCollIterator begin = Aranges.begin();
128 RangeCollIterator end = Aranges.end();
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000129 RangeCollIterator pos =
130 std::lower_bound(begin, end, range);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000131
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000132 if (pos != end && pos->containsAddress(Address)) {
133 return pos->CUOffset;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000134 } else if (pos != begin) {
135 --pos;
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000136 if (pos->containsAddress(Address))
137 return pos->CUOffset;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000138 }
139 }
140 return -1U;
141}