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