DWARFDebugArangeSet.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //===- DWARFDebugArangeSet.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/DWARFDebugArangeSet.h"
  9. #include "llvm/BinaryFormat/Dwarf.h"
  10. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  11. #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
  12. #include "llvm/Support/Errc.h"
  13. #include "llvm/Support/Format.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include <cassert>
  16. #include <cinttypes>
  17. #include <cstdint>
  18. #include <cstring>
  19. using namespace llvm;
  20. void DWARFDebugArangeSet::Descriptor::dump(raw_ostream &OS,
  21. uint32_t AddressSize) const {
  22. OS << '[';
  23. DWARFFormValue::dumpAddress(OS, AddressSize, Address);
  24. OS << ", ";
  25. DWARFFormValue::dumpAddress(OS, AddressSize, getEndAddress());
  26. OS << ')';
  27. }
  28. void DWARFDebugArangeSet::clear() {
  29. Offset = -1ULL;
  30. std::memset(&HeaderData, 0, sizeof(Header));
  31. ArangeDescriptors.clear();
  32. }
  33. Error DWARFDebugArangeSet::extract(DWARFDataExtractor data,
  34. uint64_t *offset_ptr,
  35. function_ref<void(Error)> WarningHandler) {
  36. assert(data.isValidOffset(*offset_ptr));
  37. ArangeDescriptors.clear();
  38. Offset = *offset_ptr;
  39. // 7.21 Address Range Table (extract)
  40. // Each set of entries in the table of address ranges contained in
  41. // the .debug_aranges section begins with a header containing:
  42. // 1. unit_length (initial length)
  43. // A 4-byte (32-bit DWARF) or 12-byte (64-bit DWARF) length containing
  44. // the length of the set of entries for this compilation unit,
  45. // not including the length field itself.
  46. // 2. version (uhalf)
  47. // The value in this field is 2.
  48. // 3. debug_info_offset (section offset)
  49. // A 4-byte (32-bit DWARF) or 8-byte (64-bit DWARF) offset into the
  50. // .debug_info section of the compilation unit header.
  51. // 4. address_size (ubyte)
  52. // 5. segment_selector_size (ubyte)
  53. // This header is followed by a series of tuples. Each tuple consists of
  54. // a segment, an address and a length. The segment selector size is given by
  55. // the segment_selector_size field of the header; the address and length
  56. // size are each given by the address_size field of the header. Each set of
  57. // tuples is terminated by a 0 for the segment, a 0 for the address and 0
  58. // for the length. If the segment_selector_size field in the header is zero,
  59. // the segment selectors are omitted from all tuples, including
  60. // the terminating tuple.
  61. Error Err = Error::success();
  62. std::tie(HeaderData.Length, HeaderData.Format) =
  63. data.getInitialLength(offset_ptr, &Err);
  64. HeaderData.Version = data.getU16(offset_ptr, &Err);
  65. HeaderData.CuOffset = data.getUnsigned(
  66. offset_ptr, dwarf::getDwarfOffsetByteSize(HeaderData.Format), &Err);
  67. HeaderData.AddrSize = data.getU8(offset_ptr, &Err);
  68. HeaderData.SegSize = data.getU8(offset_ptr, &Err);
  69. if (Err) {
  70. return createStringError(errc::invalid_argument,
  71. "parsing address ranges table at offset 0x%" PRIx64
  72. ": %s",
  73. Offset, toString(std::move(Err)).c_str());
  74. }
  75. // Perform basic validation of the header fields.
  76. uint64_t full_length =
  77. dwarf::getUnitLengthFieldByteSize(HeaderData.Format) + HeaderData.Length;
  78. if (!data.isValidOffsetForDataOfSize(Offset, full_length))
  79. return createStringError(errc::invalid_argument,
  80. "the length of address range table at offset "
  81. "0x%" PRIx64 " exceeds section size",
  82. Offset);
  83. if (Error SizeErr = DWARFContext::checkAddressSizeSupported(
  84. HeaderData.AddrSize, errc::invalid_argument,
  85. "address range table at offset 0x%" PRIx64, Offset))
  86. return SizeErr;
  87. if (HeaderData.SegSize != 0)
  88. return createStringError(errc::not_supported,
  89. "non-zero segment selector size in address range "
  90. "table at offset 0x%" PRIx64 " is not supported",
  91. Offset);
  92. // The first tuple following the header in each set begins at an offset that
  93. // is a multiple of the size of a single tuple (that is, twice the size of
  94. // an address because we do not support non-zero segment selector sizes).
  95. // Therefore, the full length should also be a multiple of the tuple size.
  96. const uint32_t tuple_size = HeaderData.AddrSize * 2;
  97. if (full_length % tuple_size != 0)
  98. return createStringError(
  99. errc::invalid_argument,
  100. "address range table at offset 0x%" PRIx64
  101. " has length that is not a multiple of the tuple size",
  102. Offset);
  103. // The header is padded, if necessary, to the appropriate boundary.
  104. const uint32_t header_size = *offset_ptr - Offset;
  105. uint32_t first_tuple_offset = 0;
  106. while (first_tuple_offset < header_size)
  107. first_tuple_offset += tuple_size;
  108. // There should be space for at least one tuple.
  109. if (full_length <= first_tuple_offset)
  110. return createStringError(
  111. errc::invalid_argument,
  112. "address range table at offset 0x%" PRIx64
  113. " has an insufficient length to contain any entries",
  114. Offset);
  115. *offset_ptr = Offset + first_tuple_offset;
  116. Descriptor arangeDescriptor;
  117. static_assert(sizeof(arangeDescriptor.Address) ==
  118. sizeof(arangeDescriptor.Length),
  119. "Different datatypes for addresses and sizes!");
  120. assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize);
  121. uint64_t end_offset = Offset + full_length;
  122. while (*offset_ptr < end_offset) {
  123. uint64_t EntryOffset = *offset_ptr;
  124. arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
  125. arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
  126. // Each set of tuples is terminated by a 0 for the address and 0
  127. // for the length.
  128. if (arangeDescriptor.Length == 0 && arangeDescriptor.Address == 0) {
  129. if (*offset_ptr == end_offset)
  130. return ErrorSuccess();
  131. WarningHandler(createStringError(
  132. errc::invalid_argument,
  133. "address range table at offset 0x%" PRIx64
  134. " has a premature terminator entry at offset 0x%" PRIx64,
  135. Offset, EntryOffset));
  136. }
  137. ArangeDescriptors.push_back(arangeDescriptor);
  138. }
  139. return createStringError(errc::invalid_argument,
  140. "address range table at offset 0x%" PRIx64
  141. " is not terminated by null entry",
  142. Offset);
  143. }
  144. void DWARFDebugArangeSet::dump(raw_ostream &OS) const {
  145. int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(HeaderData.Format);
  146. OS << "Address Range Header: "
  147. << format("length = 0x%0*" PRIx64 ", ", OffsetDumpWidth, HeaderData.Length)
  148. << "format = " << dwarf::FormatString(HeaderData.Format) << ", "
  149. << format("version = 0x%4.4x, ", HeaderData.Version)
  150. << format("cu_offset = 0x%0*" PRIx64 ", ", OffsetDumpWidth,
  151. HeaderData.CuOffset)
  152. << format("addr_size = 0x%2.2x, ", HeaderData.AddrSize)
  153. << format("seg_size = 0x%2.2x\n", HeaderData.SegSize);
  154. for (const auto &Desc : ArangeDescriptors) {
  155. Desc.dump(OS, HeaderData.AddrSize);
  156. OS << '\n';
  157. }
  158. }