LLVM 23.0.0git
DWARFStreamer.h
Go to the documentation of this file.
1//===- DwarfStreamer.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#ifndef LLVM_DWARFLINKER_CLASSIC_DWARFSTREAMER_H
10#define LLVM_DWARFLINKER_CLASSIC_DWARFSTREAMER_H
11
12#include "DWARFLinker.h"
15#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCInstrInfo.h"
23
24namespace llvm {
25template <typename DataT> class AccelTable;
26
27class MCCodeEmitter;
28class DWARFDebugMacro;
29
30namespace dwarf_linker {
31namespace classic {
32
33/// User of DwarfStreamer should call initialization code
34/// for AsmPrinter:
35///
36/// InitializeAllTargetInfos();
37/// InitializeAllTargetMCs();
38/// InitializeAllTargets();
39/// InitializeAllAsmPrinters();
40
41/// The Dwarf streaming logic.
42///
43/// All interactions with the MC layer that is used to build the debug
44/// information binary representation are handled in this class.
46public:
48 raw_pwrite_stream &OutFile,
50 : OutFile(OutFile), OutFileType(OutFileType), WarningHandler(Warning) {}
51 ~DwarfStreamer() override = default;
52
54 const Triple &TheTriple, DWARFLinkerBase::OutputFileType FileType,
56
57 Error init(Triple TheTriple, StringRef Swift5ReflectionSegmentName);
58
59 /// Dump the file to the disk.
60 void finish() override;
61
62 AsmPrinter &getAsmPrinter() const { return *Asm; }
63
64 /// Set the current output section to debug_info and change
65 /// the MC Dwarf version to \p DwarfVersion.
66 void switchToDebugInfoSection(unsigned DwarfVersion);
67
68 /// Emit the compilation unit header for \p Unit in the
69 /// debug_info section.
70 ///
71 /// As a side effect, this also switches the current Dwarf version
72 /// of the MC layer to the one of U.getOrigUnit().
73 void emitCompileUnitHeader(CompileUnit &Unit, unsigned DwarfVersion) override;
74
75 /// Recursively emit the DIE tree rooted at \p Die.
76 void emitDIE(DIE &Die) override;
77
78 /// Emit the abbreviation table \p Abbrevs to the debug_abbrev section.
79 void emitAbbrevs(const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
80 unsigned DwarfVersion) override;
81
82 /// Emit contents of section SecName From Obj.
83 void emitSectionContents(StringRef SecData,
84 DebugSectionKind SecKind) override;
85
86 /// Emit the string table described by \p Pool into .debug_str table.
87 void emitStrings(const NonRelocatableStringpool &Pool) override;
88
89 /// Emit the debug string offset table described by \p StringOffsets into the
90 /// .debug_str_offsets table.
91 void emitStringOffsets(const SmallVector<uint64_t> &StringOffset,
92 uint16_t TargetDWARFVersion) override;
93
94 /// Emit the string table described by \p Pool into .debug_line_str table.
95 void emitLineStrings(const NonRelocatableStringpool &Pool) override;
96
97 /// Emit the swift_ast section stored in \p Buffer.
98 void emitSwiftAST(StringRef Buffer);
99
100 /// Emit the swift reflection section stored in \p Buffer.
101 void emitSwiftReflectionSection(
103 StringRef Buffer, uint32_t Alignment, uint32_t Size);
104
105 /// Emit debug ranges(.debug_ranges, .debug_rnglists) header.
106 MCSymbol *emitDwarfDebugRangeListHeader(const CompileUnit &Unit) override;
107
108 /// Emit debug ranges(.debug_ranges, .debug_rnglists) fragment.
109 Error emitDwarfDebugRangeListFragment(const CompileUnit &Unit,
110 const AddressRanges &LinkedRanges,
111 PatchLocation Patch,
112 DebugDieValuePool &AddrPool) override;
113
114 /// Emit debug ranges(.debug_ranges, .debug_rnglists) footer.
115 void emitDwarfDebugRangeListFooter(const CompileUnit &Unit,
116 MCSymbol *EndLabel) override;
117
118 /// Emit debug locations(.debug_loc, .debug_loclists) header.
119 MCSymbol *emitDwarfDebugLocListHeader(const CompileUnit &Unit) override;
120
121 /// Emit .debug_addr header.
122 MCSymbol *emitDwarfDebugAddrsHeader(const CompileUnit &Unit) override;
123
124 /// Emit the addresses described by \p Addrs into .debug_addr table.
125 void emitDwarfDebugAddrs(const SmallVector<uint64_t> &Addrs,
126 uint8_t AddrSize) override;
127
128 /// Emit .debug_addr footer.
129 void emitDwarfDebugAddrsFooter(const CompileUnit &Unit,
130 MCSymbol *EndLabel) override;
131
132 /// Emit debug ranges(.debug_loc, .debug_loclists) fragment.
133 Error emitDwarfDebugLocListFragment(
134 const CompileUnit &Unit,
135 const DWARFLocationExpressionsVector &LinkedLocationExpression,
136 PatchLocation Patch, DebugDieValuePool &AddrPool) override;
137
138 /// Emit debug ranges(.debug_loc, .debug_loclists) footer.
139 void emitDwarfDebugLocListFooter(const CompileUnit &Unit,
140 MCSymbol *EndLabel) override;
141
142 /// Emit .debug_aranges entries for \p Unit
143 void emitDwarfDebugArangesTable(const CompileUnit &Unit,
144 const AddressRanges &LinkedRanges) override;
145
146 uint64_t getRangesSectionSize() const override { return RangesSectionSize; }
147
149 return RngListsSectionSize;
150 }
151
152 /// Emit .debug_line table entry for specified \p LineTable
153 /// The optional parameter RowOffsets, if provided, will be populated with the
154 /// offsets of each line table row in the output .debug_line section.
155 void
156 emitLineTableForUnit(const DWARFDebugLine::LineTable &LineTable,
157 const CompileUnit &Unit, OffsetsStringPool &DebugStrPool,
158 OffsetsStringPool &DebugLineStrPool,
159 std::vector<uint64_t> *RowOffsets = nullptr) override;
160
161 uint64_t getLineSectionSize() const override { return LineSectionSize; }
162
163 /// Emit the .debug_pubnames contribution for \p Unit.
164 void emitPubNamesForUnit(const CompileUnit &Unit) override;
165
166 /// Emit the .debug_pubtypes contribution for \p Unit.
167 void emitPubTypesForUnit(const CompileUnit &Unit) override;
168
169 /// Emit a CIE.
170 void emitCIE(StringRef CIEBytes) override;
171
172 /// Emit an FDE with data \p Bytes.
173 void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint64_t Address,
174 StringRef Bytes) override;
175
176 /// Emit DWARF debug names.
177 void emitDebugNames(DWARF5AccelTable &Table) override;
178
179 /// Emit Apple namespaces accelerator table.
180 void emitAppleNamespaces(
182
183 /// Emit Apple names accelerator table.
184 void
185 emitAppleNames(AccelTable<AppleAccelTableStaticOffsetData> &Table) override;
186
187 /// Emit Apple Objective-C accelerator table.
188 void
189 emitAppleObjc(AccelTable<AppleAccelTableStaticOffsetData> &Table) override;
190
191 /// Emit Apple type accelerator table.
192 void
193 emitAppleTypes(AccelTable<AppleAccelTableStaticTypeData> &Table) override;
194
195 uint64_t getFrameSectionSize() const override { return FrameSectionSize; }
196
198 return DebugInfoSectionSize;
199 }
200
202 return MacInfoSectionSize;
203 }
204
206 return MacroSectionSize;
207 }
208
210 return LocListsSectionSize;
211 }
212
213 uint64_t getDebugAddrSectionSize() const override { return AddrSectionSize; }
214
215 void emitMacroTables(DWARFContext *Context,
216 const Offset2UnitMap &UnitMacroMap,
217 OffsetsStringPool &StringPool) override;
218
219private:
220 inline void warn(const Twine &Warning, StringRef Context = "") {
221 if (WarningHandler)
222 WarningHandler(Warning, Context, nullptr);
223 }
224
226 StringRef Section) {
227 if (Offset <= FP.getDwarfMaxOffset())
228 return Offset;
229 return createStringError(Section + " section offset 0x" +
230 Twine::utohexstr(Offset) + " exceeds the " +
231 dwarf::FormatString(FP.Format) + " limit");
232 }
233
234 MCSection *getMCSection(DebugSectionKind SecKind);
235
236 void emitMacroTableImpl(const DWARFDebugMacro *MacroTable,
237 const Offset2UnitMap &UnitMacroMap,
239
240 /// Emit piece of .debug_ranges for \p LinkedRanges.
241 Error emitDwarfDebugRangesTableFragment(const CompileUnit &Unit,
242 const AddressRanges &LinkedRanges,
243 PatchLocation Patch);
244
245 /// Emit piece of .debug_rnglists for \p LinkedRanges.
246 Error emitDwarfDebugRngListsTableFragment(const CompileUnit &Unit,
247 const AddressRanges &LinkedRanges,
248 PatchLocation Patch,
249 DebugDieValuePool &AddrPool);
250
251 /// Emit piece of .debug_loc for \p LinkedRanges.
252 Error emitDwarfDebugLocTableFragment(
253 const CompileUnit &Unit,
254 const DWARFLocationExpressionsVector &LinkedLocationExpression,
255 PatchLocation Patch);
256
257 /// Emit piece of .debug_loclists for \p LinkedRanges.
258 Error emitDwarfDebugLocListsTableFragment(
259 const CompileUnit &Unit,
260 const DWARFLocationExpressionsVector &LinkedLocationExpression,
261 PatchLocation Patch, DebugDieValuePool &AddrPool);
262
263 /// \defgroup Line table emission
264 /// @{
265 void emitLineTablePrologue(const DWARFDebugLine::Prologue &P,
266 OffsetsStringPool &DebugStrPool,
267 OffsetsStringPool &DebugLineStrPool);
268 void emitLineTableString(const DWARFDebugLine::Prologue &P,
269 const DWARFFormValue &String,
270 OffsetsStringPool &DebugStrPool,
271 OffsetsStringPool &DebugLineStrPool);
272 void emitLineTableProloguePayload(const DWARFDebugLine::Prologue &P,
273 OffsetsStringPool &DebugStrPool,
274 OffsetsStringPool &DebugLineStrPool);
275 void emitLineTablePrologueV2IncludeAndFileTable(
276 const DWARFDebugLine::Prologue &P, OffsetsStringPool &DebugStrPool,
277 OffsetsStringPool &DebugLineStrPool);
278 void emitLineTablePrologueV5IncludeAndFileTable(
279 const DWARFDebugLine::Prologue &P, OffsetsStringPool &DebugStrPool,
280 OffsetsStringPool &DebugLineStrPool);
281 void emitLineTableRows(const DWARFDebugLine::LineTable &LineTable,
282 MCSymbol *LineEndSym, unsigned AddressByteSize,
283 std::vector<uint64_t> *RowOffsets = nullptr);
284 void emitIntOffset(uint64_t Offset, dwarf::DwarfFormat Format,
285 uint64_t &SectionSize);
286 void emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
287 dwarf::DwarfFormat Format, uint64_t &SectionSize);
288 /// @}
289
290 /// \defgroup MCObjects MC layer objects constructed by the streamer
291 /// @{
292 MCTargetOptions MCOptions;
293 std::unique_ptr<MCRegisterInfo> MRI;
294 std::unique_ptr<MCAsmInfo> MAI;
295 std::unique_ptr<MCObjectFileInfo> MOFI;
296 std::unique_ptr<MCContext> MC;
297 MCAsmBackend *MAB; // Owned by MCStreamer
298 std::unique_ptr<MCInstrInfo> MII;
299 std::unique_ptr<MCSubtargetInfo> MSTI;
300 MCCodeEmitter *MCE; // Owned by MCStreamer
301 MCStreamer *MS; // Owned by AsmPrinter
302 std::unique_ptr<TargetMachine> TM;
303 std::unique_ptr<AsmPrinter> Asm;
304 /// @}
305
306 /// The output file we stream the linked Dwarf to.
307 raw_pwrite_stream &OutFile;
308 DWARFLinker::OutputFileType OutFileType = DWARFLinker::OutputFileType::Object;
309
310 uint64_t RangesSectionSize = 0;
311 uint64_t RngListsSectionSize = 0;
312 uint64_t LocSectionSize = 0;
313 uint64_t LocListsSectionSize = 0;
314 uint64_t LineSectionSize = 0;
315 uint64_t FrameSectionSize = 0;
316 uint64_t DebugInfoSectionSize = 0;
317 uint64_t MacInfoSectionSize = 0;
318 uint64_t MacroSectionSize = 0;
319 uint64_t AddrSectionSize = 0;
320 uint64_t StrOffsetSectionSize = 0;
321
322 /// Keep track of emitted CUs and their Unique ID.
323 struct EmittedUnit {
324 unsigned ID;
325 MCSymbol *LabelBegin;
326 };
327 std::vector<EmittedUnit> EmittedUnits;
328
329 /// Emit the pubnames or pubtypes section contribution for \p
330 /// Unit into \p Sec. The data is provided in \p Names.
331 void emitPubSectionForUnit(MCSection *Sec, StringRef Name,
332 const CompileUnit &Unit,
333 const std::vector<CompileUnit::AccelInfo> &Names);
334
335 DWARFLinkerBase::MessageHandlerTy WarningHandler = nullptr;
336};
337
338} // end of namespace classic
339} // end of namespace dwarf_linker
340} // end of namespace llvm
341
342#endif // LLVM_DWARFLINKER_CLASSIC_DWARFSTREAMER_H
#define LLVM_ABI
Definition Compiler.h:213
#define P(N)
This class holds an abstract representation of an Accelerator Table, consisting of a sequence of buck...
Definition AccelTable.h:203
The AddressRanges class helps normalize address range collections.
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:91
A structured debug information entry.
Definition DIE.h:828
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
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
Generic interface to target specific assembler backends.
MCCodeEmitter - Generic instruction encoding interface.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:569
Streaming machine code generation interface.
Definition MCStreamer.h:222
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
A string table that doesn't need relocations.
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
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static Twine utohexstr(uint64_t Val)
Definition Twine.h:385
std::function< void( const Twine &Warning, StringRef Context, const DWARFDie *DIE)> MessageHandlerTy
Stores all information relating to a compile unit, be it in its original instance in the object file ...
DwarfEmitter presents interface to generate all debug info tables.
Definition DWARFLinker.h:41
uint64_t getDebugInfoSectionSize() const override
Returns size of generated .debug_info section.
uint64_t getRangesSectionSize() const override
Returns size of generated .debug_ranges section.
DwarfStreamer(DWARFLinkerBase::OutputFileType OutFileType, raw_pwrite_stream &OutFile, DWARFLinkerBase::MessageHandlerTy Warning)
uint64_t getDebugMacroSectionSize() const override
Returns size of generated .debug_macro section.
static Expected< std::unique_ptr< DwarfStreamer > > createStreamer(const Triple &TheTriple, DWARFLinkerBase::OutputFileType FileType, raw_pwrite_stream &OutFile, DWARFLinkerBase::MessageHandlerTy Warning)
uint64_t getLineSectionSize() const override
Returns size of generated .debug_line section.
uint64_t getDebugAddrSectionSize() const override
Returns size of generated .debug_addr section.
Error init(Triple TheTriple, StringRef Swift5ReflectionSegmentName)
uint64_t getRngListsSectionSize() const override
Returns size of generated .debug_rnglists section.
uint64_t getLocListsSectionSize() const override
Returns size of generated .debug_loclists section.
void finish() override
Dump the file to the disk.
uint64_t getDebugMacInfoSectionSize() const override
Returns size of generated .debug_macinfo section.
uint64_t getFrameSectionSize() const override
Returns size of generated .debug_frame section.
An abstract base class for streams implementations that also support a pwrite operation.
LLVM_ABI StringRef FormatString(DwarfFormat Format)
Definition Dwarf.cpp:1023
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
Swift5ReflectionSectionKind
Definition Swift.h:14
IndexedValuesMap< uint64_t > DebugDieValuePool
Definition DWARFLinker.h:38
DenseMap< uint64_t, CompileUnit * > Offset2UnitMap
Definition DWARFLinker.h:37
DebugSectionKind
List of tracked debug tables.
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition Dwarf.h:93
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
StrongType< NonRelocatableStringpool, OffsetsTag > OffsetsStringPool
std::vector< DWARFLocationExpression > DWARFLocationExpressionsVector
Represents a set of absolute location expressions.
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition Dwarf.h:1114