1 //===-- DWARFDebugInfoEntry.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_DWARFDebugInfoEntry_h_
10 #define SymbolFileDWARF_DWARFDebugInfoEntry_h_
11 
12 #include "SymbolFileDWARF.h"
13 #include "llvm/ADT/SmallVector.h"
14 
15 #include "DWARFAbbreviationDeclaration.h"
16 #include "DWARFDebugAbbrev.h"
17 #include "DWARFDebugRanges.h"
18 #include <map>
19 #include <set>
20 #include <vector>
21 
22 typedef std::map<const DWARFDebugInfoEntry *, dw_addr_t> DIEToAddressMap;
23 typedef DIEToAddressMap::iterator DIEToAddressMapIter;
24 typedef DIEToAddressMap::const_iterator DIEToAddressMapConstIter;
25 
26 typedef std::map<dw_addr_t, const DWARFDebugInfoEntry *> AddressToDIEMap;
27 typedef AddressToDIEMap::iterator AddressToDIEMapIter;
28 typedef AddressToDIEMap::const_iterator AddressToDIEMapConstIter;
29 
30 typedef std::map<dw_offset_t, dw_offset_t> DIEToDIEMap;
31 typedef DIEToDIEMap::iterator DIEToDIEMapIter;
32 typedef DIEToDIEMap::const_iterator DIEToDIEMapConstIter;
33 
34 typedef std::map<uint32_t, const DWARFDebugInfoEntry *> UInt32ToDIEMap;
35 typedef UInt32ToDIEMap::iterator UInt32ToDIEMapIter;
36 typedef UInt32ToDIEMap::const_iterator UInt32ToDIEMapConstIter;
37 
38 typedef std::multimap<uint32_t, const DWARFDebugInfoEntry *> UInt32ToDIEMMap;
39 typedef UInt32ToDIEMMap::iterator UInt32ToDIEMMapIter;
40 typedef UInt32ToDIEMMap::const_iterator UInt32ToDIEMMapConstIter;
41 
42 class DWARFDeclContext;
43 
44 #define DIE_SIBLING_IDX_BITSIZE 31
45 
46 class DWARFDebugInfoEntry {
47 public:
48   typedef std::vector<DWARFDebugInfoEntry> collection;
49   typedef collection::iterator iterator;
50   typedef collection::const_iterator const_iterator;
51 
52   typedef std::vector<dw_offset_t> offset_collection;
53   typedef offset_collection::iterator offset_collection_iterator;
54   typedef offset_collection::const_iterator offset_collection_const_iterator;
55 
56   DWARFDebugInfoEntry()
57       : m_offset(DW_INVALID_OFFSET), m_parent_idx(0), m_sibling_idx(0),
58         m_has_children(false), m_abbr_idx(0), m_tag(0) {}
59 
60   explicit operator bool() const { return m_offset != DW_INVALID_OFFSET; }
61   bool operator==(const DWARFDebugInfoEntry &rhs) const;
62   bool operator!=(const DWARFDebugInfoEntry &rhs) const;
63 
64   void BuildAddressRangeTable(SymbolFileDWARF *dwarf2Data,
65                               const DWARFUnit *cu,
66                               DWARFDebugAranges *debug_aranges) const;
67 
68   void BuildFunctionAddressRangeTable(SymbolFileDWARF *dwarf2Data,
69                                       const DWARFUnit *cu,
70                                       DWARFDebugAranges *debug_aranges) const;
71 
72   bool FastExtract(const lldb_private::DWARFDataExtractor &debug_info_data,
73                    const DWARFUnit *cu,
74                    const DWARFFormValue::FixedFormSizes &fixed_form_sizes,
75                    lldb::offset_t *offset_ptr);
76 
77   bool Extract(const DWARFUnit *cu, lldb::offset_t *offset_ptr);
78 
79   bool LookupAddress(const dw_addr_t address, SymbolFileDWARF *dwarf2Data,
80                      const DWARFUnit *cu,
81                      DWARFDebugInfoEntry **function_die,
82                      DWARFDebugInfoEntry **block_die);
83 
84   size_t GetAttributes(const DWARFUnit *cu,
85                        DWARFFormValue::FixedFormSizes fixed_form_sizes,
86                        DWARFAttributes &attrs,
87                        uint32_t curr_depth = 0)
88       const; // "curr_depth" for internal use only, don't set this yourself!!!
89 
90   dw_offset_t
91   GetAttributeValue(SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
92                     const dw_attr_t attr, DWARFFormValue &formValue,
93                     dw_offset_t *end_attr_offset_ptr = nullptr,
94                     bool check_specification_or_abstract_origin = false) const;
95 
96   const char *GetAttributeValueAsString(
97       SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
98       const dw_attr_t attr, const char *fail_value,
99       bool check_specification_or_abstract_origin = false) const;
100 
101   uint64_t GetAttributeValueAsUnsigned(
102       SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
103       const dw_attr_t attr, uint64_t fail_value,
104       bool check_specification_or_abstract_origin = false) const;
105 
106   uint64_t GetAttributeValueAsReference(
107       SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
108       const dw_attr_t attr, uint64_t fail_value,
109       bool check_specification_or_abstract_origin = false) const;
110 
111   uint64_t GetAttributeValueAsAddress(
112       SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
113       const dw_attr_t attr, uint64_t fail_value,
114       bool check_specification_or_abstract_origin = false) const;
115 
116   dw_addr_t
117   GetAttributeHighPC(SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
118                      dw_addr_t lo_pc, uint64_t fail_value,
119                      bool check_specification_or_abstract_origin = false) const;
120 
121   bool GetAttributeAddressRange(
122       SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu, dw_addr_t &lo_pc,
123       dw_addr_t &hi_pc, uint64_t fail_value,
124       bool check_specification_or_abstract_origin = false) const;
125 
126   size_t GetAttributeAddressRanges(
127       SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
128       DWARFRangeList &ranges, bool check_hi_lo_pc,
129       bool check_specification_or_abstract_origin = false) const;
130 
131   const char *GetName(SymbolFileDWARF *dwarf2Data,
132                       const DWARFUnit *cu) const;
133 
134   const char *GetMangledName(SymbolFileDWARF *dwarf2Data,
135                              const DWARFUnit *cu,
136                              bool substitute_name_allowed = true) const;
137 
138   const char *GetPubname(SymbolFileDWARF *dwarf2Data,
139                          const DWARFUnit *cu) const;
140 
141   static bool GetName(SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
142                       const dw_offset_t die_offset, lldb_private::Stream &s);
143 
144   static bool AppendTypeName(SymbolFileDWARF *dwarf2Data,
145                              const DWARFUnit *cu,
146                              const dw_offset_t die_offset,
147                              lldb_private::Stream &s);
148 
149   const char *GetQualifiedName(SymbolFileDWARF *dwarf2Data,
150                                DWARFUnit *cu,
151                                std::string &storage) const;
152 
153   const char *GetQualifiedName(SymbolFileDWARF *dwarf2Data,
154                                DWARFUnit *cu,
155                                const DWARFAttributes &attributes,
156                                std::string &storage) const;
157 
158   static bool OffsetLessThan(const DWARFDebugInfoEntry &a,
159                              const DWARFDebugInfoEntry &b);
160 
161   void Dump(SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
162             lldb_private::Stream &s, uint32_t recurse_depth) const;
163 
164   static void
165   DumpAttribute(SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
166                 const lldb_private::DWARFDataExtractor &debug_info_data,
167                 lldb::offset_t *offset_ptr, lldb_private::Stream &s,
168                 dw_attr_t attr, DWARFFormValue &form_value);
169 
170   bool
171   GetDIENamesAndRanges(SymbolFileDWARF *dwarf2Data, const DWARFUnit *cu,
172                        const char *&name, const char *&mangled,
173                        DWARFRangeList &rangeList, int &decl_file,
174                        int &decl_line, int &decl_column, int &call_file,
175                        int &call_line, int &call_column,
176                        lldb_private::DWARFExpression *frame_base = NULL) const;
177 
178   const DWARFAbbreviationDeclaration *
179   GetAbbreviationDeclarationPtr(SymbolFileDWARF *dwarf2Data,
180                                 const DWARFUnit *cu,
181                                 lldb::offset_t &offset) const;
182 
183   dw_tag_t Tag() const { return m_tag; }
184 
185   bool IsNULL() const { return m_abbr_idx == 0; }
186 
187   dw_offset_t GetOffset() const { return m_offset; }
188 
189   bool HasChildren() const { return m_has_children; }
190 
191   void SetHasChildren(bool b) { m_has_children = b; }
192 
193   // We know we are kept in a vector of contiguous entries, so we know
194   // our parent will be some index behind "this".
195   DWARFDebugInfoEntry *GetParent() {
196     return m_parent_idx > 0 ? this - m_parent_idx : NULL;
197   }
198   const DWARFDebugInfoEntry *GetParent() const {
199     return m_parent_idx > 0 ? this - m_parent_idx : NULL;
200   }
201   // We know we are kept in a vector of contiguous entries, so we know
202   // our sibling will be some index after "this".
203   DWARFDebugInfoEntry *GetSibling() {
204     return m_sibling_idx > 0 ? this + m_sibling_idx : NULL;
205   }
206   const DWARFDebugInfoEntry *GetSibling() const {
207     return m_sibling_idx > 0 ? this + m_sibling_idx : NULL;
208   }
209   // We know we are kept in a vector of contiguous entries, so we know
210   // we don't need to store our child pointer, if we have a child it will
211   // be the next entry in the list...
212   DWARFDebugInfoEntry *GetFirstChild() {
213     return HasChildren() ? this + 1 : NULL;
214   }
215   const DWARFDebugInfoEntry *GetFirstChild() const {
216     return HasChildren() ? this + 1 : NULL;
217   }
218 
219   std::vector<DWARFDIE> GetDeclContextDIEs(DWARFUnit *cu) const;
220 
221   void GetDWARFDeclContext(SymbolFileDWARF *dwarf2Data, DWARFUnit *cu,
222                            DWARFDeclContext &dwarf_decl_ctx) const;
223 
224   bool MatchesDWARFDeclContext(SymbolFileDWARF *dwarf2Data,
225                                DWARFUnit *cu,
226                                const DWARFDeclContext &dwarf_decl_ctx) const;
227 
228   DWARFDIE GetParentDeclContextDIE(SymbolFileDWARF *dwarf2Data,
229                                    DWARFUnit *cu) const;
230   DWARFDIE GetParentDeclContextDIE(SymbolFileDWARF *dwarf2Data,
231                                    DWARFUnit *cu,
232                                    const DWARFAttributes &attributes) const;
233 
234   void SetParent(DWARFDebugInfoEntry *parent) {
235     if (parent) {
236       // We know we are kept in a vector of contiguous entries, so we know
237       // our parent will be some index behind "this".
238       m_parent_idx = this - parent;
239     } else
240       m_parent_idx = 0;
241   }
242   void SetSibling(DWARFDebugInfoEntry *sibling) {
243     if (sibling) {
244       // We know we are kept in a vector of contiguous entries, so we know
245       // our sibling will be some index after "this".
246       m_sibling_idx = sibling - this;
247       sibling->SetParent(GetParent());
248     } else
249       m_sibling_idx = 0;
250   }
251 
252   void SetSiblingIndex(uint32_t idx) { m_sibling_idx = idx; }
253 
254   void SetParentIndex(uint32_t idx) { m_parent_idx = idx; }
255 
256 protected:
257   dw_offset_t
258       m_offset; // Offset within the .debug_info of the start of this entry
259   uint32_t m_parent_idx; // How many to subtract from "this" to get the parent.
260                          // If zero this die has no parent
261   uint32_t m_sibling_idx : 31, // How many to add to "this" to get the sibling.
262       // If it is zero, then the DIE doesn't have children, or the
263       // DWARF claimed it had children but the DIE only contained
264       // a single NULL terminating child.
265       m_has_children : 1;
266   uint16_t m_abbr_idx;
267   uint16_t m_tag; // A copy of the DW_TAG value so we don't have to go through
268                   // the compile unit abbrev table
269 };
270 
271 #endif // SymbolFileDWARF_DWARFDebugInfoEntry_h_
272