DWARFListTable.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //===- DWARFListTable.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/DWARFListTable.h"
  9. #include "llvm/BinaryFormat/Dwarf.h"
  10. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  11. #include "llvm/Support/Errc.h"
  12. #include "llvm/Support/Error.h"
  13. #include "llvm/Support/Format.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. using namespace llvm;
  16. Error DWARFListTableHeader::extract(DWARFDataExtractor Data,
  17. uint64_t *OffsetPtr) {
  18. HeaderOffset = *OffsetPtr;
  19. Error Err = Error::success();
  20. std::tie(HeaderData.Length, Format) = Data.getInitialLength(OffsetPtr, &Err);
  21. if (Err)
  22. return createStringError(
  23. errc::invalid_argument, "parsing %s table at offset 0x%" PRIx64 ": %s",
  24. SectionName.data(), HeaderOffset, toString(std::move(Err)).c_str());
  25. uint8_t OffsetByteSize = Format == dwarf::DWARF64 ? 8 : 4;
  26. uint64_t FullLength =
  27. HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format);
  28. if (FullLength < getHeaderSize(Format))
  29. return createStringError(errc::invalid_argument,
  30. "%s table at offset 0x%" PRIx64
  31. " has too small length (0x%" PRIx64
  32. ") to contain a complete header",
  33. SectionName.data(), HeaderOffset, FullLength);
  34. assert(FullLength == length() && "Inconsistent calculation of length.");
  35. uint64_t End = HeaderOffset + FullLength;
  36. if (!Data.isValidOffsetForDataOfSize(HeaderOffset, FullLength))
  37. return createStringError(errc::invalid_argument,
  38. "section is not large enough to contain a %s table "
  39. "of length 0x%" PRIx64 " at offset 0x%" PRIx64,
  40. SectionName.data(), FullLength, HeaderOffset);
  41. HeaderData.Version = Data.getU16(OffsetPtr);
  42. HeaderData.AddrSize = Data.getU8(OffsetPtr);
  43. HeaderData.SegSize = Data.getU8(OffsetPtr);
  44. HeaderData.OffsetEntryCount = Data.getU32(OffsetPtr);
  45. // Perform basic validation of the remaining header fields.
  46. if (HeaderData.Version != 5)
  47. return createStringError(errc::invalid_argument,
  48. "unrecognised %s table version %" PRIu16
  49. " in table at offset 0x%" PRIx64,
  50. SectionName.data(), HeaderData.Version, HeaderOffset);
  51. if (Error SizeErr = DWARFContext::checkAddressSizeSupported(
  52. HeaderData.AddrSize, errc::not_supported,
  53. "%s table at offset 0x%" PRIx64, SectionName.data(), HeaderOffset))
  54. return SizeErr;
  55. if (HeaderData.SegSize != 0)
  56. return createStringError(errc::not_supported,
  57. "%s table at offset 0x%" PRIx64
  58. " has unsupported segment selector size %" PRIu8,
  59. SectionName.data(), HeaderOffset, HeaderData.SegSize);
  60. if (End < HeaderOffset + getHeaderSize(Format) +
  61. HeaderData.OffsetEntryCount * OffsetByteSize)
  62. return createStringError(errc::invalid_argument,
  63. "%s table at offset 0x%" PRIx64 " has more offset entries (%" PRIu32
  64. ") than there is space for",
  65. SectionName.data(), HeaderOffset, HeaderData.OffsetEntryCount);
  66. Data.setAddressSize(HeaderData.AddrSize);
  67. *OffsetPtr += HeaderData.OffsetEntryCount * OffsetByteSize;
  68. return Error::success();
  69. }
  70. void DWARFListTableHeader::dump(DataExtractor Data, raw_ostream &OS,
  71. DIDumpOptions DumpOpts) const {
  72. if (DumpOpts.Verbose)
  73. OS << format("0x%8.8" PRIx64 ": ", HeaderOffset);
  74. int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
  75. OS << format("%s list header: length = 0x%0*" PRIx64, ListTypeString.data(),
  76. OffsetDumpWidth, HeaderData.Length)
  77. << ", format = " << dwarf::FormatString(Format)
  78. << format(", version = 0x%4.4" PRIx16 ", addr_size = 0x%2.2" PRIx8
  79. ", seg_size = 0x%2.2" PRIx8
  80. ", offset_entry_count = 0x%8.8" PRIx32 "\n",
  81. HeaderData.Version, HeaderData.AddrSize, HeaderData.SegSize,
  82. HeaderData.OffsetEntryCount);
  83. if (HeaderData.OffsetEntryCount > 0) {
  84. OS << "offsets: [";
  85. for (uint32_t I = 0; I < HeaderData.OffsetEntryCount; ++I) {
  86. auto Off = *getOffsetEntry(Data, I);
  87. OS << format("\n0x%0*" PRIx64, OffsetDumpWidth, Off);
  88. if (DumpOpts.Verbose)
  89. OS << format(" => 0x%08" PRIx64,
  90. Off + HeaderOffset + getHeaderSize(Format));
  91. }
  92. OS << "\n]\n";
  93. }
  94. }
  95. uint64_t DWARFListTableHeader::length() const {
  96. if (HeaderData.Length == 0)
  97. return 0;
  98. return HeaderData.Length + dwarf::getUnitLengthFieldByteSize(Format);
  99. }