1 //===-- SymbolFileDWARF.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 SymbolFileDWARF_SymbolFileDWARF_h_
10 #define SymbolFileDWARF_SymbolFileDWARF_h_
11 
12 #include <list>
13 #include <map>
14 #include <mutex>
15 #include <set>
16 #include <unordered_map>
17 #include <vector>
18 
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Threading.h"
21 
22 #include "lldb/Core/UniqueCStringMap.h"
23 #include "lldb/Core/dwarf.h"
24 #include "lldb/Expression/DWARFExpression.h"
25 #include "lldb/Symbol/DebugMacros.h"
26 #include "lldb/Symbol/SymbolContext.h"
27 #include "lldb/Symbol/SymbolFile.h"
28 #include "lldb/Utility/ConstString.h"
29 #include "lldb/Utility/Flags.h"
30 #include "lldb/Utility/RangeMap.h"
31 #include "lldb/lldb-private.h"
32 
33 #include "DWARFContext.h"
34 #include "DWARFDataExtractor.h"
35 #include "DWARFDefines.h"
36 #include "DWARFIndex.h"
37 #include "UniqueDWARFASTType.h"
38 
39 //----------------------------------------------------------------------
40 // Forward Declarations for this DWARF plugin
41 //----------------------------------------------------------------------
42 class DebugMapModule;
43 class DWARFAbbreviationDeclaration;
44 class DWARFAbbreviationDeclarationSet;
45 class DWARFileUnit;
46 class DWARFDebugAbbrev;
47 class DWARFDebugAranges;
48 class DWARFDebugInfo;
49 class DWARFDebugInfoEntry;
50 class DWARFDebugLine;
51 class DWARFDebugRangesBase;
52 class DWARFDeclContext;
53 class DWARFFormValue;
54 class SymbolFileDWARFDebugMap;
55 class SymbolFileDWARFDwo;
56 class SymbolFileDWARFDwp;
57 
58 #define DIE_IS_BEING_PARSED ((lldb_private::Type *)1)
59 
60 class SymbolFileDWARF : public lldb_private::SymbolFile,
61                         public lldb_private::UserID {
62 public:
63   friend class SymbolFileDWARFDebugMap;
64   friend class SymbolFileDWARFDwo;
65   friend class DebugMapModule;
66   friend struct DIERef;
67   friend class DWARFUnit;
68   friend class DWARFDIE;
69   friend class DWARFASTParserClang;
70 
71   //------------------------------------------------------------------
72   // Static Functions
73   //------------------------------------------------------------------
74   static void Initialize();
75 
76   static void Terminate();
77 
78   static void DebuggerInitialize(lldb_private::Debugger &debugger);
79 
80   static lldb_private::ConstString GetPluginNameStatic();
81 
82   static const char *GetPluginDescriptionStatic();
83 
84   static lldb_private::SymbolFile *
85   CreateInstance(lldb_private::ObjectFile *obj_file);
86 
87   static const lldb_private::FileSpecList &GetSymlinkPaths();
88 
89   //------------------------------------------------------------------
90   // Constructors and Destructors
91   //------------------------------------------------------------------
92 
93   SymbolFileDWARF(lldb_private::ObjectFile *ofile);
94 
95   ~SymbolFileDWARF() override;
96 
97   uint32_t CalculateAbilities() override;
98 
99   void InitializeObject() override;
100 
101   //------------------------------------------------------------------
102   // Compile Unit function calls
103   //------------------------------------------------------------------
104 
105   uint32_t GetNumCompileUnits() override;
106 
107   lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
108 
109   lldb::LanguageType
110   ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
111 
112   size_t ParseFunctions(lldb_private::CompileUnit &comp_unit) override;
113 
114   bool ParseLineTable(lldb_private::CompileUnit &comp_unit) override;
115 
116   bool ParseDebugMacros(lldb_private::CompileUnit &comp_unit) override;
117 
118   bool ParseSupportFiles(lldb_private::CompileUnit &comp_unit,
119                          lldb_private::FileSpecList &support_files) override;
120 
121   bool ParseIsOptimized(lldb_private::CompileUnit &comp_unit) override;
122 
123   size_t ParseTypes(lldb_private::CompileUnit &comp_unit) override;
124 
125   bool ParseImportedModules(
126       const lldb_private::SymbolContext &sc,
127       std::vector<lldb_private::SourceModule> &imported_modules) override;
128 
129   size_t ParseBlocksRecursive(lldb_private::Function &func) override;
130 
131   size_t
132   ParseVariablesForContext(const lldb_private::SymbolContext &sc) override;
133 
134   lldb_private::Type *ResolveTypeUID(lldb::user_id_t type_uid) override;
135   llvm::Optional<ArrayInfo> GetDynamicArrayInfoForUID(
136       lldb::user_id_t type_uid,
137       const lldb_private::ExecutionContext *exe_ctx) override;
138 
139   bool CompleteType(lldb_private::CompilerType &compiler_type) override;
140 
141   lldb_private::Type *ResolveType(const DWARFDIE &die,
142                                   bool assert_not_being_parsed = true,
143                                   bool resolve_function_context = false);
144 
145   SymbolFileDWARF *GetDWARFForUID(lldb::user_id_t uid);
146 
147   DWARFDIE
148   GetDIEFromUID(lldb::user_id_t uid);
149 
150   lldb_private::CompilerDecl GetDeclForUID(lldb::user_id_t uid) override;
151 
152   lldb_private::CompilerDeclContext
153   GetDeclContextForUID(lldb::user_id_t uid) override;
154 
155   lldb_private::CompilerDeclContext
156   GetDeclContextContainingUID(lldb::user_id_t uid) override;
157 
158   void
159   ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) override;
160 
161   uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr,
162                                 lldb::SymbolContextItem resolve_scope,
163                                 lldb_private::SymbolContext &sc) override;
164 
165   uint32_t
166   ResolveSymbolContext(const lldb_private::FileSpec &file_spec, uint32_t line,
167                        bool check_inlines,
168                        lldb::SymbolContextItem resolve_scope,
169                        lldb_private::SymbolContextList &sc_list) override;
170 
171   uint32_t
172   FindGlobalVariables(lldb_private::ConstString name,
173                       const lldb_private::CompilerDeclContext *parent_decl_ctx,
174                       uint32_t max_matches,
175                       lldb_private::VariableList &variables) override;
176 
177   uint32_t FindGlobalVariables(const lldb_private::RegularExpression &regex,
178                                uint32_t max_matches,
179                                lldb_private::VariableList &variables) override;
180 
181   uint32_t
182   FindFunctions(lldb_private::ConstString name,
183                 const lldb_private::CompilerDeclContext *parent_decl_ctx,
184                 lldb::FunctionNameType name_type_mask, bool include_inlines,
185                 bool append, lldb_private::SymbolContextList &sc_list) override;
186 
187   uint32_t FindFunctions(const lldb_private::RegularExpression &regex,
188                          bool include_inlines, bool append,
189                          lldb_private::SymbolContextList &sc_list) override;
190 
191   void GetMangledNamesForFunction(
192       const std::string &scope_qualified_name,
193       std::vector<lldb_private::ConstString> &mangled_names) override;
194 
195   uint32_t
196   FindTypes(lldb_private::ConstString name,
197             const lldb_private::CompilerDeclContext *parent_decl_ctx,
198             bool append, uint32_t max_matches,
199             llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
200             lldb_private::TypeMap &types) override;
201 
202   size_t FindTypes(const std::vector<lldb_private::CompilerContext> &context,
203                    bool append, lldb_private::TypeMap &types) override;
204 
205   lldb_private::TypeList *GetTypeList() override;
206 
207   size_t GetTypes(lldb_private::SymbolContextScope *sc_scope,
208                   lldb::TypeClass type_mask,
209                   lldb_private::TypeList &type_list) override;
210 
211   lldb_private::TypeSystem *
212   GetTypeSystemForLanguage(lldb::LanguageType language) override;
213 
214   lldb_private::CompilerDeclContext FindNamespace(
215       lldb_private::ConstString name,
216       const lldb_private::CompilerDeclContext *parent_decl_ctx) override;
217 
218   void PreloadSymbols() override;
219 
220   std::recursive_mutex &GetModuleMutex() const override;
221 
222   //------------------------------------------------------------------
223   // PluginInterface protocol
224   //------------------------------------------------------------------
225   lldb_private::ConstString GetPluginName() override;
226 
227   uint32_t GetPluginVersion() override;
228 
229   virtual const lldb_private::DWARFDataExtractor &get_debug_abbrev_data();
230   virtual const lldb_private::DWARFDataExtractor &get_debug_addr_data();
231   const lldb_private::DWARFDataExtractor &get_debug_frame_data();
232   virtual const lldb_private::DWARFDataExtractor &get_debug_info_data();
233   const lldb_private::DWARFDataExtractor &get_debug_line_data();
234   const lldb_private::DWARFDataExtractor &get_debug_line_str_data();
235   const lldb_private::DWARFDataExtractor &get_debug_macro_data();
236   const lldb_private::DWARFDataExtractor &get_debug_loc_data();
237   const lldb_private::DWARFDataExtractor &get_debug_loclists_data();
238   const lldb_private::DWARFDataExtractor &get_debug_ranges_data();
239   const lldb_private::DWARFDataExtractor &get_debug_rnglists_data();
240   virtual const lldb_private::DWARFDataExtractor &get_debug_str_data();
241   virtual const lldb_private::DWARFDataExtractor &get_debug_str_offsets_data();
242   const lldb_private::DWARFDataExtractor &get_debug_types_data();
243   const lldb_private::DWARFDataExtractor &get_apple_names_data();
244   const lldb_private::DWARFDataExtractor &get_apple_types_data();
245   const lldb_private::DWARFDataExtractor &get_apple_namespaces_data();
246   const lldb_private::DWARFDataExtractor &get_apple_objc_data();
247   const lldb_private::DWARFDataExtractor &get_gnu_debugaltlink();
248 
249   DWARFDebugAbbrev *DebugAbbrev();
250 
251   const DWARFDebugAbbrev *DebugAbbrev() const;
252 
253   DWARFDebugInfo *DebugInfo();
254 
255   const DWARFDebugInfo *DebugInfo() const;
256 
257   DWARFDebugRangesBase *DebugRanges();
258 
259   const DWARFDebugRangesBase *DebugRanges() const;
260 
261   const lldb_private::DWARFDataExtractor &DebugLocData();
262 
263   static bool SupportedVersion(uint16_t version);
264 
265   DWARFDIE
266   GetDeclContextDIEContainingDIE(const DWARFDIE &die);
267 
268   bool
269   HasForwardDeclForClangType(const lldb_private::CompilerType &compiler_type);
270 
271   lldb_private::CompileUnit *
272   GetCompUnitForDWARFCompUnit(DWARFUnit *dwarf_cu,
273                               uint32_t cu_idx = UINT32_MAX);
274 
275   virtual size_t GetObjCMethodDIEOffsets(lldb_private::ConstString class_name,
276                                          DIEArray &method_die_offsets);
277 
278   bool Supports_DW_AT_APPLE_objc_complete_type(DWARFUnit *cu);
279 
280   lldb_private::DebugMacrosSP ParseDebugMacros(lldb::offset_t *offset);
281 
282   static DWARFDIE GetParentSymbolContextDIE(const DWARFDIE &die);
283 
284   virtual lldb::CompUnitSP ParseCompileUnit(DWARFUnit *dwarf_cu,
285                                             uint32_t cu_idx);
286 
287   virtual lldb_private::DWARFExpression::LocationListFormat
288   GetLocationListFormat() const;
289 
290   lldb::ModuleSP GetDWOModule(lldb_private::ConstString name);
291 
292   typedef std::map<lldb_private::ConstString, lldb::ModuleSP>
293       ExternalTypeModuleMap;
294 
295   /// Return the list of Clang modules imported by this SymbolFile.
296   const ExternalTypeModuleMap& getExternalTypeModules() const {
297       return m_external_type_modules;
298   }
299 
300   virtual DWARFDIE GetDIE(const DIERef &die_ref);
301 
302   virtual std::unique_ptr<SymbolFileDWARFDwo>
303   GetDwoSymbolFileForCompileUnit(DWARFUnit &dwarf_cu,
304                                  const DWARFDebugInfoEntry &cu_die);
305 
306   // For regular SymbolFileDWARF instances the method returns nullptr,
307   // for the instances of the subclass SymbolFileDWARFDwo
308   // the method returns a pointer to the base compile unit.
309   virtual DWARFUnit *GetBaseCompileUnit();
310 
311   static bool
312   DIEInDeclContext(const lldb_private::CompilerDeclContext *parent_decl_ctx,
313                    const DWARFDIE &die);
314 
315   std::vector<lldb_private::CallEdge>
316   ParseCallEdgesInFunction(UserID func_id) override;
317 
318   void Dump(lldb_private::Stream &s) override;
319 
320   void DumpClangAST(lldb_private::Stream &s) override;
321 
322 protected:
323   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb_private::Type *>
324       DIEToTypePtr;
325   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb::VariableSP>
326       DIEToVariableSP;
327   typedef llvm::DenseMap<const DWARFDebugInfoEntry *,
328                          lldb::opaque_compiler_type_t>
329       DIEToClangType;
330   typedef llvm::DenseMap<lldb::opaque_compiler_type_t, DIERef> ClangTypeToDIE;
331 
332   struct DWARFDataSegment {
333     llvm::once_flag m_flag;
334     lldb_private::DWARFDataExtractor m_data;
335   };
336 
337   DISALLOW_COPY_AND_ASSIGN(SymbolFileDWARF);
338 
339   const lldb_private::DWARFDataExtractor &
340   GetCachedSectionData(lldb::SectionType sect_type,
341                        DWARFDataSegment &data_segment);
342 
343   virtual void LoadSectionData(lldb::SectionType sect_type,
344                                lldb_private::DWARFDataExtractor &data);
345 
346   bool DeclContextMatchesThisSymbolFile(
347       const lldb_private::CompilerDeclContext *decl_ctx);
348 
349   virtual DWARFUnit *
350   GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit);
351 
352   DWARFUnit *GetNextUnparsedDWARFCompileUnit(DWARFUnit *prev_cu);
353 
354   bool GetFunction(const DWARFDIE &die, lldb_private::SymbolContext &sc);
355 
356   lldb_private::Function *ParseFunction(lldb_private::CompileUnit &comp_unit,
357                                         const DWARFDIE &die);
358 
359   size_t ParseBlocksRecursive(lldb_private::CompileUnit &comp_unit,
360                               lldb_private::Block *parent_block,
361                               const DWARFDIE &die,
362                               lldb::addr_t subprogram_low_pc, uint32_t depth);
363 
364   size_t ParseTypes(const lldb_private::SymbolContext &sc, const DWARFDIE &die,
365                     bool parse_siblings, bool parse_children);
366 
367   lldb::TypeSP ParseType(const lldb_private::SymbolContext &sc,
368                          const DWARFDIE &die, bool *type_is_new);
369 
370   lldb_private::Type *ResolveTypeUID(const DWARFDIE &die,
371                                      bool assert_not_being_parsed);
372 
373   lldb_private::Type *ResolveTypeUID(const DIERef &die_ref);
374 
375   lldb::VariableSP ParseVariableDIE(const lldb_private::SymbolContext &sc,
376                                     const DWARFDIE &die,
377                                     const lldb::addr_t func_low_pc);
378 
379   size_t ParseVariables(const lldb_private::SymbolContext &sc,
380                         const DWARFDIE &orig_die,
381                         const lldb::addr_t func_low_pc, bool parse_siblings,
382                         bool parse_children,
383                         lldb_private::VariableList *cc_variable_list = NULL);
384 
385   bool ClassOrStructIsVirtual(const DWARFDIE &die);
386 
387   // Given a die_offset, figure out the symbol context representing that die.
388   bool ResolveFunction(const DWARFDIE &die, bool include_inlines,
389                        lldb_private::SymbolContextList &sc_list);
390 
391   virtual lldb::TypeSP
392   FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext &die_decl_ctx);
393 
394   virtual lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE(
395       const DWARFDIE &die, lldb_private::ConstString type_name,
396       bool must_be_implementation);
397 
398   lldb_private::Symbol *
399   GetObjCClassSymbol(lldb_private::ConstString objc_class_name);
400 
401   lldb::TypeSP GetTypeForDIE(const DWARFDIE &die,
402                              bool resolve_function_context = false);
403 
404   void SetDebugMapModule(const lldb::ModuleSP &module_sp) {
405     m_debug_map_module_wp = module_sp;
406   }
407 
408   SymbolFileDWARFDebugMap *GetDebugMapSymfile();
409 
410   DWARFDIE
411   FindBlockContainingSpecification(const DIERef &func_die_ref,
412                                    dw_offset_t spec_block_die_offset);
413 
414   DWARFDIE
415   FindBlockContainingSpecification(const DWARFDIE &die,
416                                    dw_offset_t spec_block_die_offset);
417 
418   virtual UniqueDWARFASTTypeMap &GetUniqueDWARFASTTypeMap();
419 
420   bool DIEDeclContextsMatch(const DWARFDIE &die1, const DWARFDIE &die2);
421 
422   bool ClassContainsSelector(const DWARFDIE &class_die,
423                              lldb_private::ConstString selector);
424 
425   bool FixupAddress(lldb_private::Address &addr);
426 
427   typedef std::set<lldb_private::Type *> TypeSet;
428 
429   void GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
430                 dw_offset_t max_die_offset, uint32_t type_mask,
431                 TypeSet &type_set);
432 
433   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t,
434                                         lldb_private::Variable *>
435       GlobalVariableMap;
436 
437   GlobalVariableMap &GetGlobalAranges();
438 
439   void UpdateExternalModuleListIfNeeded();
440 
441   virtual DIEToTypePtr &GetDIEToType() { return m_die_to_type; }
442 
443   virtual DIEToVariableSP &GetDIEToVariable() { return m_die_to_variable_sp; }
444 
445   virtual DIEToClangType &GetForwardDeclDieToClangType() {
446     return m_forward_decl_die_to_clang_type;
447   }
448 
449   virtual ClangTypeToDIE &GetForwardDeclClangTypeToDie() {
450     return m_forward_decl_clang_type_to_die;
451   }
452 
453   SymbolFileDWARFDwp *GetDwpSymbolFile();
454 
455   lldb::ModuleWP m_debug_map_module_wp;
456   SymbolFileDWARFDebugMap *m_debug_map_symfile;
457 
458   llvm::once_flag m_dwp_symfile_once_flag;
459   std::unique_ptr<SymbolFileDWARFDwp> m_dwp_symfile;
460 
461   lldb_private::DWARFContext m_context;
462 
463   DWARFDataSegment m_data_debug_abbrev;
464   DWARFDataSegment m_data_debug_addr;
465   DWARFDataSegment m_data_debug_frame;
466   DWARFDataSegment m_data_debug_info;
467   DWARFDataSegment m_data_debug_line;
468   DWARFDataSegment m_data_debug_line_str;
469   DWARFDataSegment m_data_debug_macro;
470   DWARFDataSegment m_data_debug_loc;
471   DWARFDataSegment m_data_debug_loclists;
472   DWARFDataSegment m_data_debug_ranges;
473   DWARFDataSegment m_data_debug_rnglists;
474   DWARFDataSegment m_data_debug_str;
475   DWARFDataSegment m_data_debug_str_offsets;
476   DWARFDataSegment m_data_debug_types;
477   DWARFDataSegment m_data_apple_names;
478   DWARFDataSegment m_data_apple_types;
479   DWARFDataSegment m_data_apple_namespaces;
480   DWARFDataSegment m_data_apple_objc;
481   DWARFDataSegment m_data_gnu_debugaltlink;
482 
483   // The unique pointer items below are generated on demand if and when someone
484   // accesses
485   // them through a non const version of this class.
486   std::unique_ptr<DWARFDebugAbbrev> m_abbr;
487   std::unique_ptr<DWARFDebugInfo> m_info;
488   std::unique_ptr<DWARFDebugLine> m_line;
489   std::unique_ptr<GlobalVariableMap> m_global_aranges_up;
490 
491   typedef std::unordered_map<lldb::offset_t, lldb_private::DebugMacrosSP>
492       DebugMacrosMap;
493   DebugMacrosMap m_debug_macros_map;
494 
495   ExternalTypeModuleMap m_external_type_modules;
496   std::unique_ptr<lldb_private::DWARFIndex> m_index;
497   bool m_fetched_external_modules : 1;
498   lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type;
499 
500   typedef std::shared_ptr<std::set<DIERef>> DIERefSetSP;
501   typedef std::unordered_map<std::string, DIERefSetSP> NameToOffsetMap;
502   NameToOffsetMap m_function_scope_qualified_name_map;
503   std::unique_ptr<DWARFDebugRangesBase> m_ranges;
504   UniqueDWARFASTTypeMap m_unique_ast_type_map;
505   DIEToTypePtr m_die_to_type;
506   DIEToVariableSP m_die_to_variable_sp;
507   DIEToClangType m_forward_decl_die_to_clang_type;
508   ClangTypeToDIE m_forward_decl_clang_type_to_die;
509 };
510 
511 #endif // SymbolFileDWARF_SymbolFileDWARF_h_
512