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();
32*fb0b7583SSean Callanan         if ((os_type == llvm::Triple::UnknownOS))
33fc7117aeSGreg Clayton             create = true;
34fc7117aeSGreg Clayton     }
35fc7117aeSGreg Clayton 
36fc7117aeSGreg Clayton     if (create)
37fc7117aeSGreg Clayton         return new DynamicLoaderStatic (process);
38fc7117aeSGreg Clayton     return NULL;
39fc7117aeSGreg Clayton }
40fc7117aeSGreg Clayton 
41fc7117aeSGreg Clayton //----------------------------------------------------------------------
42fc7117aeSGreg Clayton // Constructor
43fc7117aeSGreg Clayton //----------------------------------------------------------------------
44fc7117aeSGreg Clayton DynamicLoaderStatic::DynamicLoaderStatic (Process* process) :
45fc7117aeSGreg Clayton     DynamicLoader(process)
46fc7117aeSGreg Clayton {
47fc7117aeSGreg Clayton }
48fc7117aeSGreg Clayton 
49fc7117aeSGreg Clayton //----------------------------------------------------------------------
50fc7117aeSGreg Clayton // Destructor
51fc7117aeSGreg Clayton //----------------------------------------------------------------------
52fc7117aeSGreg Clayton DynamicLoaderStatic::~DynamicLoaderStatic()
53fc7117aeSGreg Clayton {
54fc7117aeSGreg Clayton }
55fc7117aeSGreg Clayton 
56fc7117aeSGreg Clayton //------------------------------------------------------------------
57fc7117aeSGreg Clayton /// Called after attaching a process.
58fc7117aeSGreg Clayton ///
59fc7117aeSGreg Clayton /// Allow DynamicLoader plug-ins to execute some code after
60fc7117aeSGreg Clayton /// attaching to a process.
61fc7117aeSGreg Clayton //------------------------------------------------------------------
62fc7117aeSGreg Clayton void
63fc7117aeSGreg Clayton DynamicLoaderStatic::DidAttach ()
64fc7117aeSGreg Clayton {
65fc7117aeSGreg Clayton     LoadAllImagesAtFileAddresses();
66fc7117aeSGreg Clayton }
67fc7117aeSGreg Clayton 
68fc7117aeSGreg Clayton //------------------------------------------------------------------
69fc7117aeSGreg Clayton /// Called after attaching a process.
70fc7117aeSGreg Clayton ///
71fc7117aeSGreg Clayton /// Allow DynamicLoader plug-ins to execute some code after
72fc7117aeSGreg Clayton /// attaching to a process.
73fc7117aeSGreg Clayton //------------------------------------------------------------------
74fc7117aeSGreg Clayton void
75fc7117aeSGreg Clayton DynamicLoaderStatic::DidLaunch ()
76fc7117aeSGreg Clayton {
77fc7117aeSGreg Clayton     LoadAllImagesAtFileAddresses();
78fc7117aeSGreg Clayton }
79fc7117aeSGreg Clayton 
80fc7117aeSGreg Clayton void
81fc7117aeSGreg Clayton DynamicLoaderStatic::LoadAllImagesAtFileAddresses ()
82fc7117aeSGreg Clayton {
83fc7117aeSGreg Clayton     ModuleList &module_list = m_process->GetTarget().GetImages();
84fc7117aeSGreg Clayton 
85fc7117aeSGreg Clayton     ModuleList loaded_module_list;
86fc7117aeSGreg Clayton 
87fc7117aeSGreg Clayton     const size_t num_modules = module_list.GetSize();
88fc7117aeSGreg Clayton     for (uint32_t idx = 0; idx < num_modules; ++idx)
89fc7117aeSGreg Clayton     {
90fc7117aeSGreg Clayton         ModuleSP module_sp (module_list.GetModuleAtIndex (idx));
91fc7117aeSGreg Clayton         if (module_sp)
92fc7117aeSGreg Clayton         {
93fc7117aeSGreg Clayton             bool changed = false;
94fc7117aeSGreg Clayton             ObjectFile *image_object_file = module_sp->GetObjectFile();
95fc7117aeSGreg Clayton             if (image_object_file)
96fc7117aeSGreg Clayton             {
97fc7117aeSGreg Clayton                 SectionList *section_list = image_object_file->GetSectionList ();
98fc7117aeSGreg Clayton                 if (section_list)
99fc7117aeSGreg Clayton                 {
100fc7117aeSGreg Clayton                     // All sections listed in the dyld image info structure will all
101fc7117aeSGreg Clayton                     // either be fixed up already, or they will all be off by a single
102fc7117aeSGreg Clayton                     // slide amount that is determined by finding the first segment
103fc7117aeSGreg Clayton                     // that is at file offset zero which also has bytes (a file size
104fc7117aeSGreg Clayton                     // that is greater than zero) in the object file.
105fc7117aeSGreg Clayton 
106fc7117aeSGreg Clayton                     // Determine the slide amount (if any)
107fc7117aeSGreg Clayton                     const size_t num_sections = section_list->GetSize();
108fc7117aeSGreg Clayton                     size_t sect_idx = 0;
109fc7117aeSGreg Clayton                     for (sect_idx = 0; sect_idx < num_sections; ++sect_idx)
110fc7117aeSGreg Clayton                     {
111fc7117aeSGreg Clayton                         // Iterate through the object file sections to find the
112fc7117aeSGreg Clayton                         // first section that starts of file offset zero and that
113fc7117aeSGreg Clayton                         // has bytes in the file...
114fc7117aeSGreg Clayton                         Section *section = section_list->GetSectionAtIndex (sect_idx).get();
115fc7117aeSGreg Clayton                         if (section)
116fc7117aeSGreg Clayton                         {
117fc7117aeSGreg Clayton                             if (m_process->GetTarget().GetSectionLoadList().SetSectionLoadAddress (section, section->GetFileAddress()))
118fc7117aeSGreg Clayton                                 changed = true;
119fc7117aeSGreg Clayton                         }
120fc7117aeSGreg Clayton                     }
121fc7117aeSGreg Clayton                 }
122fc7117aeSGreg Clayton             }
123fc7117aeSGreg Clayton 
124fc7117aeSGreg Clayton             if (changed)
125fc7117aeSGreg Clayton                 loaded_module_list.AppendIfNeeded (module_sp);
126fc7117aeSGreg Clayton         }
127fc7117aeSGreg Clayton     }
128fc7117aeSGreg Clayton 
129fc7117aeSGreg Clayton     if (loaded_module_list.GetSize())
130fc7117aeSGreg Clayton         m_process->GetTarget().ModulesDidLoad (loaded_module_list);
131fc7117aeSGreg Clayton }
132fc7117aeSGreg Clayton 
133fc7117aeSGreg Clayton ThreadPlanSP
134fc7117aeSGreg Clayton DynamicLoaderStatic::GetStepThroughTrampolinePlan (Thread &thread, bool stop_others)
135fc7117aeSGreg Clayton {
136fc7117aeSGreg Clayton     return ThreadPlanSP();
137fc7117aeSGreg Clayton }
138fc7117aeSGreg Clayton 
139fc7117aeSGreg Clayton Error
140fc7117aeSGreg Clayton DynamicLoaderStatic::CanLoadImage ()
141fc7117aeSGreg Clayton {
142fc7117aeSGreg Clayton     Error error;
143fc7117aeSGreg Clayton     error.SetErrorString ("can't load images on with a static debug session");
144fc7117aeSGreg Clayton     return error;
145fc7117aeSGreg Clayton }
146fc7117aeSGreg Clayton 
147fc7117aeSGreg Clayton void
148fc7117aeSGreg Clayton DynamicLoaderStatic::Initialize()
149fc7117aeSGreg Clayton {
150fc7117aeSGreg Clayton     PluginManager::RegisterPlugin (GetPluginNameStatic(),
151fc7117aeSGreg Clayton                                    GetPluginDescriptionStatic(),
152fc7117aeSGreg Clayton                                    CreateInstance);
153fc7117aeSGreg Clayton }
154fc7117aeSGreg Clayton 
155fc7117aeSGreg Clayton void
156fc7117aeSGreg Clayton DynamicLoaderStatic::Terminate()
157fc7117aeSGreg Clayton {
158fc7117aeSGreg Clayton     PluginManager::UnregisterPlugin (CreateInstance);
159fc7117aeSGreg Clayton }
160fc7117aeSGreg Clayton 
161fc7117aeSGreg Clayton 
162fc7117aeSGreg Clayton const char *
163fc7117aeSGreg Clayton DynamicLoaderStatic::GetPluginNameStatic()
164fc7117aeSGreg Clayton {
165fc7117aeSGreg Clayton     return "dynamic-loader.static";
166fc7117aeSGreg Clayton }
167fc7117aeSGreg Clayton 
168fc7117aeSGreg Clayton const char *
169fc7117aeSGreg Clayton DynamicLoaderStatic::GetPluginDescriptionStatic()
170fc7117aeSGreg Clayton {
171fc7117aeSGreg Clayton     return "Dynamic loader plug-in that will load any images at the static addresses contained in each image.";
172fc7117aeSGreg Clayton }
173fc7117aeSGreg Clayton 
174fc7117aeSGreg Clayton 
175fc7117aeSGreg Clayton //------------------------------------------------------------------
176fc7117aeSGreg Clayton // PluginInterface protocol
177fc7117aeSGreg Clayton //------------------------------------------------------------------
178fc7117aeSGreg Clayton const char *
179fc7117aeSGreg Clayton DynamicLoaderStatic::GetPluginName()
180fc7117aeSGreg Clayton {
181fc7117aeSGreg Clayton     return "DynamicLoaderStatic";
182fc7117aeSGreg Clayton }
183fc7117aeSGreg Clayton 
184fc7117aeSGreg Clayton const char *
185fc7117aeSGreg Clayton DynamicLoaderStatic::GetShortPluginName()
186fc7117aeSGreg Clayton {
187fc7117aeSGreg Clayton     return GetPluginNameStatic();
188fc7117aeSGreg Clayton }
189fc7117aeSGreg Clayton 
190fc7117aeSGreg Clayton uint32_t
191fc7117aeSGreg Clayton DynamicLoaderStatic::GetPluginVersion()
192fc7117aeSGreg Clayton {
193fc7117aeSGreg Clayton     return 1;
194fc7117aeSGreg Clayton }
195fc7117aeSGreg Clayton 
196