DWARFDebugAranges.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include "DWARFDebugAranges.h"
  10. #include "DWARFCompileUnit.h"
  11. #include "DWARFContext.h"
  12. #include "llvm/Support/Format.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. #include <algorithm>
  15. #include <cassert>
  16. using namespace llvm;
  17. // Compare function DWARFDebugAranges::Range structures
  18. static bool RangeLessThan(const DWARFDebugAranges::Range &range1,
  19. const DWARFDebugAranges::Range &range2) {
  20. return range1.LoPC < range2.LoPC;
  21. }
  22. namespace {
  23. class CountArangeDescriptors {
  24. public:
  25. CountArangeDescriptors(uint32_t &count_ref) : Count(count_ref) {}
  26. void operator()(const DWARFDebugArangeSet &set) {
  27. Count += set.getNumDescriptors();
  28. }
  29. uint32_t &Count;
  30. };
  31. class AddArangeDescriptors {
  32. public:
  33. AddArangeDescriptors(DWARFDebugAranges::RangeColl &ranges)
  34. : RangeCollection(ranges) {}
  35. void operator()(const DWARFDebugArangeSet& set) {
  36. const DWARFDebugArangeSet::Descriptor* arange_desc_ptr;
  37. DWARFDebugAranges::Range range;
  38. range.Offset = set.getCompileUnitDIEOffset();
  39. for (uint32_t i=0; (arange_desc_ptr = set.getDescriptor(i)) != NULL; ++i){
  40. range.LoPC = arange_desc_ptr->Address;
  41. range.Length = arange_desc_ptr->Length;
  42. // Insert each item in increasing address order so binary searching
  43. // can later be done!
  44. DWARFDebugAranges::RangeColl::iterator insert_pos =
  45. std::lower_bound(RangeCollection.begin(), RangeCollection.end(),
  46. range, RangeLessThan);
  47. RangeCollection.insert(insert_pos, range);
  48. }
  49. }
  50. DWARFDebugAranges::RangeColl& RangeCollection;
  51. };
  52. }
  53. bool DWARFDebugAranges::extract(DataExtractor debug_aranges_data) {
  54. if (debug_aranges_data.isValidOffset(0)) {
  55. uint32_t offset = 0;
  56. typedef std::vector<DWARFDebugArangeSet> SetCollection;
  57. typedef SetCollection::const_iterator SetCollectionIter;
  58. SetCollection sets;
  59. DWARFDebugArangeSet set;
  60. Range range;
  61. while (set.extract(debug_aranges_data, &offset))
  62. sets.push_back(set);
  63. uint32_t count = 0;
  64. std::for_each(sets.begin(), sets.end(), CountArangeDescriptors(count));
  65. if (count > 0) {
  66. Aranges.reserve(count);
  67. AddArangeDescriptors range_adder(Aranges);
  68. std::for_each(sets.begin(), sets.end(), range_adder);
  69. }
  70. }
  71. return false;
  72. }
  73. bool DWARFDebugAranges::generate(DWARFContext *ctx) {
  74. clear();
  75. if (ctx) {
  76. const uint32_t num_compile_units = ctx->getNumCompileUnits();
  77. for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
  78. DWARFCompileUnit *cu = ctx->getCompileUnitAtIndex(cu_idx);
  79. if (cu)
  80. cu->buildAddressRangeTable(this, true);
  81. }
  82. }
  83. return !isEmpty();
  84. }
  85. void DWARFDebugAranges::dump(raw_ostream &OS) const {
  86. const uint32_t num_ranges = getNumRanges();
  87. for (uint32_t i = 0; i < num_ranges; ++i) {
  88. const Range &range = Aranges[i];
  89. OS << format("0x%8.8x: [0x%8.8" PRIx64 " - 0x%8.8" PRIx64 ")\n",
  90. range.Offset, (uint64_t)range.LoPC, (uint64_t)range.HiPC());
  91. }
  92. }
  93. void DWARFDebugAranges::Range::dump(raw_ostream &OS) const {
  94. OS << format("{0x%8.8x}: [0x%8.8" PRIx64 " - 0x%8.8" PRIx64 ")\n",
  95. Offset, LoPC, HiPC());
  96. }
  97. void DWARFDebugAranges::appendRange(uint32_t offset, uint64_t low_pc,
  98. uint64_t high_pc) {
  99. if (!Aranges.empty()) {
  100. if (Aranges.back().Offset == offset && Aranges.back().HiPC() == low_pc) {
  101. Aranges.back().setHiPC(high_pc);
  102. return;
  103. }
  104. }
  105. Aranges.push_back(Range(low_pc, high_pc, offset));
  106. }
  107. void DWARFDebugAranges::sort(bool minimize, uint32_t n) {
  108. const size_t orig_arange_size = Aranges.size();
  109. // Size of one? If so, no sorting is needed
  110. if (orig_arange_size <= 1)
  111. return;
  112. // Sort our address range entries
  113. std::stable_sort(Aranges.begin(), Aranges.end(), RangeLessThan);
  114. if (!minimize)
  115. return;
  116. // Most address ranges are contiguous from function to function
  117. // so our new ranges will likely be smaller. We calculate the size
  118. // of the new ranges since although std::vector objects can be resized,
  119. // the will never reduce their allocated block size and free any excesss
  120. // memory, so we might as well start a brand new collection so it is as
  121. // small as possible.
  122. // First calculate the size of the new minimal arange vector
  123. // so we don't have to do a bunch of re-allocations as we
  124. // copy the new minimal stuff over to the new collection.
  125. size_t minimal_size = 1;
  126. for (size_t i = 1; i < orig_arange_size; ++i) {
  127. if (!Range::SortedOverlapCheck(Aranges[i-1], Aranges[i], n))
  128. ++minimal_size;
  129. }
  130. // If the sizes are the same, then no consecutive aranges can be
  131. // combined, we are done.
  132. if (minimal_size == orig_arange_size)
  133. return;
  134. // Else, make a new RangeColl that _only_ contains what we need.
  135. RangeColl minimal_aranges;
  136. minimal_aranges.resize(minimal_size);
  137. uint32_t j = 0;
  138. minimal_aranges[j] = Aranges[0];
  139. for (size_t i = 1; i < orig_arange_size; ++i) {
  140. if(Range::SortedOverlapCheck (minimal_aranges[j], Aranges[i], n)) {
  141. minimal_aranges[j].setHiPC (Aranges[i].HiPC());
  142. } else {
  143. // Only increment j if we aren't merging
  144. minimal_aranges[++j] = Aranges[i];
  145. }
  146. }
  147. assert (j+1 == minimal_size);
  148. // Now swap our new minimal aranges into place. The local
  149. // minimal_aranges will then contian the old big collection
  150. // which will get freed.
  151. minimal_aranges.swap(Aranges);
  152. }
  153. uint32_t DWARFDebugAranges::findAddress(uint64_t address) const {
  154. if (!Aranges.empty()) {
  155. Range range(address);
  156. RangeCollIterator begin = Aranges.begin();
  157. RangeCollIterator end = Aranges.end();
  158. RangeCollIterator pos = lower_bound(begin, end, range, RangeLessThan);
  159. if (pos != end && pos->LoPC <= address && address < pos->HiPC()) {
  160. return pos->Offset;
  161. } else if (pos != begin) {
  162. --pos;
  163. if (pos->LoPC <= address && address < pos->HiPC())
  164. return (*pos).Offset;
  165. }
  166. }
  167. return -1U;
  168. }
  169. bool
  170. DWARFDebugAranges::allRangesAreContiguous(uint64_t &LoPC, uint64_t &HiPC) const{
  171. if (Aranges.empty())
  172. return false;
  173. uint64_t next_addr = 0;
  174. RangeCollIterator begin = Aranges.begin();
  175. for (RangeCollIterator pos = begin, end = Aranges.end(); pos != end;
  176. ++pos) {
  177. if (pos != begin && pos->LoPC != next_addr)
  178. return false;
  179. next_addr = pos->HiPC();
  180. }
  181. // We checked for empty at the start of function so front() will be valid.
  182. LoPC = Aranges.front().LoPC;
  183. // We checked for empty at the start of function so back() will be valid.
  184. HiPC = Aranges.back().HiPC();
  185. return true;
  186. }
  187. bool DWARFDebugAranges::getMaxRange(uint64_t &LoPC, uint64_t &HiPC) const {
  188. if (Aranges.empty())
  189. return false;
  190. // We checked for empty at the start of function so front() will be valid.
  191. LoPC = Aranges.front().LoPC;
  192. // We checked for empty at the start of function so back() will be valid.
  193. HiPC = Aranges.back().HiPC();
  194. return true;
  195. }