LLVM 24.0.0git
DWP.h
Go to the documentation of this file.
1#ifndef LLVM_DWP_DWP_H
2#define LLVM_DWP_DWP_H
3
4#include "llvm/ADT/ArrayRef.h"
12#include "llvm/Support/Error.h"
13#include <deque>
14#include <vector>
15
16namespace llvm::object {
17class ObjectFile;
18}
19
20namespace llvm {
22
28
30 Disabled, ///< Don't do any conversion of .debug_str_offsets tables.
31 Enabled, ///< Convert any .debug_str_offsets tables to DWARF64 if needed.
32 Always, ///< Always emit .debug_str_offsets talbes as DWARF64 for testing.
33};
34
35/// Section identifiers for DWP output.
51
52/// Direct ELF writer for DWP output.
53///
54/// Section data is stored as zero-copy StringRef chunks pointing to the
55/// mmap'd input files, plus an inline buffer for constructed data
56/// (emitIntValue). This avoids copying gigabytes of debug section data
57/// through the MC infrastructure (MCContext, MCAssembler, MCDataFragment
58/// allocation, layout, etc.).
60 /// Per-section storage: ordered sequence of zero-copy chunks and inline
61 /// data. emitBytes() adds zero-copy StringRef references, emitIntValue()
62 /// appends to an inline buffer. When emitBytes() is called with pending
63 /// inline data, the buffer is flushed to an owned block first to preserve
64 /// the correct interleaving order in the output.
65 struct SectionData {
66 SmallVector<StringRef, 4> Chunks; // ordered segments (refs + flushed bufs)
67 SmallVector<char, 0> Buffer; // pending inline data (emitIntValue)
68 // Heap storage for flushed buffers. Uses std::deque so that push_back
69 // does not invalidate existing elements (StringRefs point into these).
70 std::deque<SmallVector<char, 0>> OwnedBuffers;
71
72 /// Flush pending Buffer data into Chunks as an owned block.
73 void flushBuffer() {
74 if (!Buffer.empty()) {
75 OwnedBuffers.push_back(std::move(Buffer));
76 auto &B = OwnedBuffers.back();
77 Chunks.push_back(StringRef(B.data(), B.size()));
78 Buffer = SmallVector<char, 0>();
79 }
80 }
81
82 uint64_t totalSize() const {
83 uint64_t Size = 0;
84 for (auto &C : Chunks)
85 Size += C.size();
86 Size += Buffer.size();
87 return Size;
88 }
89
90 bool empty() const { return Chunks.empty() && Buffer.empty(); }
91
92 void writeTo(raw_ostream &OS) {
93 for (auto &C : Chunks)
94 OS.write(C.data(), C.size());
95 if (!Buffer.empty())
96 OS.write(Buffer.data(), Buffer.size());
97
98 // Clear buffers to save some memory.
99 Chunks = {};
100 Buffer = {};
101 OwnedBuffers = {};
102 }
103 };
104
105 SectionData Sections[DS_NumSections];
106 DWPSectionId CurrentSection = DS_Info;
107 uint16_t ELFMachine = 0;
108 uint8_t ELFOSABI = 0;
109 bool IsWASM = false;
110 bool IsLittleEndian = true;
111
112public:
113 DWPWriter() = default;
114
115 void setMachine(uint16_t Machine) { ELFMachine = Machine; }
116 void setOSABI(uint8_t OSABI) { ELFOSABI = OSABI; }
117 void setIsWASM(bool V) { IsWASM = V; }
118 void setIsLittleEndian(bool V) { IsLittleEndian = V; }
119
121 return Sections[Id].Buffer;
122 }
123
124 void switchSection(DWPSectionId Id) { CurrentSection = Id; }
125
126 /// Zero-copy: stores a reference to the input data without copying.
127 /// Flushes any pending inline data first to preserve output order.
129 if (!Data.empty()) {
130 auto &SD = Sections[CurrentSection];
131 SD.flushBuffer();
132 SD.Chunks.push_back(Data);
133 }
134 }
135
136 void emitIntValue(uint64_t Value, unsigned Size) {
137 auto &Buf = Sections[CurrentSection].Buffer;
138 if (IsLittleEndian) {
139 for (unsigned I = 0; I < Size; ++I) {
140 Buf.push_back(static_cast<char>(Value & 0xff));
141 Value >>= 8;
142 }
143 } else {
144 for (unsigned I = 0; I < Size; ++I) {
145 Buf.push_back(
146 static_cast<char>((Value >> (8 * (Size - 1 - I))) & 0xff));
147 }
148 }
149 }
150
151 Error writeELF(raw_pwrite_stream &OS);
152 Error writeWASM(raw_pwrite_stream &OS);
154 return IsWASM ? writeWASM(OS) : writeELF(OS);
155 }
156};
157
159 DWPWriter &Out;
161 uint64_t Offset = 0;
162
163public:
164 DWPStringPool(DWPWriter &Out) : Out(Out) {}
165
166 uint64_t getOffset(const char *Str, unsigned Length) {
167 assert(strlen(Str) + 1 == Length && "Ensure length hint is correct");
168
169 StringRef Key(Str, Length);
170 auto Pair = Pool.insert(std::make_pair(Key, Offset));
171 if (Pair.second) {
172 Out.emitBytes(Key);
173 Offset += Length;
174 }
175
176 return Pair.first->second;
177 }
178
180};
181
188
189// Holds data for Skeleton, Split Compilation, and Type Unit Headers (only in
190// v5) as defined in Dwarf 5 specification, 7.5.1.2, 7.5.1.3 and Dwarf 4
191// specification 7.5.1.1.
193 // unit_length field. Note that the type is uint64_t even in 32-bit dwarf.
195
196 // version field.
198
199 // unit_type field. Initialized only if Version >= 5.
201
202 // address_size field.
204
205 // debug_abbrev_offset field. Note that the type is uint64_t even in 32-bit
206 // dwarf. It is assumed to be 0.
208
209 // dwo_id field. This resides in the header only if Version >= 5.
210 // In earlier versions, it is read from DW_AT_GNU_dwo_id.
211 std::optional<uint64_t> Signature;
212
213 // Derived from the length of Length field.
215
216 // The size of the Header in bytes. This is derived while parsing the header,
217 // and is stored as a convenience.
219};
220
223 const char *Name = "";
224 const char *DWOName = "";
225};
226
228 OnCuIndexOverflow OverflowOptValue,
229 Dwarf64StrOffsetsPromotion StrOffsetsOptValue,
230 raw_pwrite_stream *OS = nullptr);
231
232typedef std::vector<std::pair<DWARFSectionKind, uint32_t>> SectionLengths;
233
235parseInfoSectionUnitHeader(StringRef Info, bool IsLittleEndian);
236
237} // namespace llvm
238#endif // LLVM_DWP_DWP_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
#define I(x, y, z)
Definition MD5.cpp:57
This file implements a map that provides insertion order iteration.
Function const char TargetMachine * Machine
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
uint64_t getOffset(const char *Str, unsigned Length)
Definition DWP.h:166
DWPStringPool(DWPWriter &Out)
Definition DWP.h:164
Direct ELF writer for DWP output.
Definition DWP.h:59
void switchSection(DWPSectionId Id)
Definition DWP.h:124
Error write(raw_pwrite_stream &OS)
Definition DWP.h:153
Error writeWASM(raw_pwrite_stream &OS)
Definition DWP.cpp:1212
void setIsWASM(bool V)
Definition DWP.h:117
void setIsLittleEndian(bool V)
Definition DWP.h:118
void setMachine(uint16_t Machine)
Definition DWP.h:115
Error writeELF(raw_pwrite_stream &OS)
Definition DWP.cpp:1077
DWPWriter()=default
void setOSABI(uint8_t OSABI)
Definition DWP.h:116
void emitBytes(StringRef Data)
Zero-copy: stores a reference to the input data without copying.
Definition DWP.h:128
void emitIntValue(uint64_t Value, unsigned Size)
Definition DWP.h:136
SmallVectorImpl< char > & getSectionBuffer(DWPSectionId Id)
Definition DWP.h:120
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
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
This class is the base class for all object file types.
Definition ObjectFile.h:231
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & write(unsigned char C)
An abstract base class for streams implementations that also support a pwrite operation.
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition Dwarf.h:93
@ DWARF32
Definition Dwarf.h:93
This is an optimization pass for GlobalISel generic memory operations.
@ Length
Definition DWP.cpp:578
std::vector< std::pair< DWARFSectionKind, uint32_t > > SectionLengths
Definition DWP.h:232
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI Expected< InfoSectionUnitHeader > parseInfoSectionUnitHeader(StringRef Info, bool IsLittleEndian)
Definition DWP.cpp:404
DWPSectionId
Section identifiers for DWP output.
Definition DWP.h:36
@ DS_Abbrev
Definition DWP.h:39
@ DS_Str
Definition DWP.h:45
@ DS_Rnglists
Definition DWP.h:43
@ DS_Loc
Definition DWP.h:41
@ DS_Types
Definition DWP.h:38
@ DS_Loclists
Definition DWP.h:42
@ DS_NumSections
Definition DWP.h:49
@ DS_TUIndex
Definition DWP.h:48
@ DS_CUIndex
Definition DWP.h:47
@ DS_Info
Definition DWP.h:37
@ DS_Line
Definition DWP.h:40
@ DS_Macro
Definition DWP.h:44
@ DS_StrOffsets
Definition DWP.h:46
OnCuIndexOverflow
Definition DWP.h:23
@ SoftStop
Definition DWP.h:25
@ HardStop
Definition DWP.h:24
@ Continue
Definition DWP.h:26
Dwarf64StrOffsetsPromotion
Definition DWP.h:29
@ Enabled
Convert any .debug_str_offsets tables to DWARF64 if needed.
Definition DWP.h:31
@ Always
Always emit .debug_str_offsets talbes as DWARF64 for testing.
Definition DWP.h:32
@ Disabled
Don't do any conversion of .debug_str_offsets tables.
Definition DWP.h:30
LLVM_ABI Error write(DWPWriter &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue, Dwarf64StrOffsetsPromotion StrOffsetsOptValue, raw_pwrite_stream *OS=nullptr)
Definition DWP.cpp:747
const char * DWOName
Definition DWP.h:224
dwarf::DwarfFormat Format
Definition DWP.h:214
std::optional< uint64_t > Signature
Definition DWP.h:211
uint64_t DebugAbbrevOffset
Definition DWP.h:207
StringRef DWPName
Definition DWP.h:186
std::string DWOName
Definition DWP.h:185
DWARFUnitIndex::Entry::SectionContribution Contributions[8]
Definition DWP.h:183
std::string Name
Definition DWP.h:184