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