CodeViewDebug.h 19 KB

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