blob: 303864948f73879efab0494f6bc4e817a479c73f [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);
36 for (RangeSetColl::const_iterator I = Sets.begin(), E = Sets.end(); I != E;
37 ++I) {
38 uint32_t CUOffset = I->getCompileUnitDIEOffset();
39
40 for (uint32_t i = 0, n = I->getNumDescriptors(); i < n; ++i) {
41 const DWARFDebugArangeSet::Descriptor *ArangeDescPtr =
42 I->getDescriptor(i);
43 uint64_t LowPC = ArangeDescPtr->Address;
44 uint64_t HighPC = LowPC + ArangeDescPtr->Length;
45 appendRange(CUOffset, LowPC, HighPC);
46 }
47 }
48 sortAndMinimize();
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000049}
50
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000051void DWARFDebugAranges::generate(DWARFContext *CTX) {
52 if (CTX) {
53 const uint32_t num_compile_units = CTX->getNumCompileUnits();
Benjamin Kramer10df8062011-09-14 20:52:27 +000054 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000055 if (DWARFCompileUnit *cu = CTX->getCompileUnitAtIndex(cu_idx)) {
Alexey Samsonov63a450a2012-11-16 08:36:25 +000056 uint32_t CUOffset = cu->getOffset();
57 if (ParsedCUOffsets.insert(CUOffset).second)
Alexey Samsonov63fd2af2013-08-27 09:20:22 +000058 cu->buildAddressRangeTable(this, true, CUOffset);
Alexey Samsonov63a450a2012-11-16 08:36:25 +000059 }
Benjamin Kramer10df8062011-09-14 20:52:27 +000060 }
61 }
Alexey Samsonov17f7d092013-10-01 16:25:14 +000062 sortAndMinimize();
Benjamin Kramer10df8062011-09-14 20:52:27 +000063}
64
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000065void DWARFDebugAranges::dump(raw_ostream &OS) const {
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000066 for (RangeCollIterator I = Aranges.begin(), E = Aranges.end(); I != E; ++I) {
67 I->dump(OS);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000068 }
69}
70
71void DWARFDebugAranges::Range::dump(raw_ostream &OS) const {
Benjamin Kramer41a96492011-11-05 08:57:40 +000072 OS << format("{0x%8.8x}: [0x%8.8" PRIx64 " - 0x%8.8" PRIx64 ")\n",
Alexey Samsonov17f7d092013-10-01 16:25:14 +000073 CUOffset, LowPC, HighPC());
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000074}
75
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000076void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
77 uint64_t HighPC) {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000078 if (!Aranges.empty()) {
Alexey Samsonov17f7d092013-10-01 16:25:14 +000079 if (Aranges.back().CUOffset == CUOffset &&
80 Aranges.back().HighPC() == LowPC) {
81 Aranges.back().setHighPC(HighPC);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000082 return;
83 }
84 }
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000085 Aranges.push_back(Range(LowPC, HighPC, CUOffset));
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000086}
87
Alexey Samsonov17f7d092013-10-01 16:25:14 +000088void DWARFDebugAranges::sortAndMinimize() {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000089 const size_t orig_arange_size = Aranges.size();
90 // Size of one? If so, no sorting is needed
91 if (orig_arange_size <= 1)
92 return;
93 // Sort our address range entries
Alexey Samsonov17f7d092013-10-01 16:25:14 +000094 std::stable_sort(Aranges.begin(), Aranges.end());
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000095
96 // Most address ranges are contiguous from function to function
97 // so our new ranges will likely be smaller. We calculate the size
98 // of the new ranges since although std::vector objects can be resized,
99 // the will never reduce their allocated block size and free any excesss
100 // memory, so we might as well start a brand new collection so it is as
101 // small as possible.
102
103 // First calculate the size of the new minimal arange vector
104 // so we don't have to do a bunch of re-allocations as we
105 // copy the new minimal stuff over to the new collection.
106 size_t minimal_size = 1;
107 for (size_t i = 1; i < orig_arange_size; ++i) {
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000108 if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i]))
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000109 ++minimal_size;
110 }
111
112 // If the sizes are the same, then no consecutive aranges can be
113 // combined, we are done.
114 if (minimal_size == orig_arange_size)
115 return;
116
117 // Else, make a new RangeColl that _only_ contains what we need.
118 RangeColl minimal_aranges;
119 minimal_aranges.resize(minimal_size);
120 uint32_t j = 0;
121 minimal_aranges[j] = Aranges[0];
122 for (size_t i = 1; i < orig_arange_size; ++i) {
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000123 if (Range::SortedOverlapCheck(minimal_aranges[j], Aranges[i])) {
124 minimal_aranges[j].setHighPC(Aranges[i].HighPC());
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000125 } else {
126 // Only increment j if we aren't merging
127 minimal_aranges[++j] = Aranges[i];
128 }
129 }
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000130 assert(j+1 == minimal_size);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000131
132 // Now swap our new minimal aranges into place. The local
133 // minimal_aranges will then contian the old big collection
134 // which will get freed.
135 minimal_aranges.swap(Aranges);
136}
137
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000138uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000139 if (!Aranges.empty()) {
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000140 Range range(Address);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000141 RangeCollIterator begin = Aranges.begin();
142 RangeCollIterator end = Aranges.end();
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000143 RangeCollIterator pos =
144 std::lower_bound(begin, end, range);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000145
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000146 if (pos != end && pos->containsAddress(Address)) {
147 return pos->CUOffset;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000148 } else if (pos != begin) {
149 --pos;
Alexey Samsonov17f7d092013-10-01 16:25:14 +0000150 if (pos->containsAddress(Address))
151 return pos->CUOffset;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000152 }
153 }
154 return -1U;
155}