1 //===-- SymbolFileDWARF.h --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef SymbolFileDWARF_SymbolFileDWARF_h_
11 #define SymbolFileDWARF_SymbolFileDWARF_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <list>
16 #include <map>
17 #include <mutex>
18 #include <set>
19 #include <unordered_map>
20 #include <vector>
21 
22 // Other libraries and framework includes
23 #include "llvm/ADT/DenseMap.h"
24 
25 #include "lldb/lldb-private.h"
26 #include "lldb/Core/ConstString.h"
27 #include "lldb/Core/dwarf.h"
28 #include "lldb/Core/Flags.h"
29 #include "lldb/Core/RangeMap.h"
30 #include "lldb/Core/UniqueCStringMap.h"
31 #include "lldb/Expression/DWARFExpression.h"
32 #include "lldb/Symbol/DebugMacros.h"
33 #include "lldb/Symbol/SymbolFile.h"
34 #include "lldb/Symbol/SymbolContext.h"
35 
36 // Project includes
37 #include "DWARFDefines.h"
38 #include "DWARFDataExtractor.h"
39 #include "HashedNameToDIE.h"
40 #include "NameToDIE.h"
41 #include "UniqueDWARFASTType.h"
42 
43 //----------------------------------------------------------------------
44 // Forward Declarations for this DWARF plugin
45 //----------------------------------------------------------------------
46 class DebugMapModule;
47 class DWARFAbbreviationDeclaration;
48 class DWARFAbbreviationDeclarationSet;
49 class DWARFileUnit;
50 class DWARFDebugAbbrev;
51 class DWARFDebugAranges;
52 class DWARFDebugInfo;
53 class DWARFDebugInfoEntry;
54 class DWARFDebugLine;
55 class DWARFDebugPubnames;
56 class DWARFDebugRanges;
57 class DWARFDeclContext;
58 class DWARFDIECollection;
59 class DWARFFormValue;
60 class SymbolFileDWARFDebugMap;
61 class SymbolFileDWARFDwo;
62 
63 #define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
64 
65 class SymbolFileDWARF : public lldb_private::SymbolFile, public lldb_private::UserID
66 {
67 public:
68     friend class SymbolFileDWARFDebugMap;
69     friend class SymbolFileDWARFDwo;
70     friend class DebugMapModule;
71     friend struct DIERef;
72     friend class DWARFCompileUnit;
73     friend class DWARFDIE;
74     friend class DWARFASTParserClang;
75     friend class DWARFASTParserGo;
76     friend class DWARFASTParserJava;
77 
78     //------------------------------------------------------------------
79     // Static Functions
80     //------------------------------------------------------------------
81     static void
82     Initialize();
83 
84     static void
85     Terminate();
86 
87     static void
88     DebuggerInitialize(lldb_private::Debugger &debugger);
89 
90     static lldb_private::ConstString
91     GetPluginNameStatic();
92 
93     static const char *
94     GetPluginDescriptionStatic();
95 
96     static lldb_private::SymbolFile*
97     CreateInstance (lldb_private::ObjectFile* obj_file);
98 
99     //------------------------------------------------------------------
100     // Constructors and Destructors
101     //------------------------------------------------------------------
102 
103     SymbolFileDWARF(lldb_private::ObjectFile* ofile);
104 
105     ~SymbolFileDWARF() override;
106 
107     uint32_t
108     CalculateAbilities () override;
109 
110     void
111     InitializeObject() override;
112 
113     //------------------------------------------------------------------
114     // Compile Unit function calls
115     //------------------------------------------------------------------
116 
117     uint32_t
118     GetNumCompileUnits() override;
119 
120     lldb::CompUnitSP
121     ParseCompileUnitAtIndex(uint32_t index) override;
122 
123     lldb::LanguageType
124     ParseCompileUnitLanguage (const lldb_private::SymbolContext& sc) override;
125 
126     size_t
127     ParseCompileUnitFunctions (const lldb_private::SymbolContext& sc) override;
128 
129     bool
130     ParseCompileUnitLineTable (const lldb_private::SymbolContext& sc) override;
131 
132     bool
133     ParseCompileUnitDebugMacros (const lldb_private::SymbolContext& sc) override;
134 
135     bool
136     ParseCompileUnitSupportFiles (const lldb_private::SymbolContext& sc,
137                                   lldb_private::FileSpecList& support_files) override;
138 
139     bool
140     ParseCompileUnitIsOptimized(const lldb_private::SymbolContext &sc) override;
141 
142     bool
143     ParseImportedModules(const lldb_private::SymbolContext &sc,
144                          std::vector<lldb_private::ConstString> &imported_modules) override;
145 
146     size_t
147     ParseFunctionBlocks (const lldb_private::SymbolContext& sc) override;
148 
149     size_t
150     ParseTypes (const lldb_private::SymbolContext& sc) override;
151 
152     size_t
153     ParseVariablesForContext (const lldb_private::SymbolContext& sc) override;
154 
155     lldb_private::Type *
156     ResolveTypeUID(lldb::user_id_t type_uid) override;
157 
158     bool
159     CompleteType (lldb_private::CompilerType& compiler_type) override;
160 
161     lldb_private::Type *
162     ResolveType (const DWARFDIE &die,
163                  bool assert_not_being_parsed = true,
164                  bool resolve_function_context = false);
165 
166     SymbolFileDWARF *
167     GetDWARFForUID (lldb::user_id_t uid);
168 
169     DWARFDIE
170     GetDIEFromUID (lldb::user_id_t uid);
171 
172     lldb_private::CompilerDecl
173     GetDeclForUID (lldb::user_id_t uid) override;
174 
175     lldb_private::CompilerDeclContext
176     GetDeclContextForUID (lldb::user_id_t uid) override;
177 
178     lldb_private::CompilerDeclContext
179     GetDeclContextContainingUID (lldb::user_id_t uid) override;
180 
181     void
182     ParseDeclsForContext (lldb_private::CompilerDeclContext decl_ctx) override;
183 
184 
185     uint32_t
186     ResolveSymbolContext (const lldb_private::Address& so_addr,
187                           uint32_t resolve_scope,
188                           lldb_private::SymbolContext& sc) override;
189 
190     uint32_t
191     ResolveSymbolContext (const lldb_private::FileSpec& file_spec,
192                           uint32_t line,
193                           bool check_inlines,
194                           uint32_t resolve_scope,
195                           lldb_private::SymbolContextList& sc_list) override;
196 
197     uint32_t
198     FindGlobalVariables (const lldb_private::ConstString &name,
199                          const lldb_private::CompilerDeclContext *parent_decl_ctx,
200                          bool append,
201                          uint32_t max_matches,
202                          lldb_private::VariableList& variables) override;
203 
204     uint32_t
205     FindGlobalVariables (const lldb_private::RegularExpression& regex,
206                          bool append,
207                          uint32_t max_matches,
208                          lldb_private::VariableList& variables) override;
209 
210     uint32_t
211     FindFunctions (const lldb_private::ConstString &name,
212                    const lldb_private::CompilerDeclContext *parent_decl_ctx,
213                    uint32_t name_type_mask,
214                    bool include_inlines,
215                    bool append,
216                    lldb_private::SymbolContextList& sc_list) override;
217 
218     uint32_t
219     FindFunctions (const lldb_private::RegularExpression& regex,
220                    bool include_inlines,
221                    bool append,
222                    lldb_private::SymbolContextList& sc_list) override;
223 
224     void
225     GetMangledNamesForFunction (const std::string &scope_qualified_name,
226                                 std::vector<lldb_private::ConstString> &mangled_names) override;
227 
228     uint32_t
229     FindTypes (const lldb_private::SymbolContext& sc,
230                const lldb_private::ConstString &name,
231                const lldb_private::CompilerDeclContext *parent_decl_ctx,
232                bool append,
233                uint32_t max_matches,
234                llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
235                lldb_private::TypeMap& types) override;
236 
237     size_t
238     FindTypes (const std::vector<lldb_private::CompilerContext> &context,
239                bool append,
240                lldb_private::TypeMap& types) override;
241 
242     lldb_private::TypeList *
243     GetTypeList () override;
244 
245     size_t
246     GetTypes (lldb_private::SymbolContextScope *sc_scope,
247               uint32_t type_mask,
248               lldb_private::TypeList &type_list) override;
249 
250     lldb_private::TypeSystem *
251     GetTypeSystemForLanguage (lldb::LanguageType language) override;
252 
253     lldb_private::CompilerDeclContext
254     FindNamespace (const lldb_private::SymbolContext& sc,
255                    const lldb_private::ConstString &name,
256                    const lldb_private::CompilerDeclContext *parent_decl_ctx) override;
257 
258 
259     //------------------------------------------------------------------
260     // PluginInterface protocol
261     //------------------------------------------------------------------
262     lldb_private::ConstString
263     GetPluginName() override;
264 
265     uint32_t
266     GetPluginVersion() override;
267 
268     const lldb_private::DWARFDataExtractor&     get_debug_abbrev_data ();
269     const lldb_private::DWARFDataExtractor&     get_debug_addr_data ();
270     const lldb_private::DWARFDataExtractor&     get_debug_aranges_data ();
271     const lldb_private::DWARFDataExtractor&     get_debug_frame_data ();
272     const lldb_private::DWARFDataExtractor&     get_debug_info_data ();
273     const lldb_private::DWARFDataExtractor&     get_debug_line_data ();
274     const lldb_private::DWARFDataExtractor&     get_debug_macro_data ();
275     const lldb_private::DWARFDataExtractor&     get_debug_loc_data ();
276     const lldb_private::DWARFDataExtractor&     get_debug_ranges_data ();
277     const lldb_private::DWARFDataExtractor&     get_debug_str_data ();
278     const lldb_private::DWARFDataExtractor&     get_debug_str_offsets_data ();
279     const lldb_private::DWARFDataExtractor&     get_apple_names_data ();
280     const lldb_private::DWARFDataExtractor&     get_apple_types_data ();
281     const lldb_private::DWARFDataExtractor&     get_apple_namespaces_data ();
282     const lldb_private::DWARFDataExtractor&     get_apple_objc_data ();
283 
284 
285     DWARFDebugAbbrev*
286     DebugAbbrev();
287 
288     const DWARFDebugAbbrev*
289     DebugAbbrev() const;
290 
291     DWARFDebugInfo*
292     DebugInfo();
293 
294     const DWARFDebugInfo*
295     DebugInfo() const;
296 
297     DWARFDebugRanges*
298     DebugRanges();
299 
300     const DWARFDebugRanges*
301     DebugRanges() const;
302 
303     static bool
304     SupportedVersion(uint16_t version);
305 
306     DWARFDIE
307     GetDeclContextDIEContainingDIE (const DWARFDIE &die);
308 
309     bool
310     HasForwardDeclForClangType (const lldb_private::CompilerType &compiler_type);
311 
312     lldb_private::CompileUnit*
313     GetCompUnitForDWARFCompUnit(DWARFCompileUnit* dwarf_cu,
314                                 uint32_t cu_idx = UINT32_MAX);
315 
316     size_t
317     GetObjCMethodDIEOffsets (lldb_private::ConstString class_name,
318                              DIEArray &method_die_offsets);
319 
320     bool
321     Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu);
322 
323     lldb_private::DebugMacrosSP
324     ParseDebugMacros(lldb::offset_t *offset);
325 
326     static DWARFDIE
327     GetParentSymbolContextDIE(const DWARFDIE &die);
328 
329     virtual lldb::CompUnitSP
330     ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx);
331 
332     virtual lldb_private::DWARFExpression::LocationListFormat
333     GetLocationListFormat() const;
334 
335     lldb::ModuleSP
336     GetDWOModule (lldb_private::ConstString name);
337 
338     virtual DWARFDIE
339     GetDIE(const DIERef &die_ref);
340 
341     virtual std::unique_ptr<SymbolFileDWARFDwo>
342     GetDwoSymbolFileForCompileUnit(DWARFCompileUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die);
343 
344 protected:
345     typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb_private::Type *> DIEToTypePtr;
346     typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb::VariableSP> DIEToVariableSP;
347     typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb::opaque_compiler_type_t> DIEToClangType;
348     typedef llvm::DenseMap<lldb::opaque_compiler_type_t, DIERef> ClangTypeToDIE;
349 
350     struct DWARFDataSegment
351     {
352         std::once_flag                   m_flag;
353         lldb_private::DWARFDataExtractor m_data;
354     };
355 
356     DISALLOW_COPY_AND_ASSIGN (SymbolFileDWARF);
357 
358     const lldb_private::DWARFDataExtractor&
359     GetCachedSectionData (lldb::SectionType sect_type, DWARFDataSegment& data_segment);
360 
361     virtual void
362     LoadSectionData (lldb::SectionType sect_type, lldb_private::DWARFDataExtractor& data);
363 
364     bool
365     DeclContextMatchesThisSymbolFile (const lldb_private::CompilerDeclContext *decl_ctx);
366 
367     bool
368     DIEInDeclContext (const lldb_private::CompilerDeclContext *parent_decl_ctx,
369                       const DWARFDIE &die);
370 
371     virtual DWARFCompileUnit*
372     GetDWARFCompileUnit (lldb_private::CompileUnit *comp_unit);
373 
374     DWARFCompileUnit*
375     GetNextUnparsedDWARFCompileUnit (DWARFCompileUnit* prev_cu);
376 
377     bool
378     GetFunction (const DWARFDIE &die,
379                  lldb_private::SymbolContext& sc);
380 
381     lldb_private::Function *
382     ParseCompileUnitFunction (const lldb_private::SymbolContext& sc,
383                               const DWARFDIE &die);
384 
385     size_t
386     ParseFunctionBlocks (const lldb_private::SymbolContext& sc,
387                          lldb_private::Block *parent_block,
388                          const DWARFDIE &die,
389                          lldb::addr_t subprogram_low_pc,
390                          uint32_t depth);
391 
392     size_t
393     ParseTypes (const lldb_private::SymbolContext& sc,
394                 const DWARFDIE &die,
395                 bool parse_siblings,
396                 bool parse_children);
397 
398     lldb::TypeSP
399     ParseType (const lldb_private::SymbolContext& sc,
400                const DWARFDIE &die,
401                bool *type_is_new);
402 
403     lldb_private::Type *
404     ResolveTypeUID(const DWARFDIE &die, bool assert_not_being_parsed);
405 
406     lldb_private::Type *
407     ResolveTypeUID(const DIERef &die_ref);
408 
409     lldb::VariableSP
410     ParseVariableDIE(const lldb_private::SymbolContext& sc,
411                      const DWARFDIE &die,
412                      const lldb::addr_t func_low_pc);
413 
414     size_t
415     ParseVariables (const lldb_private::SymbolContext& sc,
416                     const DWARFDIE &orig_die,
417                     const lldb::addr_t func_low_pc,
418                     bool parse_siblings,
419                     bool parse_children,
420                     lldb_private::VariableList* cc_variable_list = NULL);
421 
422     bool
423     ClassOrStructIsVirtual (const DWARFDIE &die);
424 
425     // Given a die_offset, figure out the symbol context representing that die.
426     bool
427     ResolveFunction (const DIERef& die_ref,
428                      bool include_inlines,
429                      lldb_private::SymbolContextList& sc_list);
430 
431     bool
432     ResolveFunction (const DWARFDIE &die,
433                      bool include_inlines,
434                      lldb_private::SymbolContextList& sc_list);
435 
436     void
437     FindFunctions(const lldb_private::ConstString &name,
438                   const NameToDIE &name_to_die,
439                   bool include_inlines,
440                   lldb_private::SymbolContextList& sc_list);
441 
442     void
443     FindFunctions (const lldb_private::RegularExpression &regex,
444                    const NameToDIE &name_to_die,
445                    bool include_inlines,
446                    lldb_private::SymbolContextList& sc_list);
447 
448     void
449     FindFunctions (const lldb_private::RegularExpression &regex,
450                    const DWARFMappedHash::MemoryTable &memory_table,
451                    bool include_inlines,
452                    lldb_private::SymbolContextList& sc_list);
453 
454     virtual lldb::TypeSP
455     FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &die_decl_ctx);
456 
457     lldb::TypeSP
458     FindCompleteObjCDefinitionTypeForDIE (const DWARFDIE &die,
459                                           const lldb_private::ConstString &type_name,
460                                           bool must_be_implementation);
461 
462     lldb::TypeSP
463     FindCompleteObjCDefinitionType (const lldb_private::ConstString &type_name,
464                                     bool header_definition_ok);
465 
466     lldb_private::Symbol *
467     GetObjCClassSymbol (const lldb_private::ConstString &objc_class_name);
468 
469     void
470     ParseFunctions (const DIEArray &die_offsets,
471                     bool include_inlines,
472                     lldb_private::SymbolContextList& sc_list);
473 
474     lldb::TypeSP
475     GetTypeForDIE (const DWARFDIE &die, bool resolve_function_context = false);
476 
477     void
478     Index();
479 
480     void
481     DumpIndexes();
482 
483     void
484     SetDebugMapModule (const lldb::ModuleSP &module_sp)
485     {
486         m_debug_map_module_wp = module_sp;
487     }
488 
489     SymbolFileDWARFDebugMap *
490     GetDebugMapSymfile ();
491 
492     DWARFDIE
493     FindBlockContainingSpecification (const DIERef& func_die_ref, dw_offset_t spec_block_die_offset);
494 
495     DWARFDIE
496     FindBlockContainingSpecification (const DWARFDIE &die, dw_offset_t spec_block_die_offset);
497 
498     virtual UniqueDWARFASTTypeMap &
499     GetUniqueDWARFASTTypeMap ();
500 
501     bool
502     DIEDeclContextsMatch (const DWARFDIE &die1,
503                           const DWARFDIE &die2);
504 
505     bool
506     ClassContainsSelector (const DWARFDIE &class_die,
507                            const lldb_private::ConstString &selector);
508 
509     bool
510     FixupAddress (lldb_private::Address &addr);
511 
512     typedef std::set<lldb_private::Type *> TypeSet;
513 
514     typedef std::map<lldb_private::ConstString, lldb::ModuleSP> ExternalTypeModuleMap;
515 
516     void
517     GetTypes (const DWARFDIE &die,
518               dw_offset_t min_die_offset,
519               dw_offset_t max_die_offset,
520               uint32_t type_mask,
521               TypeSet &type_set);
522 
523     typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, lldb_private::Variable *> GlobalVariableMap;
524 
525     GlobalVariableMap &
526     GetGlobalAranges();
527 
528     void
529     UpdateExternalModuleListIfNeeded();
530 
531     virtual DIEToTypePtr&
532     GetDIEToType() { return m_die_to_type; }
533 
534     virtual DIEToVariableSP&
535     GetDIEToVariable() { return m_die_to_variable_sp; }
536 
537     virtual DIEToClangType&
538     GetForwardDeclDieToClangType() { return m_forward_decl_die_to_clang_type; }
539 
540     virtual ClangTypeToDIE&
541     GetForwardDeclClangTypeToDie() { return m_forward_decl_clang_type_to_die; }
542 
543     lldb::ModuleWP                        m_debug_map_module_wp;
544     SymbolFileDWARFDebugMap *             m_debug_map_symfile;
545     lldb_private::DWARFDataExtractor      m_dwarf_data;
546 
547     DWARFDataSegment                      m_data_debug_abbrev;
548     DWARFDataSegment                      m_data_debug_addr;
549     DWARFDataSegment                      m_data_debug_aranges;
550     DWARFDataSegment                      m_data_debug_frame;
551     DWARFDataSegment                      m_data_debug_info;
552     DWARFDataSegment                      m_data_debug_line;
553     DWARFDataSegment                      m_data_debug_macro;
554     DWARFDataSegment                      m_data_debug_loc;
555     DWARFDataSegment                      m_data_debug_ranges;
556     DWARFDataSegment                      m_data_debug_str;
557     DWARFDataSegment                      m_data_debug_str_offsets;
558     DWARFDataSegment                      m_data_apple_names;
559     DWARFDataSegment                      m_data_apple_types;
560     DWARFDataSegment                      m_data_apple_namespaces;
561     DWARFDataSegment                      m_data_apple_objc;
562 
563     // The unique pointer items below are generated on demand if and when someone accesses
564     // them through a non const version of this class.
565     std::unique_ptr<DWARFDebugAbbrev>     m_abbr;
566     std::unique_ptr<DWARFDebugInfo>       m_info;
567     std::unique_ptr<DWARFDebugLine>       m_line;
568     std::unique_ptr<DWARFMappedHash::MemoryTable> m_apple_names_ap;
569     std::unique_ptr<DWARFMappedHash::MemoryTable> m_apple_types_ap;
570     std::unique_ptr<DWARFMappedHash::MemoryTable> m_apple_namespaces_ap;
571     std::unique_ptr<DWARFMappedHash::MemoryTable> m_apple_objc_ap;
572     std::unique_ptr<GlobalVariableMap>  m_global_aranges_ap;
573 
574     typedef std::unordered_map<lldb::offset_t, lldb_private::DebugMacrosSP> DebugMacrosMap;
575     DebugMacrosMap m_debug_macros_map;
576 
577     ExternalTypeModuleMap               m_external_type_modules;
578     NameToDIE                           m_function_basename_index;  // All concrete functions
579     NameToDIE                           m_function_fullname_index;  // All concrete functions
580     NameToDIE                           m_function_method_index;    // All inlined functions
581     NameToDIE                           m_function_selector_index;  // All method names for functions of classes
582     NameToDIE                           m_objc_class_selectors_index; // Given a class name, find all selectors for the class
583     NameToDIE                           m_global_index;             // Global and static variables
584     NameToDIE                           m_type_index;               // All type DIE offsets
585     NameToDIE                           m_namespace_index;          // All type DIE offsets
586     bool                                m_indexed:1,
587                                         m_using_apple_tables:1,
588                                         m_fetched_external_modules:1;
589     lldb_private::LazyBool              m_supports_DW_AT_APPLE_objc_complete_type;
590 
591     typedef std::shared_ptr<std::set<DIERef> > DIERefSetSP;
592     typedef std::unordered_map<std::string, DIERefSetSP> NameToOffsetMap;
593     NameToOffsetMap m_function_scope_qualified_name_map;
594     std::unique_ptr<DWARFDebugRanges>     m_ranges;
595     UniqueDWARFASTTypeMap m_unique_ast_type_map;
596     DIEToTypePtr m_die_to_type;
597     DIEToVariableSP m_die_to_variable_sp;
598     DIEToClangType m_forward_decl_die_to_clang_type;
599     ClangTypeToDIE m_forward_decl_clang_type_to_die;
600 };
601 
602 #endif  // SymbolFileDWARF_SymbolFileDWARF_h_
603