DWARFDebugLoc.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFDebugLoc.h ------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_DWARF_DWARFDEBUGLOC_H
  14. #define LLVM_DEBUGINFO_DWARF_DWARFDEBUGLOC_H
  15. #include "llvm/ADT/Optional.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/DebugInfo/DIContext.h"
  18. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  19. #include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h"
  20. #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
  21. #include "llvm/Support/Errc.h"
  22. #include <cstdint>
  23. namespace llvm {
  24. class DWARFUnit;
  25. class MCRegisterInfo;
  26. class raw_ostream;
  27. /// A single location within a location list. Entries are stored in the DWARF5
  28. /// form even if they originally come from a DWARF<=4 location list.
  29. struct DWARFLocationEntry {
  30. /// The entry kind (DW_LLE_***).
  31. uint8_t Kind;
  32. /// The first value of the location entry (if applicable).
  33. uint64_t Value0;
  34. /// The second value of the location entry (if applicable).
  35. uint64_t Value1;
  36. /// The index of the section this entry is relative to (if applicable).
  37. uint64_t SectionIndex;
  38. /// The location expression itself (if applicable).
  39. SmallVector<uint8_t, 4> Loc;
  40. };
  41. /// An abstract base class for various kinds of location tables (.debug_loc,
  42. /// .debug_loclists, and their dwo variants).
  43. class DWARFLocationTable {
  44. public:
  45. DWARFLocationTable(DWARFDataExtractor Data) : Data(std::move(Data)) {}
  46. virtual ~DWARFLocationTable() = default;
  47. /// Call the user-provided callback for each entry (including the end-of-list
  48. /// entry) in the location list starting at \p Offset. The callback can return
  49. /// false to terminate the iteration early. Returns an error if it was unable
  50. /// to parse the entire location list correctly. Upon successful termination
  51. /// \p Offset will be updated point past the end of the list.
  52. virtual Error visitLocationList(
  53. uint64_t *Offset,
  54. function_ref<bool(const DWARFLocationEntry &)> Callback) const = 0;
  55. /// Dump the location list at the given \p Offset. The function returns true
  56. /// iff it has successfully reched the end of the list. This means that one
  57. /// can attempt to parse another list after the current one (\p Offset will be
  58. /// updated to point past the end of the current list).
  59. bool dumpLocationList(uint64_t *Offset, raw_ostream &OS,
  60. Optional<object::SectionedAddress> BaseAddr,
  61. const MCRegisterInfo *MRI, const DWARFObject &Obj,
  62. DWARFUnit *U, DIDumpOptions DumpOpts,
  63. unsigned Indent) const;
  64. Error visitAbsoluteLocationList(
  65. uint64_t Offset, Optional<object::SectionedAddress> BaseAddr,
  66. std::function<Optional<object::SectionedAddress>(uint32_t)> LookupAddr,
  67. function_ref<bool(Expected<DWARFLocationExpression>)> Callback) const;
  68. const DWARFDataExtractor &getData() { return Data; }
  69. protected:
  70. DWARFDataExtractor Data;
  71. virtual void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
  72. unsigned Indent, DIDumpOptions DumpOpts,
  73. const DWARFObject &Obj) const = 0;
  74. };
  75. class DWARFDebugLoc final : public DWARFLocationTable {
  76. public:
  77. /// A list of locations that contain one variable.
  78. struct LocationList {
  79. /// The beginning offset where this location list is stored in the debug_loc
  80. /// section.
  81. uint64_t Offset;
  82. /// All the locations in which the variable is stored.
  83. SmallVector<DWARFLocationEntry, 2> Entries;
  84. };
  85. private:
  86. using LocationLists = SmallVector<LocationList, 4>;
  87. /// A list of all the variables in the debug_loc section, each one describing
  88. /// the locations in which the variable is stored.
  89. LocationLists Locations;
  90. public:
  91. DWARFDebugLoc(DWARFDataExtractor Data)
  92. : DWARFLocationTable(std::move(Data)) {}
  93. /// Print the location lists found within the debug_loc section.
  94. void dump(raw_ostream &OS, const MCRegisterInfo *RegInfo,
  95. const DWARFObject &Obj, DIDumpOptions DumpOpts,
  96. Optional<uint64_t> Offset) const;
  97. Error visitLocationList(
  98. uint64_t *Offset,
  99. function_ref<bool(const DWARFLocationEntry &)> Callback) const override;
  100. protected:
  101. void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
  102. unsigned Indent, DIDumpOptions DumpOpts,
  103. const DWARFObject &Obj) const override;
  104. };
  105. class DWARFDebugLoclists final : public DWARFLocationTable {
  106. public:
  107. DWARFDebugLoclists(DWARFDataExtractor Data, uint16_t Version)
  108. : DWARFLocationTable(std::move(Data)), Version(Version) {}
  109. Error visitLocationList(
  110. uint64_t *Offset,
  111. function_ref<bool(const DWARFLocationEntry &)> Callback) const override;
  112. /// Dump all location lists within the given range.
  113. void dumpRange(uint64_t StartOffset, uint64_t Size, raw_ostream &OS,
  114. const MCRegisterInfo *MRI, const DWARFObject &Obj,
  115. DIDumpOptions DumpOpts);
  116. protected:
  117. void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
  118. unsigned Indent, DIDumpOptions DumpOpts,
  119. const DWARFObject &Obj) const override;
  120. private:
  121. uint16_t Version;
  122. };
  123. class ResolverError : public ErrorInfo<ResolverError> {
  124. public:
  125. static char ID;
  126. ResolverError(uint32_t Index, dwarf::LoclistEntries Kind) : Index(Index), Kind(Kind) {}
  127. void log(raw_ostream &OS) const override;
  128. std::error_code convertToErrorCode() const override {
  129. return llvm::errc::invalid_argument;
  130. }
  131. private:
  132. uint32_t Index;
  133. dwarf::LoclistEntries Kind;
  134. };
  135. } // end namespace llvm
  136. #endif // LLVM_DEBUGINFO_DWARF_DWARFDEBUGLOC_H
  137. #ifdef __GNUC__
  138. #pragma GCC diagnostic pop
  139. #endif