#ifndef LLVM_BINARYFORMAT_DWARF_H
#define LLVM_BINARYFORMAT_DWARF_H
#include "llvm/ADT/Optional.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/FormatVariadicDetails.h"
namespace llvm {
class StringRef;
namespace dwarf {
enum LLVMConstants : uint32_t {
DW_TAG_invalid = ~0U,
DW_VIRTUALITY_invalid = ~0U,
DW_MACINFO_invalid = ~0U,
DWARF_VERSION = 4,
DW_PUBTYPES_VERSION = 2,
DW_PUBNAMES_VERSION = 2,
DW_ARANGES_VERSION = 2,
DWARF_VENDOR_DWARF = 0,
DWARF_VENDOR_APPLE = 1,
DWARF_VENDOR_BORLAND = 2,
DWARF_VENDOR_GNU = 3,
DWARF_VENDOR_GOOGLE = 4,
DWARF_VENDOR_LLVM = 5,
DWARF_VENDOR_MIPS = 6
};
enum DwarfFormat : uint8_t { DWARF32, DWARF64 };
const uint32_t DW_CIE_ID = UINT32_MAX;
const uint64_t DW64_CIE_ID = UINT64_MAX;
const uint32_t DW_INVALID_OFFSET = UINT32_MAX;
enum Tag : uint16_t {
#define HANDLE_DW_TAG(ID, NAME, VERSION, VENDOR) DW_TAG_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_TAG_lo_user = 0x4080,
DW_TAG_hi_user = 0xffff,
DW_TAG_user_base = 0x1000
};
inline bool isType(Tag T) {
switch (T) {
case DW_TAG_array_type:
case DW_TAG_class_type:
case DW_TAG_interface_type:
case DW_TAG_enumeration_type:
case DW_TAG_pointer_type:
case DW_TAG_reference_type:
case DW_TAG_rvalue_reference_type:
case DW_TAG_string_type:
case DW_TAG_structure_type:
case DW_TAG_subroutine_type:
case DW_TAG_union_type:
case DW_TAG_ptr_to_member_type:
case DW_TAG_set_type:
case DW_TAG_subrange_type:
case DW_TAG_base_type:
case DW_TAG_const_type:
case DW_TAG_file_type:
case DW_TAG_packed_type:
case DW_TAG_volatile_type:
case DW_TAG_typedef:
return true;
default:
return false;
}
}
enum Attribute : uint16_t {
#define HANDLE_DW_AT(ID, NAME, VERSION, VENDOR) DW_AT_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_AT_lo_user = 0x2000,
DW_AT_hi_user = 0x3fff,
};
enum Form : uint16_t {
#define HANDLE_DW_FORM(ID, NAME, VERSION, VENDOR) DW_FORM_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_FORM_lo_user = 0x1f00,
};
enum LocationAtom {
#define HANDLE_DW_OP(ID, NAME, VERSION, VENDOR) DW_OP_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_OP_lo_user = 0xe0,
DW_OP_hi_user = 0xff,
DW_OP_LLVM_fragment = 0x1000
};
enum TypeKind : uint8_t {
#define HANDLE_DW_ATE(ID, NAME, VERSION, VENDOR) DW_ATE_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_ATE_lo_user = 0x80,
DW_ATE_hi_user = 0xff
};
enum DecimalSignEncoding {
DW_DS_unsigned = 0x01,
DW_DS_leading_overpunch = 0x02,
DW_DS_trailing_overpunch = 0x03,
DW_DS_leading_separate = 0x04,
DW_DS_trailing_separate = 0x05
};
enum EndianityEncoding {
DW_END_default = 0x00,
DW_END_big = 0x01,
DW_END_little = 0x02,
DW_END_lo_user = 0x40,
DW_END_hi_user = 0xff
};
enum AccessAttribute {
DW_ACCESS_public = 0x01,
DW_ACCESS_protected = 0x02,
DW_ACCESS_private = 0x03
};
enum VisibilityAttribute {
DW_VIS_local = 0x01,
DW_VIS_exported = 0x02,
DW_VIS_qualified = 0x03
};
enum VirtualityAttribute {
#define HANDLE_DW_VIRTUALITY(ID, NAME) DW_VIRTUALITY_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_VIRTUALITY_max = 0x02
};
enum DefaultedMemberAttribute {
#define HANDLE_DW_DEFAULTED(ID, NAME) DW_DEFAULTED_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_DEFAULTED_max = 0x02
};
enum SourceLanguage {
#define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) DW_LANG_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_LANG_lo_user = 0x8000,
DW_LANG_hi_user = 0xffff
};
enum CaseSensitivity {
DW_ID_case_sensitive = 0x00,
DW_ID_up_case = 0x01,
DW_ID_down_case = 0x02,
DW_ID_case_insensitive = 0x03
};
enum CallingConvention {
#define HANDLE_DW_CC(ID, NAME) DW_CC_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_CC_lo_user = 0x40,
DW_CC_hi_user = 0xff
};
enum InlineAttribute {
DW_INL_not_inlined = 0x00,
DW_INL_inlined = 0x01,
DW_INL_declared_not_inlined = 0x02,
DW_INL_declared_inlined = 0x03
};
enum ArrayDimensionOrdering {
DW_ORD_row_major = 0x00,
DW_ORD_col_major = 0x01
};
enum DiscriminantList {
DW_DSC_label = 0x00,
DW_DSC_range = 0x01
};
enum LineNumberOps : uint8_t {
#define HANDLE_DW_LNS(ID, NAME) DW_LNS_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
};
enum LineNumberExtendedOps {
#define HANDLE_DW_LNE(ID, NAME) DW_LNE_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_LNE_lo_user = 0x80,
DW_LNE_hi_user = 0xff
};
enum LineNumberEntryFormat {
#define HANDLE_DW_LNCT(ID, NAME) DW_LNCT_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_LNCT_lo_user = 0x2000,
DW_LNCT_hi_user = 0x3fff,
};
enum MacinfoRecordType {
DW_MACINFO_define = 0x01,
DW_MACINFO_undef = 0x02,
DW_MACINFO_start_file = 0x03,
DW_MACINFO_end_file = 0x04,
DW_MACINFO_vendor_ext = 0xff
};
enum MacroEntryType {
#define HANDLE_DW_MACRO(ID, NAME) DW_MACRO_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_MACRO_lo_user = 0xe0,
DW_MACRO_hi_user = 0xff
};
enum RangeListEntries {
#define HANDLE_DW_RLE(ID, NAME) DW_RLE_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
};
enum CallFrameInfo {
#define HANDLE_DW_CFA(ID, NAME) DW_CFA_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_CFA_extended = 0x00,
DW_CFA_lo_user = 0x1c,
DW_CFA_hi_user = 0x3f
};
enum Constants {
DW_CHILDREN_no = 0x00,
DW_CHILDREN_yes = 0x01,
DW_EH_PE_absptr = 0x00,
DW_EH_PE_omit = 0xff,
DW_EH_PE_uleb128 = 0x01,
DW_EH_PE_udata2 = 0x02,
DW_EH_PE_udata4 = 0x03,
DW_EH_PE_udata8 = 0x04,
DW_EH_PE_sleb128 = 0x09,
DW_EH_PE_sdata2 = 0x0A,
DW_EH_PE_sdata4 = 0x0B,
DW_EH_PE_sdata8 = 0x0C,
DW_EH_PE_signed = 0x08,
DW_EH_PE_pcrel = 0x10,
DW_EH_PE_textrel = 0x20,
DW_EH_PE_datarel = 0x30,
DW_EH_PE_funcrel = 0x40,
DW_EH_PE_aligned = 0x50,
DW_EH_PE_indirect = 0x80
};
enum LocationListEntry : unsigned char {
DW_LLE_end_of_list = 0x00,
DW_LLE_base_addressx = 0x01,
DW_LLE_startx_endx = 0x02,
DW_LLE_startx_length = 0x03,
DW_LLE_offset_pair = 0x04,
DW_LLE_default_location = 0x05,
DW_LLE_base_address = 0x06,
DW_LLE_start_end = 0x07,
DW_LLE_start_length = 0x08
};
enum ApplePropertyAttributes {
#define HANDLE_DW_APPLE_PROPERTY(ID, NAME) DW_APPLE_PROPERTY_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
};
enum UnitType : unsigned char {
#define HANDLE_DW_UT(ID, NAME) DW_UT_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_UT_lo_user = 0x80,
DW_UT_hi_user = 0xff
};
enum Index {
#define HANDLE_DW_IDX(ID, NAME) DW_IDX_##NAME = ID,
#include "llvm/BinaryFormat/Dwarf.def"
DW_IDX_lo_user = 0x2000,
DW_IDX_hi_user = 0x3fff
};
inline bool isUnitType(uint8_t UnitType) {
switch (UnitType) {
case DW_UT_compile:
case DW_UT_type:
case DW_UT_partial:
case DW_UT_skeleton:
case DW_UT_split_compile:
case DW_UT_split_type:
return true;
default:
return false;
}
}
inline bool isUnitType(dwarf::Tag T) {
switch (T) {
case DW_TAG_compile_unit:
case DW_TAG_type_unit:
case DW_TAG_partial_unit:
case DW_TAG_skeleton_unit:
return true;
default:
return false;
}
}
enum AcceleratorTable {
DW_ATOM_null = 0u,
DW_ATOM_die_offset = 1u,
DW_ATOM_cu_offset = 2u,
DW_ATOM_die_tag = 3u,
DW_ATOM_type_flags = 4u,
DW_ATOM_type_type_flags = 5u,
DW_ATOM_qual_name_hash = 6u,
DW_FLAG_type_implementation = 2u,
DW_hash_function_djb = 0u
};
enum GDBIndexEntryKind {
GIEK_NONE,
GIEK_TYPE,
GIEK_VARIABLE,
GIEK_FUNCTION,
GIEK_OTHER,
GIEK_UNUSED5,
GIEK_UNUSED6,
GIEK_UNUSED7
};
enum GDBIndexEntryLinkage { GIEL_EXTERNAL, GIEL_STATIC };
StringRef TagString(unsigned Tag);
StringRef ChildrenString(unsigned Children);
StringRef AttributeString(unsigned Attribute);
StringRef FormEncodingString(unsigned Encoding);
StringRef OperationEncodingString(unsigned Encoding);
StringRef AttributeEncodingString(unsigned Encoding);
StringRef DecimalSignString(unsigned Sign);
StringRef EndianityString(unsigned Endian);
StringRef AccessibilityString(unsigned Access);
StringRef VisibilityString(unsigned Visibility);
StringRef VirtualityString(unsigned Virtuality);
StringRef LanguageString(unsigned Language);
StringRef CaseString(unsigned Case);
StringRef ConventionString(unsigned Convention);
StringRef InlineCodeString(unsigned Code);
StringRef ArrayOrderString(unsigned Order);
StringRef LNStandardString(unsigned Standard);
StringRef LNExtendedString(unsigned Encoding);
StringRef MacinfoString(unsigned Encoding);
StringRef RangeListEncodingString(unsigned Encoding);
StringRef CallFrameString(unsigned Encoding);
StringRef ApplePropertyString(unsigned);
StringRef UnitTypeString(unsigned);
StringRef AtomTypeString(unsigned Atom);
StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind);
StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage);
StringRef IndexString(unsigned Idx);
unsigned getTag(StringRef TagString);
unsigned getOperationEncoding(StringRef OperationEncodingString);
unsigned getVirtuality(StringRef VirtualityString);
unsigned getLanguage(StringRef LanguageString);
unsigned getCallingConvention(StringRef LanguageString);
unsigned getAttributeEncoding(StringRef EncodingString);
unsigned getMacinfo(StringRef MacinfoString);
unsigned TagVersion(Tag T);
unsigned AttributeVersion(Attribute A);
unsigned FormVersion(Form F);
unsigned OperationVersion(LocationAtom O);
unsigned AttributeEncodingVersion(TypeKind E);
unsigned LanguageVersion(SourceLanguage L);
unsigned TagVendor(Tag T);
unsigned AttributeVendor(Attribute A);
unsigned FormVendor(Form F);
unsigned OperationVendor(LocationAtom O);
unsigned AttributeEncodingVendor(TypeKind E);
unsigned LanguageVendor(SourceLanguage L);
struct FormParams {
uint16_t Version;
uint8_t AddrSize;
DwarfFormat Format;
uint8_t getRefAddrByteSize() const {
if (Version == 2)
return AddrSize;
return getDwarfOffsetByteSize();
}
uint8_t getDwarfOffsetByteSize() const {
switch (Format) {
case DwarfFormat::DWARF32:
return 4;
case DwarfFormat::DWARF64:
return 8;
}
llvm_unreachable("Invalid Format value");
}
explicit operator bool() const { return Version && AddrSize; }
};
Optional<uint8_t> getFixedFormByteSize(dwarf::Form Form, FormParams Params);
bool isValidFormForVersion(Form F, unsigned Version, bool ExtensionsOk = true);
StringRef AttributeValueString(uint16_t Attr, unsigned Val);
StringRef AtomValueString(uint16_t Atom, unsigned Val);
struct PubIndexEntryDescriptor {
GDBIndexEntryKind Kind;
GDBIndexEntryLinkage Linkage;
PubIndexEntryDescriptor(GDBIndexEntryKind Kind, GDBIndexEntryLinkage Linkage)
: Kind(Kind), Linkage(Linkage) {}
PubIndexEntryDescriptor(GDBIndexEntryKind Kind)
: Kind(Kind), Linkage(GIEL_EXTERNAL) {}
explicit PubIndexEntryDescriptor(uint8_t Value)
: Kind(
static_cast<GDBIndexEntryKind>((Value & KIND_MASK) >> KIND_OFFSET)),
Linkage(static_cast<GDBIndexEntryLinkage>((Value & LINKAGE_MASK) >>
LINKAGE_OFFSET)) {}
uint8_t toBits() const {
return Kind << KIND_OFFSET | Linkage << LINKAGE_OFFSET;
}
private:
enum {
KIND_OFFSET = 4,
KIND_MASK = 7 << KIND_OFFSET,
LINKAGE_OFFSET = 7,
LINKAGE_MASK = 1 << LINKAGE_OFFSET
};
};
template <typename Enum> struct EnumTraits : public std::false_type {};
template <> struct EnumTraits<Attribute> : public std::true_type {
static constexpr char Type[3] = "AT";
static constexpr StringRef (*StringFn)(unsigned) = &AttributeString;
};
template <> struct EnumTraits<Form> : public std::true_type {
static constexpr char Type[5] = "FORM";
static constexpr StringRef (*StringFn)(unsigned) = &FormEncodingString;
};
template <> struct EnumTraits<Index> : public std::true_type {
static constexpr char Type[4] = "IDX";
static constexpr StringRef (*StringFn)(unsigned) = &IndexString;
};
template <> struct EnumTraits<Tag> : public std::true_type {
static constexpr char Type[4] = "TAG";
static constexpr StringRef (*StringFn)(unsigned) = &TagString;
};
}
template <typename Enum>
struct format_provider<
Enum, typename std::enable_if<dwarf::EnumTraits<Enum>::value>::type> {
static void format(const Enum &E, raw_ostream &OS, StringRef Style) {
StringRef Str = dwarf::EnumTraits<Enum>::StringFn(E);
if (Str.empty()) {
OS << "DW_" << dwarf::EnumTraits<Enum>::Type << "_unknown_"
<< llvm::format("%x", E);
} else
OS << Str;
}
};
}
#endif