Dwarf.h revision 341825
125603Skjc//===-- llvm/BinaryFormat/Dwarf.h ---Dwarf Constants-------------*- C++ -*-===// 225603Skjc// 325603Skjc// The LLVM Compiler Infrastructure 425603Skjc// 525603Skjc// This file is distributed under the University of Illinois Open Source 625603Skjc// License. See LICENSE.TXT for details. 725603Skjc// 825603Skjc//===----------------------------------------------------------------------===// 925603Skjc// 1025603Skjc/// \file 1125603Skjc/// This file contains constants used for implementing Dwarf 1225603Skjc/// debug support. 1325603Skjc/// 1425603Skjc/// For details on the Dwarf specfication see the latest DWARF Debugging 1525603Skjc/// Information Format standard document on http://www.dwarfstd.org. This 1625603Skjc/// file often includes support for non-released standard features. 1725603Skjc// 1825603Skjc//===----------------------------------------------------------------------===// 1925603Skjc 2025603Skjc#ifndef LLVM_BINARYFORMAT_DWARF_H 2125603Skjc#define LLVM_BINARYFORMAT_DWARF_H 2225603Skjc 2325603Skjc#include "llvm/ADT/Optional.h" 2425603Skjc#include "llvm/Support/Compiler.h" 2525603Skjc#include "llvm/Support/DataTypes.h" 2625603Skjc#include "llvm/Support/ErrorHandling.h" 2725603Skjc#include "llvm/Support/Format.h" 2825603Skjc#include "llvm/Support/FormatVariadicDetails.h" 2925603Skjc 3025603Skjcnamespace llvm { 3125603Skjcclass StringRef; 3225603Skjc 3354263Sshinnamespace dwarf { 3454263Sshin 3525603Skjc//===----------------------------------------------------------------------===// 3625603Skjc// DWARF constants as gleaned from the DWARF Debugging Information Format V.5 3725603Skjc// reference manual http://www.dwarfstd.org/. 3825603Skjc// 3925603Skjc 4025603Skjc// Do not mix the following two enumerations sets. DW_TAG_invalid changes the 4132350Seivind// enumeration base type. 4254263Sshin 43105576Srwatsonenum LLVMConstants : uint32_t { 4432925Seivind // LLVM mock tags (see also llvm/BinaryFormat/Dwarf.def). 4532350Seivind DW_TAG_invalid = ~0U, // Tag for invalid results. 4625603Skjc DW_VIRTUALITY_invalid = ~0U, // Virtuality for invalid results. 4725603Skjc DW_MACINFO_invalid = ~0U, // Macinfo type for invalid results. 48114201Sharti 49114201Sharti // Other constants. 50105576Srwatson DWARF_VERSION = 4, // Default dwarf version we output. 5125603Skjc DW_PUBTYPES_VERSION = 2, // Section version number for .debug_pubtypes. 5225603Skjc DW_PUBNAMES_VERSION = 2, // Section version number for .debug_pubnames. 5337939Skjc DW_ARANGES_VERSION = 2, // Section version number for .debug_aranges. 5437939Skjc // Identifiers we use to distinguish vendor extensions. 55114201Sharti DWARF_VENDOR_DWARF = 0, // Defined in v2 or later of the DWARF standard. 5625603Skjc DWARF_VENDOR_APPLE = 1, 5725603Skjc DWARF_VENDOR_BORLAND = 2, 5825603Skjc DWARF_VENDOR_GNU = 3, 5925603Skjc DWARF_VENDOR_GOOGLE = 4, 6025603Skjc DWARF_VENDOR_LLVM = 5, 6125603Skjc DWARF_VENDOR_MIPS = 6 6225603Skjc}; 6325603Skjc 6425603Skjc/// Constants that define the DWARF format as 32 or 64 bit. 6525603Skjcenum DwarfFormat : uint8_t { DWARF32, DWARF64 }; 6625603Skjc 6737939Skjc/// Special ID values that distinguish a CIE from a FDE in DWARF CFI. 6825603Skjc/// Not inside an enum because a 64-bit value is needed. 6925603Skjc/// @{ 7025603Skjcconst uint32_t DW_CIE_ID = UINT32_MAX; 7125603Skjcconst uint64_t DW64_CIE_ID = UINT64_MAX; 7225603Skjc/// @} 7325603Skjc 74114201Sharti/// Identifier of an invalid DIE offset in the .debug_info section. 75114201Sharticonst uint32_t DW_INVALID_OFFSET = UINT32_MAX; 7637939Skjc 7737939Skjcenum Tag : uint16_t { 7837939Skjc#define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR) DW_TAG_##NAME = ID, 7925603Skjc#include "llvm/BinaryFormat/Dwarf.def" 8025603Skjc DW_TAG_lo_user = 0x4080, 8125603Skjc DW_TAG_hi_user = 0xffff, 8225603Skjc DW_TAG_user_base = 0x1000 ///< Recommended base for user tags. 8325603Skjc}; 8425603Skjc 8525603Skjcinline bool isType(Tag T) { 8625603Skjc switch (T) { 8725603Skjc case DW_TAG_array_type: 8825603Skjc case DW_TAG_class_type: 8925603Skjc case DW_TAG_interface_type: 9025603Skjc case DW_TAG_enumeration_type: 9125603Skjc case DW_TAG_pointer_type: 9225603Skjc case DW_TAG_reference_type: 9325603Skjc case DW_TAG_rvalue_reference_type: 9425603Skjc case DW_TAG_string_type: 9525603Skjc case DW_TAG_structure_type: 9625603Skjc case DW_TAG_subroutine_type: 9725603Skjc case DW_TAG_union_type: 9825603Skjc case DW_TAG_ptr_to_member_type: 99111774Smdodd case DW_TAG_set_type: 10025603Skjc case DW_TAG_subrange_type: 10125603Skjc case DW_TAG_base_type: 10225603Skjc case DW_TAG_const_type: 10325603Skjc case DW_TAG_file_type: 10425603Skjc case DW_TAG_packed_type: 10578249Speter case DW_TAG_volatile_type: 10625603Skjc case DW_TAG_typedef: 10759633Skjc return true; 10859633Skjc default: 10925603Skjc return false; 11037939Skjc } 11125603Skjc} 11225603Skjc 113105576Srwatson/// Attributes. 114105576Srwatsonenum Attribute : uint16_t { 115105576Srwatson#define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) DW_AT_##NAME = ID, 116105576Srwatson#include "llvm/BinaryFormat/Dwarf.def" 117105576Srwatson DW_AT_lo_user = 0x2000, 118105576Srwatson DW_AT_hi_user = 0x3fff, 11925603Skjc}; 12025603Skjc 12125603Skjcenum Form : uint16_t { 12225603Skjc#define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) DW_FORM_##NAME = ID, 12325603Skjc#include "llvm/BinaryFormat/Dwarf.def" 12425603Skjc DW_FORM_lo_user = 0x1f00, ///< Not specified by DWARF. 125111767Smdodd}; 126111767Smdodd 127111767Smdoddenum LocationAtom { 12825603Skjc#define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) DW_OP_##NAME = ID, 12925603Skjc#include "llvm/BinaryFormat/Dwarf.def" 13025603Skjc DW_OP_lo_user = 0xe0, 13125603Skjc DW_OP_hi_user = 0xff, 13225603Skjc DW_OP_LLVM_fragment = 0x1000 ///< Only used in LLVM metadata. 13325603Skjc}; 13437939Skjc 13525603Skjcenum TypeKind : uint8_t { 13637939Skjc#define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) DW_ATE_##NAME = ID, 13746695Skjc#include "llvm/BinaryFormat/Dwarf.def" 138112193Sharti DW_ATE_lo_user = 0x80, 13946695Skjc DW_ATE_hi_user = 0xff 140112193Sharti}; 14125603Skjc 14225603Skjcenum DecimalSignEncoding { 14325603Skjc // Decimal sign attribute values 14425603Skjc DW_DS_unsigned = 0x01, 14525603Skjc DW_DS_leading_overpunch = 0x02, 14625603Skjc DW_DS_trailing_overpunch = 0x03, 14725603Skjc DW_DS_leading_separate = 0x04, 14825603Skjc DW_DS_trailing_separate = 0x05 14937939Skjc}; 15025603Skjc 15137939Skjcenum EndianityEncoding { 15237939Skjc // Endianity attribute values 15346695Skjc DW_END_default = 0x00, 15446695Skjc DW_END_big = 0x01, 15537939Skjc DW_END_little = 0x02, 15637939Skjc DW_END_lo_user = 0x40, 15737939Skjc DW_END_hi_user = 0xff 15837939Skjc}; 15937939Skjc 16025603Skjcenum AccessAttribute { 16125603Skjc // Accessibility codes 16225603Skjc DW_ACCESS_public = 0x01, 16325603Skjc DW_ACCESS_protected = 0x02, 16425603Skjc DW_ACCESS_private = 0x03 16525603Skjc}; 16625603Skjc 16725603Skjcenum VisibilityAttribute { 16825603Skjc // Visibility codes 16925603Skjc DW_VIS_local = 0x01, 17025603Skjc DW_VIS_exported = 0x02, 17125603Skjc DW_VIS_qualified = 0x03 17225603Skjc}; 17325603Skjc 17425603Skjcenum VirtualityAttribute { 17525603Skjc#define HANDLE_DW_VIRTUALITY(ID, NAME) DW_VIRTUALITY_##NAME = ID, 17625603Skjc#include "llvm/BinaryFormat/Dwarf.def" 177111119Simp DW_VIRTUALITY_max = 0x02 17825603Skjc}; 17925603Skjc 18025603Skjcenum DefaultedMemberAttribute { 18125603Skjc#define HANDLE_DW_DEFAULTED(ID, NAME) DW_DEFAULTED_##NAME = ID, 18225603Skjc#include "llvm/BinaryFormat/Dwarf.def" 18325603Skjc DW_DEFAULTED_max = 0x02 18437939Skjc}; 18537939Skjc 18637939Skjcenum SourceLanguage { 18737939Skjc#define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) DW_LANG_##NAME = ID, 188112193Sharti#include "llvm/BinaryFormat/Dwarf.def" 18937939Skjc DW_LANG_lo_user = 0x8000, 19037939Skjc DW_LANG_hi_user = 0xffff 19137939Skjc}; 19225603Skjc 19325603Skjcenum CaseSensitivity { 19425603Skjc // Identifier case codes 19525603Skjc DW_ID_case_sensitive = 0x00, 19625603Skjc DW_ID_up_case = 0x01, 19725603Skjc DW_ID_down_case = 0x02, 19825603Skjc DW_ID_case_insensitive = 0x03 19969152Sjlemon}; 20069152Sjlemon 20125603Skjcenum CallingConvention { 20225603Skjc// Calling convention codes 20325603Skjc#define HANDLE_DW_CC(ID, NAME) DW_CC_##NAME = ID, 20425603Skjc#include "llvm/BinaryFormat/Dwarf.def" 20525603Skjc DW_CC_lo_user = 0x40, 20625603Skjc DW_CC_hi_user = 0xff 20725603Skjc}; 20825603Skjc 20925603Skjcenum InlineAttribute { 21025603Skjc // Inline codes 21125603Skjc DW_INL_not_inlined = 0x00, 21225603Skjc DW_INL_inlined = 0x01, 21325603Skjc DW_INL_declared_not_inlined = 0x02, 21425603Skjc DW_INL_declared_inlined = 0x03 21525603Skjc}; 216111774Smdodd 21725603Skjcenum ArrayDimensionOrdering { 21825603Skjc // Array ordering 21925603Skjc DW_ORD_row_major = 0x00, 220111888Sjlemon DW_ORD_col_major = 0x01 22125603Skjc}; 22225603Skjc 22325603Skjcenum DiscriminantList { 22425603Skjc // Discriminant descriptor values 22525603Skjc DW_DSC_label = 0x00, 22625603Skjc DW_DSC_range = 0x01 22725603Skjc}; 228105576Srwatson 229105576Srwatson/// Line Number Standard Opcode Encodings. 230105576Srwatsonenum LineNumberOps : uint8_t { 23125603Skjc#define HANDLE_DW_LNS(ID, NAME) DW_LNS_##NAME = ID, 23225603Skjc#include "llvm/BinaryFormat/Dwarf.def" 23325603Skjc}; 23425603Skjc 23537939Skjc/// Line Number Extended Opcode Encodings. 23637939Skjcenum LineNumberExtendedOps { 23737939Skjc#define HANDLE_DW_LNE(ID, NAME) DW_LNE_##NAME = ID, 23837939Skjc#include "llvm/BinaryFormat/Dwarf.def" 239111888Sjlemon DW_LNE_lo_user = 0x80, 24037939Skjc DW_LNE_hi_user = 0xff 24125603Skjc}; 24237939Skjc 24337939Skjcenum LineNumberEntryFormat { 24437939Skjc#define HANDLE_DW_LNCT(ID, NAME) DW_LNCT_##NAME = ID, 24525603Skjc#include "llvm/BinaryFormat/Dwarf.def" 24625603Skjc DW_LNCT_lo_user = 0x2000, 24737939Skjc DW_LNCT_hi_user = 0x3fff, 24837939Skjc}; 24937939Skjc 25037939Skjcenum MacinfoRecordType { 25137939Skjc // Macinfo Type Encodings 25237939Skjc DW_MACINFO_define = 0x01, 25337939Skjc DW_MACINFO_undef = 0x02, 25437939Skjc DW_MACINFO_start_file = 0x03, 25537939Skjc DW_MACINFO_end_file = 0x04, 25637939Skjc DW_MACINFO_vendor_ext = 0xff 25725603Skjc}; 25837939Skjc 25937939Skjc/// DWARF v5 macro information entry type encodings. 26025603Skjcenum MacroEntryType { 26137939Skjc#define HANDLE_DW_MACRO(ID, NAME) DW_MACRO_##NAME = ID, 26237939Skjc#include "llvm/BinaryFormat/Dwarf.def" 26325603Skjc DW_MACRO_lo_user = 0xe0, 26437939Skjc DW_MACRO_hi_user = 0xff 26537939Skjc}; 26637939Skjc 26737939Skjc/// DWARF v5 range list entry encoding values. 26837939Skjcenum RangeListEntries { 26937939Skjc#define HANDLE_DW_RLE(ID, NAME) DW_RLE_##NAME = ID, 27025603Skjc#include "llvm/BinaryFormat/Dwarf.def" 27137939Skjc}; 27225603Skjc 27337939Skjc/// Call frame instruction encodings. 274111888Sjlemonenum CallFrameInfo { 27537939Skjc#define HANDLE_DW_CFA(ID, NAME) DW_CFA_##NAME = ID, 27625603Skjc#include "llvm/BinaryFormat/Dwarf.def" 27737939Skjc DW_CFA_extended = 0x00, 27837939Skjc 279111888Sjlemon DW_CFA_lo_user = 0x1c, 28037939Skjc DW_CFA_hi_user = 0x3f 28137939Skjc}; 28237939Skjc 28337939Skjcenum Constants { 28437939Skjc // Children flag 28537939Skjc DW_CHILDREN_no = 0x00, 28625603Skjc DW_CHILDREN_yes = 0x01, 287111888Sjlemon 28825603Skjc DW_EH_PE_absptr = 0x00, 28925603Skjc DW_EH_PE_omit = 0xff, 29025603Skjc DW_EH_PE_uleb128 = 0x01, 291114201Sharti DW_EH_PE_udata2 = 0x02, 29225603Skjc DW_EH_PE_udata4 = 0x03, 29325603Skjc DW_EH_PE_udata8 = 0x04, 29425603Skjc DW_EH_PE_sleb128 = 0x09, 295111774Smdodd DW_EH_PE_sdata2 = 0x0A, 29625603Skjc DW_EH_PE_sdata4 = 0x0B, 297111774Smdodd DW_EH_PE_sdata8 = 0x0C, 298111774Smdodd DW_EH_PE_signed = 0x08, 299114739Sharti DW_EH_PE_pcrel = 0x10, 30025603Skjc DW_EH_PE_textrel = 0x20, 30125603Skjc DW_EH_PE_datarel = 0x30, 30225603Skjc DW_EH_PE_funcrel = 0x40, 30325603Skjc DW_EH_PE_aligned = 0x50, 304114201Sharti DW_EH_PE_indirect = 0x80 30525603Skjc}; 30625603Skjc 307106939Ssam/// Constants for location lists in DWARF v5. 308106939Ssamenum LocationListEntry : unsigned char { 309106939Ssam DW_LLE_end_of_list = 0x00, 31046695Skjc DW_LLE_base_addressx = 0x01, 31125603Skjc DW_LLE_startx_endx = 0x02, 31225603Skjc DW_LLE_startx_length = 0x03, 31372012Sphk DW_LLE_offset_pair = 0x04, 31437939Skjc DW_LLE_default_location = 0x05, 31571959Sphk DW_LLE_base_address = 0x06, 31671959Sphk DW_LLE_start_end = 0x07, 31725603Skjc DW_LLE_start_length = 0x08 31825603Skjc}; 31925603Skjc 32025603Skjc/// Constants for the DW_APPLE_PROPERTY_attributes attribute. 32125603Skjc/// Keep this list in sync with clang's DeclSpec.h ObjCPropertyAttributeKind! 32225603Skjcenum ApplePropertyAttributes { 32325603Skjc#define HANDLE_DW_APPLE_PROPERTY(ID, NAME) DW_APPLE_PROPERTY_##NAME = ID, 32425603Skjc#include "llvm/BinaryFormat/Dwarf.def" 32525603Skjc}; 32625603Skjc 32725603Skjc/// Constants for unit types in DWARF v5. 32825603Skjcenum UnitType : unsigned char { 32937939Skjc#define HANDLE_DW_UT(ID, NAME) DW_UT_##NAME = ID, 330114739Sharti#include "llvm/BinaryFormat/Dwarf.def" 331114739Sharti DW_UT_lo_user = 0x80, 33237939Skjc DW_UT_hi_user = 0xff 333114201Sharti}; 334114201Sharti 335114201Shartienum Index { 336114201Sharti#define HANDLE_DW_IDX(ID, NAME) DW_IDX_##NAME = ID, 337114201Sharti#include "llvm/BinaryFormat/Dwarf.def" 338114201Sharti DW_IDX_lo_user = 0x2000, 339114201Sharti DW_IDX_hi_user = 0x3fff 340114201Sharti}; 341114201Sharti 342114201Shartiinline bool isUnitType(uint8_t UnitType) { 343114201Sharti switch (UnitType) { 344114201Sharti case DW_UT_compile: 345114201Sharti case DW_UT_type: 346114201Sharti case DW_UT_partial: 347114201Sharti case DW_UT_skeleton: 348114201Sharti case DW_UT_split_compile: 349114201Sharti case DW_UT_split_type: 350114201Sharti return true; 351 default: 352 return false; 353 } 354} 355 356inline bool isUnitType(dwarf::Tag T) { 357 switch (T) { 358 case DW_TAG_compile_unit: 359 case DW_TAG_type_unit: 360 case DW_TAG_partial_unit: 361 case DW_TAG_skeleton_unit: 362 return true; 363 default: 364 return false; 365 } 366} 367 368// Constants for the DWARF v5 Accelerator Table Proposal 369enum AcceleratorTable { 370 // Data layout descriptors. 371 DW_ATOM_null = 0u, /// Marker as the end of a list of atoms. 372 DW_ATOM_die_offset = 1u, // DIE offset in the debug_info section. 373 DW_ATOM_cu_offset = 2u, // Offset of the compile unit header that contains the 374 // item in question. 375 DW_ATOM_die_tag = 3u, // A tag entry. 376 DW_ATOM_type_flags = 4u, // Set of flags for a type. 377 378 DW_ATOM_type_type_flags = 5u, // Dsymutil type extension. 379 DW_ATOM_qual_name_hash = 6u, // Dsymutil qualified hash extension. 380 381 // DW_ATOM_type_flags values. 382 383 // Always set for C++, only set for ObjC if this is the @implementation for a 384 // class. 385 DW_FLAG_type_implementation = 2u, 386 387 // Hash functions. 388 389 // Daniel J. Bernstein hash. 390 DW_hash_function_djb = 0u 391}; 392 393// Constants for the GNU pubnames/pubtypes extensions supporting gdb index. 394enum GDBIndexEntryKind { 395 GIEK_NONE, 396 GIEK_TYPE, 397 GIEK_VARIABLE, 398 GIEK_FUNCTION, 399 GIEK_OTHER, 400 GIEK_UNUSED5, 401 GIEK_UNUSED6, 402 GIEK_UNUSED7 403}; 404 405enum GDBIndexEntryLinkage { GIEL_EXTERNAL, GIEL_STATIC }; 406 407/// \defgroup DwarfConstantsDumping Dwarf constants dumping functions 408/// 409/// All these functions map their argument's value back to the 410/// corresponding enumerator name or return an empty StringRef if the value 411/// isn't known. 412/// 413/// @{ 414StringRef TagString(unsigned Tag); 415StringRef ChildrenString(unsigned Children); 416StringRef AttributeString(unsigned Attribute); 417StringRef FormEncodingString(unsigned Encoding); 418StringRef OperationEncodingString(unsigned Encoding); 419StringRef AttributeEncodingString(unsigned Encoding); 420StringRef DecimalSignString(unsigned Sign); 421StringRef EndianityString(unsigned Endian); 422StringRef AccessibilityString(unsigned Access); 423StringRef VisibilityString(unsigned Visibility); 424StringRef VirtualityString(unsigned Virtuality); 425StringRef LanguageString(unsigned Language); 426StringRef CaseString(unsigned Case); 427StringRef ConventionString(unsigned Convention); 428StringRef InlineCodeString(unsigned Code); 429StringRef ArrayOrderString(unsigned Order); 430StringRef LNStandardString(unsigned Standard); 431StringRef LNExtendedString(unsigned Encoding); 432StringRef MacinfoString(unsigned Encoding); 433StringRef RangeListEncodingString(unsigned Encoding); 434StringRef CallFrameString(unsigned Encoding); 435StringRef ApplePropertyString(unsigned); 436StringRef UnitTypeString(unsigned); 437StringRef AtomTypeString(unsigned Atom); 438StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind); 439StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage); 440StringRef IndexString(unsigned Idx); 441/// @} 442 443/// \defgroup DwarfConstantsParsing Dwarf constants parsing functions 444/// 445/// These functions map their strings back to the corresponding enumeration 446/// value or return 0 if there is none, except for these exceptions: 447/// 448/// \li \a getTag() returns \a DW_TAG_invalid on invalid input. 449/// \li \a getVirtuality() returns \a DW_VIRTUALITY_invalid on invalid input. 450/// \li \a getMacinfo() returns \a DW_MACINFO_invalid on invalid input. 451/// 452/// @{ 453unsigned getTag(StringRef TagString); 454unsigned getOperationEncoding(StringRef OperationEncodingString); 455unsigned getVirtuality(StringRef VirtualityString); 456unsigned getLanguage(StringRef LanguageString); 457unsigned getCallingConvention(StringRef LanguageString); 458unsigned getAttributeEncoding(StringRef EncodingString); 459unsigned getMacinfo(StringRef MacinfoString); 460/// @} 461 462/// \defgroup DwarfConstantsVersioning Dwarf version for constants 463/// 464/// For constants defined by DWARF, returns the DWARF version when the constant 465/// was first defined. For vendor extensions, if there is a version-related 466/// policy for when to emit it, returns a version number for that policy. 467/// Otherwise returns 0. 468/// 469/// @{ 470unsigned TagVersion(Tag T); 471unsigned AttributeVersion(Attribute A); 472unsigned FormVersion(Form F); 473unsigned OperationVersion(LocationAtom O); 474unsigned AttributeEncodingVersion(TypeKind E); 475unsigned LanguageVersion(SourceLanguage L); 476/// @} 477 478/// \defgroup DwarfConstantsVendor Dwarf "vendor" for constants 479/// 480/// These functions return an identifier describing "who" defined the constant, 481/// either the DWARF standard itself or the vendor who defined the extension. 482/// 483/// @{ 484unsigned TagVendor(Tag T); 485unsigned AttributeVendor(Attribute A); 486unsigned FormVendor(Form F); 487unsigned OperationVendor(LocationAtom O); 488unsigned AttributeEncodingVendor(TypeKind E); 489unsigned LanguageVendor(SourceLanguage L); 490/// @} 491 492/// A helper struct providing information about the byte size of DW_FORM 493/// values that vary in size depending on the DWARF version, address byte 494/// size, or DWARF32/DWARF64. 495struct FormParams { 496 uint16_t Version; 497 uint8_t AddrSize; 498 DwarfFormat Format; 499 500 /// The definition of the size of form DW_FORM_ref_addr depends on the 501 /// version. In DWARF v2 it's the size of an address; after that, it's the 502 /// size of a reference. 503 uint8_t getRefAddrByteSize() const { 504 if (Version == 2) 505 return AddrSize; 506 return getDwarfOffsetByteSize(); 507 } 508 509 /// The size of a reference is determined by the DWARF 32/64-bit format. 510 uint8_t getDwarfOffsetByteSize() const { 511 switch (Format) { 512 case DwarfFormat::DWARF32: 513 return 4; 514 case DwarfFormat::DWARF64: 515 return 8; 516 } 517 llvm_unreachable("Invalid Format value"); 518 } 519 520 explicit operator bool() const { return Version && AddrSize; } 521}; 522 523/// Get the fixed byte size for a given form. 524/// 525/// If the form has a fixed byte size, then an Optional with a value will be 526/// returned. If the form is always encoded using a variable length storage 527/// format (ULEB or SLEB numbers or blocks) then None will be returned. 528/// 529/// \param Form DWARF form to get the fixed byte size for. 530/// \param Params DWARF parameters to help interpret forms. 531/// \returns Optional<uint8_t> value with the fixed byte size or None if 532/// \p Form doesn't have a fixed byte size. 533Optional<uint8_t> getFixedFormByteSize(dwarf::Form Form, FormParams Params); 534 535/// Tells whether the specified form is defined in the specified version, 536/// or is an extension if extensions are allowed. 537bool isValidFormForVersion(Form F, unsigned Version, bool ExtensionsOk = true); 538 539/// Returns the symbolic string representing Val when used as a value 540/// for attribute Attr. 541StringRef AttributeValueString(uint16_t Attr, unsigned Val); 542 543/// Returns the symbolic string representing Val when used as a value 544/// for atom Atom. 545StringRef AtomValueString(uint16_t Atom, unsigned Val); 546 547/// Describes an entry of the various gnu_pub* debug sections. 548/// 549/// The gnu_pub* kind looks like: 550/// 551/// 0-3 reserved 552/// 4-6 symbol kind 553/// 7 0 == global, 1 == static 554/// 555/// A gdb_index descriptor includes the above kind, shifted 24 bits up with the 556/// offset of the cu within the debug_info section stored in those 24 bits. 557struct PubIndexEntryDescriptor { 558 GDBIndexEntryKind Kind; 559 GDBIndexEntryLinkage Linkage; 560 PubIndexEntryDescriptor(GDBIndexEntryKind Kind, GDBIndexEntryLinkage Linkage) 561 : Kind(Kind), Linkage(Linkage) {} 562 /* implicit */ PubIndexEntryDescriptor(GDBIndexEntryKind Kind) 563 : Kind(Kind), Linkage(GIEL_EXTERNAL) {} 564 explicit PubIndexEntryDescriptor(uint8_t Value) 565 : Kind( 566 static_cast<GDBIndexEntryKind>((Value & KIND_MASK) >> KIND_OFFSET)), 567 Linkage(static_cast<GDBIndexEntryLinkage>((Value & LINKAGE_MASK) >> 568 LINKAGE_OFFSET)) {} 569 uint8_t toBits() const { 570 return Kind << KIND_OFFSET | Linkage << LINKAGE_OFFSET; 571 } 572 573private: 574 enum { 575 KIND_OFFSET = 4, 576 KIND_MASK = 7 << KIND_OFFSET, 577 LINKAGE_OFFSET = 7, 578 LINKAGE_MASK = 1 << LINKAGE_OFFSET 579 }; 580}; 581 582template <typename Enum> struct EnumTraits : public std::false_type {}; 583 584template <> struct EnumTraits<Attribute> : public std::true_type { 585 static constexpr char Type[3] = "AT"; 586 static constexpr StringRef (*StringFn)(unsigned) = &AttributeString; 587}; 588 589template <> struct EnumTraits<Form> : public std::true_type { 590 static constexpr char Type[5] = "FORM"; 591 static constexpr StringRef (*StringFn)(unsigned) = &FormEncodingString; 592}; 593 594template <> struct EnumTraits<Index> : public std::true_type { 595 static constexpr char Type[4] = "IDX"; 596 static constexpr StringRef (*StringFn)(unsigned) = &IndexString; 597}; 598 599template <> struct EnumTraits<Tag> : public std::true_type { 600 static constexpr char Type[4] = "TAG"; 601 static constexpr StringRef (*StringFn)(unsigned) = &TagString; 602}; 603} // End of namespace dwarf 604 605/// Dwarf constants format_provider 606/// 607/// Specialization of the format_provider template for dwarf enums. Unlike the 608/// dumping functions above, these format unknown enumerator values as 609/// DW_TYPE_unknown_1234 (e.g. DW_TAG_unknown_ffff). 610template <typename Enum> 611struct format_provider< 612 Enum, typename std::enable_if<dwarf::EnumTraits<Enum>::value>::type> { 613 static void format(const Enum &E, raw_ostream &OS, StringRef Style) { 614 StringRef Str = dwarf::EnumTraits<Enum>::StringFn(E); 615 if (Str.empty()) { 616 OS << "DW_" << dwarf::EnumTraits<Enum>::Type << "_unknown_" 617 << llvm::format("%x", E); 618 } else 619 OS << Str; 620 } 621}; 622} // End of namespace llvm 623 624#endif 625