1 //===- tools/dsymutil/DwarfStreamer.h - Dwarf Streamer ----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TOOLS_DSYMUTIL_DWARFSTREAMER_H
10 #define LLVM_TOOLS_DSYMUTIL_DWARFSTREAMER_H
13 #include "LinkUtils.h"
14 #include "llvm/CodeGen/AccelTable.h"
15 #include "llvm/CodeGen/AsmPrinter.h"
16 #include "llvm/CodeGen/NonRelocatableStringpool.h"
17 #include "llvm/DWARFLinker/DWARFLinker.h"
18 #include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
21 #include "llvm/MC/MCAsmBackend.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCCodeEmitter.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCDwarf.h"
26 #include "llvm/MC/MCInstrInfo.h"
27 #include "llvm/MC/MCObjectFileInfo.h"
28 #include "llvm/MC/MCObjectWriter.h"
29 #include "llvm/MC/MCRegisterInfo.h"
30 #include "llvm/MC/MCSection.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCSubtargetInfo.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/MC/MCTargetOptions.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Target/TargetOptions.h"
41 /// The Dwarf streaming logic.
43 /// All interactions with the MC layer that is used to build the debug
44 /// information binary representation are handled in this class.
45 class DwarfStreamer
: public DwarfEmitter
{
47 DwarfStreamer(raw_fd_ostream
&OutFile
, LinkOptions Options
)
48 : OutFile(OutFile
), Options(std::move(Options
)) {}
50 bool init(Triple TheTriple
);
52 /// Dump the file to the disk.
53 bool finish(const DebugMap
&, SymbolMapTranslator
&T
);
55 AsmPrinter
&getAsmPrinter() const { return *Asm
; }
57 /// Set the current output section to debug_info and change
58 /// the MC Dwarf version to \p DwarfVersion.
59 void switchToDebugInfoSection(unsigned DwarfVersion
);
61 /// Emit the compilation unit header for \p Unit in the
62 /// debug_info section.
64 /// As a side effect, this also switches the current Dwarf version
65 /// of the MC layer to the one of U.getOrigUnit().
66 void emitCompileUnitHeader(CompileUnit
&Unit
) override
;
68 /// Recursively emit the DIE tree rooted at \p Die.
69 void emitDIE(DIE
&Die
) override
;
71 /// Emit the abbreviation table \p Abbrevs to the debug_abbrev section.
72 void emitAbbrevs(const std::vector
<std::unique_ptr
<DIEAbbrev
>> &Abbrevs
,
73 unsigned DwarfVersion
) override
;
75 /// Emit DIE containing warnings.
76 void emitPaperTrailWarningsDie(const Triple
&Triple
, DIE
&Die
) override
;
78 /// Emit contents of section SecName From Obj.
79 void emitSectionContents(const object::ObjectFile
&Obj
,
80 StringRef SecName
) override
;
82 /// Emit the string table described by \p Pool.
83 void emitStrings(const NonRelocatableStringpool
&Pool
) override
;
85 /// Emit the swift_ast section stored in \p Buffer.
86 void emitSwiftAST(StringRef Buffer
);
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
;
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
;
101 uint64_t getRangesSectionSize() const override
{ return RangesSectionSize
; }
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
106 void emitLocationsForUnit(
107 const CompileUnit
&Unit
, DWARFContext
&Dwarf
,
108 std::function
<void(StringRef
, SmallVectorImpl
<uint8_t> &)> ProcessExpr
)
111 /// Emit the line table described in \p Rows into the debug_line section.
112 void emitLineTableForUnit(MCDwarfLineTableParams Params
,
113 StringRef PrologueBytes
, unsigned MinInstLength
,
114 std::vector
<DWARFDebugLine::Row
> &Rows
,
115 unsigned AdddressSize
) override
;
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
;
121 /// Copy over the debug sections that are not modified when updating.
122 void copyInvariantDebugSection(const object::ObjectFile
&Obj
);
124 uint64_t getLineSectionSize() const override
{ return LineSectionSize
; }
126 /// Emit the .debug_pubnames contribution for \p Unit.
127 void emitPubNamesForUnit(const CompileUnit
&Unit
) override
;
129 /// Emit the .debug_pubtypes contribution for \p Unit.
130 void emitPubTypesForUnit(const CompileUnit
&Unit
) override
;
133 void emitCIE(StringRef CIEBytes
) override
;
135 /// Emit an FDE with data \p Bytes.
136 void emitFDE(uint32_t CIEOffset
, uint32_t AddreSize
, uint32_t Address
,
137 StringRef Bytes
) override
;
139 /// Emit DWARF debug names.
140 void emitDebugNames(AccelTable
<DWARF5AccelTableStaticData
> &Table
) override
;
142 /// Emit Apple namespaces accelerator table.
143 void emitAppleNamespaces(
144 AccelTable
<AppleAccelTableStaticOffsetData
> &Table
) override
;
146 /// Emit Apple names accelerator table.
148 emitAppleNames(AccelTable
<AppleAccelTableStaticOffsetData
> &Table
) override
;
150 /// Emit Apple Objective-C accelerator table.
152 emitAppleObjc(AccelTable
<AppleAccelTableStaticOffsetData
> &Table
) override
;
154 /// Emit Apple type accelerator table.
156 emitAppleTypes(AccelTable
<AppleAccelTableStaticTypeData
> &Table
) override
;
158 uint64_t getFrameSectionSize() const override
{ return FrameSectionSize
; }
160 uint64_t getDebugInfoSectionSize() const override
{
161 return DebugInfoSectionSize
;
165 /// \defgroup MCObjects MC layer objects constructed by the streamer
167 std::unique_ptr
<MCRegisterInfo
> MRI
;
168 std::unique_ptr
<MCAsmInfo
> MAI
;
169 std::unique_ptr
<MCObjectFileInfo
> MOFI
;
170 std::unique_ptr
<MCContext
> MC
;
171 MCAsmBackend
*MAB
; // Owned by MCStreamer
172 std::unique_ptr
<MCInstrInfo
> MII
;
173 std::unique_ptr
<MCSubtargetInfo
> MSTI
;
174 MCInstPrinter
*MIP
; // Owned by AsmPrinter
175 MCCodeEmitter
*MCE
; // Owned by MCStreamer
176 MCStreamer
*MS
; // Owned by AsmPrinter
177 std::unique_ptr
<TargetMachine
> TM
;
178 std::unique_ptr
<AsmPrinter
> Asm
;
181 /// The file we stream the linked Dwarf to.
182 raw_fd_ostream
&OutFile
;
186 uint64_t RangesSectionSize
= 0;
187 uint64_t LocSectionSize
= 0;
188 uint64_t LineSectionSize
= 0;
189 uint64_t FrameSectionSize
= 0;
190 uint64_t DebugInfoSectionSize
= 0;
192 /// Keep track of emitted CUs and their Unique ID.
195 MCSymbol
*LabelBegin
;
197 std::vector
<EmittedUnit
> EmittedUnits
;
199 /// Emit the pubnames or pubtypes section contribution for \p
200 /// Unit into \p Sec. The data is provided in \p Names.
201 void emitPubSectionForUnit(MCSection
*Sec
, StringRef Name
,
202 const CompileUnit
&Unit
,
203 const std::vector
<CompileUnit::AccelInfo
> &Names
);
206 } // end namespace dsymutil
207 } // end namespace llvm
209 #endif // LLVM_TOOLS_DSYMUTIL_DWARFSTREAMER_H