1 //===-- llvm/BinaryFormat/Dwarf.h ---Dwarf Constants-------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file contains constants used for implementing Dwarf
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.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_BINARYFORMAT_DWARF_H
20 #define LLVM_BINARYFORMAT_DWARF_H
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"
35 //===----------------------------------------------------------------------===//
36 // DWARF constants as gleaned from the DWARF Debugging Information Format V.5
37 // reference manual http://www.dwarfstd.org/.
40 // Do not mix the following two enumerations sets. DW_TAG_invalid changes the
41 // enumeration base type.
43 enum LLVMConstants
: uint32_t {
44 // LLVM mock tags (see also llvm/BinaryFormat/Dwarf.def).
45 DW_TAG_invalid
= ~0U, // Tag for invalid results.
46 DW_VIRTUALITY_invalid
= ~0U, // Virtuality for invalid results.
47 DW_MACINFO_invalid
= ~0U, // Macinfo type for invalid results.
49 // Special values for an initial length field.
50 DW_LENGTH_lo_reserved
= 0xfffffff0, // Lower bound of the reserved range.
51 DW_LENGTH_DWARF64
= 0xffffffff, // Indicator of 64-bit DWARF format.
52 DW_LENGTH_hi_reserved
= 0xffffffff, // Upper bound of the reserved range.
55 DWARF_VERSION
= 4, // Default dwarf version we output.
56 DW_PUBTYPES_VERSION
= 2, // Section version number for .debug_pubtypes.
57 DW_PUBNAMES_VERSION
= 2, // Section version number for .debug_pubnames.
58 DW_ARANGES_VERSION
= 2, // Section version number for .debug_aranges.
59 // Identifiers we use to distinguish vendor extensions.
60 DWARF_VENDOR_DWARF
= 0, // Defined in v2 or later of the DWARF standard.
61 DWARF_VENDOR_APPLE
= 1,
62 DWARF_VENDOR_BORLAND
= 2,
64 DWARF_VENDOR_GOOGLE
= 4,
65 DWARF_VENDOR_LLVM
= 5,
69 /// Constants that define the DWARF format as 32 or 64 bit.
70 enum DwarfFormat
: uint8_t { DWARF32
, DWARF64
};
72 /// Special ID values that distinguish a CIE from a FDE in DWARF CFI.
73 /// Not inside an enum because a 64-bit value is needed.
75 const uint32_t DW_CIE_ID
= UINT32_MAX
;
76 const uint64_t DW64_CIE_ID
= UINT64_MAX
;
79 /// Identifier of an invalid DIE offset in the .debug_info section.
80 const uint32_t DW_INVALID_OFFSET
= UINT32_MAX
;
83 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) DW_TAG_##NAME = ID,
84 #include "llvm/BinaryFormat/Dwarf.def"
85 DW_TAG_lo_user
= 0x4080,
86 DW_TAG_hi_user
= 0xffff,
87 DW_TAG_user_base
= 0x1000 ///< Recommended base for user tags.
90 inline bool isType(Tag T
) {
94 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) \
96 return (KIND == DW_KIND_TYPE);
97 #include "llvm/BinaryFormat/Dwarf.def"
102 enum Attribute
: uint16_t {
103 #define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) DW_AT_##NAME = ID,
104 #include "llvm/BinaryFormat/Dwarf.def"
105 DW_AT_lo_user
= 0x2000,
106 DW_AT_hi_user
= 0x3fff,
109 enum Form
: uint16_t {
110 #define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) DW_FORM_##NAME = ID,
111 #include "llvm/BinaryFormat/Dwarf.def"
112 DW_FORM_lo_user
= 0x1f00, ///< Not specified by DWARF.
116 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) DW_OP_##NAME = ID,
117 #include "llvm/BinaryFormat/Dwarf.def"
118 DW_OP_lo_user
= 0xe0,
119 DW_OP_hi_user
= 0xff,
120 DW_OP_LLVM_fragment
= 0x1000, ///< Only used in LLVM metadata.
121 DW_OP_LLVM_convert
= 0x1001, ///< Only used in LLVM metadata.
122 DW_OP_LLVM_tag_offset
= 0x1002, ///< Only used in LLVM metadata.
125 enum TypeKind
: uint8_t {
126 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) DW_ATE_##NAME = ID,
127 #include "llvm/BinaryFormat/Dwarf.def"
128 DW_ATE_lo_user
= 0x80,
129 DW_ATE_hi_user
= 0xff
132 enum DecimalSignEncoding
{
133 // Decimal sign attribute values
134 DW_DS_unsigned
= 0x01,
135 DW_DS_leading_overpunch
= 0x02,
136 DW_DS_trailing_overpunch
= 0x03,
137 DW_DS_leading_separate
= 0x04,
138 DW_DS_trailing_separate
= 0x05
141 enum EndianityEncoding
{
142 // Endianity attribute values
143 #define HANDLE_DW_END(ID, NAME) DW_END_##NAME = ID,
144 #include "llvm/BinaryFormat/Dwarf.def"
145 DW_END_lo_user
= 0x40,
146 DW_END_hi_user
= 0xff
149 enum AccessAttribute
{
150 // Accessibility codes
151 DW_ACCESS_public
= 0x01,
152 DW_ACCESS_protected
= 0x02,
153 DW_ACCESS_private
= 0x03
156 enum VisibilityAttribute
{
159 DW_VIS_exported
= 0x02,
160 DW_VIS_qualified
= 0x03
163 enum VirtualityAttribute
{
164 #define HANDLE_DW_VIRTUALITY(ID, NAME) DW_VIRTUALITY_##NAME = ID,
165 #include "llvm/BinaryFormat/Dwarf.def"
166 DW_VIRTUALITY_max
= 0x02
169 enum DefaultedMemberAttribute
{
170 #define HANDLE_DW_DEFAULTED(ID, NAME) DW_DEFAULTED_##NAME = ID,
171 #include "llvm/BinaryFormat/Dwarf.def"
172 DW_DEFAULTED_max
= 0x02
175 enum SourceLanguage
{
176 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
178 #include "llvm/BinaryFormat/Dwarf.def"
179 DW_LANG_lo_user
= 0x8000,
180 DW_LANG_hi_user
= 0xffff
183 enum CaseSensitivity
{
184 // Identifier case codes
185 DW_ID_case_sensitive
= 0x00,
186 DW_ID_up_case
= 0x01,
187 DW_ID_down_case
= 0x02,
188 DW_ID_case_insensitive
= 0x03
191 enum CallingConvention
{
192 // Calling convention codes
193 #define HANDLE_DW_CC(ID, NAME) DW_CC_##NAME = ID,
194 #include "llvm/BinaryFormat/Dwarf.def"
195 DW_CC_lo_user
= 0x40,
199 enum InlineAttribute
{
201 DW_INL_not_inlined
= 0x00,
202 DW_INL_inlined
= 0x01,
203 DW_INL_declared_not_inlined
= 0x02,
204 DW_INL_declared_inlined
= 0x03
207 enum ArrayDimensionOrdering
{
209 DW_ORD_row_major
= 0x00,
210 DW_ORD_col_major
= 0x01
213 enum DiscriminantList
{
214 // Discriminant descriptor values
219 /// Line Number Standard Opcode Encodings.
220 enum LineNumberOps
: uint8_t {
221 #define HANDLE_DW_LNS(ID, NAME) DW_LNS_##NAME = ID,
222 #include "llvm/BinaryFormat/Dwarf.def"
225 /// Line Number Extended Opcode Encodings.
226 enum LineNumberExtendedOps
{
227 #define HANDLE_DW_LNE(ID, NAME) DW_LNE_##NAME = ID,
228 #include "llvm/BinaryFormat/Dwarf.def"
229 DW_LNE_lo_user
= 0x80,
230 DW_LNE_hi_user
= 0xff
233 enum LineNumberEntryFormat
{
234 #define HANDLE_DW_LNCT(ID, NAME) DW_LNCT_##NAME = ID,
235 #include "llvm/BinaryFormat/Dwarf.def"
236 DW_LNCT_lo_user
= 0x2000,
237 DW_LNCT_hi_user
= 0x3fff,
240 enum MacinfoRecordType
{
241 // Macinfo Type Encodings
242 DW_MACINFO_define
= 0x01,
243 DW_MACINFO_undef
= 0x02,
244 DW_MACINFO_start_file
= 0x03,
245 DW_MACINFO_end_file
= 0x04,
246 DW_MACINFO_vendor_ext
= 0xff
249 /// DWARF v5 macro information entry type encodings.
250 enum MacroEntryType
{
251 #define HANDLE_DW_MACRO(ID, NAME) DW_MACRO_##NAME = ID,
252 #include "llvm/BinaryFormat/Dwarf.def"
253 DW_MACRO_lo_user
= 0xe0,
254 DW_MACRO_hi_user
= 0xff
257 /// DWARF v5 range list entry encoding values.
258 enum RangeListEntries
{
259 #define HANDLE_DW_RLE(ID, NAME) DW_RLE_##NAME = ID,
260 #include "llvm/BinaryFormat/Dwarf.def"
263 /// Call frame instruction encodings.
265 #define HANDLE_DW_CFA(ID, NAME) DW_CFA_##NAME = ID,
266 #define HANDLE_DW_CFA_PRED(ID, NAME, ARCH) DW_CFA_##NAME = ID,
267 #include "llvm/BinaryFormat/Dwarf.def"
268 DW_CFA_extended
= 0x00,
270 DW_CFA_lo_user
= 0x1c,
271 DW_CFA_hi_user
= 0x3f
276 DW_CHILDREN_no
= 0x00,
277 DW_CHILDREN_yes
= 0x01,
279 DW_EH_PE_absptr
= 0x00,
280 DW_EH_PE_omit
= 0xff,
281 DW_EH_PE_uleb128
= 0x01,
282 DW_EH_PE_udata2
= 0x02,
283 DW_EH_PE_udata4
= 0x03,
284 DW_EH_PE_udata8
= 0x04,
285 DW_EH_PE_sleb128
= 0x09,
286 DW_EH_PE_sdata2
= 0x0A,
287 DW_EH_PE_sdata4
= 0x0B,
288 DW_EH_PE_sdata8
= 0x0C,
289 DW_EH_PE_signed
= 0x08,
290 DW_EH_PE_pcrel
= 0x10,
291 DW_EH_PE_textrel
= 0x20,
292 DW_EH_PE_datarel
= 0x30,
293 DW_EH_PE_funcrel
= 0x40,
294 DW_EH_PE_aligned
= 0x50,
295 DW_EH_PE_indirect
= 0x80
298 /// Constants for location lists in DWARF v5.
299 enum LocationListEntry
: unsigned char {
300 DW_LLE_end_of_list
= 0x00,
301 DW_LLE_base_addressx
= 0x01,
302 DW_LLE_startx_endx
= 0x02,
303 DW_LLE_startx_length
= 0x03,
304 DW_LLE_offset_pair
= 0x04,
305 DW_LLE_default_location
= 0x05,
306 DW_LLE_base_address
= 0x06,
307 DW_LLE_start_end
= 0x07,
308 DW_LLE_start_length
= 0x08
311 /// Constants for the DW_APPLE_PROPERTY_attributes attribute.
312 /// Keep this list in sync with clang's DeclSpec.h ObjCPropertyAttributeKind!
313 enum ApplePropertyAttributes
{
314 #define HANDLE_DW_APPLE_PROPERTY(ID, NAME) DW_APPLE_PROPERTY_##NAME = ID,
315 #include "llvm/BinaryFormat/Dwarf.def"
318 /// Constants for unit types in DWARF v5.
319 enum UnitType
: unsigned char {
320 #define HANDLE_DW_UT(ID, NAME) DW_UT_##NAME = ID,
321 #include "llvm/BinaryFormat/Dwarf.def"
322 DW_UT_lo_user
= 0x80,
327 #define HANDLE_DW_IDX(ID, NAME) DW_IDX_##NAME = ID,
328 #include "llvm/BinaryFormat/Dwarf.def"
329 DW_IDX_lo_user
= 0x2000,
330 DW_IDX_hi_user
= 0x3fff
333 inline bool isUnitType(uint8_t UnitType
) {
339 case DW_UT_split_compile
:
340 case DW_UT_split_type
:
347 inline bool isUnitType(dwarf::Tag T
) {
349 case DW_TAG_compile_unit
:
350 case DW_TAG_type_unit
:
351 case DW_TAG_partial_unit
:
352 case DW_TAG_skeleton_unit
:
359 // Constants for the DWARF v5 Accelerator Table Proposal
360 enum AcceleratorTable
{
361 // Data layout descriptors.
362 DW_ATOM_null
= 0u, /// Marker as the end of a list of atoms.
363 DW_ATOM_die_offset
= 1u, // DIE offset in the debug_info section.
364 DW_ATOM_cu_offset
= 2u, // Offset of the compile unit header that contains the
366 DW_ATOM_die_tag
= 3u, // A tag entry.
367 DW_ATOM_type_flags
= 4u, // Set of flags for a type.
369 DW_ATOM_type_type_flags
= 5u, // Dsymutil type extension.
370 DW_ATOM_qual_name_hash
= 6u, // Dsymutil qualified hash extension.
372 // DW_ATOM_type_flags values.
374 // Always set for C++, only set for ObjC if this is the @implementation for a
376 DW_FLAG_type_implementation
= 2u,
380 // Daniel J. Bernstein hash.
381 DW_hash_function_djb
= 0u
384 // Constants for the GNU pubnames/pubtypes extensions supporting gdb index.
385 enum GDBIndexEntryKind
{
396 enum GDBIndexEntryLinkage
{ GIEL_EXTERNAL
, GIEL_STATIC
};
398 /// \defgroup DwarfConstantsDumping Dwarf constants dumping functions
400 /// All these functions map their argument's value back to the
401 /// corresponding enumerator name or return an empty StringRef if the value
405 StringRef
TagString(unsigned Tag
);
406 StringRef
ChildrenString(unsigned Children
);
407 StringRef
AttributeString(unsigned Attribute
);
408 StringRef
FormEncodingString(unsigned Encoding
);
409 StringRef
OperationEncodingString(unsigned Encoding
);
410 StringRef
AttributeEncodingString(unsigned Encoding
);
411 StringRef
DecimalSignString(unsigned Sign
);
412 StringRef
EndianityString(unsigned Endian
);
413 StringRef
AccessibilityString(unsigned Access
);
414 StringRef
VisibilityString(unsigned Visibility
);
415 StringRef
VirtualityString(unsigned Virtuality
);
416 StringRef
LanguageString(unsigned Language
);
417 StringRef
CaseString(unsigned Case
);
418 StringRef
ConventionString(unsigned Convention
);
419 StringRef
InlineCodeString(unsigned Code
);
420 StringRef
ArrayOrderString(unsigned Order
);
421 StringRef
LNStandardString(unsigned Standard
);
422 StringRef
LNExtendedString(unsigned Encoding
);
423 StringRef
MacinfoString(unsigned Encoding
);
424 StringRef
RangeListEncodingString(unsigned Encoding
);
425 StringRef
CallFrameString(unsigned Encoding
, Triple::ArchType Arch
);
426 StringRef
ApplePropertyString(unsigned);
427 StringRef
UnitTypeString(unsigned);
428 StringRef
AtomTypeString(unsigned Atom
);
429 StringRef
GDBIndexEntryKindString(GDBIndexEntryKind Kind
);
430 StringRef
GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage
);
431 StringRef
IndexString(unsigned Idx
);
434 /// \defgroup DwarfConstantsParsing Dwarf constants parsing functions
436 /// These functions map their strings back to the corresponding enumeration
437 /// value or return 0 if there is none, except for these exceptions:
439 /// \li \a getTag() returns \a DW_TAG_invalid on invalid input.
440 /// \li \a getVirtuality() returns \a DW_VIRTUALITY_invalid on invalid input.
441 /// \li \a getMacinfo() returns \a DW_MACINFO_invalid on invalid input.
444 unsigned getTag(StringRef TagString
);
445 unsigned getOperationEncoding(StringRef OperationEncodingString
);
446 unsigned getVirtuality(StringRef VirtualityString
);
447 unsigned getLanguage(StringRef LanguageString
);
448 unsigned getCallingConvention(StringRef LanguageString
);
449 unsigned getAttributeEncoding(StringRef EncodingString
);
450 unsigned getMacinfo(StringRef MacinfoString
);
453 /// \defgroup DwarfConstantsVersioning Dwarf version for constants
455 /// For constants defined by DWARF, returns the DWARF version when the constant
456 /// was first defined. For vendor extensions, if there is a version-related
457 /// policy for when to emit it, returns a version number for that policy.
458 /// Otherwise returns 0.
461 unsigned TagVersion(Tag T
);
462 unsigned AttributeVersion(Attribute A
);
463 unsigned FormVersion(Form F
);
464 unsigned OperationVersion(LocationAtom O
);
465 unsigned AttributeEncodingVersion(TypeKind E
);
466 unsigned LanguageVersion(SourceLanguage L
);
469 /// \defgroup DwarfConstantsVendor Dwarf "vendor" for constants
471 /// These functions return an identifier describing "who" defined the constant,
472 /// either the DWARF standard itself or the vendor who defined the extension.
475 unsigned TagVendor(Tag T
);
476 unsigned AttributeVendor(Attribute A
);
477 unsigned FormVendor(Form F
);
478 unsigned OperationVendor(LocationAtom O
);
479 unsigned AttributeEncodingVendor(TypeKind E
);
480 unsigned LanguageVendor(SourceLanguage L
);
483 Optional
<unsigned> LanguageLowerBound(SourceLanguage L
);
485 /// A helper struct providing information about the byte size of DW_FORM
486 /// values that vary in size depending on the DWARF version, address byte
487 /// size, or DWARF32/DWARF64.
493 /// The definition of the size of form DW_FORM_ref_addr depends on the
494 /// version. In DWARF v2 it's the size of an address; after that, it's the
495 /// size of a reference.
496 uint8_t getRefAddrByteSize() const {
499 return getDwarfOffsetByteSize();
502 /// The size of a reference is determined by the DWARF 32/64-bit format.
503 uint8_t getDwarfOffsetByteSize() const {
505 case DwarfFormat::DWARF32
:
507 case DwarfFormat::DWARF64
:
510 llvm_unreachable("Invalid Format value");
513 explicit operator bool() const { return Version
&& AddrSize
; }
516 /// Get the byte size of the unit length field depending on the DWARF format.
517 inline uint8_t getUnitLengthFieldByteSize(DwarfFormat Format
) {
519 case DwarfFormat::DWARF32
:
521 case DwarfFormat::DWARF64
:
524 llvm_unreachable("Invalid Format value");
527 /// Get the fixed byte size for a given form.
529 /// If the form has a fixed byte size, then an Optional with a value will be
530 /// returned. If the form is always encoded using a variable length storage
531 /// format (ULEB or SLEB numbers or blocks) then None will be returned.
533 /// \param Form DWARF form to get the fixed byte size for.
534 /// \param Params DWARF parameters to help interpret forms.
535 /// \returns Optional<uint8_t> value with the fixed byte size or None if
536 /// \p Form doesn't have a fixed byte size.
537 Optional
<uint8_t> getFixedFormByteSize(dwarf::Form Form
, FormParams Params
);
539 /// Tells whether the specified form is defined in the specified version,
540 /// or is an extension if extensions are allowed.
541 bool isValidFormForVersion(Form F
, unsigned Version
, bool ExtensionsOk
= true);
543 /// Returns the symbolic string representing Val when used as a value
544 /// for attribute Attr.
545 StringRef
AttributeValueString(uint16_t Attr
, unsigned Val
);
547 /// Returns the symbolic string representing Val when used as a value
549 StringRef
AtomValueString(uint16_t Atom
, unsigned Val
);
551 /// Describes an entry of the various gnu_pub* debug sections.
553 /// The gnu_pub* kind looks like:
557 /// 7 0 == global, 1 == static
559 /// A gdb_index descriptor includes the above kind, shifted 24 bits up with the
560 /// offset of the cu within the debug_info section stored in those 24 bits.
561 struct PubIndexEntryDescriptor
{
562 GDBIndexEntryKind Kind
;
563 GDBIndexEntryLinkage Linkage
;
564 PubIndexEntryDescriptor(GDBIndexEntryKind Kind
, GDBIndexEntryLinkage Linkage
)
565 : Kind(Kind
), Linkage(Linkage
) {}
566 /* implicit */ PubIndexEntryDescriptor(GDBIndexEntryKind Kind
)
567 : Kind(Kind
), Linkage(GIEL_EXTERNAL
) {}
568 explicit PubIndexEntryDescriptor(uint8_t Value
)
570 static_cast<GDBIndexEntryKind
>((Value
& KIND_MASK
) >> KIND_OFFSET
)),
571 Linkage(static_cast<GDBIndexEntryLinkage
>((Value
& LINKAGE_MASK
) >>
573 uint8_t toBits() const {
574 return Kind
<< KIND_OFFSET
| Linkage
<< LINKAGE_OFFSET
;
580 KIND_MASK
= 7 << KIND_OFFSET
,
582 LINKAGE_MASK
= 1 << LINKAGE_OFFSET
586 template <typename Enum
> struct EnumTraits
: public std::false_type
{};
588 template <> struct EnumTraits
<Attribute
> : public std::true_type
{
589 static constexpr char Type
[3] = "AT";
590 static constexpr StringRef (*StringFn
)(unsigned) = &AttributeString
;
593 template <> struct EnumTraits
<Form
> : public std::true_type
{
594 static constexpr char Type
[5] = "FORM";
595 static constexpr StringRef (*StringFn
)(unsigned) = &FormEncodingString
;
598 template <> struct EnumTraits
<Index
> : public std::true_type
{
599 static constexpr char Type
[4] = "IDX";
600 static constexpr StringRef (*StringFn
)(unsigned) = &IndexString
;
603 template <> struct EnumTraits
<Tag
> : public std::true_type
{
604 static constexpr char Type
[4] = "TAG";
605 static constexpr StringRef (*StringFn
)(unsigned) = &TagString
;
607 } // End of namespace dwarf
609 /// Dwarf constants format_provider
611 /// Specialization of the format_provider template for dwarf enums. Unlike the
612 /// dumping functions above, these format unknown enumerator values as
613 /// DW_TYPE_unknown_1234 (e.g. DW_TAG_unknown_ffff).
614 template <typename Enum
>
615 struct format_provider
<
616 Enum
, typename
std::enable_if
<dwarf::EnumTraits
<Enum
>::value
>::type
> {
617 static void format(const Enum
&E
, raw_ostream
&OS
, StringRef Style
) {
618 StringRef Str
= dwarf::EnumTraits
<Enum
>::StringFn(E
);
620 OS
<< "DW_" << dwarf::EnumTraits
<Enum
>::Type
<< "_unknown_"
621 << llvm::format("%x", E
);
626 } // End of namespace llvm