1//===-- llvm/BinaryFormat/Dwarf.h ---Dwarf Constants-------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// This file contains constants used for implementing Dwarf
12/// debug support.
13///
14/// For details on the Dwarf specfication see the latest DWARF Debugging
15/// Information Format standard document on http://www.dwarfstd.org. This
16/// file often includes support for non-released standard features.
17//
18//===----------------------------------------------------------------------===//
19
20#ifndef LLVM_BINARYFORMAT_DWARF_H
21#define LLVM_BINARYFORMAT_DWARF_H
22
23#include "llvm/Support/Compiler.h"
24#include "llvm/Support/DataTypes.h"
25
26namespace llvm {
27class StringRef;
28
29namespace dwarf {
30
31//===----------------------------------------------------------------------===//
32// DWARF constants as gleaned from the DWARF Debugging Information Format V.5
33// reference manual http://www.dwarfstd.org/.
34//
35
36// Do not mix the following two enumerations sets.  DW_TAG_invalid changes the
37// enumeration base type.
38
39enum LLVMConstants : uint32_t {
40  // LLVM mock tags (see also llvm/BinaryFormat/Dwarf.def).
41  DW_TAG_invalid = ~0U,        // Tag for invalid results.
42  DW_VIRTUALITY_invalid = ~0U, // Virtuality for invalid results.
43  DW_MACINFO_invalid = ~0U,    // Macinfo type for invalid results.
44
45  // Other constants.
46  DWARF_VERSION = 4,       // Default dwarf version we output.
47  DW_PUBTYPES_VERSION = 2, // Section version number for .debug_pubtypes.
48  DW_PUBNAMES_VERSION = 2, // Section version number for .debug_pubnames.
49  DW_ARANGES_VERSION = 2,  // Section version number for .debug_aranges.
50  // Identifiers we use to distinguish vendor extensions.
51  DWARF_VENDOR_DWARF = 0, // Defined in v2 or later of the DWARF standard.
52  DWARF_VENDOR_APPLE = 1,
53  DWARF_VENDOR_BORLAND = 2,
54  DWARF_VENDOR_GNU = 3,
55  DWARF_VENDOR_GOOGLE = 4,
56  DWARF_VENDOR_LLVM = 5,
57  DWARF_VENDOR_MIPS = 6
58};
59
60/// Special ID values that distinguish a CIE from a FDE in DWARF CFI.
61/// Not inside an enum because a 64-bit value is needed.
62/// @{
63const uint32_t DW_CIE_ID = UINT32_MAX;
64const uint64_t DW64_CIE_ID = UINT64_MAX;
65/// @}
66
67/// Identifier of an invalid DIE offset in the .debug_info section.
68const uint32_t DW_INVALID_OFFSET = UINT32_MAX;
69
70enum Tag : uint16_t {
71#define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR) DW_TAG_##NAME = ID,
72#include "llvm/BinaryFormat/Dwarf.def"
73  DW_TAG_lo_user = 0x4080,
74  DW_TAG_hi_user = 0xffff,
75  DW_TAG_user_base = 0x1000 ///< Recommended base for user tags.
76};
77
78inline bool isType(Tag T) {
79  switch (T) {
80  case DW_TAG_array_type:
81  case DW_TAG_class_type:
82  case DW_TAG_interface_type:
83  case DW_TAG_enumeration_type:
84  case DW_TAG_pointer_type:
85  case DW_TAG_reference_type:
86  case DW_TAG_rvalue_reference_type:
87  case DW_TAG_string_type:
88  case DW_TAG_structure_type:
89  case DW_TAG_subroutine_type:
90  case DW_TAG_union_type:
91  case DW_TAG_ptr_to_member_type:
92  case DW_TAG_set_type:
93  case DW_TAG_subrange_type:
94  case DW_TAG_base_type:
95  case DW_TAG_const_type:
96  case DW_TAG_file_type:
97  case DW_TAG_packed_type:
98  case DW_TAG_volatile_type:
99  case DW_TAG_typedef:
100    return true;
101  default:
102    return false;
103  }
104}
105
106/// Attributes.
107enum Attribute : uint16_t {
108#define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) DW_AT_##NAME = ID,
109#include "llvm/BinaryFormat/Dwarf.def"
110  DW_AT_lo_user = 0x2000,
111  DW_AT_hi_user = 0x3fff,
112};
113
114enum Form : uint16_t {
115#define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) DW_FORM_##NAME = ID,
116#include "llvm/BinaryFormat/Dwarf.def"
117  DW_FORM_lo_user = 0x1f00, ///< Not specified by DWARF.
118};
119
120enum LocationAtom {
121#define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) DW_OP_##NAME = ID,
122#include "llvm/BinaryFormat/Dwarf.def"
123  DW_OP_lo_user = 0xe0,
124  DW_OP_hi_user = 0xff,
125  DW_OP_LLVM_fragment = 0x1000 ///< Only used in LLVM metadata.
126};
127
128enum TypeKind {
129#define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) DW_ATE_##NAME = ID,
130#include "llvm/BinaryFormat/Dwarf.def"
131  DW_ATE_lo_user = 0x80,
132  DW_ATE_hi_user = 0xff
133};
134
135enum DecimalSignEncoding {
136  // Decimal sign attribute values
137  DW_DS_unsigned = 0x01,
138  DW_DS_leading_overpunch = 0x02,
139  DW_DS_trailing_overpunch = 0x03,
140  DW_DS_leading_separate = 0x04,
141  DW_DS_trailing_separate = 0x05
142};
143
144enum EndianityEncoding {
145  // Endianity attribute values
146  DW_END_default = 0x00,
147  DW_END_big = 0x01,
148  DW_END_little = 0x02,
149  DW_END_lo_user = 0x40,
150  DW_END_hi_user = 0xff
151};
152
153enum AccessAttribute {
154  // Accessibility codes
155  DW_ACCESS_public = 0x01,
156  DW_ACCESS_protected = 0x02,
157  DW_ACCESS_private = 0x03
158};
159
160enum VisibilityAttribute {
161  // Visibility codes
162  DW_VIS_local = 0x01,
163  DW_VIS_exported = 0x02,
164  DW_VIS_qualified = 0x03
165};
166
167enum VirtualityAttribute {
168#define HANDLE_DW_VIRTUALITY(ID, NAME) DW_VIRTUALITY_##NAME = ID,
169#include "llvm/BinaryFormat/Dwarf.def"
170  DW_VIRTUALITY_max = 0x02
171};
172
173enum DefaultedMemberAttribute {
174#define HANDLE_DW_DEFAULTED(ID, NAME) DW_DEFAULTED_##NAME = ID,
175#include "llvm/BinaryFormat/Dwarf.def"
176  DW_DEFAULTED_max = 0x02
177};
178
179enum SourceLanguage {
180#define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) DW_LANG_##NAME = ID,
181#include "llvm/BinaryFormat/Dwarf.def"
182  DW_LANG_lo_user = 0x8000,
183  DW_LANG_hi_user = 0xffff
184};
185
186enum CaseSensitivity {
187  // Identifier case codes
188  DW_ID_case_sensitive = 0x00,
189  DW_ID_up_case = 0x01,
190  DW_ID_down_case = 0x02,
191  DW_ID_case_insensitive = 0x03
192};
193
194enum CallingConvention {
195// Calling convention codes
196#define HANDLE_DW_CC(ID, NAME) DW_CC_##NAME = ID,
197#include "llvm/BinaryFormat/Dwarf.def"
198  DW_CC_lo_user = 0x40,
199  DW_CC_hi_user = 0xff
200};
201
202enum InlineAttribute {
203  // Inline codes
204  DW_INL_not_inlined = 0x00,
205  DW_INL_inlined = 0x01,
206  DW_INL_declared_not_inlined = 0x02,
207  DW_INL_declared_inlined = 0x03
208};
209
210enum ArrayDimensionOrdering {
211  // Array ordering
212  DW_ORD_row_major = 0x00,
213  DW_ORD_col_major = 0x01
214};
215
216enum DiscriminantList {
217  // Discriminant descriptor values
218  DW_DSC_label = 0x00,
219  DW_DSC_range = 0x01
220};
221
222/// Line Number Standard Opcode Encodings.
223enum LineNumberOps : uint8_t {
224#define HANDLE_DW_LNS(ID, NAME) DW_LNS_##NAME = ID,
225#include "llvm/BinaryFormat/Dwarf.def"
226};
227
228/// Line Number Extended Opcode Encodings.
229enum LineNumberExtendedOps {
230#define HANDLE_DW_LNE(ID, NAME) DW_LNE_##NAME = ID,
231#include "llvm/BinaryFormat/Dwarf.def"
232  DW_LNE_lo_user = 0x80,
233  DW_LNE_hi_user = 0xff
234};
235
236enum LineNumberEntryFormat {
237#define HANDLE_DW_LNCT(ID, NAME) DW_LNCT_##NAME = ID,
238#include "llvm/BinaryFormat/Dwarf.def"
239  DW_LNCT_lo_user = 0x2000,
240  DW_LNCT_hi_user = 0x3fff,
241};
242
243enum MacinfoRecordType {
244  // Macinfo Type Encodings
245  DW_MACINFO_define = 0x01,
246  DW_MACINFO_undef = 0x02,
247  DW_MACINFO_start_file = 0x03,
248  DW_MACINFO_end_file = 0x04,
249  DW_MACINFO_vendor_ext = 0xff
250};
251
252/// DWARF v5 macro information entry type encodings.
253enum MacroEntryType {
254#define HANDLE_DW_MACRO(ID, NAME) DW_MACRO_##NAME = ID,
255#include "llvm/BinaryFormat/Dwarf.def"
256  DW_MACRO_lo_user = 0xe0,
257  DW_MACRO_hi_user = 0xff
258};
259
260/// DWARF v5 range list entry encoding values.
261enum RangeListEntries {
262#define HANDLE_DW_RLE(ID, NAME) DW_RLE_##NAME = ID,
263#include "llvm/BinaryFormat/Dwarf.def"
264};
265
266/// Call frame instruction encodings.
267enum CallFrameInfo {
268#define HANDLE_DW_CFA(ID, NAME) DW_CFA_##NAME = ID,
269#include "llvm/BinaryFormat/Dwarf.def"
270  DW_CFA_extended = 0x00,
271
272  DW_CFA_lo_user = 0x1c,
273  DW_CFA_hi_user = 0x3f
274};
275
276enum Constants {
277  // Children flag
278  DW_CHILDREN_no = 0x00,
279  DW_CHILDREN_yes = 0x01,
280
281  DW_EH_PE_absptr = 0x00,
282  DW_EH_PE_omit = 0xff,
283  DW_EH_PE_uleb128 = 0x01,
284  DW_EH_PE_udata2 = 0x02,
285  DW_EH_PE_udata4 = 0x03,
286  DW_EH_PE_udata8 = 0x04,
287  DW_EH_PE_sleb128 = 0x09,
288  DW_EH_PE_sdata2 = 0x0A,
289  DW_EH_PE_sdata4 = 0x0B,
290  DW_EH_PE_sdata8 = 0x0C,
291  DW_EH_PE_signed = 0x08,
292  DW_EH_PE_pcrel = 0x10,
293  DW_EH_PE_textrel = 0x20,
294  DW_EH_PE_datarel = 0x30,
295  DW_EH_PE_funcrel = 0x40,
296  DW_EH_PE_aligned = 0x50,
297  DW_EH_PE_indirect = 0x80
298};
299
300/// Constants for location lists in DWARF v5.
301enum LocationListEntry : unsigned char {
302  DW_LLE_end_of_list = 0x00,
303  DW_LLE_base_addressx = 0x01,
304  DW_LLE_startx_endx = 0x02,
305  DW_LLE_startx_length = 0x03,
306  DW_LLE_offset_pair = 0x04,
307  DW_LLE_default_location = 0x05,
308  DW_LLE_base_address = 0x06,
309  DW_LLE_start_end = 0x07,
310  DW_LLE_start_length = 0x08
311};
312
313/// Constants for the DW_APPLE_PROPERTY_attributes attribute.
314/// Keep this list in sync with clang's DeclSpec.h ObjCPropertyAttributeKind!
315enum ApplePropertyAttributes {
316#define HANDLE_DW_APPLE_PROPERTY(ID, NAME) DW_APPLE_PROPERTY_##NAME = ID,
317#include "llvm/BinaryFormat/Dwarf.def"
318};
319
320/// Constants for unit types in DWARF v5.
321enum UnitType : unsigned char {
322#define HANDLE_DW_UT(ID, NAME) DW_UT_##NAME = ID,
323#include "llvm/BinaryFormat/Dwarf.def"
324  DW_UT_lo_user = 0x80,
325  DW_UT_hi_user = 0xff
326};
327
328inline bool isUnitType(uint8_t UnitType) {
329  switch (UnitType) {
330  case DW_UT_compile:
331  case DW_UT_type:
332  case DW_UT_partial:
333  case DW_UT_skeleton:
334  case DW_UT_split_compile:
335  case DW_UT_split_type:
336    return true;
337  default:
338    return false;
339  }
340}
341
342inline bool isUnitType(dwarf::Tag T) {
343  switch (T) {
344  case DW_TAG_compile_unit:
345  case DW_TAG_type_unit:
346  case DW_TAG_partial_unit:
347  case DW_TAG_skeleton_unit:
348    return true;
349  default:
350    return false;
351  }
352}
353
354// Constants for the DWARF v5 Accelerator Table Proposal
355enum AcceleratorTable {
356  // Data layout descriptors.
357  DW_ATOM_null = 0u,       // Marker as the end of a list of atoms.
358  DW_ATOM_die_offset = 1u, // DIE offset in the debug_info section.
359  DW_ATOM_cu_offset = 2u, // Offset of the compile unit header that contains the
360                          // item in question.
361  DW_ATOM_die_tag = 3u,   // A tag entry.
362  DW_ATOM_type_flags = 4u, // Set of flags for a type.
363
364  // DW_ATOM_type_flags values.
365
366  // Always set for C++, only set for ObjC if this is the @implementation for a
367  // class.
368  DW_FLAG_type_implementation = 2u,
369
370  // Hash functions.
371
372  // Daniel J. Bernstein hash.
373  DW_hash_function_djb = 0u
374};
375
376// Constants for the GNU pubnames/pubtypes extensions supporting gdb index.
377enum GDBIndexEntryKind {
378  GIEK_NONE,
379  GIEK_TYPE,
380  GIEK_VARIABLE,
381  GIEK_FUNCTION,
382  GIEK_OTHER,
383  GIEK_UNUSED5,
384  GIEK_UNUSED6,
385  GIEK_UNUSED7
386};
387
388enum GDBIndexEntryLinkage { GIEL_EXTERNAL, GIEL_STATIC };
389
390/// \defgroup DwarfConstantsDumping Dwarf constants dumping functions
391///
392/// All these functions map their argument's value back to the
393/// corresponding enumerator name or return nullptr if the value isn't
394/// known.
395///
396/// @{
397StringRef TagString(unsigned Tag);
398StringRef ChildrenString(unsigned Children);
399StringRef AttributeString(unsigned Attribute);
400StringRef FormEncodingString(unsigned Encoding);
401StringRef OperationEncodingString(unsigned Encoding);
402StringRef AttributeEncodingString(unsigned Encoding);
403StringRef DecimalSignString(unsigned Sign);
404StringRef EndianityString(unsigned Endian);
405StringRef AccessibilityString(unsigned Access);
406StringRef VisibilityString(unsigned Visibility);
407StringRef VirtualityString(unsigned Virtuality);
408StringRef LanguageString(unsigned Language);
409StringRef CaseString(unsigned Case);
410StringRef ConventionString(unsigned Convention);
411StringRef InlineCodeString(unsigned Code);
412StringRef ArrayOrderString(unsigned Order);
413StringRef DiscriminantString(unsigned Discriminant);
414StringRef LNStandardString(unsigned Standard);
415StringRef LNExtendedString(unsigned Encoding);
416StringRef MacinfoString(unsigned Encoding);
417StringRef CallFrameString(unsigned Encoding);
418StringRef ApplePropertyString(unsigned);
419StringRef UnitTypeString(unsigned);
420StringRef AtomTypeString(unsigned Atom);
421StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind);
422StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage);
423/// @}
424
425/// \defgroup DwarfConstantsParsing Dwarf constants parsing functions
426///
427/// These functions map their strings back to the corresponding enumeration
428/// value or return 0 if there is none, except for these exceptions:
429///
430/// \li \a getTag() returns \a DW_TAG_invalid on invalid input.
431/// \li \a getVirtuality() returns \a DW_VIRTUALITY_invalid on invalid input.
432/// \li \a getMacinfo() returns \a DW_MACINFO_invalid on invalid input.
433///
434/// @{
435unsigned getTag(StringRef TagString);
436unsigned getOperationEncoding(StringRef OperationEncodingString);
437unsigned getVirtuality(StringRef VirtualityString);
438unsigned getLanguage(StringRef LanguageString);
439unsigned getCallingConvention(StringRef LanguageString);
440unsigned getAttributeEncoding(StringRef EncodingString);
441unsigned getMacinfo(StringRef MacinfoString);
442/// @}
443
444/// \defgroup DwarfConstantsVersioning Dwarf version for constants
445///
446/// For constants defined by DWARF, returns the DWARF version when the constant
447/// was first defined. For vendor extensions, if there is a version-related
448/// policy for when to emit it, returns a version number for that policy.
449/// Otherwise returns 0.
450///
451/// @{
452unsigned TagVersion(Tag T);
453unsigned AttributeVersion(Attribute A);
454unsigned FormVersion(Form F);
455unsigned OperationVersion(LocationAtom O);
456unsigned AttributeEncodingVersion(TypeKind E);
457unsigned LanguageVersion(SourceLanguage L);
458/// @}
459
460/// \defgroup DwarfConstantsVendor Dwarf "vendor" for constants
461///
462/// These functions return an identifier describing "who" defined the constant,
463/// either the DWARF standard itself or the vendor who defined the extension.
464///
465/// @{
466unsigned TagVendor(Tag T);
467unsigned AttributeVendor(Attribute A);
468unsigned FormVendor(Form F);
469unsigned OperationVendor(LocationAtom O);
470unsigned AttributeEncodingVendor(TypeKind E);
471unsigned LanguageVendor(SourceLanguage L);
472/// @}
473
474/// Tells whether the specified form is defined in the specified version,
475/// or is an extension if extensions are allowed.
476bool isValidFormForVersion(Form F, unsigned Version, bool ExtensionsOk = true);
477
478/// Returns the symbolic string representing Val when used as a value
479/// for attribute Attr.
480StringRef AttributeValueString(uint16_t Attr, unsigned Val);
481
482/// Describes an entry of the various gnu_pub* debug sections.
483///
484/// The gnu_pub* kind looks like:
485///
486/// 0-3  reserved
487/// 4-6  symbol kind
488/// 7    0 == global, 1 == static
489///
490/// A gdb_index descriptor includes the above kind, shifted 24 bits up with the
491/// offset of the cu within the debug_info section stored in those 24 bits.
492struct PubIndexEntryDescriptor {
493  GDBIndexEntryKind Kind;
494  GDBIndexEntryLinkage Linkage;
495  PubIndexEntryDescriptor(GDBIndexEntryKind Kind, GDBIndexEntryLinkage Linkage)
496      : Kind(Kind), Linkage(Linkage) {}
497  /* implicit */ PubIndexEntryDescriptor(GDBIndexEntryKind Kind)
498      : Kind(Kind), Linkage(GIEL_EXTERNAL) {}
499  explicit PubIndexEntryDescriptor(uint8_t Value)
500      : Kind(
501            static_cast<GDBIndexEntryKind>((Value & KIND_MASK) >> KIND_OFFSET)),
502        Linkage(static_cast<GDBIndexEntryLinkage>((Value & LINKAGE_MASK) >>
503                                                  LINKAGE_OFFSET)) {}
504  uint8_t toBits() const {
505    return Kind << KIND_OFFSET | Linkage << LINKAGE_OFFSET;
506  }
507
508private:
509  enum {
510    KIND_OFFSET = 4,
511    KIND_MASK = 7 << KIND_OFFSET,
512    LINKAGE_OFFSET = 7,
513    LINKAGE_MASK = 1 << LINKAGE_OFFSET
514  };
515};
516
517/// Constants that define the DWARF format as 32 or 64 bit.
518enum DwarfFormat : uint8_t { DWARF32, DWARF64 };
519
520/// The Bernstein hash function used by the accelerator tables.
521uint32_t djbHash(StringRef Buffer);
522
523} // End of namespace dwarf
524
525} // End of namespace llvm
526
527#endif
528