1 //===- llvm/CodeGen/DwarfFile.h - Dwarf Debug Framework ---------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H 10 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H 11 12 #include "DwarfStringPool.h" 13 #include "llvm/ADT/DenseMap.h" 14 #include "llvm/ADT/SmallVector.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/CodeGen/DIE.h" 17 #include "llvm/IR/Metadata.h" 18 #include "llvm/Support/Allocator.h" 19 #include <map> 20 #include <memory> 21 #include <utility> 22 23 namespace llvm { 24 25 class AsmPrinter; 26 class DbgEntity; 27 class DbgVariable; 28 class DbgLabel; 29 class DwarfCompileUnit; 30 class DwarfUnit; 31 class LexicalScope; 32 class MCSection; 33 34 // Data structure to hold a range for range lists. 35 struct RangeSpan { 36 const MCSymbol *Begin; 37 const MCSymbol *End; 38 }; 39 40 class RangeSpanList { 41 private: 42 // Index for locating within the debug_range section this particular span. 43 MCSymbol *RangeSym; 44 const DwarfCompileUnit *CU; 45 // List of ranges. 46 SmallVector<RangeSpan, 2> Ranges; 47 48 public: 49 RangeSpanList(MCSymbol *Sym, const DwarfCompileUnit &CU, 50 SmallVector<RangeSpan, 2> Ranges) 51 : RangeSym(Sym), CU(&CU), Ranges(std::move(Ranges)) {} 52 MCSymbol *getSym() const { return RangeSym; } 53 const DwarfCompileUnit &getCU() const { return *CU; } 54 const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; } 55 }; 56 57 class DwarfFile { 58 // Target of Dwarf emission, used for sizing of abbreviations. 59 AsmPrinter *Asm; 60 61 BumpPtrAllocator AbbrevAllocator; 62 63 // Used to uniquely define abbreviations. 64 DIEAbbrevSet Abbrevs; 65 66 // A pointer to all units in the section. 67 SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs; 68 69 DwarfStringPool StrPool; 70 71 // List of range lists for a given compile unit, separate from the ranges for 72 // the CU itself. 73 SmallVector<RangeSpanList, 1> CURangeLists; 74 75 /// DWARF v5: The symbol that designates the start of the contribution to 76 /// the string offsets table. The contribution is shared by all units. 77 MCSymbol *StringOffsetsStartSym = nullptr; 78 79 /// DWARF v5: The symbol that designates the base of the range list table. 80 /// The table is shared by all units. 81 MCSymbol *RnglistsTableBaseSym = nullptr; 82 83 /// The variables of a lexical scope. 84 struct ScopeVars { 85 /// We need to sort Args by ArgNo and check for duplicates. This could also 86 /// be implemented as a list or vector + std::lower_bound(). 87 std::map<unsigned, DbgVariable *> Args; 88 SmallVector<DbgVariable *, 8> Locals; 89 }; 90 /// Collection of DbgVariables of each lexical scope. 91 DenseMap<LexicalScope *, ScopeVars> ScopeVariables; 92 93 /// Collection of DbgLabels of each lexical scope. 94 using LabelList = SmallVector<DbgLabel *, 4>; 95 DenseMap<LexicalScope *, LabelList> ScopeLabels; 96 97 // Collection of abstract subprogram DIEs. 98 DenseMap<const MDNode *, DIE *> AbstractSPDies; 99 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities; 100 101 /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can 102 /// be shared across CUs, that is why we keep the map here instead 103 /// of in DwarfCompileUnit. 104 DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap; 105 106 public: 107 DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA); 108 109 const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() { 110 return CUs; 111 } 112 113 std::pair<uint32_t, RangeSpanList *> addRange(const DwarfCompileUnit &CU, 114 SmallVector<RangeSpan, 2> R); 115 116 /// getRangeLists - Get the vector of range lists. 117 const SmallVectorImpl<RangeSpanList> &getRangeLists() const { 118 return CURangeLists; 119 } 120 121 /// Compute the size and offset of a DIE given an incoming Offset. 122 unsigned computeSizeAndOffset(DIE &Die, unsigned Offset); 123 124 /// Compute the size and offset of all the DIEs. 125 void computeSizeAndOffsets(); 126 127 /// Compute the size and offset of all the DIEs in the given unit. 128 /// \returns The size of the root DIE. 129 unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU); 130 131 /// Add a unit to the list of CUs. 132 void addUnit(std::unique_ptr<DwarfCompileUnit> U); 133 134 /// Emit all of the units to the section listed with the given 135 /// abbreviation section. 136 void emitUnits(bool UseOffsets); 137 138 /// Emit the given unit to its section. 139 void emitUnit(DwarfUnit *TheU, bool UseOffsets); 140 141 /// Emit a set of abbreviations to the specific section. 142 void emitAbbrevs(MCSection *); 143 144 /// Emit all of the strings to the section given. If OffsetSection is 145 /// non-null, emit a table of string offsets to it. If UseRelativeOffsets 146 /// is false, emit absolute offsets to the strings. Otherwise, emit 147 /// relocatable references to the strings if they are supported by the target. 148 void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr, 149 bool UseRelativeOffsets = false); 150 151 /// Returns the string pool. 152 DwarfStringPool &getStringPool() { return StrPool; } 153 154 MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; } 155 void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; } 156 157 MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; } 158 void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; } 159 160 /// \returns false if the variable was merged with a previous one. 161 bool addScopeVariable(LexicalScope *LS, DbgVariable *Var); 162 163 void addScopeLabel(LexicalScope *LS, DbgLabel *Label); 164 165 DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() { 166 return ScopeVariables; 167 } 168 169 DenseMap<LexicalScope *, LabelList> &getScopeLabels() { 170 return ScopeLabels; 171 } 172 173 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() { 174 return AbstractSPDies; 175 } 176 177 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() { 178 return AbstractEntities; 179 } 180 181 void insertDIE(const MDNode *TypeMD, DIE *Die) { 182 DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die)); 183 } 184 185 DIE *getDIE(const MDNode *TypeMD) { 186 return DITypeNodeToDieMap.lookup(TypeMD); 187 } 188 }; 189 190 } // end namespace llvm 191 192 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H 193