LVBinaryReader.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- LVBinaryReader.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 LVBinaryReader class, which is used to describe a
  15. // binary reader.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVBINARYREADER_H
  19. #define LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVBINARYREADER_H
  20. #include "llvm/DebugInfo/LogicalView/Core/LVReader.h"
  21. #include "llvm/MC/MCAsmInfo.h"
  22. #include "llvm/MC/MCContext.h"
  23. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  24. #include "llvm/MC/MCInstPrinter.h"
  25. #include "llvm/MC/MCInstrInfo.h"
  26. #include "llvm/MC/MCObjectFileInfo.h"
  27. #include "llvm/MC/MCRegisterInfo.h"
  28. #include "llvm/MC/MCSubtargetInfo.h"
  29. #include "llvm/MC/TargetRegistry.h"
  30. #include "llvm/Object/ObjectFile.h"
  31. namespace llvm {
  32. namespace logicalview {
  33. constexpr bool UpdateHighAddress = false;
  34. // Logical scope, Section address, Section index, IsComdat.
  35. struct LVSymbolTableEntry final {
  36. LVScope *Scope = nullptr;
  37. LVAddress Address = 0;
  38. LVSectionIndex SectionIndex = 0;
  39. bool IsComdat = false;
  40. LVSymbolTableEntry() = default;
  41. LVSymbolTableEntry(LVScope *Scope, LVAddress Address,
  42. LVSectionIndex SectionIndex, bool IsComdat)
  43. : Scope(Scope), Address(Address), SectionIndex(SectionIndex),
  44. IsComdat(IsComdat) {}
  45. };
  46. // Function names extracted from the object symbol table.
  47. class LVSymbolTable final {
  48. using LVSymbolNames = std::map<std::string, LVSymbolTableEntry>;
  49. LVSymbolNames SymbolNames;
  50. public:
  51. LVSymbolTable() = default;
  52. void add(StringRef Name, LVScope *Function, LVSectionIndex SectionIndex = 0);
  53. void add(StringRef Name, LVAddress Address, LVSectionIndex SectionIndex,
  54. bool IsComdat);
  55. LVSectionIndex update(LVScope *Function);
  56. const LVSymbolTableEntry &getEntry(StringRef Name);
  57. LVAddress getAddress(StringRef Name);
  58. LVSectionIndex getIndex(StringRef Name);
  59. bool getIsComdat(StringRef Name);
  60. void print(raw_ostream &OS);
  61. };
  62. class LVBinaryReader : public LVReader {
  63. // Function names extracted from the object symbol table.
  64. LVSymbolTable SymbolTable;
  65. // Instruction lines for a logical scope. These instructions are fetched
  66. // during its merge with the debug lines.
  67. LVDoubleMap<LVSectionIndex, LVScope *, LVLines *> ScopeInstructions;
  68. // Links the scope with its first assembler address line.
  69. LVDoubleMap<LVSectionIndex, LVAddress, LVScope *> AssemblerMappings;
  70. // Mapping from virtual address to section.
  71. // The virtual address refers to the address where the section is loaded.
  72. using LVSectionAddresses = std::map<LVSectionIndex, object::SectionRef>;
  73. LVSectionAddresses SectionAddresses;
  74. void addSectionAddress(const object::SectionRef &Section) {
  75. if (SectionAddresses.find(Section.getAddress()) == SectionAddresses.end())
  76. SectionAddresses.emplace(Section.getAddress(), Section);
  77. }
  78. // Scopes with ranges for current compile unit. It is used to find a line
  79. // giving its exact or closest address. To support comdat functions, all
  80. // addresses for the same section are recorded in the same map.
  81. using LVSectionRanges = std::map<LVSectionIndex, LVRange *>;
  82. LVSectionRanges SectionRanges;
  83. // Image base and virtual address for Executable file.
  84. uint64_t ImageBaseAddress = 0;
  85. uint64_t VirtualAddress = 0;
  86. // Object sections with machine code.
  87. using LVSections = std::map<LVSectionIndex, object::SectionRef>;
  88. LVSections Sections;
  89. protected:
  90. // It contains the LVLineDebug elements representing the logical lines for
  91. // the current compile unit, created by parsing the debug line section.
  92. LVLines CULines;
  93. std::unique_ptr<const MCRegisterInfo> MRI;
  94. std::unique_ptr<const MCAsmInfo> MAI;
  95. std::unique_ptr<const MCSubtargetInfo> STI;
  96. std::unique_ptr<const MCInstrInfo> MII;
  97. std::unique_ptr<const MCDisassembler> MD;
  98. std::unique_ptr<MCContext> MC;
  99. std::unique_ptr<MCInstPrinter> MIP;
  100. // Loads all info for the architecture of the provided object file.
  101. Error loadGenericTargetInfo(StringRef TheTriple, StringRef TheFeatures);
  102. virtual void mapRangeAddress(const object::ObjectFile &Obj) {}
  103. virtual void mapRangeAddress(const object::ObjectFile &Obj,
  104. const object::SectionRef &Section,
  105. bool IsComdat) {}
  106. // Create a mapping from virtual address to section.
  107. void mapVirtualAddress(const object::ObjectFile &Obj);
  108. void mapVirtualAddress(const object::COFFObjectFile &COFFObj);
  109. Expected<std::pair<LVSectionIndex, object::SectionRef>>
  110. getSection(LVScope *Scope, LVAddress Address, LVSectionIndex SectionIndex);
  111. void addSectionRange(LVSectionIndex SectionIndex, LVScope *Scope);
  112. void addSectionRange(LVSectionIndex SectionIndex, LVScope *Scope,
  113. LVAddress LowerAddress, LVAddress UpperAddress);
  114. LVRange *getSectionRanges(LVSectionIndex SectionIndex);
  115. Error createInstructions();
  116. Error createInstructions(LVScope *Function, LVSectionIndex SectionIndex);
  117. Error createInstructions(LVScope *Function, LVSectionIndex SectionIndex,
  118. const LVNameInfo &NameInfo);
  119. void processLines(LVLines *DebugLines, LVSectionIndex SectionIndex);
  120. void processLines(LVLines *DebugLines, LVSectionIndex SectionIndex,
  121. LVScope *Function);
  122. public:
  123. LVBinaryReader() = delete;
  124. LVBinaryReader(StringRef Filename, StringRef FileFormatName, ScopedPrinter &W,
  125. LVBinaryType BinaryType)
  126. : LVReader(Filename, FileFormatName, W, BinaryType) {}
  127. LVBinaryReader(const LVBinaryReader &) = delete;
  128. LVBinaryReader &operator=(const LVBinaryReader &) = delete;
  129. virtual ~LVBinaryReader();
  130. void addToSymbolTable(StringRef Name, LVScope *Function,
  131. LVSectionIndex SectionIndex = 0);
  132. void addToSymbolTable(StringRef Name, LVAddress Address,
  133. LVSectionIndex SectionIndex, bool IsComdat);
  134. LVSectionIndex updateSymbolTable(LVScope *Function);
  135. const LVSymbolTableEntry &getSymbolTableEntry(StringRef Name);
  136. LVAddress getSymbolTableAddress(StringRef Name);
  137. LVSectionIndex getSymbolTableIndex(StringRef Name);
  138. bool getSymbolTableIsComdat(StringRef Name);
  139. LVSectionIndex getSectionIndex(LVScope *Scope) override {
  140. return Scope ? getSymbolTableIndex(Scope->getLinkageName())
  141. : DotTextSectionIndex;
  142. }
  143. void print(raw_ostream &OS) const;
  144. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  145. void dump() const { print(dbgs()); }
  146. #endif
  147. };
  148. } // end namespace logicalview
  149. } // end namespace llvm
  150. #endif // LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVBINARYREADER_H
  151. #ifdef __GNUC__
  152. #pragma GCC diagnostic pop
  153. #endif