Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 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 | |
Argyrios Kyrtzidis | f4bf809 | 2018-09-03 16:22:05 +0000 | [diff] [blame] | 10 | #ifndef LLVM_TOOLS_DSYMUTIL_DWARFSTREAMER_H |
| 11 | #define LLVM_TOOLS_DSYMUTIL_DWARFSTREAMER_H |
| 12 | |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 13 | #include "CompileUnit.h" |
| 14 | #include "DebugMap.h" |
Jonas Devlieghere | a5d860c | 2018-07-09 16:58:48 +0000 | [diff] [blame] | 15 | #include "LinkUtils.h" |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 16 | #include "NonRelocatableStringpool.h" |
| 17 | #include "llvm/CodeGen/AccelTable.h" |
| 18 | #include "llvm/CodeGen/AsmPrinter.h" |
| 19 | #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" |
Wolfgang Pieb | daecf94 | 2018-10-31 01:12:58 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h" |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 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" |
Jonas Devlieghere | 7c767bd | 2018-07-25 23:01:38 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCSymbol.h" |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCTargetOptions.h" |
| 35 | #include "llvm/Target/TargetMachine.h" |
| 36 | #include "llvm/Target/TargetOptions.h" |
| 37 | |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 38 | namespace llvm { |
| 39 | namespace dsymutil { |
| 40 | |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 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: |
Jonas Devlieghere | a5d860c | 2018-07-09 16:58:48 +0000 | [diff] [blame] | 47 | DwarfStreamer(raw_fd_ostream &OutFile, LinkOptions Options) |
| 48 | : OutFile(OutFile), Options(std::move(Options)) {} |
| 49 | |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 50 | bool init(Triple TheTriple); |
| 51 | |
| 52 | /// Dump the file to the disk. |
Jonas Devlieghere | 20767d1 | 2019-01-07 23:27:25 +0000 | [diff] [blame] | 53 | bool finish(const DebugMap &, SymbolMapTranslator &T); |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 54 | |
| 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, |
Wolfgang Pieb | daecf94 | 2018-10-31 01:12:58 +0000 | [diff] [blame] | 86 | const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries, |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 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 | |
| 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 | |
Jonas Devlieghere | 20767d1 | 2019-01-07 23:27:25 +0000 | [diff] [blame] | 107 | /// Copy the debug_line over to the updated binary while unobfuscating the |
| 108 | /// file names and directories. |
| 109 | void translateLineTable(DataExtractor LineData, uint32_t Offset, |
| 110 | LinkOptions &Options); |
| 111 | |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 112 | /// Copy over the debug sections that are not modified when updating. |
Jonas Devlieghere | a5d860c | 2018-07-09 16:58:48 +0000 | [diff] [blame] | 113 | void copyInvariantDebugSection(const object::ObjectFile &Obj); |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 114 | |
| 115 | uint32_t getLineSectionSize() const { return LineSectionSize; } |
| 116 | |
| 117 | /// Emit the .debug_pubnames contribution for \p Unit. |
| 118 | void emitPubNamesForUnit(const CompileUnit &Unit); |
| 119 | |
| 120 | /// Emit the .debug_pubtypes contribution for \p Unit. |
| 121 | void emitPubTypesForUnit(const CompileUnit &Unit); |
| 122 | |
| 123 | /// Emit a CIE. |
| 124 | void emitCIE(StringRef CIEBytes); |
| 125 | |
| 126 | /// Emit an FDE with data \p Bytes. |
| 127 | void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address, |
| 128 | StringRef Bytes); |
| 129 | |
Jonas Devlieghere | 7c767bd | 2018-07-25 23:01:38 +0000 | [diff] [blame] | 130 | /// Emit DWARF debug names. |
| 131 | void emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table); |
| 132 | |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 133 | /// Emit Apple namespaces accelerator table. |
| 134 | void emitAppleNamespaces(AccelTable<AppleAccelTableStaticOffsetData> &Table); |
| 135 | |
| 136 | /// Emit Apple names accelerator table. |
| 137 | void emitAppleNames(AccelTable<AppleAccelTableStaticOffsetData> &Table); |
| 138 | |
| 139 | /// Emit Apple Objective-C accelerator table. |
| 140 | void emitAppleObjc(AccelTable<AppleAccelTableStaticOffsetData> &Table); |
| 141 | |
| 142 | /// Emit Apple type accelerator table. |
| 143 | void emitAppleTypes(AccelTable<AppleAccelTableStaticTypeData> &Table); |
| 144 | |
| 145 | uint32_t getFrameSectionSize() const { return FrameSectionSize; } |
| 146 | |
| 147 | private: |
| 148 | /// \defgroup MCObjects MC layer objects constructed by the streamer |
| 149 | /// @{ |
| 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; |
Jonas Devlieghere | a5d860c | 2018-07-09 16:58:48 +0000 | [diff] [blame] | 157 | MCInstPrinter *MIP; // Owned by AsmPrinter |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 158 | MCCodeEmitter *MCE; // Owned by MCStreamer |
| 159 | MCStreamer *MS; // Owned by AsmPrinter |
| 160 | std::unique_ptr<TargetMachine> TM; |
| 161 | std::unique_ptr<AsmPrinter> Asm; |
| 162 | /// @} |
| 163 | |
| 164 | /// The file we stream the linked Dwarf to. |
| 165 | raw_fd_ostream &OutFile; |
| 166 | |
Jonas Devlieghere | a5d860c | 2018-07-09 16:58:48 +0000 | [diff] [blame] | 167 | LinkOptions Options; |
| 168 | |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 169 | uint32_t RangesSectionSize; |
| 170 | uint32_t LocSectionSize; |
| 171 | uint32_t LineSectionSize; |
| 172 | uint32_t FrameSectionSize; |
| 173 | |
Jonas Devlieghere | 7c767bd | 2018-07-25 23:01:38 +0000 | [diff] [blame] | 174 | /// Keep track of emitted CUs and their Unique ID. |
| 175 | struct EmittedUnit { |
| 176 | unsigned ID; |
| 177 | MCSymbol *LabelBegin; |
| 178 | }; |
| 179 | std::vector<EmittedUnit> EmittedUnits; |
| 180 | |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 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); |
| 186 | }; |
| 187 | |
| 188 | } // end namespace dsymutil |
| 189 | } // end namespace llvm |
| 190 | |
| 191 | #endif // LLVM_TOOLS_DSYMUTIL_DWARFSTREAMER_H |