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