1 //===- CodeView.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 // Defines constants and basic types describing CodeView debug information.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_DEBUGINFO_CODEVIEW_CODEVIEW_H
14 #define LLVM_DEBUGINFO_CODEVIEW_CODEVIEW_H
15 
16 #include <cinttypes>
17 #include <type_traits>
18 
19 #include "llvm/Support/Endian.h"
20 
21 namespace llvm {
22 namespace codeview {
23 
24 /// Distinguishes individual records in .debug$T or .debug$P section or PDB type
25 /// stream. The documentation and headers talk about this as the "leaf" type.
26 enum class TypeRecordKind : uint16_t {
27 #define TYPE_RECORD(lf_ename, value, name) name = value,
28 #include "CodeViewTypes.def"
29 };
30 
31 /// Duplicate copy of the above enum, but using the official CV names. Useful
32 /// for reference purposes and when dealing with unknown record types.
33 enum TypeLeafKind : uint16_t {
34 #define CV_TYPE(name, val) name = val,
35 #include "CodeViewTypes.def"
36 };
37 
38 /// Distinguishes individual records in the Symbols subsection of a .debug$S
39 /// section. Equivalent to SYM_ENUM_e in cvinfo.h.
40 enum class SymbolRecordKind : uint16_t {
41 #define SYMBOL_RECORD(lf_ename, value, name) name = value,
42 #include "CodeViewSymbols.def"
43 };
44 
45 /// Duplicate copy of the above enum, but using the official CV names. Useful
46 /// for reference purposes and when dealing with unknown record types.
47 enum SymbolKind : uint16_t {
48 #define CV_SYMBOL(name, val) name = val,
49 #include "CodeViewSymbols.def"
50 };
51 
52 #define CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(Class)                            \
53   inline Class operator|(Class a, Class b) {                                   \
54     return static_cast<Class>(static_cast<std::underlying_type_t<Class>>(a) |  \
55                               static_cast<std::underlying_type_t<Class>>(b));  \
56   }                                                                            \
57   inline Class operator&(Class a, Class b) {                                   \
58     return static_cast<Class>(static_cast<std::underlying_type_t<Class>>(a) &  \
59                               static_cast<std::underlying_type_t<Class>>(b));  \
60   }                                                                            \
61   inline Class operator~(Class a) {                                            \
62     return static_cast<Class>(~static_cast<std::underlying_type_t<Class>>(a)); \
63   }                                                                            \
64   inline Class &operator|=(Class &a, Class b) {                                \
65     a = a | b;                                                                 \
66     return a;                                                                  \
67   }                                                                            \
68   inline Class &operator&=(Class &a, Class b) {                                \
69     a = a & b;                                                                 \
70     return a;                                                                  \
71   }
72 
73 /// These values correspond to the CV_CPU_TYPE_e enumeration, and are documented
74 /// here: https://msdn.microsoft.com/en-us/library/b2fc64ek.aspx
75 enum class CPUType : uint16_t {
76   Intel8080 = 0x0,
77   Intel8086 = 0x1,
78   Intel80286 = 0x2,
79   Intel80386 = 0x3,
80   Intel80486 = 0x4,
81   Pentium = 0x5,
82   PentiumPro = 0x6,
83   Pentium3 = 0x7,
84   MIPS = 0x10,
85   MIPS16 = 0x11,
86   MIPS32 = 0x12,
87   MIPS64 = 0x13,
88   MIPSI = 0x14,
89   MIPSII = 0x15,
90   MIPSIII = 0x16,
91   MIPSIV = 0x17,
92   MIPSV = 0x18,
93   M68000 = 0x20,
94   M68010 = 0x21,
95   M68020 = 0x22,
96   M68030 = 0x23,
97   M68040 = 0x24,
98   Alpha = 0x30,
99   Alpha21164 = 0x31,
100   Alpha21164A = 0x32,
101   Alpha21264 = 0x33,
102   Alpha21364 = 0x34,
103   PPC601 = 0x40,
104   PPC603 = 0x41,
105   PPC604 = 0x42,
106   PPC620 = 0x43,
107   PPCFP = 0x44,
108   PPCBE = 0x45,
109   SH3 = 0x50,
110   SH3E = 0x51,
111   SH3DSP = 0x52,
112   SH4 = 0x53,
113   SHMedia = 0x54,
114   ARM3 = 0x60,
115   ARM4 = 0x61,
116   ARM4T = 0x62,
117   ARM5 = 0x63,
118   ARM5T = 0x64,
119   ARM6 = 0x65,
120   ARM_XMAC = 0x66,
121   ARM_WMMX = 0x67,
122   ARM7 = 0x68,
123   Omni = 0x70,
124   Ia64 = 0x80,
125   Ia64_2 = 0x81,
126   CEE = 0x90,
127   AM33 = 0xa0,
128   M32R = 0xb0,
129   TriCore = 0xc0,
130   X64 = 0xd0,
131   EBC = 0xe0,
132   Thumb = 0xf0,
133   ARMNT = 0xf4,
134   ARM64 = 0xf6,
135   HybridX86ARM64 = 0xf7,
136   ARM64EC = 0xf8,
137   ARM64X = 0xf9,
138   D3D11_Shader = 0x100,
139 };
140 
141 /// These values correspond to the CV_CFL_LANG enumeration, and are documented
142 /// here: https://msdn.microsoft.com/en-us/library/bw3aekw6.aspx
143 enum SourceLanguage : uint8_t {
144   C = 0x00,
145   Cpp = 0x01,
146   Fortran = 0x02,
147   Masm = 0x03,
148   Pascal = 0x04,
149   Basic = 0x05,
150   Cobol = 0x06,
151   Link = 0x07,
152   Cvtres = 0x08,
153   Cvtpgd = 0x09,
154   CSharp = 0x0a,
155   VB = 0x0b,
156   ILAsm = 0x0c,
157   Java = 0x0d,
158   JScript = 0x0e,
159   MSIL = 0x0f,
160   HLSL = 0x10,
161 
162   Rust = 0x15,
163 
164   /// The DMD & Swift compilers emit 'D' and 'S', respectively, for the CV
165   /// source language. Microsoft does not have enumerators for them yet.
166   D = 'D',
167   Swift = 'S',
168 };
169 
170 /// These values correspond to the CV_call_e enumeration, and are documented
171 /// at the following locations:
172 ///   https://msdn.microsoft.com/en-us/library/b2fc64ek.aspx
173 ///   https://msdn.microsoft.com/en-us/library/windows/desktop/ms680207(v=vs.85).aspx
174 ///
175 enum class CallingConvention : uint8_t {
176   NearC = 0x00,       // near right to left push, caller pops stack
177   FarC = 0x01,        // far right to left push, caller pops stack
178   NearPascal = 0x02,  // near left to right push, callee pops stack
179   FarPascal = 0x03,   // far left to right push, callee pops stack
180   NearFast = 0x04,    // near left to right push with regs, callee pops stack
181   FarFast = 0x05,     // far left to right push with regs, callee pops stack
182   NearStdCall = 0x07, // near standard call
183   FarStdCall = 0x08,  // far standard call
184   NearSysCall = 0x09, // near sys call
185   FarSysCall = 0x0a,  // far sys call
186   ThisCall = 0x0b,    // this call (this passed in register)
187   MipsCall = 0x0c,    // Mips call
188   Generic = 0x0d,     // Generic call sequence
189   AlphaCall = 0x0e,   // Alpha call
190   PpcCall = 0x0f,     // PPC call
191   SHCall = 0x10,      // Hitachi SuperH call
192   ArmCall = 0x11,     // ARM call
193   AM33Call = 0x12,    // AM33 call
194   TriCall = 0x13,     // TriCore Call
195   SH5Call = 0x14,     // Hitachi SuperH-5 call
196   M32RCall = 0x15,    // M32R Call
197   ClrCall = 0x16,     // clr call
198   Inline =
199       0x17, // Marker for routines always inlined and thus lacking a convention
200   NearVector = 0x18 // near left to right push with regs, callee pops stack
201 };
202 
203 enum class ClassOptions : uint16_t {
204   None = 0x0000,
205   Packed = 0x0001,
206   HasConstructorOrDestructor = 0x0002,
207   HasOverloadedOperator = 0x0004,
208   Nested = 0x0008,
209   ContainsNestedClass = 0x0010,
210   HasOverloadedAssignmentOperator = 0x0020,
211   HasConversionOperator = 0x0040,
212   ForwardReference = 0x0080,
213   Scoped = 0x0100,
214   HasUniqueName = 0x0200,
215   Sealed = 0x0400,
216   Intrinsic = 0x2000
217 };
218 CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(ClassOptions)
219 
220 enum class FrameProcedureOptions : uint32_t {
221   None = 0x00000000,
222   HasAlloca = 0x00000001,
223   HasSetJmp = 0x00000002,
224   HasLongJmp = 0x00000004,
225   HasInlineAssembly = 0x00000008,
226   HasExceptionHandling = 0x00000010,
227   MarkedInline = 0x00000020,
228   HasStructuredExceptionHandling = 0x00000040,
229   Naked = 0x00000080,
230   SecurityChecks = 0x00000100,
231   AsynchronousExceptionHandling = 0x00000200,
232   NoStackOrderingForSecurityChecks = 0x00000400,
233   Inlined = 0x00000800,
234   StrictSecurityChecks = 0x00001000,
235   SafeBuffers = 0x00002000,
236   EncodedLocalBasePointerMask = 0x0000C000,
237   EncodedParamBasePointerMask = 0x00030000,
238   ProfileGuidedOptimization = 0x00040000,
239   ValidProfileCounts = 0x00080000,
240   OptimizedForSpeed = 0x00100000,
241   GuardCfg = 0x00200000,
242   GuardCfw = 0x00400000
243 };
244 CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(FrameProcedureOptions)
245 
246 enum class FunctionOptions : uint8_t {
247   None = 0x00,
248   CxxReturnUdt = 0x01,
249   Constructor = 0x02,
250   ConstructorWithVirtualBases = 0x04
251 };
252 CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(FunctionOptions)
253 
254 enum class HfaKind : uint8_t {
255   None = 0x00,
256   Float = 0x01,
257   Double = 0x02,
258   Other = 0x03
259 };
260 
261 /// Source-level access specifier. (CV_access_e)
262 enum class MemberAccess : uint8_t {
263   None = 0,
264   Private = 1,
265   Protected = 2,
266   Public = 3
267 };
268 
269 /// Part of member attribute flags. (CV_methodprop_e)
270 enum class MethodKind : uint8_t {
271   Vanilla = 0x00,
272   Virtual = 0x01,
273   Static = 0x02,
274   Friend = 0x03,
275   IntroducingVirtual = 0x04,
276   PureVirtual = 0x05,
277   PureIntroducingVirtual = 0x06
278 };
279 
280 /// Equivalent to CV_fldattr_t bitfield.
281 enum class MethodOptions : uint16_t {
282   None = 0x0000,
283   AccessMask = 0x0003,
284   MethodKindMask = 0x001c,
285   Pseudo = 0x0020,
286   NoInherit = 0x0040,
287   NoConstruct = 0x0080,
288   CompilerGenerated = 0x0100,
289   Sealed = 0x0200
290 };
291 CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(MethodOptions)
292 
293 /// Equivalent to CV_LABEL_TYPE_e.
294 enum class LabelType : uint16_t {
295   Near = 0x0,
296   Far = 0x4,
297 };
298 
299 /// Equivalent to CV_modifier_t.
300 /// TODO: Add flag for _Atomic modifier
301 enum class ModifierOptions : uint16_t {
302   None = 0x0000,
303   Const = 0x0001,
304   Volatile = 0x0002,
305   Unaligned = 0x0004
306 };
307 CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(ModifierOptions)
308 
309 // If the subsection kind has this bit set, then the linker should ignore it.
310 enum : uint32_t { SubsectionIgnoreFlag = 0x80000000 };
311 
312 enum class DebugSubsectionKind : uint32_t {
313   None = 0,
314   Symbols = 0xf1,
315   Lines = 0xf2,
316   StringTable = 0xf3,
317   FileChecksums = 0xf4,
318   FrameData = 0xf5,
319   InlineeLines = 0xf6,
320   CrossScopeImports = 0xf7,
321   CrossScopeExports = 0xf8,
322 
323   // These appear to relate to .Net assembly info.
324   ILLines = 0xf9,
325   FuncMDTokenMap = 0xfa,
326   TypeMDTokenMap = 0xfb,
327   MergedAssemblyInput = 0xfc,
328 
329   CoffSymbolRVA = 0xfd,
330 
331   XfgHashType = 0xff,
332   XfgHashVirtual = 0x100,
333 };
334 
335 /// Equivalent to CV_ptrtype_e.
336 enum class PointerKind : uint8_t {
337   Near16 = 0x00,                // 16 bit pointer
338   Far16 = 0x01,                 // 16:16 far pointer
339   Huge16 = 0x02,                // 16:16 huge pointer
340   BasedOnSegment = 0x03,        // based on segment
341   BasedOnValue = 0x04,          // based on value of base
342   BasedOnSegmentValue = 0x05,   // based on segment value of base
343   BasedOnAddress = 0x06,        // based on address of base
344   BasedOnSegmentAddress = 0x07, // based on segment address of base
345   BasedOnType = 0x08,           // based on type
346   BasedOnSelf = 0x09,           // based on self
347   Near32 = 0x0a,                // 32 bit pointer
348   Far32 = 0x0b,                 // 16:32 pointer
349   Near64 = 0x0c                 // 64 bit pointer
350 };
351 
352 /// Equivalent to CV_ptrmode_e.
353 enum class PointerMode : uint8_t {
354   Pointer = 0x00,                 // "normal" pointer
355   LValueReference = 0x01,         // "old" reference
356   PointerToDataMember = 0x02,     // pointer to data member
357   PointerToMemberFunction = 0x03, // pointer to member function
358   RValueReference = 0x04          // r-value reference
359 };
360 
361 /// Equivalent to misc lfPointerAttr bitfields.
362 enum class PointerOptions : uint32_t {
363   None = 0x00000000,
364   Flat32 = 0x00000100,
365   Volatile = 0x00000200,
366   Const = 0x00000400,
367   Unaligned = 0x00000800,
368   Restrict = 0x00001000,
369   WinRTSmartPointer = 0x00080000,
370   LValueRefThisPointer = 0x00100000,
371   RValueRefThisPointer = 0x00200000
372 };
373 CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(PointerOptions)
374 
375 /// Equivalent to CV_pmtype_e.
376 enum class PointerToMemberRepresentation : uint16_t {
377   Unknown = 0x00,                     // not specified (pre VC8)
378   SingleInheritanceData = 0x01,       // member data, single inheritance
379   MultipleInheritanceData = 0x02,     // member data, multiple inheritance
380   VirtualInheritanceData = 0x03,      // member data, virtual inheritance
381   GeneralData = 0x04,                 // member data, most general
382   SingleInheritanceFunction = 0x05,   // member function, single inheritance
383   MultipleInheritanceFunction = 0x06, // member function, multiple inheritance
384   VirtualInheritanceFunction = 0x07,  // member function, virtual inheritance
385   GeneralFunction = 0x08              // member function, most general
386 };
387 
388 enum class VFTableSlotKind : uint8_t {
389   Near16 = 0x00,
390   Far16 = 0x01,
391   This = 0x02,
392   Outer = 0x03,
393   Meta = 0x04,
394   Near = 0x05,
395   Far = 0x06
396 };
397 
398 enum class WindowsRTClassKind : uint8_t {
399   None = 0x00,
400   RefClass = 0x01,
401   ValueClass = 0x02,
402   Interface = 0x03
403 };
404 
405 /// Corresponds to CV_LVARFLAGS bitfield.
406 enum class LocalSymFlags : uint16_t {
407   None = 0,
408   IsParameter = 1 << 0,
409   IsAddressTaken = 1 << 1,
410   IsCompilerGenerated = 1 << 2,
411   IsAggregate = 1 << 3,
412   IsAggregated = 1 << 4,
413   IsAliased = 1 << 5,
414   IsAlias = 1 << 6,
415   IsReturnValue = 1 << 7,
416   IsOptimizedOut = 1 << 8,
417   IsEnregisteredGlobal = 1 << 9,
418   IsEnregisteredStatic = 1 << 10,
419 };
420 CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(LocalSymFlags)
421 
422 /// Corresponds to the CV_PUBSYMFLAGS bitfield.
423 enum class PublicSymFlags : uint32_t {
424   None = 0,
425   Code = 1 << 0,
426   Function = 1 << 1,
427   Managed = 1 << 2,
428   MSIL = 1 << 3,
429 };
430 CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(PublicSymFlags)
431 
432 /// Corresponds to the CV_PROCFLAGS bitfield.
433 enum class ProcSymFlags : uint8_t {
434   None = 0,
435   HasFP = 1 << 0,
436   HasIRET = 1 << 1,
437   HasFRET = 1 << 2,
438   IsNoReturn = 1 << 3,
439   IsUnreachable = 1 << 4,
440   HasCustomCallingConv = 1 << 5,
441   IsNoInline = 1 << 6,
442   HasOptimizedDebugInfo = 1 << 7,
443 };
444 CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(ProcSymFlags)
445 
446 /// Corresponds to COMPILESYM2::Flags bitfield.
447 enum class CompileSym2Flags : uint32_t {
448   None = 0,
449   SourceLanguageMask = 0xFF,
450   EC = 1 << 8,
451   NoDbgInfo = 1 << 9,
452   LTCG = 1 << 10,
453   NoDataAlign = 1 << 11,
454   ManagedPresent = 1 << 12,
455   SecurityChecks = 1 << 13,
456   HotPatch = 1 << 14,
457   CVTCIL = 1 << 15,
458   MSILModule = 1 << 16,
459 };
460 CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(CompileSym2Flags)
461 
462 /// Corresponds to COMPILESYM3::Flags bitfield.
463 enum class CompileSym3Flags : uint32_t {
464   None = 0,
465   SourceLanguageMask = 0xFF,
466   EC = 1 << 8,
467   NoDbgInfo = 1 << 9,
468   LTCG = 1 << 10,
469   NoDataAlign = 1 << 11,
470   ManagedPresent = 1 << 12,
471   SecurityChecks = 1 << 13,
472   HotPatch = 1 << 14,
473   CVTCIL = 1 << 15,
474   MSILModule = 1 << 16,
475   Sdl = 1 << 17,
476   PGO = 1 << 18,
477   Exp = 1 << 19,
478 };
479 CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(CompileSym3Flags)
480 
481 enum class ExportFlags : uint16_t {
482   None = 0,
483   IsConstant = 1 << 0,
484   IsData = 1 << 1,
485   IsPrivate = 1 << 2,
486   HasNoName = 1 << 3,
487   HasExplicitOrdinal = 1 << 4,
488   IsForwarder = 1 << 5
489 };
490 CV_DEFINE_ENUM_CLASS_FLAGS_OPERATORS(ExportFlags)
491 
492 // Corresponds to BinaryAnnotationOpcode enum.
493 enum class BinaryAnnotationsOpCode : uint32_t {
494   Invalid,
495   CodeOffset,
496   ChangeCodeOffsetBase,
497   ChangeCodeOffset,
498   ChangeCodeLength,
499   ChangeFile,
500   ChangeLineOffset,
501   ChangeLineEndDelta,
502   ChangeRangeKind,
503   ChangeColumnStart,
504   ChangeColumnEndDelta,
505   ChangeCodeOffsetAndLineOffset,
506   ChangeCodeLengthAndCodeOffset,
507   ChangeColumnEnd,
508 };
509 
510 // Corresponds to CV_cookietype_e enum.
511 enum class FrameCookieKind : uint8_t {
512   Copy,
513   XorStackPointer,
514   XorFramePointer,
515   XorR13,
516 };
517 
518 // Corresponds to CV_HREG_e enum.
519 enum class RegisterId : uint16_t {
520 #define CV_REGISTERS_ALL
521 #define CV_REGISTER(name, value) name = value,
522 #include "CodeViewRegisters.def"
523 #undef CV_REGISTER
524 #undef CV_REGISTERS_ALL
525 };
526 
527 // Register Ids are shared between architectures in CodeView. CPUType is needed
528 // to map register Id to name.
529 struct CPURegister {
530   CPURegister() = delete;
CPURegisterCPURegister531   CPURegister(CPUType Cpu, codeview::RegisterId Reg) {
532     this->Cpu = Cpu;
533     this->Reg = Reg;
534   }
535   CPUType Cpu;
536   RegisterId Reg;
537 };
538 
539 /// Two-bit value indicating which register is the designated frame pointer
540 /// register. Appears in the S_FRAMEPROC record flags.
541 enum class EncodedFramePtrReg : uint8_t {
542   None = 0,
543   StackPtr = 1,
544   FramePtr = 2,
545   BasePtr = 3,
546 };
547 
548 RegisterId decodeFramePtrReg(EncodedFramePtrReg EncodedReg, CPUType CPU);
549 
550 EncodedFramePtrReg encodeFramePtrReg(RegisterId Reg, CPUType CPU);
551 
552 /// These values correspond to the THUNK_ORDINAL enumeration.
553 enum class ThunkOrdinal : uint8_t {
554   Standard,
555   ThisAdjustor,
556   Vcall,
557   Pcode,
558   UnknownLoad,
559   TrampIncremental,
560   BranchIsland
561 };
562 
563 enum class TrampolineType : uint16_t { TrampIncremental, BranchIsland };
564 
565 // These values correspond to the CV_SourceChksum_t enumeration.
566 enum class FileChecksumKind : uint8_t { None, MD5, SHA1, SHA256 };
567 
568 enum LineFlags : uint16_t {
569   LF_None = 0,
570   LF_HaveColumns = 1, // CV_LINES_HAVE_COLUMNS
571 };
572 
573 /// Data in the SUBSEC_FRAMEDATA subection.
574 struct FrameData {
575   support::ulittle32_t RvaStart;
576   support::ulittle32_t CodeSize;
577   support::ulittle32_t LocalSize;
578   support::ulittle32_t ParamsSize;
579   support::ulittle32_t MaxStackSize;
580   support::ulittle32_t FrameFunc;
581   support::ulittle16_t PrologSize;
582   support::ulittle16_t SavedRegsSize;
583   support::ulittle32_t Flags;
584   enum : uint32_t {
585     HasSEH = 1 << 0,
586     HasEH = 1 << 1,
587     IsFunctionStart = 1 << 2,
588   };
589 };
590 
591 // Corresponds to LocalIdAndGlobalIdPair structure.
592 // This structure information allows cross-referencing between PDBs.  For
593 // example, when a PDB is being built during compilation it is not yet known
594 // what other modules may end up in the PDB at link time.  So certain types of
595 // IDs may clash between the various compile time PDBs.  For each affected
596 // module, a subsection would be put into the PDB containing a mapping from its
597 // local IDs to a single ID namespace for all items in the PDB file.
598 struct CrossModuleExport {
599   support::ulittle32_t Local;
600   support::ulittle32_t Global;
601 };
602 
603 struct CrossModuleImport {
604   support::ulittle32_t ModuleNameOffset;
605   support::ulittle32_t Count; // Number of elements
606   // support::ulittle32_t ids[Count]; // id from referenced module
607 };
608 
609 enum class CodeViewContainer { ObjectFile, Pdb };
610 
alignOf(CodeViewContainer Container)611 inline uint32_t alignOf(CodeViewContainer Container) {
612   if (Container == CodeViewContainer::ObjectFile)
613     return 1;
614   return 4;
615 }
616 }
617 }
618 
619 #endif
620