1 //===-- llvm/BinaryFormat/Dwarf.h ---Dwarf Constants-------------*- 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 /// \file
10 /// This file contains constants used for implementing Dwarf
11 /// debug support.
12 ///
13 /// For details on the Dwarf specfication see the latest DWARF Debugging
14 /// Information Format standard document on http://www.dwarfstd.org. This
15 /// file often includes support for non-released standard features.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_BINARYFORMAT_DWARF_H
20 #define LLVM_BINARYFORMAT_DWARF_H
21 
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/DataTypes.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/Support/FormatVariadicDetails.h"
28 #include "llvm/ADT/Triple.h"
29 
30 namespace llvm {
31 class StringRef;
32 
33 namespace dwarf {
34 
35 //===----------------------------------------------------------------------===//
36 // DWARF constants as gleaned from the DWARF Debugging Information Format V.5
37 // reference manual http://www.dwarfstd.org/.
38 //
39 
40 // Do not mix the following two enumerations sets.  DW_TAG_invalid changes the
41 // enumeration base type.
42 
43 enum LLVMConstants : uint32_t {
44   // LLVM mock tags (see also llvm/BinaryFormat/Dwarf.def).
45   DW_TAG_invalid = ~0U,        // Tag for invalid results.
46   DW_VIRTUALITY_invalid = ~0U, // Virtuality for invalid results.
47   DW_MACINFO_invalid = ~0U,    // Macinfo type for invalid results.
48 
49   // Special values for an initial length field.
50   DW_LENGTH_lo_reserved = 0xfffffff0, // Lower bound of the reserved range.
51   DW_LENGTH_DWARF64 = 0xffffffff,     // Indicator of 64-bit DWARF format.
52   DW_LENGTH_hi_reserved = 0xffffffff, // Upper bound of the reserved range.
53 
54   // Other constants.
55   DWARF_VERSION = 4,       // Default dwarf version we output.
56   DW_PUBTYPES_VERSION = 2, // Section version number for .debug_pubtypes.
57   DW_PUBNAMES_VERSION = 2, // Section version number for .debug_pubnames.
58   DW_ARANGES_VERSION = 2,  // Section version number for .debug_aranges.
59   // Identifiers we use to distinguish vendor extensions.
60   DWARF_VENDOR_DWARF = 0, // Defined in v2 or later of the DWARF standard.
61   DWARF_VENDOR_APPLE = 1,
62   DWARF_VENDOR_BORLAND = 2,
63   DWARF_VENDOR_GNU = 3,
64   DWARF_VENDOR_GOOGLE = 4,
65   DWARF_VENDOR_LLVM = 5,
66   DWARF_VENDOR_MIPS = 6,
67   DWARF_VENDOR_WASM = 7
68 };
69 
70 /// Constants that define the DWARF format as 32 or 64 bit.
71 enum DwarfFormat : uint8_t { DWARF32, DWARF64 };
72 
73 /// Special ID values that distinguish a CIE from a FDE in DWARF CFI.
74 /// Not inside an enum because a 64-bit value is needed.
75 /// @{
76 const uint32_t DW_CIE_ID = UINT32_MAX;
77 const uint64_t DW64_CIE_ID = UINT64_MAX;
78 /// @}
79 
80 /// Identifier of an invalid DIE offset in the .debug_info section.
81 const uint32_t DW_INVALID_OFFSET = UINT32_MAX;
82 
83 enum Tag : uint16_t {
84 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) DW_TAG_##NAME = ID,
85 #include "llvm/BinaryFormat/Dwarf.def"
86   DW_TAG_lo_user = 0x4080,
87   DW_TAG_hi_user = 0xffff,
88   DW_TAG_user_base = 0x1000 ///< Recommended base for user tags.
89 };
90 
isType(Tag T)91 inline bool isType(Tag T) {
92   switch (T) {
93   default:
94     return false;
95 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND)                         \
96   case DW_TAG_##NAME:                                                          \
97     return (KIND == DW_KIND_TYPE);
98 #include "llvm/BinaryFormat/Dwarf.def"
99   }
100 }
101 
102 /// Attributes.
103 enum Attribute : uint16_t {
104 #define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) DW_AT_##NAME = ID,
105 #include "llvm/BinaryFormat/Dwarf.def"
106   DW_AT_lo_user = 0x2000,
107   DW_AT_hi_user = 0x3fff,
108 };
109 
110 enum Form : uint16_t {
111 #define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) DW_FORM_##NAME = ID,
112 #include "llvm/BinaryFormat/Dwarf.def"
113   DW_FORM_lo_user = 0x1f00, ///< Not specified by DWARF.
114 };
115 
116 enum LocationAtom {
117 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) DW_OP_##NAME = ID,
118 #include "llvm/BinaryFormat/Dwarf.def"
119   DW_OP_lo_user = 0xe0,
120   DW_OP_hi_user = 0xff,
121   DW_OP_LLVM_fragment = 0x1000,    ///< Only used in LLVM metadata.
122   DW_OP_LLVM_convert = 0x1001,     ///< Only used in LLVM metadata.
123   DW_OP_LLVM_tag_offset = 0x1002,  ///< Only used in LLVM metadata.
124   DW_OP_LLVM_entry_value = 0x1003, ///< Only used in LLVM metadata.
125 };
126 
127 enum TypeKind : uint8_t {
128 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) DW_ATE_##NAME = ID,
129 #include "llvm/BinaryFormat/Dwarf.def"
130   DW_ATE_lo_user = 0x80,
131   DW_ATE_hi_user = 0xff
132 };
133 
134 enum DecimalSignEncoding {
135   // Decimal sign attribute values
136   DW_DS_unsigned = 0x01,
137   DW_DS_leading_overpunch = 0x02,
138   DW_DS_trailing_overpunch = 0x03,
139   DW_DS_leading_separate = 0x04,
140   DW_DS_trailing_separate = 0x05
141 };
142 
143 enum EndianityEncoding {
144   // Endianity attribute values
145 #define HANDLE_DW_END(ID, NAME) DW_END_##NAME = ID,
146 #include "llvm/BinaryFormat/Dwarf.def"
147   DW_END_lo_user = 0x40,
148   DW_END_hi_user = 0xff
149 };
150 
151 enum AccessAttribute {
152   // Accessibility codes
153   DW_ACCESS_public = 0x01,
154   DW_ACCESS_protected = 0x02,
155   DW_ACCESS_private = 0x03
156 };
157 
158 enum VisibilityAttribute {
159   // Visibility codes
160   DW_VIS_local = 0x01,
161   DW_VIS_exported = 0x02,
162   DW_VIS_qualified = 0x03
163 };
164 
165 enum VirtualityAttribute {
166 #define HANDLE_DW_VIRTUALITY(ID, NAME) DW_VIRTUALITY_##NAME = ID,
167 #include "llvm/BinaryFormat/Dwarf.def"
168   DW_VIRTUALITY_max = 0x02
169 };
170 
171 enum DefaultedMemberAttribute {
172 #define HANDLE_DW_DEFAULTED(ID, NAME) DW_DEFAULTED_##NAME = ID,
173 #include "llvm/BinaryFormat/Dwarf.def"
174   DW_DEFAULTED_max = 0x02
175 };
176 
177 enum SourceLanguage {
178 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR)                 \
179   DW_LANG_##NAME = ID,
180 #include "llvm/BinaryFormat/Dwarf.def"
181   DW_LANG_lo_user = 0x8000,
182   DW_LANG_hi_user = 0xffff
183 };
184 
isCPlusPlus(SourceLanguage S)185 inline bool isCPlusPlus(SourceLanguage S) {
186   // Deliberately enumerate all the language options so we get a warning when
187   // new language options are added (-Wswitch) that'll hopefully help keep this
188   // switch up-to-date when new C++ versions are added.
189   switch (S) {
190   case DW_LANG_C_plus_plus:
191   case DW_LANG_C_plus_plus_03:
192   case DW_LANG_C_plus_plus_11:
193   case DW_LANG_C_plus_plus_14:
194     return true;
195   case DW_LANG_C89:
196   case DW_LANG_C:
197   case DW_LANG_Ada83:
198   case DW_LANG_Cobol74:
199   case DW_LANG_Cobol85:
200   case DW_LANG_Fortran77:
201   case DW_LANG_Fortran90:
202   case DW_LANG_Pascal83:
203   case DW_LANG_Modula2:
204   case DW_LANG_Java:
205   case DW_LANG_C99:
206   case DW_LANG_Ada95:
207   case DW_LANG_Fortran95:
208   case DW_LANG_PLI:
209   case DW_LANG_ObjC:
210   case DW_LANG_ObjC_plus_plus:
211   case DW_LANG_UPC:
212   case DW_LANG_D:
213   case DW_LANG_Python:
214   case DW_LANG_OpenCL:
215   case DW_LANG_Go:
216   case DW_LANG_Modula3:
217   case DW_LANG_Haskell:
218   case DW_LANG_OCaml:
219   case DW_LANG_Rust:
220   case DW_LANG_C11:
221   case DW_LANG_Swift:
222   case DW_LANG_Julia:
223   case DW_LANG_Dylan:
224   case DW_LANG_Fortran03:
225   case DW_LANG_Fortran08:
226   case DW_LANG_RenderScript:
227   case DW_LANG_BLISS:
228   case DW_LANG_Mips_Assembler:
229   case DW_LANG_GOOGLE_RenderScript:
230   case DW_LANG_BORLAND_Delphi:
231   case DW_LANG_lo_user:
232   case DW_LANG_hi_user:
233     return false;
234   }
235   llvm_unreachable("Invalid source language");
236 }
237 
238 enum CaseSensitivity {
239   // Identifier case codes
240   DW_ID_case_sensitive = 0x00,
241   DW_ID_up_case = 0x01,
242   DW_ID_down_case = 0x02,
243   DW_ID_case_insensitive = 0x03
244 };
245 
246 enum CallingConvention {
247 // Calling convention codes
248 #define HANDLE_DW_CC(ID, NAME) DW_CC_##NAME = ID,
249 #include "llvm/BinaryFormat/Dwarf.def"
250   DW_CC_lo_user = 0x40,
251   DW_CC_hi_user = 0xff
252 };
253 
254 enum InlineAttribute {
255   // Inline codes
256   DW_INL_not_inlined = 0x00,
257   DW_INL_inlined = 0x01,
258   DW_INL_declared_not_inlined = 0x02,
259   DW_INL_declared_inlined = 0x03
260 };
261 
262 enum ArrayDimensionOrdering {
263   // Array ordering
264   DW_ORD_row_major = 0x00,
265   DW_ORD_col_major = 0x01
266 };
267 
268 enum DiscriminantList {
269   // Discriminant descriptor values
270   DW_DSC_label = 0x00,
271   DW_DSC_range = 0x01
272 };
273 
274 /// Line Number Standard Opcode Encodings.
275 enum LineNumberOps : uint8_t {
276 #define HANDLE_DW_LNS(ID, NAME) DW_LNS_##NAME = ID,
277 #include "llvm/BinaryFormat/Dwarf.def"
278 };
279 
280 /// Line Number Extended Opcode Encodings.
281 enum LineNumberExtendedOps {
282 #define HANDLE_DW_LNE(ID, NAME) DW_LNE_##NAME = ID,
283 #include "llvm/BinaryFormat/Dwarf.def"
284   DW_LNE_lo_user = 0x80,
285   DW_LNE_hi_user = 0xff
286 };
287 
288 enum LineNumberEntryFormat {
289 #define HANDLE_DW_LNCT(ID, NAME) DW_LNCT_##NAME = ID,
290 #include "llvm/BinaryFormat/Dwarf.def"
291   DW_LNCT_lo_user = 0x2000,
292   DW_LNCT_hi_user = 0x3fff,
293 };
294 
295 enum MacinfoRecordType {
296   // Macinfo Type Encodings
297   DW_MACINFO_define = 0x01,
298   DW_MACINFO_undef = 0x02,
299   DW_MACINFO_start_file = 0x03,
300   DW_MACINFO_end_file = 0x04,
301   DW_MACINFO_vendor_ext = 0xff
302 };
303 
304 /// DWARF v5 macro information entry type encodings.
305 enum MacroEntryType {
306 #define HANDLE_DW_MACRO(ID, NAME) DW_MACRO_##NAME = ID,
307 #include "llvm/BinaryFormat/Dwarf.def"
308   DW_MACRO_lo_user = 0xe0,
309   DW_MACRO_hi_user = 0xff
310 };
311 
312 /// DWARF v5 range list entry encoding values.
313 enum RnglistEntries {
314 #define HANDLE_DW_RLE(ID, NAME) DW_RLE_##NAME = ID,
315 #include "llvm/BinaryFormat/Dwarf.def"
316 };
317 
318 /// DWARF v5 loc list entry encoding values.
319 enum LoclistEntries {
320 #define HANDLE_DW_LLE(ID, NAME) DW_LLE_##NAME = ID,
321 #include "llvm/BinaryFormat/Dwarf.def"
322 };
323 
324 /// Call frame instruction encodings.
325 enum CallFrameInfo {
326 #define HANDLE_DW_CFA(ID, NAME) DW_CFA_##NAME = ID,
327 #define HANDLE_DW_CFA_PRED(ID, NAME, ARCH) DW_CFA_##NAME = ID,
328 #include "llvm/BinaryFormat/Dwarf.def"
329   DW_CFA_extended = 0x00,
330 
331   DW_CFA_lo_user = 0x1c,
332   DW_CFA_hi_user = 0x3f
333 };
334 
335 enum Constants {
336   // Children flag
337   DW_CHILDREN_no = 0x00,
338   DW_CHILDREN_yes = 0x01,
339 
340   DW_EH_PE_absptr = 0x00,
341   DW_EH_PE_omit = 0xff,
342   DW_EH_PE_uleb128 = 0x01,
343   DW_EH_PE_udata2 = 0x02,
344   DW_EH_PE_udata4 = 0x03,
345   DW_EH_PE_udata8 = 0x04,
346   DW_EH_PE_sleb128 = 0x09,
347   DW_EH_PE_sdata2 = 0x0A,
348   DW_EH_PE_sdata4 = 0x0B,
349   DW_EH_PE_sdata8 = 0x0C,
350   DW_EH_PE_signed = 0x08,
351   DW_EH_PE_pcrel = 0x10,
352   DW_EH_PE_textrel = 0x20,
353   DW_EH_PE_datarel = 0x30,
354   DW_EH_PE_funcrel = 0x40,
355   DW_EH_PE_aligned = 0x50,
356   DW_EH_PE_indirect = 0x80
357 };
358 
359 /// Constants for the DW_APPLE_PROPERTY_attributes attribute.
360 /// Keep this list in sync with clang's DeclObjCCommon.h
361 /// ObjCPropertyAttribute::Kind!
362 enum ApplePropertyAttributes {
363 #define HANDLE_DW_APPLE_PROPERTY(ID, NAME) DW_APPLE_PROPERTY_##NAME = ID,
364 #include "llvm/BinaryFormat/Dwarf.def"
365 };
366 
367 /// Constants for unit types in DWARF v5.
368 enum UnitType : unsigned char {
369 #define HANDLE_DW_UT(ID, NAME) DW_UT_##NAME = ID,
370 #include "llvm/BinaryFormat/Dwarf.def"
371   DW_UT_lo_user = 0x80,
372   DW_UT_hi_user = 0xff
373 };
374 
375 enum Index {
376 #define HANDLE_DW_IDX(ID, NAME) DW_IDX_##NAME = ID,
377 #include "llvm/BinaryFormat/Dwarf.def"
378   DW_IDX_lo_user = 0x2000,
379   DW_IDX_hi_user = 0x3fff
380 };
381 
isUnitType(uint8_t UnitType)382 inline bool isUnitType(uint8_t UnitType) {
383   switch (UnitType) {
384   case DW_UT_compile:
385   case DW_UT_type:
386   case DW_UT_partial:
387   case DW_UT_skeleton:
388   case DW_UT_split_compile:
389   case DW_UT_split_type:
390     return true;
391   default:
392     return false;
393   }
394 }
395 
isUnitType(dwarf::Tag T)396 inline bool isUnitType(dwarf::Tag T) {
397   switch (T) {
398   case DW_TAG_compile_unit:
399   case DW_TAG_type_unit:
400   case DW_TAG_partial_unit:
401   case DW_TAG_skeleton_unit:
402     return true;
403   default:
404     return false;
405   }
406 }
407 
408 // Constants for the DWARF v5 Accelerator Table Proposal
409 enum AcceleratorTable {
410   // Data layout descriptors.
411   DW_ATOM_null = 0u,       ///  Marker as the end of a list of atoms.
412   DW_ATOM_die_offset = 1u, // DIE offset in the debug_info section.
413   DW_ATOM_cu_offset = 2u, // Offset of the compile unit header that contains the
414                           // item in question.
415   DW_ATOM_die_tag = 3u,   // A tag entry.
416   DW_ATOM_type_flags = 4u, // Set of flags for a type.
417 
418   DW_ATOM_type_type_flags = 5u, // Dsymutil type extension.
419   DW_ATOM_qual_name_hash = 6u,  // Dsymutil qualified hash extension.
420 
421   // DW_ATOM_type_flags values.
422 
423   // Always set for C++, only set for ObjC if this is the @implementation for a
424   // class.
425   DW_FLAG_type_implementation = 2u,
426 
427   // Hash functions.
428 
429   // Daniel J. Bernstein hash.
430   DW_hash_function_djb = 0u
431 };
432 
433 // Constants for the GNU pubnames/pubtypes extensions supporting gdb index.
434 enum GDBIndexEntryKind {
435   GIEK_NONE,
436   GIEK_TYPE,
437   GIEK_VARIABLE,
438   GIEK_FUNCTION,
439   GIEK_OTHER,
440   GIEK_UNUSED5,
441   GIEK_UNUSED6,
442   GIEK_UNUSED7
443 };
444 
445 enum GDBIndexEntryLinkage { GIEL_EXTERNAL, GIEL_STATIC };
446 
447 /// \defgroup DwarfConstantsDumping Dwarf constants dumping functions
448 ///
449 /// All these functions map their argument's value back to the
450 /// corresponding enumerator name or return an empty StringRef if the value
451 /// isn't known.
452 ///
453 /// @{
454 StringRef TagString(unsigned Tag);
455 StringRef ChildrenString(unsigned Children);
456 StringRef AttributeString(unsigned Attribute);
457 StringRef FormEncodingString(unsigned Encoding);
458 StringRef OperationEncodingString(unsigned Encoding);
459 StringRef AttributeEncodingString(unsigned Encoding);
460 StringRef DecimalSignString(unsigned Sign);
461 StringRef EndianityString(unsigned Endian);
462 StringRef AccessibilityString(unsigned Access);
463 StringRef DefaultedMemberString(unsigned DefaultedEncodings);
464 StringRef VisibilityString(unsigned Visibility);
465 StringRef VirtualityString(unsigned Virtuality);
466 StringRef LanguageString(unsigned Language);
467 StringRef CaseString(unsigned Case);
468 StringRef ConventionString(unsigned Convention);
469 StringRef InlineCodeString(unsigned Code);
470 StringRef ArrayOrderString(unsigned Order);
471 StringRef LNStandardString(unsigned Standard);
472 StringRef LNExtendedString(unsigned Encoding);
473 StringRef MacinfoString(unsigned Encoding);
474 StringRef MacroString(unsigned Encoding);
475 StringRef RangeListEncodingString(unsigned Encoding);
476 StringRef LocListEncodingString(unsigned Encoding);
477 StringRef CallFrameString(unsigned Encoding, Triple::ArchType Arch);
478 StringRef ApplePropertyString(unsigned);
479 StringRef UnitTypeString(unsigned);
480 StringRef AtomTypeString(unsigned Atom);
481 StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind);
482 StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage);
483 StringRef IndexString(unsigned Idx);
484 StringRef FormatString(DwarfFormat Format);
485 StringRef FormatString(bool IsDWARF64);
486 /// @}
487 
488 /// \defgroup DwarfConstantsParsing Dwarf constants parsing functions
489 ///
490 /// These functions map their strings back to the corresponding enumeration
491 /// value or return 0 if there is none, except for these exceptions:
492 ///
493 /// \li \a getTag() returns \a DW_TAG_invalid on invalid input.
494 /// \li \a getVirtuality() returns \a DW_VIRTUALITY_invalid on invalid input.
495 /// \li \a getMacinfo() returns \a DW_MACINFO_invalid on invalid input.
496 ///
497 /// @{
498 unsigned getTag(StringRef TagString);
499 unsigned getOperationEncoding(StringRef OperationEncodingString);
500 unsigned getVirtuality(StringRef VirtualityString);
501 unsigned getLanguage(StringRef LanguageString);
502 unsigned getCallingConvention(StringRef LanguageString);
503 unsigned getAttributeEncoding(StringRef EncodingString);
504 unsigned getMacinfo(StringRef MacinfoString);
505 unsigned getMacro(StringRef MacroString);
506 /// @}
507 
508 /// \defgroup DwarfConstantsVersioning Dwarf version for constants
509 ///
510 /// For constants defined by DWARF, returns the DWARF version when the constant
511 /// was first defined. For vendor extensions, if there is a version-related
512 /// policy for when to emit it, returns a version number for that policy.
513 /// Otherwise returns 0.
514 ///
515 /// @{
516 unsigned TagVersion(Tag T);
517 unsigned AttributeVersion(Attribute A);
518 unsigned FormVersion(Form F);
519 unsigned OperationVersion(LocationAtom O);
520 unsigned AttributeEncodingVersion(TypeKind E);
521 unsigned LanguageVersion(SourceLanguage L);
522 /// @}
523 
524 /// \defgroup DwarfConstantsVendor Dwarf "vendor" for constants
525 ///
526 /// These functions return an identifier describing "who" defined the constant,
527 /// either the DWARF standard itself or the vendor who defined the extension.
528 ///
529 /// @{
530 unsigned TagVendor(Tag T);
531 unsigned AttributeVendor(Attribute A);
532 unsigned FormVendor(Form F);
533 unsigned OperationVendor(LocationAtom O);
534 unsigned AttributeEncodingVendor(TypeKind E);
535 unsigned LanguageVendor(SourceLanguage L);
536 /// @}
537 
538 Optional<unsigned> LanguageLowerBound(SourceLanguage L);
539 
540 /// The size of a reference determined by the DWARF 32/64-bit format.
getDwarfOffsetByteSize(DwarfFormat Format)541 inline uint8_t getDwarfOffsetByteSize(DwarfFormat Format) {
542   switch (Format) {
543   case DwarfFormat::DWARF32:
544     return 4;
545   case DwarfFormat::DWARF64:
546     return 8;
547   }
548   llvm_unreachable("Invalid Format value");
549 }
550 
551 /// A helper struct providing information about the byte size of DW_FORM
552 /// values that vary in size depending on the DWARF version, address byte
553 /// size, or DWARF32/DWARF64.
554 struct FormParams {
555   uint16_t Version;
556   uint8_t AddrSize;
557   DwarfFormat Format;
558 
559   /// The definition of the size of form DW_FORM_ref_addr depends on the
560   /// version. In DWARF v2 it's the size of an address; after that, it's the
561   /// size of a reference.
getRefAddrByteSizeFormParams562   uint8_t getRefAddrByteSize() const {
563     if (Version == 2)
564       return AddrSize;
565     return getDwarfOffsetByteSize();
566   }
567 
568   /// The size of a reference is determined by the DWARF 32/64-bit format.
getDwarfOffsetByteSizeFormParams569   uint8_t getDwarfOffsetByteSize() const {
570     return dwarf::getDwarfOffsetByteSize(Format);
571   }
572 
573   explicit operator bool() const { return Version && AddrSize; }
574 };
575 
576 /// Get the byte size of the unit length field depending on the DWARF format.
getUnitLengthFieldByteSize(DwarfFormat Format)577 inline uint8_t getUnitLengthFieldByteSize(DwarfFormat Format) {
578   switch (Format) {
579   case DwarfFormat::DWARF32:
580     return 4;
581   case DwarfFormat::DWARF64:
582     return 12;
583   }
584   llvm_unreachable("Invalid Format value");
585 }
586 
587 /// Get the fixed byte size for a given form.
588 ///
589 /// If the form has a fixed byte size, then an Optional with a value will be
590 /// returned. If the form is always encoded using a variable length storage
591 /// format (ULEB or SLEB numbers or blocks) then None will be returned.
592 ///
593 /// \param Form DWARF form to get the fixed byte size for.
594 /// \param Params DWARF parameters to help interpret forms.
595 /// \returns Optional<uint8_t> value with the fixed byte size or None if
596 /// \p Form doesn't have a fixed byte size.
597 Optional<uint8_t> getFixedFormByteSize(dwarf::Form Form, FormParams Params);
598 
599 /// Tells whether the specified form is defined in the specified version,
600 /// or is an extension if extensions are allowed.
601 bool isValidFormForVersion(Form F, unsigned Version, bool ExtensionsOk = true);
602 
603 /// Returns the symbolic string representing Val when used as a value
604 /// for attribute Attr.
605 StringRef AttributeValueString(uint16_t Attr, unsigned Val);
606 
607 /// Returns the symbolic string representing Val when used as a value
608 /// for atom Atom.
609 StringRef AtomValueString(uint16_t Atom, unsigned Val);
610 
611 /// Describes an entry of the various gnu_pub* debug sections.
612 ///
613 /// The gnu_pub* kind looks like:
614 ///
615 /// 0-3  reserved
616 /// 4-6  symbol kind
617 /// 7    0 == global, 1 == static
618 ///
619 /// A gdb_index descriptor includes the above kind, shifted 24 bits up with the
620 /// offset of the cu within the debug_info section stored in those 24 bits.
621 struct PubIndexEntryDescriptor {
622   GDBIndexEntryKind Kind;
623   GDBIndexEntryLinkage Linkage;
PubIndexEntryDescriptorPubIndexEntryDescriptor624   PubIndexEntryDescriptor(GDBIndexEntryKind Kind, GDBIndexEntryLinkage Linkage)
625       : Kind(Kind), Linkage(Linkage) {}
PubIndexEntryDescriptorPubIndexEntryDescriptor626   /* implicit */ PubIndexEntryDescriptor(GDBIndexEntryKind Kind)
627       : Kind(Kind), Linkage(GIEL_EXTERNAL) {}
PubIndexEntryDescriptorPubIndexEntryDescriptor628   explicit PubIndexEntryDescriptor(uint8_t Value)
629       : Kind(
630             static_cast<GDBIndexEntryKind>((Value & KIND_MASK) >> KIND_OFFSET)),
631         Linkage(static_cast<GDBIndexEntryLinkage>((Value & LINKAGE_MASK) >>
632                                                   LINKAGE_OFFSET)) {}
toBitsPubIndexEntryDescriptor633   uint8_t toBits() const {
634     return Kind << KIND_OFFSET | Linkage << LINKAGE_OFFSET;
635   }
636 
637 private:
638   enum {
639     KIND_OFFSET = 4,
640     KIND_MASK = 7 << KIND_OFFSET,
641     LINKAGE_OFFSET = 7,
642     LINKAGE_MASK = 1 << LINKAGE_OFFSET
643   };
644 };
645 
646 template <typename Enum> struct EnumTraits : public std::false_type {};
647 
648 template <> struct EnumTraits<Attribute> : public std::true_type {
649   static constexpr char Type[3] = "AT";
650   static constexpr StringRef (*StringFn)(unsigned) = &AttributeString;
651 };
652 
653 template <> struct EnumTraits<Form> : public std::true_type {
654   static constexpr char Type[5] = "FORM";
655   static constexpr StringRef (*StringFn)(unsigned) = &FormEncodingString;
656 };
657 
658 template <> struct EnumTraits<Index> : public std::true_type {
659   static constexpr char Type[4] = "IDX";
660   static constexpr StringRef (*StringFn)(unsigned) = &IndexString;
661 };
662 
663 template <> struct EnumTraits<Tag> : public std::true_type {
664   static constexpr char Type[4] = "TAG";
665   static constexpr StringRef (*StringFn)(unsigned) = &TagString;
666 };
667 
668 template <> struct EnumTraits<LineNumberOps> : public std::true_type {
669   static constexpr char Type[4] = "LNS";
670   static constexpr StringRef (*StringFn)(unsigned) = &LNStandardString;
671 };
672 
673 template <> struct EnumTraits<LocationAtom> : public std::true_type {
674   static constexpr char Type[3] = "OP";
675   static constexpr StringRef (*StringFn)(unsigned) = &OperationEncodingString;
676 };
677 } // End of namespace dwarf
678 
679 /// Dwarf constants format_provider
680 ///
681 /// Specialization of the format_provider template for dwarf enums. Unlike the
682 /// dumping functions above, these format unknown enumerator values as
683 /// DW_TYPE_unknown_1234 (e.g. DW_TAG_unknown_ffff).
684 template <typename Enum>
685 struct format_provider<Enum, std::enable_if_t<dwarf::EnumTraits<Enum>::value>> {
686   static void format(const Enum &E, raw_ostream &OS, StringRef Style) {
687     StringRef Str = dwarf::EnumTraits<Enum>::StringFn(E);
688     if (Str.empty()) {
689       OS << "DW_" << dwarf::EnumTraits<Enum>::Type << "_unknown_"
690          << llvm::format("%x", E);
691     } else
692       OS << Str;
693   }
694 };
695 } // End of namespace llvm
696 
697 #endif
698