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