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   bool IsTypeUnit() const {
69     return m_unit_type == DW_UT_type || m_unit_type == DW_UT_split_type;
70   }
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 
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     DISALLOW_COPY_AND_ASSIGN(ScopedExtractDIEs);
102     ScopedExtractDIEs(ScopedExtractDIEs &&rhs);
103     ScopedExtractDIEs &operator=(ScopedExtractDIEs &&rhs);
104   };
105   ScopedExtractDIEs ExtractDIEsScoped();
106 
107   DWARFDIE LookupAddress(const dw_addr_t address);
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.
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
134   size_t GetLengthByteSize() const { return 4; }
135 
136   bool ContainsDIEOffset(dw_offset_t die_offset) const {
137     return die_offset >= GetFirstDIEOffset() &&
138            die_offset < GetNextUnitOffset();
139   }
140   dw_offset_t GetFirstDIEOffset() const {
141     return GetOffset() + GetHeaderByteSize();
142   }
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.
147   uint32_t GetLength() const { return m_header.GetLength(); }
148   uint16_t GetVersion() const { return m_header.GetVersion(); }
149   const DWARFAbbreviationDeclarationSet *GetAbbreviations() const;
150   dw_offset_t GetAbbrevOffset() const;
151   uint8_t GetAddressByteSize() const { return m_header.GetAddressByteSize(); }
152   dw_addr_t GetAddrBase() const { return m_addr_base; }
153   dw_addr_t GetBaseAddress() const { return m_base_addr; }
154   dw_offset_t GetLineTableOffset();
155   dw_addr_t GetRangesBase() const { return m_ranges_base; }
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 
169   DWARFBaseDIE GetUnitDIEOnly() { return DWARFDIE(this, GetUnitDIEPtrOnly()); }
170 
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 
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 
212   die_iterator_range dies() {
213     ExtractDIEsIfNeeded();
214     return die_iterator_range(m_die_array.begin(), m_die_array.end());
215   }
216 
217   DIERef::Section GetDebugSection() const { return m_section; }
218 
219   uint8_t GetUnitType() const { return m_header.GetUnitType(); }
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::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(Index))
240       return *off + m_ranges_base;
241     return llvm::None;
242   }
243 
244   llvm::Optional<uint64_t> GetLoclistOffset(uint32_t Index) {
245     if (!m_loclist_table_header)
246       return llvm::None;
247 
248     llvm::Optional<uint64_t> Offset =  m_loclist_table_header->getOffsetEntry(Index);
249     if (!Offset)
250       return llvm::None;
251     return *Offset + m_loclists_base;
252   }
253 
254   /// Return the location table for parsing the given location list data. The
255   /// format is chosen according to the unit type. Never returns null.
256   std::unique_ptr<llvm::DWARFLocationTable>
257   GetLocationTable(const lldb_private::DataExtractor &data) const;
258 
259   lldb_private::DWARFDataExtractor GetLocationData() const;
260 
261 protected:
262   DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid,
263             const DWARFUnitHeader &header,
264             const DWARFAbbreviationDeclarationSet &abbrevs,
265             DIERef::Section section, bool is_dwo);
266 
267   llvm::Error ExtractHeader(SymbolFileDWARF &dwarf,
268                             const lldb_private::DWARFDataExtractor &data,
269                             lldb::offset_t *offset_ptr);
270 
271   // Get the DWARF unit DWARF debug information entry. Parse the single DIE
272   // if needed.
273   const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() {
274     ExtractUnitDIEIfNeeded();
275     // m_first_die_mutex is not required as m_first_die is never cleared.
276     if (!m_first_die)
277       return NULL;
278     return &m_first_die;
279   }
280 
281   // Get all DWARF debug informration entries. Parse all DIEs if needed.
282   const DWARFDebugInfoEntry *DIEPtr() {
283     ExtractDIEsIfNeeded();
284     if (m_die_array.empty())
285       return NULL;
286     return &m_die_array[0];
287   }
288 
289   SymbolFileDWARF &m_dwarf;
290   std::shared_ptr<DWARFUnit> m_dwo;
291   DWARFUnitHeader m_header;
292   const DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr;
293   void *m_user_data = nullptr;
294   // The compile unit debug information entry item
295   DWARFDebugInfoEntry::collection m_die_array;
296   mutable llvm::sys::RWMutex m_die_array_mutex;
297   // It is used for tracking of ScopedExtractDIEs instances.
298   mutable llvm::sys::RWMutex m_die_array_scoped_mutex;
299   // ScopedExtractDIEs instances should not call ClearDIEsRWLocked()
300   // as someone called ExtractDIEsIfNeeded().
301   std::atomic<bool> m_cancel_scopes;
302   // GetUnitDIEPtrOnly() needs to return pointer to the first DIE.
303   // But the first element of m_die_array after ExtractUnitDIEIfNeeded()
304   // would possibly move in memory after later ExtractDIEsIfNeeded().
305   DWARFDebugInfoEntry m_first_die;
306   llvm::sys::RWMutex m_first_die_mutex;
307   // A table similar to the .debug_aranges table, but this one points to the
308   // exact DW_TAG_subprogram DIEs
309   std::unique_ptr<DWARFDebugAranges> m_func_aranges_up;
310   dw_addr_t m_base_addr = 0;
311   DWARFProducer m_producer = eProducerInvalid;
312   uint32_t m_producer_version_major = 0;
313   uint32_t m_producer_version_minor = 0;
314   uint32_t m_producer_version_update = 0;
315   llvm::Optional<uint64_t> m_language_type;
316   lldb_private::LazyBool m_is_optimized = lldb_private::eLazyBoolCalculate;
317   llvm::Optional<lldb_private::FileSpec> m_comp_dir;
318   llvm::Optional<lldb_private::FileSpec> m_file_spec;
319   dw_addr_t m_addr_base = 0;     ///< Value of DW_AT_addr_base.
320   dw_addr_t m_loclists_base = 0; ///< Value of DW_AT_loclists_base.
321   dw_addr_t m_ranges_base = 0;   ///< Value of DW_AT_rnglists_base.
322 
323   /// Value of DW_AT_stmt_list.
324   dw_offset_t m_line_table_offset = DW_INVALID_OFFSET;
325 
326   dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base.
327 
328   llvm::Optional<llvm::DWARFDebugRnglistTable> m_rnglist_table;
329   llvm::Optional<llvm::DWARFListTableHeader> m_loclist_table_header;
330 
331   const DIERef::Section m_section;
332   bool m_is_dwo;
333 
334 private:
335   void ParseProducerInfo();
336   void ExtractDIEsRWLocked();
337   void ClearDIEsRWLocked();
338 
339   void AddUnitDIE(const DWARFDebugInfoEntry &cu_die);
340   void SetDwoStrOffsetsBase();
341 
342   void ComputeCompDirAndGuessPathStyle();
343   void ComputeAbsolutePath();
344 
345   DISALLOW_COPY_AND_ASSIGN(DWARFUnit);
346 };
347 
348 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H
349