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