LLVM 23.0.0git
LVBinaryReader.h
Go to the documentation of this file.
1//===-- LVBinaryReader.h ----------------------------------------*- C++ -*-===//
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//
9// This file defines the LVBinaryReader class, which is used to describe a
10// binary reader.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVBINARYREADER_H
15#define LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVBINARYREADER_H
16
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCInstrInfo.h"
28#include "llvm/Object/COFF.h"
31
32namespace llvm {
33namespace logicalview {
34
35constexpr bool UpdateHighAddress = false;
36
37// Logical scope, Section address, Section index, IsComdat.
49
50// Function names extracted from the object symbol table.
51class LVSymbolTable final {
52 using LVSymbolNames = std::map<std::string, LVSymbolTableEntry, std::less<>>;
53 LVSymbolNames SymbolNames;
54
55public:
56 LVSymbolTable() = default;
57
59 LVSectionIndex SectionIndex = 0);
61 LVSectionIndex SectionIndex, bool IsComdat);
63
68
69 LLVM_ABI void print(raw_ostream &OS);
70};
71
72class LVBinaryReader : public LVReader {
73 // Function names extracted from the object symbol table.
74 LVSymbolTable SymbolTable;
75
76 // It contains the LVLineDebug elements representing the inlined logical
77 // lines for the current compile unit, created by parsing the CodeView
78 // S_INLINESITE symbol annotation data.
79 using LVInlineeLine = std::map<LVScope *, std::unique_ptr<LVLines>>;
80 LVInlineeLine CUInlineeLines;
81
82 // Instruction lines for a logical scope. These instructions are fetched
83 // during its merge with the debug lines.
85
86 // Links the scope with its first assembler address line.
88
89 // Mapping from virtual address to section.
90 // The virtual address refers to the address where the section is loaded.
91 using LVSectionAddresses = std::map<LVSectionIndex, object::SectionRef>;
92 LVSectionAddresses SectionAddresses;
93
94 void addSectionAddress(const object::SectionRef &Section) {
95 if (SectionAddresses.find(Section.getAddress()) == SectionAddresses.end())
96 SectionAddresses.emplace(Section.getAddress(), Section);
97 }
98
99 // Image base and virtual address for Executable file.
100 uint64_t ImageBaseAddress = 0;
101 uint64_t VirtualAddress = 0;
102
103 // Object sections with machine code.
104 using LVSections = std::map<LVSectionIndex, object::SectionRef>;
105 LVSections Sections;
106
107 std::vector<std::unique_ptr<LVLines>> DiscoveredLines;
108
109protected:
110 // It contains the LVLineDebug elements representing the logical lines for
111 // the current compile unit, created by parsing the debug line section.
113
114 std::unique_ptr<const MCRegisterInfo> MRI;
116 std::unique_ptr<const MCAsmInfo> MAI;
117 std::unique_ptr<const MCSubtargetInfo> STI;
118 std::unique_ptr<const MCInstrInfo> MII;
119 std::unique_ptr<const MCDisassembler> MD;
120 std::unique_ptr<MCContext> MC;
121 std::unique_ptr<MCInstPrinter> MIP;
122
123 // https://yurydelendik.github.io/webassembly-dwarf/
124 // 2. Consuming and Generating DWARF for WebAssembly Code
125 // Note: Some DWARF constructs don't map one-to-one onto WebAssembly
126 // constructs. We strive to enumerate and resolve any ambiguities here.
127 //
128 // 2.1. Code Addresses
129 // Note: DWARF associates various bits of debug info
130 // with particular locations in the program via its code address (instruction
131 // pointer or PC). However, WebAssembly's linear memory address space does not
132 // contain WebAssembly instructions.
133 //
134 // Wherever a code address (see 2.17 of [DWARF]) is used in DWARF for
135 // WebAssembly, it must be the offset of an instruction relative within the
136 // Code section of the WebAssembly file. The DWARF is considered malformed if
137 // a PC offset is between instruction boundaries within the Code section.
138 //
139 // Note: It is expected that a DWARF consumer does not know how to decode
140 // WebAssembly instructions. The instruction pointer is selected as the offset
141 // in the binary file of the first byte of the instruction, and it is
142 // consistent with the WebAssembly Web API conventions definition of the code
143 // location.
144 //
145 // EXAMPLE: .DEBUG_LINE INSTRUCTION POINTERS
146 // The .debug_line DWARF section maps instruction pointers to source
147 // locations. With WebAssembly, the .debug_line section maps Code
148 // section-relative instruction offsets to source locations.
149 //
150 // EXAMPLE: DW_AT_* ATTRIBUTES
151 // For entities with a single associated code address, DWARF uses
152 // the DW_AT_low_pc attribute to specify the associated code address value.
153 // For WebAssembly, the DW_AT_low_pc's value is a Code section-relative
154 // instruction offset.
155 //
156 // For entities with a single contiguous range of code, DWARF uses a
157 // pair of DW_AT_low_pc and DW_AT_high_pc attributes to specify the associated
158 // contiguous range of code address values. For WebAssembly, these attributes
159 // are Code section-relative instruction offsets.
160 //
161 // For entities with multiple ranges of code, DWARF uses the DW_AT_ranges
162 // attribute, which refers to the array located at the .debug_ranges section.
164
165 // Loads all info for the architecture of the provided object file.
167 StringRef TheFeatures, StringRef TheCPU);
168
169 virtual void mapRangeAddress(const object::ObjectFile &Obj) {}
170 virtual void mapRangeAddress(const object::ObjectFile &Obj,
171 const object::SectionRef &Section,
172 bool IsComdat) {}
173
174 // Create a mapping from virtual address to section.
177
179 getSection(LVScope *Scope, LVAddress Address, LVSectionIndex SectionIndex);
180
183
186 LVSectionIndex SectionIndex);
188 LVSectionIndex SectionIndex,
189 const LVNameInfo &NameInfo);
190
191 LLVM_ABI void processLines(LVLines *DebugLines, LVSectionIndex SectionIndex);
192 LLVM_ABI void processLines(LVLines *DebugLines, LVSectionIndex SectionIndex,
194
195public:
196 LVBinaryReader() = delete;
202 ~LVBinaryReader() override = default;
203
204 void addInlineeLines(LVScope *Scope, LVLines &Lines) {
205 CUInlineeLines.emplace(Scope, std::make_unique<LVLines>(std::move(Lines)));
206 }
207
208 // Convert Segment::Offset pair to absolute address.
210 LVAddress Addendum = 0) {
211 return ImageBaseAddress + (Segment * VirtualAddress) + Offset + Addendum;
212 }
213
215 LVSectionIndex SectionIndex = 0);
217 LVSectionIndex SectionIndex, bool IsComdat);
219
224
226 return Scope ? getSymbolTableIndex(Scope->getLinkageName())
228 }
229
230 LLVM_ABI void print(raw_ostream &OS) const;
231
232#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
233 void dump() const { print(dbgs()); }
234#endif
235};
236
237} // end namespace logicalview
238} // end namespace llvm
239
240#endif // LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVBINARYREADER_H
#define LLVM_ABI
Definition Compiler.h:215
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM_ABI const LVSymbolTableEntry & getSymbolTableEntry(StringRef Name)
LLVM_ABI LVSectionIndex updateSymbolTable(LVScope *Function)
LLVM_ABI Expected< std::pair< LVSectionIndex, object::SectionRef > > getSection(LVScope *Scope, LVAddress Address, LVSectionIndex SectionIndex)
~LVBinaryReader() override=default
LVSectionIndex getSectionIndex(LVScope *Scope) override
std::unique_ptr< MCContext > MC
LLVM_ABI void includeInlineeLines(LVSectionIndex SectionIndex, LVScope *Function)
std::unique_ptr< const MCInstrInfo > MII
LLVM_ABI Error loadGenericTargetInfo(StringRef TheTriple, StringRef TheFeatures, StringRef TheCPU)
void addInlineeLines(LVScope *Scope, LVLines &Lines)
LVBinaryReader(const LVBinaryReader &)=delete
LLVM_ABI LVAddress getSymbolTableAddress(StringRef Name)
LLVM_ABI void print(raw_ostream &OS) const
std::unique_ptr< const MCSubtargetInfo > STI
LVAddress linearAddress(uint16_t Segment, uint32_t Offset, LVAddress Addendum=0)
LLVM_ABI void addToSymbolTable(StringRef Name, LVScope *Function, LVSectionIndex SectionIndex=0)
virtual void mapRangeAddress(const object::ObjectFile &Obj)
LLVM_ABI void processLines(LVLines *DebugLines, LVSectionIndex SectionIndex)
LLVM_ABI void mapVirtualAddress(const object::ObjectFile &Obj)
std::unique_ptr< const MCAsmInfo > MAI
LLVM_ABI LVSectionIndex getSymbolTableIndex(StringRef Name)
LVBinaryReader & operator=(const LVBinaryReader &)=delete
virtual void mapRangeAddress(const object::ObjectFile &Obj, const object::SectionRef &Section, bool IsComdat)
LLVM_ABI bool getSymbolTableIsComdat(StringRef Name)
std::unique_ptr< const MCRegisterInfo > MRI
LVBinaryReader(StringRef Filename, StringRef FileFormatName, ScopedPrinter &W, LVBinaryType BinaryType)
std::unique_ptr< const MCDisassembler > MD
std::unique_ptr< MCInstPrinter > MIP
LVSectionIndex DotTextSectionIndex
Definition LVReader.h:152
LLVM_ABI LVSectionIndex getIndex(StringRef Name)
LLVM_ABI bool getIsComdat(StringRef Name)
LLVM_ABI void print(raw_ostream &OS)
LLVM_ABI LVAddress getAddress(StringRef Name)
LLVM_ABI void add(StringRef Name, LVScope *Function, LVSectionIndex SectionIndex=0)
LLVM_ABI LVSectionIndex update(LVScope *Function)
LLVM_ABI const LVSymbolTableEntry & getEntry(StringRef Name)
This class is the base class for all object file types.
Definition ObjectFile.h:231
This is a value type class that represents a single section in the list of sections in the object fil...
Definition ObjectFile.h:83
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
std::pair< LVAddress, uint64_t > LVNameInfo
Definition LVScope.h:30
constexpr bool UpdateHighAddress
uint64_t LVSectionIndex
Definition LVObject.h:35
SmallVector< LVLine *, 8 > LVLines
Definition LVObject.h:77
uint64_t LVAddress
Definition LVObject.h:36
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LVSymbolTableEntry(LVScope *Scope, LVAddress Address, LVSectionIndex SectionIndex, bool IsComdat)