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 liblldb_DynamicLoaderPOSIXDYLD_h_
10 #define liblldb_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 
33   static lldb_private::ConstString GetPluginNameStatic();
34 
35   static const char *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
56   lldb_private::ConstString GetPluginName() override;
57 
58   uint32_t GetPluginVersion() override;
59 
60 protected:
61   /// Runtime linker rendezvous structure.
62   DYLDRendezvous m_rendezvous;
63 
64   /// Virtual load address of the inferior process.
65   lldb::addr_t m_load_offset;
66 
67   /// Virtual entry address of the inferior process.
68   lldb::addr_t m_entry_point;
69 
70   /// Auxiliary vector of the inferior process.
71   std::unique_ptr<AuxVector> m_auxv;
72 
73   /// Rendezvous breakpoint.
74   lldb::break_id_t m_dyld_bid;
75 
76   /// Contains AT_SYSINFO_EHDR, which means a vDSO has been
77   /// mapped to the address space
78   lldb::addr_t m_vdso_base;
79 
80   /// Contains AT_BASE, which means a dynamic loader has been
81   /// mapped to the address space
82   lldb::addr_t m_interpreter_base;
83 
84   /// Loaded module list. (link map for each module)
85   std::map<lldb::ModuleWP, lldb::addr_t, std::owner_less<lldb::ModuleWP>>
86       m_loaded_modules;
87 
88   /// If possible sets a breakpoint on a function called by the runtime
89   /// linker each time a module is loaded or unloaded.
90   bool SetRendezvousBreakpoint();
91 
92   /// Callback routine which updates the current list of loaded modules based
93   /// on the information supplied by the runtime linker.
94   static bool RendezvousBreakpointHit(
95       void *baton, lldb_private::StoppointCallbackContext *context,
96       lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
97 
98   /// Helper method for RendezvousBreakpointHit.  Updates LLDB's current set
99   /// of loaded modules.
100   void RefreshModules();
101 
102   /// Updates the load address of every allocatable section in \p module.
103   ///
104   /// \param module The module to traverse.
105   ///
106   /// \param link_map_addr The virtual address of the link map for the @p
107   /// module.
108   ///
109   /// \param base_addr The virtual base address \p module is loaded at.
110   void UpdateLoadedSections(lldb::ModuleSP module, lldb::addr_t link_map_addr,
111                             lldb::addr_t base_addr,
112                             bool base_addr_is_offset) override;
113 
114   /// Removes the loaded sections from the target in \p module.
115   ///
116   /// \param module The module to traverse.
117   void UnloadSections(const lldb::ModuleSP module) override;
118 
119   /// Resolves the entry point for the current inferior process and sets a
120   /// breakpoint at that address.
121   void ProbeEntry();
122 
123   /// Callback routine invoked when we hit the breakpoint on process entry.
124   ///
125   /// This routine is responsible for resolving the load addresses of all
126   /// dependent modules required by the inferior and setting up the rendezvous
127   /// breakpoint.
128   static bool
129   EntryBreakpointHit(void *baton,
130                      lldb_private::StoppointCallbackContext *context,
131                      lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
132 
133   /// Helper for the entry breakpoint callback.  Resolves the load addresses
134   /// of all dependent modules.
135   virtual void LoadAllCurrentModules();
136 
137   void LoadVDSO();
138 
139   // Loading an interpreter module (if present) assumming m_interpreter_base
140   // already points to its base address.
141   lldb::ModuleSP LoadInterpreterModule();
142 
143   /// Computes a value for m_load_offset returning the computed address on
144   /// success and LLDB_INVALID_ADDRESS on failure.
145   lldb::addr_t ComputeLoadOffset();
146 
147   /// Computes a value for m_entry_point returning the computed address on
148   /// success and LLDB_INVALID_ADDRESS on failure.
149   lldb::addr_t GetEntryPoint();
150 
151   /// Evaluate if Aux vectors contain vDSO and LD information
152   /// in case they do, read and assign the address to m_vdso_base
153   /// and m_interpreter_base.
154   void EvalSpecialModulesStatus();
155 
156   /// Loads Module from inferior process.
157   void ResolveExecutableModule(lldb::ModuleSP &module_sp);
158 
159   bool AlwaysRelyOnEHUnwindInfo(lldb_private::SymbolContext &sym_ctx) override;
160 
161 private:
162   DISALLOW_COPY_AND_ASSIGN(DynamicLoaderPOSIXDYLD);
163 };
164 
165 #endif // liblldb_DynamicLoaderPOSIXDYLD_h_
166