1 //===-- DynamicLoaderHexagonDYLD.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_HEXAGON_DYLD_DYNAMICLOADERHEXAGONDYLD_H
10 #define LLDB_SOURCE_PLUGINS_DYNAMICLOADER_HEXAGON_DYLD_DYNAMICLOADERHEXAGONDYLD_H
11 
12 #include "lldb/Breakpoint/StoppointCallbackContext.h"
13 #include "lldb/Target/DynamicLoader.h"
14 
15 #include "HexagonDYLDRendezvous.h"
16 
17 class DynamicLoaderHexagonDYLD : public lldb_private::DynamicLoader {
18 public:
19   DynamicLoaderHexagonDYLD(lldb_private::Process *process);
20 
21   ~DynamicLoaderHexagonDYLD() override;
22 
23   static void Initialize();
24 
25   static void Terminate();
26 
27   static lldb_private::ConstString GetPluginNameStatic();
28 
29   static const char *GetPluginDescriptionStatic();
30 
31   static lldb_private::DynamicLoader *
32   CreateInstance(lldb_private::Process *process, bool force);
33 
34   // DynamicLoader protocol
35 
36   void DidAttach() override;
37 
38   void DidLaunch() override;
39 
40   lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread,
41                                                   bool stop_others) override;
42 
43   lldb_private::Status CanLoadImage() override;
44 
45   lldb::addr_t GetThreadLocalData(const lldb::ModuleSP module,
46                                   const lldb::ThreadSP thread,
47                                   lldb::addr_t tls_file_addr) override;
48 
49   // PluginInterface protocol
50   lldb_private::ConstString GetPluginName() override;
51 
52   uint32_t GetPluginVersion() override;
53 
54 protected:
55   /// Runtime linker rendezvous structure.
56   HexagonDYLDRendezvous m_rendezvous;
57 
58   /// Virtual load address of the inferior process.
59   lldb::addr_t m_load_offset;
60 
61   /// Virtual entry address of the inferior process.
62   lldb::addr_t m_entry_point;
63 
64   /// Rendezvous breakpoint.
65   lldb::break_id_t m_dyld_bid;
66 
67   /// Loaded module list. (link map for each module)
68   std::map<lldb::ModuleWP, lldb::addr_t, std::owner_less<lldb::ModuleWP>>
69       m_loaded_modules;
70 
71   /// Enables a breakpoint on a function called by the runtime
72   /// linker each time a module is loaded or unloaded.
73   bool SetRendezvousBreakpoint();
74 
75   /// Callback routine which updates the current list of loaded modules based
76   /// on the information supplied by the runtime linker.
77   static bool RendezvousBreakpointHit(
78       void *baton, lldb_private::StoppointCallbackContext *context,
79       lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
80 
81   /// Helper method for RendezvousBreakpointHit.  Updates LLDB's current set
82   /// of loaded modules.
83   void RefreshModules();
84 
85   /// Updates the load address of every allocatable section in \p module.
86   ///
87   /// \param module The module to traverse.
88   ///
89   /// \param link_map_addr The virtual address of the link map for the @p
90   /// module.
91   ///
92   /// \param base_addr The virtual base address \p module is loaded at.
93   void UpdateLoadedSections(lldb::ModuleSP module, lldb::addr_t link_map_addr,
94                             lldb::addr_t base_addr,
95                             bool base_addr_is_offset) override;
96 
97   /// Removes the loaded sections from the target in \p module.
98   ///
99   /// \param module The module to traverse.
100   void UnloadSections(const lldb::ModuleSP module) override;
101 
102   /// Callback routine invoked when we hit the breakpoint on process entry.
103   ///
104   /// This routine is responsible for resolving the load addresses of all
105   /// dependent modules required by the inferior and setting up the rendezvous
106   /// breakpoint.
107   static bool
108   EntryBreakpointHit(void *baton,
109                      lldb_private::StoppointCallbackContext *context,
110                      lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
111 
112   /// Helper for the entry breakpoint callback.  Resolves the load addresses
113   /// of all dependent modules.
114   void LoadAllCurrentModules();
115 
116   /// Computes a value for m_load_offset returning the computed address on
117   /// success and LLDB_INVALID_ADDRESS on failure.
118   lldb::addr_t ComputeLoadOffset();
119 
120   /// Computes a value for m_entry_point returning the computed address on
121   /// success and LLDB_INVALID_ADDRESS on failure.
122   lldb::addr_t GetEntryPoint();
123 
124   /// Checks to see if the target module has changed, updates the target
125   /// accordingly and returns the target executable module.
126   lldb::ModuleSP GetTargetExecutable();
127 
128   /// return the address of the Rendezvous breakpoint
129   lldb::addr_t FindRendezvousBreakpointAddress();
130 
131 private:
132   const lldb_private::SectionList *
133   GetSectionListFromModule(const lldb::ModuleSP module) const;
134 };
135 
136 #endif // LLDB_SOURCE_PLUGINS_DYNAMICLOADER_HEXAGON_DYLD_DYNAMICLOADERHEXAGONDYLD_H
137