1 //===-- DynamicLoaderStatic.cpp ---------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Core/Module.h"
11 #include "lldb/Core/PluginManager.h"
12 #include "lldb/Target/Target.h"
13 
14 #include "DynamicLoaderStatic.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 //----------------------------------------------------------------------
20 // Create an instance of this class. This function is filled into
21 // the plugin info class that gets handed out by the plugin factory and
22 // allows the lldb to instantiate an instance of this class.
23 //----------------------------------------------------------------------
24 DynamicLoader *
25 DynamicLoaderStatic::CreateInstance (Process* process, bool force)
26 {
27     bool create = force;
28     if (!create)
29     {
30         const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
31         const llvm::Triple::OSType os_type = triple_ref.getOS();
32         if ((os_type == llvm::Triple::UnknownOS))
33             create = true;
34     }
35 
36     if (create)
37         return new DynamicLoaderStatic (process);
38     return NULL;
39 }
40 
41 //----------------------------------------------------------------------
42 // Constructor
43 //----------------------------------------------------------------------
44 DynamicLoaderStatic::DynamicLoaderStatic (Process* process) :
45     DynamicLoader(process)
46 {
47 }
48 
49 //----------------------------------------------------------------------
50 // Destructor
51 //----------------------------------------------------------------------
52 DynamicLoaderStatic::~DynamicLoaderStatic()
53 {
54 }
55 
56 //------------------------------------------------------------------
57 /// Called after attaching a process.
58 ///
59 /// Allow DynamicLoader plug-ins to execute some code after
60 /// attaching to a process.
61 //------------------------------------------------------------------
62 void
63 DynamicLoaderStatic::DidAttach ()
64 {
65     LoadAllImagesAtFileAddresses();
66 }
67 
68 //------------------------------------------------------------------
69 /// Called after attaching a process.
70 ///
71 /// Allow DynamicLoader plug-ins to execute some code after
72 /// attaching to a process.
73 //------------------------------------------------------------------
74 void
75 DynamicLoaderStatic::DidLaunch ()
76 {
77     LoadAllImagesAtFileAddresses();
78 }
79 
80 void
81 DynamicLoaderStatic::LoadAllImagesAtFileAddresses ()
82 {
83     ModuleList &module_list = m_process->GetTarget().GetImages();
84 
85     ModuleList loaded_module_list;
86 
87     const size_t num_modules = module_list.GetSize();
88     for (uint32_t idx = 0; idx < num_modules; ++idx)
89     {
90         ModuleSP module_sp (module_list.GetModuleAtIndex (idx));
91         if (module_sp)
92         {
93             bool changed = false;
94             ObjectFile *image_object_file = module_sp->GetObjectFile();
95             if (image_object_file)
96             {
97                 SectionList *section_list = image_object_file->GetSectionList ();
98                 if (section_list)
99                 {
100                     // All sections listed in the dyld image info structure will all
101                     // either be fixed up already, or they will all be off by a single
102                     // slide amount that is determined by finding the first segment
103                     // that is at file offset zero which also has bytes (a file size
104                     // that is greater than zero) in the object file.
105 
106                     // Determine the slide amount (if any)
107                     const size_t num_sections = section_list->GetSize();
108                     size_t sect_idx = 0;
109                     for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
110                     {
111                         // Iterate through the object file sections to find the
112                         // first section that starts of file offset zero and that
113                         // has bytes in the file...
114                         Section *section = section_list->GetSectionAtIndex (sect_idx).get();
115                         if (section)
116                         {
117                             if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress()))
118                                 changed = true;
119                         }
120                     }
121                 }
122             }
123 
124             if (changed)
125                 loaded_module_list.AppendIfNeeded (module_sp);
126         }
127     }
128 
129     if (loaded_module_list.GetSize())
130         m_process->GetTarget().ModulesDidLoad (loaded_module_list);
131 }
132 
133 ThreadPlanSP
134 DynamicLoaderStatic::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
135 {
136     return ThreadPlanSP();
137 }
138 
139 Error
140 DynamicLoaderStatic::CanLoadImage ()
141 {
142     Error error;
143     error.SetErrorString ("can't load images on with a static debug session");
144     return error;
145 }
146 
147 void
148 DynamicLoaderStatic::Initialize()
149 {
150     PluginManager::RegisterPlugin (GetPluginNameStatic(),
151                                    GetPluginDescriptionStatic(),
152                                    CreateInstance);
153 }
154 
155 void
156 DynamicLoaderStatic::Terminate()
157 {
158     PluginManager::UnregisterPlugin (CreateInstance);
159 }
160 
161 
162 const char *
163 DynamicLoaderStatic::GetPluginNameStatic()
164 {
165     return "dynamic-loader.static";
166 }
167 
168 const char *
169 DynamicLoaderStatic::GetPluginDescriptionStatic()
170 {
171     return "Dynamic loader plug-in that will load any images at the static addresses contained in each image.";
172 }
173 
174 
175 //------------------------------------------------------------------
176 // PluginInterface protocol
177 //------------------------------------------------------------------
178 const char *
179 DynamicLoaderStatic::GetPluginName()
180 {
181     return "DynamicLoaderStatic";
182 }
183 
184 const char *
185 DynamicLoaderStatic::GetShortPluginName()
186 {
187     return GetPluginNameStatic();
188 }
189 
190 uint32_t
191 DynamicLoaderStatic::GetPluginVersion()
192 {
193     return 1;
194 }
195 
196