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