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