LLVM  14.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"
19 #include "llvm/MC/MCRegisterInfo.h"
21 
22 namespace llvm {
23 
24 enum class OutputFileType {
25  Object,
26  Assembly,
27 };
28 
29 /// User of DwarfStreamer should call initialization code
30 /// for AsmPrinter:
31 ///
32 /// InitializeAllTargetInfos();
33 /// InitializeAllTargetMCs();
34 /// InitializeAllTargets();
35 /// InitializeAllAsmPrinters();
36 
37 class MCCodeEmitter;
38 
39 /// The Dwarf streaming logic.
40 ///
41 /// All interactions with the MC layer that is used to build the debug
42 /// information binary representation are handled in this class.
43 class DwarfStreamer : public DwarfEmitter {
44 public:
46  std::function<StringRef(StringRef Input)> Translator,
48  : OutFile(OutFile), OutFileType(OutFileType), Translator(Translator),
49  ErrorHandler(Error), WarningHandler(Warning) {}
50 
51  bool init(Triple TheTriple);
52 
53  /// Dump the file to the disk.
54  void finish();
55 
56  AsmPrinter &getAsmPrinter() const { return *Asm; }
57 
58  /// Set the current output section to debug_info and change
59  /// the MC Dwarf version to \p DwarfVersion.
60  void switchToDebugInfoSection(unsigned DwarfVersion);
61 
62  /// Emit the compilation unit header for \p Unit in the
63  /// debug_info section.
64  ///
65  /// As a side effect, this also switches the current Dwarf version
66  /// of the MC layer to the one of U.getOrigUnit().
67  void emitCompileUnitHeader(CompileUnit &Unit, unsigned DwarfVersion) override;
68 
69  /// Recursively emit the DIE tree rooted at \p Die.
70  void emitDIE(DIE &Die) override;
71 
72  /// Emit the abbreviation table \p Abbrevs to the debug_abbrev section.
73  void emitAbbrevs(const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
74  unsigned DwarfVersion) override;
75 
76  /// Emit DIE containing warnings.
77  void emitPaperTrailWarningsDie(DIE &Die) override;
78 
79  /// Emit contents of section SecName From Obj.
80  void emitSectionContents(StringRef SecData, StringRef SecName) override;
81 
82  /// Emit the string table described by \p Pool.
83  void emitStrings(const NonRelocatableStringpool &Pool) override;
84 
85  /// Emit the swift_ast section stored in \p Buffer.
86  void emitSwiftAST(StringRef Buffer);
87 
88  /// Emit debug_ranges for \p FuncRange by translating the
89  /// original \p Entries.
90  void emitRangesEntries(
91  int64_t UnitPcOffset, uint64_t OrigLowPc,
92  const FunctionIntervals::const_iterator &FuncRange,
93  const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
94  unsigned AddressSize) override;
95 
96  /// Emit debug_aranges entries for \p Unit and if \p DoRangesSection is true,
97  /// also emit the debug_ranges entries for the DW_TAG_compile_unit's
98  /// DW_AT_ranges attribute.
99  void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection) override;
100 
101  uint64_t getRangesSectionSize() const override { return RangesSectionSize; }
102 
103  /// Emit the debug_loc contribution for \p Unit by copying the entries from
104  /// \p Dwarf and offsetting them. Update the location attributes to point to
105  /// the new entries.
107  const CompileUnit &Unit, DWARFContext &Dwarf,
108  std::function<void(StringRef, SmallVectorImpl<uint8_t> &)> ProcessExpr)
109  override;
110 
111  /// Emit the line table described in \p Rows into the debug_line section.
113  StringRef PrologueBytes, unsigned MinInstLength,
114  std::vector<DWARFDebugLine::Row> &Rows,
115  unsigned AdddressSize) override;
116 
117  /// Copy the debug_line over to the updated binary while unobfuscating the
118  /// file names and directories.
119  void translateLineTable(DataExtractor LineData, uint64_t Offset) override;
120 
121  uint64_t getLineSectionSize() const override { return LineSectionSize; }
122 
123  /// Emit the .debug_pubnames contribution for \p Unit.
124  void emitPubNamesForUnit(const CompileUnit &Unit) override;
125 
126  /// Emit the .debug_pubtypes contribution for \p Unit.
127  void emitPubTypesForUnit(const CompileUnit &Unit) override;
128 
129  /// Emit a CIE.
130  void emitCIE(StringRef CIEBytes) override;
131 
132  /// Emit an FDE with data \p Bytes.
133  void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address,
134  StringRef Bytes) override;
135 
136  /// Emit DWARF debug names.
138 
139  /// Emit Apple namespaces accelerator table.
140  void emitAppleNamespaces(
142 
143  /// Emit Apple names accelerator table.
144  void
146 
147  /// Emit Apple Objective-C accelerator table.
148  void
150 
151  /// Emit Apple type accelerator table.
152  void
154 
155  uint64_t getFrameSectionSize() const override { return FrameSectionSize; }
156 
158  return DebugInfoSectionSize;
159  }
160 
161 private:
162  inline void error(const Twine &Error, StringRef Context = "") {
163  if (ErrorHandler)
164  ErrorHandler(Error, Context, nullptr);
165  }
166 
167  inline void warn(const Twine &Warning, StringRef Context = "") {
168  if (WarningHandler)
169  WarningHandler(Warning, Context, nullptr);
170  }
171 
172  /// \defgroup MCObjects MC layer objects constructed by the streamer
173  /// @{
174  std::unique_ptr<MCRegisterInfo> MRI;
175  std::unique_ptr<MCAsmInfo> MAI;
176  std::unique_ptr<MCObjectFileInfo> MOFI;
177  std::unique_ptr<MCContext> MC;
178  MCAsmBackend *MAB; // Owned by MCStreamer
179  std::unique_ptr<MCInstrInfo> MII;
180  std::unique_ptr<MCSubtargetInfo> MSTI;
181  MCInstPrinter *MIP; // Owned by AsmPrinter
182  MCCodeEmitter *MCE; // Owned by MCStreamer
183  MCStreamer *MS; // Owned by AsmPrinter
184  std::unique_ptr<TargetMachine> TM;
185  std::unique_ptr<AsmPrinter> Asm;
186  /// @}
187 
188  /// The output file we stream the linked Dwarf to.
189  raw_pwrite_stream &OutFile;
191  std::function<StringRef(StringRef Input)> Translator;
192 
193  uint64_t RangesSectionSize = 0;
194  uint64_t LocSectionSize = 0;
195  uint64_t LineSectionSize = 0;
196  uint64_t FrameSectionSize = 0;
197  uint64_t DebugInfoSectionSize = 0;
198 
199  /// Keep track of emitted CUs and their Unique ID.
200  struct EmittedUnit {
201  unsigned ID;
202  MCSymbol *LabelBegin;
203  };
204  std::vector<EmittedUnit> EmittedUnits;
205 
206  /// Emit the pubnames or pubtypes section contribution for \p
207  /// Unit into \p Sec. The data is provided in \p Names.
208  void emitPubSectionForUnit(MCSection *Sec, StringRef Name,
209  const CompileUnit &Unit,
210  const std::vector<CompileUnit::AccelInfo> &Names);
211 
212  messageHandler ErrorHandler = nullptr;
213  messageHandler WarningHandler = nullptr;
214 };
215 
216 } // end namespace llvm
217 
218 #endif // LLVM_DWARFLINKER_DWARFSTREAMER_H
AsmPrinter.h
llvm::DwarfStreamer::emitAppleTypes
void emitAppleTypes(AccelTable< AppleAccelTableStaticTypeData > &Table) override
Emit Apple type accelerator table.
Definition: DWARFStreamer.cpp:289
llvm::DwarfStreamer::getRangesSectionSize
uint64_t getRangesSectionSize() const override
Returns size of generated .debug_ranges section.
Definition: DWARFStreamer.h:101
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:22
llvm::OutputFileType
OutputFileType
Definition: DWARFStreamer.h:24
llvm::DwarfStreamer::emitPaperTrailWarningsDie
void emitPaperTrailWarningsDie(DIE &Die) override
Emit DIE containing warnings.
Definition: DWARFStreamer.cpp:209
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
MCObjectFileInfo.h
llvm::DwarfStreamer::emitRangesEntries
void emitRangesEntries(int64_t UnitPcOffset, uint64_t OrigLowPc, const FunctionIntervals::const_iterator &FuncRange, const std::vector< DWARFDebugRangeList::RangeListEntry > &Entries, unsigned AddressSize) override
Emit debug_ranges for FuncRange by translating the original Entries.
Definition: DWARFStreamer.cpp:309
llvm::DWARFContext
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:53
llvm::DwarfStreamer::emitPubNamesForUnit
void emitPubNamesForUnit(const CompileUnit &Unit) override
Emit the .debug_pubnames contribution for Unit.
Definition: DWARFStreamer.cpp:761
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:80
llvm::IntervalMap::const_iterator
Definition: IntervalMap.h:1297
llvm::DIE
A structured debug information entry.
Definition: DIE.h:739
llvm::AccelTable
This class holds an abstract representation of an Accelerator Table, consisting of a sequence of buck...
Definition: AccelTable.h:197
llvm::HighlightColor::Warning
@ Warning
Context
ManagedStatic< detail::RecordContext > Context
Definition: Record.cpp:96
llvm::DwarfStreamer::switchToDebugInfoSection
void switchToDebugInfoSection(unsigned DwarfVersion)
Set the current output section to debug_info and change the MC Dwarf version to DwarfVersion.
Definition: DWARFStreamer.cpp:117
llvm::DwarfStreamer::emitPubTypesForUnit
void emitPubTypesForUnit(const CompileUnit &Unit) override
Emit the .debug_pubtypes contribution for Unit.
Definition: DWARFStreamer.cpp:767
TargetMachine.h
llvm::DwarfStreamer::emitAppleNamespaces
void emitAppleNamespaces(AccelTable< AppleAccelTableStaticOffsetData > &Table) override
Emit Apple namespaces accelerator table.
Definition: DWARFStreamer.cpp:265
llvm::MCDwarfLineTableParams
Definition: MCDwarf.h:237
llvm::DwarfStreamer::translateLineTable
void translateLineTable(DataExtractor LineData, uint64_t Offset) override
Copy the debug_line over to the updated binary while unobfuscating the file names and directories.
Definition: DWARFStreamer.cpp:638
llvm::DwarfStreamer::emitCIE
void emitCIE(StringRef CIEBytes) override
Emit a CIE.
Definition: DWARFStreamer.cpp:773
MCContext.h
llvm::DwarfStreamer::emitSectionContents
void emitSectionContents(StringRef SecData, StringRef SecName) override
Emit contents of section SecName From Obj.
Definition: DWARFStreamer.cpp:189
MCInstrInfo.h
llvm::DwarfStreamer::emitUnitRangesEntries
void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection) override
Emit debug_aranges entries for Unit and if DoRangesSection is true, also emit the debug_ranges entrie...
Definition: DWARFStreamer.cpp:348
AccelTable.h
llvm::DwarfStreamer::emitCompileUnitHeader
void emitCompileUnitHeader(CompileUnit &Unit, unsigned DwarfVersion) override
Emit the compilation unit header for Unit in the debug_info section.
Definition: DWARFStreamer.cpp:138
llvm::DwarfStreamer::emitLineTableForUnit
void emitLineTableForUnit(MCDwarfLineTableParams Params, StringRef PrologueBytes, unsigned MinInstLength, std::vector< DWARFDebugLine::Row > &Rows, unsigned AdddressSize) override
Emit the line table described in Rows into the debug_line section.
Definition: DWARFStreamer.cpp:492
llvm::DwarfStreamer::emitFDE
void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address, StringRef Bytes) override
Emit an FDE with data Bytes.
Definition: DWARFStreamer.cpp:783
llvm::DwarfStreamer::getLineSectionSize
uint64_t getLineSectionSize() const override
Returns size of generated .debug_line section.
Definition: DWARFStreamer.h:121
llvm::OutputFileType::Object
@ Object
DWARFLinker.h
llvm::DwarfStreamer::emitDebugNames
void emitDebugNames(AccelTable< DWARF5AccelTableStaticData > &Table) override
Emit DWARF debug names.
Definition: DWARFStreamer.cpp:242
llvm::DwarfEmitter
DwarfEmitter presents interface to generate all debug info tables.
Definition: DWARFLinker.h:102
llvm::DwarfStreamer::emitStrings
void emitStrings(const NonRelocatableStringpool &Pool) override
Emit the string table described by Pool.
Definition: DWARFStreamer.cpp:221
uint64_t
llvm::raw_pwrite_stream
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:418
llvm::DwarfStreamer::getDebugInfoSectionSize
uint64_t getDebugInfoSectionSize() const override
Returns size of generated .debug_info section.
Definition: DWARFStreamer.h:157
llvm::CompileUnit
Stores all information relating to a compile unit, be it in its original instance in the object file ...
Definition: DWARFLinkerCompileUnit.h:50
llvm::DwarfStreamer::emitAppleObjc
void emitAppleObjc(AccelTable< AppleAccelTableStaticOffsetData > &Table) override
Emit Apple Objective-C accelerator table.
Definition: DWARFStreamer.cpp:281
MCRegisterInfo.h
llvm::DwarfStreamer::emitAppleNames
void emitAppleNames(AccelTable< AppleAccelTableStaticOffsetData > &Table) override
Emit Apple names accelerator table.
Definition: DWARFStreamer.cpp:273
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::DwarfStreamer::emitSwiftAST
void emitSwiftAST(StringRef Buffer)
Emit the swift_ast section stored in Buffer.
Definition: DWARFStreamer.cpp:298
llvm::DwarfStreamer::DwarfStreamer
DwarfStreamer(OutputFileType OutFileType, raw_pwrite_stream &OutFile, std::function< StringRef(StringRef Input)> Translator, messageHandler Error, messageHandler Warning)
Definition: DWARFStreamer.h:45
MCAsmInfo.h
llvm::DwarfStreamer::getAsmPrinter
AsmPrinter & getAsmPrinter() const
Definition: DWARFStreamer.h:56
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
llvm::DwarfStreamer::emitDIE
void emitDIE(DIE &Die) override
Recursively emit the DIE tree rooted at Die.
Definition: DWARFStreamer.cpp:182
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::DwarfStreamer
The Dwarf streaming logic.
Definition: DWARFStreamer.h:43
llvm::NonRelocatableStringpool
A string table that doesn't need relocations.
Definition: NonRelocatableStringpool.h:23
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::DwarfStreamer::getFrameSectionSize
uint64_t getFrameSectionSize() const override
Returns size of generated .debug_frame section.
Definition: DWARFStreamer.h:155
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:82
llvm::DwarfStreamer::init
bool init(Triple TheTriple)
Definition: DWARFStreamer.cpp:30
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::DwarfStreamer::emitAbbrevs
void emitAbbrevs(const std::vector< std::unique_ptr< DIEAbbrev >> &Abbrevs, unsigned DwarfVersion) override
Emit the abbreviation table Abbrevs to the debug_abbrev section.
Definition: DWARFStreamer.cpp:173
llvm::SmallVectorImpl< uint8_t >
llvm::DwarfStreamer::emitLocationsForUnit
void emitLocationsForUnit(const CompileUnit &Unit, DWARFContext &Dwarf, std::function< void(StringRef, SmallVectorImpl< uint8_t > &)> ProcessExpr) override
Emit the debug_loc contribution for Unit by copying the entries from Dwarf and offsetting them.
Definition: DWARFStreamer.cpp:428
llvm::messageHandler
std::function< void(const Twine &Warning, StringRef Context, const DWARFDie *DIE)> messageHandler
Definition: DWARFLinker.h:233
llvm::OutputFileType::Assembly
@ Assembly
llvm::ISD::MCSymbol
@ MCSymbol
Definition: ISDOpcodes.h:172
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38
llvm::DwarfStreamer::finish
void finish()
Dump the file to the disk.
Definition: DWARFStreamer.cpp:115