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
12 #include "CompileUnit.h"
14 #include "LinkUtils.h"
15 #include "NonRelocatableStringpool.h"
16 #include "llvm/CodeGen/AccelTable.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
20 #include "llvm/MC/MCAsmBackend.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCCodeEmitter.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCDwarf.h"
25 #include "llvm/MC/MCInstrInfo.h"
26 #include "llvm/MC/MCObjectFileInfo.h"
27 #include "llvm/MC/MCObjectWriter.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCSection.h"
30 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/MC/MCSubtargetInfo.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/MC/MCTargetOptions.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/Target/TargetOptions.h"
40 /// The Dwarf streaming logic.
42 /// All interactions with the MC layer that is used to build the debug
43 /// information binary representation are handled in this class.
46 DwarfStreamer(raw_fd_ostream
&OutFile
, LinkOptions Options
)
47 : OutFile(OutFile
), Options(std::move(Options
)) {}
49 bool init(Triple TheTriple
);
51 /// Dump the file to the disk.
52 bool finish(const DebugMap
&, SymbolMapTranslator
&T
);
54 AsmPrinter
&getAsmPrinter() const { return *Asm
; }
56 /// Set the current output section to debug_info and change
57 /// the MC Dwarf version to \p DwarfVersion.
58 void switchToDebugInfoSection(unsigned DwarfVersion
);
60 /// Emit the compilation unit header for \p Unit in the
61 /// debug_info section.
63 /// As a side effect, this also switches the current Dwarf version
64 /// of the MC layer to the one of U.getOrigUnit().
65 void emitCompileUnitHeader(CompileUnit
&Unit
);
67 /// Recursively emit the DIE tree rooted at \p Die.
68 void emitDIE(DIE
&Die
);
70 /// Emit the abbreviation table \p Abbrevs to the debug_abbrev section.
71 void emitAbbrevs(const std::vector
<std::unique_ptr
<DIEAbbrev
>> &Abbrevs
,
72 unsigned DwarfVersion
);
74 /// Emit the string table described by \p Pool.
75 void emitStrings(const NonRelocatableStringpool
&Pool
);
77 /// Emit the swift_ast section stored in \p Buffer.
78 void emitSwiftAST(StringRef Buffer
);
80 /// Emit debug_ranges for \p FuncRange by translating the
81 /// original \p Entries.
82 void emitRangesEntries(
83 int64_t UnitPcOffset
, uint64_t OrigLowPc
,
84 const FunctionIntervals::const_iterator
&FuncRange
,
85 const std::vector
<DWARFDebugRangeList::RangeListEntry
> &Entries
,
86 unsigned AddressSize
);
88 /// Emit debug_aranges entries for \p Unit and if \p DoRangesSection is true,
89 /// also emit the debug_ranges entries for the DW_TAG_compile_unit's
90 /// DW_AT_ranges attribute.
91 void emitUnitRangesEntries(CompileUnit
&Unit
, bool DoRangesSection
);
93 uint32_t getRangesSectionSize() const { return RangesSectionSize
; }
95 /// Emit the debug_loc contribution for \p Unit by copying the entries from
96 /// \p Dwarf and offsetting them. Update the location attributes to point to
98 void emitLocationsForUnit(
99 const CompileUnit
&Unit
, DWARFContext
&Dwarf
,
100 std::function
<void(StringRef
, SmallVectorImpl
<uint8_t> &)> ProcessExpr
);
102 /// Emit the line table described in \p Rows into the debug_line section.
103 void emitLineTableForUnit(MCDwarfLineTableParams Params
,
104 StringRef PrologueBytes
, unsigned MinInstLength
,
105 std::vector
<DWARFDebugLine::Row
> &Rows
,
106 unsigned AdddressSize
);
108 /// Copy the debug_line over to the updated binary while unobfuscating the
109 /// file names and directories.
110 void translateLineTable(DataExtractor LineData
, uint64_t Offset
);
112 /// Copy over the debug sections that are not modified when updating.
113 void copyInvariantDebugSection(const object::ObjectFile
&Obj
);
115 uint64_t getLineSectionSize() const { return LineSectionSize
; }
117 /// Emit the .debug_pubnames contribution for \p Unit.
118 void emitPubNamesForUnit(const CompileUnit
&Unit
);
120 /// Emit the .debug_pubtypes contribution for \p Unit.
121 void emitPubTypesForUnit(const CompileUnit
&Unit
);
124 void emitCIE(StringRef CIEBytes
);
126 /// Emit an FDE with data \p Bytes.
127 void emitFDE(uint32_t CIEOffset
, uint32_t AddreSize
, uint32_t Address
,
130 /// Emit DWARF debug names.
131 void emitDebugNames(AccelTable
<DWARF5AccelTableStaticData
> &Table
);
133 /// Emit Apple namespaces accelerator table.
134 void emitAppleNamespaces(AccelTable
<AppleAccelTableStaticOffsetData
> &Table
);
136 /// Emit Apple names accelerator table.
137 void emitAppleNames(AccelTable
<AppleAccelTableStaticOffsetData
> &Table
);
139 /// Emit Apple Objective-C accelerator table.
140 void emitAppleObjc(AccelTable
<AppleAccelTableStaticOffsetData
> &Table
);
142 /// Emit Apple type accelerator table.
143 void emitAppleTypes(AccelTable
<AppleAccelTableStaticTypeData
> &Table
);
145 uint32_t getFrameSectionSize() const { return FrameSectionSize
; }
148 /// \defgroup MCObjects MC layer objects constructed by the streamer
150 std::unique_ptr
<MCRegisterInfo
> MRI
;
151 std::unique_ptr
<MCAsmInfo
> MAI
;
152 std::unique_ptr
<MCObjectFileInfo
> MOFI
;
153 std::unique_ptr
<MCContext
> MC
;
154 MCAsmBackend
*MAB
; // Owned by MCStreamer
155 std::unique_ptr
<MCInstrInfo
> MII
;
156 std::unique_ptr
<MCSubtargetInfo
> MSTI
;
157 MCInstPrinter
*MIP
; // Owned by AsmPrinter
158 MCCodeEmitter
*MCE
; // Owned by MCStreamer
159 MCStreamer
*MS
; // Owned by AsmPrinter
160 std::unique_ptr
<TargetMachine
> TM
;
161 std::unique_ptr
<AsmPrinter
> Asm
;
164 /// The file we stream the linked Dwarf to.
165 raw_fd_ostream
&OutFile
;
169 uint32_t RangesSectionSize
;
170 uint32_t LocSectionSize
;
171 uint64_t LineSectionSize
;
172 uint32_t FrameSectionSize
;
174 /// Keep track of emitted CUs and their Unique ID.
177 MCSymbol
*LabelBegin
;
179 std::vector
<EmittedUnit
> EmittedUnits
;
181 /// Emit the pubnames or pubtypes section contribution for \p
182 /// Unit into \p Sec. The data is provided in \p Names.
183 void emitPubSectionForUnit(MCSection
*Sec
, StringRef Name
,
184 const CompileUnit
&Unit
,
185 const std::vector
<CompileUnit::AccelInfo
> &Names
);
188 } // end namespace dsymutil
189 } // end namespace llvm
191 #endif // LLVM_TOOLS_DSYMUTIL_DWARFSTREAMER_H