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" |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 13 | #include "DWARFDebugArangeSet.h" |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Format.h" |
| 15 | #include "llvm/Support/raw_ostream.h" |
| 16 | #include <algorithm> |
| 17 | #include <cassert> |
| 18 | using namespace llvm; |
| 19 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame] | 20 | void DWARFDebugAranges::extract(DataExtractor DebugArangesData) { |
| 21 | if (!DebugArangesData.isValidOffset(0)) |
| 22 | return; |
Alexey Samsonov | 041f7c8 | 2013-10-01 16:52:46 +0000 | [diff] [blame] | 23 | uint32_t Offset = 0; |
Alexey Samsonov | 041f7c8 | 2013-10-01 16:52:46 +0000 | [diff] [blame] | 24 | DWARFDebugArangeSet Set; |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 25 | |
Alexey Samsonov | 041f7c8 | 2013-10-01 16:52:46 +0000 | [diff] [blame] | 26 | while (Set.extract(DebugArangesData, &Offset)) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 27 | uint32_t CUOffset = Set.getCompileUnitDIEOffset(); |
| 28 | for (const auto &Desc : Set.descriptors()) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 29 | uint64_t LowPC = Desc.Address; |
| 30 | uint64_t HighPC = Desc.getEndAddress(); |
Alexey Samsonov | 041f7c8 | 2013-10-01 16:52:46 +0000 | [diff] [blame] | 31 | appendRange(CUOffset, LowPC, HighPC); |
| 32 | } |
| 33 | } |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame] | 36 | void DWARFDebugAranges::generate(DWARFContext *CTX) { |
Alexey Samsonov | 3db24f5 | 2013-10-02 07:12:47 +0000 | [diff] [blame] | 37 | 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 Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 48 | for (const auto &CU : CTX->compile_units()) { |
| 49 | uint32_t CUOffset = CU->getOffset(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 50 | 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 Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 57 | } |
Alexey Samsonov | 3db24f5 | 2013-10-02 07:12:47 +0000 | [diff] [blame] | 58 | |
Alexey Samsonov | 17f7d09 | 2013-10-01 16:25:14 +0000 | [diff] [blame] | 59 | sortAndMinimize(); |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 62 | void DWARFDebugAranges::clear() { |
| 63 | Aranges.clear(); |
| 64 | ParsedCUOffsets.clear(); |
| 65 | } |
| 66 | |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame] | 67 | void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC, |
| 68 | uint64_t HighPC) { |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 69 | if (!Aranges.empty()) { |
Alexey Samsonov | 17f7d09 | 2013-10-01 16:25:14 +0000 | [diff] [blame] | 70 | if (Aranges.back().CUOffset == CUOffset && |
| 71 | Aranges.back().HighPC() == LowPC) { |
| 72 | Aranges.back().setHighPC(HighPC); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | } |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame] | 76 | Aranges.push_back(Range(LowPC, HighPC, CUOffset)); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Alexey Samsonov | 17f7d09 | 2013-10-01 16:25:14 +0000 | [diff] [blame] | 79 | void DWARFDebugAranges::sortAndMinimize() { |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 80 | 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 Samsonov | 17f7d09 | 2013-10-01 16:25:14 +0000 | [diff] [blame] | 85 | std::stable_sort(Aranges.begin(), Aranges.end()); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 86 | |
| 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 Samsonov | 17f7d09 | 2013-10-01 16:25:14 +0000 | [diff] [blame] | 99 | if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i])) |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 100 | ++minimal_size; |
| 101 | } |
| 102 | |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 103 | // 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 Samsonov | 17f7d09 | 2013-10-01 16:25:14 +0000 | [diff] [blame] | 109 | if (Range::SortedOverlapCheck(minimal_aranges[j], Aranges[i])) { |
| 110 | minimal_aranges[j].setHighPC(Aranges[i].HighPC()); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 111 | } else { |
| 112 | // Only increment j if we aren't merging |
| 113 | minimal_aranges[++j] = Aranges[i]; |
| 114 | } |
| 115 | } |
Alexey Samsonov | 17f7d09 | 2013-10-01 16:25:14 +0000 | [diff] [blame] | 116 | assert(j+1 == minimal_size); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 117 | |
| 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 Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame] | 124 | uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const { |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 125 | if (!Aranges.empty()) { |
Alexey Samsonov | d1fc0f8 | 2013-10-01 15:48:10 +0000 | [diff] [blame] | 126 | Range range(Address); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 127 | RangeCollIterator begin = Aranges.begin(); |
| 128 | RangeCollIterator end = Aranges.end(); |
Alexey Samsonov | 17f7d09 | 2013-10-01 16:25:14 +0000 | [diff] [blame] | 129 | RangeCollIterator pos = |
| 130 | std::lower_bound(begin, end, range); |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 131 | |
Alexey Samsonov | 17f7d09 | 2013-10-01 16:25:14 +0000 | [diff] [blame] | 132 | if (pos != end && pos->containsAddress(Address)) { |
| 133 | return pos->CUOffset; |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 134 | } else if (pos != begin) { |
| 135 | --pos; |
Alexey Samsonov | 17f7d09 | 2013-10-01 16:25:14 +0000 | [diff] [blame] | 136 | if (pos->containsAddress(Address)) |
| 137 | return pos->CUOffset; |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | return -1U; |
| 141 | } |