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 "DWPStringPool.h"
5#include "llvm/ADT/ArrayRef.h"
13#include "llvm/Support/Error.h"
14#include <deque>
15#include <vector>
16
17namespace llvm::object {
18class ObjectFile;
19}
20
21namespace llvm {
23
29
31 Disabled, ///< Don't do any conversion of .debug_str_offsets tables.
32 Enabled, ///< Convert any .debug_str_offsets tables to DWARF64 if needed.
33 Always, ///< Always emit .debug_str_offsets talbes as DWARF64 for testing.
34};
35
36/// Section identifiers for DWP output.
52
53/// Direct ELF writer for DWP output, bypassing MCStreamer.
54///
55/// Section data is stored as zero-copy StringRef chunks pointing to the
56/// mmap'd input files, plus an inline buffer for constructed data
57/// (emitIntValue). This avoids copying gigabytes of debug section data
58/// through the MC infrastructure (MCContext, MCAssembler, MCDataFragment
59/// allocation, layout, etc.).
61 /// Per-section storage: ordered sequence of zero-copy chunks and inline
62 /// data. emitBytes() adds zero-copy StringRef references, emitIntValue()
63 /// appends to an inline buffer. When emitBytes() is called with pending
64 /// inline data, the buffer is flushed to an owned block first to preserve
65 /// the correct interleaving order in the output.
66 struct SectionData {
67 SmallVector<StringRef, 4> Chunks; // ordered segments (refs + flushed bufs)
68 SmallVector<char, 0> Buffer; // pending inline data (emitIntValue)
69 // Heap storage for flushed buffers. Uses std::deque so that push_back
70 // does not invalidate existing elements (StringRefs point into these).
71 std::deque<SmallVector<char, 0>> OwnedBuffers;
72
73 /// Flush pending Buffer data into Chunks as an owned block.
74 void flushBuffer() {
75 if (!Buffer.empty()) {
76 OwnedBuffers.push_back(std::move(Buffer));
77 auto &B = OwnedBuffers.back();
78 Chunks.push_back(StringRef(B.data(), B.size()));
79 Buffer = SmallVector<char, 0>();
80 }
81 }
82
83 uint64_t totalSize() const {
84 uint64_t Size = 0;
85 for (auto &C : Chunks)
86 Size += C.size();
87 Size += Buffer.size();
88 return Size;
89 }
90
91 bool empty() const { return Chunks.empty() && Buffer.empty(); }
92
93 void writeTo(raw_ostream &OS) const {
94 for (auto &C : Chunks)
95 OS.write(C.data(), C.size());
96 if (!Buffer.empty())
97 OS.write(Buffer.data(), Buffer.size());
98 }
99 };
100
101 SectionData Sections[DS_NumSections];
102 DWPSectionId CurrentSection = DS_Info;
103 uint16_t ELFMachine = 0;
104 uint8_t ELFOSABI = 0;
105 bool IsWASM = false;
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
115 return Sections[Id].Buffer;
116 }
117
118 void switchSection(DWPSectionId Id) { CurrentSection = Id; }
119
120 /// Zero-copy: stores a reference to the input data without copying.
121 /// Flushes any pending inline data first to preserve output order.
123 if (!Data.empty()) {
124 auto &SD = Sections[CurrentSection];
125 SD.flushBuffer();
126 SD.Chunks.push_back(Data);
127 }
128 }
129
130 void emitIntValue(uint64_t Value, unsigned Size) {
131 auto &Buf = Sections[CurrentSection].Buffer;
132 for (unsigned I = 0; I < Size; ++I) {
133 Buf.push_back(static_cast<char>(Value & 0xff));
134 Value >>= 8;
135 }
136 }
137
138 Error writeELF(raw_pwrite_stream &OS);
139 Error writeWASM(raw_pwrite_stream &OS);
141 return IsWASM ? writeWASM(OS) : writeELF(OS);
142 }
143};
144
151
152// Holds data for Skeleton, Split Compilation, and Type Unit Headers (only in
153// v5) as defined in Dwarf 5 specification, 7.5.1.2, 7.5.1.3 and Dwarf 4
154// specification 7.5.1.1.
156 // unit_length field. Note that the type is uint64_t even in 32-bit dwarf.
158
159 // version field.
161
162 // unit_type field. Initialized only if Version >= 5.
164
165 // address_size field.
167
168 // debug_abbrev_offset field. Note that the type is uint64_t even in 32-bit
169 // dwarf. It is assumed to be 0.
171
172 // dwo_id field. This resides in the header only if Version >= 5.
173 // In earlier versions, it is read from DW_AT_GNU_dwo_id.
174 std::optional<uint64_t> Signature;
175
176 // Derived from the length of Length field.
178
179 // The size of the Header in bytes. This is derived while parsing the header,
180 // and is stored as a convenience.
182};
183
186 const char *Name = "";
187 const char *DWOName = "";
188};
189
191 OnCuIndexOverflow OverflowOptValue,
192 Dwarf64StrOffsetsPromotion StrOffsetsOptValue,
193 raw_pwrite_stream *OS = nullptr);
194
195typedef std::vector<std::pair<DWARFSectionKind, uint32_t>> SectionLengths;
196
199
200} // namespace llvm
201#endif // LLVM_DWP_DWP_H
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.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Direct ELF writer for DWP output, bypassing MCStreamer.
Definition DWP.h:60
void switchSection(DWPSectionId Id)
Definition DWP.h:118
Error write(raw_pwrite_stream &OS)
Definition DWP.h:140
Error writeWASM(raw_pwrite_stream &OS)
Definition DWP.cpp:1179
void setIsWASM(bool V)
Definition DWP.h:112
void setMachine(uint16_t Machine)
Definition DWP.h:110
Error writeELF(raw_pwrite_stream &OS)
Definition DWP.cpp:1046
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:122
void emitIntValue(uint64_t Value, unsigned Size)
Definition DWP.h:130
SmallVectorImpl< char > & getSectionBuffer(DWPSectionId Id)
Definition DWP.h:114
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.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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.
std::vector< std::pair< DWARFSectionKind, uint32_t > > SectionLengths
Definition DWP.h:195
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:37
@ DS_Abbrev
Definition DWP.h:40
@ DS_Str
Definition DWP.h:46
@ DS_Rnglists
Definition DWP.h:44
@ DS_Loc
Definition DWP.h:42
@ DS_Types
Definition DWP.h:39
@ DS_Loclists
Definition DWP.h:43
@ DS_NumSections
Definition DWP.h:50
@ DS_TUIndex
Definition DWP.h:49
@ DS_CUIndex
Definition DWP.h:48
@ DS_Info
Definition DWP.h:38
@ DS_Line
Definition DWP.h:41
@ DS_Macro
Definition DWP.h:45
@ DS_StrOffsets
Definition DWP.h:47
OnCuIndexOverflow
Definition DWP.h:24
@ SoftStop
Definition DWP.h:26
@ HardStop
Definition DWP.h:25
@ Continue
Definition DWP.h:27
Dwarf64StrOffsetsPromotion
Definition DWP.h:30
@ Enabled
Convert any .debug_str_offsets tables to DWARF64 if needed.
Definition DWP.h:32
@ Always
Always emit .debug_str_offsets talbes as DWARF64 for testing.
Definition DWP.h:33
@ Disabled
Don't do any conversion of .debug_str_offsets tables.
Definition DWP.h:31
LLVM_ABI Error write(DWPWriter &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue, Dwarf64StrOffsetsPromotion StrOffsetsOptValue, raw_pwrite_stream *OS=nullptr)
Definition DWP.cpp:720
const char * DWOName
Definition DWP.h:187
dwarf::DwarfFormat Format
Definition DWP.h:177
std::optional< uint64_t > Signature
Definition DWP.h:174
uint64_t DebugAbbrevOffset
Definition DWP.h:170
StringRef DWPName
Definition DWP.h:149
std::string DWOName
Definition DWP.h:148
DWARFUnitIndex::Entry::SectionContribution Contributions[8]
Definition DWP.h:146
std::string Name
Definition DWP.h:147