blob: f27f0a19d5d76750ae87bbe7ad9b0fb9672b4e6b [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"
Greg Clayton1f746072012-08-29 21:13:06 +000012#include "lldb/Core/Section.h"
13#include "lldb/Symbol/ObjectFile.h"
Greg Claytonfc7117a2011-03-05 01:04:56 +000014#include "lldb/Target/Target.h"
15
16#include "DynamicLoaderStatic.h"
17
18using namespace lldb;
19using 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//----------------------------------------------------------------------
26DynamicLoader *
27DynamicLoaderStatic::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();
Sean Callananfb0b7582011-03-15 00:17:19 +000034 if ((os_type == llvm::Triple::UnknownOS))
Greg Claytonfc7117a2011-03-05 01:04:56 +000035 create = true;
36 }
37
Sean Callanan49bce8e2012-02-10 20:22:35 +000038 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
Greg Claytonfc7117a2011-03-05 01:04:56 +000051 if (create)
52 return new DynamicLoaderStatic (process);
53 return NULL;
54}
55
56//----------------------------------------------------------------------
57// Constructor
58//----------------------------------------------------------------------
59DynamicLoaderStatic::DynamicLoaderStatic (Process* process) :
60 DynamicLoader(process)
61{
62}
63
64//----------------------------------------------------------------------
65// Destructor
66//----------------------------------------------------------------------
67DynamicLoaderStatic::~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//------------------------------------------------------------------
77void
78DynamicLoaderStatic::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//------------------------------------------------------------------
89void
90DynamicLoaderStatic::DidLaunch ()
91{
92 LoadAllImagesAtFileAddresses();
93}
94
95void
96DynamicLoaderStatic::LoadAllImagesAtFileAddresses ()
97{
Enrico Granata17598482012-11-08 02:22:02 +000098 const ModuleList &module_list = m_process->GetTarget().GetImages();
Greg Claytonfc7117a2011-03-05 01:04:56 +000099
100 ModuleList loaded_module_list;
101
Jim Ingham3ee12ef2012-05-30 02:19:25 +0000102 Mutex::Locker mutex_locker(module_list.GetMutex());
103
Greg Claytonfc7117a2011-03-05 01:04:56 +0000104 const size_t num_modules = module_list.GetSize();
105 for (uint32_t idx = 0; idx < num_modules; ++idx)
106 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +0000107 ModuleSP module_sp (module_list.GetModuleAtIndexUnlocked (idx));
Greg Claytonfc7117a2011-03-05 01:04:56 +0000108 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...
Greg Clayton7820bd12012-07-07 01:24:12 +0000131 SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx));
132 if (section_sp)
Greg Claytonfc7117a2011-03-05 01:04:56 +0000133 {
Greg Clayton7820bd12012-07-07 01:24:12 +0000134 if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress()))
Greg Claytonfc7117a2011-03-05 01:04:56 +0000135 changed = true;
136 }
137 }
138 }
139 }
140
141 if (changed)
142 loaded_module_list.AppendIfNeeded (module_sp);
143 }
144 }
145
Enrico Granata17598482012-11-08 02:22:02 +0000146 m_process->GetTarget().ModulesDidLoad (loaded_module_list);
Greg Claytonfc7117a2011-03-05 01:04:56 +0000147}
148
149ThreadPlanSP
150DynamicLoaderStatic::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
151{
152 return ThreadPlanSP();
153}
154
155Error
156DynamicLoaderStatic::CanLoadImage ()
157{
158 Error error;
159 error.SetErrorString ("can't load images on with a static debug session");
160 return error;
161}
162
163void
164DynamicLoaderStatic::Initialize()
165{
166 PluginManager::RegisterPlugin (GetPluginNameStatic(),
167 GetPluginDescriptionStatic(),
168 CreateInstance);
169}
170
171void
172DynamicLoaderStatic::Terminate()
173{
174 PluginManager::UnregisterPlugin (CreateInstance);
175}
176
177
178const char *
179DynamicLoaderStatic::GetPluginNameStatic()
180{
181 return "dynamic-loader.static";
182}
183
184const char *
185DynamicLoaderStatic::GetPluginDescriptionStatic()
186{
187 return "Dynamic loader plug-in that will load any images at the static addresses contained in each image.";
188}
189
190
191//------------------------------------------------------------------
192// PluginInterface protocol
193//------------------------------------------------------------------
194const char *
195DynamicLoaderStatic::GetPluginName()
196{
197 return "DynamicLoaderStatic";
198}
199
200const char *
201DynamicLoaderStatic::GetShortPluginName()
202{
203 return GetPluginNameStatic();
204}
205
206uint32_t
207DynamicLoaderStatic::GetPluginVersion()
208{
209 return 1;
210}
211