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