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