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(const CompileUnit
&Unit
, DWARFContext
&Dwarf
);
100 /// Emit the line table described in \p Rows into the debug_line section.
101 void emitLineTableForUnit(MCDwarfLineTableParams Params
,
102 StringRef PrologueBytes
, unsigned MinInstLength
,
103 std::vector
<DWARFDebugLine::Row
> &Rows
,
104 unsigned AdddressSize
);
106 /// Copy the debug_line over to the updated binary while unobfuscating the
107 /// file names and directories.
108 void translateLineTable(DataExtractor LineData
, uint32_t Offset
,
109 LinkOptions
&Options
);
111 /// Copy over the debug sections that are not modified when updating.
112 void copyInvariantDebugSection(const object::ObjectFile
&Obj
);
114 uint32_t getLineSectionSize() const { return LineSectionSize
; }
116 /// Emit the .debug_pubnames contribution for \p Unit.
117 void emitPubNamesForUnit(const CompileUnit
&Unit
);
119 /// Emit the .debug_pubtypes contribution for \p Unit.
120 void emitPubTypesForUnit(const CompileUnit
&Unit
);
123 void emitCIE(StringRef CIEBytes
);
125 /// Emit an FDE with data \p Bytes.
126 void emitFDE(uint32_t CIEOffset
, uint32_t AddreSize
, uint32_t Address
,
129 /// Emit DWARF debug names.
130 void emitDebugNames(AccelTable
<DWARF5AccelTableStaticData
> &Table
);
132 /// Emit Apple namespaces accelerator table.
133 void emitAppleNamespaces(AccelTable
<AppleAccelTableStaticOffsetData
> &Table
);
135 /// Emit Apple names accelerator table.
136 void emitAppleNames(AccelTable
<AppleAccelTableStaticOffsetData
> &Table
);
138 /// Emit Apple Objective-C accelerator table.
139 void emitAppleObjc(AccelTable
<AppleAccelTableStaticOffsetData
> &Table
);
141 /// Emit Apple type accelerator table.
142 void emitAppleTypes(AccelTable
<AppleAccelTableStaticTypeData
> &Table
);
144 uint32_t getFrameSectionSize() const { return FrameSectionSize
; }
147 /// \defgroup MCObjects MC layer objects constructed by the streamer
149 std::unique_ptr
<MCRegisterInfo
> MRI
;
150 std::unique_ptr
<MCAsmInfo
> MAI
;
151 std::unique_ptr
<MCObjectFileInfo
> MOFI
;
152 std::unique_ptr
<MCContext
> MC
;
153 MCAsmBackend
*MAB
; // Owned by MCStreamer
154 std::unique_ptr
<MCInstrInfo
> MII
;
155 std::unique_ptr
<MCSubtargetInfo
> MSTI
;
156 MCInstPrinter
*MIP
; // Owned by AsmPrinter
157 MCCodeEmitter
*MCE
; // Owned by MCStreamer
158 MCStreamer
*MS
; // Owned by AsmPrinter
159 std::unique_ptr
<TargetMachine
> TM
;
160 std::unique_ptr
<AsmPrinter
> Asm
;
163 /// The file we stream the linked Dwarf to.
164 raw_fd_ostream
&OutFile
;
168 uint32_t RangesSectionSize
;
169 uint32_t LocSectionSize
;
170 uint32_t LineSectionSize
;
171 uint32_t FrameSectionSize
;
173 /// Keep track of emitted CUs and their Unique ID.
176 MCSymbol
*LabelBegin
;
178 std::vector
<EmittedUnit
> EmittedUnits
;
180 /// Emit the pubnames or pubtypes section contribution for \p
181 /// Unit into \p Sec. The data is provided in \p Names.
182 void emitPubSectionForUnit(MCSection
*Sec
, StringRef Name
,
183 const CompileUnit
&Unit
,
184 const std::vector
<CompileUnit::AccelInfo
> &Names
);
187 } // end namespace dsymutil
188 } // end namespace llvm
190 #endif // LLVM_TOOLS_DSYMUTIL_DWARFSTREAMER_H