1 //===-- llvm/CodeGen/DwarfUnit.h - Dwarf Compile Unit ---*- 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 // This file contains support for writing dwarf compile unit. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H 14 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H 15 16 #include "DwarfDebug.h" 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/ADT/Optional.h" 19 #include "llvm/ADT/StringMap.h" 20 #include "llvm/CodeGen/AsmPrinter.h" 21 #include "llvm/CodeGen/DIE.h" 22 #include "llvm/IR/DIBuilder.h" 23 #include "llvm/IR/DebugInfo.h" 24 #include "llvm/MC/MCDwarf.h" 25 #include "llvm/MC/MCExpr.h" 26 #include "llvm/MC/MCSection.h" 27 28 namespace llvm { 29 30 class MachineOperand; 31 class ConstantInt; 32 class ConstantFP; 33 class DbgVariable; 34 class DwarfCompileUnit; 35 36 //===----------------------------------------------------------------------===// 37 /// This dwarf writer support class manages information associated with a 38 /// source file. 39 class DwarfUnit : public DIEUnit { 40 protected: 41 /// MDNode for the compile unit. 42 const DICompileUnit *CUNode; 43 44 // All DIEValues are allocated through this allocator. 45 BumpPtrAllocator DIEValueAllocator; 46 47 /// Target of Dwarf emission. 48 AsmPrinter *Asm; 49 50 /// Emitted at the end of the CU and used to compute the CU Length field. 51 MCSymbol *EndLabel = nullptr; 52 53 // Holders for some common dwarf information. 54 DwarfDebug *DD; 55 DwarfFile *DU; 56 57 /// An anonymous type for index type. Owned by DIEUnit. 58 DIE *IndexTyDie; 59 60 /// Tracks the mapping of unit level debug information variables to debug 61 /// information entries. 62 DenseMap<const MDNode *, DIE *> MDNodeToDieMap; 63 64 /// A list of all the DIEBlocks in use. 65 std::vector<DIEBlock *> DIEBlocks; 66 67 /// A list of all the DIELocs in use. 68 std::vector<DIELoc *> DIELocs; 69 70 /// This map is used to keep track of subprogram DIEs that need 71 /// DW_AT_containing_type attribute. This attribute points to a DIE that 72 /// corresponds to the MDNode mapped with the subprogram DIE. 73 DenseMap<DIE *, const DINode *> ContainingTypeMap; 74 75 DwarfUnit(dwarf::Tag, const DICompileUnit *Node, AsmPrinter *A, DwarfDebug *DW, 76 DwarfFile *DWU); 77 78 bool applySubprogramDefinitionAttributes(const DISubprogram *SP, DIE &SPDie); 79 80 bool shareAcrossDWOCUs() const; 81 bool isShareableAcrossCUs(const DINode *D) const; 82 83 public: 84 // Accessors. 85 AsmPrinter* getAsmPrinter() const { return Asm; } 86 MCSymbol *getEndLabel() const { return EndLabel; } 87 uint16_t getLanguage() const { return CUNode->getSourceLanguage(); } 88 const DICompileUnit *getCUNode() const { return CUNode; } 89 90 /// Return true if this compile unit has something to write out. 91 bool hasContent() const { return getUnitDie().hasChildren(); } 92 93 /// Get string containing language specific context for a global name. 94 /// 95 /// Walks the metadata parent chain in a language specific manner (using the 96 /// compile unit language) and returns it as a string. This is done at the 97 /// metadata level because DIEs may not currently have been added to the 98 /// parent context and walking the DIEs looking for names is more expensive 99 /// than walking the metadata. 100 std::string getParentContextString(const DIScope *Context) const; 101 102 /// Add a new global name to the compile unit. 103 virtual void addGlobalName(StringRef Name, const DIE &Die, 104 const DIScope *Context) = 0; 105 106 /// Add a new global type to the compile unit. 107 virtual void addGlobalType(const DIType *Ty, const DIE &Die, 108 const DIScope *Context) = 0; 109 110 /// Returns the DIE map slot for the specified debug variable. 111 /// 112 /// We delegate the request to DwarfDebug when the MDNode can be part of the 113 /// type system, since DIEs for the type system can be shared across CUs and 114 /// the mappings are kept in DwarfDebug. 115 DIE *getDIE(const DINode *D) const; 116 117 /// Returns a fresh newly allocated DIELoc. 118 DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc; } 119 120 /// Insert DIE into the map. 121 /// 122 /// We delegate the request to DwarfDebug when the MDNode can be part of the 123 /// type system, since DIEs for the type system can be shared across CUs and 124 /// the mappings are kept in DwarfDebug. 125 void insertDIE(const DINode *Desc, DIE *D); 126 127 void insertDIE(DIE *D); 128 129 /// Add a flag that is true to the DIE. 130 void addFlag(DIE &Die, dwarf::Attribute Attribute); 131 132 /// Add an unsigned integer attribute data and value. 133 void addUInt(DIEValueList &Die, dwarf::Attribute Attribute, 134 Optional<dwarf::Form> Form, uint64_t Integer); 135 136 void addUInt(DIEValueList &Block, dwarf::Form Form, uint64_t Integer); 137 138 /// Add an signed integer attribute data and value. 139 void addSInt(DIEValueList &Die, dwarf::Attribute Attribute, 140 Optional<dwarf::Form> Form, int64_t Integer); 141 142 void addSInt(DIELoc &Die, Optional<dwarf::Form> Form, int64_t Integer); 143 144 /// Add a string attribute data and value. 145 /// 146 /// We always emit a reference to the string pool instead of immediate 147 /// strings so that DIEs have more predictable sizes. In the case of split 148 /// dwarf we emit an index into another table which gets us the static offset 149 /// into the string table. 150 void addString(DIE &Die, dwarf::Attribute Attribute, StringRef Str); 151 152 /// Add a Dwarf label attribute data and value. 153 DIEValueList::value_iterator addLabel(DIEValueList &Die, 154 dwarf::Attribute Attribute, 155 dwarf::Form Form, 156 const MCSymbol *Label); 157 158 void addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label); 159 160 /// Add an offset into a section attribute data and value. 161 void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer); 162 163 /// Add a dwarf op address data and value using the form given and an 164 /// op of either DW_FORM_addr or DW_FORM_GNU_addr_index. 165 void addOpAddress(DIELoc &Die, const MCSymbol *Sym); 166 167 /// Add a label delta attribute data and value. 168 void addLabelDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi, 169 const MCSymbol *Lo); 170 171 /// Add a DIE attribute data and value. 172 void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry); 173 174 /// Add a DIE attribute data and value. 175 void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIEEntry Entry); 176 177 /// Add a type's DW_AT_signature and set the declaration flag. 178 void addDIETypeSignature(DIE &Die, uint64_t Signature); 179 180 /// Add block data. 181 void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc); 182 183 /// Add block data. 184 void addBlock(DIE &Die, dwarf::Attribute Attribute, DIEBlock *Block); 185 186 /// Add location information to specified debug information entry. 187 void addSourceLine(DIE &Die, unsigned Line, const DIFile *File); 188 void addSourceLine(DIE &Die, const DILocalVariable *V); 189 void addSourceLine(DIE &Die, const DIGlobalVariable *G); 190 void addSourceLine(DIE &Die, const DISubprogram *SP); 191 void addSourceLine(DIE &Die, const DILabel *L); 192 void addSourceLine(DIE &Die, const DIType *Ty); 193 void addSourceLine(DIE &Die, const DIObjCProperty *Ty); 194 195 /// Add constant value entry in variable DIE. 196 void addConstantValue(DIE &Die, const MachineOperand &MO, const DIType *Ty); 197 void addConstantValue(DIE &Die, const ConstantInt *CI, const DIType *Ty); 198 void addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty); 199 void addConstantValue(DIE &Die, const APInt &Val, bool Unsigned); 200 void addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty); 201 void addConstantValue(DIE &Die, bool Unsigned, uint64_t Val); 202 203 /// Add constant value entry in variable DIE. 204 void addConstantFPValue(DIE &Die, const MachineOperand &MO); 205 void addConstantFPValue(DIE &Die, const ConstantFP *CFP); 206 207 /// Add a linkage name, if it isn't empty. 208 void addLinkageName(DIE &Die, StringRef LinkageName); 209 210 /// Add template parameters in buffer. 211 void addTemplateParams(DIE &Buffer, DINodeArray TParams); 212 213 /// Add thrown types. 214 void addThrownTypes(DIE &Die, DINodeArray ThrownTypes); 215 216 /// Add a new type attribute to the specified entity. 217 /// 218 /// This takes and attribute parameter because DW_AT_friend attributes are 219 /// also type references. 220 void addType(DIE &Entity, const DIType *Ty, 221 dwarf::Attribute Attribute = dwarf::DW_AT_type); 222 223 DIE *getOrCreateNameSpace(const DINamespace *NS); 224 DIE *getOrCreateModule(const DIModule *M); 225 DIE *getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal = false); 226 227 void applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie, 228 bool SkipSPAttributes = false); 229 230 /// Creates type DIE with specific context. 231 DIE *createTypeDIE(const DIScope *Context, DIE &ContextDIE, const DIType *Ty); 232 233 /// Find existing DIE or create new DIE for the given type. 234 DIE *getOrCreateTypeDIE(const MDNode *TyNode); 235 236 /// Get context owner's DIE. 237 DIE *getOrCreateContextDIE(const DIScope *Context); 238 239 /// Construct DIEs for types that contain vtables. 240 void constructContainingTypeDIEs(); 241 242 /// Construct function argument DIEs. 243 void constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args); 244 245 /// Create a DIE with the given Tag, add the DIE to its parent, and 246 /// call insertDIE if MD is not null. 247 DIE &createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N = nullptr); 248 249 bool useSegmentedStringOffsetsTable() const { 250 return DD->useSegmentedStringOffsetsTable(); 251 } 252 253 /// Compute the size of a header for this unit, not including the initial 254 /// length field. 255 virtual unsigned getHeaderSize() const { 256 return sizeof(int16_t) + // DWARF version number 257 sizeof(int32_t) + // Offset Into Abbrev. Section 258 sizeof(int8_t) + // Pointer Size (in bytes) 259 (DD->getDwarfVersion() >= 5 ? sizeof(int8_t) 260 : 0); // DWARF v5 unit type 261 } 262 263 /// Emit the header for this unit, not including the initial length field. 264 virtual void emitHeader(bool UseOffsets) = 0; 265 266 /// Add the DW_AT_str_offsets_base attribute to the unit DIE. 267 void addStringOffsetsStart(); 268 269 /// Add the DW_AT_rnglists_base attribute to the unit DIE. 270 void addRnglistsBase(); 271 272 virtual DwarfCompileUnit &getCU() = 0; 273 274 void constructTypeDIE(DIE &Buffer, const DICompositeType *CTy); 275 276 /// addSectionDelta - Add a label delta attribute data and value. 277 DIE::value_iterator addSectionDelta(DIE &Die, dwarf::Attribute Attribute, 278 const MCSymbol *Hi, const MCSymbol *Lo); 279 280 /// Add a Dwarf section label attribute data and value. 281 DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 282 const MCSymbol *Label, 283 const MCSymbol *Sec); 284 285 /// Get context owner's DIE. 286 DIE *createTypeDIE(const DICompositeType *Ty); 287 288 protected: 289 ~DwarfUnit(); 290 291 /// Create new static data member DIE. 292 DIE *getOrCreateStaticMemberDIE(const DIDerivedType *DT); 293 294 /// Look up the source ID for the given file. If none currently exists, 295 /// create a new ID and insert it in the line table. 296 virtual unsigned getOrCreateSourceID(const DIFile *File) = 0; 297 298 /// Emit the common part of the header for this unit. 299 void emitCommonHeader(bool UseOffsets, dwarf::UnitType UT); 300 301 private: 302 void constructTypeDIE(DIE &Buffer, const DIBasicType *BTy); 303 void constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy); 304 void constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy); 305 void constructSubrangeDIE(DIE &Buffer, const DISubrange *SR, DIE *IndexTy); 306 void constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy); 307 void constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy); 308 DIE &constructMemberDIE(DIE &Buffer, const DIDerivedType *DT); 309 void constructTemplateTypeParameterDIE(DIE &Buffer, 310 const DITemplateTypeParameter *TP); 311 void constructTemplateValueParameterDIE(DIE &Buffer, 312 const DITemplateValueParameter *TVP); 313 314 /// Return the default lower bound for an array. 315 /// 316 /// If the DWARF version doesn't handle the language, return -1. 317 int64_t getDefaultLowerBound() const; 318 319 /// Get an anonymous type for index type. 320 DIE *getIndexTyDie(); 321 322 /// Set D as anonymous type for index which can be reused later. 323 void setIndexTyDie(DIE *D) { IndexTyDie = D; } 324 325 virtual void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) = 0; 326 327 /// If this is a named finished type then include it in the list of types for 328 /// the accelerator tables. 329 void updateAcceleratorTables(const DIScope *Context, const DIType *Ty, 330 const DIE &TyDIE); 331 332 virtual bool isDwoUnit() const = 0; 333 const MCSymbol *getCrossSectionRelativeBaseAddress() const override; 334 }; 335 336 class DwarfTypeUnit final : public DwarfUnit { 337 uint64_t TypeSignature; 338 const DIE *Ty; 339 DwarfCompileUnit &CU; 340 MCDwarfDwoLineTable *SplitLineTable; 341 bool UsedLineTable = false; 342 343 unsigned getOrCreateSourceID(const DIFile *File) override; 344 void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override; 345 bool isDwoUnit() const override; 346 347 public: 348 DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A, DwarfDebug *DW, 349 DwarfFile *DWU, MCDwarfDwoLineTable *SplitLineTable = nullptr); 350 351 void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; } 352 void setType(const DIE *Ty) { this->Ty = Ty; } 353 354 /// Emit the header for this unit, not including the initial length field. 355 void emitHeader(bool UseOffsets) override; 356 unsigned getHeaderSize() const override { 357 return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature 358 sizeof(uint32_t); // Type DIE Offset 359 } 360 void addGlobalName(StringRef Name, const DIE &Die, 361 const DIScope *Context) override; 362 void addGlobalType(const DIType *Ty, const DIE &Die, 363 const DIScope *Context) override; 364 DwarfCompileUnit &getCU() override { return CU; } 365 }; 366 } // end llvm namespace 367 #endif 368