DWARFDebugRangeList.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //===- DWARFDebugRangesList.cpp -------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
  9. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  10. #include "llvm/Support/Errc.h"
  11. #include "llvm/Support/Format.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. #include <cinttypes>
  14. #include <cstdint>
  15. using namespace llvm;
  16. bool DWARFDebugRangeList::RangeListEntry::isBaseAddressSelectionEntry(
  17. uint8_t AddressSize) const {
  18. assert(DWARFContext::isAddressSizeSupported(AddressSize));
  19. return StartAddress == dwarf::computeTombstoneAddress(AddressSize);
  20. }
  21. void DWARFDebugRangeList::clear() {
  22. Offset = -1ULL;
  23. AddressSize = 0;
  24. Entries.clear();
  25. }
  26. Error DWARFDebugRangeList::extract(const DWARFDataExtractor &data,
  27. uint64_t *offset_ptr) {
  28. clear();
  29. if (!data.isValidOffset(*offset_ptr))
  30. return createStringError(errc::invalid_argument,
  31. "invalid range list offset 0x%" PRIx64, *offset_ptr);
  32. AddressSize = data.getAddressSize();
  33. if (Error SizeErr = DWARFContext::checkAddressSizeSupported(
  34. AddressSize, errc::invalid_argument,
  35. "range list at offset 0x%" PRIx64, *offset_ptr))
  36. return SizeErr;
  37. Offset = *offset_ptr;
  38. while (true) {
  39. RangeListEntry Entry;
  40. Entry.SectionIndex = -1ULL;
  41. uint64_t prev_offset = *offset_ptr;
  42. Entry.StartAddress = data.getRelocatedAddress(offset_ptr);
  43. Entry.EndAddress =
  44. data.getRelocatedAddress(offset_ptr, &Entry.SectionIndex);
  45. // Check that both values were extracted correctly.
  46. if (*offset_ptr != prev_offset + 2 * AddressSize) {
  47. clear();
  48. return createStringError(errc::invalid_argument,
  49. "invalid range list entry at offset 0x%" PRIx64,
  50. prev_offset);
  51. }
  52. if (Entry.isEndOfListEntry())
  53. break;
  54. Entries.push_back(Entry);
  55. }
  56. return Error::success();
  57. }
  58. void DWARFDebugRangeList::dump(raw_ostream &OS) const {
  59. const char *AddrFmt;
  60. switch (AddressSize) {
  61. case 2:
  62. AddrFmt = "%08" PRIx64 " %04" PRIx64 " %04" PRIx64 "\n";
  63. break;
  64. case 4:
  65. AddrFmt = "%08" PRIx64 " %08" PRIx64 " %08" PRIx64 "\n";
  66. break;
  67. case 8:
  68. AddrFmt = "%08" PRIx64 " %016" PRIx64 " %016" PRIx64 "\n";
  69. break;
  70. default:
  71. llvm_unreachable("unsupported address size");
  72. }
  73. for (const RangeListEntry &RLE : Entries)
  74. OS << format(AddrFmt, Offset, RLE.StartAddress, RLE.EndAddress);
  75. OS << format("%08" PRIx64 " <End of list>\n", Offset);
  76. }
  77. DWARFAddressRangesVector DWARFDebugRangeList::getAbsoluteRanges(
  78. llvm::Optional<object::SectionedAddress> BaseAddr) const {
  79. DWARFAddressRangesVector Res;
  80. // debug_addr can't use the max integer tombstone because that's used for the
  81. // base address specifier entry - so use max-1.
  82. uint64_t Tombstone = dwarf::computeTombstoneAddress(AddressSize) - 1;
  83. for (const RangeListEntry &RLE : Entries) {
  84. if (RLE.isBaseAddressSelectionEntry(AddressSize)) {
  85. BaseAddr = {RLE.EndAddress, RLE.SectionIndex};
  86. continue;
  87. }
  88. DWARFAddressRange E;
  89. E.LowPC = RLE.StartAddress;
  90. if (E.LowPC == Tombstone)
  91. continue;
  92. E.HighPC = RLE.EndAddress;
  93. E.SectionIndex = RLE.SectionIndex;
  94. // Base address of a range list entry is determined by the closest preceding
  95. // base address selection entry in the same range list. It defaults to the
  96. // base address of the compilation unit if there is no such entry.
  97. if (BaseAddr) {
  98. if (BaseAddr->Address == Tombstone)
  99. continue;
  100. E.LowPC += BaseAddr->Address;
  101. E.HighPC += BaseAddr->Address;
  102. if (E.SectionIndex == -1ULL)
  103. E.SectionIndex = BaseAddr->SectionIndex;
  104. }
  105. Res.push_back(E);
  106. }
  107. return Res;
  108. }