blob: e2dfd30bed9f2ff1049af420956924669523f35e [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
19// Compare function DWARFDebugAranges::Range structures
20static bool RangeLessThan(const DWARFDebugAranges::Range &range1,
21 const DWARFDebugAranges::Range &range2) {
22 return range1.LoPC < range2.LoPC;
23}
24
25namespace {
26 class CountArangeDescriptors {
27 public:
28 CountArangeDescriptors(uint32_t &count_ref) : Count(count_ref) {}
Alexey Samsonov63a450a2012-11-16 08:36:25 +000029 void operator()(const DWARFDebugArangeSet &Set) {
30 Count += Set.getNumDescriptors();
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000031 }
32 uint32_t &Count;
33 };
34
35 class AddArangeDescriptors {
36 public:
Alexey Samsonov63a450a2012-11-16 08:36:25 +000037 AddArangeDescriptors(DWARFDebugAranges::RangeColl &Ranges,
38 DWARFDebugAranges::ParsedCUOffsetColl &CUOffsets)
39 : RangeCollection(Ranges),
40 CUOffsetCollection(CUOffsets) {}
41 void operator()(const DWARFDebugArangeSet &Set) {
42 DWARFDebugAranges::Range Range;
43 Range.Offset = Set.getCompileUnitDIEOffset();
44 CUOffsetCollection.insert(Range.Offset);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000045
Alexey Samsonov63a450a2012-11-16 08:36:25 +000046 for (uint32_t i = 0, n = Set.getNumDescriptors(); i < n; ++i) {
47 const DWARFDebugArangeSet::Descriptor *ArangeDescPtr =
48 Set.getDescriptor(i);
49 Range.LoPC = ArangeDescPtr->Address;
50 Range.Length = ArangeDescPtr->Length;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000051
52 // Insert each item in increasing address order so binary searching
53 // can later be done!
Alexey Samsonov63a450a2012-11-16 08:36:25 +000054 DWARFDebugAranges::RangeColl::iterator InsertPos =
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000055 std::lower_bound(RangeCollection.begin(), RangeCollection.end(),
Alexey Samsonov63a450a2012-11-16 08:36:25 +000056 Range, RangeLessThan);
57 RangeCollection.insert(InsertPos, Range);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000058 }
Alexey Samsonov63a450a2012-11-16 08:36:25 +000059
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000060 }
Alexey Samsonov63a450a2012-11-16 08:36:25 +000061 DWARFDebugAranges::RangeColl &RangeCollection;
62 DWARFDebugAranges::ParsedCUOffsetColl &CUOffsetCollection;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000063 };
64}
65
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000066void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
67 if (!DebugArangesData.isValidOffset(0))
68 return;
69 uint32_t offset = 0;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000070
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000071 typedef std::vector<DWARFDebugArangeSet> SetCollection;
72 SetCollection sets;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000073
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000074 DWARFDebugArangeSet set;
75 Range range;
76 while (set.extract(DebugArangesData, &offset))
77 sets.push_back(set);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000078
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000079 uint32_t count = 0;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000080
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000081 std::for_each(sets.begin(), sets.end(), CountArangeDescriptors(count));
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000082
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000083 if (count > 0) {
84 Aranges.reserve(count);
85 AddArangeDescriptors range_adder(Aranges, ParsedCUOffsets);
86 std::for_each(sets.begin(), sets.end(), range_adder);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000087 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000088}
89
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000090void DWARFDebugAranges::generate(DWARFContext *CTX) {
91 if (CTX) {
92 const uint32_t num_compile_units = CTX->getNumCompileUnits();
Benjamin Kramer10df8062011-09-14 20:52:27 +000093 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +000094 if (DWARFCompileUnit *cu = CTX->getCompileUnitAtIndex(cu_idx)) {
Alexey Samsonov63a450a2012-11-16 08:36:25 +000095 uint32_t CUOffset = cu->getOffset();
96 if (ParsedCUOffsets.insert(CUOffset).second)
Alexey Samsonov63fd2af2013-08-27 09:20:22 +000097 cu->buildAddressRangeTable(this, true, CUOffset);
Alexey Samsonov63a450a2012-11-16 08:36:25 +000098 }
Benjamin Kramer10df8062011-09-14 20:52:27 +000099 }
100 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000101 sort(true, /* overlap size */ 0);
Benjamin Kramer10df8062011-09-14 20:52:27 +0000102}
103
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000104void DWARFDebugAranges::dump(raw_ostream &OS) const {
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000105 for (RangeCollIterator I = Aranges.begin(), E = Aranges.end(); I != E; ++I) {
106 I->dump(OS);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000107 }
108}
109
110void DWARFDebugAranges::Range::dump(raw_ostream &OS) const {
Benjamin Kramer41a96492011-11-05 08:57:40 +0000111 OS << format("{0x%8.8x}: [0x%8.8" PRIx64 " - 0x%8.8" PRIx64 ")\n",
112 Offset, LoPC, HiPC());
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000113}
114
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000115void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
116 uint64_t HighPC) {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000117 if (!Aranges.empty()) {
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000118 if (Aranges.back().Offset == CUOffset && Aranges.back().HiPC() == LowPC) {
119 Aranges.back().setHiPC(HighPC);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000120 return;
121 }
122 }
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000123 Aranges.push_back(Range(LowPC, HighPC, CUOffset));
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000124}
125
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000126void DWARFDebugAranges::sort(bool Minimize, uint32_t OverlapSize) {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000127 const size_t orig_arange_size = Aranges.size();
128 // Size of one? If so, no sorting is needed
129 if (orig_arange_size <= 1)
130 return;
131 // Sort our address range entries
132 std::stable_sort(Aranges.begin(), Aranges.end(), RangeLessThan);
133
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000134 if (!Minimize)
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000135 return;
136
137 // Most address ranges are contiguous from function to function
138 // so our new ranges will likely be smaller. We calculate the size
139 // of the new ranges since although std::vector objects can be resized,
140 // the will never reduce their allocated block size and free any excesss
141 // memory, so we might as well start a brand new collection so it is as
142 // small as possible.
143
144 // First calculate the size of the new minimal arange vector
145 // so we don't have to do a bunch of re-allocations as we
146 // copy the new minimal stuff over to the new collection.
147 size_t minimal_size = 1;
148 for (size_t i = 1; i < orig_arange_size; ++i) {
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000149 if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i], OverlapSize))
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000150 ++minimal_size;
151 }
152
153 // If the sizes are the same, then no consecutive aranges can be
154 // combined, we are done.
155 if (minimal_size == orig_arange_size)
156 return;
157
158 // Else, make a new RangeColl that _only_ contains what we need.
159 RangeColl minimal_aranges;
160 minimal_aranges.resize(minimal_size);
161 uint32_t j = 0;
162 minimal_aranges[j] = Aranges[0];
163 for (size_t i = 1; i < orig_arange_size; ++i) {
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000164 if (Range::SortedOverlapCheck(minimal_aranges[j], Aranges[i],
165 OverlapSize)) {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000166 minimal_aranges[j].setHiPC (Aranges[i].HiPC());
167 } else {
168 // Only increment j if we aren't merging
169 minimal_aranges[++j] = Aranges[i];
170 }
171 }
172 assert (j+1 == minimal_size);
173
174 // Now swap our new minimal aranges into place. The local
175 // minimal_aranges will then contian the old big collection
176 // which will get freed.
177 minimal_aranges.swap(Aranges);
178}
179
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000180uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000181 if (!Aranges.empty()) {
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000182 Range range(Address);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000183 RangeCollIterator begin = Aranges.begin();
184 RangeCollIterator end = Aranges.end();
Matt Arsenault26c417b2013-03-21 00:57:21 +0000185 RangeCollIterator pos = std::lower_bound(begin, end, range, RangeLessThan);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000186
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000187 if (pos != end && pos->LoPC <= Address && Address < pos->HiPC()) {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000188 return pos->Offset;
189 } else if (pos != begin) {
190 --pos;
Alexey Samsonovd1fc0f82013-10-01 15:48:10 +0000191 if (pos->LoPC <= Address && Address < pos->HiPC())
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000192 return (*pos).Offset;
193 }
194 }
195 return -1U;
196}