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";
157 unsigned llvm::dwarf::getOperationEncoding(StringRef OperationEncodingString
) {
158 return StringSwitch
<unsigned>(OperationEncodingString
)
159 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) \
160 .Case("DW_OP_" #NAME, DW_OP_##NAME)
161 #include "llvm/BinaryFormat/Dwarf.def"
162 .Case("DW_OP_LLVM_convert", DW_OP_LLVM_convert
)
163 .Case("DW_OP_LLVM_fragment", DW_OP_LLVM_fragment
)
164 .Case("DW_OP_LLVM_tag_offset", DW_OP_LLVM_tag_offset
)
165 .Case("DW_OP_LLVM_entry_value", DW_OP_LLVM_entry_value
)
169 unsigned llvm::dwarf::OperationVersion(dwarf::LocationAtom Op
) {
173 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) \
176 #include "llvm/BinaryFormat/Dwarf.def"
180 unsigned llvm::dwarf::OperationVendor(dwarf::LocationAtom Op
) {
184 #define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) \
186 return DWARF_VENDOR_##VENDOR;
187 #include "llvm/BinaryFormat/Dwarf.def"
191 StringRef
llvm::dwarf::AttributeEncodingString(unsigned Encoding
) {
195 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) \
196 case DW_ATE_##NAME: \
197 return "DW_ATE_" #NAME;
198 #include "llvm/BinaryFormat/Dwarf.def"
202 unsigned llvm::dwarf::getAttributeEncoding(StringRef EncodingString
) {
203 return StringSwitch
<unsigned>(EncodingString
)
204 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) \
205 .Case("DW_ATE_" #NAME, DW_ATE_##NAME)
206 #include "llvm/BinaryFormat/Dwarf.def"
210 unsigned llvm::dwarf::AttributeEncodingVersion(dwarf::TypeKind ATE
) {
214 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) \
215 case DW_ATE_##NAME: \
217 #include "llvm/BinaryFormat/Dwarf.def"
221 unsigned llvm::dwarf::AttributeEncodingVendor(dwarf::TypeKind ATE
) {
225 #define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) \
226 case DW_ATE_##NAME: \
227 return DWARF_VENDOR_##VENDOR;
228 #include "llvm/BinaryFormat/Dwarf.def"
232 StringRef
llvm::dwarf::DecimalSignString(unsigned Sign
) {
235 return "DW_DS_unsigned";
236 case DW_DS_leading_overpunch
:
237 return "DW_DS_leading_overpunch";
238 case DW_DS_trailing_overpunch
:
239 return "DW_DS_trailing_overpunch";
240 case DW_DS_leading_separate
:
241 return "DW_DS_leading_separate";
242 case DW_DS_trailing_separate
:
243 return "DW_DS_trailing_separate";
248 StringRef
llvm::dwarf::EndianityString(unsigned Endian
) {
251 return "DW_END_default";
255 return "DW_END_little";
257 return "DW_END_lo_user";
259 return "DW_END_hi_user";
264 StringRef
llvm::dwarf::AccessibilityString(unsigned Access
) {
266 // Accessibility codes
267 case DW_ACCESS_public
:
268 return "DW_ACCESS_public";
269 case DW_ACCESS_protected
:
270 return "DW_ACCESS_protected";
271 case DW_ACCESS_private
:
272 return "DW_ACCESS_private";
277 StringRef
llvm::dwarf::VisibilityString(unsigned Visibility
) {
278 switch (Visibility
) {
280 return "DW_VIS_local";
281 case DW_VIS_exported
:
282 return "DW_VIS_exported";
283 case DW_VIS_qualified
:
284 return "DW_VIS_qualified";
289 StringRef
llvm::dwarf::VirtualityString(unsigned Virtuality
) {
290 switch (Virtuality
) {
293 #define HANDLE_DW_VIRTUALITY(ID, NAME) \
294 case DW_VIRTUALITY_##NAME: \
295 return "DW_VIRTUALITY_" #NAME;
296 #include "llvm/BinaryFormat/Dwarf.def"
300 unsigned llvm::dwarf::getVirtuality(StringRef VirtualityString
) {
301 return StringSwitch
<unsigned>(VirtualityString
)
302 #define HANDLE_DW_VIRTUALITY(ID, NAME) \
303 .Case("DW_VIRTUALITY_" #NAME, DW_VIRTUALITY_##NAME)
304 #include "llvm/BinaryFormat/Dwarf.def"
305 .Default(DW_VIRTUALITY_invalid
);
308 StringRef
llvm::dwarf::LanguageString(unsigned Language
) {
312 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
313 case DW_LANG_##NAME: \
314 return "DW_LANG_" #NAME;
315 #include "llvm/BinaryFormat/Dwarf.def"
319 unsigned llvm::dwarf::getLanguage(StringRef LanguageString
) {
320 return StringSwitch
<unsigned>(LanguageString
)
321 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
322 .Case("DW_LANG_" #NAME, DW_LANG_##NAME)
323 #include "llvm/BinaryFormat/Dwarf.def"
327 unsigned llvm::dwarf::LanguageVersion(dwarf::SourceLanguage Lang
) {
331 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
332 case DW_LANG_##NAME: \
334 #include "llvm/BinaryFormat/Dwarf.def"
338 unsigned llvm::dwarf::LanguageVendor(dwarf::SourceLanguage Lang
) {
342 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
343 case DW_LANG_##NAME: \
344 return DWARF_VENDOR_##VENDOR;
345 #include "llvm/BinaryFormat/Dwarf.def"
349 Optional
<unsigned> llvm::dwarf::LanguageLowerBound(dwarf::SourceLanguage Lang
) {
353 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
354 case DW_LANG_##NAME: \
356 #include "llvm/BinaryFormat/Dwarf.def"
360 StringRef
llvm::dwarf::CaseString(unsigned Case
) {
362 case DW_ID_case_sensitive
:
363 return "DW_ID_case_sensitive";
365 return "DW_ID_up_case";
366 case DW_ID_down_case
:
367 return "DW_ID_down_case";
368 case DW_ID_case_insensitive
:
369 return "DW_ID_case_insensitive";
374 StringRef
llvm::dwarf::ConventionString(unsigned CC
) {
378 #define HANDLE_DW_CC(ID, NAME) \
380 return "DW_CC_" #NAME;
381 #include "llvm/BinaryFormat/Dwarf.def"
385 unsigned llvm::dwarf::getCallingConvention(StringRef CCString
) {
386 return StringSwitch
<unsigned>(CCString
)
387 #define HANDLE_DW_CC(ID, NAME) .Case("DW_CC_" #NAME, DW_CC_##NAME)
388 #include "llvm/BinaryFormat/Dwarf.def"
392 StringRef
llvm::dwarf::InlineCodeString(unsigned Code
) {
394 case DW_INL_not_inlined
:
395 return "DW_INL_not_inlined";
397 return "DW_INL_inlined";
398 case DW_INL_declared_not_inlined
:
399 return "DW_INL_declared_not_inlined";
400 case DW_INL_declared_inlined
:
401 return "DW_INL_declared_inlined";
406 StringRef
llvm::dwarf::ArrayOrderString(unsigned Order
) {
408 case DW_ORD_row_major
:
409 return "DW_ORD_row_major";
410 case DW_ORD_col_major
:
411 return "DW_ORD_col_major";
416 StringRef
llvm::dwarf::LNStandardString(unsigned Standard
) {
420 #define HANDLE_DW_LNS(ID, NAME) \
421 case DW_LNS_##NAME: \
422 return "DW_LNS_" #NAME;
423 #include "llvm/BinaryFormat/Dwarf.def"
427 StringRef
llvm::dwarf::LNExtendedString(unsigned Encoding
) {
431 #define HANDLE_DW_LNE(ID, NAME) \
432 case DW_LNE_##NAME: \
433 return "DW_LNE_" #NAME;
434 #include "llvm/BinaryFormat/Dwarf.def"
438 StringRef
llvm::dwarf::MacinfoString(unsigned Encoding
) {
440 // Macinfo Type Encodings
441 case DW_MACINFO_define
:
442 return "DW_MACINFO_define";
443 case DW_MACINFO_undef
:
444 return "DW_MACINFO_undef";
445 case DW_MACINFO_start_file
:
446 return "DW_MACINFO_start_file";
447 case DW_MACINFO_end_file
:
448 return "DW_MACINFO_end_file";
449 case DW_MACINFO_vendor_ext
:
450 return "DW_MACINFO_vendor_ext";
451 case DW_MACINFO_invalid
:
452 return "DW_MACINFO_invalid";
457 unsigned llvm::dwarf::getMacinfo(StringRef MacinfoString
) {
458 return StringSwitch
<unsigned>(MacinfoString
)
459 .Case("DW_MACINFO_define", DW_MACINFO_define
)
460 .Case("DW_MACINFO_undef", DW_MACINFO_undef
)
461 .Case("DW_MACINFO_start_file", DW_MACINFO_start_file
)
462 .Case("DW_MACINFO_end_file", DW_MACINFO_end_file
)
463 .Case("DW_MACINFO_vendor_ext", DW_MACINFO_vendor_ext
)
464 .Default(DW_MACINFO_invalid
);
467 StringRef
llvm::dwarf::RangeListEncodingString(unsigned Encoding
) {
471 #define HANDLE_DW_RLE(ID, NAME) \
472 case DW_RLE_##NAME: \
473 return "DW_RLE_" #NAME;
474 #include "llvm/BinaryFormat/Dwarf.def"
478 StringRef
llvm::dwarf::LocListEncodingString(unsigned Encoding
) {
482 #define HANDLE_DW_LLE(ID, NAME) \
483 case DW_LLE_##NAME: \
484 return "DW_LLE_" #NAME;
485 #include "llvm/BinaryFormat/Dwarf.def"
489 StringRef
llvm::dwarf::CallFrameString(unsigned Encoding
,
490 Triple::ArchType Arch
) {
491 assert(Arch
!= llvm::Triple::ArchType::UnknownArch
);
492 #define SELECT_AARCH64 (Arch == llvm::Triple::aarch64_be || Arch == llvm::Triple::aarch64)
493 #define SELECT_MIPS64 Arch == llvm::Triple::mips64
494 #define SELECT_SPARC (Arch == llvm::Triple::sparc || Arch == llvm::Triple::sparcv9)
495 #define SELECT_X86 (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64)
496 #define HANDLE_DW_CFA(ID, NAME)
497 #define HANDLE_DW_CFA_PRED(ID, NAME, PRED) \
498 if (ID == Encoding && PRED) \
499 return "DW_CFA_" #NAME;
500 #include "llvm/BinaryFormat/Dwarf.def"
505 #define HANDLE_DW_CFA_PRED(ID, NAME, PRED)
506 #define HANDLE_DW_CFA(ID, NAME) \
507 case DW_CFA_##NAME: \
508 return "DW_CFA_" #NAME;
509 #include "llvm/BinaryFormat/Dwarf.def"
514 #undef SELECT_AARCH64
518 StringRef
llvm::dwarf::ApplePropertyString(unsigned Prop
) {
522 #define HANDLE_DW_APPLE_PROPERTY(ID, NAME) \
523 case DW_APPLE_PROPERTY_##NAME: \
524 return "DW_APPLE_PROPERTY_" #NAME;
525 #include "llvm/BinaryFormat/Dwarf.def"
529 StringRef
llvm::dwarf::UnitTypeString(unsigned UT
) {
533 #define HANDLE_DW_UT(ID, NAME) \
535 return "DW_UT_" #NAME;
536 #include "llvm/BinaryFormat/Dwarf.def"
540 StringRef
llvm::dwarf::AtomTypeString(unsigned AT
) {
542 case dwarf::DW_ATOM_null
:
543 return "DW_ATOM_null";
544 case dwarf::DW_ATOM_die_offset
:
545 return "DW_ATOM_die_offset";
546 case DW_ATOM_cu_offset
:
547 return "DW_ATOM_cu_offset";
548 case DW_ATOM_die_tag
:
549 return "DW_ATOM_die_tag";
550 case DW_ATOM_type_flags
:
551 case DW_ATOM_type_type_flags
:
552 return "DW_ATOM_type_flags";
553 case DW_ATOM_qual_name_hash
:
554 return "DW_ATOM_qual_name_hash";
559 StringRef
llvm::dwarf::GDBIndexEntryKindString(GDBIndexEntryKind Kind
) {
578 llvm_unreachable("Unknown GDBIndexEntryKind value");
582 llvm::dwarf::GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage
) {
589 llvm_unreachable("Unknown GDBIndexEntryLinkage value");
592 StringRef
llvm::dwarf::AttributeValueString(uint16_t Attr
, unsigned Val
) {
594 case DW_AT_accessibility
:
595 return AccessibilityString(Val
);
596 case DW_AT_virtuality
:
597 return VirtualityString(Val
);
599 return LanguageString(Val
);
601 return AttributeEncodingString(Val
);
602 case DW_AT_decimal_sign
:
603 return DecimalSignString(Val
);
604 case DW_AT_endianity
:
605 return EndianityString(Val
);
606 case DW_AT_visibility
:
607 return VisibilityString(Val
);
608 case DW_AT_identifier_case
:
609 return CaseString(Val
);
610 case DW_AT_calling_convention
:
611 return ConventionString(Val
);
613 return InlineCodeString(Val
);
615 return ArrayOrderString(Val
);
616 case DW_AT_APPLE_runtime_class
:
617 return LanguageString(Val
);
623 StringRef
llvm::dwarf::AtomValueString(uint16_t Atom
, unsigned Val
) {
627 case DW_ATOM_die_tag
:
628 return TagString(Val
);
634 StringRef
llvm::dwarf::IndexString(unsigned Idx
) {
638 #define HANDLE_DW_IDX(ID, NAME) \
639 case DW_IDX_##NAME: \
640 return "DW_IDX_" #NAME;
641 #include "llvm/BinaryFormat/Dwarf.def"
645 Optional
<uint8_t> llvm::dwarf::getFixedFormByteSize(dwarf::Form Form
,
650 return Params
.AddrSize
;
653 case DW_FORM_block
: // ULEB128 length L followed by L bytes.
654 case DW_FORM_block1
: // 1 byte length L followed by L bytes.
655 case DW_FORM_block2
: // 2 byte length L followed by L bytes.
656 case DW_FORM_block4
: // 4 byte length L followed by L bytes.
657 case DW_FORM_string
: // C-string with null terminator.
658 case DW_FORM_sdata
: // SLEB128.
659 case DW_FORM_udata
: // ULEB128.
660 case DW_FORM_ref_udata
: // ULEB128.
661 case DW_FORM_indirect
: // ULEB128.
662 case DW_FORM_exprloc
: // ULEB128 length L followed by L bytes.
663 case DW_FORM_strx
: // ULEB128.
664 case DW_FORM_addrx
: // ULEB128.
665 case DW_FORM_loclistx
: // ULEB128.
666 case DW_FORM_rnglistx
: // ULEB128.
667 case DW_FORM_GNU_addr_index
: // ULEB128.
668 case DW_FORM_GNU_str_index
: // ULEB128.
671 case DW_FORM_ref_addr
:
673 return Params
.getRefAddrByteSize();
694 case DW_FORM_ref_sup4
:
700 case DW_FORM_GNU_ref_alt
:
701 case DW_FORM_GNU_strp_alt
:
702 case DW_FORM_line_strp
:
703 case DW_FORM_sec_offset
:
704 case DW_FORM_strp_sup
:
706 return Params
.getDwarfOffsetByteSize();
711 case DW_FORM_ref_sig8
:
712 case DW_FORM_ref_sup8
:
715 case DW_FORM_flag_present
:
721 case DW_FORM_implicit_const
:
722 // The implicit value is stored in the abbreviation as a SLEB128, and
723 // there no data in debug info.
732 bool llvm::dwarf::isValidFormForVersion(Form F
, unsigned Version
,
734 if (FormVendor(F
) == DWARF_VENDOR_DWARF
) {
735 unsigned FV
= FormVersion(F
);
736 return FV
> 0 && FV
<= Version
;
741 constexpr char llvm::dwarf::EnumTraits
<Attribute
>::Type
[];
742 constexpr char llvm::dwarf::EnumTraits
<Form
>::Type
[];
743 constexpr char llvm::dwarf::EnumTraits
<Index
>::Type
[];
744 constexpr char llvm::dwarf::EnumTraits
<Tag
>::Type
[];