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 
GetPluginNameStatic()27   static llvm::StringRef GetPluginNameStatic() { return "hexagon-dyld"; }
28 
29   static llvm::StringRef 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
GetPluginName()50   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
51 
52 protected:
53   /// Runtime linker rendezvous structure.
54   HexagonDYLDRendezvous m_rendezvous;
55 
56   /// Virtual load address of the inferior process.
57   lldb::addr_t m_load_offset;
58 
59   /// Virtual entry address of the inferior process.
60   lldb::addr_t m_entry_point;
61 
62   /// Rendezvous breakpoint.
63   lldb::break_id_t m_dyld_bid;
64 
65   /// Loaded module list. (link map for each module)
66   std::map<lldb::ModuleWP, lldb::addr_t, std::owner_less<lldb::ModuleWP>>
67       m_loaded_modules;
68 
69   /// Enables a breakpoint on a function called by the runtime
70   /// linker each time a module is loaded or unloaded.
71   bool SetRendezvousBreakpoint();
72 
73   /// Callback routine which updates the current list of loaded modules based
74   /// on the information supplied by the runtime linker.
75   static bool RendezvousBreakpointHit(
76       void *baton, lldb_private::StoppointCallbackContext *context,
77       lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
78 
79   /// Helper method for RendezvousBreakpointHit.  Updates LLDB's current set
80   /// of loaded modules.
81   void RefreshModules();
82 
83   /// Updates the load address of every allocatable section in \p module.
84   ///
85   /// \param module The module to traverse.
86   ///
87   /// \param link_map_addr The virtual address of the link map for the @p
88   /// module.
89   ///
90   /// \param base_addr The virtual base address \p module is loaded at.
91   void UpdateLoadedSections(lldb::ModuleSP module, lldb::addr_t link_map_addr,
92                             lldb::addr_t base_addr,
93                             bool base_addr_is_offset) override;
94 
95   /// Removes the loaded sections from the target in \p module.
96   ///
97   /// \param module The module to traverse.
98   void UnloadSections(const lldb::ModuleSP module) override;
99 
100   /// Callback routine invoked when we hit the breakpoint on process entry.
101   ///
102   /// This routine is responsible for resolving the load addresses of all
103   /// dependent modules required by the inferior and setting up the rendezvous
104   /// breakpoint.
105   static bool
106   EntryBreakpointHit(void *baton,
107                      lldb_private::StoppointCallbackContext *context,
108                      lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
109 
110   /// Helper for the entry breakpoint callback.  Resolves the load addresses
111   /// of all dependent modules.
112   void LoadAllCurrentModules();
113 
114   /// Computes a value for m_load_offset returning the computed address on
115   /// success and LLDB_INVALID_ADDRESS on failure.
116   lldb::addr_t ComputeLoadOffset();
117 
118   /// Computes a value for m_entry_point returning the computed address on
119   /// success and LLDB_INVALID_ADDRESS on failure.
120   lldb::addr_t GetEntryPoint();
121 
122   /// Checks to see if the target module has changed, updates the target
123   /// accordingly and returns the target executable module.
124   lldb::ModuleSP GetTargetExecutable();
125 
126   /// return the address of the Rendezvous breakpoint
127   lldb::addr_t FindRendezvousBreakpointAddress();
128 
129 private:
130   const lldb_private::SectionList *
131   GetSectionListFromModule(const lldb::ModuleSP module) const;
132 };
133 
134 #endif // LLDB_SOURCE_PLUGINS_DYNAMICLOADER_HEXAGON_DYLD_DYNAMICLOADERHEXAGONDYLD_H
135