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