1 //===-- llvm/BinaryFormat/Dwarf.cpp - Dwarf Framework ------------*- 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 //===----------------------------------------------------------------------===//
9 // This file contains support for generic dwarf information.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/BinaryFormat/Dwarf.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/Support/ErrorHandling.h"
19 using namespace dwarf
;
21 StringRef
llvm::dwarf::TagString(unsigned Tag
) {
25 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) \
27 return "DW_TAG_" #NAME;
28 #include "llvm/BinaryFormat/Dwarf.def"
32 unsigned llvm::dwarf::getTag(StringRef TagString
) {
33 return StringSwitch
<unsigned>(TagString
)
34 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) \
35 .Case("DW_TAG_" #NAME, DW_TAG_##NAME)
36 #include "llvm/BinaryFormat/Dwarf.def"
37 .Default(DW_TAG_invalid
);
40 unsigned llvm::dwarf::TagVersion(dwarf::Tag Tag
) {
44 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) \
47 #include "llvm/BinaryFormat/Dwarf.def"
51 unsigned llvm::dwarf::TagVendor(dwarf::Tag Tag
) {
55 #define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR, KIND) \
57 return DWARF_VENDOR_##VENDOR;
58 #include "llvm/BinaryFormat/Dwarf.def"
62 StringRef
llvm::dwarf::ChildrenString(unsigned Children
) {
65 return "DW_CHILDREN_no";
67 return "DW_CHILDREN_yes";
72 StringRef
llvm::dwarf::AttributeString(unsigned Attribute
) {
76 #define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) \
78 return "DW_AT_" #NAME;
79 #include "llvm/BinaryFormat/Dwarf.def"
83 unsigned llvm::dwarf::AttributeVersion(dwarf::Attribute Attribute
) {
87 #define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) \
90 #include "llvm/BinaryFormat/Dwarf.def"
94 unsigned llvm::dwarf::AttributeVendor(dwarf::Attribute Attribute
) {
98 #define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) \
100 return DWARF_VENDOR_##VENDOR;
101 #include "llvm/BinaryFormat/Dwarf.def"
105 StringRef
llvm::dwarf::FormEncodingString(unsigned Encoding
) {
109 #define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) \
110 case DW_FORM_##NAME: \
111 return "DW_FORM_" #NAME;
112 #include "llvm/BinaryFormat/Dwarf.def"
116 unsigned llvm::dwarf::FormVersion(dwarf::Form Form
) {
120 #define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) \
121 case DW_FORM_##NAME: \
123 #include "llvm/BinaryFormat/Dwarf.def"
127 unsigned llvm::dwarf::FormVendor(dwarf::Form Form
) {
131 #define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) \
132 case DW_FORM_##NAME: \
133 return DWARF_VENDOR_##VENDOR;
134 #include "llvm/BinaryFormat/Dwarf.def"
138 StringRef
llvm::dwarf::OperationEncodingString(unsigned Encoding
) {
142 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) \
144 return "DW_OP_" #NAME;
145 #include "llvm/BinaryFormat/Dwarf.def"
146 case DW_OP_LLVM_convert
:
147 return "DW_OP_LLVM_convert";
148 case DW_OP_LLVM_fragment
:
149 return "DW_OP_LLVM_fragment";
150 case DW_OP_LLVM_tag_offset
:
151 return "DW_OP_LLVM_tag_offset";
152 case DW_OP_LLVM_entry_value
:
153 return "DW_OP_LLVM_entry_value";
154 case DW_OP_LLVM_implicit_pointer
:
155 return "DW_OP_LLVM_implicit_pointer";
157 return "DW_OP_LLVM_arg";
161 unsigned llvm::dwarf::getOperationEncoding(StringRef OperationEncodingString
) {
162 return StringSwitch
<unsigned>(OperationEncodingString
)
163 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) \
164 .Case("DW_OP_" #NAME, DW_OP_##NAME)
165 #include "llvm/BinaryFormat/Dwarf.def"
166 .Case("DW_OP_LLVM_convert", DW_OP_LLVM_convert
)
167 .Case("DW_OP_LLVM_fragment", DW_OP_LLVM_fragment
)
168 .Case("DW_OP_LLVM_tag_offset", DW_OP_LLVM_tag_offset
)
169 .Case("DW_OP_LLVM_entry_value", DW_OP_LLVM_entry_value
)
170 .Case("DW_OP_LLVM_implicit_pointer", DW_OP_LLVM_implicit_pointer
)
171 .Case("DW_OP_LLVM_arg", DW_OP_LLVM_arg
)
175 unsigned llvm::dwarf::OperationVersion(dwarf::LocationAtom Op
) {
179 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) \
182 #include "llvm/BinaryFormat/Dwarf.def"
186 unsigned llvm::dwarf::OperationVendor(dwarf::LocationAtom Op
) {
190 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) \
192 return DWARF_VENDOR_##VENDOR;
193 #include "llvm/BinaryFormat/Dwarf.def"
197 StringRef
llvm::dwarf::AttributeEncodingString(unsigned Encoding
) {
201 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) \
202 case DW_ATE_##NAME: \
203 return "DW_ATE_" #NAME;
204 #include "llvm/BinaryFormat/Dwarf.def"
208 unsigned llvm::dwarf::getAttributeEncoding(StringRef EncodingString
) {
209 return StringSwitch
<unsigned>(EncodingString
)
210 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) \
211 .Case("DW_ATE_" #NAME, DW_ATE_##NAME)
212 #include "llvm/BinaryFormat/Dwarf.def"
216 unsigned llvm::dwarf::AttributeEncodingVersion(dwarf::TypeKind ATE
) {
220 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) \
221 case DW_ATE_##NAME: \
223 #include "llvm/BinaryFormat/Dwarf.def"
227 unsigned llvm::dwarf::AttributeEncodingVendor(dwarf::TypeKind ATE
) {
231 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) \
232 case DW_ATE_##NAME: \
233 return DWARF_VENDOR_##VENDOR;
234 #include "llvm/BinaryFormat/Dwarf.def"
238 StringRef
llvm::dwarf::DecimalSignString(unsigned Sign
) {
241 return "DW_DS_unsigned";
242 case DW_DS_leading_overpunch
:
243 return "DW_DS_leading_overpunch";
244 case DW_DS_trailing_overpunch
:
245 return "DW_DS_trailing_overpunch";
246 case DW_DS_leading_separate
:
247 return "DW_DS_leading_separate";
248 case DW_DS_trailing_separate
:
249 return "DW_DS_trailing_separate";
254 StringRef
llvm::dwarf::EndianityString(unsigned Endian
) {
257 return "DW_END_default";
261 return "DW_END_little";
263 return "DW_END_lo_user";
265 return "DW_END_hi_user";
270 StringRef
llvm::dwarf::AccessibilityString(unsigned Access
) {
272 // Accessibility codes
273 case DW_ACCESS_public
:
274 return "DW_ACCESS_public";
275 case DW_ACCESS_protected
:
276 return "DW_ACCESS_protected";
277 case DW_ACCESS_private
:
278 return "DW_ACCESS_private";
283 StringRef
llvm::dwarf::DefaultedMemberString(unsigned DefaultedEncodings
) {
284 switch (DefaultedEncodings
) {
285 // Defaulted Member Encodings codes
286 case DW_DEFAULTED_no
:
287 return "DW_DEFAULTED_no";
288 case DW_DEFAULTED_in_class
:
289 return "DW_DEFAULTED_in_class";
290 case DW_DEFAULTED_out_of_class
:
291 return "DW_DEFAULTED_out_of_class";
296 StringRef
llvm::dwarf::VisibilityString(unsigned Visibility
) {
297 switch (Visibility
) {
299 return "DW_VIS_local";
300 case DW_VIS_exported
:
301 return "DW_VIS_exported";
302 case DW_VIS_qualified
:
303 return "DW_VIS_qualified";
308 StringRef
llvm::dwarf::VirtualityString(unsigned Virtuality
) {
309 switch (Virtuality
) {
312 #define HANDLE_DW_VIRTUALITY(ID, NAME) \
313 case DW_VIRTUALITY_##NAME: \
314 return "DW_VIRTUALITY_" #NAME;
315 #include "llvm/BinaryFormat/Dwarf.def"
319 unsigned llvm::dwarf::getVirtuality(StringRef VirtualityString
) {
320 return StringSwitch
<unsigned>(VirtualityString
)
321 #define HANDLE_DW_VIRTUALITY(ID, NAME) \
322 .Case("DW_VIRTUALITY_" #NAME, DW_VIRTUALITY_##NAME)
323 #include "llvm/BinaryFormat/Dwarf.def"
324 .Default(DW_VIRTUALITY_invalid
);
327 StringRef
llvm::dwarf::LanguageString(unsigned Language
) {
331 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
332 case DW_LANG_##NAME: \
333 return "DW_LANG_" #NAME;
334 #include "llvm/BinaryFormat/Dwarf.def"
338 unsigned llvm::dwarf::getLanguage(StringRef LanguageString
) {
339 return StringSwitch
<unsigned>(LanguageString
)
340 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
341 .Case("DW_LANG_" #NAME, DW_LANG_##NAME)
342 #include "llvm/BinaryFormat/Dwarf.def"
346 unsigned llvm::dwarf::LanguageVersion(dwarf::SourceLanguage Lang
) {
350 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
351 case DW_LANG_##NAME: \
353 #include "llvm/BinaryFormat/Dwarf.def"
357 unsigned llvm::dwarf::LanguageVendor(dwarf::SourceLanguage Lang
) {
361 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
362 case DW_LANG_##NAME: \
363 return DWARF_VENDOR_##VENDOR;
364 #include "llvm/BinaryFormat/Dwarf.def"
368 Optional
<unsigned> llvm::dwarf::LanguageLowerBound(dwarf::SourceLanguage Lang
) {
372 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
373 case DW_LANG_##NAME: \
375 #include "llvm/BinaryFormat/Dwarf.def"
379 StringRef
llvm::dwarf::CaseString(unsigned Case
) {
381 case DW_ID_case_sensitive
:
382 return "DW_ID_case_sensitive";
384 return "DW_ID_up_case";
385 case DW_ID_down_case
:
386 return "DW_ID_down_case";
387 case DW_ID_case_insensitive
:
388 return "DW_ID_case_insensitive";
393 StringRef
llvm::dwarf::ConventionString(unsigned CC
) {
397 #define HANDLE_DW_CC(ID, NAME) \
399 return "DW_CC_" #NAME;
400 #include "llvm/BinaryFormat/Dwarf.def"
404 unsigned llvm::dwarf::getCallingConvention(StringRef CCString
) {
405 return StringSwitch
<unsigned>(CCString
)
406 #define HANDLE_DW_CC(ID, NAME) .Case("DW_CC_" #NAME, DW_CC_##NAME)
407 #include "llvm/BinaryFormat/Dwarf.def"
411 StringRef
llvm::dwarf::InlineCodeString(unsigned Code
) {
413 case DW_INL_not_inlined
:
414 return "DW_INL_not_inlined";
416 return "DW_INL_inlined";
417 case DW_INL_declared_not_inlined
:
418 return "DW_INL_declared_not_inlined";
419 case DW_INL_declared_inlined
:
420 return "DW_INL_declared_inlined";
425 StringRef
llvm::dwarf::ArrayOrderString(unsigned Order
) {
427 case DW_ORD_row_major
:
428 return "DW_ORD_row_major";
429 case DW_ORD_col_major
:
430 return "DW_ORD_col_major";
435 StringRef
llvm::dwarf::LNStandardString(unsigned Standard
) {
439 #define HANDLE_DW_LNS(ID, NAME) \
440 case DW_LNS_##NAME: \
441 return "DW_LNS_" #NAME;
442 #include "llvm/BinaryFormat/Dwarf.def"
446 StringRef
llvm::dwarf::LNExtendedString(unsigned Encoding
) {
450 #define HANDLE_DW_LNE(ID, NAME) \
451 case DW_LNE_##NAME: \
452 return "DW_LNE_" #NAME;
453 #include "llvm/BinaryFormat/Dwarf.def"
457 StringRef
llvm::dwarf::MacinfoString(unsigned Encoding
) {
459 // Macinfo Type Encodings
460 case DW_MACINFO_define
:
461 return "DW_MACINFO_define";
462 case DW_MACINFO_undef
:
463 return "DW_MACINFO_undef";
464 case DW_MACINFO_start_file
:
465 return "DW_MACINFO_start_file";
466 case DW_MACINFO_end_file
:
467 return "DW_MACINFO_end_file";
468 case DW_MACINFO_vendor_ext
:
469 return "DW_MACINFO_vendor_ext";
470 case DW_MACINFO_invalid
:
471 return "DW_MACINFO_invalid";
476 unsigned llvm::dwarf::getMacinfo(StringRef MacinfoString
) {
477 return StringSwitch
<unsigned>(MacinfoString
)
478 .Case("DW_MACINFO_define", DW_MACINFO_define
)
479 .Case("DW_MACINFO_undef", DW_MACINFO_undef
)
480 .Case("DW_MACINFO_start_file", DW_MACINFO_start_file
)
481 .Case("DW_MACINFO_end_file", DW_MACINFO_end_file
)
482 .Case("DW_MACINFO_vendor_ext", DW_MACINFO_vendor_ext
)
483 .Default(DW_MACINFO_invalid
);
486 StringRef
llvm::dwarf::MacroString(unsigned Encoding
) {
490 #define HANDLE_DW_MACRO(ID, NAME) \
491 case DW_MACRO_##NAME: \
492 return "DW_MACRO_" #NAME;
493 #include "llvm/BinaryFormat/Dwarf.def"
497 StringRef
llvm::dwarf::GnuMacroString(unsigned Encoding
) {
501 #define HANDLE_DW_MACRO_GNU(ID, NAME) \
502 case DW_MACRO_GNU_##NAME: \
503 return "DW_MACRO_GNU_" #NAME;
504 #include "llvm/BinaryFormat/Dwarf.def"
508 unsigned llvm::dwarf::getMacro(StringRef MacroString
) {
509 return StringSwitch
<unsigned>(MacroString
)
510 #define HANDLE_DW_MACRO(ID, NAME) .Case("DW_MACRO_" #NAME, ID)
511 #include "llvm/BinaryFormat/Dwarf.def"
512 .Default(DW_MACINFO_invalid
);
514 StringRef
llvm::dwarf::RangeListEncodingString(unsigned Encoding
) {
518 #define HANDLE_DW_RLE(ID, NAME) \
519 case DW_RLE_##NAME: \
520 return "DW_RLE_" #NAME;
521 #include "llvm/BinaryFormat/Dwarf.def"
525 StringRef
llvm::dwarf::LocListEncodingString(unsigned Encoding
) {
529 #define HANDLE_DW_LLE(ID, NAME) \
530 case DW_LLE_##NAME: \
531 return "DW_LLE_" #NAME;
532 #include "llvm/BinaryFormat/Dwarf.def"
536 StringRef
llvm::dwarf::CallFrameString(unsigned Encoding
,
537 Triple::ArchType Arch
) {
538 assert(Arch
!= llvm::Triple::ArchType::UnknownArch
);
539 #define SELECT_AARCH64 (Arch == llvm::Triple::aarch64_be || Arch == llvm::Triple::aarch64)
540 #define SELECT_MIPS64 Arch == llvm::Triple::mips64
541 #define SELECT_SPARC (Arch == llvm::Triple::sparc || Arch == llvm::Triple::sparcv9)
542 #define SELECT_X86 (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64)
543 #define HANDLE_DW_CFA(ID, NAME)
544 #define HANDLE_DW_CFA_PRED(ID, NAME, PRED) \
545 if (ID == Encoding && PRED) \
546 return "DW_CFA_" #NAME;
547 #include "llvm/BinaryFormat/Dwarf.def"
552 #define HANDLE_DW_CFA_PRED(ID, NAME, PRED)
553 #define HANDLE_DW_CFA(ID, NAME) \
554 case DW_CFA_##NAME: \
555 return "DW_CFA_" #NAME;
556 #include "llvm/BinaryFormat/Dwarf.def"
561 #undef SELECT_AARCH64
565 StringRef
llvm::dwarf::ApplePropertyString(unsigned Prop
) {
569 #define HANDLE_DW_APPLE_PROPERTY(ID, NAME) \
570 case DW_APPLE_PROPERTY_##NAME: \
571 return "DW_APPLE_PROPERTY_" #NAME;
572 #include "llvm/BinaryFormat/Dwarf.def"
576 StringRef
llvm::dwarf::UnitTypeString(unsigned UT
) {
580 #define HANDLE_DW_UT(ID, NAME) \
582 return "DW_UT_" #NAME;
583 #include "llvm/BinaryFormat/Dwarf.def"
587 StringRef
llvm::dwarf::AtomTypeString(unsigned AT
) {
589 case dwarf::DW_ATOM_null
:
590 return "DW_ATOM_null";
591 case dwarf::DW_ATOM_die_offset
:
592 return "DW_ATOM_die_offset";
593 case DW_ATOM_cu_offset
:
594 return "DW_ATOM_cu_offset";
595 case DW_ATOM_die_tag
:
596 return "DW_ATOM_die_tag";
597 case DW_ATOM_type_flags
:
598 case DW_ATOM_type_type_flags
:
599 return "DW_ATOM_type_flags";
600 case DW_ATOM_qual_name_hash
:
601 return "DW_ATOM_qual_name_hash";
606 StringRef
llvm::dwarf::GDBIndexEntryKindString(GDBIndexEntryKind Kind
) {
625 llvm_unreachable("Unknown GDBIndexEntryKind value");
629 llvm::dwarf::GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage
) {
636 llvm_unreachable("Unknown GDBIndexEntryLinkage value");
639 StringRef
llvm::dwarf::AttributeValueString(uint16_t Attr
, unsigned Val
) {
641 case DW_AT_accessibility
:
642 return AccessibilityString(Val
);
643 case DW_AT_virtuality
:
644 return VirtualityString(Val
);
646 return LanguageString(Val
);
648 return AttributeEncodingString(Val
);
649 case DW_AT_decimal_sign
:
650 return DecimalSignString(Val
);
651 case DW_AT_endianity
:
652 return EndianityString(Val
);
653 case DW_AT_visibility
:
654 return VisibilityString(Val
);
655 case DW_AT_identifier_case
:
656 return CaseString(Val
);
657 case DW_AT_calling_convention
:
658 return ConventionString(Val
);
660 return InlineCodeString(Val
);
662 return ArrayOrderString(Val
);
663 case DW_AT_APPLE_runtime_class
:
664 return LanguageString(Val
);
665 case DW_AT_defaulted
:
666 return DefaultedMemberString(Val
);
672 StringRef
llvm::dwarf::AtomValueString(uint16_t Atom
, unsigned Val
) {
676 case DW_ATOM_die_tag
:
677 return TagString(Val
);
683 StringRef
llvm::dwarf::IndexString(unsigned Idx
) {
687 #define HANDLE_DW_IDX(ID, NAME) \
688 case DW_IDX_##NAME: \
689 return "DW_IDX_" #NAME;
690 #include "llvm/BinaryFormat/Dwarf.def"
694 Optional
<uint8_t> llvm::dwarf::getFixedFormByteSize(dwarf::Form Form
,
699 return Params
.AddrSize
;
702 case DW_FORM_block
: // ULEB128 length L followed by L bytes.
703 case DW_FORM_block1
: // 1 byte length L followed by L bytes.
704 case DW_FORM_block2
: // 2 byte length L followed by L bytes.
705 case DW_FORM_block4
: // 4 byte length L followed by L bytes.
706 case DW_FORM_string
: // C-string with null terminator.
707 case DW_FORM_sdata
: // SLEB128.
708 case DW_FORM_udata
: // ULEB128.
709 case DW_FORM_ref_udata
: // ULEB128.
710 case DW_FORM_indirect
: // ULEB128.
711 case DW_FORM_exprloc
: // ULEB128 length L followed by L bytes.
712 case DW_FORM_strx
: // ULEB128.
713 case DW_FORM_addrx
: // ULEB128.
714 case DW_FORM_loclistx
: // ULEB128.
715 case DW_FORM_rnglistx
: // ULEB128.
716 case DW_FORM_GNU_addr_index
: // ULEB128.
717 case DW_FORM_GNU_str_index
: // ULEB128.
720 case DW_FORM_ref_addr
:
722 return Params
.getRefAddrByteSize();
743 case DW_FORM_ref_sup4
:
749 case DW_FORM_GNU_ref_alt
:
750 case DW_FORM_GNU_strp_alt
:
751 case DW_FORM_line_strp
:
752 case DW_FORM_sec_offset
:
753 case DW_FORM_strp_sup
:
755 return Params
.getDwarfOffsetByteSize();
760 case DW_FORM_ref_sig8
:
761 case DW_FORM_ref_sup8
:
764 case DW_FORM_flag_present
:
770 case DW_FORM_implicit_const
:
771 // The implicit value is stored in the abbreviation as a SLEB128, and
772 // there no data in debug info.
781 bool llvm::dwarf::isValidFormForVersion(Form F
, unsigned Version
,
783 if (FormVendor(F
) == DWARF_VENDOR_DWARF
) {
784 unsigned FV
= FormVersion(F
);
785 return FV
> 0 && FV
<= Version
;
790 StringRef
llvm::dwarf::FormatString(DwarfFormat Format
) {
800 StringRef
llvm::dwarf::FormatString(bool IsDWARF64
) {
801 return FormatString(IsDWARF64
? DWARF64
: DWARF32
);
804 StringRef
llvm::dwarf::RLEString(unsigned RLE
) {
808 #define HANDLE_DW_RLE(ID, NAME) \
809 case DW_RLE_##NAME: \
810 return "DW_RLE_" #NAME;
811 #include "llvm/BinaryFormat/Dwarf.def"
815 constexpr char llvm::dwarf::EnumTraits
<Attribute
>::Type
[];
816 constexpr char llvm::dwarf::EnumTraits
<Form
>::Type
[];
817 constexpr char llvm::dwarf::EnumTraits
<Index
>::Type
[];
818 constexpr char llvm::dwarf::EnumTraits
<Tag
>::Type
[];
819 constexpr char llvm::dwarf::EnumTraits
<LineNumberOps
>::Type
[];
820 constexpr char llvm::dwarf::EnumTraits
<LocationAtom
>::Type
[];