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