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/Symbol/DebugMacros.h"
25 #include "lldb/Symbol/SymbolContext.h"
26 #include "lldb/Symbol/SymbolFile.h"
27 #include "lldb/Utility/ConstString.h"
28 #include "lldb/Utility/Flags.h"
29 #include "lldb/Utility/RangeMap.h"
30 #include "lldb/lldb-private.h"
31 
32 #include "DWARFContext.h"
33 #include "DWARFDataExtractor.h"
34 #include "DWARFDefines.h"
35 #include "DWARFIndex.h"
36 #include "UniqueDWARFASTType.h"
37 
38 // Forward Declarations for this DWARF plugin
39 class DebugMapModule;
40 class DWARFAbbreviationDeclaration;
41 class DWARFAbbreviationDeclarationSet;
42 class DWARFCompileUnit;
43 class DWARFDebugAbbrev;
44 class DWARFDebugAranges;
45 class DWARFDebugInfo;
46 class DWARFDebugInfoEntry;
47 class DWARFDebugLine;
48 class DWARFDebugRanges;
49 class DWARFDeclContext;
50 class DWARFFormValue;
51 class DWARFTypeUnit;
52 class SymbolFileDWARFDebugMap;
53 class SymbolFileDWARFDwo;
54 class SymbolFileDWARFDwp;
55 
56 #define DIE_IS_BEING_PARSED ((lldb_private::Type *)1)
57 
58 class SymbolFileDWARF : public lldb_private::SymbolFile,
59                         public lldb_private::UserID {
60   /// LLVM RTTI support.
61   static char ID;
62 
63 public:
64   /// LLVM RTTI support.
65   /// \{
66   bool isA(const void *ClassID) const override {
67     return ClassID == &ID || SymbolFile::isA(ClassID);
68   }
69   static bool classof(const SymbolFile *obj) { return obj->isA(&ID); }
70   /// \}
71 
72   friend class SymbolFileDWARFDebugMap;
73   friend class SymbolFileDWARFDwo;
74   friend class DebugMapModule;
75   friend class DWARFCompileUnit;
76   friend class DWARFDIE;
77   friend class DWARFASTParserClang;
78 
79   // Static Functions
80   static void Initialize();
81 
82   static void Terminate();
83 
84   static void DebuggerInitialize(lldb_private::Debugger &debugger);
85 
86   static lldb_private::ConstString GetPluginNameStatic();
87 
88   static const char *GetPluginDescriptionStatic();
89 
90   static lldb_private::SymbolFile *
91   CreateInstance(lldb::ObjectFileSP objfile_sp);
92 
93   // Constructors and Destructors
94 
95   SymbolFileDWARF(lldb::ObjectFileSP objfile_sp,
96                   lldb_private::SectionList *dwo_section_list);
97 
98   ~SymbolFileDWARF() override;
99 
100   uint32_t CalculateAbilities() override;
101 
102   void InitializeObject() override;
103 
104   // Compile Unit function calls
105 
106   lldb::LanguageType
107   ParseLanguage(lldb_private::CompileUnit &comp_unit) override;
108 
109   size_t ParseFunctions(lldb_private::CompileUnit &comp_unit) override;
110 
111   bool ParseLineTable(lldb_private::CompileUnit &comp_unit) override;
112 
113   bool ParseDebugMacros(lldb_private::CompileUnit &comp_unit) override;
114 
115   bool ForEachExternalModule(
116       lldb_private::CompileUnit &, llvm::DenseSet<lldb_private::SymbolFile *> &,
117       llvm::function_ref<bool(lldb_private::Module &)>) override;
118 
119   bool ParseSupportFiles(lldb_private::CompileUnit &comp_unit,
120                          lldb_private::FileSpecList &support_files) override;
121 
122   bool ParseIsOptimized(lldb_private::CompileUnit &comp_unit) override;
123 
124   size_t ParseTypes(lldb_private::CompileUnit &comp_unit) override;
125 
126   bool ParseImportedModules(
127       const lldb_private::SymbolContext &sc,
128       std::vector<lldb_private::SourceModule> &imported_modules) override;
129 
130   size_t ParseBlocksRecursive(lldb_private::Function &func) override;
131 
132   size_t
133   ParseVariablesForContext(const lldb_private::SymbolContext &sc) override;
134 
135   lldb_private::Type *ResolveTypeUID(lldb::user_id_t type_uid) override;
136   llvm::Optional<ArrayInfo> GetDynamicArrayInfoForUID(
137       lldb::user_id_t type_uid,
138       const lldb_private::ExecutionContext *exe_ctx) override;
139 
140   bool CompleteType(lldb_private::CompilerType &compiler_type) override;
141 
142   lldb_private::Type *ResolveType(const DWARFDIE &die,
143                                   bool assert_not_being_parsed = true,
144                                   bool resolve_function_context = false);
145 
146   lldb_private::CompilerDecl GetDeclForUID(lldb::user_id_t uid) override;
147 
148   lldb_private::CompilerDeclContext
149   GetDeclContextForUID(lldb::user_id_t uid) override;
150 
151   lldb_private::CompilerDeclContext
152   GetDeclContextContainingUID(lldb::user_id_t uid) override;
153 
154   void
155   ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) override;
156 
157   uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr,
158                                 lldb::SymbolContextItem resolve_scope,
159                                 lldb_private::SymbolContext &sc) override;
160 
161   uint32_t
162   ResolveSymbolContext(const lldb_private::FileSpec &file_spec, uint32_t line,
163                        bool check_inlines,
164                        lldb::SymbolContextItem resolve_scope,
165                        lldb_private::SymbolContextList &sc_list) override;
166 
167   void
168   FindGlobalVariables(lldb_private::ConstString name,
169                       const lldb_private::CompilerDeclContext *parent_decl_ctx,
170                       uint32_t max_matches,
171                       lldb_private::VariableList &variables) override;
172 
173   void FindGlobalVariables(const lldb_private::RegularExpression &regex,
174                            uint32_t max_matches,
175                            lldb_private::VariableList &variables) override;
176 
177   void FindFunctions(lldb_private::ConstString name,
178                      const lldb_private::CompilerDeclContext *parent_decl_ctx,
179                      lldb::FunctionNameType name_type_mask,
180                      bool include_inlines,
181                      lldb_private::SymbolContextList &sc_list) override;
182 
183   void FindFunctions(const lldb_private::RegularExpression &regex,
184                      bool include_inlines,
185                      lldb_private::SymbolContextList &sc_list) override;
186 
187   void GetMangledNamesForFunction(
188       const std::string &scope_qualified_name,
189       std::vector<lldb_private::ConstString> &mangled_names) override;
190 
191   void
192   FindTypes(lldb_private::ConstString name,
193             const lldb_private::CompilerDeclContext *parent_decl_ctx,
194             uint32_t max_matches,
195             llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
196             lldb_private::TypeMap &types) override;
197 
198   void FindTypes(llvm::ArrayRef<lldb_private::CompilerContext> pattern,
199                  lldb_private::LanguageSet languages,
200                  llvm::DenseSet<SymbolFile *> &searched_symbol_files,
201                  lldb_private::TypeMap &types) override;
202 
203   void GetTypes(lldb_private::SymbolContextScope *sc_scope,
204                 lldb::TypeClass type_mask,
205                 lldb_private::TypeList &type_list) override;
206 
207   llvm::Expected<lldb_private::TypeSystem &>
208   GetTypeSystemForLanguage(lldb::LanguageType language) override;
209 
210   lldb_private::CompilerDeclContext FindNamespace(
211       lldb_private::ConstString name,
212       const lldb_private::CompilerDeclContext *parent_decl_ctx) override;
213 
214   void PreloadSymbols() override;
215 
216   std::recursive_mutex &GetModuleMutex() const override;
217 
218   // PluginInterface protocol
219   lldb_private::ConstString GetPluginName() override;
220 
221   uint32_t GetPluginVersion() override;
222 
223   DWARFDebugAbbrev *DebugAbbrev();
224 
225   DWARFDebugInfo *DebugInfo();
226 
227   DWARFDebugRanges *GetDebugRanges();
228 
229   static bool SupportedVersion(uint16_t version);
230 
231   DWARFDIE
232   GetDeclContextDIEContainingDIE(const DWARFDIE &die);
233 
234   bool
235   HasForwardDeclForClangType(const lldb_private::CompilerType &compiler_type);
236 
237   lldb_private::CompileUnit *
238   GetCompUnitForDWARFCompUnit(DWARFCompileUnit &dwarf_cu);
239 
240   virtual size_t GetObjCMethodDIEOffsets(lldb_private::ConstString class_name,
241                                          DIEArray &method_die_offsets);
242 
243   bool Supports_DW_AT_APPLE_objc_complete_type(DWARFUnit *cu);
244 
245   lldb_private::DebugMacrosSP ParseDebugMacros(lldb::offset_t *offset);
246 
247   static DWARFDIE GetParentSymbolContextDIE(const DWARFDIE &die);
248 
249   virtual lldb::CompUnitSP ParseCompileUnit(DWARFCompileUnit &dwarf_cu);
250 
251   lldb::ModuleSP GetExternalModule(lldb_private::ConstString name);
252 
253   typedef std::map<lldb_private::ConstString, lldb::ModuleSP>
254       ExternalTypeModuleMap;
255 
256   /// Return the list of Clang modules imported by this SymbolFile.
257   const ExternalTypeModuleMap& getExternalTypeModules() const {
258       return m_external_type_modules;
259   }
260 
261   virtual DWARFDIE GetDIE(const DIERef &die_ref);
262 
263   DWARFDIE GetDIE(lldb::user_id_t uid);
264 
265   lldb::user_id_t GetUID(const DWARFBaseDIE &die) {
266     return GetUID(die.GetDIERef());
267   }
268 
269   lldb::user_id_t GetUID(const llvm::Optional<DIERef> &ref) {
270     return ref ? GetUID(*ref) : LLDB_INVALID_UID;
271   }
272 
273   lldb::user_id_t GetUID(DIERef ref);
274 
275   std::unique_ptr<SymbolFileDWARFDwo>
276   GetDwoSymbolFileForCompileUnit(DWARFUnit &dwarf_cu,
277                                  const DWARFDebugInfoEntry &cu_die);
278 
279   // For regular SymbolFileDWARF instances the method returns nullptr,
280   // for the instances of the subclass SymbolFileDWARFDwo
281   // the method returns a pointer to the base compile unit.
282   virtual DWARFCompileUnit *GetBaseCompileUnit() { return nullptr; }
283 
284   virtual llvm::Optional<uint32_t> GetDwoNum() { return llvm::None; }
285 
286   /// If this is a DWARF object with a single CU, return its DW_AT_dwo_id.
287   llvm::Optional<uint64_t> GetDWOId();
288 
289   static bool
290   DIEInDeclContext(const lldb_private::CompilerDeclContext *parent_decl_ctx,
291                    const DWARFDIE &die);
292 
293   std::vector<std::unique_ptr<lldb_private::CallEdge>>
294   ParseCallEdgesInFunction(UserID func_id) override;
295 
296   void Dump(lldb_private::Stream &s) override;
297 
298   void DumpClangAST(lldb_private::Stream &s) override;
299 
300   lldb_private::DWARFContext &GetDWARFContext() { return m_context; }
301 
302   lldb_private::FileSpec GetFile(DWARFUnit &unit, size_t file_idx);
303 
304   static llvm::Expected<lldb_private::TypeSystem &>
305   GetTypeSystem(DWARFUnit &unit);
306 
307   static DWARFASTParser *GetDWARFParser(DWARFUnit &unit);
308 
309   // CompilerDecl related functions
310 
311   static lldb_private::CompilerDecl GetDecl(const DWARFDIE &die);
312 
313   static lldb_private::CompilerDeclContext GetDeclContext(const DWARFDIE &die);
314 
315   static lldb_private::CompilerDeclContext
316   GetContainingDeclContext(const DWARFDIE &die);
317 
318   static void GetDWARFDeclContext(const DWARFDIE &die,
319                                   DWARFDeclContext &dwarf_decl_ctx);
320 
321   static lldb::LanguageType LanguageTypeFromDWARF(uint64_t val);
322 
323   static lldb::LanguageType GetLanguage(DWARFUnit &unit);
324 
325 protected:
326   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb_private::Type *>
327       DIEToTypePtr;
328   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb::VariableSP>
329       DIEToVariableSP;
330   typedef llvm::DenseMap<const DWARFDebugInfoEntry *,
331                          lldb::opaque_compiler_type_t>
332       DIEToClangType;
333   typedef llvm::DenseMap<lldb::opaque_compiler_type_t, lldb::user_id_t>
334       ClangTypeToDIE;
335 
336   DISALLOW_COPY_AND_ASSIGN(SymbolFileDWARF);
337 
338   virtual void LoadSectionData(lldb::SectionType sect_type,
339                                lldb_private::DWARFDataExtractor &data);
340 
341   bool DeclContextMatchesThisSymbolFile(
342       const lldb_private::CompilerDeclContext *decl_ctx);
343 
344   uint32_t CalculateNumCompileUnits() override;
345 
346   lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override;
347 
348   lldb_private::TypeList &GetTypeList() override;
349 
350   virtual DWARFUnit *
351   GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit);
352 
353   DWARFUnit *GetNextUnparsedDWARFCompileUnit(DWARFUnit *prev_cu);
354 
355   bool GetFunction(const DWARFDIE &die, lldb_private::SymbolContext &sc);
356 
357   lldb_private::Function *ParseFunction(lldb_private::CompileUnit &comp_unit,
358                                         const DWARFDIE &die);
359 
360   size_t ParseBlocksRecursive(lldb_private::CompileUnit &comp_unit,
361                               lldb_private::Block *parent_block,
362                               const DWARFDIE &die,
363                               lldb::addr_t subprogram_low_pc, uint32_t depth);
364 
365   size_t ParseTypes(const lldb_private::SymbolContext &sc, const DWARFDIE &die,
366                     bool parse_siblings, bool parse_children);
367 
368   lldb::TypeSP ParseType(const lldb_private::SymbolContext &sc,
369                          const DWARFDIE &die, bool *type_is_new);
370 
371   lldb_private::Type *ResolveTypeUID(const DWARFDIE &die,
372                                      bool assert_not_being_parsed);
373 
374   lldb_private::Type *ResolveTypeUID(const DIERef &die_ref);
375 
376   lldb::VariableSP ParseVariableDIE(const lldb_private::SymbolContext &sc,
377                                     const DWARFDIE &die,
378                                     const lldb::addr_t func_low_pc);
379 
380   size_t ParseVariables(const lldb_private::SymbolContext &sc,
381                         const DWARFDIE &orig_die,
382                         const lldb::addr_t func_low_pc, bool parse_siblings,
383                         bool parse_children,
384                         lldb_private::VariableList *cc_variable_list = nullptr);
385 
386   bool ClassOrStructIsVirtual(const DWARFDIE &die);
387 
388   // Given a die_offset, figure out the symbol context representing that die.
389   bool ResolveFunction(const DWARFDIE &die, bool include_inlines,
390                        lldb_private::SymbolContextList &sc_list);
391 
392   /// Resolve functions and (possibly) blocks for the given file address and a
393   /// compile unit. The compile unit comes from the sc argument and it must be
394   /// set. The results of the lookup (if any) are written back to the symbol
395   /// context.
396   void ResolveFunctionAndBlock(lldb::addr_t file_vm_addr, bool lookup_block,
397                                lldb_private::SymbolContext &sc);
398 
399   virtual lldb::TypeSP
400   FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext &die_decl_ctx);
401 
402   virtual lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE(
403       const DWARFDIE &die, lldb_private::ConstString type_name,
404       bool must_be_implementation);
405 
406   lldb_private::Symbol *
407   GetObjCClassSymbol(lldb_private::ConstString objc_class_name);
408 
409   lldb::TypeSP GetTypeForDIE(const DWARFDIE &die,
410                              bool resolve_function_context = false);
411 
412   void SetDebugMapModule(const lldb::ModuleSP &module_sp) {
413     m_debug_map_module_wp = module_sp;
414   }
415 
416   SymbolFileDWARFDebugMap *GetDebugMapSymfile();
417 
418   DWARFDIE
419   FindBlockContainingSpecification(const DIERef &func_die_ref,
420                                    dw_offset_t spec_block_die_offset);
421 
422   DWARFDIE
423   FindBlockContainingSpecification(const DWARFDIE &die,
424                                    dw_offset_t spec_block_die_offset);
425 
426   virtual UniqueDWARFASTTypeMap &GetUniqueDWARFASTTypeMap();
427 
428   bool DIEDeclContextsMatch(const DWARFDIE &die1, const DWARFDIE &die2);
429 
430   bool ClassContainsSelector(const DWARFDIE &class_die,
431                              lldb_private::ConstString selector);
432 
433   /// Parse call site entries (DW_TAG_call_site), including any nested call site
434   /// parameters (DW_TAG_call_site_parameter).
435   std::vector<std::unique_ptr<lldb_private::CallEdge>>
436   CollectCallEdges(lldb::ModuleSP module, DWARFDIE function_die);
437 
438   /// If this symbol file is linked to by a debug map (see
439   /// SymbolFileDWARFDebugMap), and \p file_addr is a file address relative to
440   /// an object file, adjust \p file_addr so that it is relative to the main
441   /// binary. Returns the adjusted address, or \p file_addr if no adjustment is
442   /// needed, on success and LLDB_INVALID_ADDRESS otherwise.
443   lldb::addr_t FixupAddress(lldb::addr_t file_addr);
444 
445   bool FixupAddress(lldb_private::Address &addr);
446 
447   typedef std::set<lldb_private::Type *> TypeSet;
448 
449   void GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
450                 dw_offset_t max_die_offset, uint32_t type_mask,
451                 TypeSet &type_set);
452 
453   typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t,
454                                         lldb_private::Variable *>
455       GlobalVariableMap;
456 
457   GlobalVariableMap &GetGlobalAranges();
458 
459   void UpdateExternalModuleListIfNeeded();
460 
461   virtual DIEToTypePtr &GetDIEToType() { return m_die_to_type; }
462 
463   virtual DIEToVariableSP &GetDIEToVariable() { return m_die_to_variable_sp; }
464 
465   virtual DIEToClangType &GetForwardDeclDieToClangType() {
466     return m_forward_decl_die_to_clang_type;
467   }
468 
469   virtual ClangTypeToDIE &GetForwardDeclClangTypeToDie() {
470     return m_forward_decl_clang_type_to_die;
471   }
472 
473   void BuildCuTranslationTable();
474   llvm::Optional<uint32_t> GetDWARFUnitIndex(uint32_t cu_idx);
475 
476   struct DecodedUID {
477     SymbolFileDWARF &dwarf;
478     DIERef ref;
479   };
480   llvm::Optional<DecodedUID> DecodeUID(lldb::user_id_t uid);
481 
482   SymbolFileDWARFDwp *GetDwpSymbolFile();
483 
484   const lldb_private::FileSpecList &GetTypeUnitSupportFiles(DWARFTypeUnit &tu);
485 
486   lldb::ModuleWP m_debug_map_module_wp;
487   SymbolFileDWARFDebugMap *m_debug_map_symfile;
488 
489   llvm::once_flag m_dwp_symfile_once_flag;
490   std::unique_ptr<SymbolFileDWARFDwp> m_dwp_symfile;
491 
492   lldb_private::DWARFContext m_context;
493 
494   std::unique_ptr<DWARFDebugAbbrev> m_abbr;
495   std::unique_ptr<DWARFDebugInfo> m_info;
496   std::unique_ptr<GlobalVariableMap> m_global_aranges_up;
497 
498   typedef std::unordered_map<lldb::offset_t, lldb_private::DebugMacrosSP>
499       DebugMacrosMap;
500   DebugMacrosMap m_debug_macros_map;
501 
502   ExternalTypeModuleMap m_external_type_modules;
503   std::unique_ptr<lldb_private::DWARFIndex> m_index;
504   bool m_fetched_external_modules : 1;
505   lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type;
506 
507   typedef std::set<lldb::user_id_t> DIERefSet;
508   typedef llvm::StringMap<DIERefSet> NameToOffsetMap;
509   NameToOffsetMap m_function_scope_qualified_name_map;
510   std::unique_ptr<DWARFDebugRanges> m_ranges;
511   UniqueDWARFASTTypeMap m_unique_ast_type_map;
512   DIEToTypePtr m_die_to_type;
513   DIEToVariableSP m_die_to_variable_sp;
514   DIEToClangType m_forward_decl_die_to_clang_type;
515   ClangTypeToDIE m_forward_decl_clang_type_to_die;
516   llvm::DenseMap<dw_offset_t, lldb_private::FileSpecList>
517       m_type_unit_support_files;
518   std::vector<uint32_t> m_lldb_cu_to_dwarf_unit;
519 };
520 
521 #endif // SymbolFileDWARF_SymbolFileDWARF_h_
522