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; }
GetDWOId()68   uint64_t GetDWOId() const { return m_dwo_id; }
IsTypeUnit()69   bool IsTypeUnit() const {
70     return m_unit_type == DW_UT_type || m_unit_type == DW_UT_split_type;
71   }
GetNextUnitOffset()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_private::DWARFContext &dwarf_context,
77           lldb::offset_t *offset_ptr);
78 };
79 
80 class DWARFUnit : public lldb_private::UserID {
81   using die_iterator_range =
82       llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>;
83 
84 public:
85   static llvm::Expected<DWARFUnitSP>
86   extract(SymbolFileDWARF &dwarf2Data, lldb::user_id_t uid,
87           const lldb_private::DWARFDataExtractor &debug_info,
88           DIERef::Section section, lldb::offset_t *offset_ptr);
89   virtual ~DWARFUnit();
90 
IsDWOUnit()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.
GetOffset()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
GetLengthByteSize()136   size_t GetLengthByteSize() const { return 4; }
137 
ContainsDIEOffset(dw_offset_t die_offset)138   bool ContainsDIEOffset(dw_offset_t die_offset) const {
139     return die_offset >= GetFirstDIEOffset() &&
140            die_offset < GetNextUnitOffset();
141   }
GetFirstDIEOffset()142   dw_offset_t GetFirstDIEOffset() const {
143     return GetOffset() + GetHeaderByteSize();
144   }
GetNextUnitOffset()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.
GetLength()149   uint32_t GetLength() const { return m_header.GetLength(); }
GetVersion()150   uint16_t GetVersion() const { return m_header.GetVersion(); }
151   const DWARFAbbreviationDeclarationSet *GetAbbreviations() const;
152   dw_offset_t GetAbbrevOffset() const;
GetAddressByteSize()153   uint8_t GetAddressByteSize() const { return m_header.GetAddressByteSize(); }
GetAddrBase()154   dw_addr_t GetAddrBase() const { return m_addr_base; }
GetBaseAddress()155   dw_addr_t GetBaseAddress() const { return m_base_addr; }
156   dw_offset_t GetLineTableOffset();
GetRangesBase()157   dw_addr_t GetRangesBase() const { return m_ranges_base; }
GetStrOffsetsBase()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 
GetUnitDIEOnly()171   DWARFBaseDIE GetUnitDIEOnly() { return {this, GetUnitDIEPtrOnly()}; }
172 
DIE()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 
GetSymbolFileDWARF()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 
dies()214   die_iterator_range dies() {
215     ExtractDIEsIfNeeded();
216     return die_iterator_range(m_die_array.begin(), m_die_array.end());
217   }
218 
GetDebugSection()219   DIERef::Section GetDebugSection() const { return m_section; }
220 
GetUnitType()221   uint8_t GetUnitType() const { return m_header.GetUnitType(); }
IsTypeUnit()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 
GetLoclistOffset(uint32_t Index)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.
GetUnitDIEPtrOnly()270   const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() {
271     ExtractUnitDIEIfNeeded();
272     // m_first_die_mutex is not required as m_first_die is never cleared.
273     if (!m_first_die)
274       return NULL;
275     return &m_first_die;
276   }
277 
278   // Get all DWARF debug informration entries. Parse all DIEs if needed.
DIEPtr()279   const DWARFDebugInfoEntry *DIEPtr() {
280     ExtractDIEsIfNeeded();
281     if (m_die_array.empty())
282       return NULL;
283     return &m_die_array[0];
284   }
285 
286   const llvm::Optional<llvm::DWARFDebugRnglistTable> &GetRnglistTable();
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   uint32_t m_producer_version_major = 0;
312   uint32_t m_producer_version_minor = 0;
313   uint32_t m_producer_version_update = 0;
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   dw_addr_t m_addr_base = 0;     ///< 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 
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   /// Value of DW_AT_GNU_dwo_id (v4) or dwo_id from CU header (v5).
334   uint64_t m_dwo_id;
335 
336 private:
337   void ParseProducerInfo();
338   void ExtractDIEsRWLocked();
339   void ClearDIEsRWLocked();
340 
341   void AddUnitDIE(const DWARFDebugInfoEntry &cu_die);
342   void SetDwoStrOffsetsBase();
343 
344   void ComputeCompDirAndGuessPathStyle();
345   void ComputeAbsolutePath();
346 
347   DWARFUnit(const DWARFUnit &) = delete;
348   const DWARFUnit &operator=(const DWARFUnit &) = delete;
349 };
350 
351 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H
352