1 //===-- SymbolFileDWARFDebugMap.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_SymbolFileDWARFDebugMap_h_
11 #define SymbolFileDWARF_SymbolFileDWARFDebugMap_h_
12 
13 
14 #include <vector>
15 #include <bitset>
16 
17 #include "lldb/Core/RangeMap.h"
18 #include "lldb/Symbol/SymbolFile.h"
19 
20 #include "UniqueDWARFASTType.h"
21 
22 class SymbolFileDWARF;
23 class DWARFDebugAranges;
24 class DWARFDeclContext;
25 
26 class SymbolFileDWARFDebugMap : public lldb_private::SymbolFile
27 {
28 public:
29 
30     //------------------------------------------------------------------
31     // Static Functions
32     //------------------------------------------------------------------
33     static void
34     Initialize();
35 
36     static void
37     Terminate();
38 
39     static lldb_private::ConstString
40     GetPluginNameStatic();
41 
42     static const char *
43     GetPluginDescriptionStatic();
44 
45     static lldb_private::SymbolFile *
46     CreateInstance (lldb_private::ObjectFile* obj_file);
47 
48     //------------------------------------------------------------------
49     // Constructors and Destructors
50     //------------------------------------------------------------------
51     SymbolFileDWARFDebugMap (lldb_private::ObjectFile* ofile);
52     ~SymbolFileDWARFDebugMap () override;
53 
54     uint32_t        CalculateAbilities () override;
55     void            InitializeObject() override;
56 
57     //------------------------------------------------------------------
58     // Compile Unit function calls
59     //------------------------------------------------------------------
60     uint32_t        GetNumCompileUnits () override;
61     lldb::CompUnitSP ParseCompileUnitAtIndex (uint32_t index) override;
62 
63     lldb::LanguageType ParseCompileUnitLanguage (const lldb_private::SymbolContext& sc) override;
64     size_t          ParseCompileUnitFunctions (const lldb_private::SymbolContext& sc) override;
65     bool            ParseCompileUnitLineTable (const lldb_private::SymbolContext& sc) override;
66     bool            ParseCompileUnitSupportFiles (const lldb_private::SymbolContext& sc, lldb_private::FileSpecList &support_files) override;
67     bool            ParseImportedModules (const lldb_private::SymbolContext &sc, std::vector<lldb_private::ConstString> &imported_modules) override;
68     size_t          ParseFunctionBlocks (const lldb_private::SymbolContext& sc) override;
69     size_t          ParseTypes (const lldb_private::SymbolContext& sc) override;
70     size_t          ParseVariablesForContext (const lldb_private::SymbolContext& sc) override;
71 
72     lldb_private::Type* ResolveTypeUID (lldb::user_id_t type_uid) override;
73     lldb_private::CompilerDeclContext GetDeclContextForUID (lldb::user_id_t uid) override;
74     lldb_private::CompilerDeclContext GetDeclContextContainingUID (lldb::user_id_t uid) override;
75     void ParseDeclsForContext (lldb_private::CompilerDeclContext decl_ctx) override;
76 
77     bool            CompleteType (lldb_private::CompilerType& compiler_type) override;
78     uint32_t        ResolveSymbolContext (const lldb_private::Address& so_addr, uint32_t resolve_scope, lldb_private::SymbolContext& sc) override;
79     uint32_t        ResolveSymbolContext (const lldb_private::FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, lldb_private::SymbolContextList& sc_list) override;
80     uint32_t        FindGlobalVariables (const lldb_private::ConstString &name, const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, uint32_t max_matches, lldb_private::VariableList& variables) override;
81     uint32_t        FindGlobalVariables (const lldb_private::RegularExpression& regex, bool append, uint32_t max_matches, lldb_private::VariableList& variables) override;
82     uint32_t        FindFunctions (const lldb_private::ConstString &name, const lldb_private::CompilerDeclContext *parent_decl_ctx, uint32_t name_type_mask, bool include_inlines, bool append, lldb_private::SymbolContextList& sc_list) override;
83     uint32_t        FindFunctions (const lldb_private::RegularExpression& regex, bool include_inlines, bool append, lldb_private::SymbolContextList& sc_list) override;
84     uint32_t        FindTypes (const lldb_private::SymbolContext& sc, const lldb_private::ConstString &name, const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, uint32_t max_matches, lldb_private::TypeMap& types) override;
85     lldb_private::CompilerDeclContext
86                     FindNamespace (const lldb_private::SymbolContext& sc,
87                                    const lldb_private::ConstString &name,
88                                    const lldb_private::CompilerDeclContext *parent_decl_ctx) override;
89     size_t          GetTypes (lldb_private::SymbolContextScope *sc_scope,
90                               uint32_t type_mask,
91                               lldb_private::TypeList &type_list) override;
92 
93     //------------------------------------------------------------------
94     // PluginInterface protocol
95     //------------------------------------------------------------------
96     lldb_private::ConstString
97     GetPluginName() override;
98 
99     uint32_t
100     GetPluginVersion() override;
101 
102 protected:
103     enum
104     {
105         kHaveInitializedOSOs = (1 << 0),
106         kNumFlags
107     };
108 
109     friend class DWARFCompileUnit;
110     friend class SymbolFileDWARF;
111     friend class DebugMapModule;
112     friend class DWARFASTParserClang;
113     struct OSOInfo
114     {
115         lldb::ModuleSP module_sp;
116 
117         OSOInfo() :
118             module_sp ()
119         {
120         }
121     };
122 
123     typedef std::shared_ptr<OSOInfo> OSOInfoSP;
124 
125     typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, lldb::addr_t> FileRangeMap;
126 
127     //------------------------------------------------------------------
128     // Class specific types
129     //------------------------------------------------------------------
130     struct CompileUnitInfo
131     {
132         lldb_private::FileSpec so_file;
133         lldb_private::ConstString oso_path;
134         lldb_private::TimeValue oso_mod_time;
135         OSOInfoSP oso_sp;
136         lldb::CompUnitSP compile_unit_sp;
137         uint32_t first_symbol_index;
138         uint32_t last_symbol_index;
139         uint32_t first_symbol_id;
140         uint32_t last_symbol_id;
141         FileRangeMap file_range_map;
142         bool file_range_map_valid;
143 
144 
145         CompileUnitInfo() :
146             so_file (),
147             oso_path (),
148             oso_mod_time (),
149             oso_sp (),
150             compile_unit_sp (),
151             first_symbol_index (UINT32_MAX),
152             last_symbol_index (UINT32_MAX),
153             first_symbol_id (UINT32_MAX),
154             last_symbol_id (UINT32_MAX),
155             file_range_map (),
156             file_range_map_valid (false)
157         {
158         }
159 
160         const FileRangeMap &
161         GetFileRangeMap(SymbolFileDWARFDebugMap *exe_symfile);
162     };
163 
164     //------------------------------------------------------------------
165     // Protected Member Functions
166     //------------------------------------------------------------------
167     void
168     InitOSO ();
169 
170     static uint32_t
171     GetOSOIndexFromUserID (lldb::user_id_t uid)
172     {
173         return (uint32_t)((uid >> 32ull) - 1ull);
174     }
175 
176     static SymbolFileDWARF *
177     GetSymbolFileAsSymbolFileDWARF (SymbolFile *sym_file);
178 
179     bool
180     GetFileSpecForSO (uint32_t oso_idx, lldb_private::FileSpec &file_spec);
181 
182     CompileUnitInfo *
183     GetCompUnitInfo (const lldb_private::SymbolContext& sc);
184 
185     size_t
186     GetCompUnitInfosForModule (const lldb_private::Module *oso_module,
187                                std::vector<CompileUnitInfo *>& cu_infos);
188 
189     lldb_private::Module *
190     GetModuleByCompUnitInfo (CompileUnitInfo *comp_unit_info);
191 
192     lldb_private::Module *
193     GetModuleByOSOIndex (uint32_t oso_idx);
194 
195     lldb_private::ObjectFile *
196     GetObjectFileByCompUnitInfo (CompileUnitInfo *comp_unit_info);
197 
198     lldb_private::ObjectFile *
199     GetObjectFileByOSOIndex (uint32_t oso_idx);
200 
201     uint32_t
202     GetCompUnitInfoIndex (const CompileUnitInfo *comp_unit_info);
203 
204     SymbolFileDWARF *
205     GetSymbolFile (const lldb_private::SymbolContext& sc);
206 
207     SymbolFileDWARF *
208     GetSymbolFileByCompUnitInfo (CompileUnitInfo *comp_unit_info);
209 
210     SymbolFileDWARF *
211     GetSymbolFileByOSOIndex (uint32_t oso_idx);
212 
213     // If closure returns "false", iteration continues.  If it returns
214     // "true", iteration terminates.
215     void
216     ForEachSymbolFile (std::function<bool (SymbolFileDWARF *)> closure)
217     {
218         for (uint32_t oso_idx = 0, num_oso_idxs = m_compile_unit_infos.size();
219              oso_idx < num_oso_idxs;
220              ++oso_idx)
221         {
222             if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx))
223             {
224                 if (closure(oso_dwarf))
225                     return;
226             }
227         }
228     }
229 
230     CompileUnitInfo *
231     GetCompileUnitInfoForSymbolWithIndex (uint32_t symbol_idx, uint32_t *oso_idx_ptr);
232 
233     CompileUnitInfo *
234     GetCompileUnitInfoForSymbolWithID (lldb::user_id_t symbol_id, uint32_t *oso_idx_ptr);
235 
236     static int
237     SymbolContainsSymbolWithIndex (uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info);
238 
239     static int
240     SymbolContainsSymbolWithID (lldb::user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info);
241 
242     uint32_t
243     PrivateFindGlobalVariables (const lldb_private::ConstString &name,
244                                 const lldb_private::CompilerDeclContext *parent_decl_ctx,
245                                 const std::vector<uint32_t> &name_symbol_indexes,
246                                 uint32_t max_matches,
247                                 lldb_private::VariableList& variables);
248 
249 
250     void
251     SetCompileUnit (SymbolFileDWARF *oso_dwarf, const lldb::CompUnitSP &cu_sp);
252 
253     lldb::CompUnitSP
254     GetCompileUnit (SymbolFileDWARF *oso_dwarf);
255 
256     CompileUnitInfo *
257     GetCompileUnitInfo (SymbolFileDWARF *oso_dwarf);
258 
259     lldb::TypeSP
260     FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &die_decl_ctx);
261 
262     bool
263     Supports_DW_AT_APPLE_objc_complete_type (SymbolFileDWARF *skip_dwarf_oso);
264 
265     lldb::TypeSP
266     FindCompleteObjCDefinitionTypeForDIE (const DWARFDIE &die,
267                                           const lldb_private::ConstString &type_name,
268                                           bool must_be_implementation);
269 
270 
271     UniqueDWARFASTTypeMap &
272     GetUniqueDWARFASTTypeMap ()
273     {
274         return m_unique_ast_type_map;
275     }
276 
277 
278     //------------------------------------------------------------------
279     // OSOEntry
280     //------------------------------------------------------------------
281     class OSOEntry
282     {
283     public:
284 
285         OSOEntry () :
286         m_exe_sym_idx (UINT32_MAX),
287         m_oso_file_addr (LLDB_INVALID_ADDRESS)
288         {
289         }
290 
291         OSOEntry (uint32_t exe_sym_idx,
292                   lldb::addr_t oso_file_addr) :
293         m_exe_sym_idx (exe_sym_idx),
294         m_oso_file_addr (oso_file_addr)
295         {
296         }
297 
298         uint32_t
299         GetExeSymbolIndex () const
300         {
301             return m_exe_sym_idx;
302         }
303 
304         bool
305         operator < (const OSOEntry &rhs) const
306         {
307             return m_exe_sym_idx < rhs.m_exe_sym_idx;
308         }
309 
310         lldb::addr_t
311         GetOSOFileAddress () const
312         {
313             return m_oso_file_addr;
314         }
315 
316         void
317         SetOSOFileAddress (lldb::addr_t oso_file_addr)
318         {
319             m_oso_file_addr = oso_file_addr;
320         }
321     protected:
322         uint32_t m_exe_sym_idx;
323         lldb::addr_t m_oso_file_addr;
324     };
325 
326     typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, OSOEntry> DebugMap;
327 
328     //------------------------------------------------------------------
329     // Member Variables
330     //------------------------------------------------------------------
331     std::bitset<kNumFlags> m_flags;
332     std::vector<CompileUnitInfo> m_compile_unit_infos;
333     std::vector<uint32_t> m_func_indexes;   // Sorted by address
334     std::vector<uint32_t> m_glob_indexes;
335     std::map<lldb_private::ConstString, OSOInfoSP> m_oso_map;
336     UniqueDWARFASTTypeMap m_unique_ast_type_map;
337     lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type;
338     DebugMap m_debug_map;
339 
340     //------------------------------------------------------------------
341     // When an object file from the debug map gets parsed in
342     // SymbolFileDWARF, it needs to tell the debug map about the object
343     // files addresses by calling this function once for each N_FUN,
344     // N_GSYM and N_STSYM and after all entries in the debug map have
345     // been matched up, FinalizeOSOFileRanges() should be called.
346     //------------------------------------------------------------------
347     bool
348     AddOSOFileRange (CompileUnitInfo *cu_info,
349                      lldb::addr_t exe_file_addr,
350                      lldb::addr_t oso_file_addr,
351                      lldb::addr_t oso_byte_size);
352 
353     //------------------------------------------------------------------
354     // Called after calling AddOSOFileRange() for each object file debug
355     // map entry to finalize the info for the unlinked compile unit.
356     //------------------------------------------------------------------
357     void
358     FinalizeOSOFileRanges (CompileUnitInfo *cu_info);
359 
360     //------------------------------------------------------------------
361     /// Convert \a addr from a .o file address, to an executable address.
362     ///
363     /// @param[in] addr
364     ///     A section offset address from a .o file
365     ///
366     /// @return
367     ///     Returns true if \a addr was converted to be an executable
368     ///     section/offset address, false otherwise.
369     //------------------------------------------------------------------
370     bool
371     LinkOSOAddress (lldb_private::Address &addr);
372 
373     //------------------------------------------------------------------
374     /// Convert a .o file "file address" to an executable "file address".
375     ///
376     /// @param[in] oso_symfile
377     ///     The DWARF symbol file that contains \a oso_file_addr
378     ///
379     /// @param[in] oso_file_addr
380     ///     A .o file "file address" to convert.
381     ///
382     /// @return
383     ///     LLDB_INVALID_ADDRESS if \a oso_file_addr is not in the
384     ///     linked executable, otherwise a valid "file address" from the
385     ///     linked executable that contains the debug map.
386     //------------------------------------------------------------------
387     lldb::addr_t
388     LinkOSOFileAddress (SymbolFileDWARF *oso_symfile, lldb::addr_t oso_file_addr);
389 
390     //------------------------------------------------------------------
391     /// Given a line table full of lines with "file addresses" that are
392     /// for a .o file represented by \a oso_symfile, link a new line table
393     /// and return it.
394     ///
395     /// @param[in] oso_symfile
396     ///     The DWARF symbol file that produced the \a line_table
397     ///
398     /// @param[in] addr
399     ///     A section offset address from a .o file
400     ///
401     /// @return
402     ///     Returns a valid line table full of linked addresses, or NULL
403     ///     if none of the line table addresses exist in the main
404     ///     executable.
405     //------------------------------------------------------------------
406     lldb_private::LineTable *
407     LinkOSOLineTable (SymbolFileDWARF *oso_symfile,
408                       lldb_private::LineTable *line_table);
409 
410     size_t
411     AddOSOARanges (SymbolFileDWARF* dwarf2Data,
412                    DWARFDebugAranges* debug_aranges);
413 };
414 
415 #endif // #ifndef SymbolFileDWARF_SymbolFileDWARFDebugMap_h_
416