DWARFDebugArangeSet.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //===-- DWARFDebugArangeSet.cpp -------------------------------------------===//
  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 "DWARFDebugArangeSet.h"
  10. #include "llvm/Support/Format.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. #include <algorithm>
  13. #include <cassert>
  14. using namespace llvm;
  15. void DWARFDebugArangeSet::clear() {
  16. Offset = -1U;
  17. std::memset(&Header, 0, sizeof(Header));
  18. ArangeDescriptors.clear();
  19. }
  20. void DWARFDebugArangeSet::compact() {
  21. if (ArangeDescriptors.empty())
  22. return;
  23. // Iterate through all arange descriptors and combine any ranges that
  24. // overlap or have matching boundaries. The ArangeDescriptors are assumed
  25. // to be in ascending order.
  26. uint32_t i = 0;
  27. while (i + 1 < ArangeDescriptors.size()) {
  28. if (ArangeDescriptors[i].getEndAddress() >= ArangeDescriptors[i+1].Address){
  29. // The current range ends at or exceeds the start of the next address
  30. // range. Compute the max end address between the two and use that to
  31. // make the new length.
  32. const uint64_t max_end_addr =
  33. std::max(ArangeDescriptors[i].getEndAddress(),
  34. ArangeDescriptors[i+1].getEndAddress());
  35. ArangeDescriptors[i].Length = max_end_addr - ArangeDescriptors[i].Address;
  36. // Now remove the next entry as it was just combined with the previous one
  37. ArangeDescriptors.erase(ArangeDescriptors.begin()+i+1);
  38. } else {
  39. // Discontiguous address range, just proceed to the next one.
  40. ++i;
  41. }
  42. }
  43. }
  44. bool
  45. DWARFDebugArangeSet::extract(DataExtractor data, uint32_t *offset_ptr) {
  46. if (data.isValidOffset(*offset_ptr)) {
  47. ArangeDescriptors.clear();
  48. Offset = *offset_ptr;
  49. // 7.20 Address Range Table
  50. //
  51. // Each set of entries in the table of address ranges contained in
  52. // the .debug_aranges section begins with a header consisting of: a
  53. // 4-byte length containing the length of the set of entries for this
  54. // compilation unit, not including the length field itself; a 2-byte
  55. // version identifier containing the value 2 for DWARF Version 2; a
  56. // 4-byte offset into the.debug_infosection; a 1-byte unsigned integer
  57. // containing the size in bytes of an address (or the offset portion of
  58. // an address for segmented addressing) on the target system; and a
  59. // 1-byte unsigned integer containing the size in bytes of a segment
  60. // descriptor on the target system. This header is followed by a series
  61. // of tuples. Each tuple consists of an address and a length, each in
  62. // the size appropriate for an address on the target architecture.
  63. Header.Length = data.getU32(offset_ptr);
  64. Header.Version = data.getU16(offset_ptr);
  65. Header.CuOffset = data.getU32(offset_ptr);
  66. Header.AddrSize = data.getU8(offset_ptr);
  67. Header.SegSize = data.getU8(offset_ptr);
  68. // Perform basic validation of the header fields.
  69. if (!data.isValidOffsetForDataOfSize(Offset, Header.Length) ||
  70. (Header.AddrSize != 4 && Header.AddrSize != 8)) {
  71. clear();
  72. return false;
  73. }
  74. // The first tuple following the header in each set begins at an offset
  75. // that is a multiple of the size of a single tuple (that is, twice the
  76. // size of an address). The header is padded, if necessary, to the
  77. // appropriate boundary.
  78. const uint32_t header_size = *offset_ptr - Offset;
  79. const uint32_t tuple_size = Header.AddrSize * 2;
  80. uint32_t first_tuple_offset = 0;
  81. while (first_tuple_offset < header_size)
  82. first_tuple_offset += tuple_size;
  83. *offset_ptr = Offset + first_tuple_offset;
  84. Descriptor arangeDescriptor;
  85. assert(sizeof(arangeDescriptor.Address) == sizeof(arangeDescriptor.Length));
  86. assert(sizeof(arangeDescriptor.Address) >= Header.AddrSize);
  87. while (data.isValidOffset(*offset_ptr)) {
  88. arangeDescriptor.Address = data.getUnsigned(offset_ptr, Header.AddrSize);
  89. arangeDescriptor.Length = data.getUnsigned(offset_ptr, Header.AddrSize);
  90. // Each set of tuples is terminated by a 0 for the address and 0
  91. // for the length.
  92. if (arangeDescriptor.Address || arangeDescriptor.Length)
  93. ArangeDescriptors.push_back(arangeDescriptor);
  94. else
  95. break; // We are done if we get a zero address and length
  96. }
  97. return !ArangeDescriptors.empty();
  98. }
  99. return false;
  100. }
  101. void DWARFDebugArangeSet::dump(raw_ostream &OS) const {
  102. OS << format("Address Range Header: length = 0x%8.8x, version = 0x%4.4x, ",
  103. Header.Length, Header.Version)
  104. << format("cu_offset = 0x%8.8x, addr_size = 0x%2.2x, seg_size = 0x%2.2x\n",
  105. Header.CuOffset, Header.AddrSize, Header.SegSize);
  106. const uint32_t hex_width = Header.AddrSize * 2;
  107. for (DescriptorConstIter pos = ArangeDescriptors.begin(),
  108. end = ArangeDescriptors.end(); pos != end; ++pos)
  109. OS << format("[0x%*.*" PRIx64 " -", hex_width, hex_width, pos->Address)
  110. << format(" 0x%*.*" PRIx64 ")\n",
  111. hex_width, hex_width, pos->getEndAddress());
  112. }
  113. namespace {
  114. class DescriptorContainsAddress {
  115. const uint64_t Address;
  116. public:
  117. DescriptorContainsAddress(uint64_t address) : Address(address) {}
  118. bool operator()(const DWARFDebugArangeSet::Descriptor &desc) const {
  119. return Address >= desc.Address && Address < (desc.Address + desc.Length);
  120. }
  121. };
  122. }
  123. uint32_t DWARFDebugArangeSet::findAddress(uint64_t address) const {
  124. DescriptorConstIter end = ArangeDescriptors.end();
  125. DescriptorConstIter pos =
  126. std::find_if(ArangeDescriptors.begin(), end, // Range
  127. DescriptorContainsAddress(address)); // Predicate
  128. if (pos != end)
  129. return Header.CuOffset;
  130. return -1U;
  131. }