1 //===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h ----*- 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 Microsoft CodeView debug info. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H 15 #define LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H 16 17 #include "DebugHandlerBase.h" 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ADT/StringMap.h" 20 #include "llvm/CodeGen/AsmPrinter.h" 21 #include "llvm/CodeGen/MachineFunction.h" 22 #include "llvm/CodeGen/MachineModuleInfo.h" 23 #include "llvm/DebugInfo/CodeView/MemoryTypeTableBuilder.h" 24 #include "llvm/DebugInfo/CodeView/TypeIndex.h" 25 #include "llvm/IR/DebugInfo.h" 26 #include "llvm/IR/DebugLoc.h" 27 #include "llvm/MC/MCStreamer.h" 28 #include "llvm/Target/TargetLoweringObjectFile.h" 29 30 namespace llvm { 31 32 class StringRef; 33 class LexicalScope; 34 struct ClassInfo; 35 36 /// \brief Collects and handles line tables information in a CodeView format. 37 class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase { 38 MCStreamer &OS; 39 llvm::BumpPtrAllocator Allocator; 40 codeview::MemoryTypeTableBuilder TypeTable; 41 42 /// Represents the most general definition range. 43 struct LocalVarDefRange { 44 /// Indicates that variable data is stored in memory relative to the 45 /// specified register. 46 int InMemory : 1; 47 48 /// Offset of variable data in memory. 49 int DataOffset : 31; 50 51 /// Offset of the data into the user level struct. If zero, no splitting 52 /// occurred. 53 uint16_t StructOffset; 54 55 /// Register containing the data or the register base of the memory 56 /// location containing the data. 57 uint16_t CVRegister; 58 59 /// Compares all location fields. This includes all fields except the label 60 /// ranges. 61 bool isDifferentLocation(LocalVarDefRange &O) { 62 return InMemory != O.InMemory || DataOffset != O.DataOffset || 63 StructOffset != O.StructOffset || CVRegister != O.CVRegister; 64 } 65 66 SmallVector<std::pair<const MCSymbol *, const MCSymbol *>, 1> Ranges; 67 }; 68 69 static LocalVarDefRange createDefRangeMem(uint16_t CVRegister, int Offset); 70 static LocalVarDefRange createDefRangeReg(uint16_t CVRegister); 71 72 /// Similar to DbgVariable in DwarfDebug, but not dwarf-specific. 73 struct LocalVariable { 74 const DILocalVariable *DIVar = nullptr; 75 SmallVector<LocalVarDefRange, 1> DefRanges; 76 }; 77 78 struct InlineSite { 79 SmallVector<LocalVariable, 1> InlinedLocals; 80 SmallVector<const DILocation *, 1> ChildSites; 81 const DISubprogram *Inlinee = nullptr; 82 83 /// The ID of the inline site or function used with .cv_loc. Not a type 84 /// index. 85 unsigned SiteFuncId = 0; 86 }; 87 88 // For each function, store a vector of labels to its instructions, as well as 89 // to the end of the function. 90 struct FunctionInfo { 91 /// Map from inlined call site to inlined instructions and child inlined 92 /// call sites. Listed in program order. 93 std::unordered_map<const DILocation *, InlineSite> InlineSites; 94 95 /// Ordered list of top-level inlined call sites. 96 SmallVector<const DILocation *, 1> ChildSites; 97 98 SmallVector<LocalVariable, 1> Locals; 99 100 DebugLoc LastLoc; 101 const MCSymbol *Begin = nullptr; 102 const MCSymbol *End = nullptr; 103 unsigned FuncId = 0; 104 unsigned LastFileId = 0; 105 bool HaveLineInfo = false; 106 }; 107 FunctionInfo *CurFn; 108 109 /// The set of comdat .debug$S sections that we've seen so far. Each section 110 /// must start with a magic version number that must only be emitted once. 111 /// This set tracks which sections we've already opened. 112 DenseSet<MCSectionCOFF *> ComdatDebugSections; 113 114 /// Switch to the appropriate .debug$S section for GVSym. If GVSym, the symbol 115 /// of an emitted global value, is in a comdat COFF section, this will switch 116 /// to a new .debug$S section in that comdat. This method ensures that the 117 /// section starts with the magic version number on first use. If GVSym is 118 /// null, uses the main .debug$S section. 119 void switchToDebugSectionForSymbol(const MCSymbol *GVSym); 120 121 /// The next available function index for use with our .cv_* directives. Not 122 /// to be confused with type indices for LF_FUNC_ID records. 123 unsigned NextFuncId = 0; 124 125 InlineSite &getInlineSite(const DILocation *InlinedAt, 126 const DISubprogram *Inlinee); 127 128 codeview::TypeIndex getFuncIdForSubprogram(const DISubprogram *SP); 129 130 static void collectInlineSiteChildren(SmallVectorImpl<unsigned> &Children, 131 const FunctionInfo &FI, 132 const InlineSite &Site); 133 134 /// Remember some debug info about each function. Keep it in a stable order to 135 /// emit at the end of the TU. 136 MapVector<const Function *, FunctionInfo> FnDebugInfo; 137 138 /// Map from DIFile to .cv_file id. 139 DenseMap<const DIFile *, unsigned> FileIdMap; 140 141 /// All inlined subprograms in the order they should be emitted. 142 SmallSetVector<const DISubprogram *, 4> InlinedSubprograms; 143 144 /// Map from a pair of DI metadata nodes and its DI type (or scope) that can 145 /// be nullptr, to CodeView type indices. Primarily indexed by 146 /// {DIType*, DIType*} and {DISubprogram*, DIType*}. 147 /// 148 /// The second entry in the key is needed for methods as DISubroutineType 149 /// representing static method type are shared with non-method function type. 150 DenseMap<std::pair<const DINode *, const DIType *>, codeview::TypeIndex> 151 TypeIndices; 152 153 /// Map from DICompositeType* to complete type index. Non-record types are 154 /// always looked up in the normal TypeIndices map. 155 DenseMap<const DICompositeType *, codeview::TypeIndex> CompleteTypeIndices; 156 157 /// Complete record types to emit after all active type lowerings are 158 /// finished. 159 SmallVector<const DICompositeType *, 4> DeferredCompleteTypes; 160 161 /// Number of type lowering frames active on the stack. 162 unsigned TypeEmissionLevel = 0; 163 164 codeview::TypeIndex VBPType; 165 166 const DISubprogram *CurrentSubprogram = nullptr; 167 168 // The UDTs we have seen while processing types; each entry is a pair of type 169 // index and type name. 170 std::vector<std::pair<std::string, codeview::TypeIndex>> LocalUDTs, 171 GlobalUDTs; 172 173 typedef std::map<const DIFile *, std::string> FileToFilepathMapTy; 174 FileToFilepathMapTy FileToFilepathMap; 175 StringRef getFullFilepath(const DIFile *S); 176 177 unsigned maybeRecordFile(const DIFile *F); 178 179 void maybeRecordLocation(const DebugLoc &DL, const MachineFunction *MF); 180 181 void clear(); 182 183 void setCurrentSubprogram(const DISubprogram *SP) { 184 CurrentSubprogram = SP; 185 LocalUDTs.clear(); 186 } 187 188 /// Emit the magic version number at the start of a CodeView type or symbol 189 /// section. Appears at the front of every .debug$S or .debug$T section. 190 void emitCodeViewMagicVersion(); 191 192 void emitTypeInformation(); 193 194 void emitCompilerInformation(); 195 196 void emitInlineeLinesSubsection(); 197 198 void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI); 199 200 void emitDebugInfoForGlobals(); 201 202 void emitDebugInfoForRetainedTypes(); 203 204 void emitDebugInfoForUDTs( 205 ArrayRef<std::pair<std::string, codeview::TypeIndex>> UDTs); 206 207 void emitDebugInfoForGlobal(const DIGlobalVariable *DIGV, 208 const GlobalVariable *GV, MCSymbol *GVSym); 209 210 /// Opens a subsection of the given kind in a .debug$S codeview section. 211 /// Returns an end label for use with endCVSubsection when the subsection is 212 /// finished. 213 MCSymbol *beginCVSubsection(codeview::ModuleSubstreamKind Kind); 214 215 void endCVSubsection(MCSymbol *EndLabel); 216 217 void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt, 218 const InlineSite &Site); 219 220 typedef DbgValueHistoryMap::InlinedVariable InlinedVariable; 221 222 void collectVariableInfo(const DISubprogram *SP); 223 224 void collectVariableInfoFromMMITable(DenseSet<InlinedVariable> &Processed); 225 226 /// Records information about a local variable in the appropriate scope. In 227 /// particular, locals from inlined code live inside the inlining site. 228 void recordLocalVariable(LocalVariable &&Var, const DILocation *Loc); 229 230 /// Emits local variables in the appropriate order. 231 void emitLocalVariableList(ArrayRef<LocalVariable> Locals); 232 233 /// Emits an S_LOCAL record and its associated defined ranges. 234 void emitLocalVariable(const LocalVariable &Var); 235 236 /// Translates the DIType to codeview if necessary and returns a type index 237 /// for it. 238 codeview::TypeIndex getTypeIndex(DITypeRef TypeRef, 239 DITypeRef ClassTyRef = DITypeRef()); 240 241 codeview::TypeIndex getMemberFunctionType(const DISubprogram *SP, 242 const DICompositeType *Class); 243 244 codeview::TypeIndex getScopeIndex(const DIScope *Scope); 245 246 codeview::TypeIndex getVBPTypeIndex(); 247 248 void addToUDTs(const DIType *Ty, codeview::TypeIndex TI); 249 250 codeview::TypeIndex lowerType(const DIType *Ty, const DIType *ClassTy); 251 codeview::TypeIndex lowerTypeAlias(const DIDerivedType *Ty); 252 codeview::TypeIndex lowerTypeArray(const DICompositeType *Ty); 253 codeview::TypeIndex lowerTypeBasic(const DIBasicType *Ty); 254 codeview::TypeIndex lowerTypePointer(const DIDerivedType *Ty); 255 codeview::TypeIndex lowerTypeMemberPointer(const DIDerivedType *Ty); 256 codeview::TypeIndex lowerTypeModifier(const DIDerivedType *Ty); 257 codeview::TypeIndex lowerTypeFunction(const DISubroutineType *Ty); 258 codeview::TypeIndex lowerTypeVFTableShape(const DIDerivedType *Ty); 259 codeview::TypeIndex lowerTypeMemberFunction(const DISubroutineType *Ty, 260 const DIType *ClassTy, 261 int ThisAdjustment); 262 codeview::TypeIndex lowerTypeEnum(const DICompositeType *Ty); 263 codeview::TypeIndex lowerTypeClass(const DICompositeType *Ty); 264 codeview::TypeIndex lowerTypeUnion(const DICompositeType *Ty); 265 266 /// Symbol records should point to complete types, but type records should 267 /// always point to incomplete types to avoid cycles in the type graph. Only 268 /// use this entry point when generating symbol records. The complete and 269 /// incomplete type indices only differ for record types. All other types use 270 /// the same index. 271 codeview::TypeIndex getCompleteTypeIndex(DITypeRef TypeRef); 272 273 codeview::TypeIndex lowerCompleteTypeClass(const DICompositeType *Ty); 274 codeview::TypeIndex lowerCompleteTypeUnion(const DICompositeType *Ty); 275 276 struct TypeLoweringScope; 277 278 void emitDeferredCompleteTypes(); 279 280 void collectMemberInfo(ClassInfo &Info, const DIDerivedType *DDTy); 281 ClassInfo collectClassInfo(const DICompositeType *Ty); 282 283 /// Common record member lowering functionality for record types, which are 284 /// structs, classes, and unions. Returns the field list index and the member 285 /// count. 286 std::tuple<codeview::TypeIndex, codeview::TypeIndex, unsigned, bool> 287 lowerRecordFieldList(const DICompositeType *Ty); 288 289 /// Inserts {{Node, ClassTy}, TI} into TypeIndices and checks for duplicates. 290 codeview::TypeIndex recordTypeIndexForDINode(const DINode *Node, 291 codeview::TypeIndex TI, 292 const DIType *ClassTy = nullptr); 293 294 unsigned getPointerSizeInBytes(); 295 296 public: 297 CodeViewDebug(AsmPrinter *Asm); 298 299 void setSymbolSize(const llvm::MCSymbol *, uint64_t) override {} 300 301 /// \brief Emit the COFF section that holds the line table information. 302 void endModule() override; 303 304 /// \brief Gather pre-function debug information. 305 void beginFunction(const MachineFunction *MF) override; 306 307 /// \brief Gather post-function debug information. 308 void endFunction(const MachineFunction *) override; 309 310 /// \brief Process beginning of an instruction. 311 void beginInstruction(const MachineInstr *MI) override; 312 }; 313 } // End of namespace llvm 314 315 #endif 316