• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- tools/dsymutil/DwarfStreamer.h - Dwarf Streamer ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "CompileUnit.h"
11 #include "DebugMap.h"
12 #include "LinkUtils.h"
13 #include "NonRelocatableStringpool.h"
14 #include "llvm/CodeGen/AccelTable.h"
15 #include "llvm/CodeGen/AsmPrinter.h"
16 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
17 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
18 #include "llvm/MC/MCAsmBackend.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCDwarf.h"
23 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MC/MCObjectFileInfo.h"
25 #include "llvm/MC/MCObjectWriter.h"
26 #include "llvm/MC/MCRegisterInfo.h"
27 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/MC/MCSubtargetInfo.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCTargetOptions.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Target/TargetOptions.h"
34 
35 #ifndef LLVM_TOOLS_DSYMUTIL_DWARFSTREAMER_H
36 #define LLVM_TOOLS_DSYMUTIL_DWARFSTREAMER_H
37 
38 namespace llvm {
39 namespace dsymutil {
40 
41 /// The Dwarf streaming logic.
42 ///
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 {
46 public:
DwarfStreamer(raw_fd_ostream & OutFile,LinkOptions Options)47   DwarfStreamer(raw_fd_ostream &OutFile, LinkOptions Options)
48       : OutFile(OutFile), Options(std::move(Options)) {}
49 
50   bool init(Triple TheTriple);
51 
52   /// Dump the file to the disk.
53   bool finish(const DebugMap &);
54 
getAsmPrinter()55   AsmPrinter &getAsmPrinter() const { return *Asm; }
56 
57   /// Set the current output section to debug_info and change
58   /// the MC Dwarf version to \p DwarfVersion.
59   void switchToDebugInfoSection(unsigned DwarfVersion);
60 
61   /// Emit the compilation unit header for \p Unit in the
62   /// debug_info section.
63   ///
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);
67 
68   /// Recursively emit the DIE tree rooted at \p Die.
69   void emitDIE(DIE &Die);
70 
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);
74 
75   /// Emit the string table described by \p Pool.
76   void emitStrings(const NonRelocatableStringpool &Pool);
77 
78   /// Emit the swift_ast section stored in \p Buffer.
79   void emitSwiftAST(StringRef Buffer);
80 
81   /// Emit debug_ranges for \p FuncRange by translating the
82   /// original \p Entries.
83   void emitRangesEntries(
84       int64_t UnitPcOffset, uint64_t OrigLowPc,
85       const FunctionIntervals::const_iterator &FuncRange,
86       const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
87       unsigned AddressSize);
88 
89   /// Emit debug_aranges entries for \p Unit and if \p DoRangesSection is true,
90   /// also emit the debug_ranges entries for the DW_TAG_compile_unit's
91   /// DW_AT_ranges attribute.
92   void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection);
93 
getRangesSectionSize()94   uint32_t getRangesSectionSize() const { return RangesSectionSize; }
95 
96   /// Emit the debug_loc contribution for \p Unit by copying the entries from
97   /// \p Dwarf and offsetting them. Update the location attributes to point to
98   /// the new entries.
99   void emitLocationsForUnit(const CompileUnit &Unit, DWARFContext &Dwarf);
100 
101   /// Emit the line table described in \p Rows into the debug_line section.
102   void emitLineTableForUnit(MCDwarfLineTableParams Params,
103                             StringRef PrologueBytes, unsigned MinInstLength,
104                             std::vector<DWARFDebugLine::Row> &Rows,
105                             unsigned AdddressSize);
106 
107   /// Copy over the debug sections that are not modified when updating.
108   void copyInvariantDebugSection(const object::ObjectFile &Obj);
109 
getLineSectionSize()110   uint32_t getLineSectionSize() const { return LineSectionSize; }
111 
112   /// Emit the .debug_pubnames contribution for \p Unit.
113   void emitPubNamesForUnit(const CompileUnit &Unit);
114 
115   /// Emit the .debug_pubtypes contribution for \p Unit.
116   void emitPubTypesForUnit(const CompileUnit &Unit);
117 
118   /// Emit a CIE.
119   void emitCIE(StringRef CIEBytes);
120 
121   /// Emit an FDE with data \p Bytes.
122   void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address,
123                StringRef Bytes);
124 
125   /// Emit DWARF debug names.
126   void emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table);
127 
128   /// Emit Apple namespaces accelerator table.
129   void emitAppleNamespaces(AccelTable<AppleAccelTableStaticOffsetData> &Table);
130 
131   /// Emit Apple names accelerator table.
132   void emitAppleNames(AccelTable<AppleAccelTableStaticOffsetData> &Table);
133 
134   /// Emit Apple Objective-C accelerator table.
135   void emitAppleObjc(AccelTable<AppleAccelTableStaticOffsetData> &Table);
136 
137   /// Emit Apple type accelerator table.
138   void emitAppleTypes(AccelTable<AppleAccelTableStaticTypeData> &Table);
139 
getFrameSectionSize()140   uint32_t getFrameSectionSize() const { return FrameSectionSize; }
141 
142 private:
143   /// \defgroup MCObjects MC layer objects constructed by the streamer
144   /// @{
145   std::unique_ptr<MCRegisterInfo> MRI;
146   std::unique_ptr<MCAsmInfo> MAI;
147   std::unique_ptr<MCObjectFileInfo> MOFI;
148   std::unique_ptr<MCContext> MC;
149   MCAsmBackend *MAB; // Owned by MCStreamer
150   std::unique_ptr<MCInstrInfo> MII;
151   std::unique_ptr<MCSubtargetInfo> MSTI;
152   MCInstPrinter *MIP; // Owned by AsmPrinter
153   MCCodeEmitter *MCE; // Owned by MCStreamer
154   MCStreamer *MS;     // Owned by AsmPrinter
155   std::unique_ptr<TargetMachine> TM;
156   std::unique_ptr<AsmPrinter> Asm;
157   /// @}
158 
159   /// The file we stream the linked Dwarf to.
160   raw_fd_ostream &OutFile;
161 
162   LinkOptions Options;
163 
164   uint32_t RangesSectionSize;
165   uint32_t LocSectionSize;
166   uint32_t LineSectionSize;
167   uint32_t FrameSectionSize;
168 
169   /// Keep track of emitted CUs and their Unique ID.
170   struct EmittedUnit {
171     unsigned ID;
172     MCSymbol *LabelBegin;
173   };
174   std::vector<EmittedUnit> EmittedUnits;
175 
176   /// Emit the pubnames or pubtypes section contribution for \p
177   /// Unit into \p Sec. The data is provided in \p Names.
178   void emitPubSectionForUnit(MCSection *Sec, StringRef Name,
179                              const CompileUnit &Unit,
180                              const std::vector<CompileUnit::AccelInfo> &Names);
181 };
182 
183 } // end namespace dsymutil
184 } // end namespace llvm
185 
186 #endif // LLVM_TOOLS_DSYMUTIL_DWARFSTREAMER_H
187