LLVM 23.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
106public:
107 DWPWriter() = default;
108
109 void setMachine(uint16_t Machine) { ELFMachine = Machine; }
110 void setOSABI(uint8_t OSABI) { ELFOSABI = OSABI; }
111 void setIsWASM(bool V) { IsWASM = V; }
112
114 return Sections[Id].Buffer;
115 }
116
117 void switchSection(DWPSectionId Id) { CurrentSection = Id; }
118
119 /// Zero-copy: stores a reference to the input data without copying.
120 /// Flushes any pending inline data first to preserve output order.
122 if (!Data.empty()) {
123 auto &SD = Sections[CurrentSection];
124 SD.flushBuffer();
125 SD.Chunks.push_back(Data);
126 }
127 }
128
129 void emitIntValue(uint64_t Value, unsigned Size) {
130 auto &Buf = Sections[CurrentSection].Buffer;
131 for (unsigned I = 0; I < Size; ++I) {
132 Buf.push_back(static_cast<char>(Value & 0xff));
133 Value >>= 8;
134 }
135 }
136
137 Error writeELF(raw_pwrite_stream &OS);
138 Error writeWASM(raw_pwrite_stream &OS);
140 return IsWASM ? writeWASM(OS) : writeELF(OS);
141 }
142};
143
145 DWPWriter &Out;
147 uint64_t Offset = 0;
148
149public:
150 DWPStringPool(DWPWriter &Out) : Out(Out) {}
151
152 uint64_t getOffset(const char *Str, unsigned Length) {
153 assert(strlen(Str) + 1 == Length && "Ensure length hint is correct");
154
155 StringRef Key(Str, Length);
156 auto Pair = Pool.insert(std::make_pair(Key, Offset));
157 if (Pair.second) {
158 Out.emitBytes(Key);
159 Offset += Length;
160 }
161
162 return Pair.first->second;
163 }
164};
165
172
173// Holds data for Skeleton, Split Compilation, and Type Unit Headers (only in
174// v5) as defined in Dwarf 5 specification, 7.5.1.2, 7.5.1.3 and Dwarf 4
175// specification 7.5.1.1.
177 // unit_length field. Note that the type is uint64_t even in 32-bit dwarf.
179
180 // version field.
182
183 // unit_type field. Initialized only if Version >= 5.
185
186 // address_size field.
188
189 // debug_abbrev_offset field. Note that the type is uint64_t even in 32-bit
190 // dwarf. It is assumed to be 0.
192
193 // dwo_id field. This resides in the header only if Version >= 5.
194 // In earlier versions, it is read from DW_AT_GNU_dwo_id.
195 std::optional<uint64_t> Signature;
196
197 // Derived from the length of Length field.
199
200 // The size of the Header in bytes. This is derived while parsing the header,
201 // and is stored as a convenience.
203};
204
207 const char *Name = "";
208 const char *DWOName = "";
209};
210
212 OnCuIndexOverflow OverflowOptValue,
213 Dwarf64StrOffsetsPromotion StrOffsetsOptValue,
214 raw_pwrite_stream *OS = nullptr);
215
216typedef std::vector<std::pair<DWARFSectionKind, uint32_t>> SectionLengths;
217
220
221} // namespace llvm
222#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:213
#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:152
DWPStringPool(DWPWriter &Out)
Definition DWP.h:150
Direct ELF writer for DWP output.
Definition DWP.h:59
void switchSection(DWPSectionId Id)
Definition DWP.h:117
Error write(raw_pwrite_stream &OS)
Definition DWP.h:139
Error writeWASM(raw_pwrite_stream &OS)
Definition DWP.cpp:1180
void setIsWASM(bool V)
Definition DWP.h:111
void setMachine(uint16_t Machine)
Definition DWP.h:109
Error writeELF(raw_pwrite_stream &OS)
Definition DWP.cpp:1047
DWPWriter()=default
void setOSABI(uint8_t OSABI)
Definition DWP.h:110
void emitBytes(StringRef Data)
Zero-copy: stores a reference to the input data without copying.
Definition DWP.h:121
void emitIntValue(uint64_t Value, unsigned Size)
Definition DWP.h:129
SmallVectorImpl< char > & getSectionBuffer(DWPSectionId Id)
Definition DWP.h:113
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:558
std::vector< std::pair< DWARFSectionKind, uint32_t > > SectionLengths
Definition DWP.h:216
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
LLVM_ABI Expected< InfoSectionUnitHeader > parseInfoSectionUnitHeader(StringRef Info)
Definition DWP.cpp:384
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:721
const char * DWOName
Definition DWP.h:208
dwarf::DwarfFormat Format
Definition DWP.h:198
std::optional< uint64_t > Signature
Definition DWP.h:195
uint64_t DebugAbbrevOffset
Definition DWP.h:191
StringRef DWPName
Definition DWP.h:170
std::string DWOName
Definition DWP.h:169
DWARFUnitIndex::Entry::SectionContribution Contributions[8]
Definition DWP.h:167
std::string Name
Definition DWP.h:168