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