1 //===-- HashedNameToDIE.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 SymbolFileDWARF_HashedNameToDIE_h_
10 #define SymbolFileDWARF_HashedNameToDIE_h_
11 
12 #include <vector>
13 
14 #include "lldb/Core/MappedHash.h"
15 #include "lldb/Core/dwarf.h"
16 #include "lldb/Utility/RegularExpression.h"
17 #include "lldb/lldb-defines.h"
18 
19 #include "DWARFDefines.h"
20 #include "DWARFFormValue.h"
21 #include "NameToDIE.h"
22 
23 class DWARFMappedHash {
24 public:
25   enum AtomType : uint16_t {
26     eAtomTypeNULL = 0u,
27     eAtomTypeDIEOffset = 1u, // DIE offset, check form for encoding
28     eAtomTypeCUOffset = 2u,  // DIE offset of the compiler unit header that
29                              // contains the item in question
30     eAtomTypeTag = 3u, // DW_TAG_xxx value, should be encoded as DW_FORM_data1
31                        // (if no tags exceed 255) or DW_FORM_data2
32     eAtomTypeNameFlags = 4u,   // Flags from enum NameFlags
33     eAtomTypeTypeFlags = 5u,   // Flags from enum TypeFlags,
34     eAtomTypeQualNameHash = 6u // A 32 bit hash of the full qualified name
35                                // (since all hash entries are basename only)
36     // For example a type like "std::vector<int>::iterator" would have a name of
37     // "iterator"
38     // and a 32 bit hash for "std::vector<int>::iterator" to allow us to not
39     // have to pull
40     // in debug info for a type when we know the fully qualified name.
41   };
42 
43   // Bit definitions for the eAtomTypeTypeFlags flags
44   enum TypeFlags {
45     // Always set for C++, only set for ObjC if this is the
46     // @implementation for class
47     eTypeFlagClassIsImplementation = (1u << 1)
48   };
49 
50   struct DIEInfo {
51     dw_offset_t die_offset = DW_INVALID_OFFSET;
52     dw_tag_t tag = 0;
53 
54     /// Any flags for this DIEInfo
55     uint32_t type_flags = 0;
56 
57     /// A 32 bit hash of the fully qualified name
58     uint32_t qualified_name_hash = 0;
59 
60     DIEInfo() = default;
61     DIEInfo(dw_offset_t o, dw_tag_t t, uint32_t f, uint32_t h);
62 
63     explicit operator DIERef() const {
64       return DIERef(llvm::None, DIERef::Section::DebugInfo, die_offset);
65     }
66   };
67 
68   struct Atom {
69     AtomType type;
70     dw_form_t form;
71   };
72 
73   typedef std::vector<DIEInfo> DIEInfoArray;
74   typedef std::vector<Atom> AtomArray;
75 
76   class Prologue {
77   public:
78     Prologue(dw_offset_t _die_base_offset = 0);
79 
80     void ClearAtoms();
81 
82     bool ContainsAtom(AtomType atom_type) const;
83 
84     void Clear();
85 
86     void AppendAtom(AtomType type, dw_form_t form);
87 
88     lldb::offset_t Read(const lldb_private::DataExtractor &data,
89                         lldb::offset_t offset);
90 
91     size_t GetByteSize() const;
92 
93     size_t GetMinimumHashDataByteSize() const;
94 
95     bool HashDataHasFixedByteSize() const;
96 
97     // DIE offset base so die offsets in hash_data can be CU relative
98     dw_offset_t die_base_offset;
99     AtomArray atoms;
100     uint32_t atom_mask;
101     size_t min_hash_data_byte_size;
102     bool hash_data_has_fixed_byte_size;
103   };
104 
105   class Header : public MappedHash::Header<Prologue> {
106   public:
107     size_t GetByteSize(const HeaderData &header_data) override;
108 
109     lldb::offset_t Read(lldb_private::DataExtractor &data,
110                         lldb::offset_t offset) override;
111 
112     bool Read(const lldb_private::DWARFDataExtractor &data,
113               lldb::offset_t *offset_ptr, DIEInfo &hash_data) const;
114   };
115 
116   // A class for reading and using a saved hash table from a block of data
117   // in memory
118   class MemoryTable
119       : public MappedHash::MemoryTable<uint32_t, DWARFMappedHash::Header,
120                                        DIEInfoArray> {
121   public:
122     MemoryTable(lldb_private::DWARFDataExtractor &table_data,
123                 const lldb_private::DWARFDataExtractor &string_table,
124                 const char *name);
125 
126     const char *GetStringForKeyType(KeyType key) const override;
127 
128     bool ReadHashData(uint32_t hash_data_offset,
129                       HashData &hash_data) const override;
130 
131     size_t
132     AppendAllDIEsThatMatchingRegex(const lldb_private::RegularExpression &regex,
133                                    DIEInfoArray &die_info_array) const;
134 
135     size_t AppendAllDIEsInRange(const uint32_t die_offset_start,
136                                 const uint32_t die_offset_end,
137                                 DIEInfoArray &die_info_array) const;
138 
139     size_t FindByName(llvm::StringRef name, DIEArray &die_offsets);
140 
141     size_t FindByNameAndTag(llvm::StringRef name, const dw_tag_t tag,
142                             DIEArray &die_offsets);
143 
144     size_t FindByNameAndTagAndQualifiedNameHash(
145         llvm::StringRef name, const dw_tag_t tag,
146         const uint32_t qualified_name_hash, DIEArray &die_offsets);
147 
148     size_t FindCompleteObjCClassByName(llvm::StringRef name,
149                                        DIEArray &die_offsets,
150                                        bool must_be_implementation);
151 
152   protected:
153     Result AppendHashDataForRegularExpression(
154         const lldb_private::RegularExpression &regex,
155         lldb::offset_t *hash_data_offset_ptr, Pair &pair) const;
156 
157     size_t FindByName(llvm::StringRef name, DIEInfoArray &die_info_array);
158 
159     Result GetHashDataForName(llvm::StringRef name,
160                               lldb::offset_t *hash_data_offset_ptr,
161                               Pair &pair) const override;
162 
163     lldb_private::DWARFDataExtractor m_data;
164     lldb_private::DWARFDataExtractor m_string_table;
165     std::string m_name;
166   };
167 
168   static void ExtractDIEArray(const DIEInfoArray &die_info_array,
169                               DIEArray &die_offsets);
170 
171 protected:
172   static void ExtractDIEArray(const DIEInfoArray &die_info_array,
173                               const dw_tag_t tag, DIEArray &die_offsets);
174 
175   static void ExtractDIEArray(const DIEInfoArray &die_info_array,
176                               const dw_tag_t tag,
177                               const uint32_t qualified_name_hash,
178                               DIEArray &die_offsets);
179 
180   static void
181   ExtractClassOrStructDIEArray(const DIEInfoArray &die_info_array,
182                                bool return_implementation_only_if_available,
183                                DIEArray &die_offsets);
184 
185   static void ExtractTypesFromDIEArray(const DIEInfoArray &die_info_array,
186                                        uint32_t type_flag_mask,
187                                        uint32_t type_flag_value,
188                                        DIEArray &die_offsets);
189 
190   static const char *GetAtomTypeName(uint16_t atom);
191 };
192 
193 #endif // SymbolFileDWARF_HashedNameToDIE_h_
194