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     {
38         Module *exe_module = process->GetTarget().GetExecutableModulePointer();
39         if (exe_module)
40         {
41             ObjectFile *object_file = exe_module->GetObjectFile();
42             if (object_file)
43             {
44                 create = (object_file->GetStrata() == ObjectFile::eStrataRawImage);
45             }
46         }
47     }
48 
49     if (create)
50         return new DynamicLoaderStatic (process);
51     return NULL;
52 }
53 
54 //----------------------------------------------------------------------
55 // Constructor
56 //----------------------------------------------------------------------
57 DynamicLoaderStatic::DynamicLoaderStatic (Process* process) :
58     DynamicLoader(process)
59 {
60 }
61 
62 //----------------------------------------------------------------------
63 // Destructor
64 //----------------------------------------------------------------------
65 DynamicLoaderStatic::~DynamicLoaderStatic()
66 {
67 }
68 
69 //------------------------------------------------------------------
70 /// Called after attaching a process.
71 ///
72 /// Allow DynamicLoader plug-ins to execute some code after
73 /// attaching to a process.
74 //------------------------------------------------------------------
75 void
76 DynamicLoaderStatic::DidAttach ()
77 {
78     LoadAllImagesAtFileAddresses();
79 }
80 
81 //------------------------------------------------------------------
82 /// Called after attaching a process.
83 ///
84 /// Allow DynamicLoader plug-ins to execute some code after
85 /// attaching to a process.
86 //------------------------------------------------------------------
87 void
88 DynamicLoaderStatic::DidLaunch ()
89 {
90     LoadAllImagesAtFileAddresses();
91 }
92 
93 void
94 DynamicLoaderStatic::LoadAllImagesAtFileAddresses ()
95 {
96     ModuleList &module_list = m_process->GetTarget().GetImages();
97 
98     ModuleList loaded_module_list;
99 
100     const size_t num_modules = module_list.GetSize();
101     for (uint32_t idx = 0; idx < num_modules; ++idx)
102     {
103         ModuleSP module_sp (module_list.GetModuleAtIndex (idx));
104         if (module_sp)
105         {
106             bool changed = false;
107             ObjectFile *image_object_file = module_sp->GetObjectFile();
108             if (image_object_file)
109             {
110                 SectionList *section_list = image_object_file->GetSectionList ();
111                 if (section_list)
112                 {
113                     // All sections listed in the dyld image info structure will all
114                     // either be fixed up already, or they will all be off by a single
115                     // slide amount that is determined by finding the first segment
116                     // that is at file offset zero which also has bytes (a file size
117                     // that is greater than zero) in the object file.
118 
119                     // Determine the slide amount (if any)
120                     const size_t num_sections = section_list->GetSize();
121                     size_t sect_idx = 0;
122                     for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
123                     {
124                         // Iterate through the object file sections to find the
125                         // first section that starts of file offset zero and that
126                         // has bytes in the file...
127                         Section *section = section_list->GetSectionAtIndex (sect_idx).get();
128                         if (section)
129                         {
130                             if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress()))
131                                 changed = true;
132                         }
133                     }
134                 }
135             }
136 
137             if (changed)
138                 loaded_module_list.AppendIfNeeded (module_sp);
139         }
140     }
141 
142     if (loaded_module_list.GetSize())
143         m_process->GetTarget().ModulesDidLoad (loaded_module_list);
144 }
145 
146 ThreadPlanSP
147 DynamicLoaderStatic::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
148 {
149     return ThreadPlanSP();
150 }
151 
152 Error
153 DynamicLoaderStatic::CanLoadImage ()
154 {
155     Error error;
156     error.SetErrorString ("can't load images on with a static debug session");
157     return error;
158 }
159 
160 void
161 DynamicLoaderStatic::Initialize()
162 {
163     PluginManager::RegisterPlugin (GetPluginNameStatic(),
164                                    GetPluginDescriptionStatic(),
165                                    CreateInstance);
166 }
167 
168 void
169 DynamicLoaderStatic::Terminate()
170 {
171     PluginManager::UnregisterPlugin (CreateInstance);
172 }
173 
174 
175 const char *
176 DynamicLoaderStatic::GetPluginNameStatic()
177 {
178     return "dynamic-loader.static";
179 }
180 
181 const char *
182 DynamicLoaderStatic::GetPluginDescriptionStatic()
183 {
184     return "Dynamic loader plug-in that will load any images at the static addresses contained in each image.";
185 }
186 
187 
188 //------------------------------------------------------------------
189 // PluginInterface protocol
190 //------------------------------------------------------------------
191 const char *
192 DynamicLoaderStatic::GetPluginName()
193 {
194     return "DynamicLoaderStatic";
195 }
196 
197 const char *
198 DynamicLoaderStatic::GetShortPluginName()
199 {
200     return GetPluginNameStatic();
201 }
202 
203 uint32_t
204 DynamicLoaderStatic::GetPluginVersion()
205 {
206     return 1;
207 }
208 
209