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 DeclSpec.h ObjCPropertyAttributeKind!
361 enum ApplePropertyAttributes {
362 #define HANDLE_DW_APPLE_PROPERTY(ID, NAME) DW_APPLE_PROPERTY_##NAME = ID,
363 #include "llvm/BinaryFormat/Dwarf.def"
364 };
365 
366 /// Constants for unit types in DWARF v5.
367 enum UnitType : unsigned char {
368 #define HANDLE_DW_UT(ID, NAME) DW_UT_##NAME = ID,
369 #include "llvm/BinaryFormat/Dwarf.def"
370   DW_UT_lo_user = 0x80,
371   DW_UT_hi_user = 0xff
372 };
373 
374 enum Index {
375 #define HANDLE_DW_IDX(ID, NAME) DW_IDX_##NAME = ID,
376 #include "llvm/BinaryFormat/Dwarf.def"
377   DW_IDX_lo_user = 0x2000,
378   DW_IDX_hi_user = 0x3fff
379 };
380 
isUnitType(uint8_t UnitType)381 inline bool isUnitType(uint8_t UnitType) {
382   switch (UnitType) {
383   case DW_UT_compile:
384   case DW_UT_type:
385   case DW_UT_partial:
386   case DW_UT_skeleton:
387   case DW_UT_split_compile:
388   case DW_UT_split_type:
389     return true;
390   default:
391     return false;
392   }
393 }
394 
isUnitType(dwarf::Tag T)395 inline bool isUnitType(dwarf::Tag T) {
396   switch (T) {
397   case DW_TAG_compile_unit:
398   case DW_TAG_type_unit:
399   case DW_TAG_partial_unit:
400   case DW_TAG_skeleton_unit:
401     return true;
402   default:
403     return false;
404   }
405 }
406 
407 // Constants for the DWARF v5 Accelerator Table Proposal
408 enum AcceleratorTable {
409   // Data layout descriptors.
410   DW_ATOM_null = 0u,       ///  Marker as the end of a list of atoms.
411   DW_ATOM_die_offset = 1u, // DIE offset in the debug_info section.
412   DW_ATOM_cu_offset = 2u, // Offset of the compile unit header that contains the
413                           // item in question.
414   DW_ATOM_die_tag = 3u,   // A tag entry.
415   DW_ATOM_type_flags = 4u, // Set of flags for a type.
416 
417   DW_ATOM_type_type_flags = 5u, // Dsymutil type extension.
418   DW_ATOM_qual_name_hash = 6u,  // Dsymutil qualified hash extension.
419 
420   // DW_ATOM_type_flags values.
421 
422   // Always set for C++, only set for ObjC if this is the @implementation for a
423   // class.
424   DW_FLAG_type_implementation = 2u,
425 
426   // Hash functions.
427 
428   // Daniel J. Bernstein hash.
429   DW_hash_function_djb = 0u
430 };
431 
432 // Constants for the GNU pubnames/pubtypes extensions supporting gdb index.
433 enum GDBIndexEntryKind {
434   GIEK_NONE,
435   GIEK_TYPE,
436   GIEK_VARIABLE,
437   GIEK_FUNCTION,
438   GIEK_OTHER,
439   GIEK_UNUSED5,
440   GIEK_UNUSED6,
441   GIEK_UNUSED7
442 };
443 
444 enum GDBIndexEntryLinkage { GIEL_EXTERNAL, GIEL_STATIC };
445 
446 /// \defgroup DwarfConstantsDumping Dwarf constants dumping functions
447 ///
448 /// All these functions map their argument's value back to the
449 /// corresponding enumerator name or return an empty StringRef if the value
450 /// isn't known.
451 ///
452 /// @{
453 StringRef TagString(unsigned Tag);
454 StringRef ChildrenString(unsigned Children);
455 StringRef AttributeString(unsigned Attribute);
456 StringRef FormEncodingString(unsigned Encoding);
457 StringRef OperationEncodingString(unsigned Encoding);
458 StringRef AttributeEncodingString(unsigned Encoding);
459 StringRef DecimalSignString(unsigned Sign);
460 StringRef EndianityString(unsigned Endian);
461 StringRef AccessibilityString(unsigned Access);
462 StringRef DefaultedMemberString(unsigned DefaultedEncodings);
463 StringRef VisibilityString(unsigned Visibility);
464 StringRef VirtualityString(unsigned Virtuality);
465 StringRef LanguageString(unsigned Language);
466 StringRef CaseString(unsigned Case);
467 StringRef ConventionString(unsigned Convention);
468 StringRef InlineCodeString(unsigned Code);
469 StringRef ArrayOrderString(unsigned Order);
470 StringRef LNStandardString(unsigned Standard);
471 StringRef LNExtendedString(unsigned Encoding);
472 StringRef MacinfoString(unsigned Encoding);
473 StringRef RangeListEncodingString(unsigned Encoding);
474 StringRef LocListEncodingString(unsigned Encoding);
475 StringRef CallFrameString(unsigned Encoding, Triple::ArchType Arch);
476 StringRef ApplePropertyString(unsigned);
477 StringRef UnitTypeString(unsigned);
478 StringRef AtomTypeString(unsigned Atom);
479 StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind);
480 StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage);
481 StringRef IndexString(unsigned Idx);
482 /// @}
483 
484 /// \defgroup DwarfConstantsParsing Dwarf constants parsing functions
485 ///
486 /// These functions map their strings back to the corresponding enumeration
487 /// value or return 0 if there is none, except for these exceptions:
488 ///
489 /// \li \a getTag() returns \a DW_TAG_invalid on invalid input.
490 /// \li \a getVirtuality() returns \a DW_VIRTUALITY_invalid on invalid input.
491 /// \li \a getMacinfo() returns \a DW_MACINFO_invalid on invalid input.
492 ///
493 /// @{
494 unsigned getTag(StringRef TagString);
495 unsigned getOperationEncoding(StringRef OperationEncodingString);
496 unsigned getVirtuality(StringRef VirtualityString);
497 unsigned getLanguage(StringRef LanguageString);
498 unsigned getCallingConvention(StringRef LanguageString);
499 unsigned getAttributeEncoding(StringRef EncodingString);
500 unsigned getMacinfo(StringRef MacinfoString);
501 /// @}
502 
503 /// \defgroup DwarfConstantsVersioning Dwarf version for constants
504 ///
505 /// For constants defined by DWARF, returns the DWARF version when the constant
506 /// was first defined. For vendor extensions, if there is a version-related
507 /// policy for when to emit it, returns a version number for that policy.
508 /// Otherwise returns 0.
509 ///
510 /// @{
511 unsigned TagVersion(Tag T);
512 unsigned AttributeVersion(Attribute A);
513 unsigned FormVersion(Form F);
514 unsigned OperationVersion(LocationAtom O);
515 unsigned AttributeEncodingVersion(TypeKind E);
516 unsigned LanguageVersion(SourceLanguage L);
517 /// @}
518 
519 /// \defgroup DwarfConstantsVendor Dwarf "vendor" for constants
520 ///
521 /// These functions return an identifier describing "who" defined the constant,
522 /// either the DWARF standard itself or the vendor who defined the extension.
523 ///
524 /// @{
525 unsigned TagVendor(Tag T);
526 unsigned AttributeVendor(Attribute A);
527 unsigned FormVendor(Form F);
528 unsigned OperationVendor(LocationAtom O);
529 unsigned AttributeEncodingVendor(TypeKind E);
530 unsigned LanguageVendor(SourceLanguage L);
531 /// @}
532 
533 Optional<unsigned> LanguageLowerBound(SourceLanguage L);
534 
535 /// A helper struct providing information about the byte size of DW_FORM
536 /// values that vary in size depending on the DWARF version, address byte
537 /// size, or DWARF32/DWARF64.
538 struct FormParams {
539   uint16_t Version;
540   uint8_t AddrSize;
541   DwarfFormat Format;
542 
543   /// The definition of the size of form DW_FORM_ref_addr depends on the
544   /// version. In DWARF v2 it's the size of an address; after that, it's the
545   /// size of a reference.
getRefAddrByteSizeFormParams546   uint8_t getRefAddrByteSize() const {
547     if (Version == 2)
548       return AddrSize;
549     return getDwarfOffsetByteSize();
550   }
551 
552   /// The size of a reference is determined by the DWARF 32/64-bit format.
getDwarfOffsetByteSizeFormParams553   uint8_t getDwarfOffsetByteSize() const {
554     switch (Format) {
555     case DwarfFormat::DWARF32:
556       return 4;
557     case DwarfFormat::DWARF64:
558       return 8;
559     }
560     llvm_unreachable("Invalid Format value");
561   }
562 
563   explicit operator bool() const { return Version && AddrSize; }
564 };
565 
566 /// Get the byte size of the unit length field depending on the DWARF format.
getUnitLengthFieldByteSize(DwarfFormat Format)567 inline uint8_t getUnitLengthFieldByteSize(DwarfFormat Format) {
568   switch (Format) {
569   case DwarfFormat::DWARF32:
570     return 4;
571   case DwarfFormat::DWARF64:
572     return 12;
573   }
574   llvm_unreachable("Invalid Format value");
575 }
576 
577 /// Get the fixed byte size for a given form.
578 ///
579 /// If the form has a fixed byte size, then an Optional with a value will be
580 /// returned. If the form is always encoded using a variable length storage
581 /// format (ULEB or SLEB numbers or blocks) then None will be returned.
582 ///
583 /// \param Form DWARF form to get the fixed byte size for.
584 /// \param Params DWARF parameters to help interpret forms.
585 /// \returns Optional<uint8_t> value with the fixed byte size or None if
586 /// \p Form doesn't have a fixed byte size.
587 Optional<uint8_t> getFixedFormByteSize(dwarf::Form Form, FormParams Params);
588 
589 /// Tells whether the specified form is defined in the specified version,
590 /// or is an extension if extensions are allowed.
591 bool isValidFormForVersion(Form F, unsigned Version, bool ExtensionsOk = true);
592 
593 /// Returns the symbolic string representing Val when used as a value
594 /// for attribute Attr.
595 StringRef AttributeValueString(uint16_t Attr, unsigned Val);
596 
597 /// Returns the symbolic string representing Val when used as a value
598 /// for atom Atom.
599 StringRef AtomValueString(uint16_t Atom, unsigned Val);
600 
601 /// Describes an entry of the various gnu_pub* debug sections.
602 ///
603 /// The gnu_pub* kind looks like:
604 ///
605 /// 0-3  reserved
606 /// 4-6  symbol kind
607 /// 7    0 == global, 1 == static
608 ///
609 /// A gdb_index descriptor includes the above kind, shifted 24 bits up with the
610 /// offset of the cu within the debug_info section stored in those 24 bits.
611 struct PubIndexEntryDescriptor {
612   GDBIndexEntryKind Kind;
613   GDBIndexEntryLinkage Linkage;
PubIndexEntryDescriptorPubIndexEntryDescriptor614   PubIndexEntryDescriptor(GDBIndexEntryKind Kind, GDBIndexEntryLinkage Linkage)
615       : Kind(Kind), Linkage(Linkage) {}
PubIndexEntryDescriptorPubIndexEntryDescriptor616   /* implicit */ PubIndexEntryDescriptor(GDBIndexEntryKind Kind)
617       : Kind(Kind), Linkage(GIEL_EXTERNAL) {}
PubIndexEntryDescriptorPubIndexEntryDescriptor618   explicit PubIndexEntryDescriptor(uint8_t Value)
619       : Kind(
620             static_cast<GDBIndexEntryKind>((Value & KIND_MASK) >> KIND_OFFSET)),
621         Linkage(static_cast<GDBIndexEntryLinkage>((Value & LINKAGE_MASK) >>
622                                                   LINKAGE_OFFSET)) {}
toBitsPubIndexEntryDescriptor623   uint8_t toBits() const {
624     return Kind << KIND_OFFSET | Linkage << LINKAGE_OFFSET;
625   }
626 
627 private:
628   enum {
629     KIND_OFFSET = 4,
630     KIND_MASK = 7 << KIND_OFFSET,
631     LINKAGE_OFFSET = 7,
632     LINKAGE_MASK = 1 << LINKAGE_OFFSET
633   };
634 };
635 
636 template <typename Enum> struct EnumTraits : public std::false_type {};
637 
638 template <> struct EnumTraits<Attribute> : public std::true_type {
639   static constexpr char Type[3] = "AT";
640   static constexpr StringRef (*StringFn)(unsigned) = &AttributeString;
641 };
642 
643 template <> struct EnumTraits<Form> : public std::true_type {
644   static constexpr char Type[5] = "FORM";
645   static constexpr StringRef (*StringFn)(unsigned) = &FormEncodingString;
646 };
647 
648 template <> struct EnumTraits<Index> : public std::true_type {
649   static constexpr char Type[4] = "IDX";
650   static constexpr StringRef (*StringFn)(unsigned) = &IndexString;
651 };
652 
653 template <> struct EnumTraits<Tag> : public std::true_type {
654   static constexpr char Type[4] = "TAG";
655   static constexpr StringRef (*StringFn)(unsigned) = &TagString;
656 };
657 } // End of namespace dwarf
658 
659 /// Dwarf constants format_provider
660 ///
661 /// Specialization of the format_provider template for dwarf enums. Unlike the
662 /// dumping functions above, these format unknown enumerator values as
663 /// DW_TYPE_unknown_1234 (e.g. DW_TAG_unknown_ffff).
664 template <typename Enum>
665 struct format_provider<
666     Enum, typename std::enable_if<dwarf::EnumTraits<Enum>::value>::type> {
667   static void format(const Enum &E, raw_ostream &OS, StringRef Style) {
668     StringRef Str = dwarf::EnumTraits<Enum>::StringFn(E);
669     if (Str.empty()) {
670       OS << "DW_" << dwarf::EnumTraits<Enum>::Type << "_unknown_"
671          << llvm::format("%x", E);
672     } else
673       OS << Str;
674   }
675 };
676 } // End of namespace llvm
677 
678 #endif
679