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