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