blob: a1d64422e600efa27dbfca3609107928073e2e44 [file] [log] [blame]
Greg Claytonfc7117a2011-03-05 01:04:56 +00001//===-- 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
16using namespace lldb;
17using 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//----------------------------------------------------------------------
24DynamicLoader *
25DynamicLoaderStatic::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();
Sean Callananfb0b7582011-03-15 00:17:19 +000032 if ((os_type == llvm::Triple::UnknownOS))
Greg Claytonfc7117a2011-03-05 01:04:56 +000033 create = true;
34 }
35
Sean Callanan49bce8e2012-02-10 20:22:35 +000036 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
Greg Claytonfc7117a2011-03-05 01:04:56 +000049 if (create)
50 return new DynamicLoaderStatic (process);
51 return NULL;
52}
53
54//----------------------------------------------------------------------
55// Constructor
56//----------------------------------------------------------------------
57DynamicLoaderStatic::DynamicLoaderStatic (Process* process) :
58 DynamicLoader(process)
59{
60}
61
62//----------------------------------------------------------------------
63// Destructor
64//----------------------------------------------------------------------
65DynamicLoaderStatic::~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//------------------------------------------------------------------
75void
76DynamicLoaderStatic::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//------------------------------------------------------------------
87void
88DynamicLoaderStatic::DidLaunch ()
89{
90 LoadAllImagesAtFileAddresses();
91}
92
93void
94DynamicLoaderStatic::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
146ThreadPlanSP
147DynamicLoaderStatic::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
148{
149 return ThreadPlanSP();
150}
151
152Error
153DynamicLoaderStatic::CanLoadImage ()
154{
155 Error error;
156 error.SetErrorString ("can't load images on with a static debug session");
157 return error;
158}
159
160void
161DynamicLoaderStatic::Initialize()
162{
163 PluginManager::RegisterPlugin (GetPluginNameStatic(),
164 GetPluginDescriptionStatic(),
165 CreateInstance);
166}
167
168void
169DynamicLoaderStatic::Terminate()
170{
171 PluginManager::UnregisterPlugin (CreateInstance);
172}
173
174
175const char *
176DynamicLoaderStatic::GetPluginNameStatic()
177{
178 return "dynamic-loader.static";
179}
180
181const char *
182DynamicLoaderStatic::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//------------------------------------------------------------------
191const char *
192DynamicLoaderStatic::GetPluginName()
193{
194 return "DynamicLoaderStatic";
195}
196
197const char *
198DynamicLoaderStatic::GetShortPluginName()
199{
200 return GetPluginNameStatic();
201}
202
203uint32_t
204DynamicLoaderStatic::GetPluginVersion()
205{
206 return 1;
207}
208