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