CodeViewDebug.h 19 KB

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