Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 1 | //===-- 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> |
| 17 | using namespace llvm; |
| 18 | |
| 19 | // Compare function DWARFDebugAranges::Range structures |
| 20 | static bool RangeLessThan(const DWARFDebugAranges::Range &range1, |
| 21 | const DWARFDebugAranges::Range &range2) { |
| 22 | return range1.LoPC < range2.LoPC; |
| 23 | } |
| 24 | |
| 25 | namespace { |
| 26 | class CountArangeDescriptors { |
| 27 | public: |
| 28 | CountArangeDescriptors(uint32_t &count_ref) : Count(count_ref) {} |
Alexey Samsonov | 63a450a | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 29 | void operator()(const DWARFDebugArangeSet &Set) { |
| 30 | Count += Set.getNumDescriptors(); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 31 | } |
| 32 | uint32_t &Count; |
| 33 | }; |
| 34 | |
| 35 | class AddArangeDescriptors { |
| 36 | public: |
Alexey Samsonov | 63a450a | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 37 | 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 Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 45 | |
Alexey Samsonov | 63a450a | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 46 | 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 Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 51 | |
| 52 | // Insert each item in increasing address order so binary searching |
| 53 | // can later be done! |
Alexey Samsonov | 63a450a | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 54 | DWARFDebugAranges::RangeColl::iterator InsertPos = |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 55 | std::lower_bound(RangeCollection.begin(), RangeCollection.end(), |
Alexey Samsonov | 63a450a | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 56 | Range, RangeLessThan); |
| 57 | RangeCollection.insert(InsertPos, Range); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 58 | } |
Alexey Samsonov | 63a450a | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 59 | |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 60 | } |
Alexey Samsonov | 63a450a | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 61 | DWARFDebugAranges::RangeColl &RangeCollection; |
| 62 | DWARFDebugAranges::ParsedCUOffsetColl &CUOffsetCollection; |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 63 | }; |
| 64 | } |
| 65 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 66 | void DWARFDebugAranges::extract(DataExtractor DebugArangesData) { |
| 67 | if (!DebugArangesData.isValidOffset(0)) |
| 68 | return; |
| 69 | uint32_t offset = 0; |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 70 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 71 | typedef std::vector<DWARFDebugArangeSet> SetCollection; |
| 72 | SetCollection sets; |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 73 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 74 | DWARFDebugArangeSet set; |
| 75 | Range range; |
| 76 | while (set.extract(DebugArangesData, &offset)) |
| 77 | sets.push_back(set); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 78 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 79 | uint32_t count = 0; |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 80 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 81 | std::for_each(sets.begin(), sets.end(), CountArangeDescriptors(count)); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 82 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 83 | if (count > 0) { |
| 84 | Aranges.reserve(count); |
| 85 | AddArangeDescriptors range_adder(Aranges, ParsedCUOffsets); |
| 86 | std::for_each(sets.begin(), sets.end(), range_adder); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 87 | } |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 90 | void DWARFDebugAranges::generate(DWARFContext *CTX) { |
| 91 | if (CTX) { |
| 92 | const uint32_t num_compile_units = CTX->getNumCompileUnits(); |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 93 | for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) { |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 94 | if (DWARFCompileUnit *cu = CTX->getCompileUnitAtIndex(cu_idx)) { |
Alexey Samsonov | 63a450a | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 95 | uint32_t CUOffset = cu->getOffset(); |
| 96 | if (ParsedCUOffsets.insert(CUOffset).second) |
Alexey Samsonov | 63fd2af | 2013-08-27 09:20:22 +0000 | [diff] [blame] | 97 | cu->buildAddressRangeTable(this, true, CUOffset); |
Alexey Samsonov | 63a450a | 2012-11-16 08:36:25 +0000 | [diff] [blame] | 98 | } |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 101 | sort(true, /* overlap size */ 0); |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 104 | void DWARFDebugAranges::dump(raw_ostream &OS) const { |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 105 | for (RangeCollIterator I = Aranges.begin(), E = Aranges.end(); I != E; ++I) { |
| 106 | I->dump(OS); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | void DWARFDebugAranges::Range::dump(raw_ostream &OS) const { |
Benjamin Kramer | 41a9649 | 2011-11-05 08:57:40 +0000 | [diff] [blame] | 111 | OS << format("{0x%8.8x}: [0x%8.8" PRIx64 " - 0x%8.8" PRIx64 ")\n", |
| 112 | Offset, LoPC, HiPC()); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 115 | void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC, |
| 116 | uint64_t HighPC) { |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 117 | if (!Aranges.empty()) { |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 118 | if (Aranges.back().Offset == CUOffset && Aranges.back().HiPC() == LowPC) { |
| 119 | Aranges.back().setHiPC(HighPC); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | } |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 123 | Aranges.push_back(Range(LowPC, HighPC, CUOffset)); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 126 | void DWARFDebugAranges::sort(bool Minimize, uint32_t OverlapSize) { |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 127 | 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 Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 134 | if (!Minimize) |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 135 | 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 Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 149 | if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i], OverlapSize)) |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 150 | ++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 Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 164 | if (Range::SortedOverlapCheck(minimal_aranges[j], Aranges[i], |
| 165 | OverlapSize)) { |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 166 | 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 Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 180 | uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const { |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 181 | if (!Aranges.empty()) { |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 182 | Range range(Address); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 183 | RangeCollIterator begin = Aranges.begin(); |
| 184 | RangeCollIterator end = Aranges.end(); |
Matt Arsenault | 26c417b | 2013-03-21 00:57:21 +0000 | [diff] [blame] | 185 | RangeCollIterator pos = std::lower_bound(begin, end, range, RangeLessThan); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 186 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 187 | if (pos != end && pos->LoPC <= Address && Address < pos->HiPC()) { |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 188 | return pos->Offset; |
| 189 | } else if (pos != begin) { |
| 190 | --pos; |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame^] | 191 | if (pos->LoPC <= Address && Address < pos->HiPC()) |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 192 | return (*pos).Offset; |
| 193 | } |
| 194 | } |
| 195 | return -1U; |
| 196 | } |