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"
30
31namespace llvm {
32namespace logicalview {
33
34constexpr bool UpdateHighAddress = false;
35
36// Logical scope, Section address, Section index, IsComdat.
48
49// Function names extracted from the object symbol table.
50class LVSymbolTable final {
51 using LVSymbolNames = std::map<std::string, LVSymbolTableEntry, std::less<>>;
52 LVSymbolNames SymbolNames;
53
54public:
55 LVSymbolTable() = default;
56
58 LVSectionIndex SectionIndex = 0);
60 LVSectionIndex SectionIndex, bool IsComdat);
62
67
68 LLVM_ABI void print(raw_ostream &OS);
69};
70
71class LVBinaryReader : public LVReader {
72 // Function names extracted from the object symbol table.
73 LVSymbolTable SymbolTable;
74
75 // It contains the LVLineDebug elements representing the inlined logical
76 // lines for the current compile unit, created by parsing the CodeView
77 // S_INLINESITE symbol annotation data.
78 using LVInlineeLine = std::map<LVScope *, std::unique_ptr<LVLines>>;
79 LVInlineeLine CUInlineeLines;
80
81 // Instruction lines for a logical scope. These instructions are fetched
82 // during its merge with the debug lines.
84
85 // Links the scope with its first assembler address line.
87
88 // Mapping from virtual address to section.
89 // The virtual address refers to the address where the section is loaded.
90 using LVSectionAddresses = std::map<LVSectionIndex, object::SectionRef>;
91 LVSectionAddresses SectionAddresses;
92
93 void addSectionAddress(const object::SectionRef &Section) {
94 if (SectionAddresses.find(Section.getAddress()) == SectionAddresses.end())
95 SectionAddresses.emplace(Section.getAddress(), Section);
96 }
97
98 // Image base and virtual address for Executable file.
99 uint64_t ImageBaseAddress = 0;
100 uint64_t VirtualAddress = 0;
101
102 // Object sections with machine code.
103 using LVSections = std::map<LVSectionIndex, object::SectionRef>;
104 LVSections Sections;
105
106 std::vector<std::unique_ptr<LVLines>> DiscoveredLines;
107
108protected:
109 // It contains the LVLineDebug elements representing the logical lines for
110 // the current compile unit, created by parsing the debug line section.
112
113 std::unique_ptr<const MCRegisterInfo> MRI;
115 std::unique_ptr<const MCAsmInfo> MAI;
116 std::unique_ptr<const MCSubtargetInfo> STI;
117 std::unique_ptr<const MCInstrInfo> MII;
118 std::unique_ptr<const MCDisassembler> MD;
119 std::unique_ptr<MCContext> MC;
120 std::unique_ptr<MCInstPrinter> MIP;
121
122 // https://yurydelendik.github.io/webassembly-dwarf/
123 // 2. Consuming and Generating DWARF for WebAssembly Code
124 // Note: Some DWARF constructs don't map one-to-one onto WebAssembly
125 // constructs. We strive to enumerate and resolve any ambiguities here.
126 //
127 // 2.1. Code Addresses
128 // Note: DWARF associates various bits of debug info
129 // with particular locations in the program via its code address (instruction
130 // pointer or PC). However, WebAssembly's linear memory address space does not
131 // contain WebAssembly instructions.
132 //
133 // Wherever a code address (see 2.17 of [DWARF]) is used in DWARF for
134 // WebAssembly, it must be the offset of an instruction relative within the
135 // Code section of the WebAssembly file. The DWARF is considered malformed if
136 // a PC offset is between instruction boundaries within the Code section.
137 //
138 // Note: It is expected that a DWARF consumer does not know how to decode
139 // WebAssembly instructions. The instruction pointer is selected as the offset
140 // in the binary file of the first byte of the instruction, and it is
141 // consistent with the WebAssembly Web API conventions definition of the code
142 // location.
143 //
144 // EXAMPLE: .DEBUG_LINE INSTRUCTION POINTERS
145 // The .debug_line DWARF section maps instruction pointers to source
146 // locations. With WebAssembly, the .debug_line section maps Code
147 // section-relative instruction offsets to source locations.
148 //
149 // EXAMPLE: DW_AT_* ATTRIBUTES
150 // For entities with a single associated code address, DWARF uses
151 // the DW_AT_low_pc attribute to specify the associated code address value.
152 // For WebAssembly, the DW_AT_low_pc's value is a Code section-relative
153 // instruction offset.
154 //
155 // For entities with a single contiguous range of code, DWARF uses a
156 // pair of DW_AT_low_pc and DW_AT_high_pc attributes to specify the associated
157 // contiguous range of code address values. For WebAssembly, these attributes
158 // are Code section-relative instruction offsets.
159 //
160 // For entities with multiple ranges of code, DWARF uses the DW_AT_ranges
161 // attribute, which refers to the array located at the .debug_ranges section.
163
164 // Loads all info for the architecture of the provided object file.
166 StringRef TheFeatures, StringRef TheCPU);
167
168 virtual void mapRangeAddress(const object::ObjectFile &Obj) {}
169 virtual void mapRangeAddress(const object::ObjectFile &Obj,
170 const object::SectionRef &Section,
171 bool IsComdat) {}
172
173 // Create a mapping from virtual address to section.
176
178 getSection(LVScope *Scope, LVAddress Address, LVSectionIndex SectionIndex);
179
182
185 LVSectionIndex SectionIndex);
187 LVSectionIndex SectionIndex,
188 const LVNameInfo &NameInfo);
189
190 LLVM_ABI void processLines(LVLines *DebugLines, LVSectionIndex SectionIndex);
191 LLVM_ABI void processLines(LVLines *DebugLines, LVSectionIndex SectionIndex,
193
194public:
195 LVBinaryReader() = delete;
201 ~LVBinaryReader() override = default;
202
203 void addInlineeLines(LVScope *Scope, LVLines &Lines) {
204 CUInlineeLines.emplace(Scope, std::make_unique<LVLines>(std::move(Lines)));
205 }
206
207 // Convert Segment::Offset pair to absolute address.
209 LVAddress Addendum = 0) {
210 return ImageBaseAddress + (Segment * VirtualAddress) + Offset + Addendum;
211 }
212
214 LVSectionIndex SectionIndex = 0);
216 LVSectionIndex SectionIndex, bool IsComdat);
218
223
225 return Scope ? getSymbolTableIndex(Scope->getLinkageName())
227 }
228
229 LLVM_ABI void print(raw_ostream &OS) const;
230
231#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
232 void dump() const { print(dbgs()); }
233#endif
234};
235
236} // end namespace logicalview
237} // end namespace llvm
238
239#endif // LLVM_DEBUGINFO_LOGICALVIEW_READERS_LVBINARYREADER_H
#define LLVM_ABI
Definition Compiler.h:213
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)