1 //===-- DIERef.h ------------------------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DIEREF_H 10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DIEREF_H 11 12 #include "lldb/Core/dwarf.h" 13 #include "lldb/Utility/LLDBAssert.h" 14 #include <cassert> 15 #include <optional> 16 17 /// Identifies a DWARF debug info entry within a given Module. It contains three 18 /// "coordinates": 19 /// - file_index: identifies the separate stand alone debug info file 20 /// that is referred to by the main debug info file. This will be the 21 /// index of a DWO file for fission, or the .o file on mac when not 22 /// using a dSYM file. If this field is not set, then this references 23 /// a DIE inside the original object file. 24 /// - section: identifies the section of the debug info entry in the given file: 25 /// debug_info or debug_types. 26 /// - die_offset: The offset of the debug info entry as an absolute offset from 27 /// the beginning of the section specified in the section field. 28 class DIERef { 29 public: 30 enum Section : uint8_t { DebugInfo, DebugTypes }; 31 DIERef(std::optional<uint32_t> file_index, Section section, 32 dw_offset_t die_offset) 33 : m_die_offset(die_offset), m_file_index(file_index.value_or(0)), 34 m_file_index_valid(file_index ? true : false), m_section(section) { 35 assert(this->file_index() == file_index && "File Index is out of range?"); 36 } 37 38 explicit DIERef(lldb::user_id_t uid) { 39 m_die_offset = uid & k_die_offset_mask; 40 m_file_index_valid = (uid & k_file_index_valid_bit) != 0; 41 m_file_index = m_file_index_valid 42 ? (uid >> k_die_offset_bit_size) & k_file_index_mask 43 : 0; 44 m_section = 45 (uid & k_section_bit) != 0 ? Section::DebugTypes : Section::DebugInfo; 46 } 47 48 lldb::user_id_t get_id() const { 49 if (m_die_offset == k_die_offset_mask) 50 return LLDB_INVALID_UID; 51 52 return lldb::user_id_t(file_index().value_or(0)) << k_die_offset_bit_size | 53 die_offset() | (m_file_index_valid ? k_file_index_valid_bit : 0) | 54 (section() == Section::DebugTypes ? k_section_bit : 0); 55 } 56 57 std::optional<uint32_t> file_index() const { 58 if (m_file_index_valid) 59 return m_file_index; 60 return std::nullopt; 61 } 62 63 Section section() const { return static_cast<Section>(m_section); } 64 65 dw_offset_t die_offset() const { return m_die_offset; } 66 67 bool operator<(DIERef other) const { 68 if (m_file_index_valid != other.m_file_index_valid) 69 return m_file_index_valid < other.m_file_index_valid; 70 if (m_file_index_valid && (m_file_index != other.m_file_index)) 71 return m_file_index < other.m_file_index; 72 if (m_section != other.m_section) 73 return m_section < other.m_section; 74 return m_die_offset < other.m_die_offset; 75 } 76 77 bool operator==(const DIERef &rhs) const { 78 return file_index() == rhs.file_index() && m_section == rhs.m_section && 79 m_die_offset == rhs.m_die_offset; 80 } 81 82 bool operator!=(const DIERef &rhs) const { return !(*this == rhs); } 83 84 /// Decode a serialized version of this object from data. 85 /// 86 /// \param data 87 /// The decoder object that references the serialized data. 88 /// 89 /// \param offset_ptr 90 /// A pointer that contains the offset from which the data will be decoded 91 /// from that gets updated as data gets decoded. 92 /// 93 /// \return 94 /// Returns a valid DIERef if decoding succeeded, std::nullopt if there was 95 /// unsufficient or invalid values that were decoded. 96 static std::optional<DIERef> Decode(const lldb_private::DataExtractor &data, 97 lldb::offset_t *offset_ptr); 98 99 /// Encode this object into a data encoder object. 100 /// 101 /// This allows this object to be serialized to disk. 102 /// 103 /// \param encoder 104 /// A data encoder object that serialized bytes will be encoded into. 105 /// 106 void Encode(lldb_private::DataEncoder &encoder) const; 107 108 static constexpr uint64_t k_die_offset_bit_size = DW_DIE_OFFSET_MAX_BITSIZE; 109 static constexpr uint64_t k_file_index_bit_size = 110 64 - DW_DIE_OFFSET_MAX_BITSIZE - /* size of control bits */ 2; 111 112 static constexpr uint64_t k_file_index_valid_bit = 113 (1ull << (k_file_index_bit_size + k_die_offset_bit_size)); 114 static constexpr uint64_t k_section_bit = 115 (1ull << (k_file_index_bit_size + k_die_offset_bit_size + 1)); 116 static constexpr uint64_t 117 k_file_index_mask = (~0ull) >> (64 - k_file_index_bit_size); // 0x3fffff; 118 static constexpr uint64_t k_die_offset_mask = (~0ull) >> 119 (64 - k_die_offset_bit_size); 120 121 private: 122 // Allow 2TB of .debug_info/.debug_types offset 123 dw_offset_t m_die_offset : k_die_offset_bit_size; 124 // Used for DWO index or for .o file index on mac 125 dw_offset_t m_file_index : k_file_index_bit_size; 126 // Set to 1 if m_file_index is a DWO number 127 dw_offset_t m_file_index_valid : 1; 128 // Set to 0 for .debug_info 1 for .debug_types, 129 dw_offset_t m_section : 1; 130 }; 131 static_assert(sizeof(DIERef) == 8); 132 133 typedef std::vector<DIERef> DIEArray; 134 135 namespace llvm { 136 template<> struct format_provider<DIERef> { 137 static void format(const DIERef &ref, raw_ostream &OS, StringRef Style); 138 }; 139 } // namespace llvm 140 141 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DIEREF_H 142