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