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) const {
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 };
99
100 SectionData Sections[DS_NumSections];
101 DWPSectionId CurrentSection = DS_Info;
102 uint16_t ELFMachine = 0;
103 uint8_t ELFOSABI = 0;
104 bool IsWASM = false;
105 bool IsLittleEndian = true;
106
107public:
108 DWPWriter() = default;
109
110 void setMachine(uint16_t Machine) { ELFMachine = Machine; }
111 void setOSABI(uint8_t OSABI) { ELFOSABI = OSABI; }
112 void setIsWASM(bool V) { IsWASM = V; }
113 void setIsLittleEndian(bool V) { IsLittleEndian = V; }
114
116 return Sections[Id].Buffer;
117 }
118
119 void switchSection(DWPSectionId Id) { CurrentSection = Id; }
120
121 /// Zero-copy: stores a reference to the input data without copying.
122 /// Flushes any pending inline data first to preserve output order.
124 if (!Data.empty()) {
125 auto &SD = Sections[CurrentSection];
126 SD.flushBuffer();
127 SD.Chunks.push_back(Data);
128 }
129 }
130
131 void emitIntValue(uint64_t Value, unsigned Size) {
132 auto &Buf = Sections[CurrentSection].Buffer;
133 if (IsLittleEndian) {
134 for (unsigned I = 0; I < Size; ++I) {
135 Buf.push_back(static_cast<char>(Value & 0xff));
136 Value >>= 8;
137 }
138 } else {
139 for (unsigned I = 0; I < Size; ++I) {
140 Buf.push_back(
141 static_cast<char>((Value >> (8 * (Size - 1 - I))) & 0xff));
142 }
143 }
144 }
145
146 Error writeELF(raw_pwrite_stream &OS);
147 Error writeWASM(raw_pwrite_stream &OS);
149 return IsWASM ? writeWASM(OS) : writeELF(OS);
150 }
151};
152
154 DWPWriter &Out;
156 uint64_t Offset = 0;
157
158public:
159 DWPStringPool(DWPWriter &Out) : Out(Out) {}
160
161 uint64_t getOffset(const char *Str, unsigned Length) {
162 assert(strlen(Str) + 1 == Length && "Ensure length hint is correct");
163
164 StringRef Key(Str, Length);
165 auto Pair = Pool.insert(std::make_pair(Key, Offset));
166 if (Pair.second) {
167 Out.emitBytes(Key);
168 Offset += Length;
169 }
170
171 return Pair.first->second;
172 }
173};
174
181
182// Holds data for Skeleton, Split Compilation, and Type Unit Headers (only in
183// v5) as defined in Dwarf 5 specification, 7.5.1.2, 7.5.1.3 and Dwarf 4
184// specification 7.5.1.1.
186 // unit_length field. Note that the type is uint64_t even in 32-bit dwarf.
188
189 // version field.
191
192 // unit_type field. Initialized only if Version >= 5.
194
195 // address_size field.
197
198 // debug_abbrev_offset field. Note that the type is uint64_t even in 32-bit
199 // dwarf. It is assumed to be 0.
201
202 // dwo_id field. This resides in the header only if Version >= 5.
203 // In earlier versions, it is read from DW_AT_GNU_dwo_id.
204 std::optional<uint64_t> Signature;
205
206 // Derived from the length of Length field.
208
209 // The size of the Header in bytes. This is derived while parsing the header,
210 // and is stored as a convenience.
212};
213
216 const char *Name = "";
217 const char *DWOName = "";
218};
219
221 OnCuIndexOverflow OverflowOptValue,
222 Dwarf64StrOffsetsPromotion StrOffsetsOptValue,
223 raw_pwrite_stream *OS = nullptr);
224
225typedef std::vector<std::pair<DWARFSectionKind, uint32_t>> SectionLengths;
226
228parseInfoSectionUnitHeader(StringRef Info, bool IsLittleEndian);
229
230} // namespace llvm
231#endif // LLVM_DWP_DWP_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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:161
DWPStringPool(DWPWriter &Out)
Definition DWP.h:159
Direct ELF writer for DWP output.
Definition DWP.h:59
void switchSection(DWPSectionId Id)
Definition DWP.h:119
Error write(raw_pwrite_stream &OS)
Definition DWP.h:148
Error writeWASM(raw_pwrite_stream &OS)
Definition DWP.cpp:1209
void setIsWASM(bool V)
Definition DWP.h:112
void setIsLittleEndian(bool V)
Definition DWP.h:113
void setMachine(uint16_t Machine)
Definition DWP.h:110
Error writeELF(raw_pwrite_stream &OS)
Definition DWP.cpp:1075
DWPWriter()=default
void setOSABI(uint8_t OSABI)
Definition DWP.h:111
void emitBytes(StringRef Data)
Zero-copy: stores a reference to the input data without copying.
Definition DWP.h:123
void emitIntValue(uint64_t Value, unsigned Size)
Definition DWP.h:131
SmallVectorImpl< char > & getSectionBuffer(DWPSectionId Id)
Definition DWP.h:115
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.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
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:225
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:217
dwarf::DwarfFormat Format
Definition DWP.h:207
std::optional< uint64_t > Signature
Definition DWP.h:204
uint64_t DebugAbbrevOffset
Definition DWP.h:200
StringRef DWPName
Definition DWP.h:179
std::string DWOName
Definition DWP.h:178
DWARFUnitIndex::Entry::SectionContribution Contributions[8]
Definition DWP.h:176
std::string Name
Definition DWP.h:177