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