CodeViewDebug.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //===- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h --------------*- 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 Microsoft CodeView debug info.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
  13. #define LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/DenseSet.h"
  17. #include "llvm/ADT/MapVector.h"
  18. #include "llvm/ADT/SetVector.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
  21. #include "llvm/CodeGen/DebugHandlerBase.h"
  22. #include "llvm/DebugInfo/CodeView/CodeView.h"
  23. #include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h"
  24. #include "llvm/DebugInfo/CodeView/TypeIndex.h"
  25. #include "llvm/IR/DebugLoc.h"
  26. #include "llvm/Support/Allocator.h"
  27. #include "llvm/Support/Compiler.h"
  28. #include <cstdint>
  29. #include <map>
  30. #include <string>
  31. #include <tuple>
  32. #include <unordered_map>
  33. #include <utility>
  34. #include <vector>
  35. namespace llvm {
  36. struct ClassInfo;
  37. class StringRef;
  38. class AsmPrinter;
  39. class Function;
  40. class GlobalVariable;
  41. class MCSectionCOFF;
  42. class MCStreamer;
  43. class MCSymbol;
  44. class MachineFunction;
  45. /// Collects and handles line tables information in a CodeView format.
  46. class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
  47. MCStreamer &OS;
  48. BumpPtrAllocator Allocator;
  49. codeview::GlobalTypeTableBuilder TypeTable;
  50. /// Whether to emit type record hashes into .debug$H.
  51. bool EmitDebugGlobalHashes = false;
  52. /// The codeview CPU type used by the translation unit.
  53. codeview::CPUType TheCPU;
  54. /// Represents the most general definition range.
  55. struct LocalVarDefRange {
  56. /// Indicates that variable data is stored in memory relative to the
  57. /// specified register.
  58. int InMemory : 1;
  59. /// Offset of variable data in memory.
  60. int DataOffset : 31;
  61. /// Non-zero if this is a piece of an aggregate.
  62. uint16_t IsSubfield : 1;
  63. /// Offset into aggregate.
  64. uint16_t StructOffset : 15;
  65. /// Register containing the data or the register base of the memory
  66. /// location containing the data.
  67. uint16_t CVRegister;
  68. /// Compares all location fields. This includes all fields except the label
  69. /// ranges.
  70. bool isDifferentLocation(LocalVarDefRange &O) {
  71. return InMemory != O.InMemory || DataOffset != O.DataOffset ||
  72. IsSubfield != O.IsSubfield || StructOffset != O.StructOffset ||
  73. CVRegister != O.CVRegister;
  74. }
  75. SmallVector<std::pair<const MCSymbol *, const MCSymbol *>, 1> Ranges;
  76. };
  77. static LocalVarDefRange createDefRangeMem(uint16_t CVRegister, int Offset);
  78. /// Similar to DbgVariable in DwarfDebug, but not dwarf-specific.
  79. struct LocalVariable {
  80. const DILocalVariable *DIVar = nullptr;
  81. SmallVector<LocalVarDefRange, 1> DefRanges;
  82. bool UseReferenceType = false;
  83. };
  84. struct CVGlobalVariable {
  85. const DIGlobalVariable *DIGV;
  86. const GlobalVariable *GV;
  87. };
  88. struct InlineSite {
  89. SmallVector<LocalVariable, 1> InlinedLocals;
  90. SmallVector<const DILocation *, 1> ChildSites;
  91. const DISubprogram *Inlinee = nullptr;
  92. /// The ID of the inline site or function used with .cv_loc. Not a type
  93. /// index.
  94. unsigned SiteFuncId = 0;
  95. };
  96. // Combines information from DILexicalBlock and LexicalScope.
  97. struct LexicalBlock {
  98. SmallVector<LocalVariable, 1> Locals;
  99. SmallVector<CVGlobalVariable, 1> Globals;
  100. SmallVector<LexicalBlock *, 1> Children;
  101. const MCSymbol *Begin;
  102. const MCSymbol *End;
  103. StringRef Name;
  104. };
  105. // For each function, store a vector of labels to its instructions, as well as
  106. // to the end of the function.
  107. struct FunctionInfo {
  108. FunctionInfo() = default;
  109. // Uncopyable.
  110. FunctionInfo(const FunctionInfo &FI) = delete;
  111. /// Map from inlined call site to inlined instructions and child inlined
  112. /// call sites. Listed in program order.
  113. std::unordered_map<const DILocation *, InlineSite> InlineSites;
  114. /// Ordered list of top-level inlined call sites.
  115. SmallVector<const DILocation *, 1> ChildSites;
  116. SmallVector<LocalVariable, 1> Locals;
  117. SmallVector<CVGlobalVariable, 1> Globals;
  118. std::unordered_map<const DILexicalBlockBase*, LexicalBlock> LexicalBlocks;
  119. // Lexical blocks containing local variables.
  120. SmallVector<LexicalBlock *, 1> ChildBlocks;
  121. std::vector<std::pair<MCSymbol *, MDNode *>> Annotations;
  122. std::vector<std::tuple<MCSymbol *, MCSymbol *, DIType *>> HeapAllocSites;
  123. const MCSymbol *Begin = nullptr;
  124. const MCSymbol *End = nullptr;
  125. unsigned FuncId = 0;
  126. unsigned LastFileId = 0;
  127. /// Number of bytes allocated in the prologue for all local stack objects.
  128. unsigned FrameSize = 0;
  129. /// Number of bytes of parameters on the stack.
  130. unsigned ParamSize = 0;
  131. /// Number of bytes pushed to save CSRs.
  132. unsigned CSRSize = 0;
  133. /// Adjustment to apply on x86 when using the VFRAME frame pointer.
  134. int OffsetAdjustment = 0;
  135. /// Two-bit value indicating which register is the designated frame pointer
  136. /// register for local variables. Included in S_FRAMEPROC.
  137. codeview::EncodedFramePtrReg EncodedLocalFramePtrReg =
  138. codeview::EncodedFramePtrReg::None;
  139. /// Two-bit value indicating which register is the designated frame pointer
  140. /// register for stack parameters. Included in S_FRAMEPROC.
  141. codeview::EncodedFramePtrReg EncodedParamFramePtrReg =
  142. codeview::EncodedFramePtrReg::None;
  143. codeview::FrameProcedureOptions FrameProcOpts;
  144. bool HasStackRealignment = false;
  145. bool HaveLineInfo = false;
  146. };
  147. FunctionInfo *CurFn = nullptr;
  148. // Map used to seperate variables according to the lexical scope they belong
  149. // in. This is populated by recordLocalVariable() before
  150. // collectLexicalBlocks() separates the variables between the FunctionInfo
  151. // and LexicalBlocks.
  152. DenseMap<const LexicalScope *, SmallVector<LocalVariable, 1>> ScopeVariables;
  153. // Map to separate global variables according to the lexical scope they
  154. // belong in. A null local scope represents the global scope.
  155. typedef SmallVector<CVGlobalVariable, 1> GlobalVariableList;
  156. DenseMap<const DIScope*, std::unique_ptr<GlobalVariableList> > ScopeGlobals;
  157. // Array of global variables which need to be emitted into a COMDAT section.
  158. SmallVector<CVGlobalVariable, 1> ComdatVariables;
  159. // Array of non-COMDAT global variables.
  160. SmallVector<CVGlobalVariable, 1> GlobalVariables;
  161. /// The set of comdat .debug$S sections that we've seen so far. Each section
  162. /// must start with a magic version number that must only be emitted once.
  163. /// This set tracks which sections we've already opened.
  164. DenseSet<MCSectionCOFF *> ComdatDebugSections;
  165. /// Switch to the appropriate .debug$S section for GVSym. If GVSym, the symbol
  166. /// of an emitted global value, is in a comdat COFF section, this will switch
  167. /// to a new .debug$S section in that comdat. This method ensures that the
  168. /// section starts with the magic version number on first use. If GVSym is
  169. /// null, uses the main .debug$S section.
  170. void switchToDebugSectionForSymbol(const MCSymbol *GVSym);
  171. /// The next available function index for use with our .cv_* directives. Not
  172. /// to be confused with type indices for LF_FUNC_ID records.
  173. unsigned NextFuncId = 0;
  174. InlineSite &getInlineSite(const DILocation *InlinedAt,
  175. const DISubprogram *Inlinee);
  176. codeview::TypeIndex getFuncIdForSubprogram(const DISubprogram *SP);
  177. void calculateRanges(LocalVariable &Var,
  178. const DbgValueHistoryMap::Entries &Entries);
  179. static void collectInlineSiteChildren(SmallVectorImpl<unsigned> &Children,
  180. const FunctionInfo &FI,
  181. const InlineSite &Site);
  182. /// Remember some debug info about each function. Keep it in a stable order to
  183. /// emit at the end of the TU.
  184. MapVector<const Function *, std::unique_ptr<FunctionInfo>> FnDebugInfo;
  185. /// Map from full file path to .cv_file id. Full paths are built from DIFiles
  186. /// and are stored in FileToFilepathMap;
  187. DenseMap<StringRef, unsigned> FileIdMap;
  188. /// All inlined subprograms in the order they should be emitted.
  189. SmallSetVector<const DISubprogram *, 4> InlinedSubprograms;
  190. /// Map from a pair of DI metadata nodes and its DI type (or scope) that can
  191. /// be nullptr, to CodeView type indices. Primarily indexed by
  192. /// {DIType*, DIType*} and {DISubprogram*, DIType*}.
  193. ///
  194. /// The second entry in the key is needed for methods as DISubroutineType
  195. /// representing static method type are shared with non-method function type.
  196. DenseMap<std::pair<const DINode *, const DIType *>, codeview::TypeIndex>
  197. TypeIndices;
  198. /// Map from DICompositeType* to complete type index. Non-record types are
  199. /// always looked up in the normal TypeIndices map.
  200. DenseMap<const DICompositeType *, codeview::TypeIndex> CompleteTypeIndices;
  201. /// Complete record types to emit after all active type lowerings are
  202. /// finished.
  203. SmallVector<const DICompositeType *, 4> DeferredCompleteTypes;
  204. /// Number of type lowering frames active on the stack.
  205. unsigned TypeEmissionLevel = 0;
  206. codeview::TypeIndex VBPType;
  207. const DISubprogram *CurrentSubprogram = nullptr;
  208. // The UDTs we have seen while processing types; each entry is a pair of type
  209. // index and type name.
  210. std::vector<std::pair<std::string, const DIType *>> LocalUDTs;
  211. std::vector<std::pair<std::string, const DIType *>> GlobalUDTs;
  212. using FileToFilepathMapTy = std::map<const DIFile *, std::string>;
  213. FileToFilepathMapTy FileToFilepathMap;
  214. StringRef getFullFilepath(const DIFile *File);
  215. unsigned maybeRecordFile(const DIFile *F);
  216. void maybeRecordLocation(const DebugLoc &DL, const MachineFunction *MF);
  217. void clear();
  218. void setCurrentSubprogram(const DISubprogram *SP) {
  219. CurrentSubprogram = SP;
  220. LocalUDTs.clear();
  221. }
  222. /// Emit the magic version number at the start of a CodeView type or symbol
  223. /// section. Appears at the front of every .debug$S or .debug$T or .debug$P
  224. /// section.
  225. void emitCodeViewMagicVersion();
  226. void emitTypeInformation();
  227. void emitTypeGlobalHashes();
  228. void emitCompilerInformation();
  229. void emitBuildInfo();
  230. void emitInlineeLinesSubsection();
  231. void emitDebugInfoForThunk(const Function *GV,
  232. FunctionInfo &FI,
  233. const MCSymbol *Fn);
  234. void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
  235. void emitDebugInfoForRetainedTypes();
  236. void
  237. emitDebugInfoForUDTs(ArrayRef<std::pair<std::string, const DIType *>> UDTs);
  238. void emitDebugInfoForGlobals();
  239. void emitGlobalVariableList(ArrayRef<CVGlobalVariable> Globals);
  240. void emitDebugInfoForGlobal(const DIGlobalVariable *DIGV,
  241. const GlobalVariable *GV, MCSymbol *GVSym);
  242. /// Opens a subsection of the given kind in a .debug$S codeview section.
  243. /// Returns an end label for use with endCVSubsection when the subsection is
  244. /// finished.
  245. MCSymbol *beginCVSubsection(codeview::DebugSubsectionKind Kind);
  246. void endCVSubsection(MCSymbol *EndLabel);
  247. /// Opens a symbol record of the given kind. Returns an end label for use with
  248. /// endSymbolRecord.
  249. MCSymbol *beginSymbolRecord(codeview::SymbolKind Kind);
  250. void endSymbolRecord(MCSymbol *SymEnd);
  251. /// Emits an S_END, S_INLINESITE_END, or S_PROC_ID_END record. These records
  252. /// are empty, so we emit them with a simpler assembly sequence that doesn't
  253. /// involve labels.
  254. void emitEndSymbolRecord(codeview::SymbolKind EndKind);
  255. void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
  256. const InlineSite &Site);
  257. using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
  258. void collectGlobalVariableInfo();
  259. void collectVariableInfo(const DISubprogram *SP);
  260. void collectVariableInfoFromMFTable(DenseSet<InlinedEntity> &Processed);
  261. // Construct the lexical block tree for a routine, pruning emptpy lexical
  262. // scopes, and populate it with local variables.
  263. void collectLexicalBlockInfo(SmallVectorImpl<LexicalScope *> &Scopes,
  264. SmallVectorImpl<LexicalBlock *> &Blocks,
  265. SmallVectorImpl<LocalVariable> &Locals,
  266. SmallVectorImpl<CVGlobalVariable> &Globals);
  267. void collectLexicalBlockInfo(LexicalScope &Scope,
  268. SmallVectorImpl<LexicalBlock *> &ParentBlocks,
  269. SmallVectorImpl<LocalVariable> &ParentLocals,
  270. SmallVectorImpl<CVGlobalVariable> &ParentGlobals);
  271. /// Records information about a local variable in the appropriate scope. In
  272. /// particular, locals from inlined code live inside the inlining site.
  273. void recordLocalVariable(LocalVariable &&Var, const LexicalScope *LS);
  274. /// Emits local variables in the appropriate order.
  275. void emitLocalVariableList(const FunctionInfo &FI,
  276. ArrayRef<LocalVariable> Locals);
  277. /// Emits an S_LOCAL record and its associated defined ranges.
  278. void emitLocalVariable(const FunctionInfo &FI, const LocalVariable &Var);
  279. /// Emits a sequence of lexical block scopes and their children.
  280. void emitLexicalBlockList(ArrayRef<LexicalBlock *> Blocks,
  281. const FunctionInfo& FI);
  282. /// Emit a lexical block scope and its children.
  283. void emitLexicalBlock(const LexicalBlock &Block, const FunctionInfo& FI);
  284. /// Translates the DIType to codeview if necessary and returns a type index
  285. /// for it.
  286. codeview::TypeIndex getTypeIndex(const DIType *Ty,
  287. const DIType *ClassTy = nullptr);
  288. codeview::TypeIndex
  289. getTypeIndexForThisPtr(const DIDerivedType *PtrTy,
  290. const DISubroutineType *SubroutineTy);
  291. codeview::TypeIndex getTypeIndexForReferenceTo(const DIType *Ty);
  292. codeview::TypeIndex getMemberFunctionType(const DISubprogram *SP,
  293. const DICompositeType *Class);
  294. codeview::TypeIndex getScopeIndex(const DIScope *Scope);
  295. codeview::TypeIndex getVBPTypeIndex();
  296. void addToUDTs(const DIType *Ty);
  297. void addUDTSrcLine(const DIType *Ty, codeview::TypeIndex TI);
  298. codeview::TypeIndex lowerType(const DIType *Ty, const DIType *ClassTy);
  299. codeview::TypeIndex lowerTypeAlias(const DIDerivedType *Ty);
  300. codeview::TypeIndex lowerTypeArray(const DICompositeType *Ty);
  301. codeview::TypeIndex lowerTypeBasic(const DIBasicType *Ty);
  302. codeview::TypeIndex lowerTypePointer(
  303. const DIDerivedType *Ty,
  304. codeview::PointerOptions PO = codeview::PointerOptions::None);
  305. codeview::TypeIndex lowerTypeMemberPointer(
  306. const DIDerivedType *Ty,
  307. codeview::PointerOptions PO = codeview::PointerOptions::None);
  308. codeview::TypeIndex lowerTypeModifier(const DIDerivedType *Ty);
  309. codeview::TypeIndex lowerTypeFunction(const DISubroutineType *Ty);
  310. codeview::TypeIndex lowerTypeVFTableShape(const DIDerivedType *Ty);
  311. codeview::TypeIndex lowerTypeMemberFunction(
  312. const DISubroutineType *Ty, const DIType *ClassTy, int ThisAdjustment,
  313. bool IsStaticMethod,
  314. codeview::FunctionOptions FO = codeview::FunctionOptions::None);
  315. codeview::TypeIndex lowerTypeEnum(const DICompositeType *Ty);
  316. codeview::TypeIndex lowerTypeClass(const DICompositeType *Ty);
  317. codeview::TypeIndex lowerTypeUnion(const DICompositeType *Ty);
  318. /// Symbol records should point to complete types, but type records should
  319. /// always point to incomplete types to avoid cycles in the type graph. Only
  320. /// use this entry point when generating symbol records. The complete and
  321. /// incomplete type indices only differ for record types. All other types use
  322. /// the same index.
  323. codeview::TypeIndex getCompleteTypeIndex(const DIType *Ty);
  324. codeview::TypeIndex lowerCompleteTypeClass(const DICompositeType *Ty);
  325. codeview::TypeIndex lowerCompleteTypeUnion(const DICompositeType *Ty);
  326. struct TypeLoweringScope;
  327. void emitDeferredCompleteTypes();
  328. void collectMemberInfo(ClassInfo &Info, const DIDerivedType *DDTy);
  329. ClassInfo collectClassInfo(const DICompositeType *Ty);
  330. /// Common record member lowering functionality for record types, which are
  331. /// structs, classes, and unions. Returns the field list index and the member
  332. /// count.
  333. std::tuple<codeview::TypeIndex, codeview::TypeIndex, unsigned, bool>
  334. lowerRecordFieldList(const DICompositeType *Ty);
  335. /// Inserts {{Node, ClassTy}, TI} into TypeIndices and checks for duplicates.
  336. codeview::TypeIndex recordTypeIndexForDINode(const DINode *Node,
  337. codeview::TypeIndex TI,
  338. const DIType *ClassTy = nullptr);
  339. unsigned getPointerSizeInBytes();
  340. protected:
  341. /// Gather pre-function debug information.
  342. void beginFunctionImpl(const MachineFunction *MF) override;
  343. /// Gather post-function debug information.
  344. void endFunctionImpl(const MachineFunction *) override;
  345. public:
  346. CodeViewDebug(AsmPrinter *AP);
  347. void setSymbolSize(const MCSymbol *, uint64_t) override {}
  348. /// Emit the COFF section that holds the line table information.
  349. void endModule() override;
  350. /// Process beginning of an instruction.
  351. void beginInstruction(const MachineInstr *MI) override;
  352. };
  353. } // end namespace llvm
  354. #endif // LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H