LVELFReader.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- LVELFReader.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. //
  14. // This file defines the LVELFReader class, which is used to describe a
  15. // debug information (DWARF) reader.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVELFREADER_H
  19. #define LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVELFREADER_H
  20. #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
  21. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  22. #include "llvm/DebugInfo/LogicalView/Readers/LVBinaryReader.h"
  23. #include <unordered_set>
  24. namespace llvm {
  25. namespace logicalview {
  26. class LVElement;
  27. class LVLine;
  28. class LVScopeCompileUnit;
  29. class LVSymbol;
  30. class LVType;
  31. using AttributeSpec = DWARFAbbreviationDeclaration::AttributeSpec;
  32. class LVELFReader final : public LVBinaryReader {
  33. object::ObjectFile &Obj;
  34. // Indicates if ranges data are available; in the case of split DWARF any
  35. // reference to ranges is valid only if the skeleton DIE has been loaded.
  36. bool RangesDataAvailable = false;
  37. LVAddress CUBaseAddress = 0;
  38. LVAddress CUHighAddress = 0;
  39. // Current elements during the processing of a DIE.
  40. LVElement *CurrentElement = nullptr;
  41. LVScope *CurrentScope = nullptr;
  42. LVSymbol *CurrentSymbol = nullptr;
  43. LVType *CurrentType = nullptr;
  44. LVOffset CurrentOffset = 0;
  45. LVOffset CurrentEndOffset = 0;
  46. // In DWARF v4, the files are 1-indexed.
  47. // In DWARF v5, the files are 0-indexed.
  48. // The ELF reader expects the indexes as 1-indexed.
  49. bool IncrementFileIndex = false;
  50. // Address ranges collected for current DIE.
  51. std::vector<LVAddressRange> CurrentRanges;
  52. // Symbols with locations for current compile unit.
  53. LVSymbols SymbolsWithLocations;
  54. // Global Offsets (Offset, Element).
  55. LVOffsetElementMap GlobalOffsets;
  56. // Low PC and High PC values for DIE being processed.
  57. LVAddress CurrentLowPC = 0;
  58. LVAddress CurrentHighPC = 0;
  59. bool FoundLowPC = false;
  60. bool FoundHighPC = false;
  61. // Cross references (Elements).
  62. using LVElementSet = std::unordered_set<LVElement *>;
  63. using LVElementEntry = std::pair<LVElement *, LVElementSet>;
  64. using LVElementReference = std::unordered_map<LVOffset, LVElementEntry>;
  65. LVElementReference ElementTable;
  66. Error loadTargetInfo(const object::ObjectFile &Obj);
  67. void mapRangeAddress(const object::ObjectFile &Obj) override;
  68. LVElement *createElement(dwarf::Tag Tag);
  69. void traverseDieAndChildren(DWARFDie &DIE, LVScope *Parent,
  70. DWARFDie &SkeletonDie);
  71. // Process the attributes for the given DIE.
  72. LVScope *processOneDie(const DWARFDie &InputDIE, LVScope *Parent,
  73. DWARFDie &SkeletonDie);
  74. void processOneAttribute(const DWARFDie &Die, LVOffset *OffsetPtr,
  75. const AttributeSpec &AttrSpec);
  76. void createLineAndFileRecords(const DWARFDebugLine::LineTable *Lines);
  77. void processLocationGaps();
  78. // Add offset to global map.
  79. void addGlobalOffset(LVOffset Offset) {
  80. if (GlobalOffsets.find(Offset) == GlobalOffsets.end())
  81. // Just associate the DIE offset with a null element, as we do not
  82. // know if the referenced element has been created.
  83. GlobalOffsets.emplace(Offset, nullptr);
  84. }
  85. // Remove offset from global map.
  86. void removeGlobalOffset(LVOffset Offset) {
  87. LVOffsetElementMap::iterator Iter = GlobalOffsets.find(Offset);
  88. if (Iter != GlobalOffsets.end())
  89. GlobalOffsets.erase(Iter);
  90. }
  91. // Get the location information for DW_AT_data_member_location.
  92. void processLocationMember(dwarf::Attribute Attr,
  93. const DWARFFormValue &FormValue,
  94. const DWARFDie &Die, uint64_t OffsetOnEntry);
  95. void processLocationList(dwarf::Attribute Attr,
  96. const DWARFFormValue &FormValue, const DWARFDie &Die,
  97. uint64_t OffsetOnEntry,
  98. bool CallSiteLocation = false);
  99. void updateReference(dwarf::Attribute Attr, const DWARFFormValue &FormValue);
  100. // Get an element given the DIE offset.
  101. LVElement *getElementForOffset(LVOffset offset, LVElement *Element);
  102. protected:
  103. Error createScopes() override;
  104. void sortScopes() override;
  105. public:
  106. LVELFReader() = delete;
  107. LVELFReader(StringRef Filename, StringRef FileFormatName,
  108. object::ObjectFile &Obj, ScopedPrinter &W)
  109. : LVBinaryReader(Filename, FileFormatName, W, LVBinaryType::ELF),
  110. Obj(Obj) {}
  111. LVELFReader(const LVELFReader &) = delete;
  112. LVELFReader &operator=(const LVELFReader &) = delete;
  113. ~LVELFReader() = default;
  114. LVAddress getCUBaseAddress() const { return CUBaseAddress; }
  115. void setCUBaseAddress(LVAddress Address) { CUBaseAddress = Address; }
  116. LVAddress getCUHighAddress() const { return CUHighAddress; }
  117. void setCUHighAddress(LVAddress Address) { CUHighAddress = Address; }
  118. const LVSymbols &GetSymbolsWithLocations() const {
  119. return SymbolsWithLocations;
  120. }
  121. std::string getRegisterName(LVSmall Opcode, uint64_t Operands[2]) override;
  122. void print(raw_ostream &OS) const;
  123. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  124. void dump() const { print(dbgs()); }
  125. #endif
  126. };
  127. } // end namespace logicalview
  128. } // end namespace llvm
  129. #endif // LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVELFREADER_H
  130. #ifdef __GNUC__
  131. #pragma GCC diagnostic pop
  132. #endif