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