xref: /freebsd/contrib/llvm-project/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- DynamicLoaderPOSIXDYLD.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 LLDB_SOURCE_PLUGINS_DYNAMICLOADER_POSIX_DYLD_DYNAMICLOADERPOSIXDYLD_H
10 #define LLDB_SOURCE_PLUGINS_DYNAMICLOADER_POSIX_DYLD_DYNAMICLOADERPOSIXDYLD_H
11 
12 #include <map>
13 #include <memory>
14 
15 #include "DYLDRendezvous.h"
16 #include "Plugins/Process/Utility/AuxVector.h"
17 #include "lldb/Breakpoint/StoppointCallbackContext.h"
18 #include "lldb/Core/ModuleList.h"
19 #include "lldb/Target/DynamicLoader.h"
20 
21 class AuxVector;
22 
23 class DynamicLoaderPOSIXDYLD : public lldb_private::DynamicLoader {
24 public:
25   DynamicLoaderPOSIXDYLD(lldb_private::Process *process);
26 
27   ~DynamicLoaderPOSIXDYLD() override;
28 
29   static void Initialize();
30 
31   static void Terminate();
32 
GetPluginNameStatic()33   static llvm::StringRef GetPluginNameStatic() { return "posix-dyld"; }
34 
35   static llvm::StringRef GetPluginDescriptionStatic();
36 
37   static lldb_private::DynamicLoader *
38   CreateInstance(lldb_private::Process *process, bool force);
39 
40   // DynamicLoader protocol
41 
42   void DidAttach() override;
43 
44   void DidLaunch() override;
45 
46   lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread,
47                                                   bool stop_others) override;
48 
49   lldb_private::Status CanLoadImage() override;
50 
51   lldb::addr_t GetThreadLocalData(const lldb::ModuleSP module,
52                                   const lldb::ThreadSP thread,
53                                   lldb::addr_t tls_file_addr) override;
54 
55   // PluginInterface protocol
GetPluginName()56   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
57 
58   lldb::ModuleSP LoadModuleAtAddress(const lldb_private::FileSpec &file,
59                                      lldb::addr_t link_map_addr,
60                                      lldb::addr_t base_addr,
61                                      bool base_addr_is_offset) override;
62 
63   void CalculateDynamicSaveCoreRanges(
64       lldb_private::Process &process,
65       std::vector<lldb_private::MemoryRegionInfo> &ranges,
66       llvm::function_ref<bool(const lldb_private::Thread &)>
67           save_thread_predicate) override;
68 
69 protected:
70   /// Runtime linker rendezvous structure.
71   DYLDRendezvous m_rendezvous;
72 
73   /// Virtual load address of the inferior process.
74   lldb::addr_t m_load_offset;
75 
76   /// Virtual entry address of the inferior process.
77   lldb::addr_t m_entry_point;
78 
79   /// Auxiliary vector of the inferior process.
80   std::unique_ptr<AuxVector> m_auxv;
81 
82   /// Rendezvous breakpoint.
83   lldb::break_id_t m_dyld_bid;
84 
85   /// Contains AT_SYSINFO_EHDR, which means a vDSO has been
86   /// mapped to the address space
87   lldb::addr_t m_vdso_base;
88 
89   /// Contains AT_BASE, which means a dynamic loader has been
90   /// mapped to the address space
91   lldb::addr_t m_interpreter_base;
92 
93   /// Contains the pointer to the interpret module, if loaded.
94   std::weak_ptr<lldb_private::Module> m_interpreter_module;
95 
96   /// Returns true if the process is for a core file.
97   bool IsCoreFile() const;
98 
99   /// If possible sets a breakpoint on a function called by the runtime
100   /// linker each time a module is loaded or unloaded.
101   bool SetRendezvousBreakpoint();
102 
103   /// Callback routine which updates the current list of loaded modules based
104   /// on the information supplied by the runtime linker.
105   static bool RendezvousBreakpointHit(
106       void *baton, lldb_private::StoppointCallbackContext *context,
107       lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
108 
109   /// Indicates whether the initial set of modules was reported added.
110   bool m_initial_modules_added;
111 
112   /// Helper method for RendezvousBreakpointHit.  Updates LLDB's current set
113   /// of loaded modules.
114   void RefreshModules();
115 
116   /// Updates the load address of every allocatable section in \p module.
117   ///
118   /// \param module The module to traverse.
119   ///
120   /// \param link_map_addr The virtual address of the link map for the @p
121   /// module.
122   ///
123   /// \param base_addr The virtual base address \p module is loaded at.
124   void UpdateLoadedSections(lldb::ModuleSP module, lldb::addr_t link_map_addr,
125                             lldb::addr_t base_addr,
126                             bool base_addr_is_offset) override;
127 
128   /// Removes the loaded sections from the target in \p module.
129   ///
130   /// \param module The module to traverse.
131   void UnloadSections(const lldb::ModuleSP module) override;
132 
133   /// Resolves the entry point for the current inferior process and sets a
134   /// breakpoint at that address.
135   void ProbeEntry();
136 
137   /// Callback routine invoked when we hit the breakpoint on process entry.
138   ///
139   /// This routine is responsible for resolving the load addresses of all
140   /// dependent modules required by the inferior and setting up the rendezvous
141   /// breakpoint.
142   static bool
143   EntryBreakpointHit(void *baton,
144                      lldb_private::StoppointCallbackContext *context,
145                      lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
146 
147   /// Helper for the entry breakpoint callback.  Resolves the load addresses
148   /// of all dependent modules.
149   virtual void LoadAllCurrentModules();
150 
151   void LoadVDSO();
152 
153   // Loading an interpreter module (if present) assuming m_interpreter_base
154   // already points to its base address.
155   lldb::ModuleSP LoadInterpreterModule();
156 
157   /// Computes a value for m_load_offset returning the computed address on
158   /// success and LLDB_INVALID_ADDRESS on failure.
159   lldb::addr_t ComputeLoadOffset();
160 
161   /// Computes a value for m_entry_point returning the computed address on
162   /// success and LLDB_INVALID_ADDRESS on failure.
163   lldb::addr_t GetEntryPoint();
164 
165   /// Evaluate if Aux vectors contain vDSO and LD information
166   /// in case they do, read and assign the address to m_vdso_base
167   /// and m_interpreter_base.
168   void EvalSpecialModulesStatus();
169 
170   /// Loads Module from inferior process.
171   void ResolveExecutableModule(lldb::ModuleSP &module_sp);
172 
173   bool AlwaysRelyOnEHUnwindInfo(lldb_private::SymbolContext &sym_ctx) override;
174 
175 private:
176   DynamicLoaderPOSIXDYLD(const DynamicLoaderPOSIXDYLD &) = delete;
177   const DynamicLoaderPOSIXDYLD &
178   operator=(const DynamicLoaderPOSIXDYLD &) = delete;
179 
180   /// Loaded module list. (link map for each module)
181   /// This may be accessed in a multi-threaded context. Use the accessor methods
182   /// to access `m_loaded_modules` safely.
183   std::map<lldb::ModuleWP, lldb::addr_t, std::owner_less<lldb::ModuleWP>>
184       m_loaded_modules;
185   llvm::sys::RWMutex m_loaded_modules_rw_mutex;
186 
187   void SetLoadedModule(const lldb::ModuleSP &module_sp,
188                        lldb::addr_t link_map_addr);
189   void UnloadModule(const lldb::ModuleSP &module_sp);
190   std::optional<lldb::addr_t>
191   GetLoadedModuleLinkAddr(const lldb::ModuleSP &module_sp);
192 };
193 
194 #endif // LLDB_SOURCE_PLUGINS_DYNAMICLOADER_POSIX_DYLD_DYNAMICLOADERPOSIXDYLD_H
195