1 //===-- DWARFUnit.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_DWARFUnit_h_
10 #define SymbolFileDWARF_DWARFUnit_h_
11 
12 #include "DWARFDIE.h"
13 #include "DWARFDebugInfoEntry.h"
14 #include "lldb/lldb-enumerations.h"
15 #include "llvm/Support/RWMutex.h"
16 #include <atomic>
17 
18 class DWARFUnit;
19 class DWARFCompileUnit;
20 class NameToDIE;
21 class SymbolFileDWARF;
22 class SymbolFileDWARFDwo;
23 
24 typedef std::shared_ptr<DWARFUnit> DWARFUnitSP;
25 
26 enum DWARFProducer {
27   eProducerInvalid = 0,
28   eProducerClang,
29   eProducerGCC,
30   eProducerLLVMGCC,
31   eProcucerOther
32 };
33 
34 /// Base class describing the header of any kind of "unit."  Some information
35 /// is specific to certain unit types.  We separate this class out so we can
36 /// parse the header before deciding what specific kind of unit to construct.
37 class DWARFUnitHeader {
38   dw_offset_t m_offset = 0;
39   dw_offset_t m_length = 0;
40   uint16_t m_version = 0;
41   dw_offset_t m_abbr_offset = 0;
42   uint8_t m_unit_type = 0;
43   uint8_t m_addr_size = 0;
44 
45   uint64_t m_type_hash = 0;
46   uint32_t m_type_offset = 0;
47 
48   uint64_t m_dwo_id = 0;
49 
50   DWARFUnitHeader() = default;
51 
52 public:
53   dw_offset_t GetOffset() const { return m_offset; }
54   uint16_t GetVersion() const { return m_version; }
55   uint16_t GetAddressByteSize() const { return m_addr_size; }
56   dw_offset_t GetLength() const { return m_length; }
57   dw_offset_t GetAbbrOffset() const { return m_abbr_offset; }
58   uint8_t GetUnitType() const { return m_unit_type; }
59   uint64_t GetTypeHash() const { return m_type_hash; }
60   dw_offset_t GetTypeOffset() const { return m_type_offset; }
61   bool IsTypeUnit() const {
62     return m_unit_type == DW_UT_type || m_unit_type == DW_UT_split_type;
63   }
64   uint32_t GetNextUnitOffset() const { return m_offset + m_length + 4; }
65 
66   static llvm::Expected<DWARFUnitHeader>
67   extract(const lldb_private::DWARFDataExtractor &data, DIERef::Section section,
68           lldb::offset_t *offset_ptr);
69 };
70 
71 class DWARFUnit : public lldb_private::UserID {
72   using die_iterator_range =
73       llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>;
74 
75 public:
76   static llvm::Expected<DWARFUnitSP>
77   extract(SymbolFileDWARF &dwarf2Data, lldb::user_id_t uid,
78           const lldb_private::DWARFDataExtractor &debug_info,
79           DIERef::Section section, lldb::offset_t *offset_ptr);
80   virtual ~DWARFUnit();
81 
82   void ExtractUnitDIEIfNeeded();
83   void ExtractDIEsIfNeeded();
84 
85   class ScopedExtractDIEs {
86     DWARFUnit *m_cu;
87   public:
88     bool m_clear_dies = false;
89     ScopedExtractDIEs(DWARFUnit &cu);
90     ~ScopedExtractDIEs();
91     DISALLOW_COPY_AND_ASSIGN(ScopedExtractDIEs);
92     ScopedExtractDIEs(ScopedExtractDIEs &&rhs);
93     ScopedExtractDIEs &operator=(ScopedExtractDIEs &&rhs);
94   };
95   ScopedExtractDIEs ExtractDIEsScoped();
96 
97   DWARFDIE LookupAddress(const dw_addr_t address);
98   size_t AppendDIEsWithTag(const dw_tag_t tag, std::vector<DWARFDIE> &dies,
99                            uint32_t depth = UINT32_MAX) const;
100   bool Verify(lldb_private::Stream *s) const;
101   virtual void Dump(lldb_private::Stream *s) const = 0;
102   /// Get the data that contains the DIE information for this unit.
103   ///
104   /// This will return the correct bytes that contain the data for
105   /// this DWARFUnit. It could be .debug_info or .debug_types
106   /// depending on where the data for this unit originates.
107   ///
108   /// \return
109   ///   The correct data for the DIE information in this unit.
110   const lldb_private::DWARFDataExtractor &GetData() const;
111 
112   /// Get the size in bytes of the unit header.
113   ///
114   /// \return
115   ///     Byte size of the unit header
116   uint32_t GetHeaderByteSize() const;
117 
118   // Offset of the initial length field.
119   dw_offset_t GetOffset() const { return m_header.GetOffset(); }
120   /// Get the size in bytes of the length field in the header.
121   ///
122   /// In DWARF32 this is just 4 bytes
123   ///
124   /// \return
125   ///     Byte size of the compile unit header length field
126   size_t GetLengthByteSize() const { return 4; }
127 
128   bool ContainsDIEOffset(dw_offset_t die_offset) const {
129     return die_offset >= GetFirstDIEOffset() &&
130            die_offset < GetNextUnitOffset();
131   }
132   dw_offset_t GetFirstDIEOffset() const {
133     return GetOffset() + GetHeaderByteSize();
134   }
135   dw_offset_t GetNextUnitOffset() const { return m_header.GetNextUnitOffset(); }
136   // Size of the CU data (without initial length and without header).
137   size_t GetDebugInfoSize() const;
138   // Size of the CU data incl. header but without initial length.
139   uint32_t GetLength() const { return m_header.GetLength(); }
140   uint16_t GetVersion() const { return m_header.GetVersion(); }
141   const DWARFAbbreviationDeclarationSet *GetAbbreviations() const;
142   dw_offset_t GetAbbrevOffset() const;
143   uint8_t GetAddressByteSize() const { return m_header.GetAddressByteSize(); }
144   dw_addr_t GetAddrBase() const { return m_addr_base; }
145   dw_addr_t GetBaseAddress() const { return m_base_addr; }
146   dw_offset_t GetLineTableOffset();
147   dw_addr_t GetRangesBase() const { return m_ranges_base; }
148   dw_addr_t GetStrOffsetsBase() const { return m_str_offsets_base; }
149   void SetAddrBase(dw_addr_t addr_base);
150   void SetRangesBase(dw_addr_t ranges_base);
151   void SetStrOffsetsBase(dw_offset_t str_offsets_base);
152   virtual void BuildAddressRangeTable(DWARFDebugAranges *debug_aranges) = 0;
153 
154   lldb::ByteOrder GetByteOrder() const;
155 
156   lldb_private::TypeSystem *GetTypeSystem();
157 
158   const DWARFDebugAranges &GetFunctionAranges();
159 
160   void SetBaseAddress(dw_addr_t base_addr);
161 
162   DWARFBaseDIE GetUnitDIEOnly() { return DWARFDIE(this, GetUnitDIEPtrOnly()); }
163 
164   DWARFDIE DIE() { return DWARFDIE(this, DIEPtr()); }
165 
166   DWARFDIE GetDIE(dw_offset_t die_offset);
167 
168   DWARFUnit &GetNonSkeletonUnit();
169 
170   static uint8_t GetAddressByteSize(const DWARFUnit *cu);
171 
172   static uint8_t GetDefaultAddressSize();
173 
174   void *GetUserData() const;
175 
176   void SetUserData(void *d);
177 
178   bool Supports_DW_AT_APPLE_objc_complete_type();
179 
180   bool DW_AT_decl_file_attributes_are_invalid();
181 
182   bool Supports_unnamed_objc_bitfields();
183 
184   SymbolFileDWARF &GetSymbolFileDWARF() const { return m_dwarf; }
185 
186   DWARFProducer GetProducer();
187 
188   uint32_t GetProducerVersionMajor();
189 
190   uint32_t GetProducerVersionMinor();
191 
192   uint32_t GetProducerVersionUpdate();
193 
194   static lldb::LanguageType LanguageTypeFromDWARF(uint64_t val);
195 
196   lldb::LanguageType GetLanguageType();
197 
198   bool GetIsOptimized();
199 
200   const lldb_private::FileSpec &GetCompilationDirectory();
201   const lldb_private::FileSpec &GetAbsolutePath();
202   lldb_private::FileSpec GetFile(size_t file_idx);
203   lldb_private::FileSpec::Style GetPathStyle();
204 
205   SymbolFileDWARFDwo *GetDwoSymbolFile() const;
206 
207   die_iterator_range dies() {
208     ExtractDIEsIfNeeded();
209     return die_iterator_range(m_die_array.begin(), m_die_array.end());
210   }
211 
212   DIERef::Section GetDebugSection() const { return m_section; }
213 
214   uint8_t GetUnitType() const { return m_header.GetUnitType(); }
215   bool IsTypeUnit() const { return m_header.IsTypeUnit(); }
216 
217   /// Return a list of address ranges resulting from a (possibly encoded)
218   /// range list starting at a given offset in the appropriate ranges section.
219   llvm::Expected<DWARFRangeList> FindRnglistFromOffset(dw_offset_t offset) const;
220 
221   /// Return a list of address ranges retrieved from an encoded range
222   /// list whose offset is found via a table lookup given an index (DWARF v5
223   /// and later).
224   llvm::Expected<DWARFRangeList> FindRnglistFromIndex(uint32_t index) const;
225 
226 protected:
227   DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid,
228             const DWARFUnitHeader &header,
229             const DWARFAbbreviationDeclarationSet &abbrevs,
230             DIERef::Section section);
231 
232   llvm::Error ExtractHeader(SymbolFileDWARF &dwarf,
233                             const lldb_private::DWARFDataExtractor &data,
234                             lldb::offset_t *offset_ptr);
235 
236   // Get the DWARF unit DWARF debug information entry. Parse the single DIE
237   // if needed.
238   const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() {
239     ExtractUnitDIEIfNeeded();
240     // m_first_die_mutex is not required as m_first_die is never cleared.
241     if (!m_first_die)
242       return NULL;
243     return &m_first_die;
244   }
245 
246   // Get all DWARF debug informration entries. Parse all DIEs if needed.
247   const DWARFDebugInfoEntry *DIEPtr() {
248     ExtractDIEsIfNeeded();
249     if (m_die_array.empty())
250       return NULL;
251     return &m_die_array[0];
252   }
253 
254   SymbolFileDWARF &m_dwarf;
255   std::unique_ptr<SymbolFileDWARFDwo> m_dwo_symbol_file;
256   DWARFUnitHeader m_header;
257   const DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr;
258   void *m_user_data = nullptr;
259   // The compile unit debug information entry item
260   DWARFDebugInfoEntry::collection m_die_array;
261   mutable llvm::sys::RWMutex m_die_array_mutex;
262   // It is used for tracking of ScopedExtractDIEs instances.
263   mutable llvm::sys::RWMutex m_die_array_scoped_mutex;
264   // ScopedExtractDIEs instances should not call ClearDIEsRWLocked()
265   // as someone called ExtractDIEsIfNeeded().
266   std::atomic<bool> m_cancel_scopes;
267   // GetUnitDIEPtrOnly() needs to return pointer to the first DIE.
268   // But the first element of m_die_array after ExtractUnitDIEIfNeeded()
269   // would possibly move in memory after later ExtractDIEsIfNeeded().
270   DWARFDebugInfoEntry m_first_die;
271   llvm::sys::RWMutex m_first_die_mutex;
272   // A table similar to the .debug_aranges table, but this one points to the
273   // exact DW_TAG_subprogram DIEs
274   std::unique_ptr<DWARFDebugAranges> m_func_aranges_up;
275   dw_addr_t m_base_addr = 0;
276   DWARFProducer m_producer = eProducerInvalid;
277   uint32_t m_producer_version_major = 0;
278   uint32_t m_producer_version_minor = 0;
279   uint32_t m_producer_version_update = 0;
280   lldb::LanguageType m_language_type = lldb::eLanguageTypeUnknown;
281   lldb_private::LazyBool m_is_optimized = lldb_private::eLazyBoolCalculate;
282   llvm::Optional<lldb_private::FileSpec> m_comp_dir;
283   llvm::Optional<lldb_private::FileSpec> m_file_spec;
284   dw_addr_t m_addr_base = 0;   // Value of DW_AT_addr_base
285   dw_addr_t m_ranges_base = 0; // Value of DW_AT_ranges_base
286 
287   /// Value of DW_AT_stmt_list.
288   dw_offset_t m_line_table_offset = DW_INVALID_OFFSET;
289 
290   dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base.
291   const DIERef::Section m_section;
292 
293 private:
294   void ParseProducerInfo();
295   void ExtractDIEsRWLocked();
296   void ClearDIEsRWLocked();
297 
298   void AddUnitDIE(const DWARFDebugInfoEntry &cu_die);
299 
300   void ComputeCompDirAndGuessPathStyle();
301   void ComputeAbsolutePath();
302 
303   DISALLOW_COPY_AND_ASSIGN(DWARFUnit);
304 };
305 
306 #endif // SymbolFileDWARF_DWARFUnit_h_
307