DWARFDebugInfoEntry.cpp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===- DWARFDebugInfoEntry.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/DWARFDebugInfoEntry.h"
  9. #include "llvm/ADT/Optional.h"
  10. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  11. #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
  12. #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
  13. #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
  14. #include "llvm/Support/DataExtractor.h"
  15. #include <cstddef>
  16. #include <cstdint>
  17. using namespace llvm;
  18. using namespace dwarf;
  19. bool DWARFDebugInfoEntry::extractFast(const DWARFUnit &U, uint64_t *OffsetPtr,
  20. const DWARFDataExtractor &DebugInfoData,
  21. uint64_t UEndOffset, uint32_t ParentIdx) {
  22. Offset = *OffsetPtr;
  23. this->ParentIdx = ParentIdx;
  24. if (Offset >= UEndOffset) {
  25. U.getContext().getWarningHandler()(
  26. createStringError(errc::invalid_argument,
  27. "DWARF unit from offset 0x%8.8" PRIx64 " incl. "
  28. "to offset 0x%8.8" PRIx64 " excl. "
  29. "tries to read DIEs at offset 0x%8.8" PRIx64,
  30. U.getOffset(), U.getNextUnitOffset(), *OffsetPtr));
  31. return false;
  32. }
  33. assert(DebugInfoData.isValidOffset(UEndOffset - 1));
  34. uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
  35. if (0 == AbbrCode) {
  36. // NULL debug tag entry.
  37. AbbrevDecl = nullptr;
  38. return true;
  39. }
  40. const auto *AbbrevSet = U.getAbbreviations();
  41. if (!AbbrevSet) {
  42. U.getContext().getWarningHandler()(
  43. createStringError(errc::invalid_argument,
  44. "DWARF unit at offset 0x%8.8" PRIx64 " "
  45. "contains invalid abbreviation set offset 0x%" PRIx64,
  46. U.getOffset(), U.getAbbreviationsOffset()));
  47. // Restore the original offset.
  48. *OffsetPtr = Offset;
  49. return false;
  50. }
  51. AbbrevDecl = AbbrevSet->getAbbreviationDeclaration(AbbrCode);
  52. if (!AbbrevDecl) {
  53. U.getContext().getWarningHandler()(
  54. createStringError(errc::invalid_argument,
  55. "DWARF unit at offset 0x%8.8" PRIx64 " "
  56. "contains invalid abbreviation %" PRIu64 " at "
  57. "offset 0x%8.8" PRIx64 ", valid abbreviations are %s",
  58. U.getOffset(), AbbrCode, *OffsetPtr,
  59. AbbrevSet->getCodeRange().c_str()));
  60. // Restore the original offset.
  61. *OffsetPtr = Offset;
  62. return false;
  63. }
  64. // See if all attributes in this DIE have fixed byte sizes. If so, we can
  65. // just add this size to the offset to skip to the next DIE.
  66. if (Optional<size_t> FixedSize = AbbrevDecl->getFixedAttributesByteSize(U)) {
  67. *OffsetPtr += *FixedSize;
  68. return true;
  69. }
  70. // Skip all data in the .debug_info for the attributes
  71. for (const auto &AttrSpec : AbbrevDecl->attributes()) {
  72. // Check if this attribute has a fixed byte size.
  73. if (auto FixedSize = AttrSpec.getByteSize(U)) {
  74. // Attribute byte size if fixed, just add the size to the offset.
  75. *OffsetPtr += *FixedSize;
  76. } else if (!DWARFFormValue::skipValue(AttrSpec.Form, DebugInfoData,
  77. OffsetPtr, U.getFormParams())) {
  78. // We failed to skip this attribute's value, restore the original offset
  79. // and return the failure status.
  80. U.getContext().getWarningHandler()(createStringError(
  81. errc::invalid_argument,
  82. "DWARF unit at offset 0x%8.8" PRIx64 " "
  83. "contains invalid FORM_* 0x%" PRIx16 " at offset 0x%8.8" PRIx64,
  84. U.getOffset(), AttrSpec.Form, *OffsetPtr));
  85. *OffsetPtr = Offset;
  86. return false;
  87. }
  88. }
  89. return true;
  90. }