LLVM 17.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_DWARFSTREAMER_H
10#define LLVM_DWARFLINKER_DWARFSTREAMER_H
11
15#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCInstrInfo.h"
22
23namespace llvm {
24template <typename DataT> class AccelTable;
25
26enum class OutputFileType {
27 Object,
29};
30
31/// User of DwarfStreamer should call initialization code
32/// for AsmPrinter:
33///
34/// InitializeAllTargetInfos();
35/// InitializeAllTargetMCs();
36/// InitializeAllTargets();
37/// InitializeAllAsmPrinters();
38
39class MCCodeEmitter;
40class DWARFDebugMacro;
41
42/// The Dwarf streaming logic.
43///
44/// All interactions with the MC layer that is used to build the debug
45/// information binary representation are handled in this class.
47public:
49 std::function<StringRef(StringRef Input)> Translator,
51 : OutFile(OutFile), OutFileType(OutFileType), Translator(Translator),
52 ErrorHandler(Error), WarningHandler(Warning) {}
53
54 bool init(Triple TheTriple, StringRef Swift5ReflectionSegmentName);
55
56 /// Dump the file to the disk.
57 void finish();
58
59 AsmPrinter &getAsmPrinter() const { return *Asm; }
60
61 /// Set the current output section to debug_info and change
62 /// the MC Dwarf version to \p DwarfVersion.
63 void switchToDebugInfoSection(unsigned DwarfVersion);
64
65 /// Emit the compilation unit header for \p Unit in the
66 /// debug_info section.
67 ///
68 /// As a side effect, this also switches the current Dwarf version
69 /// of the MC layer to the one of U.getOrigUnit().
70 void emitCompileUnitHeader(CompileUnit &Unit, unsigned DwarfVersion) override;
71
72 /// Recursively emit the DIE tree rooted at \p Die.
73 void emitDIE(DIE &Die) override;
74
75 /// Emit the abbreviation table \p Abbrevs to the debug_abbrev section.
76 void emitAbbrevs(const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
77 unsigned DwarfVersion) override;
78
79 /// Emit DIE containing warnings.
80 void emitPaperTrailWarningsDie(DIE &Die) override;
81
82 /// Emit contents of section SecName From Obj.
83 void emitSectionContents(StringRef SecData, StringRef SecName) override;
84
85 /// Emit the string table described by \p Pool into .debug_str table.
86 void emitStrings(const NonRelocatableStringpool &Pool) override;
87
88 /// Emit the string table described by \p Pool into .debug_line_str table.
89 void emitLineStrings(const NonRelocatableStringpool &Pool) override;
90
91 /// Emit the swift_ast section stored in \p Buffer.
92 void emitSwiftAST(StringRef Buffer);
93
94 /// Emit the swift reflection section stored in \p Buffer.
97 StringRef Buffer, uint32_t Alignment, uint32_t Size);
98
99 /// Emit debug ranges(.debug_ranges, .debug_rnglists) header.
101
102 /// Emit debug ranges(.debug_ranges, .debug_rnglists) fragment.
104 const AddressRanges &LinkedRanges,
105 PatchLocation Patch) override;
106
107 /// Emit debug ranges(.debug_ranges, .debug_rnglists) footer.
109 MCSymbol *EndLabel) override;
110
111 /// Emit debug locations(.debug_loc, .debug_loclists) header.
112 MCSymbol *emitDwarfDebugLocListHeader(const CompileUnit &Unit) override;
113
114 /// Emit debug ranges(.debug_loc, .debug_loclists) fragment.
116 const CompileUnit &Unit,
117 const DWARFLocationExpressionsVector &LinkedLocationExpression,
118 PatchLocation Patch) override;
119
120 /// Emit debug ranges(.debug_loc, .debug_loclists) footer.
122 MCSymbol *EndLabel) override;
123
124 /// Emit .debug_aranges entries for \p Unit
126 const AddressRanges &LinkedRanges) override;
127
128 uint64_t getRangesSectionSize() const override { return RangesSectionSize; }
129
131 return RngListsSectionSize;
132 }
133
134 /// Emit .debug_line table entry for specified \p LineTable
136 const CompileUnit &Unit,
137 OffsetsStringPool &DebugStrPool,
138 OffsetsStringPool &DebugLineStrPool) override;
139
140 uint64_t getLineSectionSize() const override { return LineSectionSize; }
141
142 /// Emit the .debug_pubnames contribution for \p Unit.
143 void emitPubNamesForUnit(const CompileUnit &Unit) override;
144
145 /// Emit the .debug_pubtypes contribution for \p Unit.
146 void emitPubTypesForUnit(const CompileUnit &Unit) override;
147
148 /// Emit a CIE.
149 void emitCIE(StringRef CIEBytes) override;
150
151 /// Emit an FDE with data \p Bytes.
152 void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint64_t Address,
153 StringRef Bytes) override;
154
155 /// Emit DWARF debug names.
157
158 /// Emit Apple namespaces accelerator table.
161
162 /// Emit Apple names accelerator table.
163 void
165
166 /// Emit Apple Objective-C accelerator table.
167 void
169
170 /// Emit Apple type accelerator table.
171 void
173
174 uint64_t getFrameSectionSize() const override { return FrameSectionSize; }
175
177 return DebugInfoSectionSize;
178 }
179
181 return MacInfoSectionSize;
182 }
183
185 return MacroSectionSize;
186 }
187
189 return LocListsSectionSize;
190 }
191
192 void emitMacroTables(DWARFContext *Context,
193 const Offset2UnitMap &UnitMacroMap,
194 OffsetsStringPool &StringPool) override;
195
196private:
197 inline void error(const Twine &Error, StringRef Context = "") {
198 if (ErrorHandler)
199 ErrorHandler(Error, Context, nullptr);
200 }
201
202 inline void warn(const Twine &Warning, StringRef Context = "") {
203 if (WarningHandler)
204 WarningHandler(Warning, Context, nullptr);
205 }
206
207 void emitMacroTableImpl(const DWARFDebugMacro *MacroTable,
208 const Offset2UnitMap &UnitMacroMap,
209 OffsetsStringPool &StringPool, uint64_t &OutOffset);
210
211 /// Emit piece of .debug_ranges for \p LinkedRanges.
212 void emitDwarfDebugRangesTableFragment(const CompileUnit &Unit,
213 const AddressRanges &LinkedRanges,
214 PatchLocation Patch);
215
216 /// Emit piece of .debug_rnglists for \p LinkedRanges.
217 void emitDwarfDebugRngListsTableFragment(const CompileUnit &Unit,
218 const AddressRanges &LinkedRanges,
219 PatchLocation Patch);
220
221 /// Emit piece of .debug_loc for \p LinkedRanges.
222 void emitDwarfDebugLocTableFragment(
223 const CompileUnit &Unit,
224 const DWARFLocationExpressionsVector &LinkedLocationExpression,
225 PatchLocation Patch);
226
227 /// Emit piece of .debug_loclists for \p LinkedRanges.
228 void emitDwarfDebugLocListsTableFragment(
229 const CompileUnit &Unit,
230 const DWARFLocationExpressionsVector &LinkedLocationExpression,
231 PatchLocation Patch);
232
233 /// \defgroup Line table emission
234 /// @{
235 void emitLineTablePrologue(const DWARFDebugLine::Prologue &P,
236 OffsetsStringPool &DebugStrPool,
237 OffsetsStringPool &DebugLineStrPool);
238 void emitLineTableString(const DWARFDebugLine::Prologue &P,
239 const DWARFFormValue &String,
240 OffsetsStringPool &DebugStrPool,
241 OffsetsStringPool &DebugLineStrPool);
242 void emitLineTableProloguePayload(const DWARFDebugLine::Prologue &P,
243 OffsetsStringPool &DebugStrPool,
244 OffsetsStringPool &DebugLineStrPool);
245 void emitLineTablePrologueV2IncludeAndFileTable(
246 const DWARFDebugLine::Prologue &P, OffsetsStringPool &DebugStrPool,
247 OffsetsStringPool &DebugLineStrPool);
248 void emitLineTablePrologueV5IncludeAndFileTable(
249 const DWARFDebugLine::Prologue &P, OffsetsStringPool &DebugStrPool,
250 OffsetsStringPool &DebugLineStrPool);
251 void emitLineTableRows(const DWARFDebugLine::LineTable &LineTable,
252 MCSymbol *LineEndSym, unsigned AddressByteSize);
253 void emitIntOffset(uint64_t Offset, dwarf::DwarfFormat Format,
254 uint64_t &SectionSize);
255 void emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
256 dwarf::DwarfFormat Format, uint64_t &SectionSize);
257 /// @}
258
259 /// \defgroup MCObjects MC layer objects constructed by the streamer
260 /// @{
261 std::unique_ptr<MCRegisterInfo> MRI;
262 std::unique_ptr<MCAsmInfo> MAI;
263 std::unique_ptr<MCObjectFileInfo> MOFI;
264 std::unique_ptr<MCContext> MC;
265 MCAsmBackend *MAB; // Owned by MCStreamer
266 std::unique_ptr<MCInstrInfo> MII;
267 std::unique_ptr<MCSubtargetInfo> MSTI;
268 MCInstPrinter *MIP; // Owned by AsmPrinter
269 MCCodeEmitter *MCE; // Owned by MCStreamer
270 MCStreamer *MS; // Owned by AsmPrinter
271 std::unique_ptr<TargetMachine> TM;
272 std::unique_ptr<AsmPrinter> Asm;
273 /// @}
274
275 /// The output file we stream the linked Dwarf to.
276 raw_pwrite_stream &OutFile;
278 std::function<StringRef(StringRef Input)> Translator;
279
280 uint64_t RangesSectionSize = 0;
281 uint64_t RngListsSectionSize = 0;
282 uint64_t LocSectionSize = 0;
283 uint64_t LocListsSectionSize = 0;
284 uint64_t LineSectionSize = 0;
285 uint64_t FrameSectionSize = 0;
286 uint64_t DebugInfoSectionSize = 0;
287 uint64_t MacInfoSectionSize = 0;
288 uint64_t MacroSectionSize = 0;
289
290 /// Keep track of emitted CUs and their Unique ID.
291 struct EmittedUnit {
292 unsigned ID;
293 MCSymbol *LabelBegin;
294 };
295 std::vector<EmittedUnit> EmittedUnits;
296
297 /// Emit the pubnames or pubtypes section contribution for \p
298 /// Unit into \p Sec. The data is provided in \p Names.
299 void emitPubSectionForUnit(MCSection *Sec, StringRef Name,
300 const CompileUnit &Unit,
301 const std::vector<CompileUnit::AccelInfo> &Names);
302
303 messageHandler ErrorHandler = nullptr;
304 messageHandler WarningHandler = nullptr;
305};
306
307} // end namespace llvm
308
309#endif // LLVM_DWARFLINKER_DWARFSTREAMER_H
std::string Name
uint64_t Size
#define P(N)
#define error(X)
@ Names
Definition: TextStubV5.cpp:106
This class holds an abstract representation of an Accelerator Table, consisting of a sequence of buck...
Definition: AccelTable.h:195
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:84
Stores all information relating to a compile unit, be it in its original instance in the object file ...
A structured debug information entry.
Definition: DIE.h:746
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:46
DwarfEmitter presents interface to generate all debug info tables.
Definition: DWARFLinker.h:90
The Dwarf streaming logic.
Definition: DWARFStreamer.h:46
void emitMacroTables(DWARFContext *Context, const Offset2UnitMap &UnitMacroMap, OffsetsStringPool &StringPool) override
Emit all available macro tables(DWARFv4 and DWARFv5).
uint64_t getDebugMacroSectionSize() const override
Returns size of generated .debug_macro section.
uint64_t getFrameSectionSize() const override
Returns size of generated .debug_frame section.
void emitAppleNamespaces(AccelTable< AppleAccelTableStaticOffsetData > &Table) override
Emit Apple namespaces accelerator table.
void emitStrings(const NonRelocatableStringpool &Pool) override
Emit the string table described by Pool into .debug_str table.
void emitAppleNames(AccelTable< AppleAccelTableStaticOffsetData > &Table) override
Emit Apple names accelerator table.
void emitDwarfDebugArangesTable(const CompileUnit &Unit, const AddressRanges &LinkedRanges) override
Emit .debug_aranges entries for Unit.
void emitPaperTrailWarningsDie(DIE &Die) override
Emit DIE containing warnings.
uint64_t getDebugInfoSectionSize() const override
Returns size of generated .debug_info section.
uint64_t getLocListsSectionSize() const override
Returns size of generated .debug_loclists section.
void emitDwarfDebugRangeListFragment(const CompileUnit &Unit, const AddressRanges &LinkedRanges, PatchLocation Patch) override
Emit debug ranges(.debug_ranges, .debug_rnglists) fragment.
void emitAppleObjc(AccelTable< AppleAccelTableStaticOffsetData > &Table) override
Emit Apple Objective-C accelerator table.
void emitDwarfDebugRangeListFooter(const CompileUnit &Unit, MCSymbol *EndLabel) override
Emit debug ranges(.debug_ranges, .debug_rnglists) footer.
void emitAppleTypes(AccelTable< AppleAccelTableStaticTypeData > &Table) override
Emit Apple type accelerator table.
void emitSectionContents(StringRef SecData, StringRef SecName) override
Emit contents of section SecName From Obj.
void finish()
Dump the file to the disk.
void emitPubNamesForUnit(const CompileUnit &Unit) override
Emit the .debug_pubnames contribution for Unit.
void emitDwarfDebugLocListFragment(const CompileUnit &Unit, const DWARFLocationExpressionsVector &LinkedLocationExpression, PatchLocation Patch) override
Emit debug ranges(.debug_loc, .debug_loclists) fragment.
void emitCIE(StringRef CIEBytes) override
Emit a CIE.
DwarfStreamer(OutputFileType OutFileType, raw_pwrite_stream &OutFile, std::function< StringRef(StringRef Input)> Translator, messageHandler Error, messageHandler Warning)
Definition: DWARFStreamer.h:48
void emitDIE(DIE &Die) override
Recursively emit the DIE tree rooted at Die.
AsmPrinter & getAsmPrinter() const
Definition: DWARFStreamer.h:59
void emitDwarfDebugLocListFooter(const CompileUnit &Unit, MCSymbol *EndLabel) override
Emit debug ranges(.debug_loc, .debug_loclists) footer.
uint64_t getLineSectionSize() const override
Returns size of generated .debug_line section.
void emitLineStrings(const NonRelocatableStringpool &Pool) override
Emit the string table described by Pool into .debug_line_str table.
void emitCompileUnitHeader(CompileUnit &Unit, unsigned DwarfVersion) override
Emit the compilation unit header for Unit in the debug_info section.
MCSymbol * emitDwarfDebugLocListHeader(const CompileUnit &Unit) override
Emit debug locations(.debug_loc, .debug_loclists) header.
uint64_t getDebugMacInfoSectionSize() const override
Returns size of generated .debug_macinfo section.
void emitPubTypesForUnit(const CompileUnit &Unit) override
Emit the .debug_pubtypes contribution for Unit.
void emitLineTableForUnit(const DWARFDebugLine::LineTable &LineTable, const CompileUnit &Unit, OffsetsStringPool &DebugStrPool, OffsetsStringPool &DebugLineStrPool) override
Emit .debug_line table entry for specified LineTable.
uint64_t getRngListsSectionSize() const override
Returns size of generated .debug_rnglists section.
void emitAbbrevs(const std::vector< std::unique_ptr< DIEAbbrev > > &Abbrevs, unsigned DwarfVersion) override
Emit the abbreviation table Abbrevs to the debug_abbrev section.
void emitSwiftReflectionSection(llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind, StringRef Buffer, uint32_t Alignment, uint32_t Size)
Emit the swift reflection section stored in Buffer.
MCSymbol * emitDwarfDebugRangeListHeader(const CompileUnit &Unit) override
Emit debug ranges(.debug_ranges, .debug_rnglists) header.
uint64_t getRangesSectionSize() const override
Returns size of generated .debug_ranges section.
void emitSwiftAST(StringRef Buffer)
Emit the swift_ast section stored in Buffer.
bool init(Triple TheTriple, StringRef Swift5ReflectionSegmentName)
void switchToDebugInfoSection(unsigned DwarfVersion)
Set the current output section to debug_info and change the MC Dwarf version to DwarfVersion.
void emitDebugNames(AccelTable< DWARF5AccelTableStaticData > &Table) override
Emit DWARF debug names.
void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint64_t Address, StringRef Bytes) override
Emit an FDE with data Bytes.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
A string table that doesn't need relocations.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Helper for making strong types.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:428
Swift5ReflectionSectionKind
Definition: Swift.h:14
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:91
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:406
OutputFileType
Definition: DWARFStreamer.h:26
std::function< void(const Twine &Warning, StringRef Context, const DWARFDie *DIE)> messageHandler
Definition: DWARFLinker.h:252
std::vector< DWARFLocationExpression > DWARFLocationExpressionsVector
Represents a set of absolute location expressions.
StrongType< NonRelocatableStringpool, OffsetsTag > OffsetsStringPool
DenseMap< uint64_t, CompileUnit * > Offset2UnitMap
Definition: DWARFLinker.h:87