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