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