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