DWARFDebugLoc.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <cstdint>
  22. namespace llvm {
  23. class DWARFUnit;
  24. class MCRegisterInfo;
  25. class raw_ostream;
  26. /// A single location within a location list. Entries are stored in the DWARF5
  27. /// form even if they originally come from a DWARF<=4 location list.
  28. struct DWARFLocationEntry {
  29. /// The entry kind (DW_LLE_***).
  30. uint8_t Kind;
  31. /// The first value of the location entry (if applicable).
  32. uint64_t Value0;
  33. /// The second value of the location entry (if applicable).
  34. uint64_t Value1;
  35. /// The index of the section this entry is relative to (if applicable).
  36. uint64_t SectionIndex;
  37. /// The location expression itself (if applicable).
  38. SmallVector<uint8_t, 4> Loc;
  39. };
  40. /// An abstract base class for various kinds of location tables (.debug_loc,
  41. /// .debug_loclists, and their dwo variants).
  42. class DWARFLocationTable {
  43. public:
  44. DWARFLocationTable(DWARFDataExtractor Data) : Data(std::move(Data)) {}
  45. virtual ~DWARFLocationTable() = default;
  46. /// Call the user-provided callback for each entry (including the end-of-list
  47. /// entry) in the location list starting at \p Offset. The callback can return
  48. /// false to terminate the iteration early. Returns an error if it was unable
  49. /// to parse the entire location list correctly. Upon successful termination
  50. /// \p Offset will be updated point past the end of the list.
  51. virtual Error visitLocationList(
  52. uint64_t *Offset,
  53. function_ref<bool(const DWARFLocationEntry &)> Callback) const = 0;
  54. /// Dump the location list at the given \p Offset. The function returns true
  55. /// iff it has successfully reched the end of the list. This means that one
  56. /// can attempt to parse another list after the current one (\p Offset will be
  57. /// updated to point past the end of the current list).
  58. bool dumpLocationList(uint64_t *Offset, raw_ostream &OS,
  59. Optional<object::SectionedAddress> BaseAddr,
  60. const MCRegisterInfo *MRI, const DWARFObject &Obj,
  61. DWARFUnit *U, DIDumpOptions DumpOpts,
  62. unsigned Indent) const;
  63. Error visitAbsoluteLocationList(
  64. uint64_t Offset, Optional<object::SectionedAddress> BaseAddr,
  65. std::function<Optional<object::SectionedAddress>(uint32_t)> LookupAddr,
  66. function_ref<bool(Expected<DWARFLocationExpression>)> Callback) const;
  67. const DWARFDataExtractor &getData() { return Data; }
  68. protected:
  69. DWARFDataExtractor Data;
  70. virtual void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
  71. unsigned Indent, DIDumpOptions DumpOpts,
  72. const DWARFObject &Obj) const = 0;
  73. };
  74. class DWARFDebugLoc final : public DWARFLocationTable {
  75. public:
  76. /// A list of locations that contain one variable.
  77. struct LocationList {
  78. /// The beginning offset where this location list is stored in the debug_loc
  79. /// section.
  80. uint64_t Offset;
  81. /// All the locations in which the variable is stored.
  82. SmallVector<DWARFLocationEntry, 2> Entries;
  83. };
  84. private:
  85. using LocationLists = SmallVector<LocationList, 4>;
  86. /// A list of all the variables in the debug_loc section, each one describing
  87. /// the locations in which the variable is stored.
  88. LocationLists Locations;
  89. public:
  90. DWARFDebugLoc(DWARFDataExtractor Data)
  91. : DWARFLocationTable(std::move(Data)) {}
  92. /// Print the location lists found within the debug_loc section.
  93. void dump(raw_ostream &OS, const MCRegisterInfo *RegInfo,
  94. const DWARFObject &Obj, DIDumpOptions DumpOpts,
  95. Optional<uint64_t> Offset) const;
  96. Error visitLocationList(
  97. uint64_t *Offset,
  98. function_ref<bool(const DWARFLocationEntry &)> Callback) const override;
  99. protected:
  100. void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
  101. unsigned Indent, DIDumpOptions DumpOpts,
  102. const DWARFObject &Obj) const override;
  103. };
  104. class DWARFDebugLoclists final : public DWARFLocationTable {
  105. public:
  106. DWARFDebugLoclists(DWARFDataExtractor Data, uint16_t Version)
  107. : DWARFLocationTable(std::move(Data)), Version(Version) {}
  108. Error visitLocationList(
  109. uint64_t *Offset,
  110. function_ref<bool(const DWARFLocationEntry &)> Callback) const override;
  111. /// Dump all location lists within the given range.
  112. void dumpRange(uint64_t StartOffset, uint64_t Size, raw_ostream &OS,
  113. const MCRegisterInfo *MRI, const DWARFObject &Obj,
  114. DIDumpOptions DumpOpts);
  115. protected:
  116. void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
  117. unsigned Indent, DIDumpOptions DumpOpts,
  118. const DWARFObject &Obj) const override;
  119. private:
  120. uint16_t Version;
  121. };
  122. } // end namespace llvm
  123. #endif // LLVM_DEBUGINFO_DWARF_DWARFDEBUGLOC_H
  124. #ifdef __GNUC__
  125. #pragma GCC diagnostic pop
  126. #endif